1 #include "CCataloguer.h"
2 
3 #include <stdexcept>
4 
5 #include "CUnit.h"
6 
7 
registerMatcher(const CategoryMatcher & m)8 void CCataloguer::registerMatcher(const CategoryMatcher& m) {
9 	if (cache.find(m) == cache.end()) {
10 		cache[m];
11 	}
12 }
13 
addUnit(UnitType * type,int id)14 void CCataloguer::addUnit(UnitType* type, int id) {
15 	const unitCategory cats = type->cats;
16 
17 	for (CacheMapIt it = cache.begin(); it != cache.end(); ++it) {
18 		if (it->first.test(cats)) {
19 			it->second[id] = type;
20 		}
21 	}
22 }
23 
addUnit(CUnit * unit)24 void CCataloguer::addUnit(CUnit* unit) {
25 	addUnit(unit->type, unit->key);
26 }
27 
removeUnit(int id)28 void CCataloguer::removeUnit(int id) {
29 	for (CacheMapIt it = cache.begin(); it != cache.end(); ++it) {
30 		it->second.erase(id);
31 	}
32 }
33 
getUnits(const CategoryMatcher & m)34 std::map<int, UnitType*>& CCataloguer::getUnits(const CategoryMatcher& m) {
35 	CacheMapIt it = cache.find(m);
36 	if (it != cache.end())
37 		return it->second;
38 	throw std::runtime_error("No unit found for category");
39 }
40 
getUnits(const unitCategory & inc,const unitCategory & exc)41 std::map<int, UnitType*>* CCataloguer::getUnits(const unitCategory& inc, const unitCategory& exc) {
42 	CacheMapIt it;
43 	CategoryMatcher m(inc, exc);
44 
45 	it = cache.find(m);
46 
47 	if (it == cache.end())
48 		return NULL;
49 	return &it->second;
50 }
51