forked from lballabio/quantlib-old
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializationfactory.cpp
More file actions
374 lines (300 loc) · 13.9 KB
/
Copy pathserializationfactory.cpp
File metadata and controls
374 lines (300 loc) · 13.9 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2007 Eric Ehlers
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/serializationfactory.hpp>
#include <oh/processor.hpp>
#include <oh/range.hpp>
#include <oh/group.hpp>
#include <oh/repository.hpp>
#include <oh/conversions/getobjectvector.hpp>
//#if BOOST_VERSION > 105000
//#define BOOST_FILESYSTEM_VERSION 3
#if BOOST_VERSION > 104601 & BOOST_VERSION < 105000
#define BOOST_FILESYSTEM_VERSION 2
#endif
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
#include <boost/serialization/variant.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <fstream>
namespace ObjectHandler {
boost::shared_ptr<Object> createRange(const boost::shared_ptr<ValueObject> &valueObject)
{
// FIXME - Implement ValueObject::permanent() and call that instead?
bool permanent =
boost::get<bool>(valueObject->getProperty("PERMANENT"));
std::vector<std::vector<double> > values =
ObjectHandler::matrix::convert2<double>(valueObject->getProperty("VALUES"), "VALUES");
boost::shared_ptr<Object> object(new Range(valueObject, values, permanent));
return object;
}
boost::shared_ptr<Object> createGroup(const boost::shared_ptr<ValueObject> &valueObject)
{
return boost::shared_ptr<Object>(
new Group(
valueObject,
vector::convert2<std::string>(valueObject->getProperty("OBJECTIDLIST"), "OBJECTIDLIST"),
// FIXME - Implement ValueObject::permanent() and call that instead?
boost::get<bool>(valueObject->getProperty("PERMANENT"))));
}
SerializationFactory *SerializationFactory::instance_;
SerializationFactory::SerializationFactory() {
instance_ = this;
registerCreator("ohRange", createRange);
registerCreator("ohGroup", createGroup);
ProcessorPtr processor(new DefaultProcessor());
ProcessorFactory::instance().storeProcessor("DefaultProcessor", processor);
}
SerializationFactory::~SerializationFactory() {
instance_ = 0;
}
SerializationFactory &SerializationFactory::instance() {
OH_REQUIRE(instance_, "Attempt to reference uninitialized SerializationFactory object");
return *instance_;
}
SerializationFactory::CreatorMap &SerializationFactory::creatorMap_() const {
static CreatorMap creatorMap;
return creatorMap;
}
void SerializationFactory::registerCreator(const std::string &className, const Creator &creator) {
creatorMap_()[className] = creator;
}
boost::shared_ptr<Object> SerializationFactory::recreateObject(
boost::shared_ptr<ObjectHandler::ValueObject> valueObject) const {
CreatorMap::const_iterator i = creatorMap_().find(valueObject->className());
OH_REQUIRE(i != creatorMap_().end(), "No creator for class " << valueObject->className());
Creator creator = i->second;
boost::shared_ptr<ObjectHandler::Object> object = creator(valueObject);
return object;
}
StrObjectPair SerializationFactory::restoreObject(
const boost::shared_ptr<ObjectHandler::ValueObject> &valueObject,
bool overwriteExisting) const {
StrObjectPair object;
object.second = recreateObject(valueObject);
// FIXME just call ValueObject::objectId()?
object.first = boost::get<std::string>(valueObject->getProperty("OBJECTID"));
ObjectHandler::Repository::instance().storeObject(object.first, object.second, overwriteExisting);
return object;
}
int SerializationFactory::saveObjectStream(
std::ostream& outputStream,
const std::vector<boost::shared_ptr<Object> > objectList)
{
std::vector<boost::shared_ptr<ObjectHandler::ValueObject> > valueObjects;
std::set<std::string> seen;
std::vector<boost::shared_ptr<ObjectHandler::Object> >::const_iterator i;
for (i=objectList.begin(); i!=objectList.end(); ++i) {
boost::shared_ptr<ObjectHandler::Object> object = *i;
// FIXME just call ValueObject::objectId()?
std::string objectID
= boost::get<std::string>(object->properties()->getProperty("OBJECTID"));
if (seen.find(objectID) == seen.end()) {
valueObjects.push_back(object->properties());
seen.insert(objectID);
}
}
// Provisionally comment out this sort because
// 1) It causes legs and schedules to appear in the wrong sequence
// 2) In our environment we can control the sequence in which objects are saved
// 3) I don't understand why this sort is required anyway?
//std::stable_sort(valueObjects.begin(), valueObjects.end(), compareCategory);
boost::archive::xml_oarchive oa(outputStream);
register_out(oa, valueObjects);
return valueObjects.size();
}
int SerializationFactory::saveObjectStream(
std::ostream& outputStream,
const std::vector<std::string>& handlesList,
bool includeGroups)
{
std::vector<boost::shared_ptr<ObjectHandler::Object> > ObjectListObjPtr =
ObjectHandler::getObjectVector<ObjectHandler::Object>(handlesList, 0, includeGroups);
return saveObjectStream(outputStream, ObjectListObjPtr);
}
int SerializationFactory::saveObject(
const std::vector<std::string>& handlesList,
const std::string &path,
bool forceOverwrite,
bool includeGroups)
{
std::vector<boost::shared_ptr<ObjectHandler::Object> > ObjectListObjPtr =
ObjectHandler::getObjectVector<ObjectHandler::Object>(handlesList, 0, includeGroups);
return saveObject(ObjectListObjPtr, path, forceOverwrite);
}
int SerializationFactory::saveObject(
const std::vector<boost::shared_ptr<ObjectHandler::Object> >& objectList,
const std::string &path,
bool forceOverwrite) {
OH_REQUIRE(objectList.size(), "Object list is empty");
// Create a boost path object from the char*.
boost::filesystem::path boostPath(path);
// If a parent directory has been specified then ensure it exists.
if ( !boostPath.parent_path().empty() ) {
OH_REQUIRE(boost::filesystem::exists(boostPath.branch_path()),
"Invalid parent path : " << path);
}
// If the file itself exists then ensure we can overwrite it.
if (boost::filesystem::exists(boostPath)) {
if (forceOverwrite) {
try {
boost::filesystem::remove(boostPath);
#if BOOST_VERSION < 105000
} catch (const boost::filesystem::basic_filesystem_error<boost::filesystem::path>&) {
#else
} catch (const boost::filesystem::filesystem_error&) {
#endif
OH_FAIL("Overwrite=TRUE but overwrite failed for existing file: " << path);
}
} else {
OH_FAIL("Overwrite=FALSE and the specified output file exists: " << path);
}
}
std::ofstream ofs(path.c_str());
return saveObjectStream(ofs, objectList);
}
/*std::string SerializationFactory::processObject(
const boost::shared_ptr<ObjectHandler::ValueObject> &valueObject,
bool overwriteExisting) {
// Code to overwrite the object ID
//valueObject->setProperty("OBJECTID", XXX);
CreatorMap::const_iterator i = creatorMap_().find(valueObject->className());
OH_REQUIRE(i != creatorMap_().end(), "No creator for class " << valueObject->className());
Creator creator = i->second;
boost::shared_ptr<ObjectHandler::Object> object = creator(valueObject);
std::string objectID =
boost::any_cast<std::string>(valueObject->getProperty("OBJECTID"));
ObjectHandler::Repository::instance().storeObject(objectID, object, overwriteExisting);
return objectID;
}*/
void SerializationFactory::processPath(
const std::string &path,
bool overwriteExisting,
std::vector<std::string> &processedIDs) {
try {
std::ifstream ifs(path.c_str());
boost::archive::xml_iarchive ia(ifs);
std::vector<boost::shared_ptr<ObjectHandler::ValueObject> > valueObjects;
register_in(ia, valueObjects);
OH_REQUIRE(valueObjects.size(), "Object list is empty");
std::vector<boost::shared_ptr<ObjectHandler::ValueObject> >::const_iterator i;
int count = 0;
for (i=valueObjects.begin(); i!=valueObjects.end(); ++i) {
try {
processedIDs.push_back(
ProcessorFactory::instance().getProcessor(*i)->process(
*this, *i, overwriteExisting));
count++;
} catch (const std::exception &e) {
OH_FAIL("Error processing item " << count << ": " << e.what());
}
}
} catch (const std::exception &e) {
OH_FAIL("Error deserializing file " << path << ": " << e.what());
}
}
std::vector<std::string> SerializationFactory::loadObject(
const std::string &directory,
const std::string &pattern,
bool recurse,
bool overwriteExisting) {
boost::filesystem::path boostPath(directory);
OH_REQUIRE(boost::filesystem::exists(boostPath) && boost::filesystem::is_directory(boostPath),
"The specified directory is not valid : " << directory);
std::vector<std::string> returnValue;
bool fileFound = false;
boost::regex r(pattern, boost::regex::perl | boost::regex::icase);
if (recurse) {
for (boost::filesystem::recursive_directory_iterator itr(boostPath);
itr != boost::filesystem::recursive_directory_iterator(); ++itr) {
#if BOOST_VERSION < 105000
if (regex_match(itr->path().leaf(), r) &&
#else
if (regex_match(itr->path().leaf().string(), r) &&
#endif
boost::filesystem::is_regular(itr->status())) {
fileFound = true;
processPath(itr->path().string(), overwriteExisting, returnValue);
}
}
} else {
for (boost::filesystem::directory_iterator itr(boostPath);
itr != boost::filesystem::directory_iterator(); ++itr) {
#if BOOST_VERSION < 105000
if (regex_match(itr->path().leaf(), r) &&
#else
if (regex_match(itr->path().leaf().string(), r) &&
#endif
boost::filesystem::is_regular(itr->status())) {
fileFound = true;
processPath(itr->path().string(), overwriteExisting, returnValue);
}
}
}
OH_REQUIRE(fileFound, "Found no files matching pattern '" << pattern << "' in directory '"
<< directory << "' with recursion = " << std::boolalpha << recurse);
// processPath() will already have thrown if empty files were detected
// so the following is a redundant sanity check.
OH_REQUIRE(!returnValue.empty(), "No objects loaded from directory : " << directory);
ProcessorFactory::instance().postProcess();
return returnValue;
}
std::string SerializationFactory::saveObjectString(
const std::vector<boost::shared_ptr<ObjectHandler::Object> > &objectList,
bool forceOverwrite /* TODO : we need to remove this arg */) {
OH_REQUIRE(objectList.size(), "Object list is empty");
std::ostringstream os;
saveObjectStream(os, objectList);
return os.str();
}
std::vector<std::string> SerializationFactory::loadObjectString(
const std::string &xml,
bool overwriteExisting) {
std::istringstream xmlStream(xml);
return loadObjectStream(xmlStream, overwriteExisting);
}
std::vector<std::string> SerializationFactory::loadObjectStream(
std::istream& xmlStream,
bool overwriteExisting) {
std::vector<std::string> returnValue;
try {
boost::archive::xml_iarchive ia(xmlStream);
std::vector<boost::shared_ptr<ObjectHandler::ValueObject> > valueObjects;
register_in(ia, valueObjects);
OH_REQUIRE(valueObjects.size(), "Object list is empty");
std::vector<boost::shared_ptr<ObjectHandler::ValueObject> >::const_iterator i;
int count = 0;
for (i=valueObjects.begin(); i!=valueObjects.end(); ++i) {
try {
returnValue.push_back(
ProcessorFactory::instance().getProcessor(*i)->process(
*this, *i, overwriteExisting));
count++;
} catch (const std::exception &e) {
OH_FAIL("Error processing item " << count << ": " << e.what());
}
}
ProcessorFactory::instance().postProcess();
} catch (const std::exception &e) {
OH_FAIL("Error deserializing xml : " << e.what());
}
OH_REQUIRE(!returnValue.empty(), "No objects loaded from xml");
return returnValue;
}
}