Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

util.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/util.h
00005  *
00006  *   DESCRIPTION
00007  *      Various utility definitions for libpqxx
00008  *
00009  * Copyright (c) 2001-2003, Jeroen T. Vermeulen <jtv@xs4all.nl>
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 #ifndef PQXX_UTIL_H
00014 #define PQXX_UTIL_H
00015 
00016 #include "pqxx/compiler.h"
00017 
00018 #include <cstdio>
00019 #include <stdexcept>
00020 #include <string>
00021 #include <typeinfo>
00022 
00023 extern "C"
00024 {
00025 #include "libpq-fe.h"
00026 }
00027 
00028 
00029 namespace pqxx
00030 {
00031 typedef long Result_size_type;
00032 typedef int Tuple_size_type;
00033 
00036 template<typename T> inline const char *FmtString(T);
00037 
00038 // Not implemented to prevent accidents with irregular meaning of argument:
00039 // template<> inline const char *FmtString(const char *&) { return "%s"; }
00040 
00041 template<> inline const char *FmtString(short)         { return "%hd"; }
00042 template<> inline const char *FmtString(unsigned short){ return "%hu"; }
00043 template<> inline const char *FmtString(int)           { return  "%i"; }
00044 template<> inline const char *FmtString(long)          { return "%li"; }
00045 template<> inline const char *FmtString(unsigned)      { return  "%u"; }
00046 template<> inline const char *FmtString(unsigned long) { return "%lu"; }
00047 template<> inline const char *FmtString(float)         { return  "%f"; }
00048 template<> inline const char *FmtString(double)        { return "%lf"; }
00049 template<> inline const char *FmtString(long double)   { return "%Lf"; }
00050 template<> inline const char *FmtString(char)          { return  "%c"; }
00051 template<> inline const char *FmtString(unsigned char) { return  "%c"; }
00052 
00053 
00055 template<typename T> inline PGSTD::string ToString(const T &Obj)
00056 {
00057   // TODO: Find a decent way to determine max string length at compile time!
00058   char Buf[500];
00059   sprintf(Buf, FmtString(Obj), Obj);
00060   return PGSTD::string(Buf);
00061 }
00062 
00063 template<> inline PGSTD::string ToString(const PGSTD::string &Obj) {return Obj;}
00064 template<> inline PGSTD::string ToString(const char *const &Obj) { return Obj; }
00065 template<> inline PGSTD::string ToString(char *const &Obj) { return Obj; }
00066 
00067 template<> inline PGSTD::string ToString(const bool &Obj) 
00068 { 
00069   return ToString(unsigned(Obj));
00070 }
00071 
00072 template<> inline PGSTD::string ToString(const short &Obj)
00073 {
00074   return ToString(int(Obj));
00075 }
00076 
00077 template<> inline PGSTD::string ToString(const unsigned short &Obj)
00078 {
00079   return ToString(unsigned(Obj));
00080 }
00081 
00082 
00083 template<typename T> inline void FromString(const char Str[], T &Obj)
00084 {
00085   if (!Str) throw PGSTD::runtime_error("Attempt to convert NULL string to " +
00086                                      PGSTD::string(typeid(T).name()));
00087 
00088   if (sscanf(Str, FmtString(Obj), &Obj) != 1)
00089     throw PGSTD::runtime_error("Cannot convert value '" + 
00090                              PGSTD::string(Str) + 
00091                              "' to " + typeid(T).name());
00092 }
00093 
00094 
00095 template<> inline void FromString(const char Str[], PGSTD::string &Obj)
00096 {
00097   if (!Str) 
00098     throw PGSTD::runtime_error("Attempt to convert NULL C string to C++ "
00099                                "string");
00100   Obj = Str;
00101 }
00102 
00103 
00104 template<> inline void FromString(const char Str[], const char *&Obj)
00105 {
00106   if (!Str)
00107     throw PGSTD::runtime_error("Attempt to read NULL string");
00108   Obj = Str;
00109 }
00110 
00111 template<> inline void FromString(const char Str[], bool &Obj)
00112 {
00113   if (!Str)
00114     throw PGSTD::runtime_error("Attempt to read NULL string");
00115 
00116   switch (Str[0])
00117   {
00118   case 0:
00119   case 'f':
00120     Obj = false;
00121     break;
00122   case '0':
00123     {
00124       int I;
00125       FromString(Str, I);
00126       Obj = (I != 0);
00127     }
00128     break;
00129   default:
00130     Obj = true;
00131   }
00132 }
00133 
00134 
00137 template<typename T> PGSTD::string Quote(const T &Obj, bool EmptyIsNull=false);
00138 
00139 
00141 template<> inline PGSTD::string Quote(const PGSTD::string &Obj, 
00142                                       bool EmptyIsNull)
00143 {
00144   if (EmptyIsNull && Obj.empty()) return "null";
00145 
00146   PGSTD::string Result;
00147   Result.reserve(Obj.size() + 2);
00148   Result += "'";
00149 
00150 #ifdef HAVE_PQESCAPESTRING
00151 
00152   char *const Buf = new char[2*Obj.size() + 1];
00153   try
00154   {
00155     PQescapeString(Buf, Obj.c_str(), Obj.size());
00156     Result += Buf;
00157   }
00158   catch (const PGSTD::exception &)
00159   {
00160     delete [] Buf;
00161     throw;
00162   }
00163   delete [] Buf;
00164 
00165 #else
00166 
00167   for (PGSTD::string::size_type i=0; i < Obj.size(); ++i)
00168   {
00169     if (isgraph(Obj[i]))
00170     {
00171       switch (Obj[i])
00172       {
00173       case '\'':
00174       case '\\':
00175         Result += '\\';
00176       }
00177       Result += Obj[i];
00178     }
00179     else
00180     {
00181         char s[10];
00182         sprintf(s, 
00183                 "\\%03o", 
00184                 static_cast<unsigned int>(static_cast<unsigned char>(Obj[i])));
00185         Result.append(s, 4);
00186     }
00187   }
00188 
00189 #endif
00190 
00191   return Result + '\'';
00192 }
00193 
00194 
00197 template<> inline PGSTD::string Quote(const char *const & Obj, 
00198                                       bool EmptyIsNull)
00199 {
00200   if (!Obj) return "null";
00201   return Quote(PGSTD::string(Obj), EmptyIsNull);
00202 }
00203 
00204 
00206 
00211 template<int LEN> inline PGSTD::string Quote(const char (&Obj)[LEN],
00212                                              bool EmptyIsNull=false)    //[t18]
00213 {
00214   return Quote(PGSTD::string(Obj), EmptyIsNull);
00215 }
00216 
00217 
00221 template<typename T> inline PGSTD::string Quote(const T &Obj, bool EmptyIsNull)
00222 {
00223   return Quote(ToString(Obj), EmptyIsNull);
00224 }
00225 
00226 
00227 
00229 template<typename T> PGSTD::string Classname(const T *);
00230 
00231 
00233 
00238 template<typename T> class PQAlloc
00239 {
00240   T *m_Obj;
00241 public:
00242   PQAlloc() : m_Obj(0) {}
00243 
00245   explicit PQAlloc(T *obj) : m_Obj(obj) {}
00246 
00247   ~PQAlloc() { close(); }
00248 
00250 
00252   PQAlloc &operator=(T *obj) throw ()
00253   { 
00254     if (obj != m_Obj)
00255     {
00256       close();
00257       m_Obj = obj;
00258     }
00259     return *this;
00260   }
00261 
00263   operator bool() const throw () { return m_Obj != 0; }
00264 
00266   bool operator!() const throw () { return !m_Obj; }
00267 
00269 
00271   T *operator->() const throw (PGSTD::logic_error)
00272   {
00273     if (!m_Obj) throw PGSTD::logic_error("Null pointer dereferenced");
00274     return m_Obj;
00275   }
00276 
00278 
00280   T &operator*() const throw (PGSTD::logic_error) { return *operator->(); }
00281 
00283 
00285   T *c_ptr() const throw () { return m_Obj; }
00286 
00288   void close() throw () { if (m_Obj) freemem(); m_Obj = 0; }
00289 
00290 private:
00291   void freemem() throw ()
00292   {
00293 #ifdef HAVE_PQFREEMEM
00294     PQfreemem(m_Obj);
00295 #else
00296     free(m_Obj);
00297 #endif
00298   }
00299 
00300   PQAlloc(const PQAlloc &);             // Not allowed
00301   PQAlloc &operator=(const PQAlloc &);  // Not allowed
00302 };
00303 
00304 
00305 
00311 template<typename GUEST>
00312 class Unique
00313 {
00314 public:
00315   Unique() : m_Guest(0) {}
00316 
00317   GUEST *get() const throw () { return m_Guest; }
00318 
00319   void Register(GUEST *G)
00320   {
00321     if (!G) throw PGSTD::logic_error("Internal libpqxx error: NULL " + 
00322                                      Classname(G));
00323     
00324     if (m_Guest)
00325     {
00326       if (G == m_Guest)
00327         throw PGSTD::logic_error(Classname(G) +
00328                                  " '" +
00329                                  G->Name() +
00330                                  "' started more than once without closing");
00331 
00332       throw PGSTD::logic_error("Started " + 
00333                                Classname(G) +
00334                                " '" + 
00335                                G->Name() + 
00336                                "' while '" +
00337                                m_Guest->Name() +
00338                                "' was still active");
00339     }
00340     
00341     m_Guest = G;
00342   }
00343 
00344   void Unregister(GUEST *G)
00345   {
00346     if (G != m_Guest)
00347     {
00348       if (!G) 
00349         throw PGSTD::logic_error("Closing NULL " + Classname(G));
00350       else if (!m_Guest)
00351         throw PGSTD::logic_error("Closing " + 
00352                                  Classname(G) +
00353                                  " '" +
00354                                  G->Name() +
00355                                  "' which wasn't open");
00356       else
00357         throw PGSTD::logic_error("Closing wrong " + 
00358                                  Classname(G) +
00359                                  "; expected '" +
00360                                  m_Guest->Name() +
00361                                  "' but got '" +
00362                                  G->Name() +
00363                                  "'");
00364     }
00365 
00366     m_Guest = 0;
00367   }
00368 
00369 private:
00370   GUEST *m_Guest;
00371 
00372   // Not allowed:
00373   Unique(const Unique &);
00374   Unique &operator=(const Unique &);
00375 };
00376 
00377 }
00378 
00379 #endif
00380 

Generated on Sun May 4 06:03:42 2003 for libpqxx by doxygen1.3-rc3