# 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 "common.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 key_type = typename T::key_type>
    00047 class MYSQLPP_EXPORT SetInsert
    00048 {
    00049 public:
    00050         SetInsert(T* o) : object_(o) { }
    00051         void operator ()(const key_type& data) { object_->insert(data); }
    00052
    00053 private:
    00054         T* object_;
    00055 };
    00056
    00057 template <class T>
    00058 inline SetInsert< std::set<T> > set_insert(std::set<T>* o)
    00059 {
    00060         return SetInsert< std::set<T> >(o);
    00061 }
    00062
    00063 template <class Insert>
    00064 void set2container(const char* str, Insert insert);
    00065
    00066 #endif // !defined(DOXYGEN_IGNORE)
    00067
    00068
    00070
    00071 template <class Container = std::set<std::string> >
    00072 class MYSQLPP_EXPORT Set : public Container
    00073 {
    00074 public:
    00076         Set() {};
    00077
    00079         Set(const char* str)
    00080         {
    00081                 set2container(str, set_insert(this));
    00082         }
    00083
    00085         Set(const std::string& str)
    00086         {
    00087                 set2container(str.c_str(), set_insert(this));
    00088         }
    00089
    00091         Set(const ColData& str)
    00092         {
    00093                 set2container(str.c_str(), set_insert(this));
    00094         }
    00095
    00098         std::ostream& out_stream(std::ostream& s) const
    00099         {
    00100                 typename Container::const_iterator i = Container::begin();
    00101                 typename Container::const_iterator e = Container::end();
    00102
    00103                 while (true) {
    00104                         s << *i;
    00105                         if (++i == e) {
    00106                                 break;
    00107                         }
    00108                         s << ",";
    00109                 }
    00110
    00111                 return s;
    00112         }
    00113
    00116         operator std::string() { return stream2string<std::string>(*this); }
    00117 };
    00118
    00119
    00121 template <class Container>
    00122 inline std::ostream& operator <<(std::ostream& s,
    00123                 const Set<Container>& d)
    00124 {
    00125         return d.out_stream(s);
    00126 }
    00127
    00128
    00129 #if !defined(DOXYGEN_IGNORE)
    00130 // Doxygen will not generate documentation for this section.
    00131
    00132 template <class Insert>
    00133 void set2container(const char* str, Insert insert)
    00134 {
    00135         while (1) {
    00136                 MutableColData s("");
    00137                 while (*str != ',' && *str) {
    00138                         s += *str;
    00139                         str++;
    00140                 }
    00141
    00142                 insert(s);
    00143
    00144                 if (!*str) {
    00145                         break;
    00146                 }
    00147
    00148                 str++;
    00149         }
    00150 }
    00151
    00152 #endif // !defined(DOXYGEN_IGNORE)
    00153
    00154
    00155 } // end namespace mysqlpp
    00156
    00157 #endif