Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

record.h

Go to the documentation of this file.
00001 
00002 // Name:        wx/dbi/record.h
00003 // Purpose:     
00004 // Author:      Joe Yates
00005 // Created:     2004-11-17
00006 // Copyright:   (c) Joe Yates
00007 // License:     BSD license (see the file 'LICENSE.txt')
00009 
00010 #ifndef __wx_dbi_record_h__
00011 #define __wx_dbi_record_h__
00012 
00013 // Set 'WX_DBI_USE_BLOB' to 1 to include ref-counted handling of BLOBs
00014 #define WX_DBI_USE_BLOB 0
00015 
00016 #if _MSC_VER > 1000
00017 #pragma once
00018 #endif // _MSC_VER > 1000
00019 
00020 #include "wx/wx.h"
00021 
00022 #ifdef _MSC_VER
00023 #pragma warning (disable : 4786) // identifier was truncated to '255' characters in the browser information
00024 #endif // def _MSC_VER
00025 
00026 #include <map>
00027 #include <vector>
00028 
00029 #include <wx/variant.h>
00030 
00031 #include <wx/exception.h>
00032 
00033 namespace wx
00034 {
00035 
00036 namespace DBI
00037 {
00038 
00039 #if WX_DBI_USE_BLOB
00040 class BLOB;
00041 #endif // WX_DBI_USE_BLOB
00042 
00047 template<typename Type>
00048 class Record
00049   {
00050   public:
00051     virtual ~Record()
00052       {}
00053 
00054   // Methods
00055   public:
00056 #ifdef _MSC_VER
00057     // Inlined due to MS bug: Q128789
00058     long GetLong(Type typKey) const
00059       {
00060       wxVariant v;
00061       GetValue(typKey, v, TYPENAME_LONG);
00062       long n = v.GetLong();
00063       return n;
00064       }
00065 
00066     wxString GetString(Type typKey) const
00067       {
00068       wxVariant v;
00069       GetValue(typKey, v, TYPENAME_STRING);
00070       wxString s = v.GetString();
00071       return s;
00072       }
00073 
00074     double GetDouble(Type typKey) const
00075       {
00076       wxVariant v;
00077       GetValue(typKey, v, TYPENAME_DOUBLE);
00078       double d = v.GetDouble();
00079       return d;
00080       }
00081 #else
00082     long          GetLong(Type typKey) const;
00083     wxString      GetString(Type typKey) const;
00084 # if WX_DBI_USE_BLOB
00085     BLOB *        GetBLOB(Type typKey); // Always requires a Release()
00086 # endif // WX_DBI_USE_BLOB
00087     double        GetDouble(Type typKey) const;
00088 #endif // def _MSC_VER
00089 
00100     virtual void  GetValue
00101       (
00102       Type typKey,
00103       wxVariant& v,
00104       const wxString& sExpectedType
00105       ) const                                           = 0;
00106     virtual void  Dump()                                = 0;
00107 
00108   protected:
00109     void          DumpField(wxVariant& v);
00110   };
00111 
00115 class ArrayRecord :
00116   public Record<int>,
00117   public std::vector<wxVariant>
00118   {
00119   // Constructors/destructors
00120   public:
00121     ~ArrayRecord();
00122 
00123   // Operators
00124   public:
00125 
00130     wxArrayString GetArrayString() const;
00131     //operator wxArrayString() const;
00132 
00133   // Methods
00134 
00135   // 'Record<>' implementation
00136   public:
00139     void GetValue
00140       (
00141       int i,
00142       wxVariant& v,
00143       const wxString& sExpectedType = wxT("")
00144       ) const;
00145     void        Dump();
00146   };
00147 
00148 class HashRecord :
00149   public Record<const wxString&>,
00150   public std::map<wxString, wxVariant>
00151   {
00152   // friends, classes, enums and types
00153 
00154   // Constructors/destructors
00155   public:
00156     ~HashRecord();
00157 
00158   // Methods
00159   
00160   public:
00167     void GetValue
00168       (
00169       const wxString& sKey,
00170       wxVariant& v,
00171       const wxString& sExpectedType = wxT("")
00172       ) const;
00173     void        Dump();
00174   };
00175 
00176 template<class Type>
00177 class RecordArray : public std::vector<Type>
00178   {
00179   // Methods
00180   public:
00181     // N.B. this fn is inlined as the MS compiler gives
00182     // an unresolved external error if it the implementation
00183     // is in a separate source file
00184     void Dump()
00185       {
00186       for(int i = 0; i < this->size(); i++)
00187         {
00188         wxLogDebug(wxT("Record %u"), i);
00189         this->at(i).Dump();
00190         }
00191       }
00192   };
00193 
00194 class ArrayRecordArray : public RecordArray<ArrayRecord>
00195   {
00196   };
00197 
00198 class HashRecordArray : public RecordArray<HashRecord>
00199   {
00200   };
00201 
00202 #if WX_DBI_USE_BLOB
00203 /*
00204  *  Holds ref-counted data object and data length.
00205  *  Any object which returns a pointer should AddRef.
00206  *  The pointer should then be Release()-ed.
00207  */
00208 class BLOB : public wxObject
00209   {
00210   public:
00211     BLOB(const void * pvc, int nLengthNew);
00212     ~BLOB();
00213 
00214   public:
00215     const void *  GetPtr() const;
00216     int           GetLength() const;
00217     int           AddRef();
00218     int           Release();
00219 
00220   private:
00221     wxObject      poData; // This object holds the ref-counted data pointer
00222     int           nLength;
00223     // Holds void * to data
00224     // The data pointer is owned by the object, which frees it on destruction
00225     void *        pv; // malloc-ed by ctor, free-d when nRefCount goes to 0 in Release()
00226     int           nRefCount;
00227   };
00228 #endif // WX_DBI_USE_BLOB
00229 
00230 } //namespace DBI
00231 
00232 } //namespace wx
00233 
00234 #endif // ndef __wx_dbi_record_h__

Generated on Wed Jan 25 08:13:09 2006 for Sherpa wxWidgets Classes by doxygen 1.3.6