include/rucy/extension.h.erb in rucy-0.1.7 vs include/rucy/extension.h.erb in rucy-0.1.8

- old
+ new

@@ -26,45 +26,45 @@ template <> native_class* value_to<native_class*> (Value value, bool); \ template <> native_class& value_to<native_class&> (Value value, bool convert); \ template <> native_class value_to<native_class> (Value value, bool convert); \ } -#define RUCY_DECLARE_WRAPPER_VALUE_FROM(wrapped_class) \ +#define RUCY_DECLARE_WRAPPER_VALUE_FROM(native_class) \ namespace Rucy \ { \ - Value value (wrapped_class* obj); \ - Value value (wrapped_class* obj, Value klass); \ + Value value (native_class* obj); \ + Value value (native_class* obj, Value klass); \ } -#define RUCY_DECLARE_WRAPPER_VALUE_TO(wrapped_class) \ +#define RUCY_DECLARE_WRAPPER_VALUE_TO(native_class) \ namespace Rucy \ { \ - template <> wrapped_class* value_to<wrapped_class*> (Value value, bool convert); \ + template <> native_class* value_to<native_class*> (Value value, bool convert); \ } -#define RUCY_DEFINE_VALUE_FROM(native_class, ruby_class) \ +#define RUCY_DEFINE_VALUE_FROM(native_class) \ namespace Rucy \ { \ Value \ value (const native_class& obj) \ { \ - return new_type(ruby_class, new native_class(obj)); \ + return new_type(get_ruby_class<native_class>(), new native_class(obj)); \ } \ Value \ value (const native_class* obj) \ { \ return obj ? value(*obj) : nil(); \ } \ } -#define RUCY_DEFINE_VALUE_TO(native_class, ruby_class) \ +#define RUCY_DEFINE_VALUE_TO(native_class) \ namespace Rucy \ { \ template <> native_class* \ value_to<native_class*> (Value value, bool) \ { \ - return get_type_ptr<native_class>(value, ruby_class); \ + return get_type_ptr<native_class>(value, get_ruby_class<native_class>()); \ } \ template <> native_class& \ value_to<native_class&> (Value value, bool convert) \ { \ native_class* obj = value_to<native_class*>(value, convert); \ @@ -72,77 +72,77 @@ rucy_error(__FILE__, __LINE__, "failed to convert from/to %s.", #native_class); \ return *obj; \ } \ } -#define RUCY_DEFINE_WRAPPER_VALUE_FROM(wrapped_class, ruby_class) \ +#define RUCY_DEFINE_WRAPPER_VALUE_FROM(native_class) \ namespace Rucy \ { \ Value \ - value (wrapped_class* obj) \ + value (native_class* obj) \ { \ - return value(obj, ruby_class); \ + return value(obj, get_ruby_class<native_class>()); \ } \ Value \ - value (wrapped_class* obj, Value klass) \ + value (native_class* obj, Value klass) \ { \ if (!obj) return nil(); \ - ClassWrapper<wrapped_class>* p = dynamic_cast<ClassWrapper<wrapped_class>*>(obj); \ + ClassWrapper<native_class>* p = dynamic_cast<ClassWrapper<native_class>*>(obj); \ if (!p) return new_ref(klass, obj); \ if (p->value.is_nil()) p->value = new_wrapper(klass, obj); \ return p->value; \ } \ } -#define RUCY_DEFINE_WRAPPER_VALUE_TO(wrapped_class, ruby_class) \ +#define RUCY_DEFINE_WRAPPER_VALUE_TO(native_class) \ namespace Rucy \ { \ - template <> wrapped_class* \ - value_to<wrapped_class*> (Value value, bool convert) \ + template <> native_class* \ + value_to<native_class*> (Value value, bool convert) \ { \ - return get_type_ptr<wrapped_class>(value, ruby_class); \ + return get_type_ptr<native_class>(value, get_ruby_class<native_class>()); \ } \ } #define RUCY_DECLARE_VALUE_FROM_TO(native_class) \ RUCY_DECLARE_VALUE_FROM(native_class) \ RUCY_DECLARE_VALUE_TO(native_class) -#define RUCY_DEFINE_VALUE_FROM_TO(native_class, ruby_class) \ - RUCY_DEFINE_VALUE_FROM(native_class, ruby_class) \ - RUCY_DEFINE_VALUE_TO(native_class, ruby_class) +#define RUCY_DEFINE_VALUE_FROM_TO(native_class) \ + RUCY_DEFINE_VALUE_FROM(native_class) \ + RUCY_DEFINE_VALUE_TO(native_class) -#define RUCY_VALUE_FROM_TO(native_class, ruby_class) \ +#define RUCY_VALUE_FROM_TO(native_class) \ RUCY_DECLARE_VALUE_FROM_TO(native_class) \ - RUCY_DEFINE_VALUE_FROM_TO(native_class, ruby_class) + RUCY_DEFINE_VALUE_FROM_TO(native_class) -#define RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(wrapped_class) \ - RUCY_DECLARE_WRAPPER_VALUE_FROM(wrapped_class) \ - RUCY_DECLARE_WRAPPER_VALUE_TO(wrapped_class) +#define RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(native_class) \ + RUCY_DECLARE_WRAPPER_VALUE_FROM(native_class) \ + RUCY_DECLARE_WRAPPER_VALUE_TO(native_class) -#define RUCY_DEFINE_WRAPPER_VALUE_FROM_TO(wrapped_class, ruby_class) \ - RUCY_DEFINE_WRAPPER_VALUE_FROM(wrapped_class, ruby_class) \ - RUCY_DEFINE_WRAPPER_VALUE_TO(wrapped_class, ruby_class) +#define RUCY_DEFINE_WRAPPER_VALUE_FROM_TO(native_class) \ + RUCY_DEFINE_WRAPPER_VALUE_FROM(native_class) \ + RUCY_DEFINE_WRAPPER_VALUE_TO(native_class) -#define RUCY_WRAPPER_VALUE_FROM_TO(wrapped_class, ruby_class) \ - RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(wrapped_class) \ - RUCY_DEFINE_WRAPPER_VALUE_FROM_TO(wrapped_class, ruby_class) +#define RUCY_WRAPPER_VALUE_FROM_TO(native_class) \ + RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(native_class) \ + RUCY_DEFINE_WRAPPER_VALUE_FROM_TO(native_class) #define RUCY_OVERRIDE_BEGIN(wrapper_class) \ - typedef wrapper_class RucyWrapper; \ + typedef wrapper_class RucyWrapper; \ typedef RucyWrapper Super; \ enum { OVERRIDE_ID_FIRST = RucyWrapper::OVERRIDE_ID_LAST - 1, #define RUCY_OVERRIDE_END \ OVERRIDE_ID_LAST }; #define RUCY_OVERRIDE_ID(name) \ OID_##name, -#define RUCY_IS_OVERRIDDEN(name, ruby_class) \ - this->is_overridden(ruby_class, name, OID_##name) +#define RUCY_IS_OVERRIDDEN(name) \ + this->is_overridden(name, OID_##name) #define RUCY_TRY \ RubyValue RUCY__rubyexception__ = nil(); \ int RUCY__rubyjumptag__ = 0; \ @@ -219,46 +219,45 @@ RubyValue name (Value self<%= params(n) {|i| ", Value v#{i}"} %>) \ { \ RUCY_TRY % end -#define RUCY_DEF_clear_override_flags(name, wrapped_class, ruby_class) \ +#define RUCY_DEF_clear_override_flags(name, native_class) \ RUCY_DEF0(name) \ { \ - RUCY_CHECK_OBJ(wrapped_class, ruby_class, self); \ - ClassWrapper<wrapped_class>* obj = \ - dynamic_cast<ClassWrapper<wrapped_class>*>(to<wrapped_class*>(self)); \ + RUCY_CHECK_OBJ(native_class, self); \ + ClassWrapper<native_class>* obj = \ + dynamic_cast<ClassWrapper<native_class>*>(to<native_class*>(self)); \ if (obj) obj->clear_override_flags(); \ } \ RUCY_END #define RUCY_END \ RUCY_CATCH \ return nil(); \ } -#define RUCY_CHECK_OBJ(native_class, ruby_class, obj) \ +#define RUCY_CHECK_OBJ(native_class, obj) \ do \ { \ - native_class* p = Rucy::get_type_ptr<native_class>(obj, ruby_class); \ + native_class* p = \ + Rucy::get_type_ptr<native_class>(obj, Rucy::get_ruby_class<native_class>()); \ if (!p) Rucy::invalid_object_error(__FILE__, __LINE__); \ } \ while(0) -#define RUCY_CHECK_OBJECT(native_class, ruby_class, obj) \ +#define RUCY_CHECK_OBJECT(native_class, obj) \ do \ { \ - native_class* p = Rucy::get_type_ptr<native_class>(obj, ruby_class); \ + native_class* p = Rucy::get_type_ptr<native_class>(obj, get_ruby_class<native_class>()); \ if (!p || !*p) Rucy::invalid_object_error(__FILE__, __LINE__); \ } \ while(0) -#define RUCY_WRAPPER_CALL(wrapped_class, obj, fun) \ - (dynamic_cast<ClassWrapper<wrapped_class>*>(obj) \ - ? reinterpret_cast<ClassWrapper<wrapped_class>*>(obj)->RucyWrapped::fun \ - : (obj)->fun) +#define RUCY_WRAPPER_CALL(native_class, obj, fun) \ + ((obj)->rucy_wrapped() ? (obj)->native_class::fun : (obj)->fun) namespace Rucy { @@ -270,10 +269,13 @@ const char* method, int nargs, int nargs_expected_n0, int n1 = -1, int n2 = -1, int n3 = -1, int n4 = -1, int n5 = -1, int n6 = -1, int n7 = -1, int n8 = -1, int n9 = -1, int n10 = -1); + template <typename T> Class get_ruby_class (); + + template <typename T> class ClassWrapper : public T { typedef ClassWrapper This; @@ -314,26 +316,31 @@ virtual void clear_override_flags () { override_flags.reset(); } - virtual bool is_overridden (const Value& klass, const Symbol& name, uint id) const + virtual bool is_overridden (const Symbol& name, uint id) const { if (id <= OVERRIDE_ID_UNKNOWN) return false; bool checked = false, overridden = false; get_override_flag(&checked, &overridden, id); if (checked) return overridden; - overridden = check_overridden(klass, name); + overridden = check_overridden(name); if (!set_override_flag(id, true, overridden)) return false; return overridden; } + virtual bool rucy_wrapped () const + { + return true; + } + protected: enum { @@ -357,14 +364,14 @@ private: mutable boost::dynamic_bitset<> override_flags; - bool check_overridden (const Value& klass, const Symbol& name) const + bool check_overridden (const Symbol& name) const { RUCY_SYM(method); RUCY_SYM(owner); - return value.call(method, name.value()).call(owner) != klass; + return value.call(method, name.value()).call(owner) != get_ruby_class<RucyWrapped>(); } void get_override_flag (bool* checked, bool* overridden, uint id) const { assert(checked || overridden);