vrpn  07.33
Virtual Reality Peripheral Network
vrpn_MainloopObject.h
Go to the documentation of this file.
1 
14 // Copyright Iowa State University 2011.
15 // Distributed under the Boost Software License, Version 1.0.
16 // (See accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
18 
19 #ifndef INCLUDED_vrpn_MainloopObject_h_GUID_38f638e4_40e0_4c6d_bebc_21c463794b88
20 #define INCLUDED_vrpn_MainloopObject_h_GUID_38f638e4_40e0_4c6d_bebc_21c463794b88
21 
22 // Internal Includes
23 #include "vrpn_Connection.h"
24 
25 // Library/third-party includes
26 // - none
27 
28 // Standard includes
29 #include <stdexcept>
30 
31 #ifdef VRPN_MAINLOOPOBJECT_VERBOSE
32 #include <iostream>
33 #define VRPN_MAINLOOPOBJECT_MSG(_x) \
34  std::cout << __FILE__ << ":" << __LINE__ << ": " << _x << std::endl;
35 #else
36 #define VRPN_MAINLOOPOBJECT_MSG(_x)
37 #endif
38 
40 
44 public:
46  struct CannotWrapNullPointerIntoMainloopObject : public std::logic_error {
48  : std::logic_error(
49  "Cannot wrap a null pointer into a vrpn_MainloopObject!")
50  {
51  }
52  };
53 
55  virtual ~vrpn_MainloopObject() {}
56 
58  virtual void mainloop() = 0;
59 
62  virtual bool broken() = 0;
63 
65  template <class T> static vrpn_MainloopObject *wrap(T o);
66 
69  template <class T> static vrpn_MainloopObject *wrap(T o, bool owner);
70 
71 protected:
74  virtual void *_returnContained() const = 0;
76  friend bool operator==(vrpn_MainloopObject const &lhs,
77  vrpn_MainloopObject const &rhs);
78  friend bool operator!=(vrpn_MainloopObject const &lhs,
79  vrpn_MainloopObject const &rhs);
80 };
81 
85 inline bool operator==(vrpn_MainloopObject const &lhs,
86  vrpn_MainloopObject const &rhs)
87 {
88  return lhs._returnContained() == rhs._returnContained();
89 }
90 
91 inline bool operator!=(vrpn_MainloopObject const &lhs,
92  vrpn_MainloopObject const &rhs)
93 {
94  return lhs._returnContained() == rhs._returnContained();
95 }
97 
99 namespace detail {
100  template <class T> class TypedMainloopObject;
101 
104  template <class T>
106  public:
107  TypedMainloopObject(T *o, bool do_delete = true)
108  : _instance(o)
109  , _do_delete(do_delete)
110  {
111  if (!o) {
112  throw vrpn_MainloopObject::
113  CannotWrapNullPointerIntoMainloopObject();
114  }
115  VRPN_MAINLOOPOBJECT_MSG("Wrapping vrpn object " << o)
116  }
118  {
119  if (_do_delete) {
120  delete _instance;
121  VRPN_MAINLOOPOBJECT_MSG("Deleted contained vrpn object "
122  << _instance)
123  }
124  else {
125  VRPN_MAINLOOPOBJECT_MSG("NOT deleting contained vrpn object "
126  << _instance)
127  }
128  }
129 
130  virtual void mainloop() { _instance->mainloop(); }
131 
132  virtual bool broken() { return (_instance->connectionPtr() == NULL); }
133 
134  protected:
135  virtual void *_returnContained() const { return _instance; }
138  };
139 
141  template <>
143  public:
145  : _instance(o)
146  {
147  if (!o) {
148  throw vrpn_MainloopObject::
149  CannotWrapNullPointerIntoMainloopObject();
150  }
151  VRPN_MAINLOOPOBJECT_MSG("Wrapping vrpn connection " << o)
152  }
154  {
155  VRPN_MAINLOOPOBJECT_MSG("Unreferencing contained vrpn connection "
156  << _instance)
157  _instance->removeReference();
158  }
159 
160  virtual void mainloop() { _instance->mainloop(); }
161 
162  virtual bool broken() { return (!_instance->doing_okay()); }
163 
164  protected:
165  virtual void *_returnContained() const { return _instance; }
167  };
168 
169 } // end of namespace detail
170 
171 template <class T> inline vrpn_MainloopObject *vrpn_MainloopObject::wrap(T o)
172 {
173  return new detail::TypedMainloopObject<T>(o);
174 }
175 
176 template <class T>
178 {
179  return new detail::TypedMainloopObject<T>(o, owner);
180 }
181 
182 #endif // INCLUDED_vrpn_MainloopObject_h_GUID_38f638e4_40e0_4c6d_bebc_21c463794b88
bool operator!=(const vrpn_ConnectionPtr &lhs, const vrpn_ConnectionPtr &rhs)
Inequality operator for connection smart pointers.
virtual void * _returnContained() const
Internal function to return a typeless pointer of the contained object, for comparison purposes...
STL namespace.
virtual bool broken()=0
Checks the connectionPtr() for the VRPN object to make sure it is not NULL.
static vrpn_MainloopObject * wrap(T o)
Templated wrapping function.
virtual bool broken()
Checks the connectionPtr() for the VRPN object to make sure it is not NULL.
Generic connection class not specific to the transport mechanism.
#define VRPN_API
Namespace enclosing internal implementation details.
Exception thrown when trying to wrap a NULL pointer.
virtual void mainloop()
The mainloop function: the primary thing we look for in a VRPN object.
virtual ~vrpn_MainloopObject()
Destructor.
virtual void mainloop()=0
The mainloop function: the primary thing we look for in a VRPN object.
bool operator==(const vrpn_ConnectionPtr &lhs, const vrpn_ConnectionPtr &rhs)
Equality operator for connection smart pointers.
An interface for all VRPN objects that have a "mainloop" method. Not instantiated directly: use vrpn_...
friend bool operator!=(vrpn_MainloopObject const &lhs, vrpn_MainloopObject const &rhs)
virtual void * _returnContained() const =0
Internal function to return a typeless pointer of the contained object, for comparison purposes...
virtual void mainloop()
The mainloop function: the primary thing we look for in a VRPN object.
friend bool operator==(vrpn_MainloopObject const &lhs, vrpn_MainloopObject const &rhs)
virtual bool broken()
Checks the connectionPtr() for the VRPN object to make sure it is not NULL.
#define VRPN_MAINLOOPOBJECT_MSG(_x)
TypedMainloopObject(T *o, bool do_delete=true)
virtual void * _returnContained() const
Internal function to return a typeless pointer of the contained object, for comparison purposes...