home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / core / obj_manager.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-07-01  |  3.0 KB  |  157 lines

  1. /***************************************************************************
  2.  * obj_manager.h  -  Generic object manager
  3.  *
  4.  * Copyright (C) 2005 - 2008 Florian Richter
  5.  ***************************************************************************/
  6. /*
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. */
  15.  
  16. #ifndef SMC_OBJ_MANAGER_H
  17. #define SMC_OBJ_MANAGER_H
  18.  
  19. /* *** *** *** *** *** cObject_Manager *** *** *** *** *** *** *** *** *** *** *** *** */
  20.  
  21. template<class T> class cObject_Manager
  22. {
  23. public:
  24.     cObject_Manager( void ) {};
  25.     virtual ~cObject_Manager( void ) {};
  26.  
  27.     //    Add the given object
  28.     virtual void Add( T *obj )
  29.     {
  30.         objects.push_back( obj );
  31.     }
  32.  
  33.     // Delete the object from given array number
  34.     virtual bool Delete( unsigned int array_num, bool delete_data = 1 )
  35.     {
  36.         // out of array
  37.         if( array_num >= objects.size() )
  38.         {
  39.             return 0;
  40.         }
  41.  
  42.         if( delete_data )
  43.         {
  44.             delete objects[array_num];
  45.         }
  46.  
  47.         objects.erase( objects.begin() + array_num );
  48.  
  49.         return 1;
  50.     }
  51.  
  52.     // Delete the given object
  53.     virtual bool Delete( T *obj, bool delete_data = 1 )
  54.     {
  55.         // empty object
  56.         if( !obj )
  57.         {
  58.             return 0;
  59.         }
  60.  
  61.         return Delete( Get_Array_Num( obj ), delete_data );
  62.     }
  63.  
  64.     // Delete all objects
  65.     virtual void Delete_All( void )
  66.     {
  67.         for( typename vector<T *>::iterator itr = objects.begin(), itr_end = objects.end(); itr != itr_end; ++itr )
  68.         {
  69.             delete *itr;
  70.         }
  71.  
  72.         objects.clear();
  73.     }
  74.  
  75.     /* Return the object pointer
  76.      * if not found returns NULL
  77.     */
  78.     virtual T *Get_Pointer( unsigned int identifier )
  79.     {
  80.         if( identifier >= objects.size() )
  81.         {
  82.             // out of array
  83.             return NULL;
  84.         }
  85.  
  86.         // available
  87.         return objects[identifier];
  88.     }
  89.  
  90.     // Switch objects array position
  91.     bool Switch_Array_Num( T *obj1, T *obj2 )
  92.     {
  93.         // empty object
  94.         if( !obj1 || !obj2 )
  95.         {
  96.             return 0;
  97.         }
  98.  
  99.         // if the same
  100.         if( obj1 == obj2 )
  101.         {
  102.             return 0;
  103.         }
  104.  
  105.         int obj1_pos = Get_Array_Num( obj1 );
  106.         int obj2_pos = Get_Array_Num( obj2 );
  107.         
  108.         // not found
  109.         if( obj1_pos < 0 || obj2_pos < 0 )
  110.         {
  111.             return 0;
  112.         }
  113.  
  114.         objects[obj1_pos] = obj2;
  115.         objects[obj2_pos] = obj1;
  116.  
  117.         return 1;
  118.     }
  119.  
  120.     /* Return the object array number
  121.      * if not found returns -1
  122.     */
  123.     int Get_Array_Num( T *obj )
  124.     {
  125.         // invalid
  126.         if( !obj )
  127.         {
  128.             return -1;
  129.         }
  130.  
  131.         for( unsigned int i = 0; i < objects.size(); i++ )
  132.         {
  133.             // check if the same
  134.             if( objects[i] == obj )
  135.             {
  136.                 // found
  137.                 return i;
  138.             }
  139.         }
  140.  
  141.         // not found
  142.         return -1;
  143.     }
  144.  
  145.     // Return the object count
  146.     unsigned int size( void )
  147.     {
  148.         return objects.size();
  149.     }
  150.  
  151.     vector<T*> objects;
  152. };
  153.  
  154. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  155.  
  156. #endif
  157.