Main MRPT website > C++ reference for MRPT 1.9.9
xsens_janitors.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #ifndef _JANITORS_H_2006_05_01
11 #define _JANITORS_H_2006_05_01
12 
13 // required for gnu c++ compiler due to difference in attribute declarations
14 #if defined(__GNUC__) && !defined(__cdecl)
15 #define __cdecl // __attribute__((cdecl)) // JLBC @ MRPT: GCC warns about
16 // ignored attribute
17 #define __stdcall // __attribute__((stdcall)) // JLBC @ MRPT: GCC warns about
18 // ignored attribute
19 #endif
20 
21 namespace xsens
22 {
23 //////////////////////////////////////////////////////////////////////////////////////////
24 /*! \brief Value restoring janitor class
25 
26  This class can be used to make sure that the value that is in the variable
27  at the time
28  the janitor is created will be in it again when the janitor leaves scope.
29 */
30 template <class T>
32 {
33  private:
36  bool m_enabled;
37 
38  public:
39  JanitorRestore<T>(T& control, bool enabl = true)
40  : m_control(control), m_value(control), m_enabled(enabl)
41  {
42  }
44  {
46  }
47 
48  void disable(void) { m_enabled = false; }
49  void enable(void) { m_enabled = true; }
50 };
51 
52 //////////////////////////////////////////////////////////////////////////////////////////
53 /*! \brief Memory releasing janitor class
54 
55  This class can be used to make sure that the associated pointer is freed
56  when the
57  janitor leaves scope.
58 */
59 template <class T>
61 {
62  private:
64  bool m_enabled;
65 
66  public:
67  JanitorFree<T>(T* control, bool enabl = true)
68  : m_control(control), m_enabled(enabl)
69  {
70  }
72  {
73  if (m_enabled) free(m_control);
74  }
75 
76  void disable(void) { m_enabled = false; }
77  void enable(void) { m_enabled = true; }
78 };
79 
80 //////////////////////////////////////////////////////////////////////////////////////////
81 /*! \brief Memory releasing janitor class
82 
83  This class can be used to make sure that the associated object is deleted
84  when the
85  janitor leaves scope.
86 */
87 template <class T>
89 {
90  private:
92  bool m_enabled;
93 
94  public:
95  JanitorDelete<T>(T* control, bool enabl = true)
96  : m_control(control), m_enabled(enabl)
97  {
98  }
100  {
101  if (m_enabled) delete m_control;
102  }
103 
104  void disable(void) { m_enabled = false; }
105  void enable(void) { m_enabled = true; }
106 };
107 
108 //////////////////////////////////////////////////////////////////////////////////////////
109 /*! \brief Memory releasing janitor class
110 
111  This class can be used to make sure that the associated object is deleted
112  when the
113  janitor leaves scope.
114 */
115 template <class T>
117 {
118  private:
120  bool m_enabled;
121 
122  public:
123  JanitorDeleteArray<T>(T* control, bool enabl = true)
124  : m_control(control), m_enabled(enabl)
125  {
126  }
128  {
129  if (m_enabled) delete[] m_control;
130  }
131 
132  void disable(void) { m_enabled = false; }
133  void enable(void) { m_enabled = true; }
134 };
135 
136 //////////////////////////////////////////////////////////////////////////////////////////
137 /*! \brief Class function calling janitor class
138 
139  This class can be used to make sure that the given class function is called
140  when the
141  janitor leaves scope.
142 */
143 template <class T, typename R = void>
145 {
146  public:
148 
149  private:
152  bool m_enabled;
153 
154  public:
156  T& control, t_func_JanitorClasssFunc func, bool enabl = true)
157  : m_control(control), m_funcJCF(func), m_enabled(enabl)
158  {
159  }
161  {
162  if (m_enabled) (m_control.*m_funcJCF)();
163  }
164 
165  void disable(void) { m_enabled = false; }
166  void enable(void) { m_enabled = true; }
167 };
168 
169 //////////////////////////////////////////////////////////////////////////////////////////
170 /*! \brief Function calling janitor class
171 
172  This class can be used to make sure that the given function is called on the
173  given
174  object when the janitor leaves scope. Take care that the object is not of a
175  type that
176  is destroyed before the function unrolling begins.
177 */
178 template <class T, typename R = void>
180 {
181  public:
182  typedef R(__cdecl* t_func_JanitorFunc)(T);
183 
184  private:
187  bool m_enabled;
188 
189  public:
190  JanitorFunc<T, R>(t_func_JanitorFunc func, T& control, bool enabl = true)
191  : m_funcJF(func), m_control(control), m_enabled(enabl)
192  {
193  }
195  {
196  if (m_enabled) (*m_funcJF)(m_control);
197  }
198 
199  void disable(void) { m_enabled = false; }
200  void enable(void) { m_enabled = true; }
201 };
202 
203 //////////////////////////////////////////////////////////////////////////////////////////
204 /*! \brief Log / printf-like function calling janitor class
205 
206  This class can be used to make sure that the given printf-like function is
207  called with the
208  supplied parameter when the janitor leaves scope. Take care that the object
209  is not of a type that
210  is destroyed before the function unrolling begins.
211 */
212 template <class T, class C, typename R = void>
214 {
215  public:
216  typedef R(__cdecl* t_func_JanitorLogFunc)(const char*, ...);
217 
218  private:
219  const char* m_str;
222  bool m_enabled;
223 
224  public:
226  t_func_JanitorLogFunc func, const char* str, T& control,
227  bool enable = true)
228  : m_funcJF(func), m_str(str), m_control(control), m_enabled(enable)
229  {
230  }
232  {
233  if (m_enabled) (*m_funcJF)(m_str, (C)m_control);
234  }
235 
236  void disable(void) { m_enabled = false; }
237  void enable(void) { m_enabled = true; }
238 };
239 
240 //////////////////////////////////////////////////////////////////////////////////////////
241 /*! \brief Function calling janitor class
242 
243  This class can be used to make sure that the given function is called on the
244  given
245  object when the janitor leaves scope. Take care that the object is not of a
246  type that
247  is destroyed before the function unrolling begins.
248 */
249 template <class T, typename R = void>
251 {
252  public:
253  typedef R(__stdcall* t_func_JanitorFuncStdCall)(T);
254 
255  private:
258  bool m_enabled;
259 
260  public:
262  t_func_JanitorFuncStdCall func, T& control, bool enabl = true)
263  : m_funcJFSC(func), m_control(control), m_enabled(enabl)
264  {
265  }
267  {
268  if (m_enabled) (*m_funcJFSC)(m_control);
269  }
270 
271  void disable(void) { m_enabled = false; }
272  void enable(void) { m_enabled = true; }
273 };
274 
275 //////////////////////////////////////////////////////////////////////////////////////////
276 /*! \brief Value restoring janitor class
277 
278  This class can be used to make sure that the value that is in the variable
279  at the time
280  the janitor is created will be in it again when the janitor leaves scope.
281 */
282 template <class T>
284 {
285  private:
288  bool m_enabled;
289 
290  public:
291  JanitorSet<T>(T& control, const T& val, bool enabl = true)
292  : m_control(control), m_value(val), m_enabled(enabl)
293  {
294  }
296  {
297  if (m_enabled) m_control = m_value;
298  }
299 
300  void disable(void) { m_enabled = false; }
301  void enable(void) { m_enabled = true; }
302 };
303 
304 } // end of xsens namespace
305 
306 #endif // _JANITORS_H_2006_05_01
R(T::* t_func_JanitorClasssFunc)(void)
R(__stdcall * t_func_JanitorFuncStdCall)(T)
R(__cdecl * t_func_JanitorFunc)(T)
R(__cdecl * t_func_JanitorLogFunc)(const char *,...)
t_func_JanitorClasssFunc m_funcJCF
Memory releasing janitor class.
Value restoring janitor class.
t_func_JanitorFuncStdCall m_funcJFSC
Memory releasing janitor class.
int val
Definition: mrpt_jpeglib.h:955
Class function calling janitor class.
const float R
Memory releasing janitor class.
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:95
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
Function calling janitor class.
Value restoring janitor class.
t_func_JanitorFunc m_funcJF
Log / printf-like function calling janitor class.
t_func_JanitorLogFunc m_funcJF
Function calling janitor class.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019