All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PropertyValue.cpp
Go to the documentation of this file.
1 // $Id:$
2 // ============================================================================
3 // Include files
4 // ============================================================================
5 #include "PropertyValue.h"
6 // ============================================================================
7 // STD & STL:
8 // ============================================================================
9 // ============================================================================
10 // Boost:
11 // ============================================================================
12 #include <boost/format.hpp>
13 #include <boost/foreach.hpp>
14 // ============================================================================
15 namespace gp = Gaudi::Parsers;
16 // ============================================================================
17 bool gp::PropertyValue::IsSimple() const {
18  return boost::get<std::string>(&value_) != NULL;
19 }
20 // ============================================================================
21 bool gp::PropertyValue::IsVector() const {
22  return boost::get<std::vector<std::string> >(&value_) != NULL;
23 }
24 // ============================================================================
25 bool gp::PropertyValue::IsMap() const {
26  return boost::get<std::map<std::string, std::string> >(&value_) != NULL;
27 }
28 // ============================================================================
29 gp::PropertyValue&
30 gp::PropertyValue::operator += (const PropertyValue& right) {
31 
32  if (IsSimple() || IsReference()) {
33  throw PropertyValueException::WrongLValue();
34  }
35 
36  if (IsVector()) {
37  if (right.IsSimple()) {
38  boost::get<VectorOfStrings>(value_).push_back(
39  boost::get<std::string>(right.value_));
40  return *this;
41  }
42  if (right.IsVector()){
43  VectorOfStrings& vec = boost::get<VectorOfStrings>(value_);
44  BOOST_FOREACH(const std::string& item,
45  boost::get<VectorOfStrings>(right.value_)) {
46  vec.push_back(item);
47  }
48  return *this;
49  }
50  throw PropertyValueException::WrongRValue();
51  }
52 
53  if (IsMap()) {
54  if (!right.IsMap()) {
55  throw PropertyValueException::WrongRValue();
56  }
57  MapOfStrings& map = boost::get<MapOfStrings>(value_);
58  const MapOfStrings& rmap = boost::get<MapOfStrings>(right.value_);
59  BOOST_FOREACH(const MapOfStrings::value_type& item, rmap) {
60  map.insert(item);
61  }
62  return *this;
63  }
64  return *this;
65 }
66 
67 const gp::PropertyValue
69  return PropertyValue(*this) += right;
70 
71 }
72 
73 gp::PropertyValue&
74 gp::PropertyValue::operator-=(const PropertyValue& right) {
75  if (IsSimple() || IsReference()) {
76  throw PropertyValueException::WrongLValue();
77  }
78 
79  if (IsVector()) {
80  VectorOfStrings& vec = Vector();
81  if (right.IsSimple()) {
82  vec.erase(std::find(vec.begin(), vec.end(), right.String()));
83  return *this;
84  }
85 
86  if (right.IsVector()) {
87  const VectorOfStrings& rvec = right.Vector();
88  BOOST_FOREACH(const std::string& item, rvec) {
89  vec.erase(std::find(vec.begin(), vec.end(), item));
90  }
91  return *this;
92  }
93  throw PropertyValueException::WrongRValue();
94  }
95 
96  if (IsMap()) {
97  MapOfStrings& map = Map();
98  if (right.IsSimple()) {
99  map.erase(right.String());
100  return *this;
101  }
102 
103  if (right.IsVector()) {
104  const VectorOfStrings& rvec = right.Vector();
105  BOOST_FOREACH(const std::string& item, rvec) {
106  map.erase(item);
107  }
108  return *this;
109  }
110  throw PropertyValueException::WrongRValue();
111  }
112  throw PropertyValueException::WrongLValue();
113 }
114 
115 const gp::PropertyValue
117  return PropertyValue(*this) -= right;
118 }
119 // ============================================================================
120 std::string gp::PropertyValue::ToString() const {
121  if (IsReference()) {
122  const std::vector<std::string>*
123  value = boost::get<std::vector<std::string> >(&value_);
124  assert(value != NULL);
125  if (value->at(0) != "") {
126  return "@"+value->at(0)+"."+value->at(1);
127  } else {
128  return "@"+value->at(0);
129  }
130  }
131  if (const std::string* value = boost::get<std::string>(&value_)) {
132  return *value;
133  } else if (const std::vector<std::string>*
134  value = boost::get<std::vector<std::string> >(&value_)) {
135  std::string result = "[";
136  std::string delim = "";
137  BOOST_FOREACH(const std::string& in, *value) {
138  result += delim + in;
139  delim = ", ";
140  }
141  return result+"]";
142  } else if (const std::map<std::string, std::string>*
143  value = boost::get<std::map<std::string, std::string> >(&value_)) {
144  std::string result = "{";
145  std::string delim = "";
146  typedef std::pair<std::string, std::string> pair_t;
147  BOOST_FOREACH(const pair_t& in, *value) {
148  result += delim + in.first + ":" + in.second;
149  delim = ", ";
150  }
151  return result+"}";
152  }
153  assert(false);
154  // @todo Check the validity of this logic
155  return std::string(); // avoid compilation warning
156 }
157 
std::vector< std::string > VectorOfStrings
Definition: PropertyValue.h:29
std::map< std::string, std::string > MapOfStrings
Definition: PropertyValue.h:30
KeyedObjectManager< vector > Vector
Forward declaration of specialized std::vector-like object manager.
struct GAUDI_API map
Parametrisation class for map-like implementation.
VectorOfStrings & Vector()
Definition: PropertyValue.h:44
tuple item
print s1,s2
Definition: ana.py:146
GAUDI_API Stat operator+(const Stat &stat, const double value)
external operator for addition of Stat and a number
Definition: Stat.cpp:217
GAUDI_API Stat operator-(const Stat &stat, const double value)
external operator for subtraction of Stat and a number
Definition: Stat.cpp:222
KeyedObjectManager< map > Map
Forward declaration of specialized std::map-like object manager.