OSVR-Core
PluginRegistration.h
Go to the documentation of this file.
1 
13 // Copyright 2014 Sensics, Inc.
14 //
15 // Licensed under the Apache License, Version 2.0 (the "License");
16 // you may not use this file except in compliance with the License.
17 // You may obtain a copy of the License at
18 //
19 // http://www.apache.org/licenses/LICENSE-2.0
20 //
21 // Unless required by applicable law or agreed to in writing, software
22 // distributed under the License is distributed on an "AS IS" BASIS,
23 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 // See the License for the specific language governing permissions and
25 // limitations under the License.
26 
27 #ifndef INCLUDED_PluginRegistration_h_GUID_4F5D6422_2977_40A9_8BA0_F86FD6245CE9
28 #define INCLUDED_PluginRegistration_h_GUID_4F5D6422_2977_40A9_8BA0_F86FD6245CE9
29 
30 // Internal Includes
35 
36 // Library/third-party includes
37 #include <boost/utility/enable_if.hpp>
38 #include <boost/type_traits/is_pointer.hpp>
39 #include <boost/type_traits/remove_pointer.hpp>
40 #include <boost/static_assert.hpp>
41 
42 // Standard includes
43 #include <cstddef>
44 #include <string>
45 #include <stdexcept>
46 
47 namespace osvr {
48 namespace pluginkit {
56  template <typename T>
73  OSVR_ReturnCode ret = osvrPluginRegisterDataWithDeleteCallback(
74  ctx, &::osvr::util::generic_deleter<T>, static_cast<void *>(obj));
75  if (ret != OSVR_RETURN_SUCCESS) {
76  throw std::runtime_error("registerObjectForDeletion failed!");
77  }
78  return obj;
79  }
80 
81 #ifndef OSVR_DOXYGEN_EXTERNAL
82  namespace detail {
86  template <typename T>
87  inline OSVR_ReturnCode registerHardwareDetectCallbackImpl(
88  OSVR_PluginRegContext ctx, T functor,
89  typename boost::enable_if<boost::is_pointer<T> >::type * = NULL) {
90  typedef typename boost::remove_pointer<T>::type FunctorType;
91  registerObjectForDeletion(ctx, functor);
94  FunctorType, util::this_last_t>::call,
95  static_cast<void *>(functor));
96  }
97 
100  template <typename T>
101  inline OSVR_ReturnCode registerHardwareDetectCallbackImpl(
102  OSVR_PluginRegContext ctx, T functor,
103  typename boost::disable_if<boost::is_pointer<T> >::type * = NULL) {
104 #ifdef OSVR_HAVE_BOOST_IS_COPY_CONSTRUCTIBLE
105  BOOST_STATIC_ASSERT_MSG(boost::is_copy_constructible<T>::value,
106  "Hardware detect callback functors must be "
107  "either passed as a pointer or be "
108  "copy-constructible");
109 #endif
110  T *functorCopy = new T(functor);
111  return registerHardwareDetectCallbackImpl(ctx, functorCopy);
112  }
113 
116  template <typename T>
118  OSVR_PluginRegContext ctx, const char driverName[], T functor,
119  typename boost::enable_if<boost::is_pointer<T> >::type * = NULL) {
120  typedef typename boost::remove_pointer<T>::type FunctorType;
121  registerObjectForDeletion(ctx, functor);
123  ctx, driverName,
125  FunctorType, util::this_last_t>::call,
126  static_cast<void *>(functor));
127  }
130  template <typename T>
132  OSVR_PluginRegContext ctx, const char driverName[], T functor,
133  typename boost::disable_if<boost::is_pointer<T> >::type * = NULL) {
134 #ifdef OSVR_HAVE_BOOST_IS_COPY_CONSTRUCTIBLE
135  BOOST_STATIC_ASSERT_MSG(
136  boost::is_copy_constructible<T>::value,
137  "Driver instantiation callback functors must be "
138  "either passed as a pointer or be "
139  "copy-constructible");
140 #endif
141  T *functorCopy = new T(functor);
142  return registerDriverInstantiationCallbackImpl(ctx, driverName,
143  functorCopy);
144  }
145  } // namespace detail
146 #endif
147 
162  template <typename T>
164  T functor) {
165  OSVR_ReturnCode ret =
167  if (ret != OSVR_RETURN_SUCCESS) {
168  throw std::runtime_error("registerHardwareDetectCallback failed!");
169  }
170  }
171 
189  template <typename T>
191  const char driverName[],
192  T functor) {
194  ctx, driverName, functor);
195  if (ret != OSVR_RETURN_SUCCESS) {
196  throw std::runtime_error(
197  "registerDriverInstantiationCallback failed!");
198  }
199  }
201 
202  inline void log(OSVR_PluginRegContext ctx, OSVR_LogLevel severity,
203  const char *message) {
204  osvrPluginLog(ctx, severity, message);
205  }
206 
207 } // namespace pluginkit
208 } // namespace osvr
209 
210 #endif // INCLUDED_PluginRegistration_h_GUID_4F5D6422_2977_40A9_8BA0_F86FD6245CE9
Header providing a templated deleter function.
OSVR_ReturnCode registerDriverInstantiationCallbackImpl(OSVR_PluginRegContext ctx, const char driverName[], T functor, typename boost::enable_if< boost::is_pointer< T > >::type *=NULL)
Traits-based overload to register an instantiation callback where we&#39;re given a pointer to a function...
Definition: PluginRegistration.h:117
Header wrapping <boost/type_traits/is_copy_constructible.hpp> for when we just use it with static ass...
OSVR_PLUGINKIT_EXPORT OSVR_ReturnCode osvrPluginRegisterDataWithDeleteCallback(OSVR_INOUT_PTR OSVR_PluginRegContext ctx, OSVR_IN OSVR_PluginDataDeleteCallback deleteCallback, OSVR_INOUT_PTR void *pluginData) OSVR_FUNC_NONNULL((1
Register plugin data along with an appropriate deleter callback.
OSVR_PLUGINKIT_EXPORT OSVR_ReturnCode osvrPluginRegisterHardwareDetectCallback(OSVR_INOUT_PTR OSVR_PluginRegContext ctx, OSVR_IN OSVR_HardwareDetectCallback detectCallback, OSVR_IN_OPT void *userData OSVR_CPP_ONLY(=NULL)) OSVR_FUNC_NONNULL((1))
Register a callback in your plugin to be notified when hardware should be detected again...
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
void registerHardwareDetectCallback(OSVR_PluginRegContext ctx, T functor)
Registers a function object to be called when the core requests a hardware detection.
Definition: PluginRegistration.h:163
OSVR_PLUGINKIT_EXPORT OSVR_ReturnCode OSVR_PLUGINKIT_EXPORT void osvrPluginLog(OSVR_INOUT_PTR OSVR_PluginRegContext ctx, OSVR_IN OSVR_LogLevel severity, OSVR_IN const char *message) OSVR_FUNC_NONNULL((1
Log a message to the plugin&#39;s log channel.
Definition: newuoa.h:1888
Header providing templated functions to automate the process of calling a C++ member function (specif...
OSVR_PLUGINKIT_EXPORT OSVR_ReturnCode osvrRegisterDriverInstantiationCallback(OSVR_INOUT_PTR OSVR_PluginRegContext ctx, OSVR_IN_STRZ const char *name, OSVR_IN_PTR OSVR_DriverInstantiationCallback cb, OSVR_IN_OPT void *userData OSVR_CPP_ONLY(=NULL)) OSVR_FUNC_NONNULL((1
Register an instantiation callback (constructor) for a driver type.
OSVR_ReturnCode(* OSVR_HardwareDetectCallback)(OSVR_PluginRegContext ctx, void *userData)
Function type of a Hardware Detect callback.
Definition: PluginCallbackTypesC.h:51
T * registerObjectForDeletion(OSVR_PluginRegContext ctx, T *obj)
Registers an object to be destroyed with delete when the plugin is unloaded.
Definition: PluginRegistration.h:72
void registerDriverInstantiationCallback(OSVR_PluginRegContext ctx, const char driverName[], T functor)
Registers a function object to be called when the server is told to instantiate a driver by name with...
Definition: PluginRegistration.h:190
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
Struct containing a single static function member named "call" that serves as a converter from a func...
Definition: GenericCaller.h:241
OSVR_LogLevel
Log message severity levels.
Definition: LogLevelC.h:44
OSVR_ReturnCode(* OSVR_DriverInstantiationCallback)(OSVR_PluginRegContext ctx, const char *params, void *userData)
Function type of a driver instantiation callback.
Definition: PluginCallbackTypesC.h:55
OSVR_EXTERN_C_BEGIN typedef void * OSVR_PluginRegContext
A context pointer passed in to your plugin&#39;s entry point and other locations of control flow transfer...
Definition: PluginRegContextC.h:47
Tag type indicating the last parameter of the function contains the "this" pointer.
Definition: GenericCaller.h:76
OSVR_ReturnCode registerHardwareDetectCallbackImpl(OSVR_PluginRegContext ctx, T functor, typename boost::enable_if< boost::is_pointer< T > >::type *=NULL)
Traits-based overload to register a hardware detect callback where we&#39;re given a pointer to a functio...
Definition: PluginRegistration.h:87