# myset.h

Go to the documentation of this file.

    00001
    00002
    00003
    00004
    00005 /***********************************************************************
    00006  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
    00007  (c) 2004-2007 by Educational Technology Resources, Inc.  Others may
    00008  also hold copyrights on code in this file.  See the CREDITS.txt file
    00009  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 "mystring.h"
    00035 #include "stream2string.h"
    00036
    00037 #include <iostream>
    00038 #include <set>
    00039
    00040 namespace mysqlpp {
    00041
    00042 #if !defined(DOXYGEN_IGNORE)
    00043 // Doxygen will not generate documentation for this section.
    00044
    00045 template <class T, class key_type = typename T::key_type>
    00046 class MYSQLPP_EXPORT SetInsert
    00047 {
    00048 public:
    00049         SetInsert(T* o) : object_(o) { }
    00050         void operator ()(const key_type& data) { object_->insert(data); }
    00051
    00052 private:
    00053         T* object_;
    00054 };
    00055
    00056 template <class T>
    00057 inline SetInsert< std::set<T> > set_insert(std::set<T>* o)
    00058 {
    00059         return SetInsert< std::set<T> >(o);
    00060 }
    00061
    00062 template <class Insert>
    00063 void set2container(const char* str, Insert insert);
    00064
    00065 #endif // !defined(DOXYGEN_IGNORE)
    00066
    00067
    00069
    00070 template <class Container = std::set<std::string> >
    00071 class MYSQLPP_EXPORT Set : public Container
    00072 {
    00073 public:
    00075         Set() {};
    00076
    00078         Set(const char* str)
    00079         {
    00080                 set2container(str, set_insert(this));
    00081         }
    00082
    00084         Set(const std::string& str)
    00085         {
    00086                 set2container(str.c_str(), set_insert(this));
    00087         }
    00088
    00090         Set(const String& str)
    00091         {
    00092                 set2container(str.c_str(), set_insert(this));
    00093         }
    00094
    00097         operator std::string() const { return stream2string(*this); }
    00098
    00100         std::string str() const { return *this; }
    00101 };
    00102
    00103
    00105 template <class Container>
    00106 inline std::ostream& operator <<(std::ostream& s,
    00107                 const Set<Container>& d)
    00108 {
    00109         typename Container::const_iterator i = d.begin();
    00110         typename Container::const_iterator e = d.end();
    00111
    00112         if (i != e) {
    00113                 while (true) {
    00114                         s << *i;
    00115                         if (++i == e) {
    00116                                 break;
    00117                         }
    00118                         s << ",";
    00119                 }
    00120         }
    00121
    00122         return s;
    00123 }
    00124
    00125
    00126 #if !defined(DOXYGEN_IGNORE)
    00127 // Doxygen will not generate documentation for this section.
    00128
    00129 template <class Insert>
    00130 void set2container(const char* str, Insert insert)
    00131 {
    00132         std::string temp;
    00133
    00134         // Break str up using comma separators
    00135         while (str && *str) {
    00136                 if (*str == ',') {
    00137                         insert(temp);
    00138                         temp.clear();
    00139
    00140                         // Handle comma at end of string case
    00141                         if (*++str) {
    00142                                 ++str;
    00143                         }
    00144                 }
    00145                 else {
    00146                         temp += *str++;
    00147                 }
    00148         }
    00149
    00150         // Save final element of set, if any
    00151         if (temp.size()) {
    00152                 insert(temp);
    00153         }
    00154 }
    00155
    00156 #endif // !defined(DOXYGEN_IGNORE)
    00157
    00158
    00159 } // end namespace mysqlpp
    00160
    00161 #endif