Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

itemrepository.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                           itemrepository.h  -  description
00003                              -------------------
00004     begin                : Thu Jul 28 2001
00005     copyright            : (C) 2001 by Martin Bickel
00006     email                : bickel@asc-hq.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef itemrepositoryH
00019 #define itemrepositoryH
00020 
00021 #include <vector>
00022 #include <sigc++/sigc++.h>
00023 #include "ascstring.h"
00024 #include "typen.h"
00025 #include "terraintype.h"
00026 #include "vehicletype.h"
00027 #include "objecttype.h"
00028 #include "buildingtype.h"
00029 #include "research.h"
00030 #include "textfile_evaluation.h"
00031 #include "objects.h"
00032 
00033 
00034 class TextFileDataLoader {
00035    public:
00036       virtual void readTextFiles( PropertyReadingContainer& prc, const ASCString& fileName, const ASCString& location ) = 0;
00037       virtual void read ( tnstream& stream ) = 0;
00038       virtual void write ( tnstream& stream ) = 0;
00039       virtual ASCString getTypeName() = 0;
00040       virtual ~TextFileDataLoader() {};
00041 };
00042 
00043 
00045 extern void registerDataLoader( TextFileDataLoader* dataLoader );
00046 
00048 extern void registerDataLoader( TextFileDataLoader& dataLoader );
00049 
00050 
00051 template<class T>
00052 class ItemRepository  {
00053    protected:
00054       ASCString typeName;
00055       
00056       typedef vector<T*> ItemContainerType;
00057       ItemContainerType   container;
00058       typedef map<int,T*>  ObjectMap;
00059       ObjectMap hash;
00060 
00061       void add( T* obj );
00062 
00063       map<int,int> idTranslation;
00064 
00065       class RegisterID {
00066             ItemRepository<T>& repository;
00067             T* object;
00068          public:
00069             RegisterID( ItemRepository<T>& parent, T* obj  ) : repository ( parent ), object(obj) {};
00070             void operator() (int id);
00071       };
00072 
00073       friend class RegisterID;
00074 
00075 
00076    public:
00077       ItemRepository( const ASCString& typeName_ ) : typeName( typeName_ ) {};
00078       
00079       T* getObject_byPos( int pos ) const { return container[pos]; };
00080 
00081       T* getObject_byID( int id ) { 
00082          typename ObjectMap::iterator i = hash.find( id );
00083          if ( i != hash.end() )
00084             return i->second;
00085 
00086          map<int,int>::iterator j = idTranslation.find( id );
00087          if ( j != idTranslation.end())
00088             return getObject_byID( j->second );
00089 
00090          return NULL;
00091       };
00092 
00093       size_t getNum() const { return container.size(); };
00094 
00095       vector<T*>& getVector() { return container; };
00096       virtual ~ItemRepository() {    
00097          for ( typename ItemContainerType::iterator i = container.begin(); i != container.end(); ++i )
00098             delete *i;
00099       };
00100 
00101 
00102       void addIdTranslation( int from, int to );
00103       ASCString getTypeName() { return typeName; };
00104 };
00105 
00106 template<class T>
00107 class ItemRepositoryLoader: public ItemRepository<T>, public TextFileDataLoader {
00108 
00109    public:
00110       ItemRepositoryLoader( const ASCString& typeName_ ) : ItemRepository<T>( typeName_ ) {};
00111       void readTextFiles( PropertyReadingContainer& prc, const ASCString& fileName, const ASCString& location );
00112       void read( tnstream& stream );
00113       void write( tnstream& stream );
00114       ASCString getTypeName() { return ItemRepository<T>::getTypeName(); };
00115 };
00116 
00117 
00118 
00119 class MineTypeRepository : public ItemRepository<MineType>  {
00120    public:
00121       MineTypeRepository();
00122 };
00123 
00124 extern MineTypeRepository mineTypeRepository;
00125 
00126 
00127 extern SigC::Signal0<void> dataLoaderTicker;
00128 
00129 
00130 extern ItemRepositoryLoader<Vehicletype>  vehicleTypeRepository;
00131 extern ItemRepositoryLoader<TerrainType>  terrainTypeRepository;
00132 extern ItemRepositoryLoader<ObjectType>   objectTypeRepository;
00133 extern ItemRepositoryLoader<BuildingType> buildingTypeRepository;
00134 extern ItemRepositoryLoader<Technology>   technologyRepository;
00135 
00136 extern void  loadAllData( bool useCache = true );
00137 
00138 typedef  deallocating_vector<TechAdapter*> TechAdapterContainer;
00139 extern TechAdapterContainer techAdapterContainer;
00140 
00141 
00142 class ItemFiltrationSystem {
00143       public:
00144          typedef enum { Building, Vehicle, Object, Terrain, Technology } Category;
00145 
00146          class ItemFilter {
00147                public:
00148                  typedef vector<IntRange> IntRangeArray;
00149                private:
00150                  IntRangeArray buildings;
00151                  IntRangeArray objects;
00152                  IntRangeArray units;
00153                  IntRangeArray terrain;
00154                  IntRangeArray technologies;
00155                  bool isContained (IntRangeArray& arr, int id );
00156                  bool active;
00157                public:
00158                  ItemFilter() { active = false; };
00159                  ItemFilter( const ASCString& _name, const IntRangeArray& unitsetIDs, bool _active );
00160                  ASCString name;
00161                  bool isActive() { return active; };
00162                  void setActive( bool _active ) { active = _active; };
00163                  void runTextIO ( PropertyContainer& pc );
00164                  void read ( tnstream& stream ) ;
00165                  void write ( tnstream& stream ) ;
00166                  bool isContained ( ItemFiltrationSystem::Category cat, int id );
00167          };
00168          static deallocating_vector<ItemFilter*> itemFilters;
00169 
00170          class DataLoader : public TextFileDataLoader {
00171             public:
00172                void readTextFiles( PropertyReadingContainer& prc, const ASCString& fileName, const ASCString& location );
00173                void read ( tnstream& stream ) ;
00174                void write ( tnstream& stream ) ;
00175                ASCString getTypeName() { return "itemfilter"; };
00176          };
00177 
00178 
00179          static bool isFiltered ( Category cat, int id );
00180          static bool isFiltered( const Vehicletype* item );
00181          static bool isFiltered( const BuildingType* item );
00182          static bool isFiltered( const ObjectType* item );
00183          static bool isFiltered( const TerrainType* item );
00184          static bool isFiltered( const MineType* item );
00185        
00186 };
00187 
00188 #endif

Generated on Tue Jun 24 01:27:44 2008 for Advanced Strategic Command by  doxygen 1.4.2