Skip to content

Commit

Permalink
Add a #define method that allows methods/fields to be set on Rice def…
Browse files Browse the repository at this point in the history
…ined classes. This is useful for dealing with C++ class templates.
  • Loading branch information
cfis committed Nov 27, 2024
1 parent 7c2325c commit 5dc6641
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
14 changes: 11 additions & 3 deletions rice/Data_Type.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdexcept>
Expand Down Expand Up @@ -158,6 +155,17 @@ namespace Rice
return *this;
}

template<typename T>
template<typename Func_T>
Data_Type<T>& Data_Type<T>::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<T>& dummy = *this;
func(*this);
return *this;
}

template<typename T>
template<typename Director_T>
inline Data_Type<T>& Data_Type<T>::define_director()
Expand Down
29 changes: 26 additions & 3 deletions rice/Data_Type_defn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Rice
static_assert(std::is_same_v<detail::intrinsic_type<T>, 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.
Expand Down Expand Up @@ -65,7 +67,28 @@ namespace Rice
* \endcode
*/
template<typename Constructor_T, typename...Arg_Ts>
Data_Type<T> & define_constructor(Constructor_T constructor, Arg_Ts const& ...args);
Data_Type<T>& 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<Matrix<T, R, C>>& klass)
* {
* klass.define_method...
* return klass;
* }
*
* define_class<<Matrix<T, R, C>>>("Matrix")
* .build(&builder);
*
* \endcode
*/
template<typename Func_T>
Data_Type<T>& define(Func_T func);

//! Register a Director class for this class.
/*! For any class that uses Rice::Director to enable polymorphism
Expand Down Expand Up @@ -133,7 +156,7 @@ namespace Rice
* \return *this
*/
template <typename Base_T = void>
static Data_Type bind(const Module& klass);
static Data_Type<T> bind(const Module& klass);

template<typename T_, typename Base_T_>
friend Rice::Data_Type<T_> define_class_under(Object module, char const * name);
Expand Down Expand Up @@ -183,7 +206,7 @@ namespace Rice
*/
template<typename T, typename Base_T = void>
Data_Type<T> define_class(char const* name);
} // namespace Rice
}

#include "Data_Type.ipp"

Expand Down

0 comments on commit 5dc6641

Please sign in to comment.