MySQL++: myset.h Source File
myset.h
Go to the documentation of this file.
/***********************************************************************
Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
(c) 2004-2007 by Educational Technology Resources, Inc. Others may
also hold copyrights on code in this file. See the CREDITS.txt file
in the top directory of the distribution for details.
This file is part of MySQL++.
MySQL++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
MySQL++ is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with MySQL++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
***********************************************************************/
#ifndef MYSQLPP_MYSET_H
#define MYSQLPP_MYSET_H
#include "common.h"
#include "mystring.h"
#include "stream2string.h"
#include <iostream>
#include <set>
namespace mysqlpp {
#if !defined(DOXYGEN_IGNORE)
// Doxygen will not generate documentation for this section.
template <class T, class key_type = typename T::key_type>
class MYSQLPP_EXPORT SetInsert
{
public:
SetInsert(T* o) : object_(o) { }
void operator ()(const key_type& data) { object_->insert(data); }
private:
T* object_;
};
template <class T>
inline SetInsert< std::set<T> > set_insert(std::set<T>* o)
{
return SetInsert< std::set<T> >(o);
}
template <class Insert>
void set2container(const char* str, Insert insert);
#endif // !defined(DOXYGEN_IGNORE)
template <class Container = std::set<std::string> >
class MYSQLPP_EXPORT Set : public Container
{
public:
Set() {};
Set(const char* str)
{
set2container(str, set_insert(this));
}
Set(const std::string& str)
{
set2container(str.c_str(), set_insert(this));
}
Set(const String& str)
{
set2container(str.c_str(), set_insert(this));
}
operator std::string() const { return stream2string(*this); }
std::string str() const { return *this; }
};
template <class Container>
inline std::ostream& operator <<(std::ostream& s,
const Set<Container>& d)
{
typename Container::const_iterator i = d.begin();
typename Container::const_iterator e = d.end();
if (i != e) {
while (true) {
s << *i;
if (++i == e) {
break;
}
s << ",";
}
}
return s;
}
#if !defined(DOXYGEN_IGNORE)
// Doxygen will not generate documentation for this section.
template <class Insert>
void set2container(const char* str, Insert insert)
{
std::string temp;
// Break str up using comma separators
while (str && *str) {
if (*str == ',') {
insert(temp);
temp.clear();
// Handle comma at end of string case
if (*++str) {
++str;
}
}
else {
temp += *str++;
}
}
// Save final element of set, if any
if (temp.size()) {
insert(temp);
}
}
#endif // !defined(DOXYGEN_IGNORE)
} // end namespace mysqlpp
#endif
mysqlpp::Set
A special std::set derivative for holding MySQL data sets.
Definition: myset.h:71
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
mysqlpp::Set::Set
Set(const char *str)
Create object from a comma-separated list of values.
Definition: myset.h:78
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::String::c_str
const char * c_str() const
Return a const pointer to the string data.
Definition: mystring.h:288
mysqlpp::Set::Set
Set(const std::string &str)
Create object from a comma-separated list of values.
Definition: myset.h:84
stream2string.h
Declares an adapter that converts something that can be inserted into a C++ stream into a std::string...
common.h
This file includes top-level definitions for use both internal to the library, and outside it....
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:139
mystring.h
Declares String class, MySQL++'s generic std::string-like class, used for holding data received from ...