Advanced Strategic Command
itemrepository.h
Go to the documentation of this file.
1 /***************************************************************************
2  itemrepository.h - description
3  -------------------
4  begin : Thu Jul 28 2001
5  copyright : (C) 2001 by Martin Bickel
6  email : bickel@asc-hq.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef itemrepositoryH
19 #define itemrepositoryH
20 
21 #include <vector>
22 #include <sigc++/sigc++.h>
23 #include "ascstring.h"
24 #include "typen.h"
25 #include "terraintype.h"
26 #include "vehicletype.h"
27 #include "objecttype.h"
28 #include "buildingtype.h"
29 #include "research.h"
30 #include "textfile_evaluation.h"
31 #include "objects.h"
32 #include "package.h"
33 
39  public:
40  virtual void readTextFiles( PropertyReadingContainer& prc, const ASCString& fileName, const ASCString& location ) = 0;
41  virtual void read ( tnstream& stream ) = 0;
42  virtual void write ( tnstream& stream ) = 0;
43  virtual ASCString getTypeName() = 0;
44  virtual void postChecks() {};
45  virtual ~TextFileDataLoader() {};
46 };
47 
48 
50 extern void registerDataLoader( TextFileDataLoader* dataLoader );
51 
53 extern void registerDataLoader( TextFileDataLoader& dataLoader );
54 
55 
56 template<class T>
58  protected:
60 
61  typedef vector<T*> ItemContainerType;
63  typedef map<int,T*> ObjectMap;
65 
66  void add( T* obj );
67 
68  map<int,int> idTranslation;
69 
70  class RegisterID {
71  ItemRepository<T>& repository;
72  T* object;
73  public:
74  RegisterID( ItemRepository<T>& parent, T* obj ) : repository ( parent ), object(obj) {};
75  void operator() (int id);
76  };
77 
78  friend class RegisterID;
79 
80 
81  public:
82  ItemRepository( const ASCString& typeName_ ) : typeName( typeName_ ) {};
83 
84  T* getObject_byPos( int pos ) const { return container[pos]; };
85 
86  T* getObject_byID( int id ) {
87  typename ObjectMap::iterator i = hash.find( id );
88  if ( i != hash.end() )
89  return i->second;
90 
91  map<int,int>::iterator j = idTranslation.find( id );
92  if ( j != idTranslation.end())
93  return getObject_byID( j->second );
94 
95  return NULL;
96  };
97 
98  size_t getNum() const { return container.size(); };
99 
100  vector<T*>& getVector() { return container; };
101  virtual ~ItemRepository() {
102  for ( typename ItemContainerType::iterator i = container.begin(); i != container.end(); ++i )
103  delete *i;
104  };
105 
106 
107  void addIdTranslation( int from, int to );
109 };
110 
111 template<class T>
113 
114  public:
115  ItemRepositoryLoader( const ASCString& typeName_ ) : ItemRepository<T>( typeName_ ) {};
116  void readTextFiles( PropertyReadingContainer& prc, const ASCString& fileName, const ASCString& location );
117  void read( tnstream& stream );
118  void write( tnstream& stream );
120 };
121 
122 
123 
124 class MineTypeRepository : public ItemRepository<MineType> {
125  public:
127 };
128 
130 
131 
132 extern sigc::signal<void> dataLoaderTicker;
133 
134 
140 
141 extern void loadAllData( bool useCache = true );
142 
144 extern TechAdapterContainer techAdapterContainer;
145 
146 
147 
149  public:
151 
152  class ItemFilter {
153  public:
154  typedef vector<IntRange> IntRangeArray;
155  private:
156  IntRangeArray buildings;
157  IntRangeArray objects;
158  IntRangeArray units;
159  IntRangeArray terrain;
160  IntRangeArray technologies;
161  bool isContained (IntRangeArray& arr, int id );
162  bool active;
163  public:
164  ItemFilter() { active = false; };
165  ItemFilter( const ASCString& _name, const IntRangeArray& unitsetIDs, bool _active );
167  bool isActive() { return active; };
168  void setActive( bool _active ) { active = _active; };
169  void runTextIO ( PropertyContainer& pc );
170  void read ( tnstream& stream ) ;
171  void write ( tnstream& stream ) ;
172  bool isContained ( ItemFiltrationSystem::Category cat, int id );
173  };
175 
177  public:
178  void readTextFiles( PropertyReadingContainer& prc, const ASCString& fileName, const ASCString& location );
179  void read ( tnstream& stream ) ;
180  void write ( tnstream& stream ) ;
181  ASCString getTypeName() { return "itemfilter"; };
182  };
183 
184 
185  static bool isFiltered ( Category cat, int id );
186  static bool isFiltered( const VehicleType* item );
187  static bool isFiltered( const BuildingType* item );
188  static bool isFiltered( const ObjectType* item );
189  static bool isFiltered( const TerrainType* item );
190  static bool isFiltered( const MineType* item );
191 
192 };
193 
194 #endif
virtual void read(tnstream &stream)=0
The type of a field.
Definition: terraintype.h:75
void addIdTranslation(int from, int to)
deallocating_vector< TechAdapter * > TechAdapterContainer
T * getObject_byID(int id)
virtual ~ItemRepository()
ItemRepositoryLoader(const ASCString &typeName_)
void runTextIO(PropertyContainer &pc)
void write(tnstream &stream)
Functions to evaluate the parsed *.asctxt files.
TechAdapterContainer techAdapterContainer
vector< T * > ItemContainerType
The interface for all kinds of IO stream.
virtual void postChecks()
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ObjectMap hash
size_t getNum() const
void readTextFiles(PropertyReadingContainer &prc, const ASCString &fileName, const ASCString &location)
T * getObject_byPos(int pos) const
vector< T * > & getVector()
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
The class describing properties that are common to all buildings of a certain kind.
Definition: buildingtype.h:35
ItemRepositoryLoader< TerrainType > terrainTypeRepository
virtual ASCString getTypeName()=0
static bool isFiltered(Category cat, int id)
An object that can be placed on fields. Roads, pipelines and ditches are examples of objects...
Definition: objecttype.h:30
static deallocating_vector< ItemFilter * > itemFilters
virtual void write(tnstream &stream)=0
The interface for the buildingtype class.
virtual void readTextFiles(PropertyReadingContainer &prc, const ASCString &fileName, const ASCString &location)=0
virtual ~TextFileDataLoader()
void loadAllData(bool useCache=true)
ASCString getTypeName()
map< int, int > idTranslation
friend class RegisterID
ItemRepositoryLoader< VehicleType > vehicleTypeRepository
ItemRepositoryLoader< ObjectType > objectTypeRepository
void add(T *obj)
void readTextFiles(PropertyReadingContainer &prc, const ASCString &fileName, const ASCString &location)
sigc::signal< void > dataLoaderTicker
ItemRepository(const ASCString &typeName_)
Mine and Objects which can be placed on a map field.
map< int, T * > ObjectMap
a container that stores pointers and deletes the pointed-to objects on destruction ...
Definition: typen.h:314
void registerDataLoader(TextFileDataLoader *dataLoader)
registers a dataLoader . The object is delete after use, so use: registerDataLoader( new MyDataLoader...
void read(tnstream &stream)
ASCString typeName
ItemRepositoryLoader< Technology > technologyRepository
RegisterID(ItemRepository< T > &parent, T *obj)
ItemRepositoryLoader< BuildingType > buildingTypeRepository
Everything related to research.
MineTypeRepository mineTypeRepository
ItemContainerType container