All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Scripting::ClassWrapper Class Reference

Makes conversion between JS and C++ objects This only works with C++ classes that are already wrapped to JS code Example: ClassWrapper rectWrapper(context, "Nimble.Rectf"); Nimble::Rectf * rect = new Nimble::Rectf(); v8::Local<v8::Value> wrappedRect = widgetWrapper.wrap(rect); or in another direction: Nimble::Rectf * rect = wrappedRect.unwrap<Nimble::Rectf>(wrappedRect); if (rect) { ... More...

#include <JS/Context.hpp>

Public Member Functions

 ClassWrapper (const QString &name)
 Creates a new wrapper object using the default JS context. More...
 
 ClassWrapper (Scripting::JSContext &context, const QString &name)
 Creates a new wrapper object. More...
 
template<typename T >
v8::Local< v8::Value > wrap (T *t)
 Wraps C++ pointer to v8 object. More...
 
 ~ClassWrapper ()
 Deletes the wrapper.
 

Static Public Member Functions

template<typename T >
static T * unwrap (v8::Local< v8::Value > value)
 Unwraps C++ object from JS object. More...
 

Detailed Description

Makes conversion between JS and C++ objects This only works with C++ classes that are already wrapped to JS code Example: ClassWrapper rectWrapper(context, "Nimble.Rectf"); Nimble::Rectf * rect = new Nimble::Rectf(); v8::Local<v8::Value> wrappedRect = widgetWrapper.wrap(rect); or in another direction: Nimble::Rectf * rect = wrappedRect.unwrap<Nimble::Rectf>(wrappedRect); if (rect) { ...

}

Template parameter must always be the base class. For example MultiWidgets::Widgets can be converted through Serializable: v8::Local<v8::Value> wrappedWidget = ...; auto tmp = ClassWrapper::unwrap<Valuable::Serializable>(wrappedWidget); MultiWidgets::WidgetPtr w(dynamic_cast<MultiWidgets::Widget*>(tmp));

Constructor & Destructor Documentation

Scripting::ClassWrapper::ClassWrapper ( const QString &  name)

Creates a new wrapper object using the default JS context.

Parameters
nameJavascript name for the wrapped object, for example "MultiWidgets.ImageWidget"
Scripting::ClassWrapper::ClassWrapper ( Scripting::JSContext context,
const QString &  name 
)

Creates a new wrapper object.

Parameters
contextJS context that contains the object with given name
nameJavascript name for the wrapped object, for example "MultiWidgets.ImageWidget"

Member Function Documentation

template<typename T >
static T* Scripting::ClassWrapper::unwrap ( v8::Local< v8::Value >  value)
inlinestatic

Unwraps C++ object from JS object.

Template Parameters
TC++ type, always use the base class for inherited types, and then use dynamic_cast with the result
Parameters
valueJS object
Returns
wrapped C++ object or null if conversion fails, or if the wrapped object is null
template<typename T >
v8::Local<v8::Value> Scripting::ClassWrapper::wrap ( T *  t)
inline

Wraps C++ pointer to v8 object.

Parameters
tC++ object to wrap