Previous: , Up: Using the Dezyne locator to distribute runtime objects   [Contents] Distributing your own data

As mentioned in the introduction of this section, you can store any object in the dzn::locator, with the limitation that there can only be one object for any given type T in the locator at any point in time. Aside from overwriting runtime objects to provide a custom implementation, you can also use the locator to distribute data objects through the System, for example to be used in foreign components. Recall that foreign components are constructed by the System and their constructors require a dzn::locator by reference:

  Siren(const dzn::locator& loc);
  Sensor(const dzn::locator& loc);
  LED(const dzn::locator& loc);
  PWManager(const dzn::locator& loc);

Within the implementation of the constructor, you can retrieve objects from the locator that is passed as parameter. This can be useful for initializing hardware. In the example implementations, we used hard-coded values for the GPIO pins. With the dzn::locator, you can distribute an object containing hardware configurations gathered from command line arguments to make the application more dynamic. This would be done by using the set() function to inject a configuration object into the locator, followed by using try_get() in the constructor of a foreign component. try_get() will return a pointer to the object of type T if it is found, or null if it is not found.

Say we have the following configuration struct:

struct HWConfig {
  int GPIO_Red, GPIO_Green, GPIO_Blue;

In your main containing the event loop, you could initialize this struct with the use of command line parameters. If you then use set() on your locator with the initialized HWConfig struct, it will be stored in the locator. In the implementation of the LED constructor, you can do the following:

HWConfig *config = loc.try_get(HWConfig);
if(config) {
  this->PIN_RED = config->GPIO_Red;
  this->PIN_GREEN = config->GPIO_Green;
  this->PIN_BLUE = config->GPIO_Blue;

With this addition, the default GPIO pin definitions will be overwritten if you supplied a HWConfig struct in the dzn::locator. If you didn’t add the struct, try_get will not be able to find a HWConfig struct and will return 0, so the if-statement will not be entered. If this is the case, you can simply default to preconfigured values.

Previous: , Up: Using the Dezyne locator to distribute runtime objects   [Contents]