[odb-users] Curiously recurring template and odb

Alexandre Pretyman alexandre.pretyman at gmail.com
Fri Mar 13 12:45:22 EDT 2015


I am very sorry for the bad explanation - it was late at night, and I did
not revise before sending.

Thank you for your feedback. The aim of the CRTP was to write shorter code.
The code generation I required was so simple I substituted the CRTP for
pre-processor macros. And odb seems to have accepted the code so far.

So the solution for the simple example of having a transition to take a
lamp to the light on state, given a switch on event, I ended up inheriting
directly from my polymorphic base class, skipping the CRTP:

struct switch_on_transition : abstract_transition {
TRIGGER_ON(switch_event)
 bool trigger_on(switch_event const & ev) const {
return ev.on == true;
}
};
#pragma db object (switch_on_transition)

What was to be generated by CRTP is now a #define directive:

#define TRIGGER_ON(EventType) bool triggered(::statemachine::event const &
ev) const { \
  if (typeid(ev) == typeid(EventType)) { \
  return trigger_on(dynamic_cast<EventType const &>(ev)); \
  } \
  else { \
return false; \
} \
}


I hope this can be useful for others in the future.

Kind regards,




On 13 March 2015 at 09:13, Boris Kolpackov <boris at codesynthesis.com> wrote:

> Hi Alexandre,
>
> Alexandre Pretyman <alexandre.pretyman at gmail.com> writes:
>
> > 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.
>
> Ok, you lost me here.
>
> In any case, check Section 15.2, "Persistent Class Template
> Instantiations" in the ODB manual.
>
> Essentially, you cannot store C++ class templates in the database but
> you can class templates instantiations. Once the dust settles after
> all your CRTP exploits, you have a set of template instantiations. So
> all you have to do is map all of them with ODB pragmas to persistent
> classes. Admittedly, CRTP may make untangling all this challenging,
> but it is definitely should be possible.
>
> Boris
>



-- 
Alexandre Pretyman


More information about the odb-users mailing list