MySQL++ Reference Manual

MySQL++: myset.h Source File

MySQL++ 3.3.0

myset.h
Go to the documentation of this file.
1
5 /***********************************************************************
6 Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
7 (c) 2004-2007 by Educational Technology Resources, Inc. Others may
8 also hold copyrights on code in this file. See the CREDITS.txt file
9 in the top directory of the distribution for details.
10
11 This file is part of MySQL++.
12
13 MySQL++ is free software; you can redistribute it and/or modify it
14 under the terms of the GNU Lesser General Public License as published
15 by the Free Software Foundation; either version 2.1 of the License, or
16 (at your option) any later version.
17
18 MySQL++ is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21 License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with MySQL++; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
26 USA
27 ***********************************************************************/
28
29 #ifndef MYSQLPP_MYSET_H
30 #define MYSQLPP_MYSET_H
31
32 #include "common.h"
33
34 #include "mystring.h"
35 #include "stream2string.h"
36
37 #include <iostream>
38 #include <set>
39
40 namespace mysqlpp {
41
42 #if !defined(DOXYGEN_IGNORE)
43 // Doxygen will not generate documentation for this section.
44
45 template <class T, class key_type = typename T::key_type>
46 class MYSQLPP_EXPORT SetInsert
47 {
48 public:
49 SetInsert(T* o) : object_(o) { }
50 void operator ()(const key_type& data) { object_->insert(data); }
51
52 private:
53 T* object_;
54 };
55
56 template <class T>
57 inline SetInsert< std::set<T> > set_insert(std::set<T>* o)
58 {
59 return SetInsert< std::set<T> >(o);
60 }
61
62 template <class Insert>
63 void set2container(const char* str, Insert insert);
64
65 #endif // !defined(DOXYGEN_IGNORE)
66
67
69
70 template <class Container = std::set<std::string> >
71 class MYSQLPP_EXPORT Set : public Container
72 {
73 public:
75 Set() {};
76
78 Set(const char* str)
79 {
80 set2container(str, set_insert(this));
81 }
82
84 Set(const std::string& str)
85 {
86 set2container(str.c_str(), set_insert(this));
87 }
88
90 Set(const String& str)
91 {
92 set2container(str.c_str(), set_insert(this));
93 }
94
97 operator std::string() const { return stream2string(*this); }
98
100 std::string str() const { return *this; }
101 };
102
103
105 template <class Container>
106 inline std::ostream& operator <<(std::ostream& s,
107 const Set<Container>& d)
108 {
109 typename Container::const_iterator i = d.begin();
110 typename Container::const_iterator e = d.end();
111
112 if (i != e) {
113 while (true) {
114 s << *i;
115 if (++i == e) {
116 break;
117 }
118 s << ",";
119 }
120 }
121
122 return s;
123 }
124
125
126 #if !defined(DOXYGEN_IGNORE)
127 // Doxygen will not generate documentation for this section.
128
129 template <class Insert>
130 void set2container(const char* str, Insert insert)
131 {
132 std::string temp;
133
134 // Break str up using comma separators
135 while (str && *str) {
136 if (*str == ',') {
137 insert(temp);
138 temp.clear();
139
140 // Handle comma at end of string case
141 if (*++str) {
142 ++str;
143 }
144 }
145 else {
146 temp += *str++;
147 }
148 }
149
150 // Save final element of set, if any
151 if (!temp.empty()) {
152 insert(temp);
153 }
154 }
155
156 #endif // !defined(DOXYGEN_IGNORE)
157
158
159 } // end namespace mysqlpp
160
161 #endif
mysqlpp::Set
A special std::set derivative for holding MySQL data sets.
Definition: myset.h:72
mysqlpp::Set::Set
Set(const std::string &str)
Create object from a comma-separated list of values.
Definition: myset.h:84
mysqlpp::Set::Set
Set()
Default constructor.
Definition: myset.h:75
mysqlpp::Set::Set
Set(const String &str)
Create object from a comma-separated list of values.
Definition: myset.h:90
mysqlpp::Set::Set
Set(const char *str)
Create object from a comma-separated list of values.
Definition: myset.h:78
mysqlpp::Set::str
std::string str() const
Return our value in std::string form.
Definition: myset.h:100
mysqlpp::String
A std::string work-alike that can convert itself from SQL text data formats to C++ data types.
Definition: mystring.h:140
mysqlpp::String::c_str
const char * c_str() const
Return a const pointer to the string data.
Definition: mystring.h:288
common.h
This file includes top-level definitions for use both internal to the library, and outside it....
mystring.h
Declares String class, MySQL++'s generic std::string-like class, used for holding data received from ...
stream2string.h
Declares an adapter that converts something that can be inserted into a C++ stream into a std::string...
mysqlpp::stream2string
std::string stream2string(const T &object)
Converts anything you can insert into a C++ stream to a std::string via std::ostringstream.
Definition: stream2string.h:41