This example demonstrates the implementation and usage of user-defined classes to be used in conjunction with the Iray SDK API. In this example the new class is defined in and used by the main application. In contrast, the next example demonstrate how to provide additional functionality with user-defined classes via plugins.
A simple class called My_class
with a corresponding interface class IMy_class
will be used in this example to demonstrate the required steps.
New Topics
- Definition of the interface of a user-defined class
- Implementation of a user-defined class
- Registration and usage of a user-defined class
Detailed Description
- Defining the interface of a user-defined class
To define user-defined classes you need to provide an interface with the public methods of your class. The interface is an abstract base class which contains only pure virtual methods. The interface must be (directly or indirectly) derived from mi::neuraylib::IUser_class.
The easiest way to define such an interface is by deriving from the mixin class mi::base::Interface_declare. The first eleven template parameters are used to construct the mi::base::Uuid (which must be unique). The last template parameter denotes the actual interface to derive from (should be mi::neuraylib::IUser_class, or another interface (directly or indirectly) derived from mi::neuraylib::IUser_class).
This definition of the interface is required by the applications (or other plugins) in which the user-defined class is supposed to be used.
- Implementation of a user-defined class
You need to implement the interface defined in IMy_class
.
Deriving your implementation from the mixin mi::neuraylib::User_class is required to ensure that your implementation class works together with the class framework, including features like creation, serialization and deserialization. Note that if you derive your implementation class directly from your interface or indirectly via mi::base::Interface_implement it will not correctly work together with the class framework.
To make serialization and deserialization work, you need to implement mi::neuraylib::ISerializable::serialize() and mi::neuraylib::ISerializable::deserialize(). In this example it is sufficient to write the single member m_foo
to the serializer and to read it from the deserializer, respectively. Furthermore, you need to implement mi::neuraylib::IUser_class::copy() which has to create a copy of a given object.
- Registration and usage of a user-defined class
Finally the example demonstrates how to use the user-defined class. Before using the new class it is necessary to register it with the Iray SDK API. This can be accomplished by mi::neuraylib::IExtension_api::register_class().
Using the user-defined class works the same way as for all other classes. You can use mi::neuraylib::ITransaction::create() to create new instances; you can store them in the database and retrieve them from the database. Here, the example just does a simply test of the serialization and deserialization functionality.
Example Source
Source Code Location: example/imy_class.h
#ifndef IMY_CLASS_H
#define IMY_CLASS_H
class IMy_class : public
mi::base::Interface_declare<0x7274f2a1,0xb7e1,0x4fc7,0x8c,0xe4,0xef,0x2d,0xd0,0x9b,0x75,0x52,
mi::neuraylib::IUser_class>
{
public:
};
#endif
Mixin class template for deriving new interface declarations.
Definition: interface_declare.h:43
signed int Sint32
32-bit signed integer.
Definition: types.h:46
Source Code Location: example/my_class.h
#ifndef MY_CLASS_H
#define MY_CLASS_H
#include "imy_class.h"
class My_class : public
mi::neuraylib::User_class<0x0650d689,0xef50,0x433f,0xb3,0xae,0xae,0x83,0xa9,0xf2,0xf2,0xa6,
IMy_class>
{
public:
My_class() { m_foo = 0; }
~My_class() { }
{
serializer->
write( &m_foo);
}
{
deserializer->
read( &m_foo);
}
{
My_class* copy = new My_class( *this);
return copy;
}
const char* get_class_name() const
{
return "My_class";
}
{
return 0;
}
private:
};
#endif
This interface represents static arrays, i.e., arrays with a fixed number of elements.
Definition: iarray.h:37
Source for deserializing objects from byte streams.
Definition: ideserializer.h:35
virtual bool read(bool *value, Size count=1)=0
Reads values of type bool from the deserializer.
Target for serializing objects to byte streams.
Definition: iserializer.h:171
virtual bool write(const bool *value, Size count=1)=0
Writes values of type bool to the serializer.
A transaction provides a consistent view on the database.
Definition: itransaction.h:81
Abstract interface for user-defined classes.
Definition: iuser_class.h:37
This mixin class should be used to implement the IUser_class interface.
Definition: iuser_class.h:92
Source Code Location: examples/example_user_defined_classes.cpp
#include "example_shared.h"
#include "imy_class.h"
#include "my_class.h"
{
check_success( extension_api.is_valid_interface());
check_success( extension_api->register_class<My_class>( "My_class") == 0);
}
{
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);
check_success( transaction->store( my_class.get(), "some_name") == 0);
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->set_foo( 43);
my_class = 0;
transaction->commit();
}
int main( int , char* [])
{
check_success( neuray.is_valid_interface());
configuration( neuray.get());
check_start_success( result);
test_plugin( neuray.get());
check_success( neuray->
shutdown() == 0);
neuray = 0;
check_success( unload());
keep_console_open();
return EXIT_SUCCESS;
}
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
This interface is used to interact with the distributed database.
Definition: idatabase.h:293
This interface is used to extent the Iray SDK API.
Definition: iextension_api.h:32
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.