OSVR-Core
WrapCallback.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_WrapCallback_h_GUID_4C52D585_7A4B_49BA_D069_B1F3B42E9896
26 #define INCLUDED_WrapCallback_h_GUID_4C52D585_7A4B_49BA_D069_B1F3B42E9896
27 
28 // Internal Includes
29 #include <osvr/Util/TimeValue.h>
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <functional>
36 #include <memory>
37 
38 namespace osvr {
39 namespace pluginkit {
40  namespace detail {
41  template <typename ReportType>
42  using WrappedCallbackFunction =
43  std::function<void(const OSVR_TimeValue *, const ReportType *)>;
44 
45  template <typename ReportType>
46  using WrappedCallbackPtr =
47  std::unique_ptr<WrappedCallbackFunction<ReportType>>;
48  template <typename ReportType>
49  void callbackCaller(void *userdata, const OSVR_TimeValue *timestamp,
50  const ReportType *report) {
51  auto &f =
52  *static_cast<WrappedCallbackFunction<ReportType> *>(userdata);
53  f(timestamp, report);
54  }
55  template <typename ReportType> struct CallbackType_impl {
56  typedef void (*type)(void *, const OSVR_TimeValue *,
57  const ReportType *);
58  };
59  template <typename ReportType>
60  using CallbackType = typename CallbackType_impl<ReportType>::type;
61 
62  template <typename ReportType>
63  inline CallbackType<ReportType> getCaller() {
64  return &callbackCaller<ReportType>;
65  }
66 
67  template <typename ReportType, typename F>
68  inline std::pair<CallbackType<ReportType>,
69  WrappedCallbackPtr<ReportType>>
70  wrapCallback(F &&f) {
71  auto functor = WrappedCallbackPtr<ReportType>{
72  new WrappedCallbackFunction<ReportType>{std::forward<F>(f)}};
73  return std::make_pair(getCaller<ReportType>(), std::move(functor));
74  }
75  } // namespace detail
76  using detail::wrapCallback;
77  using detail::WrappedCallbackPtr;
78 } // namespace pluginkit
79 } // namespace osvr
80 
81 #endif // INCLUDED_WrapCallback_h_GUID_4C52D585_7A4B_49BA_D069_B1F3B42E9896
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: newuoa.h:1888
Header providing a C++ wrapper around TimeValueC.h.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
Definition: WrapCallback.h:55