OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ResolveTreeNode.cpp
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 // Internal Includes
28 #include <osvr/Common/PathNode.h>
32 #include <osvr/Common/ParseAlias.h>
33 #include <osvr/Util/Verbosity.h>
34 
35 // Library/third-party includes
36 #include <boost/variant.hpp>
37 #include <boost/lexical_cast.hpp>
38 #include <json/value.h>
39 #include <json/reader.h>
40 
41 // Standard includes
42 #include <sstream>
43 
44 namespace osvr {
45 namespace common {
46 
52  if (nullptr == boost::get<elements::NullElement>(&node.value())) {
54  return;
55  }
56 
57  if (nullptr == node.getParent()) {
58  // couldn't help, no parent.
59  return;
60  }
61  auto const &parent = *node.getParent();
62 
63  if (nullptr ==
64  boost::get<elements::InterfaceElement>(&(parent.value()))) {
65  return; // parent isn't an interface.
66  }
67  // So if we get here, parent is present and an interface, which means
68  // that we're a sensor.
69  node.value() = elements::SensorElement();
70  }
71 
72  // Forward declaration
73  void resolveTreeNodeImpl(PathTree &pathTree, std::string const &path,
74  OriginalSource &source);
75 
76  class TreeResolutionVisitor : public boost::static_visitor<>,
77  boost::noncopyable {
78  public:
80  common::OriginalSource &source)
81  : boost::static_visitor<>(), m_tree(tree), m_node(node),
82  m_source(source) {}
83 
85  template <typename T> void operator()(T const &) {
86  // Can't handle it.
87  }
88 
91  // This is an alias.
92  ParsedAlias parsed(elt.getSource());
93  if (!parsed.isValid()) {
94  OSVR_DEV_VERBOSE("Couldn't parse alias: " << elt.getSource());
95  return;
96  }
98  if (!parsed.isSimple()) {
99  // Not simple: store the full string as a transform.
100  m_source.nestTransform(parsed.getAliasValue());
101  }
102  m_recurse(parsed.getLeaf());
103  }
104 
108  m_decompose();
109  BOOST_ASSERT_MSG(m_source.isResolved(),
110  "Landing on a sensor means we should have an "
111  "interface and device, exceptions would be thrown "
112  "in Decompose otherwise.");
113  }
114 
118  m_decompose();
119  BOOST_ASSERT_MSG(m_source.isResolved(),
120  "Landing on an interface means we should have an "
121  "interface and device, exceptions would be thrown "
122  "in Decompose otherwise.");
123  }
124 
125  private:
126  void m_decompose() { m_source.decompose(m_node); }
127  void m_recurse(std::string const &path) {
128  resolveTreeNodeImpl(m_tree, path, m_source);
129  }
130  PathTree &m_getPathTree() { return m_tree; }
131 
132  PathTree &m_tree;
133  PathNode &m_node;
134  OriginalSource &m_source;
135  };
136 
137  inline void resolveTreeNodeImpl(PathTree &pathTree, std::string const &path,
138  OriginalSource &source) {
139  auto &node = pathTree.getNodeByPath(path);
140 
141  // First do any inference possible here.
143 
144  // Now visit.
145  TreeResolutionVisitor visitor(pathTree, node, source);
146  boost::apply_visitor(visitor, node.value());
147  }
148 
149  boost::optional<OriginalSource> resolveTreeNode(PathTree &pathTree,
150  std::string const &path) {
151  OriginalSource source;
152  resolveTreeNodeImpl(pathTree, path, source);
153  if (source.isResolved()) {
154  return source;
155  }
156  return boost::optional<OriginalSource>();
157  }
158 } // namespace common
159 } // namespace osvr
The result of resolving a tree node to a device: either an original source to connect to...
Header.
The element type corresponding to a path alias, with a priority level for sorting out whether automat...
A tree representation, with path/url syntax, of the known OSVR system.
Definition: PathTree.h:43
::osvr::util::TreeNode< PathElement > PathNode
The specific tree node type that contains a path element.
Definition: PathNode_fwd.h:42
Header including PathTree.h and all additional headers needed to define related types.
std::string & getSource()
Get the source of data for this alias.
The element type corresponding to a particular sensor of an interface.
void decompose(PathNode &node)
Decompose a path node representing an original source into its device, interface, and sensor...
The element type corresponding to an interface, which often may have one or more sensors.
void operator()(elements::InterfaceElement const &)
Handle an interface element.
Internal, configured header file for verbosity macros.
void operator()(T const &)
Fallback case.
void ifNullTryInferFromParent(common::PathNode &node)
Given a node, if it's null, try to infer from the parent what it should be.
void operator()(elements::SensorElement const &)
Handle a sensor element.
void operator()(elements::AliasElement const &elt)
Handle an alias element.
Header.