2005-11-05 23:04:33 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Patrick Ohly
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCL_EVOLUTION_SMART_POINTER
|
|
|
|
# define INCL_EVOLUTION_SMART_POINTER
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <glib-object.h>
|
2006-07-23 12:27:21 +02:00
|
|
|
#ifdef ENABLE_EBOOK
|
2005-11-05 23:04:33 +01:00
|
|
|
#include <libebook/e-book.h>
|
2006-07-23 12:27:21 +02:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_ECAL
|
2006-06-10 14:26:44 +02:00
|
|
|
#include <libecal/e-cal.h>
|
2006-07-23 12:27:21 +02:00
|
|
|
#endif
|
2005-11-05 23:04:33 +01:00
|
|
|
|
2006-05-26 14:49:19 +02:00
|
|
|
#include <stdexcept>
|
2006-08-14 22:52:34 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
void inline unref( char *pointer ) { free( pointer ); }
|
|
|
|
void inline unref( GObject *pointer ) { g_object_unref( pointer ); }
|
|
|
|
#ifdef ENABLE_EBOOK
|
|
|
|
void inline unref( EBookQuery *pointer ) { e_book_query_unref( pointer ); }
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_ECAL
|
|
|
|
void inline unref( icalcomponent *pointer ) { icalcomponent_free( pointer ); }
|
|
|
|
void inline unref( icaltimezone *pointer ) { icaltimezone_free( pointer, 1 ); }
|
|
|
|
#endif
|
|
|
|
#if 0
|
|
|
|
void inline unref( EBook *pointer ) { g_object_unref( pointer ); }
|
|
|
|
void inline unref( EContact *pointer ) { g_object_unref( pointer ); }
|
|
|
|
#endif
|
|
|
|
template <class T> void unref(T *pointer) { delete pointer; }
|
2006-05-26 14:49:19 +02:00
|
|
|
|
2005-11-05 23:04:33 +01:00
|
|
|
/**
|
|
|
|
* a smart pointer implementation for objects for which
|
|
|
|
* a unref() function exists;
|
|
|
|
* trying to store a NULL pointer raises an exception,
|
|
|
|
* unreferencing valid objects is done automatically
|
|
|
|
*/
|
2006-08-14 22:52:34 +02:00
|
|
|
template<class T, class base = T> class eptr {
|
2005-11-05 23:04:33 +01:00
|
|
|
/** do not allow copy construction */
|
2006-08-14 22:52:34 +02:00
|
|
|
eptr( const eptr &other) {};
|
2005-11-05 23:04:33 +01:00
|
|
|
|
|
|
|
/** do not allow copying */
|
2006-08-14 22:52:34 +02:00
|
|
|
void operator = ( const eptr &other ) {}
|
2005-11-05 23:04:33 +01:00
|
|
|
|
2006-06-10 14:26:44 +02:00
|
|
|
protected:
|
|
|
|
T *m_pointer;
|
|
|
|
|
2005-11-05 23:04:33 +01:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* create a smart pointer that owns the given object;
|
|
|
|
* passing a NULL pointer and a name for the object raises an error
|
|
|
|
*/
|
2006-08-14 22:52:34 +02:00
|
|
|
eptr(T *pointer = NULL, const char *objectName = NULL) :
|
2005-11-05 23:04:33 +01:00
|
|
|
m_pointer( pointer )
|
|
|
|
{
|
|
|
|
if (!pointer && objectName ) {
|
2006-08-14 22:52:34 +02:00
|
|
|
throw std::runtime_error(std::string("Error allocating ") + objectName);
|
2005-11-05 23:04:33 +01:00
|
|
|
}
|
|
|
|
};
|
2006-08-14 22:52:34 +02:00
|
|
|
~eptr()
|
2005-11-05 23:04:33 +01:00
|
|
|
{
|
|
|
|
set( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* store another object in this pointer, replacing any which was
|
|
|
|
* referenced there before;
|
|
|
|
* passing a NULL pointer and a name for the object raises an error
|
|
|
|
*/
|
|
|
|
void set( T *pointer, const char *objectName = NULL )
|
|
|
|
{
|
|
|
|
if (m_pointer) {
|
|
|
|
unref( (base *)m_pointer );
|
|
|
|
}
|
|
|
|
if (!pointer && objectName) {
|
2006-08-14 22:52:34 +02:00
|
|
|
throw std::runtime_error(std::string("Error allocating ") + objectName);
|
2005-11-05 23:04:33 +01:00
|
|
|
}
|
|
|
|
m_pointer = pointer;
|
|
|
|
}
|
|
|
|
|
2006-08-14 22:52:34 +02:00
|
|
|
eptr<T, base> &operator = ( T *pointer ) { set( pointer ); return *this; }
|
2005-11-05 23:04:33 +01:00
|
|
|
T *operator-> () { return m_pointer; }
|
|
|
|
T &operator* () { return *m_pointer; }
|
|
|
|
operator T * () { return m_pointer; }
|
2006-04-09 13:48:11 +02:00
|
|
|
operator void * () { return (void *)m_pointer; }
|
|
|
|
operator bool () { return m_pointer != NULL; }
|
2005-11-05 23:04:33 +01:00
|
|
|
};
|
|
|
|
|
2006-06-10 14:26:44 +02:00
|
|
|
/**
|
2006-08-14 22:52:34 +02:00
|
|
|
* a eptr for C++ arrays: everything is unref'ed via delete []
|
2006-06-10 14:26:44 +02:00
|
|
|
*/
|
2006-08-14 22:52:34 +02:00
|
|
|
template <class T> class arrayptr : public eptr<T> {
|
2006-06-10 14:26:44 +02:00
|
|
|
void unref( T *pointer) { delete [] pointer; }
|
|
|
|
|
|
|
|
public:
|
|
|
|
arrayptr(T *pointer = NULL, const char *objectName = NULL) :
|
2006-08-14 22:52:34 +02:00
|
|
|
eptr<T>(pointer, objectName)
|
2006-06-10 14:26:44 +02:00
|
|
|
{}
|
|
|
|
~arrayptr()
|
|
|
|
{
|
|
|
|
set(NULL);
|
|
|
|
}
|
|
|
|
arrayptr<T> &operator = ( T *pointer ) { set( pointer ); return *this; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* has to be duplicated, base class does not pick up our unref()
|
|
|
|
* above otherwise
|
|
|
|
*/
|
|
|
|
void set( T *pointer, const char *objectName = NULL )
|
|
|
|
{
|
2006-06-11 21:12:04 +02:00
|
|
|
if (this->m_pointer) {
|
|
|
|
unref(this->m_pointer);
|
2006-06-10 14:26:44 +02:00
|
|
|
}
|
|
|
|
if (!pointer && objectName) {
|
2006-08-14 22:52:34 +02:00
|
|
|
throw std::runtime_error(std::string("Error allocating ") + objectName);
|
2006-06-10 14:26:44 +02:00
|
|
|
}
|
2006-06-11 21:12:04 +02:00
|
|
|
this->m_pointer = pointer;
|
2006-06-10 14:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2005-11-05 23:04:33 +01:00
|
|
|
#endif
|