libzypp  15.28.6
Rel.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <map>
14 
15 #include "zypp/base/Exception.h"
16 
17 #include "zypp/Rel.h"
18 
20 namespace zypp
21 {
22 
23  namespace
24  {
25  std::map<std::string,Rel::for_use_in_switch> _table;
26 
27  std::map<std::string,Rel::for_use_in_switch>::const_iterator findStr( const std::string & strval_r )
28  {
29  if ( _table.empty() )
30  {
31  // initialize it
32  _table["EQ"] = _table["eq"] = _table["=="] = _table["="] = Rel::EQ_e;
33  _table["NE"] = _table["ne"] = _table["!="] = Rel::NE_e;
34  _table["LT"] = _table["lt"] = _table["<"] = Rel::LT_e;
35  _table["LE"] = _table["le"] = _table["lte"] = _table["<="] = Rel::LE_e;
36  _table["GT"] = _table["gt"] = _table[">"] = Rel::GT_e;
37  _table["GE"] = _table["ge"] = _table["gte"] = _table[">="] = Rel::GE_e;
38  _table["ANY"] = _table["any"] = _table["(any)"] = _table[""] = Rel::ANY_e;
39  _table["NONE"] = _table["none"] = Rel::NONE_e;
40  }
41 
42  return _table.find( strval_r );
43  }
44 
45  Rel::for_use_in_switch parse( const std::string & strval_r )
46  {
47  std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r );
48  if ( it == _table.end() )
49  {
50  ZYPP_THROW( Exception("Rel parse: illegal string value '"+strval_r+"'") );
51  }
52  return it->second;
53  }
54 
55  Rel::for_use_in_switch parse( const std::string & strval_r, const Rel & default_r )
56  {
57  std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r );
58  if ( it == _table.end() )
59  {
60  return default_r.inSwitch();
61  }
62  return it->second;
63  }
64  }
66 
67  const Rel Rel::EQ( Rel::EQ_e );
68  const Rel Rel::NE( Rel::NE_e );
69  const Rel Rel::LT( Rel::LT_e );
70  const Rel Rel::LE( Rel::LE_e );
71  const Rel Rel::GT( Rel::GT_e );
72  const Rel Rel::GE( Rel::GE_e );
73  const Rel Rel::ANY( Rel::ANY_e );
74  const Rel Rel::NONE( Rel::NONE_e );
75 
77  //
78  // METHOD NAME : Rel::Rel
79  // METHOD TYPE : Constructor
80  //
81  Rel::Rel( const std::string & strval_r )
82  : _op( parse( strval_r ) )
83  {}
84 
85  Rel::Rel( const std::string & strval_r, const Rel & default_r )
86  : _op( parse( strval_r, default_r ) )
87  {}
88 
89  bool Rel::parseFrom( const std::string & strval_r )
90  {
91  std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r );
92  if ( it == _table.end() )
93  {
94  return false;
95  }
96  _op = it->second;
97  return true;
98  }
99 
101  //
102  // METHOD NAME : Rel::asString
103  // METHOD TYPE : const std::string &
104  //
105  const std::string & Rel::asString() const
106  {
107  static std::map<for_use_in_switch,std::string> _table;
108  if ( _table.empty() )
109  {
110  // initialize it
111  _table[EQ_e] = "==";
112  _table[NE_e] = "!=";
113  _table[LT_e] = "<";
114  _table[LE_e] = "<=";
115  _table[GT_e] = ">";
116  _table[GE_e] = ">=";
117  _table[ANY_e] = "ANY";
118  _table[NONE_e] = "NONE";
119  }
120  return _table[_op];
121  }
122 
124 } // namespace zypp
static const Rel NE
Definition: Rel.h:51
static const Rel LT
Definition: Rel.h:52
static const Rel GT
Definition: Rel.h:54
static std::map< std::string, ServiceType::Type > _table
Definition: ServiceType.cc:21
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:321
Relational operators.
Definition: Rel.h:43
static const Rel EQ
Definition: Rel.h:50
static const Rel LE
Definition: Rel.h:53
static const Rel ANY
Definition: Rel.h:56
static const Rel GE
Definition: Rel.h:55
const std::string & asString() const
String representation of relational operator.
Definition: Rel.cc:105
for_use_in_switch _op
The operator.
Definition: Rel.h:154
Rel()
DefaultCtor ANY.
Definition: Rel.h:77
for_use_in_switch
Enumarators provided only for use inSwitch statement.
Definition: Rel.h:65
bool parseFrom(const std::string &strval_r)
Assign from string IFF it contains a legal value.
Definition: Rel.cc:89
static const Rel NONE
Definition: Rel.h:57