This example demonstrates the implemention of database elements and jobs.
- Implementation of database elements
Database elements are derived from the mi::neuraylib::IElement interface. There are two ways to accomplish this:
- Derive your own interface
IMy_element
from mi::base::Interface_declare<...,mi::neuraylib::IElement> and implement it in My_element
derived from mi::base::Interface_implement<IMy_element>
.
- Derive your implementation without separate interface from the mixin class mi::neuraylib::Element.
The first approach is required to implement interface hierarchies. It follows the design principle of interfaces with only pure virtual methods and separate implementation classes. The second approach eliminates the need for new interfaces. The mixin class provides default implementations for some of the pure virtual methods. This and the next example use the second approach, not only for database elements, but also for jobs and fragmented jobs.
To implement a database element, you need to provide implementations (at least) for the methods serialize()
, deserialize()
, copy()
, and get_class_name()
. In case you store references to other elements, you need to implement get_references()
as well. There are several other methods for which the mi::neuraylib::Element mixin provides default implementations. In addition you can implement methods of your choice according to the desired functionality of your class. In this example, there is only a setter and getter for the single data value.
- Implementation of jobs
To implement a database job, you need, similar to database elements, to provide implementations (at least) for the methods serialize()
, deserialize()
, copy()
, and get_class_name()
. The actual job is implemented in the execute()
method. There are several other methods for which the mi::neuraylib::Job mixin provides default implementations.
In this example the job class references another database element. The job creates another instance of such a database element, where the value of the data member has been squared.
- Using database elements and jobs
To use your database elements and jobs, and in general, to use the DiCE API, two steps are required:
You can register your classes with the DiCE API using mi::neuraylib::IDice_configuration::register_serializable_class(). A DiCE transaction can be obtained from a regular transaction by means of its get_interface()
method.
The DiCE transaction offers similar to a regular transaction methods to store, retrieve, and remove database elements. Elements are identified by tags. Names can be associated with tags for the users convenience but are not used otherwise.
Jobs are executed if their tag is accessed for the first time (and the job has not been executed yet). However, it is strongly recommended to call advise()
for such tags first. This allows the parallel execution of jobs, e.g., in other threads or on other hosts.
#ifndef EXAMPLE_ELEMENT_AND_JOB_H
#define EXAMPLE_ELEMENT_AND_JOB_H
class My_element
:
public mi::neuraylib::Element<0xc3711242,0x5aed,0x4bd7,0xa0,0xa4,0x9c,0x2d,0x7f,0x48,0xfc,0x40>
{
public:
My_element(
mi::Uint64 data = 0) : m_data( data) { }
const char* get_class_name() const { return "My_element"; }
void set_data(
mi::Uint64 data) { m_data = data; }
private:
};
class My_job
:
public mi::neuraylib::Job<0x55ae55aa,0x7996,0x4b49,0xba,0xd8,0xe2,0xdc,0x4b,0xa4,0x85,0x57>
{
public:
const char* get_class_name() const { return "My_job"; }
{
mi::Uint64 result = element->get_data() * element->get_data();
return new My_element( result);
}
private:
};
#endif
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
This mixin class can be used to implement the mi::neuraylib::IElement interface.
Definition: dice.h:1542
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.
A transaction provides a consistent view on the database.
Definition: dice.h:272
virtual const base::IInterface * access(Tag_struct tag)=0
Retrieves an element from the database.
This interface represents the abstract base class for all database elements.
Definition: dice.h:837
This interface represents the base class for all database jobs.
Definition: dice.h:957
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.
This mixin class can be used to implement the mi::neuraylib::IJob interface.
Definition: dice.h:1653
A tag represents a unique identifier for database elements in the database.
Definition: iserializer.h:54
unsigned long long Uint64
64-bit unsigned integer.
Definition: types.h:62
#include "example_shared.h"
#include "example_element_and_job.h"
{
check_success( !element_tag);
check_success( element_tag);
check_success( element_tag2 == element_tag);
check_success( access1->get_data() == 42);
edit->set_data( 2);
edit = 0;
check_success( access1->get_data() == 42);
check_success( access2->get_data() == 2);
check_success( !job_tag);
check_success( job_tag);
check_success( job_tag2 == job_tag);
transaction->
advise( job_tag);
check_success( access3->get_data() == 4);
}
{
check_success( dice_configuration.is_valid_interface());
check_success( dice_configuration->register_serializable_class<My_element>());
check_success( dice_configuration->register_serializable_class<My_job>());
}
{
check_success( database.is_valid_interface());
check_success( transaction.is_valid_interface());
test_my_element_and_my_job( transaction.get());
}
int main( int , char* [])
{
check_success( neuray.is_valid_interface());
configuration( neuray.get());
check_start_success( result);
run( 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 interface allows configuration of DiCE.
Definition: dice.h:82
virtual Tag_struct name_to_tag(const char *name)=0
Returns the tag associated with a name in the database.
virtual base::IInterface * edit(Tag_struct tag)=0
Retrieves an element from the database and returns it ready for editing.
virtual Tag_struct store(IElement *element, Tag_struct tag=NULL_TAG, const char *name=0, Privacy_level privacy_level=LOCAL_SCOPE)=0
Stores a new database element in the database.
virtual void advise(Tag_struct tag)=0
Advises the database that a given tag is required soon.
virtual Sint32 commit()=0
Commits the transaction.
This is an object representing the DiCE 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 DiCE API.
virtual Sint32 start(bool blocking=true)=0
Starts the operation of the DiCE library.
signed int Sint32
32-bit signed integer.
Definition: types.h:46