Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
envelope.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
21
23
24namespace ore {
25namespace data {
26
28 XMLUtils::checkNode(node, "Envelope");
29 counterparty_ = XMLUtils::getChildValue(node, "CounterParty", false);
30
31 XMLNode* nettingSetDetailsNode = XMLUtils::getChildNode(node, "NettingSetDetails");
32 if (nettingSetDetailsNode) {
33 nettingSetDetails_.fromXML(nettingSetDetailsNode);
34 } else {
35 string nettingSetId = XMLUtils::getChildValue(node, "NettingSetId", false);
37 }
38
39 portfolioIds_.clear();
40 XMLNode* portfolioNode = XMLUtils::getChildNode(node, "PortfolioIds");
41 if (portfolioNode) {
42 for (auto const& c : XMLUtils::getChildrenNodes(portfolioNode, "PortfolioId"))
44 }
45
46 std::function<boost::any(XMLNode*)> getValue;
47 getValue = [&getValue](XMLNode* node) {
48 boost::any value;
49 vector<XMLNode*> children = XMLUtils::getChildrenNodes(node, "");
50 // If node is a single-value node
51 if (children.size() == 1 && XMLUtils::getNodeName(children[0]) == "") {
53 } else {
54 std::multimap<string, boost::any> subAddFields;
55 for (XMLNode* child : children) {
56 const string& name = XMLUtils::getNodeName(child);
57 boost::any childValue = getValue(child);
58 subAddFields.insert({name, childValue});
59 }
60 value = subAddFields;
61 }
62 return value;
63 };
64
65 additionalFields_.clear();
66 XMLNode* additionalNode = XMLUtils::getChildNode(node, "AdditionalFields");
67 if (additionalNode) {
68 for (XMLNode* child = XMLUtils::getChildNode(additionalNode); child; child = XMLUtils::getNextSibling(child)) {
69 additionalFields_[XMLUtils::getNodeName(child)] = getValue(child);
70 }
71 }
72 initialized_ = true;
73}
74
76 XMLNode* node = doc.allocNode("Envelope");
77 XMLUtils::addChild(doc, node, "CounterParty", counterparty_);
79 XMLUtils::addChild(doc, node, "NettingSetId", nettingSetDetails_.nettingSetId());
80 } else {
82 }
83 XMLNode* portfolioNode = doc.allocNode("PortfolioIds");
84 XMLUtils::appendNode(node, portfolioNode);
85 for (const auto& p : portfolioIds_)
86 XMLUtils::addChild(doc, portfolioNode, "PortfolioId", p);
87 XMLNode* additionalNode = doc.allocNode("AdditionalFields");
88 XMLUtils::appendNode(node, additionalNode);
89
90 std::function<void(XMLNode*, const string&, const boost::any&)> addChild;
91 addChild = [&addChild, &doc](XMLNode* node, const string& name, const boost::any& val) {
92 if (val.type() == typeid(string)) {
93 XMLUtils::addChild(doc, node, name, boost::any_cast<string>(val));
94 } else {
95 QL_REQUIRE(val.type() == typeid(std::multimap<string, boost::any>),
96 "Additional field type must be either string or map<string, boost::any>");
97 XMLNode* childNode = doc.allocNode(name);
98 XMLUtils::appendNode(node, childNode);
99 for (const auto& kv : boost::any_cast<std::multimap<string, boost::any>>(val)) {
100 addChild(childNode, kv.first, kv.second);
101 }
102 }
103 };
104
105 for (const auto& it : additionalFields_)
106 addChild(additionalNode, it.first, it.second);
107 return node;
108}
109
110const map<string, string> Envelope::additionalFields() const {
111 map<string, string> stringAddFields;
112 for (const auto& f : additionalFields_)
113 if (f.second.type() == typeid(string))
114 stringAddFields[f.first] = boost::any_cast<string>(f.second);
115 return stringAddFields;
116}
117
118string Envelope::additionalField(const std::string& name, const bool mandatory, const std::string& defaultValue) const {
119 auto af = additionalFields();
120 auto it = af.find(name);
121 QL_REQUIRE(it != af.end() || !mandatory,
122 "Envelope::additionalField(): Mandatory field '" << name << "' not found.");
123 return it == af.end() ? defaultValue : it->second;
124}
125
126boost::any Envelope::additionalAnyField(const std::string& name, const bool mandatory, const boost::any& defaultValue) const {
127 auto it = additionalFields_.find(name);
128 QL_REQUIRE(it != additionalFields_.end() || !mandatory,
129 "Envelope::additionalField(): Mandatory field '" << name << "' not found.");
130 return it == additionalFields_.end() ? defaultValue : it->second;
131}
132
133void Envelope::setAdditionalField(const std::string& key, const boost::any& value) {
135}
136
137
138} // namespace data
139} // namespace ore
set< string > portfolioIds_
Definition: envelope.hpp:124
const map< string, string > additionalFields() const
Definition: envelope.cpp:110
virtual void fromXML(XMLNode *node) override
Definition: envelope.cpp:27
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: envelope.cpp:75
void setAdditionalField(const std::string &key, const boost::any &value)
Definition: envelope.cpp:133
NettingSetDetails nettingSetDetails_
Definition: envelope.hpp:123
const string & nettingSetId() const
Definition: envelope.hpp:101
boost::any additionalAnyField(const std::string &name, const bool mandatory=true, const boost::any &defaultValue=boost::none) const
Definition: envelope.cpp:126
map< string, boost::any > additionalFields_
Definition: envelope.hpp:125
string additionalField(const std::string &name, const bool mandatory=true, const std::string &defaultValue=std::string()) const
Definition: envelope.cpp:118
Serializable object holding netting set identification data.
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(XMLDocument &doc) const override
const string & nettingSetId() const
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
XML Utilities Class.
Definition: xmlutils.hpp:119
static void checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
Definition: xmlutils.cpp:428
static string getNodeName(XMLNode *n)
Get and set a node's name.
Definition: xmlutils.cpp:473
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static string getNodeValue(XMLNode *node)
Get a node's value.
Definition: xmlutils.cpp:489
static XMLNode * getNextSibling(XMLNode *node, const string &name="")
Get a node's next sibling node.
Definition: xmlutils.cpp:484
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
SafeStack< ValueType > value
trade envelope data model and serialization
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string name