OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
PathTreeOwner.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
29 
30 // Library/third-party includes
31 // - none
32 
33 // Standard includes
34 #include <algorithm>
35 #include <iterator>
36 
37 namespace osvr {
38 namespace common {
39  namespace {
42  class PathTreeObserverConcrete : public PathTreeObserver {
43  public:
44  PathTreeObserverConcrete() = default;
45  };
46  } // namespace
47 
53  template <typename T, typename F>
54  inline void for_each_cleanup_pointers(T &c, F &&f) {
55  using weak_ptr_type = typename T::value_type;
56  auto newEnd =
57  std::remove_if(begin(c), end(c), [&](weak_ptr_type const &ptr) {
58  auto fullPtr = ptr.lock();
59  if (fullPtr) {
60  f(*fullPtr);
61  return false; // don't remove this one
62  }
63  return true; // this one we couldn't lock - expired observer.
64  });
65  c.erase(newEnd, end(c));
66  }
67 
68  PathTreeObserverPtr PathTreeOwner::makeObserver() {
69  auto ret = PathTreeObserverPtr{make_shared<PathTreeObserverConcrete>()};
70  m_observers.push_back(ret);
71  return ret;
72  }
73 
74  void PathTreeOwner::replaceTree(Json::Value const &nodes) {
76  m_observers, [&](PathTreeObserver const &observer) {
77  observer.notifyEvent(PathTreeEvents::AboutToUpdate, m_tree);
78  });
79 
80  m_tree.reset();
81 
82  common::jsonToPathTree(m_tree, nodes);
83 
84  m_valid = true;
85 
87  m_observers, [&](PathTreeObserver const &observer) {
88  observer.notifyEvent(PathTreeEvents::AfterUpdate, m_tree);
89  });
90  }
91 } // namespace common
92 } // namespace osvr
void replaceTree(Json::Value const &nodes)
Replace the entirety of the path tree from the given serialized array of nodes.
void jsonToPathTree(PathTree &tree, Json::Value nodes)
Deserialize a path tree from a JSON array of objects.
PathTreeObserverPtr makeObserver()
Make an observer object that can hold callbacks for tree updates. Callbacks are called in the order t...
void reset()
Reset the path tree to a new, empty root node.
Definition: PathTree.cpp:65
void for_each_cleanup_pointers(T &c, F &&f)
void remove_if(GeneralizedTransform &transform, UnaryPredicate pred)
Remove levels from a generalized transform as dictated by an arbitrary predicate. ...