Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

const_string.h

Go to the documentation of this file.
00001 #ifndef MYSQLPP_CONST_STRING_H
00002 #define MYSQLPP_CONST_STRING_H
00003 
00007 
00008 #include "defs.h"
00009 
00010 #include <stdexcept>
00011 #include <string>
00012 #include <iostream>
00013 
00014 namespace mysqlpp {
00015 
00025 class const_string {
00026 private:
00027   const char *str_data; 
00028 public:
00029   typedef const char   value_type;
00030   typedef unsigned int size_type;
00031   typedef          int difference_type;
00032 
00033   typedef const char&     const_reference;
00034   typedef const_reference reference;
00035   typedef const char*     const_pointer;
00036   typedef const_pointer   pointer;
00037 
00038   typedef const char*    const_iterator;
00039 
00042   typedef const_iterator iterator;         
00043   
00044   const_string() : str_data("") {}
00045   const_string(const char *str) : str_data(str) {}
00046   const_string& operator = (const char *str)
00047   {
00048         str_data = str;
00049         return *this;
00050   }
00051  
00052   size_type size() const
00053   {
00054     register int i = 0;
00055     while (str_data[i]) i++;
00056     return i;
00057   }
00058 
00059   const_iterator  begin() const {return str_data;}
00060   const_iterator  end()   const {return str_data + size();}
00061 
00062   size_type length() const {return size();}
00063   size_type max_size() const {return size();}
00064   
00065   const_reference operator[](size_type pos) const {return str_data[pos];}
00066   const_reference at(size_type pos) const
00067   {
00068     if (pos >= size()) throw std::out_of_range("");
00069     else return str_data[pos];
00070   }
00071   
00072   const char* c_str() const {return str_data;}
00073   const char* data()  const {return str_data;}
00074   
00075   int compare(const const_string& str) const
00076   {
00077     const char* str1 = str_data;
00078     const char* str2 = str.str_data;
00079     while (*str1 == *str2 && (*str1 || *str2)) {str1++; str2++;}
00080         return *str1-*str2;
00081   }
00082 };
00083 
00084 
00085 inline std::ostream& operator << (std::ostream &o, const const_string &str)
00086 {
00087   return o << str.c_str();
00088 }
00089 
00090 inline int compare (const const_string &lhs, const const_string &rhs)
00091 {
00092   return lhs.compare(rhs);
00093 }
00094 
00095 inline bool operator == (const_string &lhs, const_string &rhs) 
00096 {
00097   return compare(lhs,rhs) == 0;
00098 }
00099 
00100 inline bool operator != (const_string &lhs, const_string &rhs)
00101 {
00102   return compare(lhs,rhs) != 0;
00103 }
00104 
00105 inline bool operator < (const_string &lhs, const_string &rhs)
00106 {
00107   return compare(lhs,rhs) < 0;
00108 }
00109 
00110 inline bool operator <= (const_string &lhs, const_string &rhs)
00111 {
00112   return compare(lhs,rhs) <= 0;
00113 }
00114 
00115 inline bool operator > (const_string &lhs, const_string &rhs)
00116 {
00117   return compare(lhs,rhs) > 0;
00118 }
00119 
00120 inline bool operator >= (const_string &lhs, const_string &rhs)
00121 {
00122   return compare(lhs,rhs) >= 0;
00123 }
00124 
00125 } // end namespace mysqlpp
00126 
00127 #endif
00128 

Generated on Thu May 5 05:30:43 2005 for MySQL++ by doxygen1.2.18