This example demonstrates how to provide additional functionality in form of plugins. It extends the previous example about user-defined classes.
In contrast to the previous application, the functionality of the IMy_class
interface will be provided to the main application via a plugin. Therefore, the main application only needs the definition of the interface IMy_class
and the compiled plugin, but not the source code of the implementation.
New Topics
- Implementation of a plugin
- Loading a plugin
Detailed Description
- Implementation of a plugin
To implement a plugin you need to provide an implementation of the mi::neuraylib::IPlugin interface. An example implementation of such a class is shown in the code below. The most important part is the implementation of the mi::neuraylib::IPlugin::init() method which has to register all user-defined classes.
Finally you need to provide a factory function called mi_plugin_factory()
that creates an instance of your implementation of mi::neuraylib::IPlugin. Again, using the example implementation as shown in the code below is fine.
You need to compile the implementation of your interfaces, your implementation of mi::neuraylib::IPlugin, and the factory function, and to create a DSO from the object code. Using g++
you can use commands similar to
g++ -I$NEURAY_ROOT/include -fPIC -c plugin.cpp -o plugin.o
g++ -shared -export-dynamic --whole-archive plugin.o -o libplugin.so
- Loading a plugin
You can load plugins by passing the filename to the method mi::neuraylib::IPlugin_configuration::load_plugin_library(). After the plugin has been loaded (in a way similar to the loading of the DSO for the Iray SDK API itself), the method mi::neuraylib::IPlugin::init() is called. In this method you need to register all user-defined classes.
Using the user-defined class works the same way as for all other classes. In particular there is no difference to user-defined classes defined in the main application as in the previous example.
Source Code Location: example/plugin.cpp
#include "my_class.h"
{
public:
virtual ~Plugin_impl() { }
const char*
get_name()
const {
return "example plugin"; }
{
logging_configuration->get_forwarding_logger());
"Plugin_impl::init() called");
if( ! extension_api.is_valid_interface())
return false;
return extension_api->register_class<My_class>( "My_class") == 0;
}
{
logging_configuration->get_forwarding_logger());
"Plugin_impl::exit() called");
return true;
}
};
extern "C"
MI_DLL_EXPORT
void* context)
{
if( index != 0)
return 0;
return new Plugin_impl;
}
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
The abstract base class for plugins.
Definition: plugin.h:34
This interface is used to extent the Iray SDK API.
Definition: iextension_api.h:32
This interface is used for configuring the logging for the Iray library.
Definition: ilogging_configuration.h:68
This abstract interface gives access to the Iray SDK API to plugins.
Definition: iplugin_api.h:27
virtual base::IInterface * get_api_component(const base::Uuid &uuid) const =0
Returns an API component from the Iray SDK API.
The basic interface to be implemented by Iray SDK API plugins.
Definition: iplugin.h:28
virtual bool exit(IPlugin_api *plugin_api)=0
De-initializes the plugin.
virtual bool init(IPlugin_api *plugin_api)=0
Initializes the plugin.
@ MESSAGE_SEVERITY_INFO
This is a normal operational message.
Definition: enums.h:39
virtual const char * get_name() const =0
Returns the name of the plugin.
virtual const char * get_type() const =0
Returns the type of the plugin.
virtual Sint32 get_version() const
Returns the version number of the plugin.
Definition: plugin.h:57
virtual void release()=0
Destroys the plugin instance.
virtual const char * get_compiler() const
Returns the compiler used to compile the plugin.
Definition: plugin.h:60
signed int Sint32
32-bit signed integer.
Definition: types.h:46
#define MI_NEURAYLIB_PLUGIN_TYPE
Type of plugins for the Iray SDK API.
Definition: version.h:80
Source Code Location: examples/example_plugins.cpp
#include "example_shared.h"
#include "imy_class.h"
{
check_success( database.is_valid_interface());
database->get_global_scope());
scope->create_transaction());
check_success( transaction.is_valid_interface());
check_success( my_class.is_valid_interface());
my_class->set_foo( 42);
transaction->store( my_class.get(), "some_name");
my_class = 0;
my_class = transaction->edit<IMy_class>( "some_name");
check_success( my_class.is_valid_interface());
check_success( my_class->get_foo() == 42);
my_class = 0;
transaction->commit();
}
int main( int , char* [])
{
check_success( neuray.is_valid_interface());
check_success( plugin_configuration->load_plugin_library(
plugin_configuration = 0;
check_start_success( result);
test_plugin( neuray.get());
check_success( neuray->
shutdown() == 0);
neuray = 0;
check_success( unload());
keep_console_open();
return EXIT_SUCCESS;
}
This interface is used to interact with the distributed database.
Definition: idatabase.h:293
This is an object representing the Iray library.
Definition: ineuray.h:44
virtual Sint32 shutdown(bool blocking=true)=0
Shuts down the library.
virtual base::IInterface * get_api_component(const base::Uuid &uuid) const =0
Returns an API component from the Iray SDK API.
virtual Sint32 start(bool blocking=true)=0
Starts the operation of the Iray library.
This interface is used to load plugins and to query information about loaded plugins.
Definition: iplugin_configuration.h:24
#define MI_BASE_DLL_FILE_EXT
The operating system specific default filename extension for shared libraries (DLLs)
Definition: config.h:340