Main MRPT website > C++ reference for MRPT 1.5.7
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 ignored attribute
16 # define __stdcall // __attribute__((stdcall)) // JLBC @ MRPT: GCC warns about ignored attribute
17 #endif
18 
19 namespace xsens {
20 
21 //////////////////////////////////////////////////////////////////////////////////////////
22 /*! \brief Value restoring janitor class
23 
24  This class can be used to make sure that the value that is in the variable at the time
25  the janitor is created will be in it again when the janitor leaves scope.
26 */
27 template <class T>
29 private:
32  bool m_enabled;
33 public:
34  JanitorRestore<T>(T& control, bool enabl = true) :
35  m_control(control), m_value(control), m_enabled(enabl) {}
37  {
38  if (m_enabled)
40  }
41 
42  void disable(void)
43  { m_enabled = false; }
44 
45  void enable(void)
46  { m_enabled = true; }
47 };
48 
49 //////////////////////////////////////////////////////////////////////////////////////////
50 /*! \brief Memory releasing janitor class
51 
52  This class can be used to make sure that the associated pointer is freed when the
53  janitor leaves scope.
54 */
55 template <class T>
56 class JanitorFree {
57 private:
59  bool m_enabled;
60 public:
61  JanitorFree<T>(T* control, bool enabl = true) :
62  m_control(control), m_enabled(enabl) {}
64  {
65  if (m_enabled)
66  free(m_control);
67  }
68 
69  void disable(void)
70  { m_enabled = false; }
71 
72  void enable(void)
73  { m_enabled = true; }
74 };
75 
76 //////////////////////////////////////////////////////////////////////////////////////////
77 /*! \brief Memory releasing janitor class
78 
79  This class can be used to make sure that the associated object is deleted when the
80  janitor leaves scope.
81 */
82 template <class T>
84 private:
86  bool m_enabled;
87 public:
88  JanitorDelete<T>(T* control, bool enabl = true) :
89  m_control(control), m_enabled(enabl) {}
91  {
92  if (m_enabled)
93  delete m_control;
94  }
95 
96  void disable(void)
97  { m_enabled = false; }
98 
99  void enable(void)
100  { m_enabled = true; }
101 };
102 
103 //////////////////////////////////////////////////////////////////////////////////////////
104 /*! \brief Memory releasing janitor class
105 
106  This class can be used to make sure that the associated object is deleted when the
107  janitor leaves scope.
108 */
109 template <class T>
111 private:
113  bool m_enabled;
114 public:
115  JanitorDeleteArray<T>(T* control, bool enabl = true) :
116  m_control(control), m_enabled(enabl) {}
118  {
119  if (m_enabled)
120  delete[] m_control;
121  }
122 
123  void disable(void)
124  { m_enabled = false; }
125 
126  void enable(void)
127  { m_enabled = true; }
128 };
129 
130 //////////////////////////////////////////////////////////////////////////////////////////
131 /*! \brief Class function calling janitor class
132 
133  This class can be used to make sure that the given class function is called when the
134  janitor leaves scope.
135 */
136 template <class T, typename R = void>
138 public:
140 private:
143  bool m_enabled;
144 public:
145 
146  JanitorClassFunc<T,R>(T& control, t_func_JanitorClasssFunc func, bool enabl = true) :
147  m_control(control), m_funcJCF(func), m_enabled(enabl)
148  {
149  }
151  {
152  if (m_enabled)
153  (m_control.*m_funcJCF)();
154  }
155 
156  void disable(void)
157  { m_enabled = false; }
158 
159  void enable(void)
160  { m_enabled = true; }
161 };
162 
163 //////////////////////////////////////////////////////////////////////////////////////////
164 /*! \brief Function calling janitor class
165 
166  This class can be used to make sure that the given function is called on the given
167  object when the janitor leaves scope. Take care that the object is not of a type that
168  is destroyed before the function unrolling begins.
169 */
170 template <class T, typename R = void>
171 class JanitorFunc {
172 public:
173  typedef R (__cdecl * t_func_JanitorFunc)(T);
174 private:
177  bool m_enabled;
178 public:
179 
180  JanitorFunc<T,R>(t_func_JanitorFunc func, T& control, bool enabl = true) :
181  m_funcJF(func), m_control(control), m_enabled(enabl) {}
183  {
184  if (m_enabled)
185  (*m_funcJF)(m_control);
186  }
187 
188  void disable(void)
189  { m_enabled = false; }
190 
191  void enable(void)
192  { m_enabled = true; }
193 };
194 
195 //////////////////////////////////////////////////////////////////////////////////////////
196 /*! \brief Log / printf-like function calling janitor class
197 
198  This class can be used to make sure that the given printf-like function is called with the
199  supplied parameter when the janitor leaves scope. Take care that the object is not of a type that
200  is destroyed before the function unrolling begins.
201 */
202 template <class T, class C, typename R = void>
204 public:
205  typedef R (__cdecl * t_func_JanitorLogFunc)(const char*,...);
206 private:
207  const char* m_str;
210  bool m_enabled;
211 public:
212 
213  JanitorLogFunc<T,C,R>(t_func_JanitorLogFunc func, const char* str, T& control, bool enable = true) :
214  m_funcJF(func), m_str(str), m_control(control), m_enabled(enable) {}
216  {
217  if (m_enabled)
218  (*m_funcJF)(m_str,(C) m_control);
219  }
220 
221  void disable(void)
222  { m_enabled = false; }
223 
224  void enable(void)
225  { m_enabled = true; }
226 };
227 
228 //////////////////////////////////////////////////////////////////////////////////////////
229 /*! \brief Function calling janitor class
230 
231  This class can be used to make sure that the given function is called on the given
232  object when the janitor leaves scope. Take care that the object is not of a type that
233  is destroyed before the function unrolling begins.
234 */
235 template <class T, typename R = void>
237 public:
238  typedef R (__stdcall * t_func_JanitorFuncStdCall)(T);
239 private:
242  bool m_enabled;
243 public:
244 
245  JanitorFuncStdCall<T,R>(t_func_JanitorFuncStdCall func, T& control, bool enabl = true) :
246  m_funcJFSC(func), m_control(control), m_enabled(enabl) {}
248  {
249  if (m_enabled)
250  (*m_funcJFSC)(m_control);
251  }
252 
253  void disable(void)
254  { m_enabled = false; }
255 
256  void enable(void)
257  { m_enabled = true; }
258 };
259 
260 //////////////////////////////////////////////////////////////////////////////////////////
261 /*! \brief Value restoring janitor class
262 
263  This class can be used to make sure that the value that is in the variable at the time
264  the janitor is created will be in it again when the janitor leaves scope.
265 */
266 template <class T>
267 class JanitorSet {
268 private:
271  bool m_enabled;
272 public:
273  JanitorSet<T>(T& control, const T& val, bool enabl = true) :
274  m_control(control), m_value(val), m_enabled(enabl) {}
276  {
277  if (m_enabled)
278  m_control = m_value;
279  }
280 
281  void disable(void)
282  { m_enabled = false; }
283 
284  void enable(void)
285  { m_enabled = true; }
286 };
287 
288 } // end of xsens namespace
289 
290 #endif // _JANITORS_H_2006_05_01
R(T::* t_func_JanitorClasssFunc)(void)
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:953
Class function calling janitor class.
R(__stdcall * t_func_JanitorFuncStdCall)(T)
const float R
R(__cdecl * t_func_JanitorFunc)(T)
Memory releasing janitor class.
R(__cdecl * t_func_JanitorLogFunc)(const char *,...)
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:92
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.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019