UniqueString.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_BASE_UNIQUESTRING_H
00013 #define ZYPP_BASE_UNIQUESTRING_H
00014 
00015 #include <iosfwd>
00016 #include <string>
00017 
00018 #include <zypp/base/Hash.h>
00019 
00021 namespace zypp
00022 { 
00023 
00024   namespace base
00025   { 
00026 
00028     //
00029     //  CLASS NAME : UniqueString
00030     //
00078     template<class _Derived>
00079         class UniqueString
00080     {
00081     protected:
00083       UniqueString()
00084       {}
00085 
00087       UniqueString( const std::string & name_r )
00088       {
00089         if ( !name_r.empty() )
00090         {
00091           _name = *(this->hash().insert( name_r ).first);
00092         }
00093       }
00094 
00096       virtual ~UniqueString()
00097       {}
00098 
00099     public:
00101       const std::string & asString() const
00102       { return _name; }
00103 
00105       const std::string & str() const
00106       { return asString(); }
00107 
00109       operator const std::string & () const
00110       { return asString(); }
00111 
00113       std::string::size_type size() const
00114       { return asString().size(); }
00115 
00117       bool empty() const
00118       { return asString().empty(); }
00119 
00121       int compare( const std::string & rhs ) const
00122       { return asString().compare( rhs ); }
00123 
00124     public:
00125       typedef hash_set<std::string>::size_type size_type;
00126       typedef hash_set<std::string>::const_iterator const_iterator;
00127 
00129       static bool allEmpty()
00130       { return hash().empty(); }
00131 
00133       static size_type allSize()
00134       { return hash().size(); }
00135 
00137       static const_iterator allBegin()
00138       { return hash().begin(); }
00139 
00141       static const_iterator allEnd()
00142       { return hash().end(); }
00143 
00144     private:
00146       static hash_set<std::string> & hash()
00147       {
00148         static hash_set<std::string> _value;
00149         return _value;
00150       }
00151 
00152     private:
00154       std::string _name;
00155     };
00157 
00159     template<class _Derived>
00160         inline bool operator==( const UniqueString<_Derived> & lhs, const UniqueString<_Derived> & rhs )
00161     { return ( lhs.str() == rhs.str() ); }
00163     template<class _Derived>
00164         inline bool operator==( const UniqueString<_Derived> & lhs, const std::string & rhs )
00165     { return ( lhs.str() == rhs ); }
00167     template<class _Derived>
00168         inline bool operator==( const std::string & lhs, const UniqueString<_Derived> & rhs )
00169     { return ( lhs == rhs.str() ); }
00170 
00171 
00173     template<class _Derived>
00174         inline bool operator!=( const UniqueString<_Derived> & lhs, const UniqueString<_Derived> & rhs )
00175     { return ( lhs.str() != rhs.str() ); }
00177     template<class _Derived>
00178         inline bool operator!=( const UniqueString<_Derived> & lhs, const std::string & rhs )
00179     { return ( lhs.str() != rhs ); }
00181     template<class _Derived>
00182         inline bool operator!=( const std::string & lhs, const UniqueString<_Derived> & rhs )
00183     { return ( lhs != rhs.str() ); }
00184 
00185 
00187     template<class _Derived>
00188         inline bool operator<( const UniqueString<_Derived> & lhs, const UniqueString<_Derived> & rhs )
00189     { return ( lhs.str() < rhs.str() ); }
00191     template<class _Derived>
00192         inline bool operator<( const UniqueString<_Derived> & lhs, const std::string & rhs )
00193     { return ( lhs.str() < rhs ); }
00195     template<class _Derived>
00196         inline bool operator<( const std::string & lhs, const UniqueString<_Derived> & rhs )
00197     { return ( lhs < rhs.str() ); }
00198 
00199 
00201     template<class _Derived>
00202         inline bool operator>( const UniqueString<_Derived> & lhs, const UniqueString<_Derived> & rhs )
00203     { return ( lhs.str() > rhs.str() ); }
00205     template<class _Derived>
00206         inline bool operator>( const UniqueString<_Derived> & lhs, const std::string & rhs )
00207     { return ( lhs.str() > rhs ); }
00209     template<class _Derived>
00210         inline bool operator>( const std::string & lhs, const UniqueString<_Derived> & rhs )
00211     { return ( lhs > rhs.str() ); }
00212 
00213 
00215     template<class _Derived>
00216         inline bool operator<=( const UniqueString<_Derived> & lhs, const UniqueString<_Derived> & rhs )
00217     { return ( lhs.str() <= rhs.str() ); }
00219     template<class _Derived>
00220         inline bool operator<=( const UniqueString<_Derived> & lhs, const std::string & rhs )
00221     { return ( lhs.str() <= rhs ); }
00223     template<class _Derived>
00224         inline bool operator<=( const std::string & lhs, const UniqueString<_Derived> & rhs )
00225     { return ( lhs <= rhs.str() ); }
00226 
00227 
00229     template<class _Derived>
00230         inline bool operator>=( const UniqueString<_Derived> & lhs, const UniqueString<_Derived> & rhs )
00231     { return ( lhs.str() >= rhs.str() ); }
00233     template<class _Derived>
00234         inline bool operator>=( const UniqueString<_Derived> & lhs, const std::string & rhs )
00235     { return ( lhs.str() >= rhs ); }
00237     template<class _Derived>
00238         inline bool operator>=( const std::string & lhs, const UniqueString<_Derived> & rhs )
00239     { return ( lhs >= rhs.str() ); }
00240 
00242     template<class _Derived>
00243         inline std::ostream & operator<<( std::ostream & str, const UniqueString<_Derived> & obj )
00244     { return str << obj.str(); }
00245 
00247   } // namespace base
00250 } // namespace zypp
00252 #endif // ZYPP_BASE_UNIQUESTRING_H

Generated on Tue Sep 25 19:22:59 2007 for libzypp by  doxygen 1.5.3