strconv.hxx

00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/stringconv.hxx
00005  *
00006  *   DESCRIPTION
00007  *      String conversion definitions for libpqxx
00008  *      DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/stringconv instead.
00009  *
00010  * Copyright (c) 2008-2015, Jeroen T. Vermeulen <jtv@xs4all.nl>
00011  *
00012  * See COPYING for copyright license.  If you did not receive a file called
00013  * COPYING with this source code, please notify the distributor of this mistake,
00014  * or contact the author.
00015  *
00016  *-------------------------------------------------------------------------
00017  */
00018 #ifndef PQXX_H_STRINGCONV
00019 #define PQXX_H_STRINGCONV
00020 
00021 #include "pqxx/compiler-public.hxx"
00022 
00023 #include <sstream>
00024 #include <stdexcept>
00025 
00026 
00027 namespace pqxx
00028 {
00029 
00041 
00043 
00046 template<typename T> struct string_traits {};
00047 
00048 namespace internal
00049 {
00051 PQXX_NORETURN PQXX_LIBEXPORT void throw_null_conversion(
00052         const std::string &type);
00053 } // namespace pqxx::internal
00054 
00055 #define PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(T)                    \
00056 template<> struct PQXX_LIBEXPORT string_traits<T>                       \
00057 {                                                                       \
00058   typedef T subject_type;                                               \
00059   static const char *name() { return #T; }                              \
00060   static bool has_null() { return false; }                              \
00061   static bool is_null(T) { return false; }                              \
00062   static T null()                                                       \
00063     { internal::throw_null_conversion(name()); return subject_type(); } \
00064   static void from_string(const char Str[], T &Obj);                    \
00065   static std::string to_string(T Obj);                                  \
00066 };
00067 
00068 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(bool)
00069 
00070 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(short)
00071 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(unsigned short)
00072 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(int)
00073 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(unsigned int)
00074 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(long)
00075 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(unsigned long)
00076 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(long long)
00077 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(unsigned long long)
00078 
00079 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(float)
00080 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(double)
00081 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(long double)
00082 
00083 #undef PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION
00084 
00086 template<> struct PQXX_LIBEXPORT string_traits<const char *>
00087 {
00088   static const char *name() { return "const char *"; }
00089   static bool has_null() { return true; }
00090   static bool is_null(const char *t) { return !t; }
00091   static const char *null() { return NULL; }
00092   static void from_string(const char Str[], const char *&Obj) { Obj = Str; }
00093   static std::string to_string(const char *Obj) { return Obj; }
00094 };
00095 
00097 template<> struct PQXX_LIBEXPORT string_traits<char *>
00098 {
00099   static const char *name() { return "char *"; }
00100   static bool has_null() { return true; }
00101   static bool is_null(const char *t) { return !t; }
00102   static const char *null() { return NULL; }
00103 
00104   // Don't allow this conversion since it breaks const-safety.
00105   // static void from_string(const char Str[], char *&Obj);
00106 
00107   static std::string to_string(char *Obj) { return Obj; }
00108 };
00109 
00111 template<size_t N> struct PQXX_LIBEXPORT string_traits<char[N]>
00112 {
00113   static const char *name() { return "char[]"; }
00114   static bool has_null() { return true; }
00115   static bool is_null(const char t[]) { return !t; }
00116   static const char *null() { return NULL; }
00117   static std::string to_string(const char Obj[]) { return Obj; }
00118 };
00119 
00121 
00124 template<size_t N> struct PQXX_LIBEXPORT string_traits<const char[N]>
00125 {
00126   static const char *name() { return "char[]"; }
00127   static bool has_null() { return true; }
00128   static bool is_null(const char t[]) { return !t; }
00129   static const char *null() { return NULL; }
00130   static std::string to_string(const char Obj[]) { return Obj; }
00131 };
00132 
00133 
00134 template<> struct PQXX_LIBEXPORT string_traits<std::string>
00135 {
00136   static const char *name() { return "string"; }
00137   static bool has_null() { return false; }
00138   static bool is_null(const std::string &) { return false; }
00139   static std::string null()
00140         { internal::throw_null_conversion(name()); return std::string(); }
00141   static void from_string(const char Str[], std::string &Obj) { Obj=Str; }
00142   static std::string to_string(const std::string &Obj) { return Obj; }
00143 };
00144 
00145 template<> struct PQXX_LIBEXPORT string_traits<const std::string>
00146 {
00147   static const char *name() { return "const string"; }
00148   static bool has_null() { return false; }
00149   static bool is_null(const std::string &) { return false; }
00150   static const std::string null()
00151         { internal::throw_null_conversion(name()); return std::string(); }
00152   static const std::string to_string(const std::string &Obj) { return Obj; }
00153 };
00154 
00155 template<> struct PQXX_LIBEXPORT string_traits<std::stringstream>
00156 {
00157   static const char *name() { return "stringstream"; }
00158   static bool has_null() { return false; }
00159   static bool is_null(const std::stringstream &) { return false; }
00160   static std::stringstream null()
00161   {
00162     internal::throw_null_conversion(name());
00163     // No, dear compiler, we don't need a return here.
00164     throw 0;
00165   }
00166   static void from_string(const char Str[], std::stringstream &Obj)
00167                                                     { Obj.clear(); Obj << Str; }
00168   static std::string to_string(const std::stringstream &Obj)
00169                                                            { return Obj.str(); }
00170 };
00171 
00172 
00173 // TODO: Implement date conversions
00174 
00176 
00188 template<typename T>
00189   inline void from_string(const char Str[], T &Obj)
00190 {
00191   if (!Str)
00192     throw std::runtime_error("Attempt to read NULL string");
00193   string_traits<T>::from_string(Str, Obj);
00194 }
00195 
00196 
00198 
00204 template<typename T> inline void from_string(const char Str[], T &Obj, size_t)
00205 {
00206   return from_string(Str, Obj);
00207 }
00208 
00209 template<>
00210   inline void from_string<std::string>(const char Str[],
00211         std::string &Obj,
00212         size_t len)                                                     //[t0]
00213 {
00214   if (!Str)
00215     throw std::runtime_error("Attempt to read NULL string");
00216   Obj.assign(Str, len);
00217 }
00218 
00219 template<typename T>
00220   inline void from_string(const std::string &Str, T &Obj)               //[t45]
00221         { from_string(Str.c_str(), Obj); }
00222 
00223 template<typename T>
00224   inline void from_string(const std::stringstream &Str, T &Obj)         //[t0]
00225         { from_string(Str.str(), Obj); }
00226 
00227 template<> inline void
00228 from_string(const std::string &Str, std::string &Obj)                   //[t46]
00229         { Obj = Str; }
00230 
00231 
00232 namespace internal
00233 {
00235 inline int digit_to_number(char c) PQXX_NOEXCEPT { return c-'0'; }
00236 inline char number_to_digit(int i) PQXX_NOEXCEPT
00237         { return static_cast<char>(i+'0'); }
00238 } // namespace pqxx::internal
00239 
00240 
00242 
00246 template<typename T> inline std::string to_string(const T &Obj)
00247         { return string_traits<T>::to_string(Obj); }
00248 
00250 
00251 } // namespace pqxx
00252 
00253 #endif
00254 

Generated on 17 Mar 2017 for libpqxx by  doxygen 1.6.1