MySQL++ Reference Manual

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
    00002
    00003
    00004
    00005 /***********************************************************************
    00006  Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
    00007  MySQL AB, and (c) 2004, 2005 by Educational Technology Resources, Inc.
    00008  Others may also hold copyrights on code in this file.  See the CREDITS
    00009  file in the top directory of the distribution for details.
    00010
    00011  This file is part of MySQL++.
    00012
    00013  MySQL++ is free software; you can redistribute it and/or modify it
    00014  under the terms of the GNU Lesser General Public License as published
    00015  by the Free Software Foundation; either version 2.1 of the License, or
    00016  (at your option) any later version.
    00017
    00018  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
    00019  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    00020  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
    00021  License for more details.
    00022
    00023  You should have received a copy of the GNU Lesser General Public
    00024  License along with MySQL++; if not, write to the Free Software
    00025  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00026  USA
    00027 ***********************************************************************/
    00028
    00029 #ifndef MYSQLPP_CONST_STRING_H
    00030 #define MYSQLPP_CONST_STRING_H
    00031
    00032 #include "defs.h"
    00033
    00034 #include <stdexcept>
    00035 #include <string>
    00036 #include <iostream>
    00037
    00038 namespace mysqlpp {
    00039
    00049 class const_string
    00050 {
    00051 public:
    00054         typedef const char value_type;
    00055
    00057         typedef unsigned int size_type;
    00058
    00061         typedef const char& const_reference;
    00062
    00064         typedef const char* const_iterator;
    00065
    00068         typedef const_iterator iterator;
    00069
    00070 #if !defined(DOXYGEN_IGNORE)
    00071 // Doxygen will not generate documentation for this section.
    00072         typedef int difference_type;
    00073         typedef const_reference reference;
    00074         typedef const char* const_pointer;
    00075         typedef const_pointer pointer;
    00076 #endif // !defined(DOXYGEN_IGNORE)
    00077
    00079         const_string() :
    00080         str_data_("")
    00081         {
    00082         }
    00083
    00085         const_string(const char* str) :
    00086         str_data_(str)
    00087         {
    00088         }
    00089
    00091         const_string& operator=(const char* str)
    00092         {
    00093                 str_data_ = str;
    00094                 return *this;
    00095         }
    00096
    00098         size_type size() const
    00099         {
    00100                 register int i = 0;
    00101                 while (str_data_[i])
    00102                          i++;
    00103                  return i;
    00104         }
    00105
    00108         const_iterator begin() const
    00109         {
    00110                 return str_data_;
    00111         }
    00112
    00115         const_iterator end() const
    00116         {
    00117                 return str_data_ + size();
    00118         }
    00119
    00121         size_type length() const
    00122         {
    00123                 return size();
    00124         }
    00125
    00131         size_type max_size() const
    00132         {
    00133                 return size();
    00134         }
    00135
    00137         const_reference operator [](size_type pos) const
    00138         {
    00139                 return str_data_[pos];
    00140         }
    00141
    00146         const_reference at(size_type pos) const
    00147         {
    00148                 if (pos >= size())
    00149                         throw std::out_of_range("");
    00150                 else
    00151                         return str_data_[pos];
    00152         }
    00153
    00156         const char* c_str() const
    00157         {
    00158                 return str_data_;
    00159         }
    00160
    00162         const char* data() const
    00163         {
    00164                 return str_data_;
    00165         }
    00166
    00174         int compare(const const_string& str) const
    00175         {
    00176                 const char* str1 = str_data_;
    00177                 const char* str2 = str.str_data_;
    00178                 while (*str1 == *str2 && (*str1 && *str2)) {
    00179                         str1++;
    00180                         str2++;
    00181                 }
    00182                 return *str1 - *str2;
    00183         }
    00184
    00185 private:
    00186         const char* str_data_;
    00187 };
    00188
    00189
    00191 inline std::ostream& operator <<(std::ostream& o,
    00192                 const const_string& str)
    00193 {
    00194         return o << str.c_str();
    00195 }
    00196
    00198 inline int compare(const const_string& lhs, const const_string& rhs)
    00199 {
    00200         return lhs.compare(rhs);
    00201 }
    00202
    00204 inline bool operator ==(const_string& lhs, const_string& rhs)
    00205 {
    00206         return compare(lhs, rhs) == 0;
    00207 }
    00208
    00210 inline bool operator !=(const_string& lhs, const_string& rhs)
    00211 {
    00212         return compare(lhs, rhs) != 0;
    00213 }
    00214
    00216 inline bool operator <(const_string& lhs, const_string& rhs)
    00217 {
    00218         return compare(lhs, rhs) < 0;
    00219 }
    00220
    00222 inline bool operator <=(const_string& lhs, const_string& rhs)
    00223 {
    00224         return compare(lhs, rhs) <= 0;
    00225 }
    00226
    00228 inline bool operator >(const_string& lhs, const_string& rhs)
    00229 {
    00230         return compare(lhs, rhs) > 0;
    00231 }
    00232
    00234 inline bool operator >=(const_string& lhs, const_string& rhs)
    00235 {
    00236         return compare(lhs, rhs) >= 0;
    00237 }
    00238
    00239 } // end namespace mysqlpp
    00240
    00241 #endif