From 4bd204aa8e36b8cbebf65a43ed7bb6f39c600840 Mon Sep 17 00:00:00 2001 From: Stefan Behnel <stefan_ml@behnel.de> Date: Fri, 14 Nov 2014 20:51:16 +0100 Subject: [PATCH] reference naming conflict resolution from C++ wrapping doc, show how to wrap C++ objects in cdef class attributes --- docs/src/userguide/external_C_code.rst | 2 ++ docs/src/userguide/wrapping_CPlusPlus.rst | 31 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/docs/src/userguide/external_C_code.rst b/docs/src/userguide/external_C_code.rst index 8f7fb909d..fa39d35ed 100644 --- a/docs/src/userguide/external_C_code.rst +++ b/docs/src/userguide/external_C_code.rst @@ -214,6 +214,8 @@ If ``__stdcall`` is used, the function is only considered compatible with other ``__stdcall`` functions of the same signature. +.. _resolve-conflicts: + Resolving naming conflicts - C name specifications -------------------------------------------------- diff --git a/docs/src/userguide/wrapping_CPlusPlus.rst b/docs/src/userguide/wrapping_CPlusPlus.rst index 5119b63b9..2ba508683 100644 --- a/docs/src/userguide/wrapping_CPlusPlus.rst +++ b/docs/src/userguide/wrapping_CPlusPlus.rst @@ -283,6 +283,9 @@ attribute access, you could just implement some properties:: def __set__(self, x0): self.thisptr.x0 = x0 ... +If you prefer giving the same name to the wrapper as the C++ class, see the +section on :ref:`resolving naming conflicts <resolve-conflicts>`. + Advanced C++ features ====================== @@ -477,6 +480,34 @@ The items in the containers are converted to a corresponding type automatically, which includes recursively converting containers inside of containers, e.g. a C++ vector of maps of strings. + +Simplified wrapping with default constructor +-------------------------------------------- + +If your extension type instantiates a wrapped C++ class using the default +constructor (not passing any arguments), you may be able to simplify the +lifecycle handling by tying it directly to the lifetime of the Python wrapper +object. Instead of a pointer attribute, you can declare an instance:: + + cdef class VectorStack: + cdef vector[int] v + + def push(self, x): + self.v.push_back(x) + + def pop(self): + if self.v.empty(): + raise IndexError() + x = self.v.back() + self.v.pop_back() + return x + +Cython will automatically generate code that instantiates the C++ object +instance when the Python object is created and deletes it when the Python +object is garbage collected. + + + Exceptions ----------- -- 2.30.9