5 #define snprintf _snprintf
12 typedef std::pair<std::weak_ptr<void *>, std::weak_ptr<void *>> wrapped_t;
14 Ruby(
const char *scriptname);
18 static const int False;
19 static const int True;
24 void printErrorInfo();
26 typedef unsigned long Value;
28 void defineGlobalConst(
const char *rbname, Value obj);
39 static bool isConvertible(Value var);
41 template <
class P,
class T>
43 Class(std::shared_ptr<P> parent,
const char *rbname, Value super = Nil);
45 template<Value(P::*Func)(const std::shared_ptr<T>&)>
47 template<Value(P::*Func)(const std::shared_ptr<T>&,Value)>
48 void defineSingletonMethod1(Value obj,
const char *rbname);
49 template<Value(P::*Func)(const std::shared_ptr<T>&,Value,Value)>
50 void defineSingletonMethod2(Value obj,
const char *rbname);
51 template<Value(P::*Func)(const std::shared_ptr<T>&)>
52 void defineMethod(
const char *rbname);
53 template<Value(P::*Func)(const std::shared_ptr<T>&,Value)>
54 void defineMethod1(
const char *rbname);
55 template<Value(P::*Func)(const std::shared_ptr<T>&,Value,Value)>
56 void defineMethod2(
const char *rbname);
57 Value rubyClassObject()
const;
58 Value rubyObject(
const std::shared_ptr<T> &obj)
const;
59 static std::weak_ptr<T> unwrap(Value v) {
60 return unwrap_internal<Ptr>(v).second;
64 #define RUBYDECL __cdecl
68 typedef std::pair<std::weak_ptr<P>, std::weak_ptr<T>> Ptr;
69 static_assert(
sizeof(Ptr) ==
sizeof(wrapped_t),
"");
73 template<Value(P::*Func)(const std::shared_ptr<T>&)>
75 *func = [](Value
self)->Value {
78 auto &st = unwrap_internal<Ptr>(
self);
79 std::shared_ptr<P> p(st.first);
80 return (p.get()->*Func)(std::shared_ptr<T>(st.second));
82 catch(std::bad_weak_ptr &) {
83 snprintf(errstr,
sizeof(errstr) - 1,
"C object no longer exists.");}
84 catch(std::string &e) {
85 snprintf(errstr,
sizeof(errstr) - 1,
"%s", e.c_str());}
86 catch(
const char *e) {
87 snprintf(errstr,
sizeof(errstr) - 1,
"%s", e);}
93 template<Value(P::*Func)(const std::shared_ptr<T>&, Value)>
95 *func = [](Value
self, Value x)->Value {
98 auto &st = unwrap_internal<Ptr>(
self);
99 std::shared_ptr<P> p(st.first);
100 return (p.get()->*Func)(std::shared_ptr<T>(st.second), x);
102 catch(std::bad_weak_ptr &) {
103 snprintf(errstr,
sizeof(errstr) - 1,
"C object no longer exists.");}
104 catch(std::string &e) {
105 snprintf(errstr,
sizeof(errstr) - 1,
"%s", e.c_str());}
106 catch(
const char *e) {
107 snprintf(errstr,
sizeof(errstr) - 1,
"%s", e);}
113 template<Value(P::*Func)(const std::shared_ptr<T>&, Value, Value)>
115 *func = [](Value
self, Value x, Value y)->Value {
118 auto &st = unwrap_internal<Ptr>(
self);
119 std::shared_ptr<P> p(st.first);
120 return (p.get()->*Func)(std::shared_ptr<T>(st.second), x, y);
122 catch(std::bad_weak_ptr &) {
123 snprintf(errstr,
sizeof(errstr) - 1,
"C object no longer exists.");}
124 catch(std::string &e) {
125 snprintf(errstr,
sizeof(errstr) - 1,
"%s", e.c_str());}
126 catch(
const char *e) {
127 snprintf(errstr,
sizeof(errstr) - 1,
"%s", e);}
132 std::weak_ptr<P> m_parent;
137 static Y &unwrap_internal(Value
self) {
138 return *
static_cast<Y*
>(unwrap_obj(
self));
140 static Value wrap_obj(Value cl,
void *p,
void (*)(
void *));
141 static void *unwrap_obj(Value
self);
143 static void define_method(Value cl,
const char *rbname, Value (*func)(...),
int argnum);
144 static void define_singleton_method(Value obj,
const char *rbname, Value (*func)(...),
int argnum);
145 static Value define_class(
const char *rbname, Value super);
151 template <
class P,
class T>
153 m_rbObj = define_class(rbname, super);
155 template <
class P,
class T>
156 template<Ruby::Value(P::*Func)(const std::shared_ptr<T>&)>
159 Value (*func)(Value);
160 typedef Value(*fp)(...);
161 int arg_num = create_function<Func>(&func);
162 define_method(m_rbObj, rbname, reinterpret_cast<fp>(func), arg_num);
164 template <
class P,
class T>
165 template<Ruby::Value(P::*Func)(const std::shared_ptr<T>&,Ruby::Value)>
168 Value (*func)(Value,Value);
169 typedef Value(*fp)(...);
170 int arg_num = create_function<Func>(&func);
171 define_method(m_rbObj, rbname, reinterpret_cast<fp>(func), arg_num);
173 template <
class P,
class T>
174 template<Ruby::Value(P::*Func)(const std::shared_ptr<T>&,Ruby::Value,Ruby::Value)>
177 Value (*func)(Value,Value,Value);
178 typedef Value(*fp)(...);
179 int arg_num = create_function<Func>(&func);
180 define_method(m_rbObj, rbname, reinterpret_cast<fp>(func), arg_num);
182 template <
class P,
class T>
183 template<Ruby::Value(P::*Func)(const std::shared_ptr<T>&)>
186 Value (*func)(Value);
187 typedef Value(*fp)(...);
188 int arg_num = create_function<Func>(&func);
189 define_singleton_method(obj, rbname, reinterpret_cast<fp>(func), arg_num);
191 template <
class P,
class T>
192 template<Ruby::Value(P::*Func)(const std::shared_ptr<T>&,Ruby::Value)>
195 Value (*func)(Value,Value);
196 typedef Value(*fp)(...);
197 int arg_num = create_function<Func>(&func);
198 define_singleton_method(obj, rbname, reinterpret_cast<fp>(func), arg_num);
200 template <
class P,
class T>
201 template<Ruby::Value(P::*Func)(const std::shared_ptr<T>&,Ruby::Value,Ruby::Value)>
204 Value (*func)(Value,Value,Value);
205 typedef Value(*fp)(...);
206 int arg_num = create_function<Func>(&func);
207 define_singleton_method(obj, rbname, reinterpret_cast<fp>(func), arg_num);
209 template <
class P,
class T>
212 template <
class P,
class T>
215 auto f = [](
void *p){
delete (Ptr*)p;};
216 return wrap_obj(m_rbObj,
new Ptr(m_parent, obj), f);