OSVR-Core
GetOptionalParameter.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_GetOptionalParameter_h_GUID_F4FA55E0_C946_4AAE_6741_57C269C56D24
26 #define INCLUDED_GetOptionalParameter_h_GUID_F4FA55E0_C946_4AAE_6741_57C269C56D24
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 #include <json/value.h>
33 
34 // Standard includes
35 // - none
36 
37 namespace osvr {
38 namespace vbtracker {
39  namespace detail {
40  template <typename T> struct JsonTypeGetter;
41 #define OSVR_DECLARE_JSON_TYPE_GETTER(TYPENAME, METHOD) \
42  template <> struct JsonTypeGetter<TYPENAME> { \
43  static TYPENAME apply(Json::Value const &val) { return val.METHOD(); } \
44  };
45  OSVR_DECLARE_JSON_TYPE_GETTER(bool, asBool)
46  OSVR_DECLARE_JSON_TYPE_GETTER(float, asFloat)
47  OSVR_DECLARE_JSON_TYPE_GETTER(double, asDouble)
48  OSVR_DECLARE_JSON_TYPE_GETTER(int, asInt)
49  OSVR_DECLARE_JSON_TYPE_GETTER(std::string, asString)
50 
51 #undef OSVR_DECLARE_JSON_TYPE_GETTER
52  } // namespace detail
53  template <typename T> inline T json_cast(Json::Value const &val) {
55  }
56 
59  template <typename T>
60  inline void getOptionalParameter(T &dest, Json::Value const &obj,
61  const char *key) {
62  dest = json_cast<T>(obj.get(key, dest));
63  }
66  template <typename T, Json::Value::ArrayIndex N>
67  inline void getOptionalParameter(T (&dest)[N], Json::Value const &obj,
68  const char *key) {
69  Json::Value const &node = obj[key];
70  if (!node.isArray()) {
71  return;
72  }
73  if (node.size() != N) {
74  return;
75  }
76  for (Json::Value::ArrayIndex i = 0; i < N; ++i) {
77  dest[i] = json_cast<T>(node[i]);
78  }
79  }
80 } // namespace vbtracker
81 } // namespace osvr
82 #endif // INCLUDED_GetOptionalParameter_h_GUID_F4FA55E0_C946_4AAE_6741_57C269C56D24
Definition: GetOptionalParameter.h:40
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: TypeSafeIdHash.h:44
typename F::template apply< Args... > apply
Apply an alias class.
Definition: Apply.h:44
Definition: newuoa.h:1888
void getOptionalParameter(T &dest, Json::Value const &obj, const char *key)
Gets an optional parameter from a JSON object: if it&#39;s not present, the existing value is left there...
Definition: GetOptionalParameter.h:60