OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
PathElementTypes.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
27 #include <osvr/Util/DefaultPort.h>
28 
29 // Library/third-party includes
30 // - none
31 
32 // Standard includes
33 #include <sstream>
34 
35 namespace osvr {
36 namespace common {
37  namespace elements {
46  namespace {
47  struct CRTPChecker {
48  template <typename T> struct apply {
49  typedef ElementBase<T> base_type;
50  static_assert(std::is_base_of<base_type, T>::value,
51  "A given element type T must inherit from "
52  "ElementBase<T> (the CRTP)!");
53  typedef void type;
54  };
55  };
56 
57  // Force instantiation of the types for static asserts.
58  typedef boost::mpl::transform<PathElement::types, CRTPChecker>::type
59  CRTPCheckDummy;
60  } // namespace
61 
62  AliasElement::AliasElement(std::string const &source,
63  AliasPriority priority)
64  : m_source(source), m_priority(priority) {}
65 
66  AliasElement::AliasElement(std::string const &source)
67  : AliasElement(source, ALIASPRIORITY_MINIMUM) {}
68 
69  void AliasElement::setSource(std::string const &source) {
71  m_source = source;
72  }
73 
74  std::string &AliasElement::getSource() { return m_source; }
75  std::string const &AliasElement::getSource() const { return m_source; }
76 
77  AliasPriority &AliasElement::priority() { return m_priority; }
78  AliasPriority AliasElement::priority() const { return m_priority; }
79 
81  StringElement::StringElement(std::string const &s) : m_val(s) {}
82 
83  std::string &StringElement::getString() { return m_val; }
84 
85  std::string const &StringElement::getString() const { return m_val; }
86 
89  static inline std::string
90  attachPortToServerIfNoneSpecified(std::string const &server, int port) {
91  // See if the string ends with a colon (:) followed by only digits.
92  // If so, it ends with a port number so we don't need to add one and
93  // can just return it.
94  size_t lastColon = server.find_last_of(':');
95  if (lastColon != std::string::npos) {
96  bool nonDigit = false;
97  for (size_t i = lastColon + 1; i < server.size(); i++) {
98  if (!isdigit(server[i])) {
99  nonDigit = true;
100  }
101  }
102  if (!nonDigit) { return server; }
103  }
104  // OK, didn't end with a port
105  if (server.find(":") != std::string::npos &&
106  server.find("tcp://") == std::string::npos) {
107  // Ah, but it's got a weird protocol specification or something,
108  // more complicated than sticking tcp:// on the front, so we
109  // should just leave it alone.
110  return server;
111  }
112  // Now, we may append the port.
113  std::ostringstream os;
114  os << server;
115  os << ":";
116  os << port;
117  return os.str();
118  }
119 
120  DeviceElement
121  DeviceElement::createVRPNDeviceElement(std::string const &deviceName,
122  std::string const &server) {
123  DeviceElement ret;
124  ret.m_devName = deviceName;
125  // explicitly specify VRPN port
126  ret.m_server =
127  attachPortToServerIfNoneSpecified(server, util::DefaultVRPNPort);
128 
129  return ret;
130  }
131 
132  DeviceElement
133  DeviceElement::createDeviceElement(std::string const &deviceName,
134  std::string const &server,
135  int port) {
136  DeviceElement ret;
137  ret.m_devName = deviceName;
138  switch (port) {
140  ret.m_server = server;
141  break;
142  case util::UseDefaultPort:
143  // explicitly specify OSVR port
144  ret.m_server = attachPortToServerIfNoneSpecified(
145  server, util::DefaultOSVRPort);
146  break;
147  default:
148  // explicitly specify the user's port
149  ret.m_server = attachPortToServerIfNoneSpecified(server, port);
150  break;
151  }
152  return ret;
153  }
154 
155  std::string &DeviceElement::getDeviceName() { return m_devName; }
156  std::string const &DeviceElement::getDeviceName() const {
157  return m_devName;
158  }
159 
160  std::string &DeviceElement::getServer() { return m_server; }
161  std::string const &DeviceElement::getServer() const { return m_server; }
162 
163  std::string DeviceElement::getFullDeviceName() const {
164  return getDeviceName() + "@" + getServer();
165  }
166  Json::Value &DeviceElement::getDescriptor() { return m_descriptor; }
167  Json::Value const &DeviceElement::getDescriptor() const {
168  return m_descriptor;
169  }
170  } // namespace elements
171 } // namespace common
172 } // namespace osvr
The element type corresponding to a path alias, with a priority level for sorting out whether automat...
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.
typename F::template apply< Args...> apply
Apply an alias class.
Definition: Apply.h:44
static DeviceElement createDeviceElement(std::string const &deviceName, std::string const &server, int port=util::UseDefaultPort)
The element type corresponding to a device, which implements 0 or more interfaces.
Namespace for the various element types that may constitute a node in the path tree.
Header with default port numbers for OSVR and VRPN. These constants are specified as enums...
std::string & getString()
Get/set (if non const) the stored string.
void setSource(std::string const &source)
Sets the source of this alias.