OSVR-Core
ClientInterface.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_ClientInterface_h_GUID_A3A55368_DE2F_4980_BAE9_1C398B0D40A1
26 #define INCLUDED_ClientInterface_h_GUID_A3A55368_DE2F_4980_BAE9_1C398B0D40A1
27 
28 // Internal Includes
29 #include <osvr/Common/Export.h>
34 #include <osvr/Common/StateType.h>
36 #include <osvr/Common/Tracing.h>
38 #include <osvr/Util/ClientCallbackTypesC.h>
39 
40 // Library/third-party includes
41 #include <boost/noncopyable.hpp>
42 #include <boost/any.hpp>
43 
44 // Standard includes
45 #include <string>
46 #include <vector>
47 #include <functional>
48 
49 struct OSVR_ClientInterfaceObject : boost::noncopyable {
50 
51  protected:
53  OSVR_COMMON_EXPORT
55  std::string const &path);
56 
57  public:
58  ~OSVR_ClientInterfaceObject() {
59  osvr::common::tracing::markReleaseInterface(m_path);
60  }
61 
63  OSVR_COMMON_EXPORT std::string const &getPath() const;
64 
70  template <typename ReportType>
71  bool
72  getState(osvr::util::time::TimeValue &timestamp,
74  osvr::common::tracing::markGetState(m_path);
75  if (!m_state.hasState<ReportType>()) {
76  return false;
77  }
78  m_state.getState<ReportType>(timestamp, state);
79  return true;
80  }
81 
82  template <typename ReportType> bool hasStateForReportType() const {
83  return m_state.hasState<ReportType>();
84  }
85 
86  bool hasAnyState() const { return m_state.hasAnyState(); }
87 
89  template <typename ReportType>
90  void setState(const OSVR_TimeValue &timestamp, ReportType const &report) {
91  static_assert(
93  "Should only call setState if we're keeping state for this report "
94  "type!");
95  m_state.setStateFromReport(timestamp, report);
96  }
98 
103  template <typename CallbackType>
104  void registerCallback(CallbackType cb, void *userdata) {
105  m_callbacks.addCallback(cb, userdata);
106  }
107 
110  template <typename ReportType>
111  void triggerCallbacks(const OSVR_TimeValue &timestamp,
112  ReportType const &report) {
113  m_callbacks.triggerCallbacks(timestamp, report);
114  }
115 
117  template <typename ReportType>
118  std::size_t getNumCallbacksFor(ReportType const &r) const {
119  return m_callbacks.getNumCallbacksFor(r);
120  }
122 
124  void update();
125 
126  osvr::common::ClientContext &getContext() const { return m_ctx; }
127 
129  boost::any &data() { return m_data; }
130 
131  private:
133  std::string const m_path;
136  boost::any m_data;
137 };
138 
139 #endif // INCLUDED_ClientInterface_h_GUID_A3A55368_DE2F_4980_BAE9_1C398B0D40A1
typepack::t_< StateType< T >> StateFromReport_t
Alias for the StateType associated with a given ReportType.
Definition: ReportTraits.h:83
void update()
Update any state.
Definition: ClientInterface.cpp:45
Class to maintain state for an interface for each report (and thus state) type explicitly enumerated...
Definition: InterfaceState.h:66
Header declaring opaque types used by Client and ClientKit.
boost::any & data()
Access the type-erased data for this interface.
Definition: ClientInterface.h:129
Header.
OSVR_COMMON_EXPORT OSVR_ClientInterfaceObject(osvr::common::ClientContext &ctx, std::string const &path)
Constructor - only to be called by a factory function.
Definition: ClientInterface.cpp:35
OSVR_COMMON_EXPORT std::string const & getPath() const
Get the path as a string.
Definition: ClientInterface.cpp:41
Type predicate: Whether callbacks of a report type should store state for that type.
Definition: ReportStateTraits.h:42
void triggerCallbacks(const OSVR_TimeValue &timestamp, ReportType const &report)
Trigger all callbacks for the given known report type.
Definition: ClientInterface.h:111
void getState(util::time::TimeValue &timestamp, traits::StateFromReport_t< ReportType > &state) const
Definition: InterfaceState.h:86
Definition: ClientContext.h:50
Header.
std::size_t getNumCallbacksFor(ReportType const &r) const
Get the number of registered callbacks for the given report type.
Definition: ClientInterface.h:118
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
Definition: ClientInterface.h:49
Class to maintain callbacks for an interface for each report type explicitly enumerated.
Definition: InterfaceCallbacks.h:57
void setState(const OSVR_TimeValue &timestamp, ReportType const &report)
Set saved state for a report type.
Definition: ClientInterface.h:90