GeezerVison is showing github.com — Embed C++ objects directly in Python objects · boostorg/python@526d99f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: include/boost/python/class.hpp +12-8Lines changed: 12 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | # include <boost/mpl/bool_t.hpp> | 22 | 22 | # include <boost/python/object/select_holder.hpp> | 23 | 23 | # include <boost/python/object/class_wrapper.hpp> | | 24 | +# include <boost/python/object/make_instance.hpp> | 24 | 25 | # include <boost/python/data_members.hpp> | 25 | 26 | # include <boost/utility.hpp> | 26 | 27 | # include <boost/python/detail/operator_id.hpp> | @@ -56,7 +57,7 @@ namespace detail | 56 | 57 | static inline void register_copy_constructor(mpl::bool_t<true> const&, SelectHolder const& , T* = 0) | 57 | 58 | { | 58 | 59 | typedef typename SelectHolder::type holder; | 59 | | - force_instantiate(objects::class_wrapper<T,holder>()); | | 60 | + force_instantiate(objects::class_wrapper<T,holder, objects::make_instance<T,holder> >()); | 60 | 61 | SelectHolder::register_(); | 61 | 62 | } | 62 | 63 |
| @@ -96,7 +97,7 @@ class class_ : public objects::class_base | 96 | 97 | X3 | 97 | 98 | >::type>::type>::type held_type; | 98 | 99 |
| 99 | | - typedef objects::class_id class_id; | | 100 | + typedef objects::select_holder<T,held_type> holder_selector; | 100 | 101 |
| 101 | 102 | typedef typename detail::select_bases<X1 | 102 | 103 | , typename detail::select_bases<X2 | @@ -108,20 +109,19 @@ class class_ : public objects::class_base | 108 | 109 | // passed to the base class constructor | 109 | 110 | struct id_vector | 110 | 111 | { | 111 | | - typedef objects::class_id class_id; | 112 | 112 | id_vector() | 113 | 113 | { | 114 | 114 | // Stick the derived class id into the first element of the array | 115 | 115 | ids[0] = type_id<T>(); | 116 | 116 |
| 117 | 117 | // Write the rest of the elements into succeeding positions. | 118 | | - class_id* p = ids + 1; | | 118 | + type_info* p = ids + 1; | 119 | 119 | mpl::for_each<bases, void, detail::write_type_id>::execute(&p); | 120 | 120 | } | 121 | 121 |
| 122 | 122 | BOOST_STATIC_CONSTANT( | 123 | 123 | std::size_t, size = mpl::size<bases>::value + 1); | 124 | | - class_id ids[size]; | | 124 | + type_info ids[size]; | 125 | 125 | }; | 126 | 126 | friend struct id_vector; | 127 | 127 |
| @@ -146,6 +146,7 @@ class class_ : public objects::class_base | 146 | 146 | { | 147 | 147 | this->register_(); | 148 | 148 | this->def_init(InitArgs()); | | 149 | + this->set_instance_size(holder_selector::additional_size()); | 149 | 150 | } | 150 | 151 |
| 151 | 152 |
| @@ -155,6 +156,7 @@ class class_ : public objects::class_base | 155 | 156 | { | 156 | 157 | this->register_(); | 157 | 158 | this->def_init(InitArgs(), initdoc); | | 159 | + this->set_instance_size(holder_selector::additional_size()); | 158 | 160 | } | 159 | 161 |
| 160 | 162 | // Wrap a member function or a non-member function which can take | @@ -237,7 +239,7 @@ class class_ : public objects::class_base | 237 | 239 | return this->def("__init__", | 238 | 240 | python::make_constructor<Args>( | 239 | 241 | // Using runtime type selection works around a CWPro7 bug. | 240 | | - objects::select_holder<T,held_type>((held_type*)0).get() | | 242 | + holder_selector::execute((held_type*)0).get() | 241 | 243 | ) | 242 | 244 | ); | 243 | 245 | } | @@ -252,7 +254,7 @@ class class_ : public objects::class_base | 252 | 254 | python::make_constructor<Args>( | 253 | 255 | helper::get_policy(policy_or_doc) | 254 | 256 | // Using runtime type selection works around a CWPro7 bug. | 255 | | - , objects::select_holder<T,held_type>((held_type*)0).get() | | 257 | + , holder_selector::execute((held_type*)0).get() | 256 | 258 | ) | 257 | 259 | , helper::get_doc(policy_or_doc, doc) | 258 | 260 | ); | @@ -389,7 +391,7 @@ inline void class_<T,X1,X2,X3>::register_() const | 389 | 391 |
| 390 | 392 | detail::register_copy_constructor<T>( | 391 | 393 | mpl::bool_t<is_copyable>() | 392 | | - , objects::select_holder<T,held_type>((held_type*)0) | | 394 | + , holder_selector::execute((held_type*)0) | 393 | 395 | ); | 394 | 396 | } | 395 | 397 |
| @@ -402,6 +404,7 @@ inline class_<T,X1,X2,X3>::class_() | 402 | 404 | this->register_(); | 403 | 405 | detail::force_instantiate(sizeof(detail::assert_default_constructible(T()))); | 404 | 406 | this->def_init(); | | 407 | + this->set_instance_size(holder_selector::additional_size()); | 405 | 408 | } | 406 | 409 |
| 407 | 410 | template <class T, class X1, class X2, class X3> | @@ -419,6 +422,7 @@ inline class_<T,X1,X2,X3>::class_(char const* name, char const* doc) | 419 | 422 | this->register_(); | 420 | 423 | detail::force_instantiate(sizeof(detail::assert_default_constructible(T()))); | 421 | 424 | this->def_init(); | | 425 | + this->set_instance_size(holder_selector::additional_size()); | 422 | 426 | } | 423 | 427 |
| 424 | 428 | template <class T, class X1, class X2, class X3> | |
Collapse file Copy file name to clipboardExpand all lines: include/boost/python/converter/smart_ptr.hpp -70Lines changed: 0 additions & 70 deletions Collapse file Copy file name to clipboardExpand all lines: include/boost/python/instance_holder.hpp +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 |
| 9 | 9 | # include <boost/python/detail/config.hpp> | 10 | 10 | # include <boost/utility.hpp> | | 11 | +# include <boost/python/type_id.hpp> | | 12 | +# include <cstddef> | 11 | 13 |
| 12 | 14 | namespace boost { namespace python { | 13 | 15 |
| @@ -25,6 +27,16 @@ struct BOOST_PYTHON_DECL instance_holder : private noncopyable | 25 | 27 |
| 26 | 28 | void install(PyObject* inst) throw(); | 27 | 29 |
| | 30 | + // These functions should probably be located elsewhere. | | 31 | + | | 32 | + // Allocate storage for an object of the given size at the given | | 33 | + // offset in the Python instance<> object if bytes are available | | 34 | + // there. Otherwise allocate size bytes of heap memory. | | 35 | + static void* allocate(PyObject*, std::size_t offset, std::size_t size); | | 36 | + | | 37 | + // Deallocate storage from the heap if it was not carved out of | | 38 | + // the given Python object by allocate(), above. | | 39 | + static void deallocate(PyObject*, void* storage) throw(); | 28 | 40 | private: | 29 | 41 | instance_holder* m_next; | 30 | 42 | }; | |
Collapse file Copy file name to clipboardExpand all lines: include/boost/python/object.hpp +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | # include <boost/python/object_items.hpp> | 12 | 12 | # include <boost/python/object_slices.hpp> | 13 | 13 | # include <boost/python/object_operators.hpp> | | 14 | +# include <boost/python/converter/arg_to_python.hpp> | 14 | 15 |
| 15 | 16 | namespace boost { namespace python { | 16 | 17 |
| |
Collapse file Copy file name to clipboardExpand all lines: include/boost/python/object/class.hpp +8-18Lines changed: 8 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | # include <boost/python/detail/wrap_python.hpp> | 10 | 10 | # include <boost/python/detail/config.hpp> | 11 | 11 | # include <boost/utility.hpp> | 12 | | -# include <boost/python/type_id.hpp> | 13 | | -# include <boost/python/handle.hpp> | 14 | 12 | # include <boost/python/instance_holder.hpp> | 15 | 13 | # include <boost/python/object_core.hpp> | 16 | 14 | # include <cstddef> | 17 | 15 |
| 18 | 16 | namespace boost { namespace python { | 19 | 17 |
| 20 | | -class module; | 21 | | - | 22 | 18 | namespace objects { | 23 | 19 |
| 24 | | -// To identify a class, we don't need cv/reference decorations | 25 | | -typedef type_info class_id; | 26 | | - | 27 | 20 | struct BOOST_PYTHON_DECL class_base : python::api::object | 28 | 21 | { | 29 | 22 | // constructor | @@ -33,6 +26,7 @@ struct BOOST_PYTHON_DECL class_base : python::api::object | 33 | 26 | , std::size_t num_types // A list of class_ids. The first is the type | 34 | 27 | , class_id const*const types // this is wrapping. The rest are the types of | 35 | 28 | // any bases. | | 29 | + | 36 | 30 | , char const* doc = 0 // Docstring, if any. | 37 | 31 | ); | 38 | 32 |
| @@ -41,21 +35,17 @@ struct BOOST_PYTHON_DECL class_base : python::api::object | 41 | 35 | void add_property(char const* name, object const& fget, object const& fset); | 42 | 36 | void setattr(char const* name, object const&); | 43 | 37 | void enable_pickling(bool getstate_manages_dict); | 44 | | - void def_no_init(); | 45 | | -}; | 46 | 38 |
| 47 | | -BOOST_PYTHON_DECL type_handle registered_class_object(class_id id); | | 39 | + // Set a special attribute in the class which tells Boost.Python | | 40 | + // to allocate extra bytes for embedded C++ objects in Python | | 41 | + // instances. | | 42 | + void set_instance_size(std::size_t bytes); | 48 | 43 |
| 49 | | -// Each extension instance will be one of these | 50 | | -struct instance | 51 | | -{ | 52 | | - PyObject_HEAD | 53 | | - instance_holder* objects; | | 44 | + // Set an __init__ function which throws an appropriate exception | | 45 | + // for abstract classes. | | 46 | + void def_no_init(); | 54 | 47 | }; | 55 | 48 |
| 56 | | -BOOST_PYTHON_DECL type_handle class_metatype(); | 57 | | -BOOST_PYTHON_DECL type_handle class_type(); | 58 | | - | 59 | 49 | }}} // namespace boost::python::objects | 60 | 50 |
| 61 | 51 | #endif // CLASS_DWA20011214_HPP |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Copyright David Abrahams 2002. Permission to copy, use, | | 2 | +// modify, sell and distribute this software is granted provided this | | 3 | +// copyright notice appears in all copies. This software is provided | | 4 | +// "as is" without express or implied warranty, and with no claim as | | 5 | +// to its suitability for any purpose. | | 6 | +#ifndef CLASS_DETAIL_DWA200295_HPP | | 7 | +# define CLASS_DETAIL_DWA200295_HPP | | 8 | + | | 9 | +# include <boost/python/handle.hpp> | | 10 | +# include <boost/python/type_id.hpp> | | 11 | + | | 12 | +namespace boost { namespace python { namespace objects { | | 13 | + | | 14 | +BOOST_PYTHON_DECL type_handle registered_class_object(type_info id); | | 15 | +BOOST_PYTHON_DECL type_handle class_metatype(); | | 16 | +BOOST_PYTHON_DECL type_handle class_type(); | | 17 | + | | 18 | +}}} // namespace boost::python::object | | 19 | + | | 20 | +#endif // CLASS_DETAIL_DWA200295_HPP |
Collapse file Copy file name to clipboardExpand all lines: include/boost/python/object/class_wrapper.hpp +3-38Lines changed: 3 additions & 38 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | #ifndef CLASS_WRAPPER_DWA20011221_HPP | 7 | 7 | # define CLASS_WRAPPER_DWA20011221_HPP | 8 | 8 |
| 9 | | -# include <boost/python/handle.hpp> | 10 | 9 | # include <boost/python/to_python_converter.hpp> | 11 | | -# include <boost/python/converter/registered.hpp> | 12 | 10 | # include <boost/ref.hpp> | 13 | 11 |
| 14 | 12 | namespace boost { namespace python { namespace objects { | 15 | 13 |
| 16 | | -template <class Src, class Holder> | 17 | | -struct copy_construct_instance | 18 | | -{ | 19 | | - static Holder* execute(PyObject* instance, Src const& x) | 20 | | - { | 21 | | - return new Holder(instance, cref(x)); | 22 | | - } | 23 | | -}; | 24 | | - | 25 | 14 | // Used to convert objects of type Src to wrapped C++ classes by | 26 | 15 | // building a new instance object and installing a Holder constructed | 27 | 16 | // from the Src object. | 28 | | -template <class Src, class Holder, class MakeHolder = copy_construct_instance<Src,Holder> > | | 17 | +template <class Src, class Holder, class MakeInstance> | 29 | 18 | struct class_wrapper | 30 | | - : to_python_converter<Src,class_wrapper<Src,Holder,MakeHolder> > | | 19 | + : to_python_converter<Src,class_wrapper<Src,Holder,MakeInstance> > | 31 | 20 | { | 32 | 21 | static PyObject* convert(Src const& x) | 33 | 22 | { | 34 | | - // Get the class object associated with the wrapped type | 35 | | - typedef typename Holder::value_type value_type; | 36 | | - PyTypeObject* class_object = converter::registered<value_type>::converters.class_object; | 37 | | - | 38 | | - // Don't call the type directly to do the construction, since | 39 | | - // that would require the registration of an appropriate | 40 | | - // __init__ function. | 41 | | - PyObject* raw_result = class_object->tp_alloc(class_object, 0); | 42 | | - | 43 | | - if (raw_result == 0) | 44 | | - return 0; | 45 | | - | 46 | | - // Everything's OK; Bypass NULL checks but guard against | 47 | | - // exceptions. | 48 | | - handle<> result(python::allow_null(raw_result)); | 49 | | - | 50 | | - // Build a value_holder to contain the object using the copy | 51 | | - // constructor | 52 | | - Holder* p = MakeHolder::execute(raw_result, x); | 53 | | - | 54 | | - // Install it in the instance | 55 | | - p->install(raw_result); | 56 | | - | 57 | | - // Return the new result | 58 | | - return result.release(); | | 23 | + return MakeInstance::execute(cref(x)); | 59 | 24 | } | 60 | 25 | }; | 61 | 26 |
| |
Collapse file Copy file name to clipboard +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Copyright David Abrahams 2002. Permission to copy, use, | | 2 | +// modify, sell and distribute this software is granted provided this | | 3 | +// copyright notice appears in all copies. This software is provided | | 4 | +// "as is" without express or implied warranty, and with no claim as | | 5 | +// to its suitability for any purpose. | | 6 | +#ifndef INSTANCE_DWA200295_HPP | | 7 | +# define INSTANCE_DWA200295_HPP | | 8 | + | | 9 | +# include <boost/python/detail/wrap_python.hpp> | | 10 | +# include <boost/type_traits/alignment_traits.hpp> | | 11 | +# include <cstddef> | | 12 | + | | 13 | +namespace boost { namespace python { namespace objects { | | 14 | + | | 15 | +// Each extension instance will be one of these | | 16 | +template <class Data = char> | | 17 | +struct instance | | 18 | +{ | | 19 | + PyObject_VAR_HEAD | | 20 | + PyObject* dict; | | 21 | + PyObject* weakrefs; | | 22 | + instance_holder* objects; | | 23 | + | | 24 | + BOOST_STATIC_CONSTANT(std::size_t, alignment = alignment_of<Data>::value); | | 25 | + typedef typename type_with_alignment<alignment>::type align_t; | | 26 | + | | 27 | + union | | 28 | + { | | 29 | + align_t align; | | 30 | + char bytes[sizeof(Data)]; | | 31 | + } storage; | | 32 | +}; | | 33 | + | | 34 | +template <class Data> | | 35 | +struct additional_instance_size | | 36 | +{ | | 37 | + typedef instance<Data> instance_data; | | 38 | + typedef instance<char> instance_char; | | 39 | + BOOST_STATIC_CONSTANT( | | 40 | + std::size_t, value = sizeof(instance_data) - offsetof(instance_char,storage)); | | 41 | +}; | | 42 | + | | 43 | +}}} // namespace boost::python::object | | 44 | + | | 45 | +#endif // INSTANCE_DWA200295_HPP |
|