Add a non-throwing way of Rel construction
authorMichael Andres <ma@suse.de>
Fri, 7 Dec 2007 16:10:47 +0000 (16:10 +0000)
committerMichael Andres <ma@suse.de>
Fri, 7 Dec 2007 16:10:47 +0000 (16:10 +0000)
zypp/Rel.cc
zypp/Rel.h

index 7c9983f..089ca7c 100644 (file)
@@ -22,34 +22,46 @@ namespace zypp
 
   namespace
   {
-
     std::map<std::string,Rel::for_use_in_switch> _table;
 
-    Rel::for_use_in_switch parse( const std::string & strval_r )
+    std::map<std::string,Rel::for_use_in_switch>::const_iterator findStr( const std::string & strval_r )
     {
       if ( _table.empty() )
-        {
-          // initialize it
-          _table["EQ"]   = _table["eq"]  = _table["=="]    = _table["="] = Rel::EQ_e;
-          _table["NE"]   = _table["ne"]  = _table["!="]                  = Rel::NE_e;
-          _table["LT"]   = _table["lt"]  = _table["<"]                   = Rel::LT_e;
-          _table["LE"]   = _table["le"]  = _table["lte"]  = _table["<="] = Rel::LE_e;
-          _table["GT"]   = _table["gt"]  = _table[">"]                   = Rel::GT_e;
-          _table["GE"]   = _table["ge"]  = _table["gte"]  = _table[">="] = Rel::GE_e;
-          _table["ANY"]  = _table["any"] = _table["(any)"] = _table[""]  = Rel::ANY_e;
-          _table["NONE"] = _table["none"]                                = Rel::NONE_e;
-        }
+      {
+        // initialize it
+        _table["EQ"]   = _table["eq"]  = _table["=="]    = _table["="] = Rel::EQ_e;
+        _table["NE"]   = _table["ne"]  = _table["!="]                  = Rel::NE_e;
+        _table["LT"]   = _table["lt"]  = _table["<"]                   = Rel::LT_e;
+        _table["LE"]   = _table["le"]  = _table["lte"]  = _table["<="] = Rel::LE_e;
+        _table["GT"]   = _table["gt"]  = _table[">"]                   = Rel::GT_e;
+        _table["GE"]   = _table["ge"]  = _table["gte"]  = _table[">="] = Rel::GE_e;
+        _table["ANY"]  = _table["any"] = _table["(any)"] = _table[""]  = Rel::ANY_e;
+        _table["NONE"] = _table["none"]                                = Rel::NONE_e;
+      }
 
-      std::map<std::string,Rel::for_use_in_switch>::const_iterator it
-      = _table.find( strval_r );
+      return _table.find( strval_r );
+    }
+
+    Rel::for_use_in_switch parse( const std::string & strval_r )
+    {
+      std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r );
       if ( it == _table.end() )
-        {
-          ZYPP_THROW( Exception("Rel parse: illegal string value '"+strval_r+"'") );
-        }
+      {
+        ZYPP_THROW( Exception("Rel parse: illegal string value '"+strval_r+"'") );
+      }
       return it->second;
     }
-  }
 
+    Rel::for_use_in_switch parse( const std::string & strval_r, const Rel & default_r )
+    {
+      std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r );
+      if ( it == _table.end() )
+      {
+        return default_r.inSwitch();
+      }
+      return it->second;
+    }
+  }
   ///////////////////////////////////////////////////////////////////
 
   const Rel Rel::EQ( Rel::EQ_e );
@@ -70,6 +82,10 @@ namespace zypp
   : _op( parse( strval_r ) )
   {}
 
+  Rel::Rel( const std::string & strval_r, const Rel & default_r )
+  : _op( parse( strval_r, default_r ) )
+  {}
+
   ///////////////////////////////////////////////////////////////////
   //
   //   METHOD NAME : Rel::asString
index d3aa96f..9866780 100644 (file)
@@ -91,6 +91,11 @@ namespace zypp
     explicit
     Rel( const std::string & strval_r );
 
+    /** Ctor from string (non-throwing).
+     * Illegal string values resolve to \c default_r
+    */
+    Rel( const std::string & strval_r, const Rel & default_r );
+
     /** Ctor from bits. */
     explicit
     Rel( unsigned bits_r )