Prev: Asynchronous Request Handling
Native Objects
The objects produced by native modules are not restricted to
m types. For instance, the streams returned by
io.open are
native objects which cannot be manipulated from within
m, only by module
io and a few other modules.
But native objects are not restricted to streams: an instance of any subclass of CBase can be wrapped into a native object. When doing so, the instance will automatically be deleted if the native object containing it is freed by garbage collection.
Each native object has a type which is a zero terminated character string. The type is used to check for a particular object type (C++ runtime types are not supported by Symbian), and when converting the object to a string, e.g. when printing it to the console. For instance, native streams have the type
/**
The type of all native I/O streams.
*/
static const char * const TYPE = "stream";
|
Native objects are created by the following function from class Runtime:
- Runtime::Value NewNativeL(CBase *obj, const char *type);
Creates a new native object. The object obj must not be on the cleanup stack, and is destroyed when this function leaves.
To access native objects,
Runtime::Value offers the following methods:
- TBool IsNative() const;
Checks whether this value is a native object.
- NativeObject &GetNativeL() const;
Gets this value as a native object, leaving if it isn't a native object.
- CBase *GetNativeObjL(const char *type) const;
Gets the object defined by this native object. Leaves if this isn't a native object, or if it has a different type.
And here is a sample fragment using native objects:
class MyObject : public CBase {
public:
static const char * const TYPE = "myobject";
static MyObject *NewL();
...
TInt count() const;
}
Runtime::Value ExecuteL(TInt index, Runtime::Value *params,
TInt paramCount, TRequestStatus &status)
{
MyObject *obj;
Runtime::Value result;
switch (index) {
case NewFunction:
// create a native object around a MyObject instance
obj = MyObject::NewL();
result = runtime->NewNativeL(obj, MyObject::TYPE);
break;
case CountFunction:
// return the result of obj->count()
obj = (MyObject*)params[0].GetNativeObjL(MyObject::TYPE);
result.SetNumber(obj->count());
break;
...
|
Next: Native Streams© 2004-2011 airbit AG, CH-8008 Zürich
Document AB-M-NMI-887