const_string.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 /***********************************************************************
00006  Copyright © 1998 by Kevin Atkinson, © 1999, 2000 and 2001 by MySQL AB,
00007  and © 2004-2005, 2007, 2017 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 <cstring>
00036 #include <iostream>
00037 #include <stdexcept>
00038 #include <string>
00039 
00040 namespace mysqlpp {
00041 
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_(0),
00082         length_(0)
00083         {
00084         }
00085         
00087         const_string(const std::string& str) :
00088         str_data_(0),
00089         length_(str.length())
00090         {
00091                 str_data_ = new char[length_ + 1];
00092                 memcpy(str_data_, str.data(), length_);
00093                 str_data_[length_] = '\0';
00094         }
00095         
00097         const_string(const char* str) :
00098         str_data_(0),
00099         length_(size_type(strlen(str)))
00100         {
00101                 str_data_ = new char[length_ + 1];
00102                 memcpy(str_data_, str, length_);
00103                 str_data_[length_] = '\0';
00104         }
00105         
00107         const_string(const char* str, size_type len) :
00108         str_data_(0),
00109         length_(size_type(len))
00110         {
00111                 str_data_ = new char[length_ + 1];
00112                 memcpy(str_data_, str, length_);
00113                 str_data_[length_] = '\0';
00114         }
00115 
00117         ~const_string()
00118         {
00119                 delete[] str_data_;
00120         }
00121         
00123         const_string& operator=(const char* str)
00124         {
00125                 delete[] str_data_;
00126                 length_ = size_type(strlen(str));
00127                 str_data_ = new char[length_];
00128                 memcpy(str_data_, str, length_);
00129                 return *this;
00130         }
00131 
00133         const_string& operator=(const const_string& cs)
00134         {
00135                 delete[] str_data_;
00136                 length_ = cs.length_;
00137                 str_data_ = new char[length_];
00138                 memcpy(str_data_, cs.str_data_, length_);
00139                 return *this;
00140         }
00141 
00143         size_type length() const { return length_; }
00144 
00146         size_type size() const { return length_; }
00147 
00150         const_iterator begin() const { return str_data_; }
00151         
00154         const_iterator end() const { return str_data_ + size(); }
00155         
00161         size_type max_size() const { return size(); }
00162         
00164         const_reference operator [](size_type pos) const
00165                         { return str_data_[pos]; }
00166         
00171         const_reference at(size_type pos) const
00172         {
00173                 if (pos >= size())
00174                         throw std::out_of_range("");
00175                 else
00176                         return str_data_[pos];
00177         }
00178         
00181         const char* c_str() const { return str_data_; }
00182         
00184         const char* data() const { return str_data_; }
00185         
00193         int compare(const const_string& str) const
00194         {
00195                 size_type i = 0, short_len = std::min(length(), str.length());
00196                 while ((i < short_len) && (str_data_[i] != str.str_data_[i])) {
00197                         ++i;
00198                 }
00199                 return str_data_[i] - str.str_data_[i];
00200         }
00201 
00202 private:
00203         char* str_data_;
00204         size_type length_;
00205 };
00206 
00207 
00209 inline std::ostream& operator <<(std::ostream& o,
00210                 const const_string& str)
00211 {
00212         return o << str.c_str();
00213 }
00214 
00216 inline int compare(const const_string& lhs, const const_string& rhs)
00217 {
00218         return lhs.compare(rhs);
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 
00240 inline bool operator <=(const_string& lhs, const_string& rhs)
00241 {
00242         return compare(lhs, rhs) <= 0;
00243 }
00244 
00246 inline bool operator >(const_string& lhs, const_string& rhs)
00247 {
00248         return compare(lhs, rhs) > 0;
00249 }
00250 
00252 inline bool operator >=(const_string& lhs, const_string& rhs)
00253 {
00254         return compare(lhs, rhs) >= 0;
00255 }
00256 
00257 } // end namespace mysqlpp
00258 
00259 #endif

Generated on 10 Oct 2017 for MySQL++ by  doxygen 1.4.7