Hi Bradley,

thanks for the reply, but maybe I have not expressed clearly: I don't want 
to *convert* my c++ classes to javascript.
Instead, I create a *custom module* with *node::ObjectWrap*

class AnimalWrap : public node::ObjectWrap {
  public:
    static Handle<Value> sound(const Arguments &args);
    static Persistent<FunctionTemplate> constructor_tpl;

  protected:
    int typeId;
}

Now, the Dog, that inherits from Animal should also have a Wrapper.
*Option (1):*
class DogWrap : public AnimalWrap, public Dog {
  protected:
    bool houseTrained;
}

*Option (2):*
template <class A> class AnimalWrap {...};
template <> AnimalWrap<Dog> {
  protected:
    bool houseTrained;    
};

With multiple inheritance (1), I would not know how to check if Dog is a 
Dog but not any other animal, since "constructor_tpl" would not be 
specialized for Dog. Also, I cannot overwrite sound() in Dog, because it is 
static and would not be virtually evaluated to Dog::sound() if called from 
other functions in Animal::*.
With template specialization (2), all the static code, like sound() would 
have to be reimplemented in AnimalWrap<Dog>.

tl;rt
Wrapping one class with ObjectWrap is perfectly fine, but how to wrap an 
existing C++ class hierarchy?



Am Montag, 10. Dezember 2012 20:40:11 UTC+5:30 schrieb Bradley Meck:
>
> Encapsulated bindings.
>
> Make all bindings available w/ some naming convention for the namespaces.
> Have a constructor function per type
> * Have Animal.prototype be the members of Animal::
> * Have Dog.prototype be the members of Dog::
> Attach the static functions to constructor functions
>
> Add sugar if you need a destructor / be sure to call the parent function 
> constructor inside of subclasses:
>
> function Dog() {
>   Animal.call(this);
>   return this;
> }
>
> Checking for instance type:
>
> // since people should be using our constructor that maps to an instance 
> via ObjectWrap....
> if (spot instanceof Dog) { ... }
>
> When using virtual static functions [ :-( ] you can add them to the 
> constructor (this pattern is highly discouraged generally):
>
> function Dog() { ... }
> var DogStatics = Object.create(AnimalStatics)
> ...
> Object.keys(DogStatics).forEach(function (key) {
>   Dog[key] = DogStatics[key]
> });
>
> In general however, directly mapping C++ onto JS will meet with some 
> confusion as the mentalities of the languages are vast, so be sure that 
> this is what you want to do.
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to