Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <rice/Class.hpp>
- #include <rice/Constructor.hpp>
- #include <rice/Object.hpp>
- #include <string>
- using namespace Rice;
- // Basic testing implementation to show the following
- // code works.
- class Color {
- public:
- Color() { name = "Color";}
- Color(int r, int g, int b) {}
- Color(int r, int g, int b, int a) {}
- void setName(std::string name) { this->name = name; }
- std::string to_string() const { return name; }
- private:
- std::string name;
- };
- // This method needs the "self" right now, or you get invalid number
- // of arguments.
- // This will be changed in the next version of Rice.
- Color* color_from_string(Object self, std::string name) {
- Color* c = new Color();
- c->setName(name);
- return c;
- }
- // The crux of the issue is that you need to explicitly define
- // how Rice is to convert from the Color class in C++ to Ruby and back.
- // The following two methods do this.
- Data_Type<Color> rb_cColor;
- template<>
- Color* from_ruby<Color*>(Object x) {
- Color* c;
- Data_Get_Struct(x.value(), Color, c);
- return c;
- }
- template<>
- Object to_ruby<Color*>(Color * const & c) {
- return Data_Wrap_Struct(rb_cColor, 0, Default_Allocation_Strategy<Color*>::free, c);
- }
- extern "C"
- void Init_static_methods()
- {
- rb_cColor = define_class<Color>("Color")
- .define_constructor(Constructor<Color>())
- .define_method("to_s", &Color::to_string)
- .define_singleton_method("[]", &color_from_string);
- }
Add Comment
Please, Sign In to add comment