File indexing completed on 2024-04-28 05:32:12

0001 #ifndef ObjectCounterMap_h
0002 #define ObjectCounterMap_h
0003 
0004 /*
0005 * this file is part of the oxygen gtk engine
0006 * SPDX-FileCopyrightText: 2012 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0007 *
0008 * SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #include <map>
0012 #include <string>
0013 
0014 //! thread-safe Object counter storage map
0015 namespace Oxygen
0016 {
0017     class ObjectCounterMap: public std::map<std::string, int>
0018     {
0019 
0020         public:
0021 
0022         //! singleton
0023         static ObjectCounterMap& get( void );
0024 
0025         //!  get counter for a given name
0026         /*!
0027         if the name is found, returns adress of the existing counter
0028         creates new counter otherwise and returns adress
0029         */
0030         int* counter( const std::string& name )
0031         {
0032             iterator iter = find( name );
0033             if( iter == end() ) return &(insert( std::make_pair( name, 0 ) ).first->second);
0034             else return &(iter->second);
0035         }
0036 
0037         //! increment
0038         void increment( int& counter )
0039         { counter++; }
0040 
0041         //! increment
0042         void decrement( int& counter )
0043         { counter--; }
0044 
0045         private:
0046 
0047         //! constructor
0048         ObjectCounterMap( void )
0049         {}
0050 
0051     };
0052 
0053 }
0054 
0055 #endif