CMS 3D CMS Logo

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 <stdexcept>
15 #include <algorithm>
16 #include <boost/bind.hpp>
17 
18 // user include files
20 
21 //
22 // constants, enums and typedefs
23 //
24 
25 //
26 // static data member definitions
27 //
28 
29 //
30 // constructors and destructor
31 //
32 //FWConfiguration::FWConfiguration()
33 //{
34 //}
35 
37  : m_stringValues(rhs.m_stringValues ? new std::vector<std::string>(*(rhs.m_stringValues)) : nullptr),
38  m_keyValues(rhs.m_keyValues ? new KeyValues(*(rhs.m_keyValues)) : nullptr),
39  m_version(rhs.m_version) {}
40 
42 
43 //
44 // assignment operators
45 //
47  //An exception safe implementation is
48  FWConfiguration temp(rhs);
49  swap(temp);
50 
51  return *this;
52 }
53 
54 //
55 // member functions
56 //
58  if (m_stringValues) {
59  throw std::runtime_error("adding key/value to configuration containing string values");
60  }
61  if (not m_keyValues) {
62  m_keyValues.reset(new KeyValues(1, std::make_pair(iKey, iConfig)));
63  } else {
64  m_keyValues->push_back(std::make_pair(iKey, iConfig));
65  }
66  return *this;
67 }
69  if (m_stringValues) {
70  throw std::runtime_error("adding key/value to configuration containing string values");
71  }
72  if (m_stringValues) {
73  throw std::runtime_error("adding key/value to configuration containing string values");
74  }
75  if (not m_keyValues) {
76  if (not iDoSwap) {
77  m_keyValues.reset(new KeyValues(1, std::make_pair(iKey, iConfig)));
78  } else {
79  m_keyValues.reset(new KeyValues(1, std::make_pair(iKey, FWConfiguration())));
80  m_keyValues->back().second.swap(iConfig);
81  }
82  } else {
83  if (not iDoSwap) {
84  m_keyValues->push_back(std::make_pair(iKey, iConfig));
85  } else {
86  m_keyValues->push_back(std::make_pair(iKey, FWConfiguration()));
87  m_keyValues->back().second.swap(iConfig);
88  }
89  }
90  return *this;
91 }
92 
94  if (m_keyValues) {
95  throw std::runtime_error("adding string value to configuration containing key/value pairs");
96  }
97  if (not m_stringValues) {
98  m_stringValues.reset(new std::vector<std::string>(1, iValue));
99  } else {
100  m_stringValues->push_back(iValue);
101  }
102  return *this;
103 }
104 
107  m_stringValues.swap(iRHS.m_stringValues);
108  m_keyValues.swap(iRHS.m_keyValues);
109 }
110 
111 //
112 // const member functions
113 //
114 const std::string& FWConfiguration::value(unsigned int iIndex) const {
115  if (not m_stringValues) {
116  throw std::runtime_error("no string values set");
117  }
118  return m_stringValues->at(iIndex);
119 }
120 
122  if (m_stringValues) {
123  throw std::runtime_error("valueFoKey fails because configuration containing string values");
124  }
125  if (not m_keyValues) {
126  throw std::runtime_error("valueForKey fails becuase no key/values set");
127  }
128  KeyValues::iterator itFind = std::find_if(m_keyValues->begin(),
129  m_keyValues->end(),
130  boost::bind(&std::pair<std::string, FWConfiguration>::first, _1) == iKey);
131  if (itFind == m_keyValues->end()) {
132  return nullptr;
133  }
134  return &(itFind->second);
135 }
136 
142  index = 0;
143  while (std::string::npos != (index = value.find('&', index))) {
144  value.replace(index, 1, "&amp;", 5);
145  // Do not check "&quot;" for quotes.
146  index += 5;
147  }
148 
149  while (std::string::npos != (index = value.find('"', index))) {
150  value.replace(index, 1, "&quot;", 6);
151  // Do not check "&quot;" for quotes.
152  index += 6;
153  }
154 
155  index = 0;
156  while (std::string::npos != (index = value.find('<', index))) {
157  value.replace(index, 1, "&lt;", 4);
158  // Do not check "&quot;" for quotes.
159  index += 4;
160  }
161 
162  index = 0;
163  while (std::string::npos != (index = value.find('>', index))) {
164  value.replace(index, 1, "&gt;", 4);
165  // Do not check "&quot;" for quotes.
166  index += 4;
167  }
168 
169  return value;
170 }
171 
192 void FWConfiguration::streamTo(std::ostream& oTo, const FWConfiguration& iConfig, const std::string& name) {
193  static int recursionLevel = -1;
194  recursionLevel += 1;
195  std::string indentation(recursionLevel, ' ');
196  oTo << indentation << "<config name=\"" << name << "\" version=\"" << iConfig.version() << "\">\n";
197  if (iConfig.stringValues()) {
198  for (FWConfiguration::StringValues::const_iterator it = iConfig.stringValues()->begin();
199  it != iConfig.stringValues()->end();
200  ++it) {
201  oTo << indentation << " <string>" << attrEscape(*it) << "</string>\n";
202  }
203  }
204  if (iConfig.keyValues()) {
205  for (FWConfiguration::KeyValues::const_iterator it = iConfig.keyValues()->begin(); it != iConfig.keyValues()->end();
206  ++it) {
207  FWConfiguration::streamTo(oTo, it->second, attrEscape(it->first));
208  }
209  }
210  oTo << indentation << "</config>" << std::endl;
211  recursionLevel -= 1;
212 }
213 
214 //
215 // static member functions
216 //
FWConfiguration::streamTo
static void streamTo(std::ostream &oTo, const FWConfiguration &iConfig, const std::string &name)
Definition: FWConfiguration.cc:192
FWConfiguration::swap
void swap(FWConfiguration &)
Definition: FWConfiguration.cc:105
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
FWConfiguration::KeyValues
std::vector< std::pair< std::string, FWConfiguration > > KeyValues
Definition: FWConfiguration.h:40
dqmdumpme.first
first
Definition: dqmdumpme.py:55
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
std::swap
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
Definition: DataFrameContainer.h:209
FWConfiguration::m_version
unsigned int m_version
Definition: FWConfiguration.h:69
FWConfiguration::valueForKey
const FWConfiguration * valueForKey(const std::string &iKey) const
Definition: FWConfiguration.cc:121
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
FWConfiguration::value
const std::string & value(unsigned int iIndex=0) const
Definition: FWConfiguration.cc:114
FWConfiguration::addValue
FWConfiguration & addValue(const std::string &)
Definition: FWConfiguration.cc:93
FWConfiguration::operator=
FWConfiguration & operator=(const FWConfiguration &)
Definition: FWConfiguration.cc:46
value
Definition: value.py:1
FWConfiguration::version
unsigned int version() const
Definition: FWConfiguration.h:49
FWConfiguration::addKeyValue
FWConfiguration & addKeyValue(const std::string &, const FWConfiguration &)
Definition: FWConfiguration.cc:57
FWConfiguration::FWConfiguration
FWConfiguration(unsigned int iVersion=1)
Definition: FWConfiguration.h:33
std
Definition: JetResolutionObject.h:76
relativeConstraints.value
value
Definition: relativeConstraints.py:53
FWConfiguration::keyValues
const KeyValues * keyValues() const
Definition: FWConfiguration.h:51
FWConfiguration::m_keyValues
std::unique_ptr< std::vector< std::pair< std::string, FWConfiguration > > > m_keyValues
Definition: FWConfiguration.h:68
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
printContent_cfi.indentation
indentation
Definition: printContent_cfi.py:10
FWConfiguration::m_stringValues
std::unique_ptr< std::vector< std::string > > m_stringValues
Definition: FWConfiguration.h:67
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
FWConfiguration::stringValues
const StringValues * stringValues() const
Definition: FWConfiguration.h:52
FWConfiguration
Definition: FWConfiguration.h:31
attrEscape
std::string attrEscape(std::string value)
Definition: FWConfiguration.cc:140
FWConfiguration.h
FWConfiguration::~FWConfiguration
virtual ~FWConfiguration()
Definition: FWConfiguration.cc:41