| ||||
|
Instantiating ObjectsBy default, both the host and a plugin is free to instantiate objects. This can happen in some different ways: Also, at library load time, there is the possibility of passing a reference to an object to the library and receiving one in return (strictly speaking not instantiation):Instantiating from a loaded libraryLibraries are loaded using the DynObjLib class, either directly or indirectly through, doRunTime::LoadLib().Suppose we have a plugin library named TestPlugin and want to create an object implementing TestI with type ID TEST_TYPE_ID:
DynObjLib *plib = /* Loaded library from somewhere */; Instantiating using doRunTimeIThe doRunTimeI interface gives access to a global singleton which provides shared functionality to host and plugins. It can also be used to manage open libraries and to instantiate objects:TestI *pti = DORT.Create<TestI>("TestPlugin",NULL); It ends up simpler and we don't need to do any type casts. DORT is a macro returning a reference to the doRunTime singleton, for both applications and plugins.Creating with do_new<T>()We can also use a syntax close to that of C++ new:TestI *pti = do_new<TestI>(); DynObj will select the first library that is registered as capable of instantiating this interface. This is most useful for interfaces that are only intended to have a single implementation (one-to-one interface).Instantiating using global declarationsIn a module implementing an interface, there is nothing stopping us from using global instances of the implementation, and exposing this object. Suppose we want to expose a string converter (StrConvI) we're implementing in a library:
Note: We add Notifier to our base class list to enable our object to be safeky tracked. Otherwise, the ObjectPublish function may fail (it only allows references to trackable objects). In this way, users of FastStrConv are safe, even if this plugin is unexpectedly unloaded. As parameter or return value at library load timeAt library load time, it is possible to pass an object to the library being loaded and to receive an object reference in return. Using doRunTime we do:
DynI *pdi_init = /* Get init object from somewhere */;
Note: Both the object passed to the library init function and the one returned from it should be trackable. The simplest way to do that is to add Notifier or GNotifier to the base class list (see object ownership). |