vrpn  07.33
Virtual Reality Peripheral Network
vrpn_MainloopContainer.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 // *** WARNING: This file must only be included in a .cpp or .C file, not
20 // in a .h file, because it includes <vector>, and we can't have any standard
21 // library files in the VRPN headers because that will make VRPN incompatible
22 // with libraries that use the other flavor of <vector.h>.
23 
24 #pragma once
25 
26 // Internal Includes
27 #include "vrpn_MainloopObject.h"
28 
29 // Library/third-party includes
30 // - none
31 
32 // Standard includes
33 #include <vector>
34 
37 public:
42 
45  void clear();
46 
51 
55  template <class T> T add(T o)
56  {
57  if (!add(vrpn_MainloopObject::wrap(o))) {
58  return NULL;
59  }
60  return o;
61  }
62 
65  void mainloop();
66 
67 private:
68  std::vector<vrpn_MainloopObject *> _vrpn;
69 };
70 
71 /* -- inline implementations -- */
72 
74 
76 {
77  // If the object is NULL, we have a problem and don't add it.
78  if (!o) {
79  return o;
80  }
81 
82  // If the object is broken(), this indicates a problem
83  // with the device. We also do not add it and we return NULL to show.
84  // that there is a problem.
85  if (o->broken()) {
86  fprintf(
87  stderr,
88  "vrpn_MainloopContainer::add() Device is broken, not adding.\n");
89  return NULL;
90  }
91 
92  _vrpn.push_back(o);
93  return o;
94 }
95 
97 {
98  if (_vrpn.empty()) {
99  return;
100  }
102  for (int i = int(_vrpn.size()) - 1; i >= 0; --i) {
103  delete _vrpn[i];
104  _vrpn[i] = NULL;
105  }
106  _vrpn.clear();
107 }
108 
110 {
111  const size_t n = _vrpn.size();
112  for (size_t i = 0; i < n; ++i) {
113  _vrpn[i]->mainloop();
114  }
115 }
A container that holds and owns one or more VRPN objects,.
void mainloop()
Runs mainloop on all contained objects, in the order that they were added.
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.
vrpn_MainloopContainer()
Constructor.
T add(T o)
Template method to automatically wrap objects with vrpn_MainloopObject before adding them...
void clear()
Clear internal structure holding objects, deleting them in reverse order of their addition...
vrpn_MainloopObject * add(vrpn_MainloopObject *o)
Add an object wrapped by vrpn_MainloopObject. Return NULL if the object has a problem (indicated by b...
An interface for all VRPN objects that have a "mainloop" method. Not instantiated directly: use vrpn_...
~vrpn_MainloopContainer()
Destructor: invokes clear()