OSVR-Core
KeyedOwnershipContainer.h
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 #ifndef INCLUDED_KeyedOwnershipContainer_h_GUID_002CD118_DF06_45AC_44D8_C37BA99E0E93
26 #define INCLUDED_KeyedOwnershipContainer_h_GUID_002CD118_DF06_45AC_44D8_C37BA99E0E93
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 #include <boost/any.hpp>
33 
34 // Standard includes
35 #include <map>
36 
37 namespace osvr {
38 namespace util {
40  protected:
42  void *doInsert(void *rawPtr, boost::any smartPtr) {
43  m_container[rawPtr] = smartPtr;
44  return rawPtr;
45  }
46 
47  bool doReleaseOne(void *rawPtr) {
48 
49  size_t found = m_container.erase(rawPtr);
50  return (0 != found);
51  }
52 
53  private:
54  typedef std::map<void *, boost::any> Container;
55  Container m_container;
56  };
57 
59  protected:
61 
62  void *doInsert(void *rawPtr, boost::any smartPtr) {
63  m_container.insert(std::make_pair(rawPtr, smartPtr));
64  return rawPtr;
65  }
66 
67  bool doReleaseOne(void *rawPtr) {
68  auto it = m_container.find(rawPtr);
69  if (m_container.end() != it) {
70  m_container.erase(it);
71  return true;
72  }
73  return false;
74  }
75 
76  private:
77  typedef std::multimap<void *, boost::any> Container;
78  Container m_container;
79  };
82  template <typename Policy = SingleOwnershipPolicy>
83  class BasicKeyedOwnershipContainer : private Policy {
84  public:
88  template <typename T> void *acquire(T ptr) {
89  return Policy::doInsert(ptr.get(), ptr);
90  }
91 
100  bool release(void *ptr) { return Policy::doReleaseOne(ptr); }
101  };
104 
107 } // namespace util
108 } // namespace osvr
109 
110 #endif // INCLUDED_KeyedOwnershipContainer_h_GUID_002CD118_DF06_45AC_44D8_C37BA99E0E93
Definition: RunLoopManager.h:42
void * acquire(T ptr)
Adds an object held by a smart pointer to our ownership, returning its void * usable to release it be...
Definition: KeyedOwnershipContainer.h:88
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: KeyedOwnershipContainer.h:39
Definition: KeyedOwnershipContainer.h:58
bool release(void *ptr)
Releases the indicated smart pointer in our ownership, if we have it.
Definition: KeyedOwnershipContainer.h:100
Holds on to smart pointers by value, and lets you free them by providing the corresponding void *...
Definition: KeyedOwnershipContainer.h:83