-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manager.h
111 lines (93 loc) · 2.56 KB
/
Manager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef H_MANAGER
#define H_MANAGER
#include "mtrand.h"
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <string>
#include "Sise.h"
#include <ctime>
template<class T>
class ResourceManager {
// not thread-safe for assignment
private:
typedef std::map<std::string,T*> ResourceContainer;
ResourceContainer resources;
public:
ResourceManager(void) {}
~ResourceManager(void) {
for(typename ResourceContainer::iterator i=resources.begin();i != resources.end(); i++) {
delete i->second;
}
}
ResourceManager(const std::string&);
void bind(const std::string& str, T* resource) {
typename ResourceContainer::iterator i = resources.find( str );
if( i != resources.end() ) {
delete i->second;
}
resources[ str ] = resource;
}
T& operator[](const std::string& str) {
return *resources[ str ];
}
};
template
<class T>
class RandomVariantsCollection {
private:
MTRand_int32 prng;
std::vector<T*> elts;
public:
RandomVariantsCollection(void) :
prng (time(0)), // this doesn't need high-quality seeding
elts ()
{
}
RandomVariantsCollection(int seed) : // ..if it does..
prng (seed),
elts ()
{
}
virtual ~RandomVariantsCollection(void) {
for(typename std::vector<T*>::iterator i = elts.begin(); i != elts.end(); i++) {
delete *i;
}
}
void adopt(T* elt) {
elts.push_back( elt );
}
T& choose(void) {
return *elts[abs( prng() ) % elts.size()];
}
};
template
<class T>
void fillManagerFromFile(const std::string& filename, ResourceManager<T>& uts) {
using namespace Sise;
using namespace std;
SExpStreamParser streamParser;
ifstream is ( filename.c_str(), ios::in );
char byte;
if( !is.good() ) {
throw FileInputError();
}
while( !is.eof() ) {
is.read( &byte, 1 );
streamParser.feed( byte );
}
streamParser.end();
while( !streamParser.empty() ) {
Cons *data = asProperCons( streamParser.pop() );
std::string name = * asSymbol(data->getcar() );
T *ut = new T( data );
delete data;
uts.bind( name, ut );
}
}
template<class T>
ResourceManager<T>::ResourceManager(const std::string& name){
fillManagerFromFile( name, *this );
}
#endif