[odb-users] Curiously recurring template and odb

Alexandre Pretyman alexandre.pretyman at gmail.com
Fri Mar 13 00:35:01 EDT 2015


Hello Boris!

I have a little simple dynamically configurable state machine framework,
which would be nice to persist in odb. However in my transitions, to avoid
boiler plate code, I resorted to the curiously recurring template pattern
(CRTP) and I don't seem to find a solution for persisting this template
code in the manual or in the mailing list.

My states have a list of associated transitions, of base type:

#pragma db object polymorphic
struct abstract_transition {
virtual ~abstract_transition() {}
virtual bool triggered(event const & e) const = 0;
 #pragma db id auto
long long id;
 state * source;
state * destination = {nullptr};
};

Since my events (unfortunately) inherit from base class event, I test their
type to see if it what is expected by the transition, casting them to their
derived type, and use the CRTP to cast the transition to its derived type,
calling a method with the derived event. yielding the following code:

template < typename TransitionType, typename EventType >
struct transition : abstract_transition {
bool triggered(event const & ev) const {
  if (typeid(ev) == typeid(EventType)) {
  return static_cast<TransitionType const *>(this)->
trigger_on(dynamic_cast<EventType const &>(ev));
  }
  else {
return false;
}
}
};

So if I have a struct switch_event : event { bool on; }; as my event of
turning the switch of a lamp on or off, and would like to create a
transition to enter the light on state of my state machine, I would define
a switch_on_transition as:

struct switch_on_transition : transition<switch_on_transition,
switch_event> {
bool trigger_on(switch_event const & ev) const {
return ev.on == true;
}
};
#pragma db object (switch_on_transition)

However compiling through odb compiler gives me:

error: no data member designated as an object id

Is there a solution for this? Or am I miss-using odb?

Best regards,
-- 
Alexandre Pretyman


More information about the odb-users mailing list