1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
18 ///////////////////////////////////////////////////////////////////
20 { /////////////////////////////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////
26 /** Relational operators.
27 * Yes, it could as well be simply an \c enum.<BR>
28 * Yes, you can use the relational operators as if it was an \c enum.<BR>
29 * Except for use in a \c switch statement; see \ref inSwitch for this.
31 * But we want to construct them from a string representation, as well as
32 * providing one. And this way they are wrapped into a namespace, which is
35 * \ref ANY and \ref NONE are somewhat special. \ref ANY is the
36 * operator created by the default ctor, and it should always resolve
37 * to \c true. This may be handy in queries when you're looking for a
38 * Resolvable in \c ANY Edition if no operator was specified.
39 * While \ref NONE should always resolve to \c false.
41 * \ingroup g_EnumerationClass
45 /** \name Relational operators
46 * These are the \em real relational operator contants to
47 * use. Don't mind that it's not an enum. See also: \ref zypp::Rel::inSwitch
57 static const Rel NONE;
60 /** Enumarators provided \b only for use \ref inSwitch statement.
62 * \note Enumarator values also correspond to the values libsolv
63 * uses to encode these relations.
65 enum for_use_in_switch {
73 ANY_e = GT_e|EQ_e|LT_e,
76 /** DefaultCtor ANY. */
82 * Legal values for \a strval_r are: "==", "!=", "<", "<=", ">", ">=",<BR>
83 * as well as "EQ", "NE", "LT", "LE", "GT", "GE", "ANY", "NONE"<BR>
84 * and "" (empty string resolves to ANY).
86 * Lower case names are accepted as well.
88 * \throw PARSE if \a strval_r is not legal.
89 * \todo refine exceptions and check throw.
92 Rel( const std::string & strval_r );
94 /** Ctor from string (non-throwing).
95 * Illegal string values resolve to \c default_r
97 Rel( const std::string & strval_r, const Rel & default_r );
99 /** Assign from string IFF it contains a legal value.
100 * \return Whether \a strval_r contained a legal value.
102 bool parseFrom( const std::string & strval_r );
104 /** Ctor from bits. */
106 Rel( unsigned bits_r )
107 : _op( for_use_in_switch(bits_r & ANY_e) )
110 /** Test whether \a bits_r is a valid \ref Rel (no extra bits set). */
111 static bool isRel( unsigned bits_r )
112 { return (bits_r & ANY_e) == bits_r; }
114 /** String representation of relational operator.
115 * \return "==", "!=", "<", "<=", ">", ">=", "ANY" or "NONE"
117 const std::string & asString() const;
119 const char * c_str() const
120 { return asString().c_str(); }
122 /** Enumarator provided for use in \c switch statement.
123 * The sole reason for providing enum \ref for_use_in_switch is,
124 * that we may want to use the relational operators in a \c switch
125 * statement. Tht's the only case where you should have to use the
129 * switch ( op.inSwitch() )
137 * // No default! Let compiler warn if case is missing
141 for_use_in_switch inSwitch() const
144 /** Enumarator values suitable for libsolv. */
145 unsigned bits() const
149 /** Ctor to initialize the relational operator contants. */
150 Rel( for_use_in_switch op_r )
154 for_use_in_switch _op;
156 ///////////////////////////////////////////////////////////////////
158 /** \relates Rel Stream output. */
159 inline std::ostream & operator<<( std::ostream & str, const Rel & obj )
160 { return str << obj.asString(); }
162 ///////////////////////////////////////////////////////////////////
165 inline bool operator==( const Rel & lhs, const Rel & rhs )
166 { return lhs.inSwitch() == rhs.inSwitch(); }
169 inline bool operator!=( const Rel & lhs, const Rel & rhs )
170 { return ! ( lhs == rhs ); }
172 /////////////////////////////////////////////////////////////////
174 ///////////////////////////////////////////////////////////////////