libzypp 17.37.10

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 using NameMap = std::map<std::string, Tp>;
44 using ValueMap = std::map<Tp, std::string>;
45
46 public:
48 bool empty() const
49 { return( _nameMap.empty() && _valueMap.empty() ); }
50
51 public:
54
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 }
73
74 const Tp & getValue( const std::string & name_r ) const
75 { return _nameMap.at( name_r ); }
76
77
78
81
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 }
100
101 const std::string & getName( const Tp & value_r ) const
102 { return _valueMap.at( value_r ); }
103
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:
119 NamedValue * _parent;
120 Tp _value;
121 };
122
123 TInserter operator()( const Tp & value_r )
124 { return TInserter( *this, value_r ); }
125
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:
154 NameMap _nameMap;
155 ValueMap _valueMap;
156 };
157
158
159} // namespace zypp
161#endif // ZYPP_BASE_NAMEDVALUE_H
zypp::NamedValue::TInserter
Definition NamedValue.h:110
zypp::NamedValue::TInserter::TInserter
TInserter(NamedValue &parent_r, const Tp &value_r)
Definition NamedValue.h:112
zypp::NamedValue::TInserter::_value
Tp _value
Definition NamedValue.h:120
zypp::NamedValue::TInserter::_parent
NamedValue * _parent
Definition NamedValue.h:119
zypp::NamedValue::TInserter::operator|
TInserter & operator|(const std::string &name_r)
Definition NamedValue.h:116
zypp::NamedValue
Simple value<>name mapping supporting aliases.
Definition NamedValue.h:42
zypp::NamedValue::operator()
TInserter operator()(const Tp &value_r)
Definition NamedValue.h:123
zypp::NamedValue::ValueMap
std::map< Tp, std::string > ValueMap
Definition NamedValue.h:44
zypp::NamedValue::empty
bool empty() const
Whether not initialized (no (name,value) pair remembered)
Definition NamedValue.h:48
zypp::NamedValue::NameMap
std::map< std::string, Tp > NameMap
Definition NamedValue.h:43
zypp::NamedValue::getValue
bool getValue(const std::string &name_r, Tp &value_r) const
Get value mapped for name or alias.
Definition NamedValue.h:65
zypp::NamedValue::_nameMap
NameMap _nameMap
Definition NamedValue.h:154
zypp::NamedValue::getValue
const Tp & getValue(const std::string &name_r) const
Definition NamedValue.h:74
zypp::NamedValue::haveName
bool haveName(const std::string &value_r) const
Whether there is a name mapped for value_r.
Definition NamedValue.h:83
zypp::NamedValue::getName
const std::string & getName(const Tp &value_r) const
Definition NamedValue.h:101
zypp::NamedValue::haveValue
bool haveValue(const std::string &name_r) const
Whether there is a value mapped for name_r.
Definition NamedValue.h:56
zypp::NamedValue::insert
bool insert(const Tp &value_r, const std::string &name_r)
Remember name (1st call) or alias (subsequent calls).
Definition NamedValue.h:132
zypp::NamedValue::getName
bool getName(const Tp &value_r, std::string &name_r) const
Get name of value.
Definition NamedValue.h:92
zypp::NamedValue::_valueMap
ValueMap _valueMap
Definition NamedValue.h:155
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20