00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef INCL_EVOLUTION_SMART_POINTER
00020 # define INCL_EVOLUTION_SMART_POINTER
00021
00022 #include "eds_abi_wrapper.h"
00023
00024 #include <stdlib.h>
00025 #include <stdexcept>
00026 #include <string>
00027 #include <memory>
00028 using namespace std;
00029
00030 #include <boost/scoped_array.hpp>
00031 #include <boost/shared_ptr.hpp>
00032
00033 template<class T> class EvolutionUnrefFree {
00034 public:
00035 static void unref(T *pointer) { free(pointer); }
00036 };
00037
00038 class EvolutionUnref {
00039 public:
00040
00041
00042
00043
00044 static void unref(char *pointer) { free(pointer); }
00045
00046 #ifdef HAVE_EDS
00047 static void unref(GObject *pointer) { g_object_unref(pointer); }
00048
00049 static void unref(GList *pointer) {
00050 if (pointer) {
00051 GList *next = pointer;
00052 do {
00053 g_object_unref(G_OBJECT(next->data));
00054 next = next->next;
00055 } while (next);
00056 g_list_free(pointer);
00057 }
00058 }
00059 #ifdef ENABLE_EBOOK
00060 static void unref(EBookQuery *pointer) { e_book_query_unref(pointer); }
00061 #endif
00062 #ifdef ENABLE_ECAL
00063 static void unref(icalcomponent *pointer) { icalcomponent_free(pointer); }
00064 static void unref(icaltimezone *pointer) { icaltimezone_free(pointer, 1); }
00065 #endif
00066 #endif
00067 };
00068
00069
00070
00071
00072
00073
00074
00075 template<class T, class base = T, class R = EvolutionUnref > class eptr {
00076 protected:
00077 T *m_pointer;
00078
00079 public:
00080
00081
00082
00083
00084 eptr(T *pointer = NULL, const char *objectName = NULL) :
00085 m_pointer( pointer )
00086 {
00087 if (!pointer && objectName ) {
00088 throw std::runtime_error(std::string("Error allocating ") + objectName);
00089 }
00090 };
00091 ~eptr()
00092 {
00093 set( NULL );
00094 }
00095
00096
00097 eptr(eptr &other) {
00098 m_pointer = other.m_pointer;
00099 other.m_pointer = NULL;
00100 }
00101 eptr & operator = (eptr &other) {
00102 m_pointer = other.m_pointer;
00103 other.m_pointer = NULL;
00104 return *this;
00105 }
00106
00107
00108
00109
00110
00111
00112 void set( T *pointer, const char *objectName = NULL )
00113 {
00114 if (m_pointer) {
00115 R::unref((base *)m_pointer);
00116 }
00117 if (!pointer && objectName) {
00118 throw std::runtime_error(std::string("Error allocating ") + objectName);
00119 }
00120 m_pointer = pointer;
00121 }
00122
00123
00124
00125
00126
00127
00128 T *release() { T *res = m_pointer; m_pointer = NULL; return res; }
00129
00130 eptr<T, base, R> &operator = ( T *pointer ) { set( pointer ); return *this; }
00131 T *get() { return m_pointer; }
00132 T *operator-> () { return m_pointer; }
00133 T &operator* () { return *m_pointer; }
00134 operator T * () { return m_pointer; }
00135 operator void * () { return (void *)m_pointer; }
00136 operator bool () { return m_pointer != NULL; }
00137 };
00138
00139 template <class T> class CxxUnref {
00140 public:
00141 static void unref(T *pointer) { delete pointer; }
00142 };
00143
00144
00145 template <class T> class cxxptr : public eptr<T, T, CxxUnref<T> > {
00146 public:
00147 cxxptr(T *pointer = NULL, const char *objectName = NULL) :
00148 eptr<T, T, CxxUnref<T> > (pointer, objectName)
00149 {
00150 };
00151 };
00152
00153 template <class T> class ArrayUnref {
00154 public:
00155 static void unref(T *pointer) { delete [] pointer; }
00156 };
00157
00158
00159
00160 template <class T> class arrayptr : public eptr<T, T, ArrayUnref<T> > {
00161 public:
00162 arrayptr(T *pointer = NULL, const char *objectName = NULL) :
00163 eptr<T, T, ArrayUnref<T> > (pointer, objectName)
00164 {
00165 };
00166 };
00167
00168 #endif