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 "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
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
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 }
00156
00157 #endif