00001 #ifndef MYSQLPP_MYSET_H
00002 #define MYSQLPP_MYSET_H
00003
00007
00008 #include "defs.h"
00009
00010 #include "coldata.h"
00011 #include "stream2string.h"
00012
00013 #include <iostream>
00014 #include <set>
00015 #include <vector>
00016
00017 namespace mysqlpp {
00018
00020
00021
00022 template <class T, class value_type = typename T::value_type>
00023 class ListInsert {
00024 private:
00025 T *object;
00026 public:
00027 ListInsert(T *o) {object = o;}
00028 void operator () (const value_type &data) {object->push_back(data);}
00029 };
00030
00031 template <class T, class key_type = typename T::key_type>
00032 class SetInsert {
00033 private:
00034 T *object;
00035 public:
00036 SetInsert(T *o) {object = o;}
00037 void operator () (const key_type &data) {object->insert(data);}
00038 };
00039
00040 template <class T>
00041 inline SetInsert<std::set<T> > set_insert(std::set<T> *o)
00042 {
00043 return SetInsert<std::set<T> >(o);
00044 }
00045
00046 template <class T>
00047 inline ListInsert<std::vector<T> > set_insert(std::vector<T> *o)
00048 {
00049 return ListInsert<std::vector<T> >(o);
00050 }
00051
00052 template <class Insert>
00053 void set2container (const char *str, Insert insert);
00054
00056
00057
00059 template <class Container = std::set<std::string> >
00060 class Set : public Container {
00061 public:
00062 Set(const char* str) { set2container(str, set_insert(this)); }
00063 Set(const std::string &str)
00064 {
00065 set2container(str.c_str(), set_insert(this));
00066 }
00067 Set(const ColData& str)
00068 {
00069 set2container(str.c_str(), set_insert(this));
00070 }
00071
00072 std::ostream& out_stream(std::ostream &s) const;
00073
00074 operator std::string ();
00075 };
00076
00077
00078 template <class Container>
00079 inline std::ostream& operator << (std::ostream &s, const Set<Container> &d)
00080 {
00081 return d.out_stream(s);
00082 }
00083
00084
00085 template <class Container>
00086 inline Set<Container>::operator std::string ()
00087 {
00088 return stream2string<std::string>(*this);
00089 }
00090
00091
00092 template <class Insert>
00093 void set2container (const char *str, Insert insert)
00094 {
00095 while (1) {
00096 MutableColData s("");
00097 while (*str != ',' && *str) {
00098 s += *str;
00099 str++;
00100 }
00101 insert(s);
00102 if (!*str) break;
00103 str++;
00104 }
00105 }
00106
00107 template <class Container>
00108 std::ostream& Set<Container>::out_stream (std::ostream &s) const
00109 {
00110 typename Container::const_iterator i = Container::begin();
00111 typename Container::const_iterator e = Container::end();
00112 while (true) {
00113 s << *i;
00114 i++;
00115 if (i==e) break;
00116 s << ",";
00117 }
00118 return s;
00119 }
00120
00121 }
00122
00123 #endif
00124