OSVR-Core
BaseDevice.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_BaseDevice_h_GUID_52C0B784_DB20_4FA6_0C87_47FB116DA61B
26 #define INCLUDED_BaseDevice_h_GUID_52C0B784_DB20_4FA6_0C87_47FB116DA61B
27 
28 // Internal Includes
29 #include <osvr/Common/Export.h>
35 #include <osvr/Common/Buffer.h>
37 #include <osvr/Util/TimeValue.h>
38 
39 // Library/third-party includes
40 #include <vrpn_ConnectionPtr.h>
41 #include <vrpn_Connection.h>
42 
43 // Standard includes
44 // - none
45 
46 namespace osvr {
47 namespace common {
48 
51  class BaseDevice {
52  public:
54  OSVR_COMMON_EXPORT virtual ~BaseDevice();
55 
58  template <typename T> T *addComponent(shared_ptr<T> component) {
59  T *ret = component.get();
60  m_addComponent(component);
61  return ret;
62  }
63 
64  void registerHandler(vrpn_MESSAGEHANDLER handler, void *userdata,
65  RawMessageType const &msgType);
66  void unregisterHandler(vrpn_MESSAGEHANDLER handler, void *userdata,
67  RawMessageType const &msgType);
68 
71  template <typename T>
73  messageReg.setMessageType(m_registerMessageType(T::identifier()));
74  }
75 
76  RawSenderType getSender();
77 
80  OSVR_COMMON_EXPORT void update();
81 
84  OSVR_COMMON_EXPORT void sendPending();
85 
86  template <typename T, typename ClassOfService>
87  void packMessage(
88  Buffer<T> const &buf, RawMessageType const &msgType,
89  util::time::TimeValue const &timestamp,
91 
92  template <typename T>
93  void packMessage(Buffer<T> const &buf, RawMessageType const &msgType,
94  util::time::TimeValue const &timestamp);
95 
96  template <typename T, typename ClassOfService>
97  void packMessage(
98  Buffer<T> const &buf, RawMessageType const &msgType,
100 
101  template <typename T>
102  void packMessage(Buffer<T> const &buf, RawMessageType const &msgType);
103 
104  std::string const &getDeviceName() const;
105 
106  protected:
108  OSVR_COMMON_EXPORT BaseDevice();
110  OSVR_COMMON_EXPORT void m_setup(vrpn_ConnectionPtr conn,
111  RawSenderType sender,
112  std::string const &name);
114  vrpn_ConnectionPtr m_getConnection() const;
117  virtual void m_update() = 0;
118 
119  private:
122  RawMessageType m_registerMessageType(const char *identifier);
123 
124  OSVR_COMMON_EXPORT void m_addComponent(DeviceComponentPtr component);
125 
126  void m_packMessage(size_t len, const char *buf,
127  RawMessageType const &msgType,
128  util::time::TimeValue const &timestamp,
129  uint32_t classOfService);
130  DeviceComponentList m_components;
131  vrpn_ConnectionPtr m_conn;
132  RawSenderType m_sender;
133  std::string m_name;
134  };
135 
136  template <typename T, typename ClassOfService>
137  inline void BaseDevice::packMessage(
138  Buffer<T> const &buf, RawMessageType const &msgType,
139  util::time::TimeValue const &timestamp,
141  m_packMessage(
142  buf.size(), buf.data(), msgType, timestamp,
144  }
145  template <typename T>
146  inline void
147  BaseDevice::packMessage(Buffer<T> const &buf, RawMessageType const &msgType,
148  util::time::TimeValue const &timestamp) {
149  packMessage(buf, msgType, timestamp, class_of_service::Reliable());
150  }
151 
152  template <typename T, typename ClassOfService>
153  inline void BaseDevice::packMessage(
154  Buffer<T> const &buf, RawMessageType const &msgType,
156  classOfService) {
159  packMessage(buf, msgType, t, classOfService);
160  }
161 
162  template <typename T>
163  inline void BaseDevice::packMessage(Buffer<T> const &buf,
164  RawMessageType const &msgType) {
165  packMessage(buf, msgType, class_of_service::Reliable());
166  }
167 } // namespace common
168 } // namespace osvr
169 
170 #endif // INCLUDED_BaseDevice_h_GUID_52C0B784_DB20_4FA6_0C87_47FB116DA61B
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
Definition: NetworkClassOfService.h:134
Class used as an interface for underlying devices that can have device components (corresponding to i...
Definition: BaseDevice.h:51
void getNow(TimeValue &tv)
Set the given TimeValue to the current time.
Definition: TimeValue.h:51
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
T * addComponent(shared_ptr< T > component)
Adds a component to a base device.
Definition: BaseDevice.h:58
CRTP base for classes of service, useful for accepting classes of service for an argument without let...
Definition: NetworkClassOfService.h:81
size_t size() const
Gets the current size, in bytes.
Definition: Buffer.h:282
CRTP class template wrapping message-specific data and/or logic.
Definition: MessageRegistration.h:44
virtual void m_update()=0
Implementation-specific update (call client_mainloop() or server_mainloop() in it!) ...
OSVR_COMMON_EXPORT BaseDevice()
Constructor.
Definition: BaseDevice.cpp:41
A buffer of bytes, built on a byte-vector-like container.
Definition: Buffer.h:206
OSVR_COMMON_EXPORT void sendPending()
Called from a component to send pending messages instead of waiting for next time.
Definition: BaseDevice.cpp:86
OSVR_COMMON_EXPORT void m_setup(vrpn_ConnectionPtr conn, RawSenderType sender, std::string const &name)
Should be called by derived class to set the connection, etc.
Definition: BaseDevice.cpp:106
Association of class of service types with integral constants.
Definition: NetworkClassOfService.h:95
OSVR_COMMON_EXPORT void update()
Called from the outside to run the mainloop on the device and its components.
Definition: BaseDevice.cpp:79
vrpn_ConnectionPtr m_getConnection() const
Accessor for underlying connection.
Definition: BaseDevice.cpp:113
Header providing a C++ wrapper around TimeValueC.h.
Type-safe wrapper with built-in default for a VRPN "message type" integer.
Definition: RawMessageType.h:45
virtual OSVR_COMMON_EXPORT ~BaseDevice()
Virtual destructor.
Definition: BaseDevice.cpp:42
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
Header defining buffer types, with optional alignment dependent on Boost version. ...
void registerMessageType(MessageRegistration< T > &messageReg)
Call with a MessageRegistration object, and the message type will be registered and stored in the typ...
Definition: BaseDevice.h:72
ElementType const * data() const
Provides access to the underlying data.
Definition: Buffer.h:288
Type-safe wrapper with built-in default for a VRPN "sender type" integer.
Definition: RawSenderType.h:45