00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00045
00046 template <class T, class value_type = typename T::value_type>
00047 class ListInsert
00048 {
00049 private:
00050 T* object;
00051
00052 public:
00053 ListInsert(T* o) : object(o) { }
00054 void operator ()(const value_type& data) { object->push_back(data); }
00055 };
00056
00057 template <class T, class key_type = typename T::key_type>
00058 class SetInsert
00059 {
00060 private:
00061 T* object;
00062
00063 public:
00064 SetInsert(T* o) : object(o) { }
00065 void operator ()(const key_type& data) { object->insert(data); }
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
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 }
00177
00178 #endif