OSVR-Core
InternalInterfaceOwner.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_InternalInterfaceOwner_h_GUID_CE168C67_29A8_44A4_44E4_BCE428AF71EB
26 #define INCLUDED_InternalInterfaceOwner_h_GUID_CE168C67_29A8_44A4_44E4_BCE428AF71EB
27 
28 // Internal Includes
32 
33 // Library/third-party includes
34 #include <boost/assert.hpp>
35 
36 // Standard includes
37 #include <utility>
38 #include <algorithm>
39 
40 namespace osvr {
41 namespace client {
46  public:
48  InternalInterfaceOwner() { m_checkInvariants(); }
49 
53  : m_ctx(ctx), m_iface(iface) {
54  m_checkInvariants();
55  }
56 
60  common::ClientInterfacePtr const &iface)
61  : m_ctx(ctx), m_iface(iface.get()), m_owningIface(iface) {
62  m_checkInvariants();
63  }
64 
67  : m_ctx(ctx) {
68  m_owningIface = ctx->getInterface(path);
69  m_iface = m_owningIface.get();
70  m_checkInvariants();
71  }
72 
75 
78  operator=(InternalInterfaceOwner const &) = delete;
79 
82  : m_ctx(std::move(other.m_ctx)),
83  m_owningIface(std::move(other.m_owningIface)) {
84  std::swap(m_iface, other.m_iface);
85  m_checkInvariants();
86  }
87 
90  if (&other == this) {
91  return *this;
92  }
93  m_reset();
94  std::swap(m_ctx, other.m_ctx);
95  std::swap(m_iface, other.m_iface);
96  m_owningIface = std::move(other.m_owningIface);
97  m_checkInvariants();
98  return *this;
99  }
100 
102  typedef contained_type &reference;
103  typedef contained_type *pointer;
104 
106  explicit operator bool() const { return nullptr != m_iface; }
107 
110  reference operator*() const { return *m_iface; }
111  pointer operator->() const { return m_iface; }
113 
115  ~InternalInterfaceOwner() { m_reset(); }
116 
117  private:
118  void m_reset() {
119  if (m_ctx && m_iface) {
120  auto oldIface = m_ctx->releaseInterface(m_iface);
121  BOOST_ASSERT_MSG(oldIface, "We should never be the last one "
122  "holding a reference to this "
123  "interface!");
124  m_ctx = nullptr;
125  m_iface = nullptr;
126  m_owningIface.reset();
127  oldIface.reset();
128  }
129  }
130 
131  void m_checkInvariants() const {
132  BOOST_ASSERT_MSG(
133  (!m_iface || (m_iface && m_ctx)),
134  "If we have an interface, we must have a context.");
135  BOOST_ASSERT_MSG(!m_owningIface || (m_owningIface.get() == m_iface),
136  "Either owningIface should be empty or the same "
137  "as our raw pointer.");
138  }
139  common::ClientContext *m_ctx = nullptr;
140  osvr::common::ClientInterface *m_iface = nullptr;
141  osvr::common::ClientInterfacePtr m_owningIface;
142  };
143 
144 } // namespace client
145 } // namespace osvr
146 
147 #endif // INCLUDED_InternalInterfaceOwner_h_GUID_CE168C67_29A8_44A4_44E4_BCE428AF71EB
shared_ptr< ClientInterface > ClientInterfacePtr
Pointer for holding ClientInterface objects safely.
Definition: ClientInterfacePtr.h:43
Header declaring opaque types used by Client and ClientKit.
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
InternalInterfaceOwner & operator=(InternalInterfaceOwner const &)=delete
non-assignable
Definition: TypeSafeIdHash.h:44
InternalInterfaceOwner & operator=(InternalInterfaceOwner &&other)
move-assignable
Definition: InternalInterfaceOwner.h:89
Class that should be used for all internally-used client interface objects as it handles ownership wi...
Definition: InternalInterfaceOwner.h:45
InternalInterfaceOwner(InternalInterfaceOwner &&other)
move constructible
Definition: InternalInterfaceOwner.h:81
OSVR_COMMON_EXPORT osvr::common::ClientInterfacePtr getInterface(const char path[])
Creates an interface object for the given path.
Definition: ClientContext.cpp:94
OSVR_COMMON_EXPORT osvr::common::ClientInterfacePtr releaseInterface(osvr::common::ClientInterface *iface)
Searches through this context to determine if the passed interface object has been retained...
Definition: ClientContext.cpp:105
InternalInterfaceOwner(common::ClientContext *ctx, OSVR_ClientInterface iface)
Constructor.
Definition: InternalInterfaceOwner.h:51
InternalInterfaceOwner()
Empty constructor.
Definition: InternalInterfaceOwner.h:48
~InternalInterfaceOwner()
Destructor - frees interface.
Definition: InternalInterfaceOwner.h:115
Definition: ClientContext.h:50
InternalInterfaceOwner(common::ClientContext *ctx, common::ClientInterfacePtr const &iface)
Constructor that keeps an additional reference to the interface.
Definition: InternalInterfaceOwner.h:59
InternalInterfaceOwner(common::ClientContext *ctx, const char path[])
Constructor from a context and path.
Definition: InternalInterfaceOwner.h:66
Definition: ClientInterface.h:49