CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FWConfiguration.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWConfiguration
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Fri Feb 22 15:54:29 EST 2008
11 //
12 
13 // system include files
14 #include <memory>
15 
16 #include <stdexcept>
17 #include <algorithm>
18 #include <functional>
19 // user include files
21 
22 //
23 // constants, enums and typedefs
24 //
25 
26 //
27 // static data member definitions
28 //
29 
30 //
31 // constructors and destructor
32 //
33 //FWConfiguration::FWConfiguration()
34 //{
35 //}
36 
38  : m_stringValues(rhs.m_stringValues ? new std::vector<std::string>(*(rhs.m_stringValues)) : nullptr),
39  m_keyValues(rhs.m_keyValues ? new KeyValues(*(rhs.m_keyValues)) : nullptr),
40  m_version(rhs.m_version) {}
41 
43 
44 //
45 // assignment operators
46 //
48  //An exception safe implementation is
49  FWConfiguration temp(rhs);
50  swap(temp);
51 
52  return *this;
53 }
54 
55 //
56 // member functions
57 //
59  if (m_stringValues) {
60  throw std::runtime_error("adding key/value to configuration containing string values");
61  }
62  if (not m_keyValues) {
63  m_keyValues = std::make_unique<KeyValues>(1, std::make_pair(iKey, iConfig));
64  } else {
65  m_keyValues->push_back(std::make_pair(iKey, iConfig));
66  }
67  return *this;
68 }
70  if (m_stringValues) {
71  throw std::runtime_error("adding key/value to configuration containing string values");
72  }
73  if (m_stringValues) {
74  throw std::runtime_error("adding key/value to configuration containing string values");
75  }
76  if (not m_keyValues) {
77  if (not iDoSwap) {
78  m_keyValues = std::make_unique<KeyValues>(1, std::make_pair(iKey, iConfig));
79  } else {
80  m_keyValues = std::make_unique<KeyValues>(1, std::make_pair(iKey, FWConfiguration()));
81  m_keyValues->back().second.swap(iConfig);
82  }
83  } else {
84  if (not iDoSwap) {
85  m_keyValues->push_back(std::make_pair(iKey, iConfig));
86  } else {
87  m_keyValues->push_back(std::make_pair(iKey, FWConfiguration()));
88  m_keyValues->back().second.swap(iConfig);
89  }
90  }
91  return *this;
92 }
93 
95  if (m_keyValues) {
96  throw std::runtime_error("adding string value to configuration containing key/value pairs");
97  }
98  if (not m_stringValues) {
99  m_stringValues = std::make_unique<std::vector<std::string>>(1, iValue);
100  } else {
101  m_stringValues->push_back(iValue);
102  }
103  return *this;
104 }
105 
108  m_stringValues.swap(iRHS.m_stringValues);
109  m_keyValues.swap(iRHS.m_keyValues);
110 }
111 
112 //
113 // const member functions
114 //
115 const std::string& FWConfiguration::value(unsigned int iIndex) const {
116  if (not m_stringValues) {
117  throw std::runtime_error("no string values set");
118  }
119  return m_stringValues->at(iIndex);
120 }
121 
123  if (m_stringValues) {
124  throw std::runtime_error("valueFoKey fails because configuration containing string values");
125  }
126  if (not m_keyValues) {
127  throw std::runtime_error("valueForKey fails becuase no key/values set");
128  }
129  KeyValues::iterator itFind =
130  std::find_if(m_keyValues->begin(),
131  m_keyValues->end(),
132  std::bind(std::equal_to<void>(),
133  std::bind(&std::pair<std::string, FWConfiguration>::first, std::placeholders::_1),
134  iKey));
135  if (itFind == m_keyValues->end()) {
136  return nullptr;
137  }
138  return &(itFind->second);
139 }
140 
146  index = 0;
147  while (std::string::npos != (index = value.find('&', index))) {
148  value.replace(index, 1, "&amp;", 5);
149  // Do not check "&quot;" for quotes.
150  index += 5;
151  }
152 
153  while (std::string::npos != (index = value.find('"', index))) {
154  value.replace(index, 1, "&quot;", 6);
155  // Do not check "&quot;" for quotes.
156  index += 6;
157  }
158 
159  index = 0;
160  while (std::string::npos != (index = value.find('<', index))) {
161  value.replace(index, 1, "&lt;", 4);
162  // Do not check "&quot;" for quotes.
163  index += 4;
164  }
165 
166  index = 0;
167  while (std::string::npos != (index = value.find('>', index))) {
168  value.replace(index, 1, "&gt;", 4);
169  // Do not check "&quot;" for quotes.
170  index += 4;
171  }
172 
173  return value;
174 }
175 
196 void FWConfiguration::streamTo(std::ostream& oTo, const FWConfiguration& iConfig, const std::string& name) {
197  static int recursionLevel = -1;
198  recursionLevel += 1;
199  std::string indentation(recursionLevel, ' ');
200  oTo << indentation << "<config name=\"" << name << "\" version=\"" << iConfig.version() << "\">\n";
201  if (iConfig.stringValues()) {
202  for (FWConfiguration::StringValues::const_iterator it = iConfig.stringValues()->begin();
203  it != iConfig.stringValues()->end();
204  ++it) {
205  oTo << indentation << " <string>" << attrEscape(*it) << "</string>\n";
206  }
207  }
208  if (iConfig.keyValues()) {
209  for (FWConfiguration::KeyValues::const_iterator it = iConfig.keyValues()->begin(); it != iConfig.keyValues()->end();
210  ++it) {
211  FWConfiguration::streamTo(oTo, it->second, attrEscape(it->first));
212  }
213  }
214  oTo << indentation << "</config>" << std::endl;
215  recursionLevel -= 1;
216 }
217 
218 //
219 // static member functions
220 //
const StringValues * stringValues() const
FWConfiguration(unsigned int iVersion=1)
const KeyValues * keyValues() const
unsigned int version() const
FWConfiguration & operator=(const FWConfiguration &)
uint16_t size_type
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void swap(FWConfiguration &)
std::unique_ptr< std::vector< std::pair< std::string, FWConfiguration > > > m_keyValues
std::vector< std::pair< std::string, FWConfiguration > > KeyValues
FWConfiguration & addKeyValue(const std::string &, const FWConfiguration &)
const std::string & value(unsigned int iIndex=0) const
FWConfiguration & addValue(const std::string &)
std::string attrEscape(std::string value)
virtual ~FWConfiguration()
unsigned int m_version
const FWConfiguration * valueForKey(const std::string &iKey) const
std::unique_ptr< std::vector< std::string > > m_stringValues
static void streamTo(std::ostream &oTo, const FWConfiguration &iConfig, const std::string &name)