vrpn  07.33
Virtual Reality Peripheral Network
vrpn_ConnectionPtr.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_ConnectionPtr_h_GUID_52044DCC_1409_4F8B_FC18_0F80285ABDBE
20 #define INCLUDED_vrpn_ConnectionPtr_h_GUID_52044DCC_1409_4F8B_FC18_0F80285ABDBE
21 
22 // Internal Includes
23 // - none
24 
25 // Library/third-party includes
26 #include <vrpn_Connection.h>
27 #include <vrpn_MainloopObject.h>
28 
29 // Standard includes
30 // - none
31 
35 public:
37  explicit vrpn_ConnectionPtr(vrpn_Connection* c = NULL)
38  : _p(c)
39  {
40  if (_p) {
41  _p->addReference();
42  }
43  }
44 
47  : _p(other._p)
48  {
49  if (_p) {
50  _p->addReference();
51  }
52  }
53 
56  {
57  if (this == &other || _p == other._p) {
59  return *this;
60  }
61 
62  reset();
63  if (other._p) {
64  _p = other._p;
65  _p->addReference();
66  }
67  return *this;
68  }
69 
72  {
73  if (_p == other) {
75  return *this;
76  }
77 
78  reset();
79  if (other) {
80  _p = other;
81  _p->addReference();
82  }
83  return *this;
84  }
85 
88 
91  void reset()
92  {
93  if (_p) {
94  _p->removeReference();
95  }
96  _p = NULL;
97  }
98 
103  vrpn_Connection* get() const { return _p; }
104 
107  vrpn_Connection& operator*() { return *_p; }
108 
109  vrpn_Connection const& operator*() const { return *_p; }
110 
111  vrpn_Connection* operator->() { return _p; }
112 
113  vrpn_Connection const* operator->() const { return _p; }
115 
116  bool operator!() const { return !_p; }
117 
121  operator unspecified_bool_type() const
122  {
123  return (this->operator!()) ? &vrpn_ConnectionPtr::_p : NULL;
124  }
126 
133  static vrpn_ConnectionPtr
135  const char* local_in_logfile_name = NULL,
136  const char* local_out_logfile_name = NULL,
137  const char* NIC_NAME = NULL)
138  {
139  return vrpn_ConnectionPtr(
140  vrpn_create_server_connection(port, local_in_logfile_name,
141  local_out_logfile_name, NIC_NAME),
142  false);
143  }
144 
145  static vrpn_ConnectionPtr
146  create_server_connection(const char* cname,
147  const char* local_in_logfile_name = NULL,
148  const char* local_out_logfile_name = NULL)
149  {
150  return vrpn_ConnectionPtr(
151  vrpn_create_server_connection(cname, local_in_logfile_name,
152  local_out_logfile_name),
153  false);
154  }
156 
157 private:
161  vrpn_ConnectionPtr(vrpn_Connection* c, bool needsAddRef)
162  : _p(c)
163  {
164  if (_p && needsAddRef) {
165  _p->addReference();
166  }
167  }
168 
170  vrpn_Connection* _p;
171 
173  void this_type_does_not_support_comparisons() const {}
174 };
175 
178 inline bool operator==(const vrpn_ConnectionPtr& lhs,
179  const vrpn_ConnectionPtr& rhs)
180 {
181  return lhs.get() == rhs.get();
182 }
183 
186 inline bool operator!=(const vrpn_ConnectionPtr& lhs,
187  const vrpn_ConnectionPtr& rhs)
188 {
189  return lhs.get() != rhs.get();
190 }
191 
192 #if 0
193 template <typename T>
199 bool operator!=(const T& lhs, const vrpn_ConnectionPtr& rhs) {
200  rhs.this_type_does_not_support_comparisons();
201  return false;
202 }
203 
204 template <typename T>
205 bool operator==(const T& lhs, const vrpn_ConnectionPtr& rhs) {
206  rhs.this_type_does_not_support_comparisons();
207  return false;
208 }
209 
210 template <typename T>
211 bool operator!=(const vrpn_ConnectionPtr& lhs, const T& rhs) {
212  lhs.this_type_does_not_support_comparisons();
213  return false;
214 }
215 
216 template <typename T>
217 bool operator==(const vrpn_ConnectionPtr& lhs, const T& rhs) {
218  lhs.this_type_does_not_support_comparisons();
219  return false;
220 }
222 #endif
223 
225 namespace detail {
226  template <class T> class TypedMainloopObject;
227 
230  template <>
232  public:
234  : _instance(o)
235  {
236  if (!o) {
237  throw vrpn_MainloopObject::
238  CannotWrapNullPointerIntoMainloopObject();
239  }
240  }
241 
242  virtual bool broken() { return (!_instance->doing_okay()); }
243 
244  virtual void mainloop() { _instance->mainloop(); }
245 
246  protected:
247  virtual void* _returnContained() const { return _instance.get(); }
248 
249  private:
250  vrpn_ConnectionPtr _instance;
251  };
252 } // end of namespace detail
253 
254 #endif // INCLUDED_vrpn_ConnectionPtr_h_GUID_52044DCC_1409_4F8B_FC18_0F80285ABDBE
bool operator!=(const vrpn_ConnectionPtr &lhs, const vrpn_ConnectionPtr &rhs)
Inequality operator for connection smart pointers.
virtual bool broken()
Checks the connectionPtr() for the VRPN object to make sure it is not NULL.
~vrpn_ConnectionPtr()
Destructor - decrements the contained reference count, if applicable.
void reset()
Clears the contained pointer and decrements the reference count, if applicable.
vrpn_Connection const & operator*() const
vrpn_Connection const * operator->() const
vrpn_ConnectionPtr & operator=(vrpn_Connection *other)
Assignment operator from non-smart pointer.
vrpn_ConnectionPtr(vrpn_Connection *c=NULL)
Explicit constructor from a non-smart connection pointer.
Generic connection class not specific to the transport mechanism.
static vrpn_ConnectionPtr create_server_connection(int port=vrpn_DEFAULT_LISTEN_PORT_NO, const char *local_in_logfile_name=NULL, const char *local_out_logfile_name=NULL, const char *NIC_NAME=NULL)
vrpn_ConnectionPtr & operator=(vrpn_ConnectionPtr const &other)
Assignment operator from smart pointer.
Namespace enclosing internal implementation details.
virtual void mainloop()
The mainloop function: the primary thing we look for in a VRPN object.
vrpn_Connection * operator->()
vrpn_Connection * get() const
Gets the contained "non-smart" pointer. You are responsible for calling vrpn_Connection::addReference...
vrpn_Connection & operator*()
A shared pointer class for holding on to vrpn_Connection instances, using the existing "intrusive ref...
static vrpn_ConnectionPtr create_server_connection(const char *cname, const char *local_in_logfile_name=NULL, const char *local_out_logfile_name=NULL)
vrpn_Connection *vrpn_ConnectionPtr::* unspecified_bool_type
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_...
#define vrpn_DEFAULT_LISTEN_PORT_NO
vrpn_Connection * vrpn_create_server_connection(const char *cname, const char *local_in_logfile_name, const char *local_out_logfile_name)
Create a server connection of arbitrary type (VRPN UDP/TCP, TCP, File, Loopback, MPI).
void addReference()
Counting references to this connection.
vrpn_ConnectionPtr(vrpn_ConnectionPtr const &other)
Copy constructor from smart pointer.
virtual void * _returnContained() const
Internal function to return a typeless pointer of the contained object, for comparison purposes...