Chapter 2. The UObject API

Table of Contents

Creating a class, binding variables and functions
Creating new instances
Notification of a variable change or access
Timers
The special case of sensor/effector variables
Using URBI variables
Using binary types
Using hubs to group objects
Sending URBI code

Creating a class, binding variables and functions

Let’s illustrate those concepts by defining a simple object: adder. This object has one variable v, and a method ’add’ that returns the sum of this variable and its argument.

First the required include and namespace.

#include <uobject.hh>

Then we declare our adder class:

class adder : public urbi::UObject // must inherit from UObject
{
  public:
     // the class must have a single constructor taking a string
     adder (const std::string&);

     // our variable
     urbi::UVar v;

     // our method
     double add (double);
};

Finally the implementation of the constructor and our add method.

// the constructor defines what is available from URBI
adder::adder (const std::string& s)
  : UObject (s) // required
{

  // macro used to bind variables
  UBindVar (adder,v);

  // macro used to bind methods
  UBindFunction (adder, add);
}

double
adder::add (double rhs)
{
  return v + rhs;
}

// register the class to the URBI kernel.
UStart (adder);

To summarize:

  • Declare your object class as inheriting from urbi::UObject.

  • Declare a single constructor taking a string, and pass this string to the constructor of urbi::UObject.

  • Declare the variables you want to share with URBI with the type urbi::UVar.

  • In the constructor, call UBindVar (classname, varname) for each UVar you want as an instance variable, and UBindFunction (classname, functionname) for each function you want to bind.

  • Don’t forget to call the macro UStart for each object.