00001 #ifndef _CMT_MONOLITHIC 00002 /*! \file 00003 \brief Contains the Janitor class-interfaces and implementations 00004 00005 This file contains a number of janitor classes. These classes can be used to perform 00006 simple actions upon leaving scope, such as deleting an object. 00007 This greatly simplifies exit code. Functions that have lots of exit points can benefit 00008 greatly from janitors. 00009 00010 Each janitor is named after its main functionality, eg Restore, Free, Delete... 00011 00012 \section FileCopyright Copyright Notice 00013 Copyright (C) Xsens Technologies B.V., 2006. All rights reserved. 00014 00015 This source code is intended for use only by Xsens Technologies BV and 00016 those that have explicit written permission to use it from 00017 Xsens Technologies BV. 00018 00019 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 00020 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 00021 IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 00022 PARTICULAR PURPOSE. 00023 */ 00024 #endif 00025 00026 #ifndef _JANITORS_H_2006_05_01 00027 #define _JANITORS_H_2006_05_01 00028 00029 // required for older gnu c++ compiler versions due to difference in attribute declarations 00030 #if defined(__GNUC__) && !defined(HAVE_CDECL) 00031 # define __cdecl __attribute__((cdecl)) 00032 # define __stdcall __attribute__((stdcall)) 00033 #endif 00034 00035 namespace xsens { 00036 00037 00038 ////////////////////////////////////////////////////////////////////////////////////////// 00039 /*! \brief Class function calling janitor class 00040 00041 This class can be used to make sure that the given class function is called when the 00042 janitor leaves scope. 00043 */ 00044 template <class T, typename R = void> 00045 class JanitorClassFunc { 00046 public: 00047 typedef R (T::*t_func_JanitorClasssFunc)(void); 00048 private: 00049 T& m_control; 00050 t_func_JanitorClasssFunc m_funcJCF; 00051 bool m_enabled; 00052 public: 00053 00054 JanitorClassFunc<T,R>(T& control, t_func_JanitorClasssFunc func, bool enabl = true) : 00055 m_control(control), m_funcJCF(func), m_enabled(enabl) 00056 { 00057 } 00058 ~JanitorClassFunc<T,R>() 00059 { 00060 if (m_enabled) 00061 (m_control.*m_funcJCF)(); 00062 } 00063 00064 void disable(void) 00065 { m_enabled = false; } 00066 00067 void enable(void) 00068 { m_enabled = true; } 00069 }; 00070 00071 00072 } // end of xsens namespace 00073 00074 #endif // _JANITORS_H_2006_05_01