Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class 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 "common.h"
00033 
00034 #include <algorithm>
00035 #include <iostream>
00036 #include <stdexcept>
00037 #include <string>
00038 
00039 namespace mysqlpp {
00040 
00050 class MYSQLPP_EXPORT const_string
00051 {
00052 public:
00055         typedef const char value_type;
00056 
00058         typedef unsigned int size_type;
00059 
00062         typedef const char& const_reference;
00063 
00065         typedef const char* const_iterator;
00066 
00069         typedef const_iterator iterator;
00070 
00071 #if !defined(DOXYGEN_IGNORE)
00072 // Doxygen will not generate documentation for this section.
00073         typedef int difference_type;
00074         typedef const_reference reference;
00075         typedef const char* const_pointer;
00076         typedef const_pointer pointer;
00077 #endif // !defined(DOXYGEN_IGNORE)
00078 
00080         const_string() :
00081         str_data_(""),
00082         length_(0)
00083         {
00084         }
00085         
00087         const_string(const char* str) :
00088         str_data_(str),
00089         length_(size_type(strlen(str)))
00090         {
00091         }
00092         
00094         const_string(const char* str, size_type len) :
00095         str_data_(str),
00096         length_(size_type(len))
00097         {
00098         }
00099         
00101         const_string& operator=(const char* str)
00102         {
00103                 str_data_ = str;
00104                 length_ = size_type(strlen(str));
00105                 return *this;
00106         }
00107 
00109         size_type length() const { return length_; }
00110 
00112         size_type size() const { return length(); }
00113 
00116         const_iterator begin() const { return str_data_; }
00117         
00120         const_iterator end() const { return str_data_ + size(); }
00121         
00127         size_type max_size() const { return size(); }
00128         
00130         const_reference operator [](size_type pos) const
00131                         { return str_data_[pos]; }
00132         
00137         const_reference at(size_type pos) const
00138         {
00139                 if (pos >= size())
00140                         throw std::out_of_range("");
00141                 else
00142                         return str_data_[pos];
00143         }
00144         
00147         const char* c_str() const { return str_data_; }
00148         
00150         const char* data() const { return str_data_; }
00151         
00159         int compare(const const_string& str) const
00160         {
00161                 size_type i = 0, short_len = std::min(length(), str.length());
00162                 while ((i < short_len) && (str_data_[i] != str.str_data_[i])) {
00163                         ++i;
00164                 }
00165                 return str_data_[i] - str.str_data_[i];
00166         }
00167 
00168 private:
00169         const char* str_data_;
00170         size_type length_;
00171 };
00172 
00173 
00175 inline std::ostream& operator <<(std::ostream& o,
00176                 const const_string& str)
00177 {
00178         return o << str.c_str();
00179 }
00180 
00182 inline int compare(const const_string& lhs, const const_string& rhs)
00183 {
00184         return lhs.compare(rhs);
00185 }
00186 
00188 inline bool operator ==(const_string& lhs, const_string& rhs)
00189 {
00190         return compare(lhs, rhs) == 0;
00191 }
00192 
00194 inline bool operator !=(const_string& lhs, const_string& rhs)
00195 {
00196         return compare(lhs, rhs) != 0;
00197 }
00198 
00200 inline bool operator <(const_string& lhs, const_string& rhs)
00201 {
00202         return compare(lhs, rhs) < 0;
00203 }
00204 
00206 inline bool operator <=(const_string& lhs, const_string& rhs)
00207 {
00208         return compare(lhs, rhs) <= 0;
00209 }
00210 
00212 inline bool operator >(const_string& lhs, const_string& rhs)
00213 {
00214         return compare(lhs, rhs) > 0;
00215 }
00216 
00218 inline bool operator >=(const_string& lhs, const_string& rhs)
00219 {
00220         return compare(lhs, rhs) >= 0;
00221 }
00222 
00223 } // end namespace mysqlpp
00224 
00225 #endif

Generated on Fri Apr 13 09:28:45 2007 for MySQL++ by doxygen 1.3.5