OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
PathTree.cpp
Go to the documentation of this file.
1 
11 // Copyright 2014 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
26 #include <osvr/Common/PathTree.h>
27 #include <osvr/Common/PathNode.h>
30 #include <osvr/Common/ParseAlias.h>
33 #include <osvr/Util/Verbosity.h>
36 #include "PathParseAndRetrieve.h"
37 
38 // Library/third-party includes
39 #include <boost/variant/get.hpp>
40 
41 // Standard includes
42 // - none
43 
44 namespace osvr {
45 namespace common {
46  PathTree::PathTree() : m_root(PathNode::createRoot()) {}
47  PathNode &PathTree::getNodeByPath(std::string const &path) {
48  return pathParseAndRetrieve(*m_root, path);
49  }
50  PathNode &
51  PathTree::getNodeByPath(std::string const &path,
52  PathElement const &finalComponentDefault) {
53  auto &ret = pathParseAndRetrieve(*m_root, path);
54 
55  // Handle null elements as final component.
56  elements::ifNullReplaceWith(ret.value(), finalComponentDefault);
57  return ret;
58  }
59 
60  PathNode const &PathTree::getNodeByPath(std::string const &path) const {
61  return pathParseAndRetrieve(const_cast<PathNode const &>(*m_root),
62  path);
63  }
64 
65  void PathTree::reset() { m_root = PathNode::createRoot(); }
66 
69  static inline bool aliasNeedsUpdate(PathNode &node,
70  std::string const &source,
71  AliasPriority priority) {
73  boost::get<elements::AliasElement>(&node.value());
74  if (nullptr == elt) {
76  return true;
77  }
78  if (priority > elt->priority()) {
81  return true;
82  }
83  if (priority == elt->priority() && source != elt->getSource()) {
85  return true;
86  }
87  return false;
88  }
89 
90  static inline bool addAliasImpl(PathNode &node, std::string const &source,
91  AliasPriority priority) {
92 
93  if (!aliasNeedsUpdate(node, source, priority)) {
94  return false;
95  }
97  elt.setSource(source);
98  elt.priority() = priority;
99  node.value() = elt;
100  return true;
101  }
102 
103  bool addAlias(PathNode &node, std::string const &source,
104  AliasPriority priority) {
105  ParsedAlias newSource(source);
106  if (!newSource.isValid()) {
108  OSVR_DEV_VERBOSE("Could not parse source: " << source);
109  return false;
110  }
111  if (!isPathAbsolute(newSource.getLeaf())) {
113  OSVR_DEV_VERBOSE(
114  "Source contains a relative path, not permitted: " << source);
115  return false;
116  }
117  return addAliasImpl(node, newSource.getAlias(), priority);
118  }
119 
120  bool addAliasFromRoute(PathNode &node, std::string const &route,
121  AliasPriority priority) {
122  auto val = jsonParse(route);
123  auto alias = applyPriorityToAlias(convertRouteToAlias(val), priority);
124  return AliasProcessor().process(node, alias);
125  }
126 
127  static inline std::string getAbsolutePath(PathNode &node,
128  std::string const &path) {
129  if (isPathAbsolute(path)) {
130  return path;
131  }
132  return getFullPath(treePathRetrieve(node, path));
133  }
134 
136  std::string const &source,
137  std::string const &dest,
138  AliasPriority priority) {
139  auto &aliasNode = treePathRetrieve(node, dest);
140  ParsedAlias newSource(source);
141  if (!newSource.isValid()) {
143  OSVR_DEV_VERBOSE("Could not parse source: " << source);
144  return false;
145  }
146  auto absSource = getAbsolutePath(node, newSource.getLeaf());
147  newSource.setLeaf(absSource);
148  return addAliasImpl(aliasNode, newSource.getAlias(), priority);
149  }
150 
151  bool isPathAbsolute(std::string const &source) {
152  return !source.empty() && source.at(0) == getPathSeparatorCharacter();
153  }
154 
155  void clonePathTree(PathTree const &src, PathTree &dest) {
156  auto nodes = pathTreeToJson(src);
157  jsonToPathTree(dest, nodes);
158  }
159 } // namespace common
160 } // namespace osvr
A short-lived class for setting up options then processing alias directives to apply to a path tree...
Header.
The element type corresponding to a path alias, with a priority level for sorting out whether automat...
Header containing wrappers for some common jsoncpp operations.
bool isValid() const
Did the alias parse in a valid way?
Definition: ParseAlias.cpp:66
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
AliasPriority & priority()
Get/set whether this alias was automatically set (and thus subject to being override by explicit rout...
std::string & getSource()
Get the source of data for this alias.
PathTree()
Constructor.
Definition: PathTree.cpp:46
util::TreeNode< ValueType > & treePathRetrieve(util::TreeNode< ValueType > &node, std::string path, bool permitParent=false)
Internal method for parsing a path and getting or creating the nodes along it.
void jsonToPathTree(PathTree &tree, Json::Value nodes)
Deserialize a path tree from a JSON array of objects.
std::string getFullPath(PathNode const &node)
Gets the absolute path for the given node.
Definition: PathNode.cpp:42
Json::Value convertRouteToAlias(Json::Value const &val)
Given a single JSON object, if it's an old-fashioned "routing directive", convert it to a normal "ali...
PathNode & getNodeByPath(std::string const &path)
Returns the node indicated by the path, which must be absolute (begin with a /). Any non-existent nod...
Definition: PathTree.cpp:47
Json::Value applyPriorityToAlias(Json::Value const &alias, AliasPriority priority)
Given a JSON object describing one or more aliases, set the priority of the alias(es).
void ifNullReplaceWith(PathElement &dest, PathElement const &src)
If dest is a NullElement, replace it with the provided src element.
void reset()
Reset the path tree to a new, empty root node.
Definition: PathTree.cpp:65
std::string getLeaf() const
Get the ultimate source/leaf of the alias.
Definition: ParseAlias.cpp:69
Header.
std::string getAlias() const
Get the normalized, cleaned, compacted version of the alias.
Definition: ParseAlias.cpp:72
bool addAlias(PathNode &node, std::string const &source, AliasPriority priority)
Definition: PathTree.cpp:103
util::TreeNode< ValueType > & pathParseAndRetrieve(util::TreeNode< ValueType > &root, std::string const &path)
Internal method for parsing a path and getting or creating the nodes along it.
void setSource(std::string const &source)
Sets the source of this alias.
Internal, configured header file for verbosity macros.
Json::Value pathTreeToJson(PathTree const &tree, bool keepNulls=false)
Serialize a path tree to a JSON array of objects, one for each node.
boost::variant< NullElement, AliasElement, SensorElement, InterfaceElement, DeviceElement, PluginElement, StringElement > PathElement
The variant type containing a particular kind of path element.
void setLeaf(std::string const &leaf)
Set the leaf of the alias: should be an absolute path.
Definition: ParseAlias.cpp:70
bool addAliasFromSourceAndRelativeDest(PathNode &node, std::string const &source, std::string const &dest, AliasPriority priority=ALIASPRIORITY_MANUAL)
Definition: PathTree.cpp:135
Json::Value jsonParse(std::string const &str)
Parses a string as JSON, returning a null value if parsing fails.
Definition: JSONHelpers.h:42
Header.