strconv.hxx
/*-------------------------------------------------------------------------
*
* FILE
* pqxx/stringconv.hxx
*
* DESCRIPTION
* String conversion definitions for libpqxx
* DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/stringconv instead.
*
* Copyright (c) 2008-2011, Jeroen T. Vermeulen <jtv@xs4all.nl>
*
* See COPYING for copyright license. If you did not receive a file called
* COPYING with this source code, please notify the distributor of this mistake,
* or contact the author.
*
*-------------------------------------------------------------------------
*/
#ifndef PQXX_H_STRINGCONV
#define PQXX_H_STRINGCONV
#include "pqxx/compiler-public.hxx"
#include <sstream>
#include <stdexcept>
namespace pqxx
{
template<typename T> struct string_traits {};
namespace internal
{
void PQXX_LIBEXPORT PQXX_NORETURN throw_null_conversion(
const PGSTD::string &type);
} // namespace pqxx::internal
#define PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(T) \
template<> struct PQXX_LIBEXPORT string_traits<T> \
{ \
typedef T subject_type; \
static const char *name() { return #T; } \
static bool has_null() { return false; } \
static bool is_null(T) { return false; } \
static T null() \
{ internal::throw_null_conversion(name()); return subject_type(); } \
static void from_string(const char Str[], T &Obj); \
static PGSTD::string to_string(T Obj); \
};
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(bool)
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(short)
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(unsigned short)
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(int)
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(unsigned int)
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(long)
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(unsigned long)
#ifdef PQXX_HAVE_LONG_LONG
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(long long)
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(unsigned long long)
#endif
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(float)
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(double)
#ifdef PQXX_HAVE_LONG_DOUBLE
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(long double)
#endif
#undef PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION
template<> struct PQXX_LIBEXPORT string_traits<const char *>
{
static const char *name() { return "const char *"; }
static bool has_null() { return true; }
static bool is_null(const char *t) { return !t; }
static const char *null() { return NULL; }
static void from_string(const char Str[], const char *&Obj) { Obj = Str; }
static PGSTD::string to_string(const char *Obj) { return Obj; }
};
template<> struct PQXX_LIBEXPORT string_traits<char *>
{
static const char *name() { return "char *"; }
static bool has_null() { return true; }
static bool is_null(const char *t) { return !t; }
static const char *null() { return NULL; }
// Don't allow this conversion since it breaks const-safety.
// static void from_string(const char Str[], char *&Obj);
static PGSTD::string to_string(char *Obj) { return Obj; }
};
template<size_t N> struct PQXX_LIBEXPORT string_traits<char[N]>
{
static const char *name() { return "char[]"; }
static bool has_null() { return true; }
static bool is_null(const char t[]) { return !t; }
static const char *null() { return NULL; }
static PGSTD::string to_string(const char Obj[]) { return Obj; }
};
template<size_t N> struct PQXX_LIBEXPORT string_traits<const char[N]>
{
static const char *name() { return "char[]"; }
static bool has_null() { return true; }
static bool is_null(const char t[]) { return !t; }
static const char *null() { return NULL; }
static PGSTD::string to_string(const char Obj[]) { return Obj; }
};
template<> struct PQXX_LIBEXPORT string_traits<PGSTD::string>
{
static const char *name() { return "string"; }
static bool has_null() { return false; }
static bool is_null(const PGSTD::string &) { return false; }
static PGSTD::string null()
{ internal::throw_null_conversion(name()); return PGSTD::string(); }
static void from_string(const char Str[], PGSTD::string &Obj) { Obj=Str; }
static PGSTD::string to_string(const PGSTD::string &Obj) { return Obj; }
};
template<> struct PQXX_LIBEXPORT string_traits<const PGSTD::string>
{
static const char *name() { return "const string"; }
static bool has_null() { return false; }
static bool is_null(const PGSTD::string &) { return false; }
static const PGSTD::string null()
{ internal::throw_null_conversion(name()); return PGSTD::string(); }
static const PGSTD::string to_string(const PGSTD::string &Obj) { return Obj; }
};
template<> struct PQXX_LIBEXPORT string_traits<PGSTD::stringstream>
{
static const char *name() { return "stringstream"; }
static bool has_null() { return false; }
static bool is_null(const PGSTD::stringstream &) { return false; }
static PGSTD::stringstream null()
{
internal::throw_null_conversion(name());
// No, dear compiler, we don't need a return here.
throw 0;
}
static void from_string(const char Str[], PGSTD::stringstream &Obj)
{ Obj.clear(); Obj << Str; }
static PGSTD::string to_string(const PGSTD::stringstream &Obj)
{ return Obj.str(); }
};
// TODO: Implement date conversions
template<typename T>
inline void from_string(const char Str[], T &Obj)
{
if (!Str)
throw PGSTD::runtime_error("Attempt to read NULL string");
string_traits<T>::from_string(Str, Obj);
}
template<typename T> inline void from_string(const char Str[], T &Obj, size_t)
{
return from_string(Str, Obj);
}
template<>
inline void from_string<PGSTD::string>(const char Str[],
PGSTD::string &Obj,
size_t len) //[t0]
{
if (!Str)
throw PGSTD::runtime_error("Attempt to read NULL string");
Obj.assign(Str, len);
}
template<typename T>
inline void from_string(const PGSTD::string &Str, T &Obj) //[t45]
{ from_string(Str.c_str(), Obj); }
template<typename T>
inline void from_string(const PGSTD::stringstream &Str, T &Obj) //[t0]
{ from_string(Str.str(), Obj); }
template<> inline void
from_string(const PGSTD::string &Str, PGSTD::string &Obj) //[t46]
{ Obj = Str; }
namespace internal
{
inline int digit_to_number(char c) throw () { return c-'0'; }
inline char number_to_digit(int i) throw () { return static_cast<char>(i+'0'); }
} // namespace pqxx::internal
template<typename T> inline PGSTD::string to_string(const T &Obj)
{ return string_traits<T>::to_string(Obj); }
} // namespace pqxx
#endif
pqxx::string_traits< std::stringstream >::is_null
static bool is_null(const std::stringstream &)
Definition: strconv.hxx:163
pqxx::string_traits< const std::string >::to_string
static const std::string to_string(const std::string &Obj)
Definition: strconv.hxx:156
pqxx::string_traits< const std::string >::is_null
static bool is_null(const std::string &)
Definition: strconv.hxx:153
pqxx::string_traits< const char * >::to_string
static std::string to_string(const char *Obj)
Definition: strconv.hxx:97
pqxx::string_traits< const char * >::has_null
static bool has_null()
Definition: strconv.hxx:93
pqxx::string_traits< std::stringstream >::null
static std::stringstream null()
Definition: strconv.hxx:164
pqxx::string_traits< char * >::is_null
static bool is_null(const char *t)
Definition: strconv.hxx:105
pqxx::string_traits< const char[N]>::null
static const char * null()
Definition: strconv.hxx:133
pqxx::string_traits< std::string >::null
static std::string null()
Definition: strconv.hxx:143
pqxx::string_traits< char * >::has_null
static bool has_null()
Definition: strconv.hxx:104
pqxx::from_string
void from_string(const field &F, T &Obj)
Convert a field&#39;s string contents to another type.
Definition: result.hxx:462
pqxx::string_traits< const char[N]>::is_null
static bool is_null(const char t[])
Definition: strconv.hxx:132
pqxx::string_traits< const char * >::from_string
static void from_string(const char Str[], const char *&Obj)
Definition: strconv.hxx:96
pqxx::string_traits< std::string >::is_null
static bool is_null(const std::string &)
Definition: strconv.hxx:142
pqxx::string_traits< std::string >::from_string
static void from_string(const char Str[], std::string &Obj)
Definition: strconv.hxx:145
PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION
#define PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(T)
Definition: strconv.hxx:55
pqxx::string_traits< const char * >::is_null
static bool is_null(const char *t)
Definition: strconv.hxx:94
pqxx::string_traits< char[N]>::to_string
static std::string to_string(const char Obj[])
Definition: strconv.hxx:121
pqxx::string_traits< std::stringstream >::name
static const char * name()
Definition: strconv.hxx:161
pqxx::string_traits< char * >::name
static const char * name()
Definition: strconv.hxx:103
pqxx::string_traits< const char[N]>::has_null
static bool has_null()
Definition: strconv.hxx:131
pqxx::string_traits< char[N]>::has_null
static bool has_null()
Definition: strconv.hxx:118
pqxx::string_traits< const std::string >::name
static const char * name()
Definition: strconv.hxx:151
pqxx::string_traits< std::string >::to_string
static std::string to_string(const std::string &Obj)
Definition: strconv.hxx:146
pqxx::string_traits< std::string >::name
static const char * name()
Definition: strconv.hxx:140
pqxx::string_traits< const char[N]>::name
static const char * name()
Definition: strconv.hxx:130
pqxx::string_traits< char[N]>::null
static const char * null()
Definition: strconv.hxx:120
pqxx::string_traits
Traits class for use in string conversions.
Definition: strconv.hxx:46
pqxx::to_string
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.hxx:467
pqxx::internal::throw_null_conversion
void PQXX_NORETURN throw_null_conversion(const std::string &type)
Throw exception for attempt to convert null to given type.
Definition: strconv.cxx:353
pqxx::string_traits< std::string >::has_null
static bool has_null()
Definition: strconv.hxx:141
pqxx::string_traits< char * >::null
static const char * null()
Definition: strconv.hxx:106
pqxx::string_traits< const std::string >::has_null
static bool has_null()
Definition: strconv.hxx:152
pqxx::string_traits< char[N]>::is_null
static bool is_null(const char t[])
Definition: strconv.hxx:119
pqxx::from_string
void from_string(const std::string &Str, std::string &Obj)
Definition: strconv.hxx:232
pqxx::to_string
std::string to_string(const T &Obj)
Convert built-in type to a readable string that PostgreSQL will understand.
Definition: strconv.hxx:249
pqxx::string_traits< std::stringstream >::has_null
static bool has_null()
Definition: strconv.hxx:162
pqxx::internal::number_to_digit
char number_to_digit(int i)
Definition: strconv.hxx:240
pqxx::string_traits< char * >::to_string
static std::string to_string(char *Obj)
Definition: strconv.hxx:111
pqxx::internal::digit_to_number
int digit_to_number(char c)
Compute numeric value of given textual digit (assuming that it is a digit)
Definition: strconv.hxx:239
pqxx::string_traits< const std::string >::null
static const std::string null()
Definition: strconv.hxx:154
pqxx::string_traits< std::stringstream >::from_string
static void from_string(const char Str[], std::stringstream &Obj)
Definition: strconv.hxx:170
pqxx::string_traits< const char[N]>::to_string
static std::string to_string(const char Obj[])
Definition: strconv.hxx:134
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: basic_connection.hxx:35
pqxx::string_traits< char[N]>::name
static const char * name()
Definition: strconv.hxx:117
pqxx::string_traits< const char * >::name
static const char * name()
Definition: strconv.hxx:92
pqxx::string_traits< const char * >::null
static const char * null()
Definition: strconv.hxx:95
pqxx::string_traits< std::stringstream >::to_string
static std::string to_string(const std::stringstream &Obj)
Definition: strconv.hxx:172