OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
AddDevice.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/AddDevice.h>
27 #include <osvr/Common/PathNode.h>
28 #include <osvr/Common/PathTree.h>
33 
34 // Library/third-party includes
35 // - none
36 
37 // Standard includes
38 #include <stdexcept>
39 
40 namespace osvr {
41 namespace common {
42  PathNode &addDevice(PathTree &tree, std::string const &deviceName,
43  elements::DeviceElement /* dev */) {
44  if (deviceName.size() < 3) { // Minimum size: a/b
45  throw exceptions::InvalidDeviceName("Given device name is too "
46  "short to be a complete device "
47  "name: " +
48  deviceName);
49  }
50  std::string normalized = (deviceName[0] == getPathSeparatorCharacter())
51  ? (deviceName)
52  : (getPathSeparator() + deviceName);
53  PathNode &device = tree.getNodeByPath(normalized);
54  auto plugin = device.getParent();
55  if (plugin == nullptr || plugin->isRoot()) {
58  "Given device name did not include both a "
59  "plugin component and a device "
60  "component: " +
61  deviceName);
62  }
63  // Might be several layers before we hit the plugin - get the last
64  // parent before the root.
67  while (plugin->getParent() != nullptr &&
68  !plugin->getParent()->isRoot()) {
69  plugin = plugin->getParent();
70  }
73  return device;
74  }
75 } // namespace common
76 } // namespace osvr
Header.
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
Thrown when attempting to add an invalid device name to the tree.
The element type corresponding to a plugin.
PathNode & addDevice(PathTree &tree, std::string const &deviceName, elements::DeviceElement dev=elements::DeviceElement())
Adds/updates nodes for the basic path to a device.
Definition: AddDevice.cpp:42
The element type corresponding to a device, which implements 0 or more interfaces.
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
void ifNullReplaceWith(PathElement &dest, PathElement const &src)
If dest is a NullElement, replace it with the provided src element.
Header.
const char * getPathSeparator()
Gets the path separator - a slash - as a null-terminated string.
Header.