diff --git a/include/boost/python/object/class_converters.hpp b/include/boost/python/object/class_converters.hpp new file mode 100644 index 00000000..86fcd64b --- /dev/null +++ b/include/boost/python/object/class_converters.hpp @@ -0,0 +1,93 @@ +// Copyright David Abrahams 2002. 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. +#ifndef CLASS_CONVERTERS_DWA2002119_HPP +# define CLASS_CONVERTERS_DWA2002119_HPP + +# include +# include +# include +# include +# include +# include + +namespace boost { namespace python { namespace objects { + +////////////////////////////////////////////////////////////////////// +// +// register_base_of - +// A BinaryMetaFunction object which registers a single base +// class of T, and the corresponding cast(s) +// + + +// register_downcast/do_nothing - +// Helpers for register_base_of<> which take care of registering +// down-casts +template +struct register_downcast +{ + static void execute() + { + register_conversion(true); + } +}; + +struct do_nothing +{ + static void execute() { } +}; + +// Here's where the real work gets done: +template +struct register_base_of +{ + // Ignored is needed because mpl::for_each is still actually + // accumulate. We're not using any state so it just sits there. + template + struct apply + { + typedef void type; // 'type' needs to be defined for the same reasons + + // Here's the runtime part: + static void execute() + { + // Register the Base class + register_dynamic_id(); + // Register the up-cast + register_conversion(false); + + // Register the down-cast, if appropriate. + mpl::select_type< + is_polymorphic::value + , register_downcast + , do_nothing + >::type::execute(); + } + }; +}; + + +// Brings into existence all converters associated with a class Bases +// is expected to be an mpl sequence of base types. +template +inline void register_class_from_python(Derived* = 0, Bases* = 0) +{ + // cause the static registration to be instantiated. Can't just + // cast it to void on all compilers; some will skip its + // initialization. + void const* ignored = &instance_finder::registration; + (void)ignored; + + // register all up/downcasts here + register_dynamic_id(); + + // register each base in the sequence + mpl::for_each >::execute(); +} + +}}} // namespace boost::python::object + +#endif // CLASS_CONVERTERS_DWA2002119_HPP