26 #ifndef INCLUDED_PathElementSerialization_h_GUID_5DA3C116_3537_4084_F45A_3FCC20B1AAC1 
   27 #define INCLUDED_PathElementSerialization_h_GUID_5DA3C116_3537_4084_F45A_3FCC20B1AAC1 
   35 #include <json/value.h> 
   36 #include <json/reader.h> 
   37 #include <boost/variant.hpp> 
   38 #include <boost/mpl/for_each.hpp> 
   39 #include <boost/noncopyable.hpp> 
   42 #include <type_traits> 
   50         class PathElementToJSONFunctor : boost::noncopyable {
 
   52             PathElementToJSONFunctor(Json::Value &val) : m_val(val) {}
 
   55             void operator()(
const char name[], T 
const &data, ...) {
 
   65         class PathElementFromJsonFunctor : boost::noncopyable {
 
   67             PathElementFromJsonFunctor(Json::Value 
const &val) : m_val(val) {}
 
   70             void operator()(
const char name[], std::string &dataRef) {
 
   72                 dataRef = m_val[name].asString();
 
   75             void operator()(
const char name[], 
bool &dataRef) {
 
   77                 dataRef = m_val[name].asBool();
 
   80             void operator()(
const char name[], 
bool &dataRef, 
bool defaultVal) {
 
   81                 if (m_hasName(name)) {
 
   82                     dataRef = m_val[name].asBool();
 
   90             void operator()(
const char name[], Json::Value &dataRef) {
 
   92                 if (m_val[name].isString()) {
 
   95                     if (reader.parse(m_val[name].asString(), val)) {
 
   99                     dataRef = m_val[name];
 
  104             void operator()(
const char name[], uint8_t &dataRef) {
 
  106                 dataRef = 
static_cast<uint8_t
>(m_val[name].asInt());
 
  110             void operator()(
const char name[], uint8_t &dataRef,
 
  111                             uint8_t defaultVal) {
 
  112                 if (m_hasName(name)) {
 
  113                     dataRef = 
static_cast<uint8_t
>(m_val[name].asInt());
 
  115                     dataRef = defaultVal;
 
  121             void m_requireName(
const char name[]) {
 
  122                 if (!m_hasName(name)) {
 
  123                     throw std::runtime_error(
 
  124                         "Missing JSON object member named " +
 
  128             bool m_hasName(
const char name[]) { 
return m_val.isMember(name); }
 
  129             Json::Value 
const &m_val;
 
  135         class DeserializeElementFunctor {
 
  137             DeserializeElementFunctor(Json::Value 
const &val,
 
  139                 : m_val(val), m_typename(val[
"type"].asString()), m_elt(elt) {}
 
  142             DeserializeElementFunctor &
 
  143             operator=(
const DeserializeElementFunctor &) = 
delete;
 
  145             template <
typename T> 
void operator()(T 
const &) {
 
  146                 if (elements::getTypeName<T>() == m_typename) {
 
  148                     PathElementFromJsonFunctor functor(m_val);
 
  149                     serializationDescription(functor, value);
 
  155             Json::Value 
const &m_val;
 
  156             std::string 
const m_typename;
 
  163     template <
typename T>
 
  165         Json::Value ret{Json::objectValue};
 
  166         PathElementToJSONFunctor f(ret);
 
  167         serializationDescription(f, element);
 
  173         DeserializeElementFunctor functor{json, elt};
 
  174         boost::mpl::for_each<elements::PathElement::types>(functor);
 
  181 #endif // INCLUDED_PathElementSerialization_h_GUID_5DA3C116_3537_4084_F45A_3FCC20B1AAC1 
Json::Value pathElementToJson(T const &element)
Returns a JSON object with any element-type-specific data for the given PathElement-holdable type...
Header containing the serializationDescriptor function templates that serve to describe (for code gen...
boost::variant< NullElement, AliasElement, SensorElement, InterfaceElement, DeviceElement, PluginElement, StringElement > PathElement
The variant type containing a particular kind of path element.