libzypp  13.10.6
NamedValue.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_NAMEDVALUE_H
13 #define ZYPP_BASE_NAMEDVALUE_H
14 
15 #include <stdexcept>
16 #include <string>
17 #include <map>
18 
20 namespace zypp
21 {
22 
40  template< class _Tp, const bool _WithAlias = true >
41  class NamedValue
42  {
43  typedef std::map< std::string, _Tp > NameMap;
44  typedef std::map< _Tp, std::string > ValueMap;
45 
46  public:
48  bool empty() const
49  { return( _nameMap.empty() && _valueMap.empty() ); }
50 
51  public:
56  bool haveValue( const std::string & name_r ) const
57  {
58  typename NameMap::const_iterator it( _nameMap.find( name_r ) );
59  return( it != _nameMap.end() );
60  }
61 
65  bool getValue( const std::string & name_r, _Tp & value_r ) const
66  {
67  typename NameMap::const_iterator it( _nameMap.find( name_r ) );
68  if ( it == _nameMap.end() )
69  return false;
70  value_r = it->second;
71  return true;
72  }
74  const _Tp & getValue( const std::string & name_r ) const
75  { return _nameMap.at( name_r ); }
77 
78 
83  bool haveName( const std::string & value_r ) const
84  {
85  typename ValueMap::const_iterator it( _valueMap.find( value_r ) );
86  return( it != _valueMap.end() );
87  }
88 
92  bool getName( const _Tp & value_r, std::string & name_r ) const
93  {
94  typename ValueMap::const_iterator it( _valueMap.find( value_r ) );
95  if ( it == _valueMap.end() )
96  return false;
97  value_r = it->second;
98  return true;
99  }
101  const std::string & getName( const _Tp & value_r ) const
102  { return _valueMap.at( value_r ); }
104 
105  public:
109  class _Inserter
110  {
111  public:
112  _Inserter( NamedValue & parent_r, const _Tp & value_r )
113  : _parent( &parent_r )
114  , _value( value_r )
115  {}
116  _Inserter & operator|( const std::string & name_r )
117  { _parent->insert( _value, name_r ); return *this; }
118  private:
120  _Tp _value;
121  };
122 
123  _Inserter operator()( const _Tp & value_r )
124  { return _Inserter( *this, value_r ); }
126 
132  bool insert( const _Tp & value_r, const std::string & name_r )
133  {
134  typename NameMap::const_iterator nit( _nameMap.find( name_r ) );
135  if ( nit != _nameMap.end() ) // duplicate name
136  throw std::logic_error( "NamedValue::insert name" );
137 
138  typename ValueMap::const_iterator tit( _valueMap.find( value_r ) );
139  if ( tit != _valueMap.end() ) // duplicate value, i.e. an alias
140  {
141  if ( !_WithAlias )
142  throw std::logic_error( "NamedValue::insert alias" );
143 
144  _nameMap[name_r] = value_r;
145  return false;
146  }
147  // here: 1st entry for value_r
148  _nameMap[name_r] = value_r;
149  _valueMap[value_r] = name_r;
150  return true;
151  }
152 
153  private:
156  };
158 
159 } // namespace zypp
161 #endif // ZYPP_BASE_NAMEDVALUE_H
const _Tp & getValue(const std::string &name_r) const
Definition: NamedValue.h:74
bool getValue(const std::string &name_r, _Tp &value_r) const
Get value mapped for name or alias.
Definition: NamedValue.h:65
std::map< _Tp, std::string > ValueMap
Definition: NamedValue.h:44
bool haveName(const std::string &value_r) const
Whether there is a name mapped for value_r.
Definition: NamedValue.h:83
NameMap _nameMap
Definition: NamedValue.h:154
_Inserter(NamedValue &parent_r, const _Tp &value_r)
Definition: NamedValue.h:112
bool getName(const _Tp &value_r, std::string &name_r) const
Get name of value.
Definition: NamedValue.h:92
const std::string & getName(const _Tp &value_r) const
Definition: NamedValue.h:101
_Inserter & operator|(const std::string &name_r)
Definition: NamedValue.h:116
_Inserter operator()(const _Tp &value_r)
Definition: NamedValue.h:123
bool insert(const _Tp &value_r, const std::string &name_r)
Remember name (1st call) or alias (subsequent calls).
Definition: NamedValue.h:132
bool empty() const
Whether not initialized (no (name,value) pair remembered)
Definition: NamedValue.h:48
bool haveValue(const std::string &name_r) const
Whether there is a value mapped for name_r.
Definition: NamedValue.h:56
ValueMap _valueMap
Definition: NamedValue.h:155
std::map< std::string, _Tp > NameMap
Definition: NamedValue.h:43