Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Ustring.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                        (C) SuSE GmbH |
00011 \----------------------------------------------------------------------/
00012 
00013    File:       Ustring.h
00014 
00015    Author:     Michael Andres <ma@suse.de>
00016    Maintainer: Michael Andres <ma@suse.de>
00017 
00018 /-*/
00019 #ifndef Ustring_h
00020 #define Ustring_h
00021 
00022 #include <iostream>
00023 #include <string>
00024 // MemUsage.h defines/undefines D_MEMUSAGE
00025 #include "y2util/MemUsage.h"
00026 #include <set>
00027 
00029 //
00030 //      CLASS NAME : UstringHash
00038 class UstringHash
00039 #ifdef D_MEMUSAGE
00040  : public MemUsage
00041 #endif
00042  {
00043 
00044   protected:
00045 
00046     typedef std::set<std::string> UstringHash_type;
00047 
00048     UstringHash_type _UstringHash;
00049 
00050   public:
00051 
00052     const std::string & add( const std::string & nstr_r )
00053     {
00054         return *(_UstringHash.insert( nstr_r ).first);
00055     }
00056 
00060     unsigned size() const { return _UstringHash.size(); }
00061     unsigned long sum() const {
00062         UstringHash_type::const_iterator it = _UstringHash.begin();
00063         UstringHash_type::const_iterator e = _UstringHash.end();
00064         unsigned long sum = 0;
00065         while (it != e)
00066         {
00067             sum += it->size();
00068             it++;
00069         }
00070         return sum;
00071     }
00072 #ifdef D_MEMUSAGE
00073     virtual size_t mem_size () const { return sizeof (UstringHash); }
00074 #endif
00075 };
00076 
00078 
00080 //
00081 //      CLASS NAME : Ustring
00124 class Ustring
00125 #ifdef D_MEMUSAGE
00126   : public MemUsage
00127 #endif
00128  {
00129 
00130   private:
00131 
00136     std::string _name;
00137 
00138   public:
00139 #ifdef D_MEMUSAGE
00140     virtual size_t mem_size () const { return sizeof (Ustring); }
00141 #endif
00142 
00146     Ustring( UstringHash & nameHash_r, const std::string & n  )
00147       :_name( nameHash_r.add( n ) )
00148     {}
00149 
00150   public:
00151 
00155     const std::string & asString() const { return _name; }
00156 
00160     operator const std::string & () const { return asString(); }
00161 
00165     std::string::size_type size() const { return asString().size(); }
00166 
00170     bool empty() const { return asString().empty(); }
00171 
00172 
00173     int compare( const std::string & rhs ) const {
00174       return asString().compare( rhs );
00175     }
00176 
00177     int compare( const Ustring & rhs ) const {
00178       if ( *this == rhs )
00179         return 0;
00180       return( *this < rhs ? -1 : 1 );
00181     }
00182 
00186     const std::string * operator->() const { return & asString(); }
00187 
00188     // operator ==
00189 
00190     friend bool operator==( const Ustring & lhs, const Ustring & rhs ) {
00191       // Ustrings share their string representation
00192       return ( lhs->c_str() == rhs->c_str() );
00193     }
00194 
00195     friend bool operator==( const Ustring & lhs, const std::string & rhs ) {
00196       return ( (const std::string &)lhs == rhs );
00197     }
00198 
00199     friend bool operator==( const std::string & lhs, const Ustring & rhs ) {
00200       return ( lhs == (const std::string &)rhs );
00201     }
00202 
00203     // operator !=
00204 
00205     friend bool operator!=( const Ustring & lhs, const Ustring & rhs ) {
00206       return ( ! operator==( lhs, rhs ) );
00207     }
00208 
00209     friend bool operator!=( const Ustring & lhs, const std::string & rhs ) {
00210       return ( ! operator==( lhs, rhs ) );
00211     }
00212 
00213     friend bool operator!=( const std::string & lhs, const Ustring & rhs ) {
00214       return ( ! operator==( lhs, rhs ) );
00215     }
00216 
00217     // operator <
00218 
00219     friend bool operator<( const Ustring & lhs, const Ustring & rhs ) {
00220       return ( (const std::string &)lhs < (const std::string &)rhs );
00221     }
00222 
00223     friend bool operator<( const Ustring & lhs, const std::string & rhs ) {
00224       return ( (const std::string &)lhs < rhs );
00225     }
00226 
00227     friend bool operator<( const std::string & lhs, const Ustring & rhs ) {
00228       return ( lhs < (const std::string &)rhs );
00229     }
00230 
00231     // operator >
00232 
00233     friend bool operator>( const Ustring & lhs, const Ustring & rhs ) {
00234       return ( (const std::string &)lhs > (const std::string &)rhs );
00235     }
00236 
00237     friend bool operator>( const Ustring & lhs, const std::string & rhs ) {
00238       return ( (const std::string &)lhs > rhs );
00239     }
00240 
00241     friend bool operator>( const std::string & lhs, const Ustring & rhs ) {
00242       return ( lhs > (const std::string &)rhs );
00243     }
00244 
00245     // operator >=
00246 
00247     friend bool operator>=( const Ustring & lhs, const Ustring & rhs ) {
00248       return ( ! operator<( lhs, rhs ) );
00249     }
00250 
00251     friend bool operator>=( const Ustring & lhs, const std::string & rhs ) {
00252       return ( ! operator<( lhs, rhs ) );
00253     }
00254 
00255     friend bool operator>=( const std::string & lhs, const Ustring & rhs ) {
00256       return ( ! operator<( lhs, rhs ) );
00257     }
00258 
00259     // operator <=
00260 
00261     friend bool operator<=( const Ustring & lhs, const Ustring & rhs ) {
00262       return ( ! operator>( lhs, rhs ) );
00263     }
00264 
00265     friend bool operator<=( const Ustring & lhs, const std::string & rhs ) {
00266       return ( ! operator>( lhs, rhs ) );
00267     }
00268 
00269     friend bool operator<=( const std::string & lhs, const Ustring & rhs ) {
00270       return ( ! operator>( lhs, rhs ) );
00271     }
00272 
00273     // IO
00274 
00275     friend std::ostream & operator<<( std::ostream & str, const Ustring & obj ) {
00276       return str << (const std::string &)obj;
00277     }
00278 };
00279 
00280 #endif // Ustring_h

Generated on Fri Feb 24 00:30:02 2006 for liby2util by  doxygen 1.4.4