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