.doc/ext/rucy/class.cpp in rucy-0.1.6 vs .doc/ext/rucy/class.cpp in rucy-0.1.7

- old
+ new

@@ -1,22 +1,20 @@ #include "class.h" -#include <rucy.h> +#include "rucy.h" using namespace Rucy; -static Class cBase, cSub, cRubyObj; +static Class cBase, cSub, cSimpleObj; +RUCY_WRAPPER_VALUE_FROM_TO(Base, cBase) +RUCY_WRAPPER_VALUE_FROM_TO(Sub, cSub) +RUCY_WRAPPER_VALUE_FROM_TO(SimpleObj, cSimpleObj) -RUCY_WRAPPER_VALUE_FROM_TO(Base, cBase) -RUCY_WRAPPER_VALUE_FROM_TO(Sub, cSub) -RUCY_WRAPPER_VALUE_FROM_TO(RubyObj, cRubyObj) - - template <typename T> Class get_class (); template <> Class get_class<Base> () {return cBase;} template <> Class get_class<Sub> () {return cSub;} @@ -24,41 +22,47 @@ template <typename T> class RubyBase : public ClassWrapper<T> { - typedef ClassWrapper<T> Super; + public: - RUCY_OVERRIDE_ID_START(name_overridable_faster) - RUCY_OVERRIDE_ID_LAST + RUCY_OVERRIDE_BEGIN(ClassWrapper<T>) - public: + RUCY_OVERRIDE_ID(name_overridable_faster) + RUCY_OVERRIDE_END + virtual const char* name_overridable () const { - RUCY_OVERRIDABLE_METHOD(name_overridable, get_class<T>(), .c_str()); + RUCY_SYM(name_overridable); + return this->value.call(name_overridable).c_str(); } virtual const char* name_overridable_faster () const { - RUCY_OVERRIDABLE_METHOD_FAST(name_overridable_faster, get_class<T>(), .c_str()); + RUCY_SYM(name_overridable_faster); + if (RUCY_IS_OVERRIDDEN(name_overridable_faster, get_class<T>())) + return this->value.call(name_overridable_faster).c_str(); + else + return Super::name_overridable_faster(); } bool is_name_overridable_faster_overridden () const { - SYM(name_overridable_faster); + RUCY_SYM(name_overridable_faster); return RUCY_IS_OVERRIDDEN(name_overridable_faster, get_class<T>()); } };// RubyBase #define THIS(type) to<type*>(self) -#define CHECK(type) RUCY_CHECK_OBJ(self, type, c##type) +#define CHECK(type) RUCY_CHECK_OBJ(type, c##type, self) -#define CALL(type, obj, fun) RUCY_WRAPPER_CALL(ClassWrapper<type>, obj, fun) +#define CALL(type, obj, fun) RUCY_WRAPPER_CALL(type, obj, fun) /* alloc function. */ @@ -112,38 +116,20 @@ template <typename T> static VALUE is_name_overridable_faster_overridden(VALUE self) { - RUCY_CHECK_OBJ(self, T, get_class<T>()); + RUCY_CHECK_OBJ(T, get_class<T>(), self); RubyBase<T>* obj = dynamic_cast<RubyBase<T>*>(THIS(T)); - if (!obj) invalid_object_error("dynamic_cast() failed."); + if (!obj) invalid_object_error(__FILE__, __LINE__, "dynamic_cast() failed."); return value(obj->is_name_overridable_faster_overridden()); } -template <typename T> -static -VALUE clear_override_flags(VALUE self) -{ - RUCY_CHECK_OBJ(self, T, get_class<T>()); - ClassWrapper<T>* obj = dynamic_cast<ClassWrapper<T>*>(THIS(T)); - if (obj) obj->clear_override_flags(); -} +static RUCY_DEF_clear_override_flags(Base_clear_override_flags, Base, cBase); +static RUCY_DEF_clear_override_flags(Sub_clear_override_flags, Sub, cSub); -template <bool singleton> static -VALUE method_added_or_removed(VALUE self, VALUE method_name) -{ - SYMBOL(klass, "class"); - SYM(name); - eval( - Xot::stringf( - "ObjectSpace.each_object(%s) {|o| o.clear_override_flags}", - (singleton ? self(klass) : self)(name).c_str()).c_str()); -} - -static VALUE base_new_raw(VALUE self) { return value(new Base); } @@ -180,28 +166,37 @@ /* alloc function. */ static -VALUE rubyobj_alloc(VALUE klass) +VALUE simpleobj_alloc(VALUE klass) { - return value(new ClassWrapper<RubyObj>, klass); + return value(new ClassWrapper<SimpleObj>, klass); } -static Xot::Ref<RubyObj> rubyobj_ref; +static +VALUE simpleobj_initialize(VALUE self, VALUE name) +{ + CHECK(SimpleObj); + THIS(SimpleObj)->init(name.c_str()); +} +static std::vector<Xot::Ref<SimpleObj> > simpleobj_refs; + static -VALUE rubyobj_set_ref(VALUE self, VALUE obj) +VALUE simpleobj_set_refs(VALUE self, VALUE objs) { - rubyobj_ref = to<RubyObj*>(obj); - return obj; + int size = objs.size(); + for (int i = 0; i < size; ++i) + simpleobj_refs.push_back(to<SimpleObj*>(objs[i])); + return objs; } static -VALUE rubyobj_clear_ref(VALUE self) +VALUE simpleobj_clear_refs(VALUE self) { - rubyobj_ref.reset(); + simpleobj_refs.clear(); } void Init_class () @@ -218,27 +213,24 @@ rb_define_method(cBase, "call_name_overridable", RUBY_METHOD_FUNC(call_name_overridable), 0); rb_define_method(cBase, "call_name_overridable_faster", RUBY_METHOD_FUNC(call_name_overridable_faster), 0); cBase.define_method( "is_name_overridable_faster_overridden", is_name_overridable_faster_overridden<Base>); - cBase.define_method("clear_override_flags", clear_override_flags<Base>); - cBase.define_method("singleton_method_added", method_added_or_removed<true>); - cBase.define_method("singleton_method_removed", method_added_or_removed<true>); - cBase.define_singleton_method("method_added", method_added_or_removed<false>); - cBase.define_singleton_method("method_removed", method_added_or_removed<false>); rb_define_singleton_method(cBase, "new_raw", RUBY_METHOD_FUNC(base_new_raw), 0); + cSub = rb_define_class_under(mTester, "Sub", cBase); rb_define_alloc_func(cSub, sub_alloc); rb_define_method(cSub, "name_overridable", RUBY_METHOD_FUNC(sub_name_overridable), 0); rb_define_method(cSub, "name_overridable_faster", RUBY_METHOD_FUNC(sub_name_overridable_faster), 0); cSub.define_method( "is_name_overridable_faster_overridden", is_name_overridable_faster_overridden<Sub>); - cSub.define_method("clear_override_flags", clear_override_flags<Sub>); rb_define_singleton_method(cSub, "new_raw", RUBY_METHOD_FUNC(sub_new_raw), 0); + - cRubyObj = rb_define_class_under(mTester, "RubyObj", rb_cObject); - rb_define_alloc_func(cRubyObj, rubyobj_alloc); - rb_define_function(cRubyObj, "set_ref", RUBY_METHOD_FUNC(rubyobj_set_ref), 1); - rb_define_function(cRubyObj, "clear_ref", RUBY_METHOD_FUNC(rubyobj_clear_ref), 0); + cSimpleObj = rb_define_class_under(mTester, "SimpleObj", rb_cObject); + rb_define_alloc_func(cSimpleObj, simpleobj_alloc); + rb_define_private_method(cSimpleObj, "initialize", RUBY_METHOD_FUNC(simpleobj_initialize), 1); + rb_define_function(cSimpleObj, "set_refs", RUBY_METHOD_FUNC(simpleobj_set_refs), 1); + rb_define_function(cSimpleObj, "clear_refs", RUBY_METHOD_FUNC(simpleobj_clear_refs), 0); }