// (C) Copyright David Abrahams 2000. Permission to copy, use, modify, sell and // distribute this software is granted provided this copyright notice appears // in all copies. This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. // // The author gratefully acknowleges the support of Dragon Systems, Inc., in // producing this work. // // This file automatically generated for 10-argument constructors by // gen_extclass.python #ifndef EXTENSION_CLASS_DWA052000_H_ # define EXTENSION_CLASS_DWA052000_H_ # include "pyconfig.h" # include "subclass.h" # include # include "none.h" # include "objects.h" # include "functions.h" # include # include "init_function.h" # include # include namespace python { // forward declarations template struct operators; template struct left_operand; template struct right_operand; enum without_downcast_t { without_downcast }; namespace detail { // forward declarations class extension_instance; class extension_class_base; template class instance_holder; template class instance_value_holder; template class instance_ptr_holder; template struct operand_select; template struct choose_op; template struct choose_rop; template struct choose_unary_op; template struct define_operator; meta_class* extension_meta_class(); extension_instance* get_extension_instance(PyObject* p); void report_missing_instance_data(extension_instance*, class_t*, const std::type_info&); void report_missing_ptr_data(extension_instance*, class_t*, const std::type_info&); void report_missing_class_object(const std::type_info&); void report_released_smart_pointer(const std::type_info&); template T* check_non_null(T* p) { if (p == 0) report_released_smart_pointer(typeid(T)); return p; } template class held_instance; typedef void* (*ConversionFunction)(void*); struct BaseClassInfo { BaseClassInfo(extension_class_base* t, ConversionFunction f) :class_object(t), convert(f) {} extension_class_base* class_object; ConversionFunction convert; }; typedef BaseClassInfo derived_class_info; struct add_operator_base; class extension_class_base : public class_t { public: extension_class_base(const char* name); public: // the purpose of try_class_conversions() and its related functions // is explained in extclass.cpp void* try_class_conversions(instance_holder_base*) const; void* try_base_class_conversions(instance_holder_base*) const; void* try_derived_class_conversions(instance_holder_base*) const; void set_attribute(const char* name, PyObject* x); void set_attribute(const char* name, ref x); private: virtual void* extract_object_from_holder(instance_holder_base* v) const = 0; virtual std::vector const& base_classes() const = 0; virtual std::vector const& derived_classes() const = 0; protected: friend struct add_operator_base; void add_method(reference method, const char* name); void add_method(function* method, const char* name); void add_constructor_object(function*); void add_setter_method(function*, const char* name); void add_getter_method(function*, const char* name); }; template class class_registry { public: static extension_class_base* class_object() { return static_class_object; } // Register/unregister the Python class object corresponding to T static void register_class(extension_class_base*); static void unregister_class(extension_class_base*); // Establish C++ inheritance relationships static void register_base_class(BaseClassInfo const&); static void register_derived_class(derived_class_info const&); // Query the C++ inheritance relationships static std::vector const& base_classes(); static std::vector const& derived_classes(); private: static extension_class_base* static_class_object; static std::vector static_base_class_info; static std::vector static_derived_class_info; }; }} // namespace python::detail BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // This class' only job is to define from_python and to_python converters for T // and U. T is the class the user really intends to wrap. U is a class derived // from T with some virtual function overriding boilerplate, or if there are no // virtual functions, U = held_instance. template > class PyExtensionClassConverters { public: // Get an object which can be used to convert T to/from python. This is used // as a kind of concept check by the global template // // PyObject* to_python(const T& x) // // below this class, to prevent the confusing messages that would otherwise // pop up. Now, if T hasn't been wrapped as an extension class, the user // will see an error message about the lack of an eligible // py_extension_class_converters() function. friend PyExtensionClassConverters py_extension_class_converters(python::type) { return PyExtensionClassConverters(); } // This is a member function because in a conforming implementation, friend // funcitons defined inline in the class body are all instantiated as soon // as the enclosing class is instantiated. If T is not copyable, that causes // a compiler error. Instead, we access this function through the global // template // // PyObject* to_python(const T& x) // // defined below this class. Since template functions are instantiated only // on demand, errors will be avoided unless T is noncopyable and the user // writes code which causes us to try to copy a T. PyObject* to_python(const T& x) const { python::reference result(create_instance()); result->add_implementation( std::auto_ptr( new python::detail::instance_value_holder(result.get(), x))); return result.release(); } // Convert to T* friend T* from_python(PyObject* obj, python::type) { // downcast to an extension_instance, then find the actual T python::detail::extension_instance* self = python::detail::get_extension_instance(obj); typedef std::vector::const_iterator iterator; for (iterator p = self->wrapped_objects().begin(); p != self->wrapped_objects().end(); ++p) { python::detail::instance_holder* held = dynamic_cast*>(*p); if (held != 0) return held->target(); // see extclass.cpp for an explanation of try_class_conversions() void* target = python::detail::class_registry::class_object()->try_class_conversions(*p); if(target) return static_cast(target); } python::detail::report_missing_instance_data(self, python::detail::class_registry::class_object(), typeid(T)); throw python::argument_error(); } // Convert to PtrType, where PtrType can be dereferenced to obtain a T. template static PtrType& ptr_from_python(PyObject* obj, python::type) { // downcast to an extension_instance, then find the actual T python::detail::extension_instance* self = python::detail::get_extension_instance(obj); typedef std::vector::const_iterator iterator; for (iterator p = self->wrapped_objects().begin(); p != self->wrapped_objects().end(); ++p) { python::detail::instance_ptr_holder* held = dynamic_cast*>(*p); if (held != 0) return held->ptr(); } python::detail::report_missing_ptr_data(self, python::detail::class_registry::class_object(), typeid(T)); throw python::argument_error(); } template static PyObject* ptr_to_python(PtrType x) { python::reference result(create_instance()); result->add_implementation( std::auto_ptr( new python::detail::instance_ptr_holder(x))); return result.release(); } static python::reference create_instance() { PyTypeObject* class_object = python::detail::class_registry::class_object(); if (class_object == 0) python::detail::report_missing_class_object(typeid(T)); return python::reference( new python::detail::extension_instance(class_object)); } // Convert to const T* friend const T* from_python(PyObject* p, python::type) { return from_python(p, python::type()); } // Convert to const T* const& friend const T* from_python(PyObject* p, python::type) { return from_python(p, python::type()); } // Convert to T* const& friend T* from_python(PyObject* p, python::type) { return from_python(p, python::type()); } // Convert to T& friend T& from_python(PyObject* p, python::type) { return *python::detail::check_non_null(from_python(p, python::type())); } // Convert to const T& friend const T& from_python(PyObject* p, python::type) { return from_python(p, python::type()); } // Convert to T friend const T& from_python(PyObject* p, python::type) { return from_python(p, python::type()); } friend std::auto_ptr& from_python(PyObject* p, python::type&>) { return ptr_from_python(p, python::type >()); } friend std::auto_ptr& from_python(PyObject* p, python::type >) { return ptr_from_python(p, python::type >()); } friend const std::auto_ptr& from_python(PyObject* p, python::type&>) { return ptr_from_python(p, python::type >()); } friend PyObject* to_python(std::auto_ptr x) { return ptr_to_python(x); } friend boost::shared_ptr& from_python(PyObject* p, python::type&>) { return ptr_from_python(p, python::type >()); } friend boost::shared_ptr& from_python(PyObject* p, python::type >) { return ptr_from_python(p, python::type >()); } friend const boost::shared_ptr& from_python(PyObject* p, python::type&>) { return ptr_from_python(p, python::type >()); } friend PyObject* to_python(boost::shared_ptr x) { return ptr_to_python(x); } }; // Convert T to_python, instantiated on demand and only if there isn't a // non-template overload for this function. This version is the one invoked when // T is a wrapped class. See the first 2 functions declared in // PyExtensionClassConverters above for more info. template PyObject* to_python(const T& x) { return py_extension_class_converters(python::type()).to_python(x); } BOOST_PYTHON_END_CONVERSION_NAMESPACE namespace python { BOOST_PYTHON_IMPORT_CONVERSION(PyExtensionClassConverters); namespace detail { template class instance_holder; class read_only_setattr_function : public function { public: read_only_setattr_function(const char* name); PyObject* do_call(PyObject* args, PyObject* keywords) const; const char* description() const; private: string m_name; }; template struct define_conversion { static void* upcast_ptr(void* v) { return static_cast(static_cast(v)); } static void* downcast_ptr(void* v) { return dynamic_cast(static_cast(v)); } }; // An easy way to make an extension base class which wraps T. Note that Python // subclasses of this class will simply be class_t objects. // // U should be a class derived from T which overrides virtual functions with // boilerplate code to call back into Python. See extclass_demo.h for examples. // // U is optional, but you won't be able to override any member functions in // Python which are called from C++ if you don't supply it. If you just want to // be able to use T in python without overriding member functions, you can omit // U. template > class extension_class : public PyExtensionClassConverters, // This generates the to_python/from_python functions public extension_class_base { public: typedef T wrapped_type; typedef U callback_type; // Construct with a name that comes from typeid(T).name(). The name only // affects the objects of this class are represented through repr() extension_class(); // Construct with the given name. The name only affects the objects of this // class are represented through repr() extension_class(const char* name); ~extension_class(); // define constructors template inline void def(constructor) // The following incantation builds a signature1, signature2,... object. It // should _all_ get optimized away. { add_constructor( prepend(type::id(), prepend(type::id(), prepend(type::id(), prepend(type::id(), prepend(type::id(), prepend(type::id(), prepend(type::id(), prepend(type::id(), prepend(type::id(), prepend(type::id(), signature0()))))))))))); } // export homogeneous operators (type of both lhs and rhs is 'operator') // usage: foo_class.def(python::operators<(python::op_add | python::op_sub), Foo>()); // export homogeneous operators (type of both lhs and rhs is 'T const&') // usage: foo_class.def(python::operators<(python::op_add | python::op_sub)>()); template inline void def(operators) { typedef typename operand_select::template wrapped::type true_operand; def_operators(operators()); } // export heterogeneous operators (type of lhs: 'left', of rhs: 'right') // usage: foo_class.def(python::operators<(python::op_add | python::op_sub), Foo>(), // python::right_operand()); // export heterogeneous operators (type of lhs: 'T const&', of rhs: 'right') // usage: foo_class.def(python::operators<(python::op_add | python::op_sub)>(), // python::right_operand()); template inline void def(operators, right_operand r) { typedef typename operand_select::template wrapped::type true_left; def_operators(operators(), r); } // export heterogeneous reverse-argument operators // (type of lhs: 'left', of rhs: 'right') // usage: foo_class.def(python::operators<(python::op_add | python::op_sub), Foo>(), // python::left_operand()); // export heterogeneous reverse-argument operators // (type of lhs: 'left', of rhs: 'T const&') // usage: foo_class.def(python::operators<(python::op_add | python::op_sub)>(), // python::left_operand()); template inline void def(operators, left_operand l) { typedef typename operand_select::template wrapped::type true_right; def_operators(operators(), l); } // define a function that passes Python arguments and keywords // to C++ verbatim (as a 'tuple const&' and 'dictionary const&' // respectively). This is useful for manual argument passing. // It's also the only possibility to pass keyword arguments to C++. // Fn must have a signatur that is compatible to // PyObject* (*)(PyObject* aTuple, PyObject* aDictionary) template inline void def_raw(Fn fn, const char* name) { this->add_method(new_raw_arguments_function(fn), name); } // define member functions. In fact this works for free functions, too - // they act like static member functions, or if they start with the // appropriate self argument (as a pointer), they can be used just like // ordinary member functions -- just like Python! template inline void def(Fn fn, const char* name) { this->add_method(new_wrapped_function(fn), name); } // Define a virtual member function with a default implementation. // default_fn should be a function which provides the default implementation. // Be careful that default_fn does not in fact call fn virtually! template inline void def(Fn fn, const char* name, DefaultFn default_fn) { this->add_method(new_virtual_function(type(), fn, default_fn), name); } // Provide a function which implements x., reading from the given // member (pm) of the T obj template inline void def_getter(MemberType T::*pm, const char* name) { this->add_getter_method(new getter_function(pm), name); } // Provide a function which implements assignment to x., writing to // the given member (pm) of the T obj template inline void def_setter(MemberType T::*pm, const char* name) { this->add_setter_method(new setter_function(pm), name); } // Expose the given member (pm) of the T obj as a read-only attribute template inline void def_readonly(MemberType T::*pm, const char* name) { this->add_setter_method(new read_only_setattr_function(name), name); this->def_getter(pm, name); } // Expose the given member (pm) of the T obj as a read/write attribute template inline void def_read_write(MemberType T::*pm, const char* name) { 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 void declare_base(extension_class* base) { // see extclass.cpp for an explanation of why we need to register // conversion functions BaseClassInfo baseInfo(base, &define_conversion::downcast_ptr); class_registry::register_base_class(baseInfo); add_base(ref(as_object(base), ref::increment_count)); derived_class_info derivedInfo(this, &define_conversion::upcast_ptr); class_registry::register_derived_class(derivedInfo); } // declare the given class a base class of this one and register // only up conversion function template void declare_base(extension_class* base, without_downcast_t) { // see extclass.cpp for an explanation of why we need to register // conversion functions BaseClassInfo baseInfo(base, 0); class_registry::register_base_class(baseInfo); add_base(ref(as_object(base), ref::increment_count)); derived_class_info derivedInfo(this, &define_conversion::upcast_ptr); class_registry::register_derived_class(derivedInfo); } private: // types typedef instance_value_holder holder; private: // extension_class_base virtual function implementations std::vector const& base_classes() const; std::vector const& derived_classes() const; void* extract_object_from_holder(instance_holder_base* v) const; private: // Utility functions template inline void def_operators(operators) { def_standard_coerce(); // for some strange reason, this prevents MSVC from having an // "unrecoverable block scoping error"! typedef choose_op<(which & op_add)> choose_add; choose_op<(which & op_add)>::template args::add(this); choose_op<(which & op_sub)>::template args::add(this); choose_op<(which & op_mul)>::template args::add(this); choose_op<(which & op_div)>::template args::add(this); choose_op<(which & op_mod)>::template args::add(this); choose_op<(which & op_divmod)>::template args::add(this); choose_op<(which & op_pow)>::template args::add(this); choose_op<(which & op_lshift)>::template args::add(this); choose_op<(which & op_rshift)>::template args::add(this); choose_op<(which & op_and)>::template args::add(this); choose_op<(which & op_xor)>::template args::add(this); choose_op<(which & op_or)>::template args::add(this); choose_unary_op<(which & op_neg)>::template args::add(this); choose_unary_op<(which & op_pos)>::template args::add(this); choose_unary_op<(which & op_abs)>::template args::add(this); choose_unary_op<(which & op_invert)>::template args::add(this); choose_unary_op<(which & op_int)>::template args::add(this); choose_unary_op<(which & op_long)>::template args::add(this); choose_unary_op<(which & op_float)>::template args::add(this); choose_op<(which & op_cmp)>::template args::add(this); choose_unary_op<(which & op_str)>::template args::add(this); } template inline void def_operators(operators, right_operand) { def_standard_coerce(); choose_op<(which & op_add)>::template args::add(this); choose_op<(which & op_sub)>::template args::add(this); choose_op<(which & op_mul)>::template args::add(this); choose_op<(which & op_div)>::template args::add(this); choose_op<(which & op_mod)>::template args::add(this); choose_op<(which & op_divmod)>::template args::add(this); choose_op<(which & op_pow)>::template args::add(this); choose_op<(which & op_lshift)>::template args::add(this); choose_op<(which & op_rshift)>::template args::add(this); choose_op<(which & op_and)>::template args::add(this); choose_op<(which & op_xor)>::template args::add(this); choose_op<(which & op_or)>::template args::add(this); choose_op<(which & op_cmp)>::template args::add(this); } template inline void def_operators(operators, left_operand) { def_standard_coerce(); choose_rop<(which & op_add)>::template args::add(this); choose_rop<(which & op_sub)>::template args::add(this); choose_rop<(which & op_mul)>::template args::add(this); choose_rop<(which & op_div)>::template args::add(this); choose_rop<(which & op_mod)>::template args::add(this); choose_rop<(which & op_divmod)>::template args::add(this); choose_rop<(which & op_pow)>::template args::add(this); choose_rop<(which & op_lshift)>::template args::add(this); choose_rop<(which & op_rshift)>::template args::add(this); choose_rop<(which & op_and)>::template args::add(this); choose_rop<(which & op_xor)>::template args::add(this); choose_rop<(which & op_or)>::template args::add(this); choose_rop<(which & op_cmp)>::template args::add(this); } template void add_constructor(signature sig) { this->add_constructor_object(init_function::create(sig)); } }; // A simple wrapper over a T which allows us to use extension_class with a // single template parameter only. See extension_class, above. template class held_instance : public T { // There are no member functions: we want to avoid inadvertently overriding // any virtual functions in T. public: held_instance(PyObject*) : T() {} template held_instance(PyObject*, A1 a1) : T(a1) {} template held_instance(PyObject*, A1 a1, A2 a2) : T(a1, a2) {} template held_instance(PyObject*, A1 a1, A2 a2, A3 a3) : T(a1, a2, a3) {} template held_instance(PyObject*, A1 a1, A2 a2, A3 a3, A4 a4) : T(a1, a2, a3, a4) {} template held_instance(PyObject*, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) : T(a1, a2, a3, a4, a5) {} template held_instance(PyObject*, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) : T(a1, a2, a3, a4, a5, a6) {} template held_instance(PyObject*, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) : T(a1, a2, a3, a4, a5, a6, a7) {} template held_instance(PyObject*, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) : T(a1, a2, a3, a4, a5, a6, a7, a8) {} template held_instance(PyObject*, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) : T(a1, a2, a3, a4, a5, a6, a7, a8, a9) {} template held_instance(PyObject*, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10) : T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) {} }; // Abstract base class for all obj holders. Base for template class // instance_holder<>, below. class instance_holder_base { public: virtual ~instance_holder_base() {} virtual bool held_by_value() = 0; }; // Abstract base class which holds a Held, somehow. Provides a uniform way to // get a pointer to the held object template class instance_holder : public instance_holder_base { public: virtual Held*target() = 0; }; // Concrete class which holds a Held by way of a wrapper class Wrapper. If Held // can be constructed with arguments (A1...An), Wrapper must have a // corresponding constructor for arguments (PyObject*, A1...An). Wrapper is // neccessary to implement virtual function callbacks (there must be a // back-pointer to the actual Python object so that we can call any // overrides). held_instance (above) is used as a default Wrapper class when // there are no virtual functions. template class instance_value_holder : public instance_holder { public: Held* target() { return &m_held; } Wrapper* value_target() { return &m_held; } instance_value_holder(extension_instance* p) : m_held(p) {} template instance_value_holder(extension_instance* p, A1 a1) : m_held(p, a1) {} template instance_value_holder(extension_instance* p, A1 a1, A2 a2) : m_held(p, a1, a2) {} template instance_value_holder(extension_instance* p, A1 a1, A2 a2, A3 a3) : m_held(p, a1, a2, a3) {} template instance_value_holder(extension_instance* p, A1 a1, A2 a2, A3 a3, A4 a4) : m_held(p, a1, a2, a3, a4) {} template instance_value_holder(extension_instance* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) : m_held(p, a1, a2, a3, a4, a5) {} template instance_value_holder(extension_instance* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) : m_held(p, a1, a2, a3, a4, a5, a6) {} template instance_value_holder(extension_instance* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) : m_held(p, a1, a2, a3, a4, a5, a6, a7) {} template instance_value_holder(extension_instance* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) : m_held(p, a1, a2, a3, a4, a5, a6, a7, a8) {} template instance_value_holder(extension_instance* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) : m_held(p, a1, a2, a3, a4, a5, a6, a7, a8, a9) {} template instance_value_holder(extension_instance* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10) : m_held(p, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) {} public: // implementation of instance_holder_base required interface bool held_by_value() { return true; } private: Wrapper m_held; }; // Concrete class which holds a HeldType by way of a (possibly smart) pointer // PtrType. By default, these are only generated for PtrType == // std::auto_ptr and PtrType == boost::shared_ptr. template class instance_ptr_holder : public instance_holder { public: HeldType* target() { return &*m_ptr; } PtrType& ptr() { return m_ptr; } instance_ptr_holder(PtrType ptr) : m_ptr(ptr) {} public: // implementation of instance_holder_base required interface bool held_by_value() { return false; } private: PtrType m_ptr; }; class extension_instance : public instance { public: extension_instance(PyTypeObject* class_); ~extension_instance(); void add_implementation(std::auto_ptr holder); typedef std::vector held_objects; const held_objects& wrapped_objects() const { return m_wrapped_objects; } private: held_objects m_wrapped_objects; }; // // Template function implementations // tuple extension_class_coerce(ref l, ref r); template extension_class::extension_class() : extension_class_base(typeid(T).name()) { class_registry::register_class(this); } template extension_class::extension_class(const char* name) : extension_class_base(name) { class_registry::register_class(this); } template void extension_class::def_standard_coerce() { ref coerce_fct = dict().get_item(string("__coerce__")); if(coerce_fct.get() == 0) // not yet defined this->def(&extension_class_coerce, "__coerce__"); } template inline std::vector const& extension_class::base_classes() const { return class_registry::base_classes(); } template inline std::vector const& extension_class::derived_classes() const { return class_registry::derived_classes(); } template void* extension_class::extract_object_from_holder(instance_holder_base* v) const { instance_holder* held = dynamic_cast*>(v); if(held) return held->target(); return 0; } template extension_class::~extension_class() { class_registry::unregister_class(this); } template inline void class_registry::register_class(extension_class_base* p) { // You're not expected to create more than one of these! assert(static_class_object == 0); static_class_object = p; } template inline void class_registry::unregister_class(extension_class_base* p) { // The user should be destroying the same object they created. assert(static_class_object == p); (void)p; // unused in shipping version static_class_object = 0; } template void class_registry::register_base_class(BaseClassInfo const& i) { static_base_class_info.push_back(i); } template void class_registry::register_derived_class(derived_class_info const& i) { static_derived_class_info.push_back(i); } template std::vector const& class_registry::base_classes() { return static_base_class_info; } template std::vector const& class_registry::derived_classes() { return static_derived_class_info; } // // Static data member declaration. // template extension_class_base* class_registry::static_class_object; template std::vector class_registry::static_base_class_info; template std::vector class_registry::static_derived_class_info; }} // namespace python::detail #endif // EXTENSION_CLASS_DWA052000_H_