XORDE  v1.0
eXtensible Operational Robotic Development Environment
Signals | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ComponentBase Class Reference

The base for the ONB component. More...

#include <ComponentBase.h>

Inherits QObject.

Signals

void objectChanged (QString name)
 [DEPRECATED] More...
 

Public Member Functions

 ComponentBase (QObject *parent=nullptr)
 Creates component and binds service objects.
 
 ComponentBase (QString name, unsigned long classId, QString description=QString(), QObject *parent=nullptr)
 Construct the component with given name, classID and description.
 
virtual ~ComponentBase ()
 Deletes all its objects, including service objects.
 
unsigned short id () const
 Get componentID of the component. More...
 
unsigned long timestamp () const
 Get current timestamp. More...
 
QJsonObject getJsonConfig () const
 wut? whatta hell I see?? it is work for the core!!
 

Protected Member Functions

void setIcon (QImage icon)
 Set an icon to the component. More...
 
void setIcon (QString filename)
 Set an icon to the component from file. More...
 
template<class T >
ONBObject< T > & createInput (QString name, T &var)
 Create an input of the component. More...
 
template<class T >
ONBObject< T > & createOutput (QString name, T &var)
 Create an ouput of the component. More...
 
template<class T >
ONBObject< T > & createSetting (QString name, T &var)
 Create a setting of the component. More...
 
const ObjectInfoobject (QString name) const
 Get the object by name. More...
 
template<class T >
bool rebindObject (QString name, T &var)
 Rebind the existing object to another variable. More...
 
bool renameObject (QString oldName, QString newName)
 Rename existing object. More...
 
bool deleteObject (QString name)
 Delete existing object. More...
 
void sendObject (QString name)
 Send the object to the core immediately. More...
 
void touchOutput (QString name, bool notifyAnyway=false)
 Make the object ready to send. More...
 
virtual void onCreate ()
 Component creation event. More...
 
virtual void onDestroy ()
 Component deletion event. More...
 
virtual void objectRequestEvent (QString name)
 Event of object request. More...
 
virtual void objectReceiveEvent (QString name)
 Event of object receive. More...
 
virtual void objectChangeEvent (QString name)
 Event of object change. More...
 
void log (QString message)
 Send the arbitrary message to the core's log output.
 

Protected Attributes

QString componentName
 The name of the component. Used to name the type, must be unique.
 
unsigned long classID = 0
 ClassID of the component. Class identifier, must be unique. There is classification table (must be somewhere).
 
QString description
 Human-readable description for the help.
 
unsigned long serialNumber = 0
 Component's serial number. This field commonly used in devices, contains unique number associated with a certain device.
 
unsigned char & version
 Major version of the component.
 
unsigned char & versionMinor
 Minor version of the component.
 
QString releaseInfo
 This field can be filled with arbitrary information about component release (e.g. build date and time, release number etc.).
 
QString hardwareInfo
 This field commonly used in devices. Can contain CPU description or something else about hardware.
 
unsigned long burnCount = 0
 Historical thing from devices. Indicates how many times the device burned out. ]:->
 

Detailed Description

The base for the ONB component.

To create the component you should derive from this class. Declare the variables to be bound as members of your class. The component description is located in the constructor. There you must complete protected fields with necessary values. The required fields are componentName and classID. Then bind the variables by means of createInput(), createOutput() or createSetting(), set the meta-values with desired info, see ONBObject. Variables must be allocated if they are pointers or items of a container.

Attention
Don't perform any initialization of your logic in the constructor!

Your component should reimplement the virtual methods onCreate(), onDestroy() to initialize and terminate respectively the component logic (e.g. run/stop timers, threads).

Object flow is controlled by following methods: to issue an object use sendObject(), touchOutput(); when an object or object request is received, the virtual methods are called: objectReceiveEvent(), objectChangeEvent(), objectRequestEvent().

The component is able to change its objects at run-time. Just use createInput(), createOutput(), createSetting() during the run-time. Also you can use renameObject() and deleteObject() to modify the component appearance. To modify the object itself use rebindObject(), this method allows you to bind the existing object to another variable. Changing the meta-values associated with the object is not supported yet.

See also
ONBObject

Here is an example of ONB component implementing Gain function.
Gain.h

#include "ComponentBase.h"
class Gain : public ComponentBase
{
public:
Gain();
protected:
void onCreate() override;
void onDestroy() override;
void objectReceiveEvent(QString name) override;
private:
float value = 0;
float result = 0;
float gain = 1.0f;
};

Gain.cpp

#include "Gain.h"
Gain::Gain() :
ComponentBase("Gain", 0x12345678)
{
description = "Basic amplifier with specified gain";
version = 1;
createInput("value", value);
createOutput("result", result);
createSetting("gain", gain).def(1.0f);
}
void Gain::objectReceiveEvent(QString name)
{
if (name == "value")
{
result = value * gain;
touchOutput("result");
}
}
void Gain::onCreate()
{
// do_a_barrel_roll();
}
void Gain::onDestroy()
{
// rollback();
}

Member Function Documentation

template<class T >
ONBObject<T>& ComponentBase::createInput ( QString  name,
T &  var 
)
inlineprotected

Create an input of the component.

Creates a volatile writable object bound to a given variable. Also meta-values can be associated with the object.

Parameters
nameName of the object.
varReference to variable to bind.
Returns
reference to created object.
template<class T >
ONBObject<T>& ComponentBase::createOutput ( QString  name,
T &  var 
)
inlineprotected

Create an ouput of the component.

Creates a volatile readable object bound to a given variable. Also meta-values can be associated with the object.

Parameters
nameName of the object.
varReference to variable to bind.
Returns
reference to created object.
template<class T >
ONBObject<T>& ComponentBase::createSetting ( QString  name,
T &  var 
)
inlineprotected

Create a setting of the component.

Creates a non-volatile read-write object bound to a given variable. Also meta-values can be associated with the object.

Parameters
nameName of the object.
varReference to variable to bind.
Returns
reference to created object.
bool ComponentBase::deleteObject ( QString  name)
protected

Delete existing object.

Use this method to remove the object from component at run-time.

Parameters
nameThe name of existing object.
Returns
true if success, false if the object with given oldName does not exist.
unsigned short ComponentBase::id ( ) const
inline

Get componentID of the component.

After connection to the core, the componentID is assigned to the component.

Returns
Assigned componentID.
const ObjectInfo * ComponentBase::object ( QString  name) const
protected

Get the object by name.

If the object with given name is created, the method return a constant pointer to the object base. You can cast it manually to const ONBObject<T>* where T must match the type of bound variable.

Parameters
nameThe object name.
Returns
A constant pointer to the object. If an object with given name does not exist, nullptr is returned.
void ComponentBase::objectChanged ( QString  name)
signal

[DEPRECATED]

Deprecated:
Use objectChangeEvent() instead!
virtual void ComponentBase::objectChangeEvent ( QString  name)
inlineprotectedvirtual

Event of object change.

This method is similar to objectReceiveEvent() but it is called when the value of the variable was changed only.

Parameters
nameThe name of the requested object.
virtual void ComponentBase::objectReceiveEvent ( QString  name)
inlineprotectedvirtual

Event of object receive.

It is called when the core sends the object with the given name after its value is written to the bound variable. So you can use the variable with its actual value just inside this method.

Parameters
nameThe name of the requested object.
virtual void ComponentBase::objectRequestEvent ( QString  name)
inlineprotectedvirtual

Event of object request.

It is called when the core requests the object with the given name or the sampling interval has come. The event is called before sending object to the core. You should complete the bound variable with actual value when the event is arrived.

Parameters
nameThe name of the requested object.
virtual void ComponentBase::onCreate ( )
inlineprotectedvirtual

Component creation event.

It is called when component is created by core. Reimplement this method to initialize your component logic, e.g. run timers, threads, allocate buffers etc.

virtual void ComponentBase::onDestroy ( )
inlineprotectedvirtual

Component deletion event.

It is called when component is deleted by core. Reimplement this method to terminate your component logic, e.g. stop timers, threads, deallocate buffers etc.

template<class T >
bool ComponentBase::rebindObject ( QString  name,
T &  var 
)
inlineprotected

Rebind the existing object to another variable.

Use this method if the bound variable is moved or reallocated. This method also can be used for changing variable type at run-time.

Parameters
nameThe name of the object.
varReference to variable to bind.
Returns
true if rebind was successful, false if object does not exist.
bool ComponentBase::renameObject ( QString  oldName,
QString  newName 
)
protected

Rename existing object.

Use this method to rename the existing object at run-time. The new name must not match the existing objects' names.

Parameters
oldNameThe name of existing object.
newNameThe desired name.
Returns
true if success, false if the object with given oldName does not exist.
void ComponentBase::sendObject ( QString  name)
protected

Send the object to the core immediately.

If the object with given name does not exist the method does nothing.

Parameters
nameThe name of the object.
void ComponentBase::setIcon ( QImage  icon)
protected

Set an icon to the component.

Component icon is used in the UI.

Parameters
iconThe image containing icon.
void ComponentBase::setIcon ( QString  filename)
protected

Set an icon to the component from file.

This is an overloaded method.

Parameters
filenameName of icon file. Supported formats matches ones in Qt's QImage.
unsigned long ComponentBase::timestamp ( ) const

Get current timestamp.

The timestamp is measured in milliseconds since system start. Synchronization procedure between components is planned but not implemented yet.

Returns
Current timestamp value in milliseconds.
void ComponentBase::touchOutput ( QString  name,
bool  notifyAnyway = false 
)
protected

Make the object ready to send.

The object will be sent as soon as its next sampling interval has come. The object is sent immediately if the sampling interval is set to special value of zero. If the object with given name does not exist the method does nothing.

Parameters
nameThe name of the object.
notifyAnywayMaybe it's better to use sendObject()?