The Gaudi Framework  v30r3 (a5ef0a68)
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 { return boost::get<std::string>( &value_ ) != NULL; }
16 // ============================================================================
17 bool gp::PropertyValue::IsVector() const { return boost::get<std::vector<std::string>>( &value_ ) != NULL; }
18 // ============================================================================
19 bool gp::PropertyValue::IsMap() const { return boost::get<std::map<std::string, std::string>>( &value_ ) != NULL; }
20 // ============================================================================
21 gp::PropertyValue& gp::PropertyValue::operator+=( const PropertyValue& right )
22 {
23 
24  if ( IsSimple() || IsReference() ) {
25  throw PropertyValueException::WrongLValue();
26  }
27 
28  if ( IsVector() ) {
29  if ( right.IsSimple() ) {
30  boost::get<VectorOfStrings>( value_ ).push_back( boost::get<std::string>( right.value_ ) );
31  return *this;
32  }
33  if ( right.IsVector() ) {
34  VectorOfStrings& vec = boost::get<VectorOfStrings>( value_ );
35  for ( const auto& item : boost::get<VectorOfStrings>( right.value_ ) ) {
36  vec.push_back( item );
37  }
38  return *this;
39  }
40  throw PropertyValueException::WrongRValue();
41  }
42 
43  if ( IsMap() ) {
44  if ( !right.IsMap() ) {
45  throw PropertyValueException::WrongRValue();
46  }
47  MapOfStrings& map = boost::get<MapOfStrings>( value_ );
48  const MapOfStrings& rmap = boost::get<MapOfStrings>( right.value_ );
49  for ( const auto& item : rmap ) {
50  map.insert( item );
51  }
52  return *this;
53  }
54  return *this;
55 }
56 
57 const gp::PropertyValue gp::PropertyValue::operator+( const PropertyValue& right )
58 {
59  return PropertyValue( *this ) += right;
60 }
61 
62 gp::PropertyValue& gp::PropertyValue::operator-=( const PropertyValue& right )
63 {
64  if ( IsSimple() || IsReference() ) {
65  throw PropertyValueException::WrongLValue();
66  }
67 
68  if ( IsVector() ) {
69  VectorOfStrings& vec = Vector();
70  if ( right.IsSimple() ) {
71  vec.erase( std::find( vec.begin(), vec.end(), right.String() ) );
72  return *this;
73  }
74 
75  if ( right.IsVector() ) {
76  const VectorOfStrings& rvec = right.Vector();
77  for ( const auto& item : rvec ) {
78  vec.erase( std::find( vec.begin(), vec.end(), item ) );
79  }
80  return *this;
81  }
82  throw PropertyValueException::WrongRValue();
83  }
84 
85  if ( IsMap() ) {
86  MapOfStrings& map = Map();
87  if ( right.IsSimple() ) {
88  map.erase( right.String() );
89  return *this;
90  }
91 
92  if ( right.IsVector() ) {
93  const VectorOfStrings& rvec = right.Vector();
94  for ( const auto& item : rvec ) {
95  map.erase( item );
96  }
97  return *this;
98  }
99  throw PropertyValueException::WrongRValue();
100  }
101  throw PropertyValueException::WrongLValue();
102 }
103 
104 const gp::PropertyValue gp::PropertyValue::operator-( const PropertyValue& right )
105 {
106  return PropertyValue( *this ) -= right;
107 }
108 // ============================================================================
109 std::string gp::PropertyValue::ToString() const
110 {
111  if ( IsReference() ) {
112  const std::vector<std::string>* value = boost::get<std::vector<std::string>>( &value_ );
113  assert( value != NULL );
114  if ( value->at( 0 ) != "" ) {
115  return "@" + value->at( 0 ) + "." + value->at( 1 );
116  } else {
117  return "@" + value->at( 0 );
118  }
119  }
120  if ( const std::string* value = boost::get<std::string>( &value_ ) ) {
121  return *value;
122  } else if ( const std::vector<std::string>* value = boost::get<std::vector<std::string>>( &value_ ) ) {
123  std::string result = "[";
124  std::string delim = "";
125  for ( const auto& in : *value ) {
126  result += delim + in;
127  delim = ", ";
128  }
129  return result + "]";
130  } else if ( const std::map<std::string, std::string>* value =
132  std::string result = "{";
133  std::string delim = "";
134  for ( const auto& in : *value ) {
135  result += delim + in.first + ":" + in.second;
136  delim = ", ";
137  }
138  return result + "}";
139  }
140  assert( false );
141  // @todo Check the validity of this logic
142  return std::string(); // avoid compilation warning
143 }
T end(T...args)
void push_back(Container &c, const Value &v, std::true_type)
STL class.
T at(T...args)
T push_back(T...args)
struct GAUDI_API map
Parametrisation class for map-like implementation.
KeyedObjectManager< vector > Vector
Forward declaration of specialized std::vector-like object manager.
T erase(T...args)
VectorOfStrings & Vector()
Definition: PropertyValue.h:45
T insert(T...args)
T find(T...args)
T begin(T...args)
decltype(auto) operator+(const T &v, const Property< TP, V, H > &p)
implemantation of (value + property)
Definition: Property.h:757
GAUDI_API Stat operator-(const Stat &stat, const double value)
external operator for subtraction of Stat and a number
Definition: Stat.cpp:127
KeyedObjectManager< map > Map
Forward declaration of specialized std::map-like object manager.