Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers debug_count.h Source File

debug_count.h

Go to the documentation of this file.
00001 ///\file
00002 
00003 /******************************************************************************
00004 The MIT License(MIT)
00005 
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 
00010 Copyright(c) 2017 jwellbelove
00011 
00012 Permission is hereby granted, free of charge, to any person obtaining a copy
00013 of this software and associated documentation files(the "Software"), to deal
00014 in the Software without restriction, including without limitation the rights
00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00016 copies of the Software, and to permit persons to whom the Software is
00017 furnished to do so, subject to the following conditions :
00018 
00019 The above copyright notice and this permission notice shall be included in all
00020 copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00028 SOFTWARE.
00029 ******************************************************************************/
00030 
00031 #ifndef __ETL_DEBUG_COUNT__
00032 #define __ETL_DEBUG_COUNT__
00033 
00034 #include <stdint.h>
00035 #include <assert.h>
00036 
00037 #include "platform.h "
00038 
00039 ///\defgroup debug_count debug count
00040 ///\ingroup utilities
00041 
00042 namespace etl
00043 {
00044   //***************************************************************************
00045   /// Used to count instances.
00046   /// Asserts if the count is decremented below zero.
00047   /// Asserts if the count is not zero when destructed.
00048   /// Does nothing in a non-debug build.
00049   ///\ingroup reference
00050   //***************************************************************************
00051   class debug_count
00052   {
00053   public:
00054     
00055 #if defined(ETL_DEBUG)
00056     inline debug_count()
00057       : count(0)
00058     {
00059     }
00060 
00061     inline ~debug_count()
00062     {
00063       assert(count == 0);
00064     }
00065 
00066     inline debug_count& operator ++()
00067     {
00068       ++count;
00069       return *this;
00070     }
00071 
00072     inline debug_count& operator --()
00073     {
00074       --count;
00075       assert(count >= 0);
00076       return *this;
00077     }
00078 
00079     inline debug_count& operator +=(int32_t n)
00080     {
00081       count += n;
00082       return *this;
00083     }
00084 
00085     inline debug_count& operator -=(int32_t n)
00086     {
00087       count -= n;
00088       return *this;
00089     }
00090 
00091     inline operator int32_t()
00092     {
00093       return count;
00094     }
00095 
00096     inline void clear()
00097     {
00098       count = 0;
00099     }
00100 
00101   private:
00102 
00103     int32_t count;
00104 #else
00105     inline debug_count()
00106     {
00107     }
00108 
00109     inline ~debug_count()
00110     {
00111     }
00112 
00113     inline debug_count& operator ++()
00114     {
00115       return *this;
00116     }
00117 
00118     inline debug_count& operator --()
00119     {
00120       return *this;
00121     }
00122 
00123     inline debug_count& operator +=(int32_t /*n*/)
00124     {
00125       return *this;
00126     }
00127 
00128     inline debug_count& operator -=(int32_t /*n*/)
00129     {
00130       return *this;
00131     }
00132 
00133     inline operator int32_t()
00134     {
00135       return 0;
00136     }
00137 
00138     inline void clear()
00139     {
00140     }
00141 
00142 #endif
00143   };
00144 }
00145 
00146 #endif
00147 
00148