librelist archives

« back to archive

access to members of a class?

access to members of a class?

From:
Jens Finkhäuser
Date:
2011-02-03 @ 18:18
Hiya!

I'm glad I found Rice :) Am used to similar stuff from Boost.Python
and Luabind, and now I need to expose some C++ library API to Ruby.

One thing that I didn't find in the documentation is what to do to
expose access to data members of a class, though. For example:

  class Foo
  {
    // stuff
    int bar;
  };

I'd expect to be able to do something like this:

  define_class<Foo>("Foo")
    .define_member("bar", &Foo::bar)
  ;

Any hints? I've found Struct, but that doesn't seem to do what I want
either.

Jens

-- 
1.21 Jiggabytes of memory should be enough for anybody.

Re: [rice] access to members of a class?

From:
Jason Roelofs
Date:
2011-02-03 @ 18:27
Glad Rice is working for you.

So Rice doesn't currently have what Boost.Python has in terms of exposing 
members. What you have to do is build quick setter / getter methods and 
expose those to Ruby.

An example of what rb++[1] spits out in this situation

  class Adder {
    public:
     Adder();
      int value1;
      float value2;
  };

void wrap_classes_Adder_value1_set(classes::Adder* self, int val) {
	self->value1 = val;
}
int wrap_classes_Adder_value1_get(classes::Adder* self) {
	return self->value1;
}

Rice::Data_Type< classes::Adder > rb_cAdder =  Rice::define_class< 
classes::Adder >("Adder");
rb_cAdder.define_constructor(Rice::Constructor< classes::Adder >());
rb_cAdder.define_method("value1=", &wrap_classes_Adder_value1_set);
rb_cAdder.define_method("value1", &wrap_classes_Adder_value1_get);

Hope that helps. I'll make a note that this needs to be documented.

Jason

[1] http://rbplusplus.rubyforge.org (if you're familiar with py++, this is
that for Ruby / Rice)

On Feb 3, 2011, at 1:18 PM, Jens Finkhäuser wrote:

> Hiya!
> 
> I'm glad I found Rice :) Am used to similar stuff from Boost.Python
> and Luabind, and now I need to expose some C++ library API to Ruby.
> 
> One thing that I didn't find in the documentation is what to do to
> expose access to data members of a class, though. For example:
> 
>  class Foo
>  {
>    // stuff
>    int bar;
>  };
> 
> I'd expect to be able to do something like this:
> 
>  define_class<Foo>("Foo")
>    .define_member("bar", &Foo::bar)
>  ;
> 
> Any hints? I've found Struct, but that doesn't seem to do what I want
> either.
> 
> Jens
> 
> -- 
> 1.21 Jiggabytes of memory should be enough for anybody.

Re: [rice] access to members of a class?

From:
Jens Finkhäuser
Date:
2011-02-03 @ 19:12
On Thu, Feb 03, 2011 at 01:27:26PM -0500, Jason Roelofs wrote:
> <snip/>
> Hope that helps. I'll make a note that this needs to be documented.
  It does. It's not terribly convenient, but I'll check out rb++ -
that might work better for me :)

Thanks!
  Jens

-- 
1.21 Jiggabytes of memory should be enough for anybody.