OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
CommonComponent.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_CommonComponent_h_GUID_199AFD54_3489_4295_30F9_00B7CE33FE31
26 #define INCLUDED_CommonComponent_h_GUID_199AFD54_3489_4295_30F9_00B7CE33FE31
27 
28 // Internal Includes
30 #include <osvr/Common/Export.h>
32 #include <osvr/TypePack/List.h>
34 
35 // Library/third-party includes
36 // - none
37 
38 // Standard includes
39 #include <vector>
40 #include <functional>
41 
42 namespace osvr {
43 namespace common {
44  namespace messages {
45  class VRPNPing : public MessageRegistration<VRPNPing> {
46  public:
47  OSVR_COMMON_EXPORT static const char *identifier();
48  };
49  class VRPNPong : public MessageRegistration<VRPNPong> {
50  public:
51  OSVR_COMMON_EXPORT static const char *identifier();
52  };
54  : public MessageRegistration<VRPNGotFirstConnection> {
55  public:
56  OSVR_COMMON_EXPORT static const char *identifier();
57  };
59  : public MessageRegistration<VRPNGotConnection> {
60  public:
61  OSVR_COMMON_EXPORT static const char *identifier();
62  };
64  : public MessageRegistration<VRPNDroppedConnection> {
65  public:
66  OSVR_COMMON_EXPORT static const char *identifier();
67  };
69  : public MessageRegistration<VRPNDroppedLastConnection> {
70  public:
71  OSVR_COMMON_EXPORT static const char *identifier();
72  };
73 
80  } // namespace messages
83  public:
88  static OSVR_COMMON_EXPORT shared_ptr<CommonComponent> create();
89 
90  typedef std::function<void()> Handler;
91 
92  messages::VRPNPing ping;
93 
96  OSVR_COMMON_EXPORT void registerPingHandler(Handler const &handler);
97 
98  messages::VRPNPong pong;
99 
102  OSVR_COMMON_EXPORT void registerPongHandler(Handler const &handler);
103 
104  messages::VRPNGotFirstConnection gotFirstConnection;
105  messages::VRPNGotConnection gotConnection;
106  messages::VRPNDroppedConnection droppedConnection;
107  messages::VRPNDroppedLastConnection droppedLastConnection;
108 
109  template <typename T>
110  void registerHandler(MessageRegistration<T> const &message,
111  Handler const &handler) {
112  auto &handlers = typepack::get<T>(m_handlers);
113  m_registerHandlerImpl(handlers, message.getMessageType(), handler);
114  }
115 
116  private:
117  CommonComponent();
118  void m_parentSet() override;
119 
120  using HandlerList = std::vector<Handler>;
121  OSVR_COMMON_EXPORT void
122  m_registerHandlerImpl(HandlerList &handlers,
123  osvr::common::RawMessageType rawMessageType,
124  Handler const &handler);
125 
126  static int VRPN_CALLBACK m_baseHandler(void *userdata,
127  vrpn_HANDLERPARAM);
128 
130  std::vector<Handler>> m_handlers;
131  };
132 } // namespace common
133 } // namespace osvr
134 
135 #endif // INCLUDED_CommonComponent_h_GUID_199AFD54_3489_4295_30F9_00B7CE33FE31
static shared_ptr< CommonComponent > create()
Factory method.
static const char * identifier()
A wrapper for a template parameter pack of types.
Definition: List.h:52
static const char * identifier()
A class that uses types as an index into a container with uniform-typed contents, somewhat like a map...
Definition: TypeKeyedMap.h:49
BaseDevice component, for the VRPN built-in common messages.
Header with a run-time extensions to the TypePack simple template-parameter-pack-based metaprogrammin...
void registerPongHandler(Handler const &handler)
Register a pong handler: a pong replies to a ping, and is sent from a server device to the correspond...
CRTP class template wrapping message-specific data and/or logic.
void registerPingHandler(Handler const &handler)
Register a ping handler: a ping is sent from a client device to the corresponding server device upon ...
Header.
Automatically-generated export header - do not edit!
Type-safe wrapper with built-in default for a VRPN "message type" integer.