forked from lballabio/quantlib-old
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepository.cpp
More file actions
333 lines (271 loc) · 11 KB
/
Copy pathrepository.cpp
File metadata and controls
333 lines (271 loc) · 11 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2004, 2005, 2006, 2007, 2008 Eric Ehlers
Copyright (C) 2005, 2006, 2007, 2012 Ferdinando Ametrano
Copyright (C) 2008 Nazcatech sprl Belgium
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#if defined(HAVE_CONFIG_H) // Dynamically created by configure
#include <oh/config.hpp>
#endif
#include <oh/repository.hpp>
#include <oh/serializationfactory.hpp>
#include <oh/exception.hpp>
#include <oh/group.hpp>
#include <boost/regex.hpp>
#include <ostream>
#include <sstream>
using boost::shared_ptr;
using std::set;
using std::string;
using std::endl;
namespace ObjectHandler {
Repository *Repository::instance_;
// std::map cannot be exported across DLL boundaries
// so instead we use a static variable.
Repository::ObjectMap objectMap_;
Repository::Repository() {
instance_ = this;
}
Repository::~Repository() {
instance_ = 0;
}
Repository &Repository::instance() {
OH_REQUIRE(instance_,
"Attempt to reference uninitialized Repository object");
return *instance_;
}
// TODO: implement Scott Meyers' "Effective STL" item 24
string Repository::storeObject(const string &objectID,
const shared_ptr<Object> &object,
bool overwrite,
boost::shared_ptr<ValueObject>) {
OH_REQUIRE(overwrite || !objectExists(objectID),
"Cannot store object with ID '" << objectID <<
"' because an object with that ID already exists");
if(objectExists(objectID)){
ObjectMap::const_iterator result = objectMap_.find(objectID);
//result->second->notifyObservers();
result->second->reset(object);
} else{
shared_ptr<ObjectWrapper> objWrapper(new ObjectWrapper(object));
objectMap_[objectID] = objWrapper;
}
registerObserver(objectMap_[objectID]);
return objectID;
}
void Repository::retrieveObject(shared_ptr<Object> &ret,
const string &id) {
ret = retrieveObjectImpl(id);
}
shared_ptr<Object> Repository::retrieveObjectImpl(const string &objectID) {
ObjectMap::const_iterator result = objectMap_.find(formatID(objectID));
OH_REQUIRE(result != objectMap_.end(),
"ObjectHandler error: attempt to retrieve object "
"with unknown ID '" << objectID << "'");
if(result->second->dirty()) {
result->second->recreate();
}
return result->second->object();
}
const shared_ptr<ObjectWrapper>&
Repository::getObjectWrapper(const string &objectID) const {
ObjectMap::const_iterator result = objectMap_.find(objectID);
OH_REQUIRE(result != objectMap_.end(),
"ObjectHandler error: attempt to retrieve object "
"with unknown ID '" << objectID << "'");
return result->second;
}
void Repository::registerObserver(shared_ptr<ObjectWrapper> objWrapper) {
objWrapper->unregisterWithAll();
const set<string>& relationObs =
objWrapper->object()->properties()->getPrecedentObjects();
set<string>::const_iterator iter = relationObs.begin();
for(; iter != relationObs.end(); iter++) {
shared_ptr<ObjectWrapper> objServable =
getObjectWrapper(formatID(*iter));
objWrapper->registerWith(objServable);
}
}
void Repository::deleteObject(const string &objectID) {
string realID = formatID(objectID);
OH_REQUIRE(objectExists(realID),
"Cannot delete '" << realID << "' because no Object with "
"that ID is present in the Repository");
objectMap_.erase(realID);
}
void Repository::deleteObject(const std::vector<string> &objectIDs) {
OH_REQUIRE(!objectIDs.empty(),
"List of Object IDs for deletion is empty");
std::vector<string>::const_iterator i;
for (i = objectIDs.begin(); i != objectIDs.end(); ++i)
deleteObject(*i);
}
void Repository::deleteAllObjects(const bool &deletePermanent) {
if (deletePermanent) {
objectMap_.clear();
} else {
ObjectMap::iterator i = objectMap_.begin();
while (i != objectMap_.end()) {
if (i->second->object()->permanent())
++i;
else
objectMap_.erase(i++);
}
}
}
void Repository::dump(std::ostream& out) {
out << "dump of all objects in ObjectHandler:" << endl << endl;
ObjectMap::const_iterator i;
for (i=objectMap_.begin(); i!=objectMap_.end(); ++i) {
shared_ptr<Object> object = i->second->object();
out << "Object with ID = " << i->first << ":" << endl <<object;
}
}
void Repository::dumpObject(const string &objectID, std::ostream &out) {
string realID = formatID(objectID);
ObjectMap::const_iterator result = objectMap_.find(realID);
if (result == objectMap_.end()) {
out << "no object in repository with ID = " << realID << endl;
} else {
out << "log dump of object with ID = " << realID <<
endl << result->second;
}
}
int Repository::objectCount() {
return objectMap_.size();
}
const std::vector<string> Repository::listObjectIDs(const string ®ex) {
std::vector<string> objectIDs;
if (regex.empty()) {
objectIDs.reserve(objectMap_.size());
ObjectMap::const_iterator i;
for (i=objectMap_.begin(); i!=objectMap_.end(); ++i)
objectIDs.push_back(i->first);
} else {
boost::regex r(regex, boost::regex::perl | boost::regex::icase);
ObjectMap::const_iterator i;
for (i=objectMap_.begin(); i!=objectMap_.end(); ++i) {
string objectID = i->first;
if (regex_match(objectID, r))
objectIDs.push_back(objectID);
}
}
return objectIDs;
}
bool Repository::objectExists(const string &objectID) const {
return objectMap_.find(objectID) != objectMap_.end();
}
std::vector<bool>
Repository::objectExists(const std::vector<string> &objectList) {
std::vector<bool> ret;
std::vector<string>::const_iterator i;
for (i = objectList.begin(); i != objectList.end(); ++i) {
ret.push_back(objectExists(formatID(*i)));
}
return ret;
}
std::vector<double>
Repository::creationTime(const std::vector<string> &objectList) {
std::vector<double> ret;
std::vector<string>::const_iterator i;
for (i = objectList.begin(); i != objectList.end(); ++i) {
string realID = formatID(*i);
if(objectExists(realID)){
ObjectMap::const_iterator result = objectMap_.find(realID);
ret.push_back(result->second->creationTime());
} else {
OH_FAIL("Unable to retrieve object with ID "<<*i);
}
}
return ret;
}
std::vector<double>
Repository::updateTime(const std::vector<string> &objectList) {
std::vector<double> ret;
for (std::vector<string>::const_iterator i = objectList.begin();
i != objectList.end(); ++i) {
string realID = formatID(*i);
if (objectExists(realID)){
ObjectMap::const_iterator result = objectMap_.find(realID);
ret.push_back( result->second->updateTime());
} else {
OH_FAIL("Unable to retrieve object with ID "<<*i);
}
}
return ret;
}
const std::vector<string>
Repository::precedentIDs(const string &objectID) {
string realID = formatID(objectID);
if (objectExists(realID)){
ObjectMap::const_iterator result = objectMap_.find(realID);
shared_ptr<Object> object = result->second->object();
shared_ptr<Group> group = boost::dynamic_pointer_cast<Group>(object);
if(group)
return precedentIDs(group);
const set<string>& relationObs =
object->properties()->getPrecedentObjects();
std::vector<string> vecRelationObs;
set<string>::const_iterator it = relationObs.begin();
for(; it != relationObs.end(); ++it){
vecRelationObs.push_back(formatID(*it));
}
return vecRelationObs;
} else {
OH_FAIL( "Unable to retrieve object with ID "<<objectID);
}
}
const std::vector<string>
Repository::precedentIDs(const shared_ptr<Group>& group) {
std::vector<string> ret;
std::vector<string>::const_iterator i;
for(i = group->list().begin(); i != group->list().end(); ++i) {
if (objectExists(*i))
ret.push_back(*i);
}
return ret;
}
std::vector<bool>
Repository::isPermanent(const std::vector<string> &objectList) {
std::vector<bool> ret;
std::vector<string>::const_iterator i;
for (i = objectList.begin(); i != objectList.end(); ++i) {
string realID = formatID(*i);
if (objectExists(realID)){
ObjectMap::const_iterator result = objectMap_.find(realID);
ret.push_back(result->second->object()->permanent());
} else {
OH_FAIL("Unable to retrieve object with ID "<<*i);
}
}
return ret;
}
const std::vector<string>
Repository::className(const std::vector<string> &objectList) {
std::vector<string> ret;
std::vector<string>::const_iterator i;
for (i = objectList.begin(); i != objectList.end(); ++i) {
string realID = formatID(*i);
if (objectExists(realID)){
ObjectMap::const_iterator result = objectMap_.find(realID);
ret.push_back(result->second->object()->properties()->className());
} else {
OH_FAIL("Unable to retrieve object with ID "<<*i);
}
}
return ret;
}
string Repository::formatID(const string &objectID) {
return objectID;
}
}