OSVR-Core
GetJSONStringFromTree.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_GetJSONStringFromTree_h_GUID_031FFB6A_B837_4080_4784_53730E86669E
26 #define INCLUDED_GetJSONStringFromTree_h_GUID_031FFB6A_B837_4080_4784_53730E86669E
27 
28 // Internal Includes
30 #include <osvr/Common/PathNode.h>
31 #include <osvr/Common/PathTree.h>
32 
33 // Library/third-party includes
34 #include <boost/variant.hpp>
35 
36 // Standard includes
37 // - none
38 
39 namespace osvr {
40 namespace common {
41  namespace {
42  struct GetStringVisitor : boost::static_visitor<std::string> {
43  std::string operator()(elements::StringElement const &elt) const {
44  return elt.getString();
45  }
46  template <typename T> std::string operator()(T const &) const {
47  return std::string{};
48  }
49  };
50  }
51  inline std::string getJSONStringAtNode(PathNode const &node) {
52  return boost::apply_visitor(GetStringVisitor(), node.value());
53  }
54  inline std::string getJSONStringFromTree(PathTree const &tree,
55  std::string const &path) {
56  try {
57  auto const &desiredNode = tree.getNodeByPath(path);
58  return getJSONStringAtNode(desiredNode);
59  } catch (util::tree::NoSuchChild &) {
60  return std::string{};
61  }
62  }
63 } // namespace common
64 } // namespace osvr
65 
66 #endif // INCLUDED_GetJSONStringFromTree_h_GUID_031FFB6A_B837_4080_4784_53730E86669E
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
::osvr::util::TreeNode< PathElement > PathNode
The specific tree node type that contains a path element.
Definition: PathNode_fwd.h:42
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
The element type corresponding to a string value such as a JSON string.
Definition: PathElementTypes.h:217
Header.
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:148
OSVR_COMMON_EXPORT std::string & getString()
Get/set (if non const) the stored string.
Definition: PathElementTypes.cpp:83
Header.