OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ParseAlias.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
26 #include <osvr/Common/ParseAlias.h>
27 #include <osvr/Util/Verbosity.h>
28 
29 // Library/third-party includes
30 #include <json/reader.h>
31 #include <json/writer.h>
32 
33 // Standard includes
34 #include <sstream>
35 
36 namespace osvr {
37 namespace common {
38 
43  inline std::string getPathFromOldRouteSource(Json::Value obj) {
44  std::ostringstream ret;
45  if (obj.isObject() && obj.isMember("tracker")) {
46  auto tracker = obj["tracker"].asString();
47  if (tracker.front() != '/') {
48  ret << "/";
49  }
50  ret << tracker;
51  ret << "/tracker";
52  if (obj.isMember("sensor")) {
53  ret << "/";
54  ret << obj["sensor"].asInt();
55  }
56  }
57  return ret.str();
58  }
59 
60  ParsedAlias::ParsedAlias(std::string const &src) : m_simple(true) {
61  m_parse(src);
62  }
63 
64  ParsedAlias::ParsedAlias(Json::Value src) : m_simple(true) { m_parse(src); }
65 
66  bool ParsedAlias::isValid() const { return !m_value.isNull(); }
67 
68  bool ParsedAlias::isSimple() const { return m_simple; }
69  std::string ParsedAlias::getLeaf() const { return m_leaf().asString(); }
70  void ParsedAlias::setLeaf(std::string const &leaf) { m_leaf() = leaf; }
71 
72  std::string ParsedAlias::getAlias() const {
73  if (m_value.isString()) {
74  return m_value.asString();
75  }
76  Json::FastWriter writer;
77  std::string ret = writer.write(m_value);
78  // Remove trailing line feed, if any.
79  while (ret.back() == '\n' || ret.back() == '\r') {
80  ret.pop_back();
81  }
82  return ret;
83  }
84 
85  Json::Value ParsedAlias::getAliasValue() const { return m_value; }
86 
87  void ParsedAlias::m_parse(std::string const &src) {
88  Json::Value val;
89  Json::Reader reader;
90  if (!reader.parse(src, val)) {
91  // If it didn't parse as JSON, just assume it's a string.
92  m_value = src;
93  return;
94  }
95  m_parse(val);
96  }
97 
98  static const char CHILD_KEY[] = "child";
99  static const char SOURCE_KEY[] = "source";
100  void ParsedAlias::m_parse(Json::Value &val) {
101  if (val.isString()) {
102  // Assume a string is just a string.
103  m_value = val;
104  return;
105  }
106  if (val.isObject()) {
107  if (val.isMember(SOURCE_KEY)) {
108  // Strip any initial "source" level
109  m_parse(val[SOURCE_KEY]);
110  return;
111  }
112 
113  // Assume an object means a transform.
114  m_simple = false;
115  m_value = val;
116 
117  auto &leaf = m_leaf();
118  if (leaf.isString()) {
119  return;
120  }
121 
122  auto trackerEquiv = getPathFromOldRouteSource(leaf);
123  if (!trackerEquiv.empty()) {
124  leaf = trackerEquiv;
125  return;
126  }
127 
128  OSVR_DEV_VERBOSE(
129  "Couldn't handle transform leaf: " << leaf.toStyledString());
130  }
131  m_value = Json::nullValue;
133  }
134  Json::Value &ParsedAlias::m_leaf() {
135  Json::Value *current = &m_value;
136  while (current->isObject() && current->isMember(CHILD_KEY)) {
137  current = &(*current)[CHILD_KEY];
138  }
139  return *current;
140  }
141  Json::Value const &ParsedAlias::m_leaf() const {
142  Json::Value const *current = &m_value;
143  while (current->isObject() && current->isMember(CHILD_KEY)) {
144  current = &(*current)[CHILD_KEY];
145  }
146  return *current;
147  }
148 
149 } // namespace common
150 } // namespace osvr
Header.
bool isValid() const
Did the alias parse in a valid way?
Definition: ParseAlias.cpp:66
ParsedAlias(std::string const &src)
Constructor - performs parse and normalization of format.
Definition: ParseAlias.cpp:60
Json::Value getAliasValue() const
Gets a copy of the normalized version of the alias as a Json::Value.
Definition: ParseAlias.cpp:85
std::string getPathFromOldRouteSource(Json::Value obj)
Helper, converts old-style tracker source into normal. For a little backward-compatibility.
Definition: ParseAlias.cpp:43
std::string getLeaf() const
Get the ultimate source/leaf of the alias.
Definition: ParseAlias.cpp:69
std::string getAlias() const
Get the normalized, cleaned, compacted version of the alias.
Definition: ParseAlias.cpp:72
Internal, configured header file for verbosity macros.
void setLeaf(std::string const &leaf)
Set the leaf of the alias: should be an absolute path.
Definition: ParseAlias.cpp:70
bool isSimple() const
Is this a simple (string-only, no transform) alias?
Definition: ParseAlias.cpp:68