MySQL++ Reference Manual

Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members

# myset.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_MYSET_H
    00030 #define MYSQLPP_MYSET_H
    00031
    00032 #include "defs.h"
    00033
    00034 #include "coldata.h"
    00035 #include "stream2string.h"
    00036
    00037 #include <iostream>
    00038 #include <set>
    00039 #include <vector>
    00040
    00041 namespace mysqlpp {
    00042
    00043 #if !defined(DOXYGEN_IGNORE)
    00044 // Doxygen will not generate documentation for this section.
    00045
    00046 template <class T, class value_type = typename T::value_type>
    00047 class ListInsert
    00048 {
    00049 public:
    00050         ListInsert(T* o) : object_(o) { }
    00051         void operator ()(const value_type& data) { object_->push_back(data); }
    00052
    00053 private:
    00054         T* object_;
    00055 };
    00056
    00057 template <class T, class key_type = typename T::key_type>
    00058 class SetInsert
    00059 {
    00060 public:
    00061         SetInsert(T* o) : object_(o) { }
    00062         void operator ()(const key_type& data) { object_->insert(data); }
    00063
    00064 private:
    00065         T* object_;
    00066 };
    00067
    00068 template <class T>
    00069 inline SetInsert< std::set<T> > set_insert(std::set<T>* o)
    00070 {
    00071         return SetInsert< std::set<T> >(o);
    00072 }
    00073
    00074 template <class T>
    00075 inline ListInsert< std::vector<T> > set_insert(std::vector<T> *o)
    00076 {
    00077         return ListInsert< std::vector<T> >(o);
    00078 }
    00079
    00080 template <class Insert>
    00081 void set2container(const char* str, Insert insert);
    00082
    00083 #endif // !defined(DOXYGEN_IGNORE)
    00084
    00085
    00087
    00088 template <class Container = std::set< std::string> >
    00089 class Set : public Container
    00090 {
    00091 public:
    00093         Set(const char* str)
    00094         {
    00095                 set2container(str, set_insert(this));
    00096         }
    00097
    00099         Set(const std::string& str)
    00100         {
    00101                 set2container(str.c_str(), set_insert(this));
    00102         }
    00103
    00105         Set(const ColData& str)
    00106         {
    00107                 set2container(str.c_str(), set_insert(this));
    00108         }
    00109
    00112         std::ostream& out_stream(std::ostream& s) const
    00113         {
    00114                 typename Container::const_iterator i = Container::begin();
    00115                 typename Container::const_iterator e = Container::end();
    00116
    00117                 while (true) {
    00118                         s << *i;
    00119                         if (++i == e) {
    00120                                 break;
    00121                         }
    00122                         s << ",";
    00123                 }
    00124
    00125                 return s;
    00126         }
    00127
    00130         operator std::string();
    00131 };
    00132
    00133
    00135 template <class Container>
    00136 inline std::ostream& operator <<(std::ostream& s,
    00137                 const Set<Container>& d)
    00138 {
    00139         return d.out_stream(s);
    00140 }
    00141
    00142
    00143 template <class Container>
    00144 inline Set<Container>::operator std::string()
    00145 {
    00146         return stream2string<std::string>(*this);
    00147 }
    00148
    00149
    00150 #if !defined(DOXYGEN_IGNORE)
    00151 // Doxygen will not generate documentation for this section.
    00152
    00153 template <class Insert>
    00154 void set2container(const char* str, Insert insert)
    00155 {
    00156         while (1) {
    00157                 MutableColData s("");
    00158                 while (*str != ',' && *str) {
    00159                         s += *str;
    00160                         str++;
    00161                 }
    00162
    00163                 insert(s);
    00164
    00165                 if (!*str) {
    00166                         break;
    00167                 }
    00168
    00169                 str++;
    00170         }
    00171 }
    00172
    00173 #endif // !defined(DOXYGEN_IGNORE)
    00174
    00175
    00176 } // end namespace mysqlpp
    00177
    00178 #endif