Back to index

im-sdk  12.3.91
basictype.hh
Go to the documentation of this file.
00001 /*  This header file defines fundamental type operations
00002     like functors. */
00003 
00004 #ifndef BASICTYPE_HH_
00005 #define BASICTYPE_HH_
00006 #include <config.h>
00007 #include <string>
00008 #include <utility>
00009 #include <functional>
00010 #include <algorithm>
00011 #include <string.h>
00012 #include <stdlib.h>
00013 
00014 //
00015 // Usuful functors
00016 //
00017 struct Functor_strcmp {
00018     bool operator()(const char* s1, const char* s2) const
00019     {
00020        return strcmp(s1, s2) < 0;
00021     }
00022 };
00023 
00024 // Delete/Destroy all members.
00025 
00026 struct Functor_deleteobject {
00027     template <typename Key, typename T>
00028     bool operator()(const std::pair<const Key, T*>& cp) const
00029     {
00030        delete cp.second;
00031        return true;
00032     }
00033     template <typename T>
00034     bool operator()(const T* p) const
00035     {
00036        delete p;
00037        return true;
00038     }
00039 };
00040 
00041 // Delete all pointers of the container.
00042 template <typename T>
00043 void delete_all(T &x)
00044 {
00045     for_each(x.begin(), x.end(), Functor_deleteobject());
00046 }
00047 
00048 struct Functor_destroyobject {
00049     template <typename Key, typename T>
00050     bool operator()(std::pair<const Key, T*>& cp) const
00051     {
00052        cp.second->destroy();
00053        return true;
00054     }
00055     template <typename T>
00056     bool operator()(T* p) const
00057     {
00058        p->destroy();
00059        return true;
00060     }
00061 };
00062 
00063 // Call destroy() for all pointers of the container.
00064 template <typename T>
00065 void destroy_all(T &x)
00066 {
00067     for_each(x.begin(), x.end(), Functor_destroyobject());
00068 }
00069 
00070 
00071 //
00072 // UTF-16 string.
00073 //
00074 
00075 typedef CARD16BIT UTFCHAR;
00076 
00077 // char_traits for u16string.
00078 struct UTFCHAR_char_traits {
00079     typedef UTFCHAR char_type;
00080     typedef unsigned long int_type;
00081 
00082     static void assign(char_type& c1, const char_type& c2)
00083     { c1 = c2; }
00084     static bool eq(const char_type& c1, const char_type& c2)
00085     { return (c1 == c2); }
00086     static bool lt(const char_type& c1, const char_type& c2)
00087     { return (c1 < c2); }
00088     static int compare(const char_type* s1, const char_type* s2, size_t n)
00089     { return memcmp(s1, s2, n * sizeof(char_type)); }
00090     static size_t length(const char_type* s)
00091     { 
00092        int i;
00093        for (i = 0; *s; s++, i++);
00094        return i;
00095     }
00096     static const char_type* find(const char_type* s, size_t n, const char_type& a)
00097     {
00098        for (size_t i = 0; (i < n); s++, i++) {
00099            if (*s == a) return s;
00100        }
00101        return NULL;
00102     }
00103     static char_type* move(char_type* s1, const char_type* s2, size_t n)
00104     { return (char_type*) memmove(s1, s2, n * sizeof(char_type)); }
00105     static char_type* copy(char_type* s1, const char_type* s2, size_t n)
00106     { return (char_type*) memcpy(s1, s2, n * sizeof(char_type)); }
00107     static char_type* assign(char_type* s, size_t n, const char_type& a)
00108     { 
00109        for (size_t i = 0; i < n; i++) s[i] = a;
00110        return s;
00111     }
00112     static char_type to_char_type(const int_type& i)
00113     { return (char_type) i; }
00114     static int_type to_int_type(const char_type& ch)
00115     { return (int_type) ch; }
00116     static bool eq_int_type(const int_type& i1, const int_type& i2)
00117     { return i1 == i2; }
00118     static int_type eof()
00119     { return (int_type) EOF; }
00120     static int_type not_eof(const int_type& i)
00121     { return (i == eof()) ? 0 : i; }
00122 
00123     // obsolete string_char_traits method.
00124     static char_type* set(char_type* s, const char_type& c, size_t num)
00125     { return assign(s, num, c); }
00126 };
00127 
00128 // Prohibit implicit conversion from u16string to basic_string,
00129 // make the base class private.
00130 class u16string : private std::basic_string<UTFCHAR, UTFCHAR_char_traits>
00131 {
00132     typedef std::basic_string<UTFCHAR, UTFCHAR_char_traits> BaseClass;
00133     enum CHARSTR_STATE {
00134            UNINIT,
00135            VALID,
00136            INVALID
00137     } state;
00138     std::string charstr;
00139     void set_charstr()
00140     {
00141        size_type size = BaseClass::size();
00142        charstr = std::string(size, 0);
00143        BaseClass::iterator it_s = begin();
00144        std::string::iterator it_d = charstr.begin();
00145        for (; it_s != end(); it_s++, it_d++) {
00146            if (*it_s > 0x7F) {
00147               state = INVALID;
00148               return;
00149            }
00150            *it_d = (char) *it_s;
00151        }
00152        state = VALID;
00153        return;
00154     }
00155 
00156   public:
00157     // basic_string public functions
00158     typedef BaseClass::iterator iterator;
00159     typedef BaseClass::const_iterator const_iterator;
00160     typedef BaseClass::reverse_iterator reverse_iterator;
00161     typedef BaseClass::const_reverse_iterator const_reverse_iterator;
00162 
00163     BaseClass::npos;
00164     BaseClass::size;
00165     BaseClass::begin;
00166     BaseClass::end;
00167     BaseClass::empty;
00168     BaseClass::data;
00169     BaseClass::c_str;
00170 
00171     int compare(const u16string& s) const
00172     { return this->BaseClass::compare(s); }
00173     size_type find(UTFCHAR c, size_type pos = 0) const
00174     { return this->BaseClass::find(c, pos); }
00175     size_type find(const u16string& s, size_type pos = 0) const
00176     { return this->BaseClass::find(s, pos); }
00177 
00178     u16string substr(size_type pos = 0, size_type n = npos) const
00179     { return u16string(*this, pos, n); }
00180 
00181   public:
00182     // u16string original extension.
00183 
00184     const char* get_charstr() const
00185     {
00186        if (state != VALID) return NULL;
00187        return charstr.c_str();
00188     }
00189     const std::string* get_string() const
00190     {
00191        if (state != VALID) return NULL;
00192        return &charstr;
00193     }
00194     u16string() : BaseClass()
00195     {
00196        charstr = "";
00197        state = VALID;
00198     }
00199     u16string(UTFCHAR *str) : BaseClass(str)
00200     {
00201        set_charstr();
00202     }
00203     u16string(UTFCHAR *str, size_type n) : BaseClass(str, n)
00204     {
00205        set_charstr();
00206     }
00207     u16string(const u16string& str, size_type pos, size_type n)
00208        : BaseClass(str, pos, n)
00209     {
00210        set_charstr();
00211     }
00212  
00213     u16string(const char *cstr)
00214        : BaseClass(strlen(cstr), 0)
00215     {
00216        const char *p;
00217        BaseClass::iterator it = begin();
00218        charstr = cstr;
00219        state = VALID;
00220        for (p = cstr; *p; p++, it++) {
00221            *it = *p;
00222        }
00223     }
00224     u16string(const std::string& s)
00225        : BaseClass(s.size(), 0)
00226     {
00227        int i;
00228        int n = s.size();
00229        BaseClass::iterator it = begin();
00230        std::string::const_iterator its = s.begin();
00231        charstr = s;
00232        state = VALID;
00233        for (i = 0; i < n; i++, it++, its++) {
00234            *it = (UTFCHAR) *its;
00235        }
00236     }
00237 };
00238 
00239 inline bool
00240 operator< (
00241     const u16string& lhs,
00242     const u16string& rhs
00243 )
00244 {
00245   return (lhs.compare (rhs) < 0);
00246 }
00247 
00248 inline bool
00249 operator== (
00250     const u16string& lhs,
00251     const u16string& rhs
00252 )
00253 {
00254   return (lhs.compare (rhs) == 0);
00255 }
00256 
00257 //sigh...
00258 using namespace std;
00259 
00260 #endif /* not BASICTYPE_HH_ */
00261 
00262 /* Local Variables: */
00263 /* c-file-style: "iiim-project" */
00264 /* End: */
00265