OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ClientContext.h
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 #ifndef INCLUDED_ContextImpl_h_GUID_9000C62E_3693_4888_83A2_0D26F4591B6A
26 #define INCLUDED_ContextImpl_h_GUID_9000C62E_3693_4888_83A2_0D26F4591B6A
27 
28 // Internal Includes
29 #include <osvr/Common/Export.h>
36 #include <osvr/Util/UniquePtr.h>
37 #include <osvr/Util/SharedPtr.h>
38 #include <osvr/Util/LogLevel.h>
39 #include <osvr/Util/Logger.h>
40 
41 // Library/third-party includes
42 #include <boost/noncopyable.hpp>
43 #include <boost/any.hpp>
44 
45 // Standard includes
46 #include <string>
47 #include <vector>
48 #include <map>
49 
50 struct OSVR_ClientContextObject : boost::noncopyable {
51  public:
52  typedef std::vector<osvr::common::ClientInterfacePtr> InterfaceList;
54  OSVR_COMMON_EXPORT virtual ~OSVR_ClientContextObject();
55 
57  OSVR_COMMON_EXPORT void update();
58 
60  std::string const &getAppId() const;
61 
66  OSVR_COMMON_EXPORT osvr::common::ClientInterfacePtr
67  getInterface(const char path[]);
68 
77  OSVR_COMMON_EXPORT osvr::common::ClientInterfacePtr
79 
80  InterfaceList const &getInterfaces() const { return m_interfaces; }
81 
83  OSVR_COMMON_EXPORT void sendRoute(std::string const &route);
84 
86  OSVR_COMMON_EXPORT std::string
87  getStringParameter(std::string const &path) const;
88 
90  OSVR_COMMON_EXPORT osvr::common::PathTree const &getPathTree() const;
91 
94  template <typename T> void *acquireObject(T obj) {
95  return m_ownedObjects.acquire(obj);
96  }
97 
102  OSVR_COMMON_EXPORT bool releaseObject(void *obj);
103 
105  OSVR_COMMON_EXPORT osvr::common::Transform const &
106  getRoomToWorldTransform() const;
107 
109  OSVR_COMMON_EXPORT void
111 
113  OSVR_COMMON_EXPORT osvr::common::ClientContextDeleter getDeleter() const;
114 
117  OSVR_COMMON_EXPORT bool getStatus() const;
118 
120  OSVR_COMMON_EXPORT void log(osvr::util::log::LogLevel severity,
121  const char *message);
122 
124  OSVR_COMMON_EXPORT osvr::util::log::LoggerPtr const &logger() const;
125 
126  protected:
128  OSVR_COMMON_EXPORT
129  OSVR_ClientContextObject(const char appId[],
130  osvr::common::ClientContextDeleter del);
132  OSVR_COMMON_EXPORT
134  const char appId[],
135  osvr::common::ClientInterfaceFactory const &interfaceFactory,
136  osvr::common::ClientContextDeleter del);
137 
138  private:
139  virtual void m_update() = 0;
140  virtual void m_sendRoute(std::string const &route) = 0;
141  OSVR_COMMON_EXPORT virtual bool m_getStatus() const;
144  OSVR_COMMON_EXPORT virtual void
145  m_handleNewInterface(osvr::common::ClientInterfacePtr const &iface);
148  OSVR_COMMON_EXPORT virtual void
149  m_handleReleasingInterface(osvr::common::ClientInterfacePtr const &iface);
150 
152  OSVR_COMMON_EXPORT virtual osvr::common::PathTree const &
153  m_getPathTree() const = 0;
154 
157  virtual osvr::common::Transform const &
158  m_getRoomToWorldTransform() const = 0;
159 
162  virtual void
163  m_setRoomToWorldTransform(osvr::common::Transform const &xform) = 0;
164 
165  std::string const m_appId;
166  InterfaceList m_interfaces;
167  osvr::common::ClientInterfaceFactory m_clientInterfaceFactory;
168 
170  osvr::common::ClientContextDeleter m_deleter;
171 
173  osvr::util::log::LoggerPtr m_logger;
175  osvr::util::log::LoggerPtr m_clientLogger;
176 };
177 
178 namespace osvr {
179 namespace common {
182  OSVR_COMMON_EXPORT void deleteContext(ClientContext *ctx);
183 
184  namespace detail {
187  void operator()(ClientContext *ctx) const {
188  if (ctx) {
189  deleteContext(ctx);
190  }
191  }
192  };
193  namespace {
195  template <typename T>
196  inline void context_deleter(ClientContext *obj) {
197  T *o = static_cast<T *>(obj);
198  delete o;
199  }
200  } // namespace
201  } // namespace detail
202 
205  template <typename T = ClientContext>
206  using ClientContextUniquePtr = unique_ptr<T, ClientContextDeleter>;
207  using ClientContextSharedPtr = shared_ptr<ClientContext>;
208 
212  template <typename T, typename... Args>
213  inline T *makeContext(Args... args) {
214  return new T(std::forward<Args>(args)..., &detail::context_deleter<T>);
215  }
216 
219  template <typename T>
220  inline ClientContextSharedPtr wrapSharedContext(T *context) {
221  ClientContextSharedPtr ret(context, &deleteContext);
222  return ret;
223  }
224 } // namespace common
225 } // namespace osvr
226 
227 #endif // INCLUDED_ContextImpl_h_GUID_9000C62E_3693_4888_83A2_0D26F4591B6A
bool getStatus() const
Returns true if we are started up and fully connected (path tree received, etc.)
unique_ptr< T, ClientContextDeleter > ClientContextUniquePtr
Template alias for a ClientContext unique_ptr with the correct deleter class.
shared_ptr< ClientInterface > ClientInterfacePtr
Pointer for holding ClientInterface objects safely.
void * acquire(T ptr)
Adds an object held by a smart pointer to our ownership, returning its void * usable to release it be...
std::string getStringParameter(std::string const &path) const
Gets a string parameter value.
A tree representation, with path/url syntax, of the known OSVR system.
Definition: PathTree.h:43
osvr::common::Transform const & getRoomToWorldTransform() const
Gets the transform from room space to world space.
void sendRoute(std::string const &route)
Sends a JSON route/transform object to the server.
Header to bring unique_ptr into the osvr namespace.
Header to bring shared_ptr into the osvr namespace.
osvr::common::ClientInterfacePtr getInterface(const char path[])
Creates an interface object for the given path. The context retains shared ownership.
osvr::common::ClientInterfacePtr releaseInterface(osvr::common::ClientInterface *iface)
Searches through this context to determine if the passed interface object has been retained...
std::string const & getAppId() const
Accessor for app ID.
Holds on to smart pointers by value, and lets you free them by providing the corresponding void *...
void setRoomToWorldTransform(osvr::common::Transform const &xform)
Sets the transform from room space to world space.
void * acquireObject(T obj)
Pass (smart-pointer) ownership of some object to the client context.
Definition: ClientContext.h:94
Deleter for use with std::unique_ptr.
ClientContextSharedPtr wrapSharedContext(T *context)
Wrap a client context pointer in a shared pointer with the correct custom deleter.
OSVR_ClientContextObject(const char appId[], osvr::common::ClientContextDeleter del)
Constructor for derived class use only.
bool releaseObject(void *obj)
Frees some object whose lifetime is controlled by the client context.
osvr::common::PathTree const & getPathTree() const
Accessor for the path tree.
Header.
virtual ~OSVR_ClientContextObject()
Destructor.
void log(osvr::util::log::LogLevel severity, const char *message)
Logs a message from the client.
std::function< ClientInterfacePtr(ClientContext &, const char[])> ClientInterfaceFactory
A factory function type taking the client context and path, and returning a ClientInterfacePtr. The ClientContext will handle notifying its internals about the new interface before returning it.
Header to include for OSVR-internal usage of the logging mechanism: provides the needed definition of...
Automatically-generated export header - do not edit!
osvr::util::log::LoggerPtr const & logger() const
Provides logger access for related internal classes.
void deleteContext(ClientContext *ctx)
Use the stored deleter to appropriately delete the client context.
T * makeContext(Args...args)
Create a subclass object of ClientContext, setting the deleter appropriately by passing it as the las...
void update()
System-wide update method.
Spatial transformation, consisting of both pre and post components.
Definition: Transform.h:44
osvr::common::ClientContextDeleter getDeleter() const
Returns the specialized deleter for this object.