From: Phlip Date: 2005-10-01T14:51:45+09:00 Subject: Re: Interest in Boost::Ruby Phlip wrote: > The best thing I have done with the VALUE object is very simple: I wrap it > in a C++ class that exposes the functions taking a VALUE as simple > methods, overloaded operators, etc. Very simple, and I don't pretend it's > a C++ class. It makes coding very fast. Here's that wrapper for Ruby's VALUE object: class rbValue { public: rbValue(VALUE nuV = Qnil): v(nuV) {} rbValue(char const * gv): v(Qnil) { assert(gv); assert('$' == gv[0]); // documentation sez this is optional. We don't agree v = rb_gv_get(gv); } operator VALUE() const { return v; } rbValue &operator =(VALUE nuV) { v = nuV; return *this; } rbValue fetch(char const * tag) { return funcall("fetch", 2, rb_str_new2(tag), Qnil); } rbValue fetch(int idx) { return funcall("fetch", 2, INT2FIX(idx), Qnil); } rbValue fetch(size_t idx) { return funcall("fetch", 2, INT2FIX(idx), Qnil); } VALUE * getPtr() { assert(T_ARRAY == TYPE(v)); return RARRAY(v)->ptr; } long getLen() { assert(T_ARRAY == TYPE(v)); return RARRAY(v)->len; } rbValue getAt(long idx) { assert(idx < getLen()); return RARRAY(v)->ptr[idx]; } rbValue operator[](long idx) { return getAt(idx); } bool isNil() { return Qnil == v; } double to_f() { assert(T_FLOAT == TYPE(v) || T_FIXNUM == TYPE(v)); return NUM2DBL(v); } char const * to_s() { assert(T_STRING == TYPE(v)); return STR2CSTR(v); } rbValue funcall ( char const * method, int argc = 0, VALUE arg1 = Qnil, VALUE arg2 = Qnil, VALUE arg3 = Qnil ) { return rb_funcall(v, rb_intern(method), argc, arg1, arg2, arg3); } rbValue iv_get(char const * member) { VALUE iv = rb_iv_get(v, member); return iv; } void iv_set(char const * member, VALUE datum) { rb_iv_set(v, member, datum); } void iv_set(char const * member, int datum) { iv_set(member, INT2FIX(datum)); } private: VALUE v; }; // a smart wrapper for the Ruby VALUE type Call its members like this: void push(rbValue xyzIn) { rbValue str = xyzIn.funcall("inspect"); OutputDebugStringA(str.to_s()); OutputDebugStringA("\n"); } Google for that code to find its project. Converting it into a snarl of Boostoidal templates is left as an exercise for the obsessed. -- Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!!