diff --git a/rice/Data_Type.ipp b/rice/Data_Type.ipp index 78ceca51..541386f6 100644 --- a/rice/Data_Type.ipp +++ b/rice/Data_Type.ipp @@ -3,15 +3,12 @@ #include "traits/attribute_traits.hpp" #include "traits/method_traits.hpp" -#include "detail/NativeRegistry.hpp" #include "detail/NativeAttributeGet.hpp" #include "detail/NativeAttributeSet.hpp" #include "detail/default_allocation_func.hpp" #include "detail/TypeRegistry.hpp" #include "detail/Wrapper.hpp" #include "detail/NativeIterator.hpp" -#include "cpp_api/Class.hpp" -#include "cpp_api/String.hpp" #include "ruby_mark.hpp" #include @@ -158,6 +155,17 @@ namespace Rice return *this; } + template + template + Data_Type& Data_Type::define(Func_T func) + { + // The passed in this pointer is an RValue, so we need to keep it alive by + // assigning it to a const lvalue + const Data_Type& dummy = *this; + func(*this); + return *this; + } + template template inline Data_Type& Data_Type::define_director() diff --git a/rice/Data_Type_defn.hpp b/rice/Data_Type_defn.hpp index 8b9a27cd..e3df102d 100644 --- a/rice/Data_Type_defn.hpp +++ b/rice/Data_Type_defn.hpp @@ -17,6 +17,8 @@ namespace Rice static_assert(std::is_same_v, T>); public: + using type = T; + //! Default constructor which does not bind. /*! No member functions must be called on this Data_Type except bind, * until the type is bound. @@ -65,7 +67,28 @@ namespace Rice * \endcode */ template - Data_Type & define_constructor(Constructor_T constructor, Arg_Ts const& ...args); + Data_Type& define_constructor(Constructor_T constructor, Arg_Ts const& ...args); + + /*! Runs a function that should define this Data_Types methods and attributes. + * This is useful when creating classes from a C++ class template. + * + * \param builder A function that addes methods/attributes to this class + * + * For example: + * \code + * void builder(Data_Type>& klass) + * { + * klass.define_method... + * return klass; + * } + * + * define_class<>>("Matrix") + * .build(&builder); + * + * \endcode + */ + template + Data_Type& define(Func_T func); //! Register a Director class for this class. /*! For any class that uses Rice::Director to enable polymorphism @@ -133,7 +156,7 @@ namespace Rice * \return *this */ template - static Data_Type bind(const Module& klass); + static Data_Type bind(const Module& klass); template friend Rice::Data_Type define_class_under(Object module, char const * name); @@ -183,7 +206,7 @@ namespace Rice */ template Data_Type define_class(char const* name); -} // namespace Rice +} #include "Data_Type.ipp"