Interfacing with C++

When you compile a Cell program, the compiler does not generate a binary executable. Instead, it generates a number of text files containing code in the chosen output language. We'll examine the C++ code generator here. If you define a Main(..) procedure in you Cell code, the generated code can then be handed over to a C++ compiler to generate an executable file. That's how for instance the Cell compiler itself is built, and also the simplest way to build a program that tests your automata. But if you don't define a Main(..) procedure, the compiler will just generate a set of classes, one for each type of automaton in your Cell code, that can be used to instantiate and manipulate the corresponding automata from your C++ code. The compiler will produce two files, generated.cpp and generated.h. The first one contains the generated code and the second the declaration of the classes you'll be working with in your own C++ code.

It's often a good idea not to use the generated classes directly, but to derive your own classes from them, and add new methods (and/or member or class variables, if needed) there. Another slightly more laborious alternative is to write a wrapper class for them. Among other things, if a method of the generated classes requires some sort of manual data conversion, you really don't want to repeatedly perform those conversions all over your codebase: it's much better to have all of them in just one place. The best thing to do is probably to define an overloaded version of the same method, or a similar one, in the derived class, and have it take care of all data conversions before and/or after invoking the generated one.

Data conversion

The biggest issue one encounters when interfacing two languages as different as Cell and C++ is converting data back and forth between the two native representations. Fortunately, the compiler does most of the heavy lifting for you. For starters, there's a number of simple Cell data types that are mapped directly to a corresponding C++ type. They are shown in the following table:

Cell C++
Int long long
Float double
Bool bool
String string
T* vector<T>
[T] vector<T>
[K -> V] vector<tuple<K, V>>
[T1, T2] vector<tuple<T1, T2>>
[T1, T2, T3] vector<tuple<T1, T2, T3>>
(T1, T2, ..) tuple<T1, T2, ..>
any_tag(T) T
Maybe[T] T*/unique_ptr<T>

The first four entries in the above table are self-explanatory. The mapping for Cell sequences and sets is also straightforward, as both are mapped to C++ vectors. For example, the Cell type Int* is mapped to vector<long> in C++, and [String] is mapped to vector<string>. Similarly, maps and binary relations are mapped to arrays of 2-tuples, ternary relations to arrays of 3-tuples and Cell tuples are mapped to C++ ones. The couple of entries in the above list require a bit of explanation:

  • Tagged types can be mapped directly to a C++ type only if the tag is a known symbol. In this case, the tag is simply ignored and the mapping of the untagged value is used. A type like <user_id(Int)>, for example, will be mapped to a long long in C++, and the generated code will take care of adding or removing the user_id tag as needed.
  • For input data, that is, data that is passed as an argument to the methods of the generated classes, the Maybe[T] type is mapped directly to a pointer to the type of its parameter, and the value :nothing is mapped to NULL. Maybe[String], for example, is mapped to string *, and the value :just("Hello!") has to be passed as the C++ string "Hello!". When returning data from those same methods, Maybe[T] is instead mapped to unique_ptr<T>, so as to automatically take care of memory deallocation.

Not all types can be handled using the above mapping and that includes important practical cases like polymorphic and recursive types. We'll see how to deal with those more complex types later.

Relational automata

Let's now take a look at the classes that are generated when a relational automaton is compiled. We'll make use of a very simple one we've seen before, Counter:

schema Counter {
  value:   Int = 0;
  updates: Int = 0;
}

Counter.incr {
  set value = value + 1;
  set updates = updates + 1;
}

Counter.decr {
  set value = value - 1;
  set updates = updates + 1;
}

Counter.reset {
  set value = 0;
  set updates = updates + 1;
}

Counter.reset(Int) {
  set value = untag(this);
  set updates = updates + 1;
}

using Counter {
  Int value   = value;
  Int updates = updates;

  Bool is_greater_than(Int a_value) = value > a_value;

  (Int, Int) copy_state = (value, updates);
}

This is the interface of the corresponding C++ class generated by the Cell compiler:

namespace cell_lang {
  class Counter {
    public:
      Counter();
      ~Counter();

      void load(istream &);
      void save(ostream &);

      void execute(const string &);

      // Message handlers
      void incr();
      void decr();
      void reset();
      void reset(long long);

      // User-defined methods
      long long value();
      long long updates();
      bool is_greater_than(long long);
      tuple<long long, long long> copy_state();

    private:
      ...
  }
}

As you can see, the generated C++ class has the same name of the Cell automaton it derives from, and is declared in the cell_lang namespace. The first three methods, load(), save(..) and execute(..) are the same for all relational automata. All other methods are different for each automaton, and are used to send specific types of messages to it or to invoke its (read-only) methods.

The save(..) method is the equivalent of the Save(..) procedure in Cell, in that it takes a snapshot of the state of the automaton, which is written to the provided std::ostream. The state is saved in the standard text format used for all Cell values.

load(..) is used to set the state of an automaton instance, which is read from a std::istream, and is the equivalent of the Load(..) procedure in Cell. It can be used at any time in the life of the automaton instance, any number of times. The new state has to be provided in the standard text format. If the provided state is not a valid one, load(..) will throw an exception. In that case, the automaton instance will just retain the state it had before, and will still be perfectly functional.

execute(..) is used to send the automaton a message, which has to be passed in text form. A few examples:

counter.execute("incr");
counter.execute("decr");
counter.execute("reset");
counter.execute("reset(-1)");

Errors handling works in the same way as with load(..). If an error occurs an exception will be thrown, but the automaton will remain fully operational, and its state will be left untouched.

The next four methods, incr(), decr(), reset() and reset(long long) provides another way to send messages to the automaton:

counter.incr();    // Same as counter.execute("incr");
counter.reset(-1); // Same as counter.execute("reset(-1)");

They are a lot faster than execute(..), and usually they're more convenient too. There are cases though when the ability to generically send a message of any type to an automaton is crucial, so that's why the compiler provides two ways of doing the same thing.

When updating an automaton instance keep in mind that Cell does not (yet) provide a way to incrementally persist its state: every time you call the save(..) method the entire state of the automaton is saved. That's an expensive operation so typically you'll be performing it only once in a while. That means that you would have unsaved data in memory most of the time, which is of course at risk of being lost in the event of a crash. One simple and efficient way to avoid that is to store the list of messages that were received since the last save. If the application crashes, all you need to do when you restart it is to load the last saved state and re-send all the messages it received after that. That will recreate the exact same state you lost in the crash.

The last four methods, value(), updates(), is_greater_than(..) and copy_state(..), are just wrappers for the corresponding (read-only) methods of Counter.

You can see how in the signatures of the methods of the generated class the types of both arguments and return values are derived using the rules described in the previous paragraph. For example, is_greater_than(..) takes an argument of type Int and returns a value of type Bool, which become long long and bool respectively in C++. Similarly, copy_state returns a tuple in Cell, which is mapped to a C++ tuple, and the types of those fields are in turn mapped from Int to long long.

More on data conversions

What happens when the type of an argument or the return value of a method (or message handler) is too complex to be dealt with using the mapping described earlier? Here the default behavior of the compiler is to use the standard textual representation of Cell values as the data exchange format. Let's illustrate this with an example:

type Point = point(x: Int, y: Int);

type Shape = square(left: Int, bottom: Int, side: Nat),
             rectangle(left: Int, bottom: Int, width: Nat, height: Nat),
             circle(center: Point, radius: Int);

schema Canvas {
  ...
}

using Canvas {
  Shape* shapes_at(Point p) = ...
}

In this example, shapes_at(..) return a sequence of values of type Shape, which is a polymorphic type. By default the generated Canvas class will look like this:

namespace cell_lang {
  class Canvas {
    public:
      Canvas();
      ~Canvas();

      void load(istream &);
      void save(ostream &);

      vector<string> shapes_at(const string &);
  }
}

As you can see, the return type of shapes_at(..) is vector<string>, and the type of its parameter is const string &. Each string in the returned array is the textual representation of the corresponding Cell value. That is, if shapes_at(..) returns the following Cell value:

( square(left: 0, bottom: 0, side: 10),
  circle(center: point(x: 8, y: 3), radius: 5),
  rectangle(left: -25, bottom: 14, width: 4, height: 2)
)

then the caller of shapes_at(..) on the C++ side will get back the a C++ vector with the following content:

{ "square(left: 0, bottom: 0, side: 10)",
  "circle(center: point(x: 8, y: 3), radius: 5)",
  "rectangle(left: -25, bottom: 14, width: 4, height: 2)"
}

Similarly, if you want to pass shapes_at(..) the Cell value point(x: 5, y: -1), you'll have to pass the C++ string "point(x: 5, y: -1)".

Exchanging data in text form is not particularly elegant nor efficient, but it's at least simple and straightforward, and in some cases it works just fine. It tends to work better when passing data from C++ to Cell than in the other direction, since strings are easy to generate but difficult to parse.

As an alternative, you can ask the compiler to generate an equivalent C++ class for some of the types defined in your Cell code base. What you need to do is create a text file (let's call it types.txt) containing the list of types you want to generate (one type per line). Let's say for instance that you want to generate C++ classes for the Point and Shape types above. The content of types.txt would then look like this:

  Point
Shape

When you compile your code, you'll have to point the compiler to that file using the -g flag:

  cellc -g types.txt <project file> <output directory>

The interface of the generated Canvas class will now look like this:

namespace cell_lang {
  class Canvas {
    public:
      Canvas();
      ~Canvas();

      void load(istream &);
      void save(ostream &);

      vector<unique_ptr<Shape>> shapes_at(const Point &);
  }
}

As you can see, the return type of shapes_at(..) has now become vector<Shape>. This is the declaration of Shape:

namespace cell_lang {
  class Point {
    public:
      long long x;
      long long y;
  }

  class Shape {
    public:
      virtual ~Shape() {}
  }

  class Rectangle : public Shape {
    public:
      long long left;
      long long width;
      long long bottom;
      long long height;
  }

  class Circle : public Shape {
    public:
      Point      center;
      long long  radius;
  }

  class Square : public Shape {
    public:
      long long left;
      long long side;
      long long bottom;
  }
}

The definition of the Point class at the top is straightforward: the type Point, which in the Cell codebase is defined as a tagged record with two fields, x and y, of type Int, is mapped to a C++ class by the same name with two member variables x and y of type long long.

The mapping for Shape is a bit more complicated, since that's a polymorphic type. The compiler here has created a base class, Shape and three derived ones, Square, Rectangle and Circle each of which corresponds to one of the three alternatives in the Shape type definition. Shape in C++ is defined as an empty interface class, which is then implemented by the three concrete classes, so that they can be manipulated polymorphically.

In order for the compiler to be able to generate a corresponding C++ class for it, a Cell type definition has to obey a number of restrictions. Non-polymorphic types have to be defined as (possibly tagged) records with no optional fields or (possibly tagged) tuples. None of the following definitions for Point, for example, would allow the generation of a corresponding C++ class:

type Point = (x: Int, y: Int);
type Point = point(Int, Int);
type Point = point(x: Int, y: Int, z: Int?);

since either the type is not tagged, or it isn't a record, or the field z is optional (in the latter case though you can achieve the same result by making z mandatory and changing its type to Maybe[Int]). The rules are similar for polymorphic types: each alternative in a type union must be a tagged record, and the tags have to be different. You're free to define each alternative as a separate type though. Shape for example could have been defined as follow, without affecting the ability of the compiler to generated a corresponding C++ class for it:

type Square = square(left: Int, bottom: Int, side: Nat);
type Rect   = rectangle(left: Int, bottom: Int, width: Nat, height: Nat);
type Circle = circle(center: Point, radius: Int);

type Shape = Square, Rect, Circle;

Reactive automata

We'll use Switch as our first example. We defined it in a previous chapter as follows:

reactive Switch {
  input:
    switch_on  : Bool;
    switch_off : Bool;

  output:
    is_on : Bool;

  state:
    is_on : Bool = switch_on;

  rules:
    is_on = true  when switch_on;
    is_on = false when switch_off;
}

This is the interface of the corresponding C++ class:

namespace cell_lang {
  class Switch {
    public:
      enum Input {SWITCH_ON, SWITCH_OFF};

      enum Output {IS_ON};

      Switch();
      ~Switch();

      void set_input(Input input, const string &);
      string read_output(Output output);

      void apply();
      string read_state();
      void set_state(const string &);

      vector<Output> changed_outputs();

      // Inputs
      void set_switch_on(bool);
      void set_switch_off(bool);

      // Outputs
      bool is_on();
  }
}

The first thing to note here is the two enumerations Input and Output, whose elements are the uppercase version of the names of the inputs and outputs of Switch. These are used in conjunction with the methods set_input() and read_output() as shown here:

// Setting the value of the two inputs
switch.set_input(Input.SWITCH_ON, "true");
switch.set_input(Input.SWITCH_OFF, "false");

// Propagating the changes to the inputs
// throughout the automaton instance
switch.apply();

// Reading and printing the value of the only output
string is_on = switch.read_output(Output.IS_ON);
cout << "is_on = " << is_on << endl;

As an alternative to set_input(..) and read_output(..), which can operate on any input or output and use the textual representation of a value as a data exchange format, the generated class also provides another set of methods each of which can manipulate a single input or output, but that are more convenient to use in most cases. The above code snippet can be rewritten as follow:

// Setting the value of the two inputs
switch.set_switch_on(true);
switch.set_switch_off(false);

// Propagating the changes to the inputs
// throughout the automaton instance
switch.apply();

// Reading and printing the value of the only output
bool is_on = switch.is_on();
cout << "is_on = " << (is_on ? "true" : "false");

read_state() takes a snapshot of the state of the automaton and returns it in textual form. set_state(..) does the opposite: it sets the state of the automaton to whatever state is passed to it. Here too the new state has to be provided in textual form. When working with time-aware automata both methods are subjects to the limitations that we've already discussed in a previous chapter. The method changed_outputs() provides a list of outputs that have changed (or have been active, in the case of discrete outputs) as a result of the last call to apply():

// Changing inputs here
...

// Propagating those changes
switch.apply();

// Iterating through the outputs that have changed
// if continuous or have been activated if discrete

vector<Switch::Output> changed_outputs = switch.changed_outputs();
for (Switch::Output output_id : changed_outputs) {
  // Reading the value of the changed output
  string value = switch.read_output(output_id);

  // Now time to do something with the value of the output
  ...
}

The last thing we need to see is how to deal with time-aware automata. We'll use WaterSensor, whose definition is copied here:

type WaterSensorState = initializing, unknown, submerged(Bool);

reactive WaterSensor raw_reading -> sensor_state {
  input:
    raw_reading* : Maybe[Bool];

  output:
    sensor_state : WaterSensorState;

  state:
    sensor_state : WaterSensorState = :initializing;

  rules:
    good_reading := value(raw_reading) if raw_reading != :nothing;
    too_long_without_readings = 30s sans good_reading;
    sensor_state = :submerged(good_reading);
    sensor_state = :unknown when too_long_without_readings;
}

This is the interface of the generated C++ class:

namespace cell_lang {
  class WaterSensor {
    public:
      enum Input {RAW_READING};

      enum Output {SENSOR_STATE};

      WaterSensor();
      ~WaterSensor();

      void set_input(Input input, string value);
      string read_output(Output output);

      void set_elapsed_millisecs(unsigned int);
      void set_elapsed_secs(unsigned int);

      bool apply();
      string read_state();
      void set_state(const string &);

      vector<Output> changed_outputs();

      // Inputs
      void set_raw_reading(bool *);

      // Outputs
      string sensor_state();
  }
}

The only differences here, apart from the input setters and output getters which are obviously specific to each automaton type, are the two extra methods set_elapsed_secs(..) and set_elapsed_millisecs(..) and the fact that apply() now returns a boolean value. The former are the equivalent of the elapsed instruction in Cell, and the value now returned by apply() has the same meaning as the one returned by the apply instruction in a Cell procedure. Here's an example of how to update an instance of WaterSensor:

// Updating the values of the inputs here
...

// Setting the amount of time that has elapsed
// since the last call to waterSensor.Apply()
waterSensor.set_elapsed_millisecs(100);

do {
  // Repeatedly calling Apply() until it returns true
  // That happens only once all pending timers have
  // been processed and the changes in the values of
  // the inputs propagated throughout the automaton
  bool done = waterSensor.apply();

  // Iterating through the outputs that have changed
  // if countinuous or have been activated if discrete
  vector<WaterSensor::Output> changed_outputs = switch.changed_outputs();
  forh (WaterSensor::Output output_id : changed_outputs) {
    // Reading the value of the changed output
    string value = waterSensor.read_output(output_id);

    // Now time to do something with the value of the output
    ...
  }
} while (!done);