libzypp  17.14.0
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 _tWithAlias = 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 TInserter
110  {
111  public:
112  TInserter( NamedValue & parent_r, const Tp & value_r )
113  : _parent( &parent_r )
114  , _value( value_r )
115  {}
116  TInserter & operator|( const std::string & name_r )
117  { _parent->insert( _value, name_r ); return *this; }
118  private:
120  Tp _value;
121  };
122 
123  TInserter operator()( const Tp & value_r )
124  { return TInserter( *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 ( !_tWithAlias )
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
ValueMap _valueMap
Definition: NamedValue.h:155
TInserter operator()(const Tp &value_r)
Definition: NamedValue.h:123
bool haveValue(const std::string &name_r) const
Whether there is a value mapped for name_r.
Definition: NamedValue.h:56
const Tp & getValue(const std::string &name_r) const
Definition: NamedValue.h:74
TInserter(NamedValue &parent_r, const Tp &value_r)
Definition: NamedValue.h:112
std::map< std::string, Tp > NameMap
Definition: NamedValue.h:43
std::map< Tp, std::string > ValueMap
Definition: NamedValue.h:44
NameMap _nameMap
Definition: NamedValue.h:154
const std::string & getName(const Tp &value_r) const
Definition: NamedValue.h:101
bool insert(const Tp &value_r, const std::string &name_r)
Remember name (1st call) or alias (subsequent calls).
Definition: NamedValue.h:132
bool haveName(const std::string &value_r) const
Whether there is a name mapped for value_r.
Definition: NamedValue.h:83
bool getName(const Tp &value_r, std::string &name_r) const
Get name of value.
Definition: NamedValue.h:92
bool getValue(const std::string &name_r, Tp &value_r) const
Get value mapped for name or alias.
Definition: NamedValue.h:65
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
TInserter & operator|(const std::string &name_r)
Definition: NamedValue.h:116
bool empty() const
Whether not initialized (no (name,value) pair remembered)
Definition: NamedValue.h:48
Simple value<>name mapping supporting aliases.
Definition: NamedValue.h:41