Skip to content

Commit 023ece2

Browse files
author
Eric Ehlers
committed
1) Instantiate instances of class Object with a single call to the ctor
rather than the "three-phase initialization" used previously: 1a) Parameters "Permanent" and "Properties" (the Value Object) are now inputs to the Object ctor 1b) Member functions setPermanent() and setProperties() are redundant 2) Item 1a) must be propagated throughout QuantLibObjects class hierarchy 2a) Introduce #defines to save typing: OH_OBJ_CLASS, OH_OBJ_CTOR, OH_LIB_CLASS, OH_LIB_CTOR 3) Parmeter "Permanent" is now a property of the Value Object so the value is preserved during serialization
1 parent 2acc9af commit 023ece2

159 files changed

Lines changed: 1908 additions & 948 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ObjectHandler/Examples/C++/example.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void makeAccount(
4848
ObjectHandler::Variant balance = ObjectHandler::Variant()) {
4949

5050
boost::shared_ptr <ObjectHandler::ValueObject> valueObject(
51-
new AccountExample::AccountValueObject(objectID, type, number, balance));
51+
new AccountExample::AccountValueObject(objectID, type, number, balance, false));
5252

5353
AccountExample::Account::Type typeEnum =
5454
ObjectHandler::Create<AccountExample::Account::Type>()(type);
@@ -58,9 +58,8 @@ void makeAccount(
5858

5959
boost::shared_ptr<ObjectHandler::Object> accountObject1(
6060
new AccountExample::AccountObject(
61-
typeEnum, number, accountBalance));
61+
valueObject, typeEnum, number, accountBalance, false));
6262

63-
accountObject1->setProperties(valueObject);
6463
ObjectHandler::Repository::instance().storeObject(objectID, accountObject1);
6564
}
6665

@@ -117,16 +116,23 @@ int main() {
117116
ObjectHandler::logAllObjects();
118117

119118
// Serialize an object
119+
std::vector<boost::shared_ptr<ObjectHandler::Object> > objectList;
120+
objectList.push_back(accountObject2_retrieve);
120121
ObjectHandler::Repository::instance().saveObject(
121-
accountObject2_retrieve, "account.xml");
122+
objectList, "account.xml", true);
123+
124+
// Delete all objects
125+
ObjectHandler::Repository::instance().deleteAllObjects();
126+
122127
// Deserialize an object
123128
ObjectHandler::Repository::instance().loadObject(
124-
"account1_load", "account.xml");
129+
"account.xml", true);
130+
125131
// Manipulate the deserialized object
126132
OH_GET_OBJECT(accountObject1_load,
127-
"account1_load", AccountExample::AccountObject)
133+
"account2", AccountExample::AccountObject)
128134
accountObject1_load->setBalance(200);
129-
OH_LOG_MESSAGE("Balance of account account1_load = "
135+
OH_LOG_MESSAGE("Balance of account account2 = "
130136
<< accountObject1_load->balance());
131137

132138
OH_LOG_MESSAGE("End example program");

ObjectHandler/Examples/ExampleObjects/Objects/accountobject.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ namespace AccountExample {
2929
public:
3030

3131
AccountObject(
32-
const Account::Type &type,
33-
const long &number,
34-
const long &balance) {
32+
const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
33+
const Account::Type &type,
34+
const long &number,
35+
const long &balance,
36+
bool permanent)
37+
: ObjectHandler::LibraryObject<Account>(properties, permanent) {
38+
3539
libraryObject_ = boost::shared_ptr<Account>(
3640
new Account(type, number, balance));
3741
}

ObjectHandler/Examples/ExampleObjects/Objects/customerobject.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ namespace AccountExample {
2828

2929
public:
3030

31-
CustomerObject(const std::string &name, const long &age) {
31+
CustomerObject(
32+
const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
33+
const std::string &name,
34+
const long &age,
35+
bool permanent)
36+
: ObjectHandler::LibraryObject<Customer>(properties, permanent) {
37+
3238
libraryObject_ = boost::shared_ptr<Customer>(
3339
new Customer(name, age));
3440
}

ObjectHandler/Examples/ExampleObjects/Serialization/creators.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,31 @@ namespace AccountExample {
3232
boost::shared_ptr<ObjectHandler::Object> createAccount(
3333
const boost::shared_ptr<ObjectHandler::ValueObject> &valueObject) {
3434

35-
std::string type = boost::any_cast<std::string>(valueObject->getProperty("type"));
36-
long number = boost::any_cast<long>(valueObject->getProperty("number"));
37-
ObjectHandler::Variant balance = boost::any_cast<ObjectHandler::Variant>(valueObject->getProperty("balance"));
35+
bool permanent = boost::any_cast<bool>(valueObject->getProperty("Permanent"));
36+
std::string type = boost::any_cast<std::string>(valueObject->getProperty("Type"));
37+
long number = boost::any_cast<long>(valueObject->getProperty("Number"));
38+
ObjectHandler::Variant balance = boost::any_cast<ObjectHandler::Variant>(valueObject->getProperty("Balance"));
3839

3940
Account::Type typeEnum =
4041
ObjectHandler::Create<Account::Type>()(type);
4142

4243
long accountBalance = ObjectHandler::variantToScalar<ObjectHandler::Variant, long>(
43-
balance, "balance", 100);
44+
balance, "Balance", 100);
4445

4546
boost::shared_ptr<ObjectHandler::Object> object(
46-
new AccountObject(typeEnum, number, accountBalance));
47-
object->setProperties(valueObject);
47+
new AccountObject(valueObject, typeEnum, number, accountBalance, permanent));
4848
return object;
4949
}
5050

5151
boost::shared_ptr<ObjectHandler::Object> createCustomer(
5252
const boost::shared_ptr<ObjectHandler::ValueObject> &valueObject) {
5353

54+
bool permanent = boost::any_cast<bool>(valueObject->getProperty("Permanent"));
5455
std::string name = boost::any_cast<std::string>(valueObject->getProperty("name"));
5556
long age = boost::any_cast<long>(valueObject->getProperty("age"));
5657

5758
boost::shared_ptr<ObjectHandler::Object> object(
58-
new CustomerObject(name, age));
59-
object->setProperties(valueObject);
59+
new CustomerObject(valueObject, name, age, permanent));
6060
return object;
6161
}
6262

ObjectHandler/Examples/ExampleObjects/ValueObjects/accountvalueobject.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ namespace AccountExample {
2626
const char* AccountValueObject::mPropertyNames[] = {
2727
"objectID",
2828
"className",
29-
"number",
30-
"type",
31-
"balance"};
29+
"Permanent",
30+
"Number",
31+
"Type",
32+
"Balance"};
3233

3334
std::vector<std::string> AccountValueObject::getPropertyNames() const {
3435
return std::vector<std::string>(mPropertyNames,
@@ -38,19 +39,21 @@ namespace AccountExample {
3839
boost::any AccountValueObject::getProperty(const std::string& name) const {
3940
if(name == "objectID") return objectID_;
4041
else if(name == "className") return className_;
41-
else if(name == "number") return number_;
42-
else if(name == "type") return type_;
43-
else if(name == "balance") return balance_;
42+
else if(name == "Permanent") return permanent_;
43+
else if(name == "Number") return number_;
44+
else if(name == "Type") return type_;
45+
else if(name == "Balance") return balance_;
4446
else
4547
OH_FAIL("Error: attempt to retrieve non-existent Property: '" + name + "'");
4648
}
4749

4850
void AccountValueObject::setProperty(const std::string& name, const boost::any& value) {
4951
if(name == "objectID") objectID_ = boost::any_cast<std::string>(value);
5052
else if(name == "className") className_ = boost::any_cast<std::string>(value);
51-
else if(name == "number") number_ = boost::any_cast<long>(value);
52-
else if(name == "type") type_ = boost::any_cast<std::string>(value);
53-
else if(name == "balance") balance_ = boost::any_cast<ObjectHandler::Variant>(value);
53+
else if(name == "Permanent") number_ = boost::any_cast<bool>(value);
54+
else if(name == "Number") number_ = boost::any_cast<long>(value);
55+
else if(name == "Type") type_ = boost::any_cast<std::string>(value);
56+
else if(name == "Balance") balance_ = boost::any_cast<ObjectHandler::Variant>(value);
5457
else
5558
OH_FAIL("Error: attempt to retrieve non-existent Property: '" + name + "'");
5659
}

ObjectHandler/Examples/ExampleObjects/ValueObjects/accountvalueobject.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ namespace AccountExample {
3737
const std::string &objectID,
3838
const std::string &type,
3939
const long &number,
40-
const ObjectHandler::Variant &balance)
41-
: ObjectHandler::ValueObject(objectID, "Account"),
40+
const ObjectHandler::Variant &balance,
41+
bool permanent)
42+
: ObjectHandler::ValueObject(objectID, "Account", permanent),
4243
type_(type), number_(number), balance_(balance) {}
4344

4445
std::vector<std::string> getPropertyNames() const;
@@ -57,9 +58,10 @@ namespace AccountExample {
5758
boost::serialization::void_cast_register<AccountValueObject, ValueObject>(this, this);
5859
ar & boost::serialization::make_nvp("objectID", objectID_)
5960
& boost::serialization::make_nvp("className", className_)
60-
& boost::serialization::make_nvp("type", type_)
61-
& boost::serialization::make_nvp("number", number_)
62-
& boost::serialization::make_nvp("balance", balance_);
61+
& boost::serialization::make_nvp("Type", type_)
62+
& boost::serialization::make_nvp("Number", number_)
63+
& boost::serialization::make_nvp("Balance", balance_)
64+
& boost::serialization::make_nvp("Permanent", permanent_);
6365
}
6466

6567
};

ObjectHandler/Examples/ExampleObjects/ValueObjects/customervalueobject.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ namespace AccountExample {
2626
const char* CustomerValueObject::mPropertyNames[] = {
2727
"objectID",
2828
"className",
29-
"name",
30-
"age"};
29+
"Permanent",
30+
"Name",
31+
"Age"};
3132

3233
std::vector<std::string> CustomerValueObject::getPropertyNames() const {
3334
return std::vector<std::string>(mPropertyNames,
@@ -37,17 +38,19 @@ namespace AccountExample {
3738
boost::any CustomerValueObject::getProperty(const std::string& name) const {
3839
if(name == "objectID") return objectID_;
3940
else if(name == "className") return className_;
40-
else if(name == "name") return name_;
41-
else if(name == "age") return age_;
41+
else if(name == "Permanent") return permanent_;
42+
else if(name == "Name") return name_;
43+
else if(name == "Age") return age_;
4244
else
4345
OH_FAIL("Error: attempt to retrieve non-existent Property: '" + name + "'");
4446
}
4547

4648
void CustomerValueObject::setProperty(const std::string& name, const boost::any& value) {
4749
if(name == "objectID") objectID_ = boost::any_cast<std::string>(value);
4850
else if(name == "className") className_ = boost::any_cast<std::string>(value);
49-
else if(name == "name") name_ = boost::any_cast<std::string>(value);
50-
else if(name == "age") age_ = boost::any_cast<long>(value);
51+
else if(name == "Permanent") name_ = boost::any_cast<bool>(value);
52+
else if(name == "Name") name_ = boost::any_cast<std::string>(value);
53+
else if(name == "Age") age_ = boost::any_cast<long>(value);
5154
else
5255
OH_FAIL("Error: attempt to retrieve non-existent Property: '" + name + "'");
5356
}

ObjectHandler/Examples/ExampleObjects/ValueObjects/customervalueobject.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ namespace AccountExample {
3737
CustomerValueObject(
3838
const std::string &objectID,
3939
const std::string &name,
40-
const long &age)
41-
: ObjectHandler::ValueObject(objectID, "Customer"), name_(name), age_(age) {}
40+
const long &age,
41+
bool permanent)
42+
: ObjectHandler::ValueObject(objectID, "Customer", permanent), name_(name), age_(age) {}
4243

4344
std::vector<std::string> getPropertyNames() const;
4445
boost::any getProperty(const std::string& name) const;
@@ -55,8 +56,9 @@ namespace AccountExample {
5556
boost::serialization::void_cast_register<CustomerValueObject, ValueObject>(this, this);
5657
ar & boost::serialization::make_nvp("objectID", objectID_)
5758
& boost::serialization::make_nvp("className", className_)
58-
& boost::serialization::make_nvp("name", name_)
59-
& boost::serialization::make_nvp("age", age_);
59+
& boost::serialization::make_nvp("Name", name_)
60+
& boost::serialization::make_nvp("Age", age_)
61+
& boost::serialization::make_nvp("Permanent", permanent_);
6062
}
6163

6264
};

0 commit comments

Comments
 (0)