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 "mystring.h"
00035 #include "stream2string.h"
00036
00037 #include <iostream>
00038 #include <set>
00039
00040 namespace mysqlpp {
00041
00042 #if !defined(DOXYGEN_IGNORE)
00043
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
00128
00129 template <class Insert>
00130 void set2container(const char* str, Insert insert)
00131 {
00132 std::string temp;
00133
00134
00135 while (str && *str) {
00136 if (*str == ',') {
00137 insert(temp);
00138 temp.clear();
00139
00140
00141 if (*++str) {
00142 ++str;
00143 }
00144 }
00145 else {
00146 temp += *str++;
00147 }
00148 }
00149
00150
00151 if (temp.size()) {
00152 insert(temp);
00153 }
00154 }
00155
00156 #endif // !defined(DOXYGEN_IGNORE)
00157
00158
00159 }
00160
00161 #endif