OSVR-Core
PathElementSerialization.h
Go to the documentation of this file.
1 
12 // Copyright 2015 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 #ifndef INCLUDED_PathElementSerialization_h_GUID_5DA3C116_3537_4084_F45A_3FCC20B1AAC1
27 #define INCLUDED_PathElementSerialization_h_GUID_5DA3C116_3537_4084_F45A_3FCC20B1AAC1
28 
29 // Internal Includes
33 
34 // Library/third-party includes
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>
40 
41 // Standard includes
42 #include <type_traits>
43 #include <stdexcept>
44 
45 namespace osvr {
46 namespace common {
47  namespace {
50  class PathElementToJSONFunctor : boost::noncopyable {
51  public:
52  PathElementToJSONFunctor(Json::Value &val) : m_val(val) {}
53 
54  template <typename T>
55  void operator()(const char name[], T const &data, ...) {
56  m_val[name] = data;
57  }
58 
59  private:
60  Json::Value &m_val;
61  };
62 
65  class PathElementFromJsonFunctor : boost::noncopyable {
66  public:
67  PathElementFromJsonFunctor(Json::Value const &val) : m_val(val) {}
68 
70  void operator()(const char name[], std::string &dataRef) {
71  m_requireName(name);
72  dataRef = m_val[name].asString();
73  }
75  void operator()(const char name[], bool &dataRef) {
76  m_requireName(name);
77  dataRef = m_val[name].asBool();
78  }
80  void operator()(const char name[], bool &dataRef, bool defaultVal) {
81  if (m_hasName(name)) {
82  dataRef = m_val[name].asBool();
83  } else {
84  dataRef = defaultVal;
85  }
86  }
87 
90  void operator()(const char name[], Json::Value &dataRef) {
91  m_requireName(name);
92  if (m_val[name].isString()) {
93  Json::Reader reader;
94  Json::Value val;
95  if (reader.parse(m_val[name].asString(), val)) {
96  dataRef = val;
97  }
98  } else {
99  dataRef = m_val[name];
100  }
101  }
102 
104  void operator()(const char name[], uint8_t &dataRef) {
105  m_requireName(name);
106  dataRef = static_cast<uint8_t>(m_val[name].asInt());
107  }
108 
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());
114  } else {
115  dataRef = defaultVal;
116  }
117  }
118 
120  private:
121  void m_requireName(const char name[]) {
122  if (!m_hasName(name)) {
123  throw std::runtime_error(
124  "Missing JSON object member named " +
125  std::string(name));
126  }
127  }
128  bool m_hasName(const char name[]) { return m_val.isMember(name); }
129  Json::Value const &m_val;
130  };
131 
135  class DeserializeElementFunctor {
136  public:
137  DeserializeElementFunctor(Json::Value const &val,
139  : m_val(val), m_typename(val["type"].asString()), m_elt(elt) {}
140 
142  DeserializeElementFunctor &
143  operator=(const DeserializeElementFunctor &) = delete;
144 
145  template <typename T> void operator()(T const &) {
146  if (elements::getTypeName<T>() == m_typename) {
147  T value;
148  PathElementFromJsonFunctor functor(m_val);
149  serializationDescription(functor, value);
150  m_elt = value;
151  }
152  }
153 
154  private:
155  Json::Value const &m_val;
156  std::string const m_typename;
157  elements::PathElement &m_elt;
158  };
159  } // namespace
160 
163  template <typename T>
164  inline Json::Value pathElementToJson(T const &element) {
165  Json::Value ret{Json::objectValue};
166  PathElementToJSONFunctor f(ret);
167  serializationDescription(f, element);
168  return ret;
169  }
170 
171  inline elements::PathElement jsonToPathElement(Json::Value const &json) {
173  DeserializeElementFunctor functor{json, elt};
174  boost::mpl::for_each<elements::PathElement::types>(functor);
175  return elt;
176  }
177 
178 } // namespace common
179 } // namespace osvr
180 
181 #endif // INCLUDED_PathElementSerialization_h_GUID_5DA3C116_3537_4084_F45A_3FCC20B1AAC1
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Json::Value pathElementToJson(T const &element)
Returns a JSON object with any element-type-specific data for the given PathElement-holdable type...
Definition: PathElementSerialization.h:164
Header containing the serializationDescriptor function templates that serve to describe (for code gen...
boost::variant< NullElement, AliasElement, ArticulationElement, SensorElement, InterfaceElement, DeviceElement, PluginElement, StringElement > PathElement
The variant type containing a particular kind of path element.
Definition: PathElementTypes_fwd.h:54
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:148