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 
00049 class MYSQLPP_EXPORT 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_(0),
00081         length_(0)
00082         {
00083         }
00084         
00086         const_string(const std::string& str) :
00087         str_data_(0),
00088         length_(str.length())
00089         {
00090                 str_data_ = new char[length_];
00091                 memcpy(str_data_, str.data(), length_);
00092         }
00093         
00095         const_string(const char* str) :
00096         str_data_(0),
00097         length_(size_type(strlen(str)))
00098         {
00099                 str_data_ = new char[length_];
00100                 memcpy(str_data_, str, length_);
00101         }
00102         
00104         const_string(const char* str, size_type len) :
00105         str_data_(0),
00106         length_(size_type(len))
00107         {
00108                 str_data_ = new char[length_];
00109                 memcpy(str_data_, str, length_);
00110         }
00111 
00113         ~const_string()
00114         {
00115                 delete[] str_data_;
00116         }
00117         
00119         const_string& operator=(const char* str)
00120         {
00121                 delete[] str_data_;
00122                 length_ = size_type(strlen(str));
00123                 str_data_ = new char[length_];
00124                 memcpy(str_data_, str, length_);
00125                 return *this;
00126         }
00127 
00129         const_string& operator=(const const_string& cs)
00130         {
00131                 delete[] str_data_;
00132                 length_ = cs.length_;
00133                 str_data_ = new char[length_];
00134                 memcpy(str_data_, cs.str_data_, length_);
00135                 return *this;
00136         }
00137 
00139         size_type length() const { return length_; }
00140 
00142         size_type size() const { return length_; }
00143 
00146         const_iterator begin() const { return str_data_; }
00147         
00150         const_iterator end() const { return str_data_ + size(); }
00151         
00157         size_type max_size() const { return size(); }
00158         
00160         const_reference operator [](size_type pos) const
00161                         { return str_data_[pos]; }
00162         
00167         const_reference at(size_type pos) const
00168         {
00169                 if (pos >= size())
00170                         throw std::out_of_range("");
00171                 else
00172                         return str_data_[pos];
00173         }
00174         
00177         const char* c_str() const { return str_data_; }
00178         
00180         const char* data() const { return str_data_; }
00181         
00189         int compare(const const_string& str) const
00190         {
00191                 size_type i = 0, short_len = std::min(length(), str.length());
00192                 while ((i < short_len) && (str_data_[i] != str.str_data_[i])) {
00193                         ++i;
00194                 }
00195                 return str_data_[i] - str.str_data_[i];
00196         }
00197 
00198 private:
00199         char* str_data_;
00200         size_type length_;
00201 };
00202 
00203 
00205 inline std::ostream& operator <<(std::ostream& o,
00206                 const const_string& str)
00207 {
00208         return o << str.c_str();
00209 }
00210 
00212 inline int compare(const const_string& lhs, const const_string& rhs)
00213 {
00214         return lhs.compare(rhs);
00215 }
00216 
00218 inline bool operator ==(const_string& lhs, const_string& rhs)
00219 {
00220         return compare(lhs, rhs) == 0;
00221 }
00222 
00224 inline bool operator !=(const_string& lhs, const_string& rhs)
00225 {
00226         return compare(lhs, rhs) != 0;
00227 }
00228 
00230 inline bool operator <(const_string& lhs, const_string& rhs)
00231 {
00232         return compare(lhs, rhs) < 0;
00233 }
00234 
00236 inline bool operator <=(const_string& lhs, const_string& rhs)
00237 {
00238         return compare(lhs, rhs) <= 0;
00239 }
00240 
00242 inline bool operator >(const_string& lhs, const_string& rhs)
00243 {
00244         return compare(lhs, rhs) > 0;
00245 }
00246 
00248 inline bool operator >=(const_string& lhs, const_string& rhs)
00249 {
00250         return compare(lhs, rhs) >= 0;
00251 }
00252 
00253 } // end namespace mysqlpp
00254 
00255 #endif

Generated on Tue Jul 10 16:46:09 2007 for MySQL++ by doxygen 1.3.5