From b7805af02c9e24f02042f2f024f8c186449b6cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ullrich=20K=C3=B6the?= Date: Tue, 21 Nov 2000 11:15:30 +0000 Subject: [PATCH] Renamed ExtensionClass::register_coerce() into ExtensionClass::def_standard_coerce() and made it public. Added ClassWrapper::def_standard_coerce(). [SVN r8272] --- class_wrapper.h | 4 ++ extclass.h | 14 +++--- gen_extclass.py | 11 ++-- release_notes.txt | 2 + special.html | 126 ++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 135 insertions(+), 22 deletions(-) diff --git a/class_wrapper.h b/class_wrapper.h index 85e2de79..f7955729 100644 --- a/class_wrapper.h +++ b/class_wrapper.h @@ -92,6 +92,10 @@ class ClassWrapper void def_read_write(MemberType T::*pm, const char* name) { m_class->def_read_write(pm, name); } + // define the standard coercion needed for operator overloading + void def_standard_coerce() + { m_class->def_standard_coerce(); } + // declare the given class a base class of this one and register // conversion functions template diff --git a/extclass.h b/extclass.h index 55d83e21..d0a09bf6 100644 --- a/extclass.h +++ b/extclass.h @@ -480,7 +480,10 @@ class ExtensionClass this->def_getter(pm, name); this->def_setter(pm, name); } - + + // define the standard coercion needed for operator overloading + void def_standard_coerce(); + // declare the given class a base class of this one and register // up and down conversion functions template @@ -526,7 +529,7 @@ class ExtensionClass template inline void def_operators(operators) { - register_coerce(); + def_standard_coerce(); // for some strange reason, this prevents MSVC from having an // "unrecoverable block scoping error"! @@ -558,7 +561,7 @@ class ExtensionClass template inline void def_operators(operators, right_operand) { - register_coerce(); + def_standard_coerce(); choose_op<(which & op_add)>::template args::add(this); choose_op<(which & op_sub)>::template args::add(this); @@ -578,7 +581,7 @@ class ExtensionClass template inline void def_operators(operators, left_operand) { - register_coerce(); + def_standard_coerce(); choose_rop<(which & op_add)>::template args::add(this); choose_rop<(which & op_sub)>::template args::add(this); @@ -601,7 +604,6 @@ class ExtensionClass this->add_constructor_object(InitFunction::create(sig)); } - void register_coerce(); }; // A simple wrapper over a T which allows us to use ExtensionClass with a @@ -736,7 +738,7 @@ ExtensionClass::ExtensionClass(const char* name) } template -void ExtensionClass::register_coerce() +void ExtensionClass::def_standard_coerce() { Ptr coerce_fct = dict().get_item(String("__coerce__")); diff --git a/gen_extclass.py b/gen_extclass.py index 6de88b92..2a7b6be2 100644 --- a/gen_extclass.py +++ b/gen_extclass.py @@ -542,6 +542,9 @@ class ExtensionClass this->def_setter(pm, name); } + // define the standard coercion needed for operator overloading + void def_standard_coerce(); + // declare the given class a base class of this one and register // up and down conversion functions template @@ -587,7 +590,7 @@ class ExtensionClass template inline void def_operators(operators) { - register_coerce(); + def_standard_coerce(); detail::choose_op<(which & op_add)>::template args::add(this); detail::choose_op<(which & op_sub)>::template args::add(this); @@ -615,7 +618,7 @@ class ExtensionClass template inline void def_operators(operators, right_operand) { - register_coerce(); + def_standard_coerce(); detail::choose_op<(which & op_add)>::template args::add(this); detail::choose_op<(which & op_sub)>::template args::add(this); @@ -635,7 +638,7 @@ class ExtensionClass template inline void def_operators(operators, left_operand) { - register_coerce(); + def_standard_coerce(); detail::choose_rop<(which & op_add)>::template args::add(this); detail::choose_rop<(which & op_sub)>::template args::add(this); @@ -657,8 +660,6 @@ class ExtensionClass { this->add_constructor_object(InitFunction::create(sig)); } - - void register_coerce(); }; // A simple wrapper over a T which allows us to use ExtensionClass with a diff --git a/release_notes.txt b/release_notes.txt index 707e226f..cd0b99b1 100644 --- a/release_notes.txt +++ b/release_notes.txt @@ -1,4 +1,6 @@ 2000-11-20 10:00 + Ullrich renamed ExtensionClass:register_coerce() into + ExtensionClass:def_standard_coerce() and made it public Ullrich improved shared_pod_manager. diff --git a/special.html b/special.html index 13bc9617..d225451c 100644 --- a/special.html +++ b/special.html @@ -13,18 +13,122 @@ Overview

- Py_cpp supports all of the standard - special method names supported by real Python class instances - except: -

- - (more on the reasons below). So, for example, we can wrap a + special method names supported by real Python class instances except + __complex__ (more on the reasons below). + + +

Numeric Operators

+ +There are two fundamental ways to define numeric operators within py_cpp: automatic wrapping +and manual wrapping. Suppose, C++ defines an addition operator for type Rational, so that we can write: + +
+    Rational a, b, c;
+    ...
+    c = a + b;
+
+ +To enable the same functionality in Python, we first wrap the Rational class as usual: + +
+    py::ClassWrapper<Rational> rational_class(my_module, "Rational");
+    rational_class.def(py::Constructor<>());
+    ...
+
+ +Then we export the addition operator like this: + +
+    rational_class.def(py::operators<py::op_add>());
+
+ +Since Rational also supports subtraction, multiplication, adn division, we want to export those also. This can be done in a single command by 'or'ing the operator identifiers together (a complete list of these identifiers and the corresponding operators can be found in the table): + +
+    rational_class.def(py::operators<(py::op_sub | py::op_mul | py::op_div)>());
+
+ +Note that the or-expression must be enclosed in parentheses. This form of operator definition will wrap homogeneous operators, that is operators whose left and right operand have the same type. Now, suppose that our C++ library also supports addition of Rationals and integers: + +
+    Rational a, b;
+    int i;
+    ...
+    a = b + i;
+    a = i + b;
+
+ +To wrap these heterogeneous operators (left and right hand side have different types), we need a possibility to specify a different operand type. This is done using the right_operand and left_operand templates: + +
+    rational_class.def(py::operators<py::op_add>(), py::right_operand<int>());
+    rational_class.def(py::operators<py::op_add>(), py::left_operand<int>());
+
+ +Py_cpp uses overloading to register several variants of the same operation (more on this in the context of coercion). Again, several operators can be exported at once: + +
+    rational_class.def(py::operators<(py::op_sub | py::op_mul | py::op_div)>(),
+                       py::right_operand<int>());
+    rational_class.def(py::operators<(py::op_sub | py::op_mul | py::op_div)>(), 
+                       py::left_operand<int>());
+
+ + +The type of the operand not mentioned is taken from the class object. In our example, the class object is rational_class, and thus the other operand's type is `Rational const &'. You can override this default by explicitly specifying a type in the operators template: + +
+    rational_class.def(py::operators<py::op_add, Rational>(), py::right_operand<int>());
+
+ +Here, `Rational' would be used instead of `Rational const &'. +

+Note that automatic wrapping doesn't need any specific form of operator+() (or any other operator), but rather wraps the expression `left + right'. That is, this mechanism can be used for any definition of operator+(), such as a free function `Rational operator+(Rational, Rational)' or a member function `Rational Rational::operator+(Rational)'. + +

+In some cases, automatic wrapping of operators is not possible or not desirable. Suppose, for example, that the power operation for Rationals is defined by a set of functions pow(): + +

+    Rational pow(Rational const & left, Rational const & right);
+    Rational pow(Rational const & left, int right);
+    Rational pow(int left, Rational const & right);
+
+ +In order to create the Python operator "pow" from these functions, we have to wrap them manually: + +
+    rational_class.def((Rational (*)(Rational const &, Rational const &))&pow, "__pow__");
+    rational_class.def((Rational (*)(Rational const &, int))&pow, "__pow__");
+
+ +The third form (with int as left operand) cannot be wrapped this way. We must first create a function rpow() with the operands reversed: + +
+    Rational rpow(Rational const & right, int left)
+    {
+        return pow(left, right);
+    }
+
+ +This function must be wrapped under the name "__rpow__": + +
+    rational_class.def(&rpow,  "__rpow__");
+
+ +A list of the possible operator names is also found in the table. +Special treatment is necessary to define the ternary pow. +

+Automatic and manual wrapping can be mixed arbitrarily. + + +

Coercion

+ + + So, for example, we can wrap a std::map<std::size_t,std::string> as follows:

Example