OSVR-Core
InterfaceCallbacks.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_InterfaceCallbacks_h_GUID_0CE1EE79_D74A_4EAA_CF16_3AABDA3A1B6A
26 #define INCLUDED_InterfaceCallbacks_h_GUID_0CE1EE79_D74A_4EAA_CF16_3AABDA3A1B6A
27 
28 // Internal Includes
31 #include <osvr/Util/TimeValue.h>
32 
33 // Library/third-party includes
35 #include <osvr/TypePack/Quote.h>
36 
37 // Standard includes
38 #include <vector>
39 #include <functional>
40 
41 namespace osvr {
42 namespace common {
47  struct CallbackStorage {
48  template <typename ReportType>
49  using apply = std::vector<
50  std::function<void(const OSVR_TimeValue *, const ReportType *)>>;
51  };
52 
53  using CallbackTuple =
58  public:
59  template <typename CallbackType>
60  void addCallback(CallbackType cb, void *userdata) {
62  typepack::get<ReportType>(m_callbacks)
63  .push_back(
64  [cb, userdata](util::time::TimeValue const *timestamp,
65  ReportType const *report) {
66  cb(userdata, timestamp, report);
67  });
68  }
69 
70  template <typename ReportType>
71  void triggerCallbacks(util::time::TimeValue const &timestamp,
72  ReportType const &report) const {
73  for (auto const &f : typepack::cget<ReportType>(m_callbacks)) {
74  f(&timestamp, &report);
75  }
76  }
77 
78  template <typename ReportType>
79  std::size_t getNumCallbacksFor(ReportType const &) const {
80  return typepack::cget<ReportType>(m_callbacks).size();
81  }
82 
83  private:
84  CallbackTuple m_callbacks;
85  };
86 
87 } // namespace common
88 } // namespace osvr
89 
90 #endif // INCLUDED_InterfaceCallbacks_h_GUID_0CE1EE79_D74A_4EAA_CF16_3AABDA3A1B6A
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
typepack::t_< ReportFromCallback< T >> ReportFromCallback_t
Alias for the ReportType associated with a given CallbackType.
Definition: ReportTraits.h:88
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
detail::size< coerce_list< Ts... >> size
Get the size of a list (number of elements.)
Definition: Size.h:56
Header providing common::traits::ReportFromCallback.
Trait computing the storage for callbacks for a report type.
Definition: InterfaceCallbacks.h:47
Header containing a typelist of all special report types.
Header.
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
Class to maintain callbacks for an interface for each report type explicitly enumerated.
Definition: InterfaceCallbacks.h:57