- Class to replace the strings "yum" and "yast" everywhere.
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Mon, 4 Jun 2007 14:23:47 +0000 (14:23 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Mon, 4 Jun 2007 14:23:47 +0000 (14:23 +0000)
- They are safer, as can represent more than one string rep.
- can be used in switches.

zypp2/CMakeLists.txt
zypp2/repo/RepoType.cc [new file with mode: 0644]
zypp2/repo/RepoType.h [new file with mode: 0644]

index f1d9165..556cfae 100644 (file)
@@ -79,12 +79,14 @@ SET( zypp2_repository_SRCS
   repo/dummy.cc
   repo/RepositoryImpl.cc
   repo/RepoException.cc
+  repo/RepoType.cc
 )
 
 SET( zypp2_repository_HEADERS
   repo/dummy.h
   repo/RepositoryImpl.h
   repo/RepoException.h
+  repo/RepoType.h
 )
 
 SET( zypp2_repository_cached_SRCS
diff --git a/zypp2/repo/RepoType.cc b/zypp2/repo/RepoType.cc
new file mode 100644 (file)
index 0000000..5df9e99
--- /dev/null
@@ -0,0 +1,72 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+
+#include <map>
+#include "zypp/base/Exception.h"
+#include "RepoType.h"
+
+namespace zypp
+{
+namespace repo
+{
+
+  static std::map<std::string,RepoType::Type> _table;
+
+  const RepoType RepoType::RPMMD(RepoType::RPMMD_e);
+  const RepoType RepoType::YAST2(RepoType::YAST2_e);
+  //const RepoType RepoType::NONE(RepoType::NONE_e);
+
+  RepoType::RepoType(const std::string & strval_r)
+    : _type(parse(strval_r))
+  {}
+
+  RepoType::Type RepoType::parse(const std::string & strval_r)
+  {
+    if (_table.empty())
+    {
+      // initialize it
+      _table["repomd"] = RepoType::RPMMD_e;
+      _table["rpmmd"] = RepoType::RPMMD_e;
+      _table["rpm-md"] = RepoType::RPMMD_e;
+      _table["yum"] = RepoType::RPMMD_e;
+      _table["susetags"] = RepoType::YAST2_e;
+      _table["yast"] = RepoType::YAST2_e;
+      _table["yast2"] = RepoType::YAST2_e;
+      _table["NONE"] = _table["none"] = RepoType::NONE_e;
+    }
+
+    std::map<std::string,RepoType::Type>::const_iterator it
+      = _table.find(strval_r);
+    if (it == _table.end())
+    {
+      ZYPP_THROW(Exception(
+        "RepoType parse: illegal string value '" + strval_r + "'"));
+    }
+    return it->second;
+  }
+
+
+  const std::string & RepoType::asString() const
+  {
+    static std::map<Type, std::string> _table;
+    if ( _table.empty() )
+    {
+      // initialize it
+      _table[RPMMD_e]   = "rpm-md";
+      _table[YAST2_e]   = "yast2";
+      _table[NONE_e] = "NONE";
+    }
+    return _table[_type];
+  }
+
+
+  } // ns repo
+} // ns zypp
+
+// vim: set ts=2 sts=2 sw=2 et ai:
diff --git a/zypp2/repo/RepoType.h b/zypp2/repo/RepoType.h
new file mode 100644 (file)
index 0000000..e355bdf
--- /dev/null
@@ -0,0 +1,66 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+
+#ifndef ZYPP_REPO_TYPE_H_
+#define ZYPP_REPO_TYPE_H_
+
+#include <iosfwd>
+#include<string>
+
+namespace zypp
+{
+  namespace repo
+  {
+  
+  /**
+   * \short Repository type enumeration
+   *
+   * Repositories can be from varous types
+   * ...
+   */
+  struct RepoType
+  {
+    static const RepoType RPMMD;
+    static const RepoType YAST2;
+    //static const RepoType NONE;
+    
+    enum Type
+    {
+      NONE_e,
+      RPMMD_e,
+      YAST2_e
+    };
+
+    RepoType(Type type) : _type(type) {}
+
+    explicit RepoType(const std::string & strval_r);
+
+    const Type toEnum() const { return _type; }
+    
+    RepoType::Type parse(const std::string & strval_r);
+
+    const std::string & asString() const;
+
+    Type _type;
+  };
+
+
+  inline std::ostream & operator<<( std::ostream & str, const RepoType & obj )
+  { return str << obj.asString(); }
+
+  inline bool operator==(const RepoType & obj1, const RepoType & obj2)
+  { return obj1._type == obj2._type; }
+
+
+  } // ns repo
+} // ns zypp
+
+#endif
+
+// vim: set ts=2 sts=2 sw=2 et ai: