OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
PluginSpecificRegistrationContextImpl.cpp
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 #define OSVR_DEV_VERBOSE_DISABLE
26 
27 // Internal Includes
29 #include <osvr/Util/Verbosity.h>
30 
31 // Library/third-party includes
32 #include <boost/range/adaptor/reversed.hpp>
33 
34 // Standard includes
35 #include <stdexcept>
36 
37 namespace osvr {
38 namespace pluginhost {
41  : PluginSpecificRegistrationContext(name), m_parent(nullptr) {
42  OSVR_DEV_VERBOSE("PluginSpecificRegistrationContextImpl:\t"
43  << "Creating a plugin registration context for "
44  << name);
45  }
46 
49  OSVR_DEV_VERBOSE("PluginSpecificRegistrationContextImpl:\t"
50  "Destroying plugin reg context for "
51  << getName());
52 
53  // Delete the data in reverse order.
54  for (auto &ptr : m_dataList | boost::adaptors::reversed) {
55  ptr.reset();
56  }
57  m_parent = nullptr; // before anything else destructs, for safety?
58  }
59 
61  libfunc::PluginHandle &handle) {
62  m_handle = handle;
63  }
64 
66  RegistrationContext &parent) {
67  if (m_parent != nullptr && m_parent != &parent) {
68  throw std::logic_error(
69  "Can't set the registration context parent - already set!");
70  }
71  m_parent = &parent;
72  }
73 
75  if (m_parent == nullptr) {
76  throw std::logic_error(
77  "Can't access the registration context parent - it is null!");
78  }
79  return *m_parent;
80  }
81 
82  RegistrationContext const &
84  if (m_parent == nullptr) {
85  throw std::logic_error(
86  "Can't access the registration context parent - it is null!");
87  }
88  return *m_parent;
89  }
90 
91  void
93  OSVR_DEV_VERBOSE("PluginSpecificRegistrationContext:\t"
94  "In triggerHardwareDetectCallbacks for "
95  << getName());
96 
97  for (auto const &f : m_hardwareDetectCallbacks) {
98  f(this);
99  }
100  }
101 
103  const std::string &driverName, const std::string &params) const {
104  auto it = m_driverInstantiationCallbacks.find(driverName);
105  if (it == end(m_driverInstantiationCallbacks)) {
106  throw std::logic_error("No driver initialization callback was "
107  "registered for the driver name " +
108  driverName);
109  }
110  OSVR_ReturnCode ret = (it->second)(params.c_str());
111  if (ret != OSVR_RETURN_SUCCESS) {
112  throw std::runtime_error("Failure returned from driver "
113  "initialization callback by name " +
114  driverName);
115  }
116  }
117 
119  OSVR_PluginDataDeleteCallback deleteCallback, void *pluginData) {
120  m_dataList.emplace_back(pluginData, deleteCallback);
121  OSVR_DEV_VERBOSE("PluginSpecificRegistrationContext:\t"
122  "Now have "
123  << m_dataList.size()
124  << " data delete callbacks registered for "
125  << getName());
126  }
127 
129  OSVR_HardwareDetectCallback detectCallback, void *userData) {
130  OSVR_DEV_VERBOSE("PluginSpecificRegistrationContext:\t"
131  "In registerHardwareDetectCallback");
132  m_hardwareDetectCallbacks.emplace_back(detectCallback, userData);
133  OSVR_DEV_VERBOSE("PluginSpecificRegistrationContext:\t"
134  "Now have "
135  << m_hardwareDetectCallbacks.size()
136  << " hardware detect callbacks registered for "
137  << getName());
138  }
139 
140  void
142  const char *name, OSVR_DriverInstantiationCallback constructor,
143  void *userData) {
144  OSVR_DEV_VERBOSE("PluginSpecificRegistrationContext:\t"
145  "In registerDriverInstantiationCallback");
146  std::string n(name);
147  if (n.empty()) {
148  throw std::logic_error("Cannot register a driver instantiation "
149  "callback with an empty name!");
150  }
151  using namespace std::placeholders;
152  auto it = m_driverInstantiationCallbacks.find(name);
153  if (it != end(m_driverInstantiationCallbacks)) {
154  throw std::logic_error("A driver initialization callback by this "
155  "name for this plugin has already been "
156  "registered!");
157  }
158  auto opaque = extractOpaquePointer();
159  m_driverInstantiationCallbacks[name] =
160  [constructor, opaque, userData](const char *params) {
161  return constructor(opaque, params, userData);
162  };
163  }
164 
166  return m_data;
167  }
168 
170  return m_data;
171  }
172 } // namespace pluginhost
173 } // namespace osvr
void setParent(RegistrationContext &parent)
Set parent registration context.
virtual void registerDriverInstantiationCallback(const char *name, OSVR_DriverInstantiationCallback constructor, void *userData)
Register a callback for constructing a driver by name with parameters.
Class responsible for hosting plugins, along with their registration and destruction.
void instantiateDriver(const std::string &driverName, const std::string &params=std::string()) const
Call a driver instantiation callback for the given driver name.
Class providing the external interface of a registration context backing a single plugin...
const std::string & getName() const
Accessor for plugin name.
void(* OSVR_PluginDataDeleteCallback)(void *pluginData)
Function type of a Plugin Data Delete callback.
A data structure storing "any" by name, to reduce coupling.
Definition: AnyMap.h:42
OSVR_ReturnCode(* OSVR_HardwareDetectCallback)(OSVR_PluginRegContext ctx, void *userData)
Function type of a Hardware Detect callback.
void takePluginHandle(libfunc::PluginHandle &handle)
Assume ownership of the plugin handle keeping the plugin library loaded.
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
void triggerHardwareDetectCallbacks()
Call all hardware detect callbacks registered by this plugin, if any.
virtual RegistrationContext & getParent()
Get parent registration context.
OSVR_PluginRegContext extractOpaquePointer()
Extracts the opaque pointer for this interface to send to C.
OSVR_ReturnCode(* OSVR_DriverInstantiationCallback)(OSVR_PluginRegContext ctx, const char *params, void *userData)
Function type of a driver instantiation callback.
virtual void registerHardwareDetectCallback(OSVR_HardwareDetectCallback detectCallback, void *userData)
Register a callback to be invoked on some hardware detection event.
Internal, configured header file for verbosity macros.
virtual void registerDataWithDeleteCallback(OSVR_PluginDataDeleteCallback deleteCallback, void *pluginData)
Register data and a delete callback to be called on plugin unload.