fernandez.dan wrote:
> I was wondering if anyone has any recommendation for
> embbedding a script engine in a c++ application.
All of Lua, Python, Ruby, etc. are written with embedding in mind. Lua
hardly has any other presence.
> I want to feed my C++
> application scripts which based on the script would create C++ objects
> and call the appropriate methods.
Here's a 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.
--
Phlip
http://industrialxp.org/community/bi...UserInterfaces