On Thu, 28 December 2000, "Lyle Johnson" wrote:

> 
> All,
> 
> I'm using the Ruby module in SWIG1.3a5 to attempt to generate wrappers for a
> C++ class library. It was going fairly well but I've run into the first
> snag. Before I spend too much time on this problem I wanted to see if any
> others have run into it and have come up with a solution.
> 
> In its current implementation, when SWIG/Ruby generates wrappers a the C++
> class constructor it defines a singleton method for "new", e.g.
> 
>     rb_define_singleton_method(cMyBaseClass, "new",
> wrap_new_MyBaseClass, -1);
> 
> The wrapper function (wrap_new_MyBaseClass) basically collects the input
> arguments, calls the C++ constructor, and wraps up the result using Ruby's
> Data_Wrap_Struct() function. This is a pretty reasonable approach but it
> seems to prevent me from then overriding the initialize() function in my
> Ruby code, when I derive new classes from MyBaseClass:
> 
>     require 'MyExtensionModule'
> 
>     class MyDerivedClass < MyExtensionModule::MyBaseClass
>         def initialize(one, two)
>             # Base class constructor requires four arguments
>             super(one, two, "Foo", "Bar")
>         end
>     end
> 
>  If I try to create an instance of MyDerivedClass:
> 
>     thing = MyDerivedClass.new("first argument", "second argument")
> 
> I get an error indicating that "new" wants four input arguments instead of
> the two I've provided. It looks like the wrapper code for "MyBaseClass"
> intercepts the call to "new" and never even tries to invoke "initialize".
> 
> So before I try to do something clever hacking in SWIG, do any SWIG/Ruby
> veterans have any thoughts on this problem?
> 
> TIA,
> 
> Lyle

Lyle:

I am not using SWIG but an internal function check the number or args you pass during initialization.
Example:

int my_object_new(int argc, VALUE *argv, VALUE klass)
{
  VALUE a, b c, obj;
   ...........
   ...........
  rb_scan_args(argc, argv, "13", &a, &b, &c);
  if (!NIL_P(b)
  { 
    b_initialized_var = STR2CSTR(b);
  }
  else b_initialized_var = some_default;
// same thing for c ....
  ..........
   return obj;
}  
   ...........
 The 'magic' string "13" means:
  First digit '1' tells that at least comes one argument, in this example named 'a'
  Second digit '3' tells that maximun allowed args will be 3. ('a' and/or 'b' and/or 'c').
  then you must check existence of not mandatory args with function NIL_P(some_arg), and if it fails (false return) you perform default initialization. So you can create a new object with at least one arg, and/or two and/or three (in this example). Check the magic string if it set correctly. For your class it would be "24"
(two first mandatory args plus two optative args)
Sorry about my english.
Arturo Borquez
Chile, South America
email aborquez / altavista.com


Find the best deals on the web at AltaVista Shopping!
http://www.shopping.altavista.com