diff options
author | Gonzalo Garramuno <ggarra@advancedsl.com.ar> | 2007-05-16 22:48:01 +0000 |
---|---|---|
committer | Gonzalo Garramuno <ggarra@advancedsl.com.ar> | 2007-05-16 22:48:01 +0000 |
commit | b20fd0d06f13c0a70abfcb3a09cb7c9c62a3d560 (patch) | |
tree | 8e2b03f2feb38a4320cc54c0d9dafc2ce561fd80 | |
parent | 765e3d27f00f7440db89850110148c08c75f8d97 (diff) | |
download | swig-b20fd0d06f13c0a70abfcb3a09cb7c9c62a3d560.tar.gz |
Moved functors away from std_set.i and std_map.i
into their own std_functors.i file.
Made binarypredicate, etc. be templates to allow more
strict predicates/functors.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9815 626c5289-ae23-0410-ae9c-e8d60b6d4f22
-rw-r--r-- | Lib/ruby/rubyiterators.swg | 19 | ||||
-rw-r--r-- | Lib/ruby/rubystdfunctors.swg | 87 | ||||
-rw-r--r-- | Lib/ruby/std_functors.i | 29 | ||||
-rw-r--r-- | Lib/ruby/std_map.i | 2 | ||||
-rw-r--r-- | Lib/ruby/std_set.i | 2 |
5 files changed, 102 insertions, 37 deletions
diff --git a/Lib/ruby/rubyiterators.swg b/Lib/ruby/rubyiterators.swg index fa70f74d3..b2482a61d 100644 --- a/Lib/ruby/rubyiterators.swg +++ b/Lib/ruby/rubyiterators.swg @@ -316,6 +316,10 @@ namespace swig { } public: + ConstIterator_T() : ConstIterator(Qnil) + { + } + ConstIterator_T(const_iter curr, VALUE seq = Qnil) : ConstIterator(seq), current(curr) { @@ -326,6 +330,11 @@ namespace swig { return current; } + const value_type& operator*() const + { + return *current; + } + virtual VALUE inspect() const { VALUE ret = rb_str_new2("#<"); @@ -422,6 +431,16 @@ namespace swig { *current = b; return *this; } + + const value_type& operator*() const + { + return *current; + } + + value_type& operator*() + { + return *current; + } virtual VALUE inspect() const { diff --git a/Lib/ruby/rubystdfunctors.swg b/Lib/ruby/rubystdfunctors.swg index 9446ab809..be23ffe84 100644 --- a/Lib/ruby/rubystdfunctors.swg +++ b/Lib/ruby/rubystdfunctors.swg @@ -8,7 +8,10 @@ * * You can use them in a swig file like: * - * %template< IntSet > std::set< int, RubyBinaryPredicate<> >; + * %include <std_set.i> + * %include <std_functors.i> + * + * %template< IntSet > std::set< int, swig::BinaryPredicate<> >; * * * which will then allow calling them from Ruby either like: @@ -74,104 +77,120 @@ namespace swig { $1 = SWIG_Ruby_isCallable($input) && SWIG_Ruby_arity($input, 2); } - %typemap(in,noblock=1) UnaryFunction&, UnaryFunction { - $1 = new swig::UnaryFunction<>($input); + %typemap(in,noblock=1) BinaryFunction&, BinaryFunction { + $1 = new swig::BinaryFunction< >($input); } - %typemap(in,noblock=1) UnaryPredicate&, UnaryPredicate { - $1 = new swig::UnaryPredicate<>($input); + %typemap(in,noblock=1) UnaryFunction&, UnaryFunction { + $1 = new swig::UnaryFunction< >($input); } - %typemap(in,noblock=1) BinaryFunction&, BinaryFunction { - $1 = new swig::BinaryFunction<>($input); - } %typemap(in,noblock=1) BinaryPredicate&, BinaryPredicate { $1 = new swig::BinaryPredicate<>($input); } + %typemap(in,noblock=1) UnaryPredicate&, UnaryPredicate { + $1 = new swig::UnaryPredicate< >($input); + } + %ignore BinaryFunction; - struct BinaryFunction {}; + template< class _T = GC_VALUE > + struct BinaryFunction { + }; %ignore UnaryFunction; - struct UnaryFunction {}; + template< class _T = GC_VALUE > + struct UnaryFunction { + }; %ignore BinaryPredicate; - struct BinaryPredicate {}; + template< class _T = GC_VALUE > + struct BinaryPredicate { + }; %ignore UnaryPredicate; - struct UnaryPredicate {}; + template< class _T = GC_VALUE > + struct UnaryPredicate { + + }; } -%{ +%fragment("StdFunctors","header",fragment="StdTraits") +{ namespace swig { static ID call_id = rb_intern("call"); - template <class _DefaultFunc = std::less<GC_VALUE> > - struct BinaryPredicate : GC_VALUE, std::binary_function<GC_VALUE, GC_VALUE, bool> + template <class _T = GC_VALUE, class _DefaultFunc = std::less<GC_VALUE> > + struct BinaryPredicate : GC_VALUE, std::binary_function< _T, _T, bool > { BinaryPredicate(VALUE obj = Qnil) : GC_VALUE(obj) { } - bool operator()(GC_VALUE arg1, GC_VALUE arg2) const + bool operator()(_T a, _T b) const { if (_obj != Qnil) { SWIG_RUBY_THREAD_BEGIN_BLOCK; - VALUE res = rb_funcall( _obj, swig::call_id, 2, - VALUE(arg1), VALUE(arg2)); + VALUE arg1 = swig::from(a); + VALUE arg2 = swig::from(b); + VALUE res = rb_funcall( _obj, swig::call_id, 2, arg1, arg2); SWIG_RUBY_THREAD_END_BLOCK; return RTEST(res); } else { - return _DefaultFunc()(arg1, arg2); + return _DefaultFunc()(a, b); } } }; - template <class _DefaultFunc = std::less<GC_VALUE> > - struct BinaryFunction : GC_VALUE, std::binary_function<GC_VALUE, GC_VALUE, GC_VALUE> + template <class _T = GC_VALUE, class _DefaultFunc = std::less< _T > > + struct BinaryFunction : GC_VALUE, std::binary_function< _T, _T, _T > { BinaryFunction(VALUE obj = Qnil) : GC_VALUE(obj) { } - GC_VALUE operator()(GC_VALUE arg1, GC_VALUE arg2) const + _T operator()(_T a, _T b) const { if (_obj != Qnil) { SWIG_RUBY_THREAD_BEGIN_BLOCK; - VALUE res = rb_funcall( _obj, swig::call_id, 2, - VALUE(arg1), VALUE(arg2)); + VALUE arg1 = swig::from(a); + VALUE arg2 = swig::from(b); + VALUE res = rb_funcall( _obj, swig::call_id, 2, arg1, arg2); SWIG_RUBY_THREAD_END_BLOCK; - return GC_VALUE(res); + return swig::as<_T >(res); } else { - return _DefaultFunc()(arg1, arg2); + return _DefaultFunc()(a, b); } } }; - - struct UnaryPredicate : GC_VALUE, std::unary_function<GC_VALUE, bool> + template< class _T = GC_VALUE > + struct UnaryPredicate : GC_VALUE, std::unary_function< _T, bool > { UnaryPredicate(VALUE obj = Qnil) : GC_VALUE(obj) { } - bool operator()(GC_VALUE arg1) const + bool operator()(_T a) const { SWIG_RUBY_THREAD_BEGIN_BLOCK; - VALUE res = rb_funcall( _obj, swig::call_id, 1, VALUE(arg1)); + VALUE arg1 = swig::from<_T >(a); + VALUE res = rb_funcall( _obj, swig::call_id, 1, arg1); SWIG_RUBY_THREAD_END_BLOCK; return RTEST(res); } }; - struct UnaryFunction : GC_VALUE, std::unary_function<GC_VALUE, GC_VALUE> + template< class _T = GC_VALUE > + struct UnaryFunction : GC_VALUE, std::unary_function< _T, _T > { UnaryFunction(VALUE obj = Qnil) : GC_VALUE(obj) { } - GC_VALUE operator()(GC_VALUE arg1) const + _T operator()(_T a) const { SWIG_RUBY_THREAD_BEGIN_BLOCK; + VALUE arg1 = swig::from(a); VALUE res = rb_funcall( _obj, swig::call_id, 1, VALUE(arg1)); SWIG_RUBY_THREAD_END_BLOCK; - return GC_VALUE(res); + return swig::as< _T >(res); } }; +} // namespace swig } -%} diff --git a/Lib/ruby/std_functors.i b/Lib/ruby/std_functors.i new file mode 100644 index 000000000..54ee97b56 --- /dev/null +++ b/Lib/ruby/std_functors.i @@ -0,0 +1,29 @@ +/** + * @file std_functors.i + * @date Sun May 6 00:44:33 2007 + * + * @brief This file provides unary and binary functors for STL + * containers, that will invoke a Ruby proc or method to do + * their operation. + * + * You can use them in a swig file like: + * + * %include <std_set.i> + * %include <std_functors.i> + * + * %template< IntSet > std::set< int, swig::BinaryPredicate<int> >; + * + * + * which will then allow calling them from Ruby either like: + * + * # order of set is defined by C++ default + * a = IntSet.new + * + * # sort order defined by Ruby proc + * b = IntSet.new( proc { |a,b| a > b } ) + * + */ + +%include <rubystdfunctors.swg> + +%fragment("StdFunctors"); diff --git a/Lib/ruby/std_map.i b/Lib/ruby/std_map.i index 33fa9569c..cc22fef51 100644 --- a/Lib/ruby/std_map.i +++ b/Lib/ruby/std_map.i @@ -403,8 +403,6 @@ %mixin std::map "Enumerable"; -%include <rubystdfunctors.swg> - %rename("delete") std::map::__delete__; %rename("reject!") std::map::reject_bang; diff --git a/Lib/ruby/std_set.i b/Lib/ruby/std_set.i index 9d817f7b3..0385be708 100644 --- a/Lib/ruby/std_set.i +++ b/Lib/ruby/std_set.i @@ -60,7 +60,6 @@ %mixin std::set "Enumerable"; -%include <rubystdfunctors.swg> %rename("delete") std::set::__delete__; @@ -74,3 +73,4 @@ %include <std/std_set.i> + |