OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
InterfaceTree.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 "../Common/PathParseAndRetrieve.h"
29 
30 // Library/third-party includes
31 // - none
32 
33 // Standard includes
34 #include <algorithm>
35 
36 namespace osvr {
37 namespace client {
39  public:
40  HandlerClearVisitor(InterfaceTree &tree) : m_tree(&tree) {}
41  void operator()(InterfaceTree::node_type &node) {
42  m_tree->m_removeHandler(node);
43  node.visitChildren(*this);
44  }
45 
46  private:
47  InterfaceTree *m_tree;
48  };
49 
50  InterfaceTree::InterfaceTree() : m_root(node_type::createRoot()) {}
51 
53  auto &ifaces = getInterfacesForPath(iface->getPath());
54  bool ret = ifaces.empty();
55 
56  // Makes sure we only have one copy of any interface pointer in the
57  // vector
58  auto it = std::find(begin(ifaces), end(ifaces), iface);
59  if (it == end(ifaces)) {
60  ifaces.push_back(iface);
61  }
62  return ret;
63  }
64 
65  bool
67  auto &ifaces = getInterfacesForPath(iface->getPath());
68  auto it = std::find(begin(ifaces), end(ifaces), iface);
69  if (it != end(ifaces)) {
70  ifaces.erase(it);
71  }
72  return ifaces.empty();
73  }
74 
75  common::InterfaceList &
76  InterfaceTree::getInterfacesForPath(std::string const &path) {
77  return m_getNodeForPath(path).value().interfaces;
78  }
79 
80  RemoteHandlerPtr InterfaceTree::getHandlerForPath(std::string const &path) {
81  return m_getNodeForPath(path).value().handler;
82  }
83 
84  RemoteHandlerPtr
85  InterfaceTree::eraseHandlerForPath(std::string const &path) {
86  return m_removeHandler(m_getNodeForPath(path));
87  }
88 
89  RemoteHandlerPtr
90  InterfaceTree::replaceHandlerForPath(std::string const &path,
91  RemoteHandlerPtr const &handler) {
92  return m_setHandler(m_getNodeForPath(path), handler);
93  }
94 
95  void InterfaceTree::updateHandlers() { m_handlers.update(); }
97  HandlerClearVisitor visitor{*this};
98  visitor(*m_root);
99  }
100 
101  InterfaceTree::node_type &
102  InterfaceTree::m_getNodeForPath(std::string const &path) {
103  return common::pathParseAndRetrieve(*m_root, path);
104  }
105 
106  RemoteHandlerPtr InterfaceTree::m_removeHandler(node_type &node) {
107  auto ret = node.value().handler;
108  node.value().handler.reset();
109  m_handlers.remove(ret);
110  return ret;
111  }
112 
113  RemoteHandlerPtr
114  InterfaceTree::m_setHandler(node_type &node,
115  RemoteHandlerPtr const &handler) {
116  auto ret = m_removeHandler(node);
117  node.value().handler = handler;
118  m_handlers.add(handler);
119  return ret;
120  }
121 } // namespace client
122 } // namespace osvr
shared_ptr< ClientInterface > ClientInterfacePtr
Pointer for holding ClientInterface objects safely.
RemoteHandlerPtr getHandlerForPath(std::string const &path)
Returns the handler for a given path.
RemoteHandlerPtr eraseHandlerForPath(std::string const &path)
Clears and returns the handler for a given path.
bool addInterface(common::ClientInterfacePtr const &iface)
Add an interface to the tree.
RemoteHandlerPtr replaceHandlerForPath(std::string const &path, RemoteHandlerPtr const &handler)
Sets the handler for a given path, returning the old handler if any.
void updateHandlers()
Call the update method on all handlers.
common::InterfaceList & getInterfacesForPath(std::string const &path)
Returns a reference to the list of interfaces registered for a given path.
bool removeInterface(common::ClientInterfacePtr const &iface)
Remove an interface from the tree.
Holds on to lists of interfaces organized into the tree structure, as well as their associated handle...
Definition: InterfaceTree.h:53
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 clearHandlers()
Removes all handlers.