Debian Codegraph @

3rdparty/xspublic/xscommon/xsens_janitors.h

raw symbols SCIP
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSENS_JANITORS_H
34 #define XSENS_JANITORS_H
35
36 #ifndef __AVR32__
37 #include <functional>
38 #endif
39
40 // required for gnu c++ compiler versions due to difference in attribute declarations
41 #if defined(__AVR32__)
42 # define __cdecl
43 # define __stdcall
44 #elif defined(_ADI_COMPILER)
45 # define __cdecl
46 # define __stdcall
47 #elif defined(__GNUC__) && !defined(HAVE_CDECL)
48 # if !defined(__cdecl)
49 # if defined(__x86_64__)
50 # define __cdecl
51 # else
52 # define __cdecl __attribute__((cdecl))
53 # endif
54 # endif
55 # if !defined(__stdcall)
56 # if defined(__x86_64__)
57 # define __stdcall
58 # else
59 # define __stdcall __attribute__((stdcall))
60 #endif
61 # endif
62 #endif
63
64 namespace xsens {
65
66 #ifndef NOT_FOR_PUBLIC_RELEASE
67 //////////////////////////////////////////////////////////////////////////////////////////
68 /*! \brief Value restoring janitor class
69 \details This class can be used to make sure that the value that is in the variable at the time
70 the janitor is created will be in it again when the janitor leaves scope.
71 */
72 template <class T>
73 class JanitorRestore {
74 private:
75 T& m_control;
76 T m_value;
77 bool m_enabled;
78 public:
79
80 /*! \brief Constructor
81 */
82 JanitorRestore<T>(T& control, bool enabl = true) :
83 m_control(control), m_value(control), m_enabled(enabl) {}
84
85 /*! \brief Destructor
86 */
87 ~JanitorRestore()
88 {
89 if (m_enabled)
90 m_control = m_value;
91 }
92
93 /*! \brief Disables the value restoring
94 */
95 void disable(void)
96 { m_enabled = false; }
97
98 /*! \brief Enables the value restoring
99 */
100 void enable(void)
101 { m_enabled = true; }
102 };
103
104 //////////////////////////////////////////////////////////////////////////////////////////
105 /*! \brief Memory releasing janitor class
106 \details This class can be used to make sure that the associated pointer is freed when the
107 janitor leaves scope.
108 */
109 template <class T>
110 class JanitorFree {
111 private:
112 T* m_control;
113 bool m_enabled;
114 public:
115
116 /*! \brief Constructor
117 */
118 JanitorFree<T>(T* control, bool enabl = true) :
119 m_control(control), m_enabled(enabl) {}
120
121 /*! \brief Destructor
122 */
123 ~JanitorFree()
124 {
125 if (m_enabled)
126 free(m_control);
127 }
128
129 /*! \brief Disables the memory releasing
130 */
131 void disable(void)
132 { m_enabled = false; }
133
134 /*! \brief Enables the memory releasing
135 */
136 void enable(void)
137 { m_enabled = true; }
138 };
139
140 //////////////////////////////////////////////////////////////////////////////////////////
141 /*! \brief Memory releasing janitor class
142 \details This class can be used to make sure that the associated object is deleted when the
143 janitor leaves scope.
144 */
145 template <class T>
146 class JanitorDelete {
147 private:
148 T* m_control;
149 bool m_enabled;
150 public:
151
152 /*! \brief Default constructor
153 */
154 JanitorDelete<T>() :
155 m_control(NULL),
156 m_enabled(true)
157 {
158
159 }
160
161 /*! \brief Constructor
162 */
163 JanitorDelete<T>(T* control, bool enabl = true) :
164 m_control(control),
165 m_enabled(enabl)
166 {
167
168 }
169
170 /*! \brief Destructor
171 */
172 ~JanitorDelete()
173 {
174 if (m_enabled)
175 delete m_control;
176 }
177
178 /*! \brief Sets the control object
179 \param control The control object
180 */
181 void setControl(T* control)
182 { m_control = control; }
183
184 /*! \brief Disables the memory releasing
185 */
186 void disable(void)
187 { m_enabled = false; }
188
189 /*! \brief Enables the memory releasing
190 */
191 void enable(void)
192 { m_enabled = true; }
193
194 /*! \returns A const pointer to the data. This does not detach/copy the data.
195 */
196 const T *operator->() const { return m_control; }
197
198 /*! \returns A pointer to the data. This does not detach/copy the data.
199 */
200 T *operator->() { return m_control; }
201 };
202
203 //////////////////////////////////////////////////////////////////////////////////////////
204 /*! \brief Memory releasing and nulling janitor class
205 \details This class can be used to make sure that the associated object is deleted when the
206 janitor leaves scope and the referenced pointer is set to NULL.
207 */
208 template <class T>
209 class JanitorDeleteNull {
210 private:
211 T*& m_control;
212 bool m_enabled;
213 public:
214
215 /*! \brief Constructor
216 */
217 JanitorDeleteNull<T>(T*& control, bool enabl = true) :
218 m_control(control),
219 m_enabled(enabl)
220 {
221
222 }
223
224 /*! \brief Destructor
225 */
226 ~JanitorDeleteNull()
227 {
228 if (m_enabled)
229 {
230 delete m_control;
231 m_control = 0;
232 }
233 }
234
235 /*! \brief Disables the memory releasing and nulling
236 */
237 void disable(void)
238 { m_enabled = false; }
239
240 /*! \brief Enables the memory releasing and nulling
241 */
242 void enable(void)
243 { m_enabled = true; }
244
245 /*! \returns A const pointer to the data. This does not detach/copy the data.
246 */
247 const T *operator->() const { return m_control; }
248
249 /*! \returns A pointer to the data. This does not detach/copy the data.
250 */
251 T *operator->() { return m_control; }
252 };
253
254 //////////////////////////////////////////////////////////////////////////////////////////
255 /*! \brief Memory releasing janitor class
256 \details This class can be used to make sure that the associated object is deleted when the
257 janitor leaves scope.
258 */
259 template <class T>
260 class JanitorDeleteArray {
261 private:
262 T* m_control;
263 bool m_enabled;
264 public:
265
266 /*! \brief Constructor
267 */
268 JanitorDeleteArray<T>(T* control, bool enabl = true) :
269 m_control(control), m_enabled(enabl) {}
270
271 /*! \brief Destructor
272 */
273 ~JanitorDeleteArray()
274 {
275 if (m_enabled)
276 delete[] m_control;
277 }
278
279 /*! \brief Disables the memory releasing
280 */
281 void disable(void)
282 { m_enabled = false; }
283
284 /*! \brief Enables the memory releasing
285 */
286 void enable(void)
287 { m_enabled = true; }
288 };
289 #endif
290
291 //////////////////////////////////////////////////////////////////////////////////////////
292 /*! \brief Class function calling janitor class
293 \details This class can be used to make sure that the given class function is called when the
294 janitor leaves scope.
295 */
296 template <class T, typename R = void>
297 class JanitorClassFunc {
298 public:
299 typedef R (T::*t_func_JanitorClassFunc)(void); //!< A function prototype for a janitor class
300 private:
301 const JanitorClassFunc& operator = (const JanitorClassFunc&);
302
303 T& m_control;
304 t_func_JanitorClassFunc m_funcJCF;
305 bool m_enabled;
306 public:
307
308 /*! \brief Constructor
309 */
310 JanitorClassFunc<T,R>(T& control, t_func_JanitorClassFunc func, bool enabl = true) :
311 m_control(control), m_funcJCF(func), m_enabled(enabl)
312 {
313 }
314
315 /*! \brief Destructor
316 */
317 ~JanitorClassFunc()
318 {
319 if (m_enabled)
320 (m_control.*m_funcJCF)();
321 }
322
323 /*! \brief Disables the function calling
324 */
325 void disable(void)
326 { m_enabled = false; }
327
328 /*! \brief Enables the function calling
329 */
330 void enable(void)
331 { m_enabled = true; }
332 };
333
334 //////////////////////////////////////////////////////////////////////////////////////////
335 /*! \brief Class function calling janitor class with a parameter
336 \details This class can be used to make sure that the given class function is called when the
337 janitor leaves scope.
338 */
339 template <class T, typename P1, typename R = void>
340 class JanitorClassFuncP1 {
341 public:
342 typedef R (T::*t_func_JanitorClassFunc)(P1); //!< A function prototype for a janitor class
343 private:
344 const JanitorClassFuncP1& operator = (const JanitorClassFuncP1&);
345
346 T& m_control;
347 P1 m_param1;
348 t_func_JanitorClassFunc m_funcJCF;
349 bool m_enabled;
350 public:
351
352 /*! \brief Constructor
353 */
354 JanitorClassFuncP1<T,P1,R>(T& control, P1 p1, t_func_JanitorClassFunc func, bool enabl = true) :
355 m_control(control), m_param1(p1), m_funcJCF(func), m_enabled(enabl)
356 {
357 }
358
359 /*! \brief Destructor
360 */
361 ~JanitorClassFuncP1()
362 {
363 if (m_enabled)
364 (m_control.*m_funcJCF)(m_param1);
365 }
366
367 /*! \brief Disables the function calling
368 */
369 void disable(void)
370 { m_enabled = false; }
371
372 /*! \brief Enable the function calling
373 */
374 void enable(void)
375 { m_enabled = true; }
376 };
377
378 #ifndef NOT_FOR_PUBLIC_RELEASE
379 //////////////////////////////////////////////////////////////////////////////////////////
380 /*! \brief Function calling janitor class for function with 0 parameters
381 \details This class can be used to make sure that the given function is called on the given
382 object when the janitor leaves scope. Take care that the object is not of a type that
383 is destroyed before the function unrolling begins.
384 */
385 template <typename ResultType = void>
386 class JanitorFunc0 {
387 public:
388 typedef ResultType (__cdecl * t_func_JanitorFunc)(void); //!< A function prototype for a janitor function
389 private:
390 const JanitorFunc0& operator = (const JanitorFunc0&);
391 t_func_JanitorFunc m_funcJF;
392 bool m_enabled;
393 public:
394
395 /*! \brief Constructor
396 */
397 JanitorFunc0<ResultType>(t_func_JanitorFunc func, bool enabl = true) :
398 m_funcJF(func), m_enabled(enabl) {}
399
400 /*! \brief Destructor
401 */
402 ~JanitorFunc0()
403 {
404 if (m_enabled)
405 (*m_funcJF)();
406 }
407
408 /*! \brief Disables the function calling
409 */
410 void disable(void)
411 { m_enabled = false; }
412
413 /*! \brief Enables the function calling
414 */
415 void enable(void)
416 { m_enabled = true; }
417 };
418
419 #ifndef __AVR32__
420 //////////////////////////////////////////////////////////////////////////////////////////
421 /*! \brief Function calling janitor class for std::function with 0 parameters
422 \details This class can be used to make sure that the given function is called on the given
423 object when the janitor leaves scope. Take care that the object is not of a type that
424 is destroyed before the function unrolling begins.
425 */
426 template <typename ResultType = void>
427 class JanitorStdFunc0 {
428 public:
429 typedef std::function<ResultType (void)> t_func_JanitorFunc; //!< A function prototype for a janitor function
430 private:
431 const JanitorStdFunc0& operator = (const JanitorStdFunc0&);
432 t_func_JanitorFunc m_funcJF;
433 bool m_enabled;
434 public:
435
436 /*! \brief Constructor
437 */
438 JanitorStdFunc0<ResultType>(t_func_JanitorFunc func, bool enabl = true) :
439 m_funcJF(func), m_enabled(enabl) {}
440
441 /*! \brief Destructor
442 */
443 ~JanitorStdFunc0()
444 {
445 if (m_enabled)
446 m_funcJF();
447 }
448
449 /*! \brief Disables the function calling
450 */
451 void disable(void)
452 { m_enabled = false; }
453
454 /*! \brief Enables the function calling
455 */
456 void enable(void)
457 { m_enabled = true; }
458 };
459 #endif
460
461 //////////////////////////////////////////////////////////////////////////////////////////
462 /*! \brief Function calling janitor class for function with 1 parameter
463 \details This class can be used to make sure that the given function is called on the given
464 object when the janitor leaves scope. Take care that the object is not of a type that
465 is destroyed before the function unrolling begins.
466 */
467 template <class ParamType, typename ResultType = void>
468 class JanitorFunc1 {
469 public:
470 typedef ResultType (__cdecl * t_func_JanitorFunc)(ParamType); //!< A function prototype for a janitor function
471 private:
472 const JanitorFunc1& operator = (const JanitorFunc1&);
473 ParamType m_control;
474 t_func_JanitorFunc m_funcJF;
475 bool m_enabled;
476 public:
477
478 /*! \brief Constructor
479 */
480 JanitorFunc1<ParamType,ResultType>(t_func_JanitorFunc func, ParamType control, bool enabl = true) :
481 m_control(control), m_funcJF(func), m_enabled(enabl) {}
482
483 /*! \brief Destructor
484 */
485 ~JanitorFunc1()
486 {
487 if (m_enabled)
488 (*m_funcJF)(m_control);
489 }
490
491 /*! \brief Disables the function calling
492 */
493 void disable(void)
494 { m_enabled = false; }
495
496 /*! \brief Enables the function calling
497 */
498 void enable(void)
499 { m_enabled = true; }
500 };
501
502 //////////////////////////////////////////////////////////////////////////////////////////
503 /*! \brief Function calling janitor class for function with 1 reference parameter
504 \details This class can be used to make sure that the given function is called on the given
505 object when the janitor leaves scope. Take care that the object is not of a type that
506 is destroyed before the function unrolling begins.
507 */
508 template <class ParamType, typename ResultType = void>
509 class JanitorFunc1R {
510 public:
511 typedef ResultType (__cdecl * t_func_JanitorFunc)(ParamType); //!< A function prototype for a janitor function
512 private:
513 const JanitorFunc1R& operator = (const JanitorFunc1R&);
514 ParamType& m_control;
515 t_func_JanitorFunc m_funcJF;
516 bool m_enabled;
517 public:
518
519 /*! \brief Constructor
520 */
521 JanitorFunc1R<ParamType,ResultType>(t_func_JanitorFunc func, ParamType& control, bool enabl = true) :
522 m_control(control), m_funcJF(func), m_enabled(enabl) {}
523
524 /*! \brief Destructor
525 */
526 ~JanitorFunc1R()
527 {
528 if (m_enabled)
529 (*m_funcJF)(m_control);
530 }
531
532 /*! \brief Disables the function calling
533 */
534 void disable(void)
535 { m_enabled = false; }
536
537 /*! \brief Enables the function calling
538 */
539 void enable(void)
540 { m_enabled = true; }
541 };
542
543 //////////////////////////////////////////////////////////////////////////////////////////
544 /*! \brief Function calling janitor class for function with 2 parameters
545 \details This class can be used to make sure that the given function is called on the given
546 object when the janitor leaves scope. Take care that the object is not of a type that
547 is destroyed before the function unrolling begins.
548 */
549 template <class Param1Type, class Param2Type, typename ResultType = void>
550 class JanitorFunc2 {
551 public:
552 typedef ResultType (__cdecl * t_func_JanitorFunc)(Param1Type,Param2Type); //!< A function prototype for a janitor function
553 private:
554 const JanitorFunc2& operator = (const JanitorFunc2&);
555 Param1Type m_control1;
556 Param2Type m_control2;
557 t_func_JanitorFunc m_funcJF;
558 bool m_enabled;
559 public:
560
561 /*! \brief Constructor
562 */
563 JanitorFunc2<Param1Type,Param2Type,ResultType>(t_func_JanitorFunc func, Param1Type control1, Param2Type control2, bool enabl = true) :
564 m_funcJF(func), m_control1(control1), m_control2(control2), m_enabled(enabl) {}
565
566 /*! \brief Destructor
567 */
568 ~JanitorFunc2()
569 {
570 if (m_enabled)
571 (*m_funcJF)(m_control1,m_control2);
572 }
573
574 /*! \brief Disables the function calling
575 */
576 void disable(void)
577 { m_enabled = false; }
578
579 /*! \brief Enables the function calling
580 */
581 void enable(void)
582 { m_enabled = true; }
583 };
584
585 //////////////////////////////////////////////////////////////////////////////////////////
586 /*! \brief Function calling janitor class for function with 2 reference parameters
587 \details This class can be used to make sure that the given function is called on the given
588 object when the janitor leaves scope. Take care that the object is not of a type that
589 is destroyed before the function unrolling begins.
590 */
591 template <class Param1Type, class Param2Type, typename ResultType = void>
592 class JanitorFunc2R {
593 public:
594 typedef ResultType (__cdecl * t_func_JanitorFunc)(Param1Type,Param2Type); //!< A function prototype for a janitor function
595 private:
596 const JanitorFunc2R& operator = (const JanitorFunc2R&);
597 Param1Type& m_control1;
598 Param2Type& m_control2;
599 t_func_JanitorFunc m_funcJF;
600 bool m_enabled;
601 public:
602
603 /*! \brief Constructor
604 */
605 JanitorFunc2R<Param1Type,Param2Type,ResultType>(t_func_JanitorFunc func, Param1Type& control1, Param2Type& control2, bool enabl = true) :
606 m_funcJF(func), m_control1(control1), m_control2(control2), m_enabled(enabl) {}
607
608 /*! \brief Destructor
609 */
610 ~JanitorFunc2R()
611 {
612 if (m_enabled)
613 (*m_funcJF)(m_control1,m_control2);
614 }
615
616 /*! \brief Disables the function calling
617 */
618 void disable(void)
619 { m_enabled = false; }
620
621 /*! \brief Enables the function calling
622 */
623 void enable(void)
624 { m_enabled = true; }
625 };
626
627 //////////////////////////////////////////////////////////////////////////////////////////
628 /*! \brief Log / printf-like function calling janitor class
629 \details This class can be used to make sure that the given printf-like function is called with the
630 supplied parameter when the janitor leaves scope. Take care that the object is not of a type that
631 is destroyed before the function unrolling begins.
632 */
633 template <class T, class C, typename R = void>
634 class JanitorLogFunc {
635 public:
636 typedef R (__cdecl * t_func_JanitorLogFunc)(const char*, const char*, ...); //!< A function prototype for a janitor log function
637 private:
638 const JanitorLogFunc& operator = (const JanitorLogFunc&);
639 t_func_JanitorLogFunc m_funcJF;
640 const char* m_filter;
641 const char* m_str;
642 const char* m_functionName;
643 T& m_control;
644 bool m_enabled;
645 public:
646
647 /*! \brief Constructor with additional 'function name' parameter
648 */
649 JanitorLogFunc<T,C,R>(t_func_JanitorLogFunc func, const char *filter, const char *str, const char *functionName, T& control, bool enable = true) :
650 m_funcJF(func), m_filter(filter), m_str(str), m_functionName(functionName), m_control(control), m_enabled(enable)
651 {}
652
653 /*! \brief Constructor
654 */
655 JanitorLogFunc<T,C,R>(t_func_JanitorLogFunc func, const char *filter, const char* str, T& control, bool enable = true) :
656 m_funcJF(func), m_filter(filter), m_str(str), m_functionName(0), m_control(control), m_enabled(enable) {}
657
658 /*! \brief Destructor
659 */
660 ~JanitorLogFunc()
661 {
662 if (m_enabled)
663 {
664 if (m_functionName)
665 (*m_funcJF)(m_filter, m_str, m_functionName, (C) m_control);
666 else
667 (*m_funcJF)(m_filter, m_str, (C) m_control);
668 }
669 }
670
671 /*! \brief Disables the log function calling
672 */
673 void disable(void)
674 { m_enabled = false; }
675
676 /*! \brief Enables the log function calling
677 */
678 void enable(void)
679 { m_enabled = true; }
680 };
681
682 //////////////////////////////////////////////////////////////////////////////////////////
683 /*! \brief Log / printf-like function calling janitor class
684 \details This class can be used to make sure that the given printf-like function is called with the
685 supplied string when the janitor leaves scope.
686 */
687 template <typename R = void>
688 class JanitorSimpleLogFunc {
689 public:
690 typedef R (__cdecl * t_func_JanitorSimpleLogFunc)(const char*, const char*, ...); //!< A function prototype for a janitor simple log function
691 private:
692 const JanitorSimpleLogFunc& operator = (const JanitorSimpleLogFunc&);
693 t_func_JanitorSimpleLogFunc m_funcJF;
694 const char* m_filter;
695 const char* m_str;
696 const char* m_functionName;
697 bool m_enabled;
698 public:
699
700 /*! \brief Constructor with additional 'function name' parameter
701 */
702 JanitorSimpleLogFunc<R>(t_func_JanitorSimpleLogFunc func, const char *filter, const char *str, const char *functionName, bool enable = true) :
703 m_funcJF(func), m_filter(filter), m_str(str), m_functionName(functionName), m_enabled(enable)
704 {}
705
706 /*! \brief Constructor
707 */
708 JanitorSimpleLogFunc<R>(t_func_JanitorSimpleLogFunc func, const char *filter, const char* str, bool enable = true) :
709 m_funcJF(func), m_filter(filter), m_str(str), m_functionName(0), m_enabled(enable) {}
710
711 /*! \brief Destructor
712 */
713 ~JanitorSimpleLogFunc()
714 {
715 if (m_enabled)
716 {
717 if (m_functionName)
718 (*m_funcJF)(m_filter, m_str, m_functionName);
719 else
720 (*m_funcJF)(m_filter, m_str);
721 }
722 }
723
724 /*! \brief Disables the log function calling
725 */
726 void disable(void)
727 { m_enabled = false; }
728
729 /*! \brief Enables the log function calling
730 */
731 void enable(void)
732 { m_enabled = true; }
733 };
734
735 //////////////////////////////////////////////////////////////////////////////////////////
736 /*! \brief Function calling janitor class
737 \details This class can be used to make sure that the given function is called on the given
738 object when the janitor leaves scope. Take care that the object is not of a type that
739 is destroyed before the function unrolling begins.
740 */
741 template <class ParamType, typename ResultType = void>
742 class JanitorFuncStdCall {
743 public:
744 typedef ResultType (__stdcall * t_func_JanitorFuncStdCall)(ParamType); //!< A function prototype for a calling janitor function
745 private:
746 const JanitorFuncStdCall& operator = (const JanitorFuncStdCall&);
747 ParamType& m_control;
748 t_func_JanitorFuncStdCall m_funcJFSC;
749 bool m_enabled;
750 public:
751
752 /*! \brief Constructor
753 */
754 JanitorFuncStdCall<ParamType,ResultType>(t_func_JanitorFuncStdCall func, ParamType& control, bool enabl = true) :
755 m_funcJFSC(func), m_control(control), m_enabled(enabl) {}
756
757 /*! \brief Destructor
758 */
759 ~JanitorFuncStdCall()
760 {
761 if (m_enabled)
762 (*m_funcJFSC)(m_control);
763 }
764
765 /*! \brief Disables the function calling
766 */
767 void disable(void)
768 { m_enabled = false; }
769
770 /*! \brief Enables the function calling
771 */
772 void enable(void)
773 { m_enabled = true; }
774 };
775
776 //////////////////////////////////////////////////////////////////////////////////////////
777 /*! \brief Value restoring janitor class
778 \details This class can be used to make sure that the value that is in the variable at the time
779 the janitor is created will be in it again when the janitor leaves scope.
780 */
781 template <class T>
782 class JanitorSet {
783 private:
784 const JanitorSet& operator = (const JanitorSet&);
785 T& m_control;
786 T m_value;
787 bool m_enabled;
788 public:
789
790 /*! \brief Constructor
791 */
792 JanitorSet<T>(T& control, const T& val, bool enabl = true) :
793 m_control(control), m_value(val), m_enabled(enabl) {}
794
795 /*! \brief Destructor
796 */
797 ~JanitorSet()
798 {
799 if (m_enabled)
800 m_control = m_value;
801 }
802
803 /*! \brief Disables the value restoring
804 */
805 void disable(void)
806 { m_enabled = false; }
807
808 /*! \brief Enables the value restoring
809 */
810 void enable(void)
811 { m_enabled = true; }
812 };
813
814
815 //////////////////////////////////////////////////////////////////////////////////////////
816 /*! \brief Reference counting janitor class
817 \details This class can be used to make sure that the associated pointer is deleted when the
818 last janitor for it leaves scope.
819 */
820 template <typename T>
821 class JanitorRCDeallocationPolicy_Delete
822 {
823 public:
824 /*! \brief Deallocates (deletes) a pointer to an object
825 \param p The pointer to an object to delete
826 */
827 static void Deallocate(T* p) {delete p;}
828 protected:
829 /*! \brief Destructor
830 \note Always use non-virtual protected destructors in policies
831 */
832 ~JanitorRCDeallocationPolicy_Delete() {}
833 };
834
835 /*! \brief Reference counting janitor class
836 \details This class can be used to make sure that the associated pointer is freed when the
837 last janitor for it leaves scope.
838 */
839 template <typename T>
840 class JanitorRCDeallocationPolicy_Free
841 {
842 public:
843 /*! \brief Deallocates (frees) a pointer to an object
844 \param p The pointer to an object to free
845 */
846 static void Deallocate(T* p) {free(p);}
847 protected:
848 /*! \brief Destructor
849 \note Always use non-virtual protected destructors in policies
850 */
851 ~JanitorRCDeallocationPolicy_Free() {}
852 };
853
854 /*! \brief Reference counting janitor class
855 \details This class can be used to make sure that the associated pointer is deleted/freed when the
856 last janitor for it leaves scope.
857 */
858 template <class T, template <class> class DeallocationPolicy>
859 class JanitorRCDeallocation {
860 public:
861 typedef JanitorRCDeallocation<T, DeallocationPolicy> ThisType; //!< A type definition for this type
862 typedef T ElementType; //!< A type definition for an element type
863 typedef void (*PostDeallocateFunc)(); //!< A function prototype for a post deallocation function
864
865 /*! \brief Constructor with an element type
866 */
867 JanitorRCDeallocation(ElementType *target = 0, bool enabled=true)
868 : m_target(target)
869 , m_refCounter(0)
870 , m_enabled(enabled)
871 {
872 addRef();
873 }
874
875 /*! \brief Constructor with this type
876 */
877 JanitorRCDeallocation(const ThisType &j, bool enabled=true)
878 : m_target(j.m_target)
879 , m_refCounter(j.m_refCounter)
880 , m_enabled(enabled)
881 {
882 addRef();
883 }
884
885 /*! \brief Destructor
886 */
887 virtual ~JanitorRCDeallocation() { removeRef(); }
888
889 /*! \brief Copy the data from \a rhs
890 \param rhs The janitor deallication class
891 \returns this
892 */
893 JanitorRCDeallocation& operator=(const JanitorRCDeallocation &rhs)
894 {
895 if (m_target != rhs.m_target) {
896 removeRef();
897 m_target = rhs.m_target;
898 m_refCounter = rhs.m_refCounter;
899 addRef();
900 }
901 return *this;
902 }
903 /*! \returns A const pointer to the data. This does not detach/copy the data.
904 */
905 ElementType* operator->() const
906 {
907 return m_target;
908 }
909
910 /*! \returns A reference to the data. This does not detach/copy the data.
911 */
912 ElementType& operator*() const
913 {
914 return *m_target;
915 }
916
917 /*! \brief Disables the reference counting
918 */
919 void disable(void)
920 {
921 m_enabled = false;
922 }
923
924 /*! \brief Enables the reference counting
925 */
926 void enable(void)
927 {
928 m_enabled = true;
929 }
930
931 /*! \returns A pointer to an element type
932 */
933 ElementType* get() const throw()
934 {
935 return m_target;
936 }
937
938 /*! \brief Swaps the current object's variables with an other object's variables
939 \param other The reference to an other object
940 */
941 void swap(JanitorRCDeallocation& other) throw()
942 {
943 std::swap(m_target, other.m_target);
944 std::swap(m_refCounter, other.m_refCounter);
945 std::swap(m_enabled, other.m_enabled);
946 }
947
948 /*! \brief Swaps an element type with this type
949 \details If no argument is passed the object will be reset
950 \param p The pointer to an element type
951 */
952 void reset(ElementType* p = 0) throw()
953 {
954 if (m_target != p) {
955 ThisType(p).swap(*this);
956 }
957 }
958
959 private:
960
961 /*! \brief Adds a reference to a reference counter
962 \note If there was no reference counter, then it will create a new one
963 */
964 void addRef(void)
965 {
966 if (m_target) {
967 if (!m_refCounter)
968 m_refCounter = new int(0);
969 (*m_refCounter)++;
970 }
971 }
972
973 /*! \brief Removes a reference from a reference counter
974 \note If the reference counter has less than one reference, it will delete itself and deallocate an element type
975 */
976 void removeRef(void)
977 {
978 if (m_refCounter) {
979 if (*m_refCounter > 1)
980 (*m_refCounter)--;
981 else {
982 delete m_refCounter;
983 DeallocationPolicy<ElementType>::Deallocate(m_target);
984 }
985 }
986 }
987
988 ElementType *m_target;
989 int *m_refCounter;
990 bool m_enabled;
991 };
992
993 /*! \brief A reference counter memory releasing janitor class
994 */
995 template <class T, template <class> class DeallocationPolicy = JanitorRCDeallocationPolicy_Free>
996 class JanitorRCFree : public JanitorRCDeallocation<T, DeallocationPolicy>
997 {
998 public:
999
1000 /*! \brief Constructor
1001 */
1002 JanitorRCFree(T *target = 0, bool enabled=true): JanitorRCDeallocation<T, DeallocationPolicy>(target, enabled) {}
1003 };
1004
1005 /*! \brief A reference counter memory releasing janitor class
1006 */
1007 template <class T, template <class> class DeallocationPolicy = JanitorRCDeallocationPolicy_Delete>
1008 class JanitorRCDelete : public JanitorRCDeallocation<T, DeallocationPolicy>
1009 {
1010 public:
1011
1012 /*! \brief Constructor
1013 */
1014 JanitorRCDelete(T *target = 0, bool enabled=true): JanitorRCDeallocation<T, DeallocationPolicy>(target, enabled) {}
1015 };
1016
1017 #endif
1018
1019 }
1020
1021 #endif