RflySimSDK v3.08
RflySimSDK说明文档
载入中...
搜索中...
未找到
vrpn_MainloopObject.h
浏览该文件的文档.
1/** @file
2 @brief Header
3
4 @date 2011
5
6 @author
7 Ryan Pavlik
8 <rpavlik@iastate.edu> and <abiryan@ryand.net>
9 http://academic.cleardefinition.com/
10 Iowa State University Virtual Reality Applications Center
11 Human-Computer Interaction Graduate Program
12*/
13
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
39class VRPN_API vrpn_Connection;
40
41/// An interface for all VRPN objects that have a "mainloop" method.
42/// Not instantiated directly: use vrpn_MainloopObject::wrap() to create one
44public:
45 /// Exception thrown when trying to wrap a NULL pointer.
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
54 /// Destructor
56
57 /// The mainloop function: the primary thing we look for in a VRPN object
58 virtual void mainloop() = 0;
59
60 /// Checks the connectionPtr() for the VRPN object to make sure it is not
61 /// NULL.
62 virtual bool broken() = 0;
63
64 /// Templated wrapping function
65 template <class T> static vrpn_MainloopObject *wrap(T o);
66
67 /// Templated wrapping function that can encourage the
68 /// wrapper to not destroy the wrapped object at destruction
69 template <class T> static vrpn_MainloopObject *wrap(T o, bool owner);
70
71protected:
72 /// Internal function to return a typeless pointer of the contained
73 /// object, for comparison purposes.
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
82/// @name Comparison operators
83/// @relates vrpn_MainloopObject
84/// @{
85inline bool operator==(vrpn_MainloopObject const &lhs,
86 vrpn_MainloopObject const &rhs)
87{
88 return lhs._returnContained() == rhs._returnContained();
89}
90
91inline bool operator!=(vrpn_MainloopObject const &lhs,
92 vrpn_MainloopObject const &rhs)
93{
94 return lhs._returnContained() == rhs._returnContained();
95}
96/// @}
97
98/// Namespace enclosing internal implementation details
99namespace detail {
100 template <class T> class TypedMainloopObject;
101
102 /// Template class for holding generic VRPN objects with
103 /// type information.
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 }
117 virtual ~TypedMainloopObject()
118 {
119 if (_do_delete) {
120 try {
121 delete _instance;
122 } catch (...) {
123 fprintf(stderr, "TypedMainloopObject::~TypedMainloopObject: delete failed\n");
124 return;
125 }
126 VRPN_MAINLOOPOBJECT_MSG("Deleted contained vrpn object "
127 << _instance)
128 }
129 else {
130 VRPN_MAINLOOPOBJECT_MSG("NOT deleting contained vrpn object "
131 << _instance)
132 }
133 }
134
135 virtual void mainloop() { _instance->mainloop(); }
136
137 virtual bool broken() { return (_instance->connectionPtr() == NULL); }
138
139 protected:
140 virtual void *_returnContained() const { return _instance; }
141 T *_instance;
142 bool _do_delete;
143 };
144
145 /// Specialization for connections, since they're reference-counted.
146 template <>
148 public:
150 : _instance(o)
151 {
152 if (!o) {
153 throw vrpn_MainloopObject::
154 CannotWrapNullPointerIntoMainloopObject();
155 }
156 VRPN_MAINLOOPOBJECT_MSG("Wrapping vrpn connection " << o)
157 }
158 virtual ~TypedMainloopObject()
159 {
160 VRPN_MAINLOOPOBJECT_MSG("Unreferencing contained vrpn connection "
161 << _instance)
162 _instance->removeReference();
163 }
164
165 virtual void mainloop() { _instance->mainloop(); }
166
167 virtual bool broken() { return (!_instance->doing_okay()); }
168
169 protected:
170 virtual void *_returnContained() const { return _instance; }
171 vrpn_Connection *_instance;
172 };
173
174} // end of namespace detail
175
176template <class T> inline vrpn_MainloopObject *vrpn_MainloopObject::wrap(T o)
177{
178 vrpn_MainloopObject *ret = NULL;
179 try {
181 } catch (...) {}
182 return ret;
183}
184
185template <class T>
187{
188 vrpn_MainloopObject *ret = NULL;
189 try {
190 ret = new detail::TypedMainloopObject<T>(o, owner);
191 } catch (...) { }
192 return ret;
193}
194
195#endif // INCLUDED_vrpn_MainloopObject_h_GUID_38f638e4_40e0_4c6d_bebc_21c463794b88
virtual void mainloop()
The mainloop function: the primary thing we look for in a VRPN object
定义 vrpn_MainloopObject.h:135
virtual bool broken()
定义 vrpn_MainloopObject.h:137
virtual void * _returnContained() const
定义 vrpn_MainloopObject.h:140
virtual void mainloop()
The mainloop function: the primary thing we look for in a VRPN object
定义 vrpn_MainloopObject.h:165
virtual bool broken()
定义 vrpn_MainloopObject.h:167
virtual void * _returnContained() const
定义 vrpn_MainloopObject.h:170
定义 vrpn_MainloopObject.h:100
Generic connection class not specific to the transport mechanism.
定义 vrpn_Connection.h:562
定义 vrpn_MainloopObject.h:43
virtual void * _returnContained() const =0
virtual ~vrpn_MainloopObject()
Destructor
定义 vrpn_MainloopObject.h:55
virtual void mainloop()=0
The mainloop function: the primary thing we look for in a VRPN object
static vrpn_MainloopObject * wrap(T o)
Templated wrapping function
定义 vrpn_MainloopObject.h:176
virtual bool broken()=0
Namespace enclosing internal implementation details
定义 vrpn_ConnectionPtr.h:225
Exception thrown when trying to wrap a NULL pointer.
定义 vrpn_MainloopObject.h:46