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_ + 1];
    00091                 memcpy(str_data_, str.data(), length_);
    00092                 str_data_[length_] = '\0';
    00093         }
    00094
    00096         const_string(const char* str) :
    00097         str_data_(0),
    00098         length_(size_type(strlen(str)))
    00099         {
    00100                 str_data_ = new char[length_ + 1];
    00101                 memcpy(str_data_, str, length_);
    00102                 str_data_[length_] = '\0';
    00103         }
    00104
    00106         const_string(const char* str, size_type len) :
    00107         str_data_(0),
    00108         length_(size_type(len))
    00109         {
    00110                 str_data_ = new char[length_ + 1];
    00111                 memcpy(str_data_, str, length_);
    00112                 str_data_[length_] = '\0';
    00113         }
    00114
    00116         ~const_string()
    00117         {
    00118                 delete[] str_data_;
    00119         }
    00120
    00122         const_string& operator=(const char* str)
    00123         {
    00124                 delete[] str_data_;
    00125                 length_ = size_type(strlen(str));
    00126                 str_data_ = new char[length_];
    00127                 memcpy(str_data_, str, length_);
    00128                 return *this;
    00129         }
    00130
    00132         const_string& operator=(const const_string& cs)
    00133         {
    00134                 delete[] str_data_;
    00135                 length_ = cs.length_;
    00136                 str_data_ = new char[length_];
    00137                 memcpy(str_data_, cs.str_data_, length_);
    00138                 return *this;
    00139         }
    00140
    00142         size_type length() const { return length_; }
    00143
    00145         size_type size() const { return length_; }
    00146
    00149         const_iterator begin() const { return str_data_; }
    00150
    00153         const_iterator end() const { return str_data_ + size(); }
    00154
    00160         size_type max_size() const { return size(); }
    00161
    00163         const_reference operator [](size_type pos) const
    00164                         { return str_data_[pos]; }
    00165
    00170         const_reference at(size_type pos) const
    00171         {
    00172                 if (pos >= size())
    00173                         throw std::out_of_range("");
    00174                 else
    00175                         return str_data_[pos];
    00176         }
    00177
    00180         const char* c_str() const { return str_data_; }
    00181
    00183         const char* data() const { return str_data_; }
    00184
    00192         int compare(const const_string& str) const
    00193         {
    00194                 size_type i = 0, short_len = std::min(length(), str.length());
    00195                 while ((i < short_len) && (str_data_[i] != str.str_data_[i])) {
    00196                         ++i;
    00197                 }
    00198                 return str_data_[i] - str.str_data_[i];
    00199         }
    00200
    00201 private:
    00202         char* str_data_;
    00203         size_type length_;
    00204 };
    00205
    00206
    00208 inline std::ostream& operator <<(std::ostream& o,
    00209                 const const_string& str)
    00210 {
    00211         return o << str.c_str();
    00212 }
    00213
    00215 inline int compare(const const_string& lhs, const const_string& rhs)
    00216 {
    00217         return lhs.compare(rhs);
    00218 }
    00219
    00221 inline bool operator ==(const_string& lhs, const_string& rhs)
    00222 {
    00223         return compare(lhs, rhs) == 0;
    00224 }
    00225
    00227 inline bool operator !=(const_string& lhs, const_string& rhs)
    00228 {
    00229         return compare(lhs, rhs) != 0;
    00230 }
    00231
    00233 inline bool operator <(const_string& lhs, const_string& rhs)
    00234 {
    00235         return compare(lhs, rhs) < 0;
    00236 }
    00237
    00239 inline bool operator <=(const_string& lhs, const_string& rhs)
    00240 {
    00241         return compare(lhs, rhs) <= 0;
    00242 }
    00243
    00245 inline bool operator >(const_string& lhs, const_string& rhs)
    00246 {
    00247         return compare(lhs, rhs) > 0;
    00248 }
    00249
    00251 inline bool operator >=(const_string& lhs, const_string& rhs)
    00252 {
    00253         return compare(lhs, rhs) >= 0;
    00254 }
    00255
    00256 } // end namespace mysqlpp
    00257
    00258 #endif