OSVR-Core
CallbackWrapper.h
Go to the documentation of this file.
1 
12 // Copyright 2014 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 #ifndef INCLUDED_CallbackWrapper_h_GUID_6169ADE2_5BA1_4A81_47C9_9E492F6405ED
27 #define INCLUDED_CallbackWrapper_h_GUID_6169ADE2_5BA1_4A81_47C9_9E492F6405ED
28 
29 // Internal Includes
30 // - none
31 
32 // Library/third-party includes
33 #include <boost/type_traits/function_traits.hpp>
34 #include <boost/type_traits/remove_pointer.hpp>
35 
36 // Standard includes
37 #include <type_traits>
38 #include <utility>
39 
40 namespace osvr {
41 namespace util {
45  template <typename FunctionPtrType> class CallbackWrapper {
46  public:
48  CallbackWrapper(FunctionPtrType f, void *userData)
49  : m_f(f), m_ud(userData) {}
50 
52  typedef
53  typename boost::remove_pointer<FunctionPtrType>::type FunctionType;
54 
56  typedef typename boost::function_traits<FunctionType>::result_type
58 
60  template <typename... Args>
61  ReturnType operator()(Args &&... args) const {
62  return (*m_f)(std::forward<Args>(args)..., m_ud);
63  }
64 
65  private:
66  FunctionPtrType m_f;
67  void *m_ud;
68  };
69 } // namespace util
70 } // namespace osvr
71 #endif // INCLUDED_CallbackWrapper_h_GUID_6169ADE2_5BA1_4A81_47C9_9E492F6405ED
Definition: RunLoopManager.h:42
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
CallbackWrapper(FunctionPtrType f, void *userData)
Constructor from function pointer and user data pointer.
Definition: CallbackWrapper.h:48
boost::function_traits< FunctionType >::result_type ReturnType
Return type of the function (computed)
Definition: CallbackWrapper.h:57
A class template turning a callback with some number of arguments, with a userdata pointer last...
Definition: CallbackWrapper.h:45
ReturnType operator()(Args &&... args) const
Function call operator with non-void return.
Definition: CallbackWrapper.h:61
boost::remove_pointer< FunctionPtrType >::type FunctionType
Function type (remove pointer - computed)
Definition: CallbackWrapper.h:53