deleted SharedPtr and renamed MmgedSharedPtr to SharedPtr

This commit is contained in:
hyung-hwan 2015-03-11 01:16:31 +00:00
parent 9a3ca2e3c1
commit 23b718f366
7 changed files with 79 additions and 246 deletions

View File

@ -9,7 +9,7 @@ pkginclude_HEADERS = \
if ENABLE_CXX
pkginclude_HEADERS += \
Types.hpp Hashable.hpp Uncopyable.hpp RefCounted.hpp \
ScopedPtr.hpp SharedPtr.hpp Exception.hpp
ScopedPtr.hpp Exception.hpp
endif
install-data-hook:

View File

@ -52,7 +52,7 @@ build_triplet = @build@
host_triplet = @host@
@ENABLE_CXX_TRUE@am__append_1 = \
@ENABLE_CXX_TRUE@ Types.hpp Hashable.hpp Uncopyable.hpp RefCounted.hpp \
@ENABLE_CXX_TRUE@ ScopedPtr.hpp SharedPtr.hpp Exception.hpp
@ENABLE_CXX_TRUE@ ScopedPtr.hpp Exception.hpp
subdir = include/qse
DIST_COMMON = $(am__pkginclude_HEADERS_DIST) $(srcdir)/Makefile.am \
@ -95,7 +95,7 @@ am__can_run_installinfo = \
am__pkginclude_HEADERS_DIST = conf-msw.h conf-os2.h conf-dos.h \
conf-vms.h conf-mac.h conf-inf.h types.h macros.h pack1.h \
unpack.h Types.hpp Hashable.hpp Uncopyable.hpp RefCounted.hpp \
ScopedPtr.hpp SharedPtr.hpp Exception.hpp
ScopedPtr.hpp Exception.hpp
am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
am__vpath_adj = case $$p in \
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \

View File

@ -51,7 +51,7 @@ pkginclude_HEADERS = \
if ENABLE_CXX
pkginclude_HEADERS += \
Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp MmgedSharedPtr.hpp \
Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp SharedPtr.hpp \
Mpool.hpp Association.hpp LinkedList.hpp HashList.hpp HashTable.hpp \
RedBlackTree.hpp RedBlackTable.hpp \
Array.hpp BinaryHeap.hpp

View File

@ -51,7 +51,7 @@ POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
@ENABLE_CXX_TRUE@am__append_1 = \
@ENABLE_CXX_TRUE@ Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp MmgedSharedPtr.hpp \
@ENABLE_CXX_TRUE@ Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp SharedPtr.hpp \
@ENABLE_CXX_TRUE@ Mpool.hpp Association.hpp LinkedList.hpp HashList.hpp HashTable.hpp \
@ENABLE_CXX_TRUE@ RedBlackTree.hpp RedBlackTable.hpp \
@ENABLE_CXX_TRUE@ Array.hpp BinaryHeap.hpp
@ -93,7 +93,7 @@ am__pkginclude_HEADERS_DIST = alg.h chr.h cp949.h cp950.h dir.h dll.h \
nwio.h oht.h opt.h path.h pio.h pma.h rbt.h rex.h sck.h sio.h \
sll.h slmb.h str.h task.h time.h tio.h tmr.h tre.h uni.h uri.h \
utf8.h xma.h Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp \
MmgedSharedPtr.hpp Mpool.hpp Association.hpp LinkedList.hpp \
SharedPtr.hpp Mpool.hpp Association.hpp LinkedList.hpp \
HashList.hpp HashTable.hpp RedBlackTree.hpp RedBlackTable.hpp \
Array.hpp BinaryHeap.hpp
am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;

View File

@ -1,173 +0,0 @@
/*
* $Id$
*
Copyright (c) 2006-2014 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _QSE_CMN_MMGEDSHAREDPTR_HPP_
#define _QSE_CMN_MMGEDSHAREDPTR_HPP_
#include <qse/cmn/Mmged.hpp>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
template <typename T>
struct MmgedSharedPtrDeleter
{
void operator() (T* ptr, void* arg)
{
delete ptr;
}
};
template <typename T>
struct MmgedSharedPtrArrayDeleter
{
void operator() (T* ptr, void* arg)
{
delete[] ptr;
}
};
///
/// The MmgedSharedPtr class is similar to SharedPtr except that i
/// accepts a pointer to a memory manager to allocate the space for
/// shared reference count.
///
template<typename T, typename DELETER = MmgedSharedPtrDeleter<T> >
class QSE_EXPORT MmgedSharedPtr: public Mmged
{
public:
typedef MmgedSharedPtr<T,DELETER> SelfType;
typedef MmgedSharedPtrDeleter<T> DefaultDeleter;
MmgedSharedPtr (T* ptr = (T*)QSE_NULL, void* darg = (void*)QSE_NULL): Mmged(QSE_NULL), _ptr(ptr), _darg(darg)
{
this->_ref = new(this->getMmgr()) qse_size_t;
(*this->_ref) = 1;
}
MmgedSharedPtr (Mmgr* mmgr, T* ptr = (T*)QSE_NULL, void* darg = (void*)QSE_NULL): Mmged(mmgr), _ptr(ptr), _darg(darg)
{
this->_ref = new(this->getMmgr()) qse_size_t;
(*this->_ref) = 1;
}
MmgedSharedPtr (const SelfType& ptr): Mmged(ptr), _ref(ptr._ref), _ptr (ptr._ptr), _darg (ptr._darg)
{
(*this->_ref)++;
}
~MmgedSharedPtr ()
{
(*this->_ref)--;
if (*this->_ref <= 0)
{
if (this->_ptr) this->deleter (this->_ptr, this->_darg);
// no destructor as *this->_ref is a plain type.
::operator delete (this->_ref, this->getMmgr());
}
}
SelfType& operator= (const SelfType& ptr)
{
if (this != &ptr)
{
(*this->_ref)--;
if (*this->_ref <= 0)
{
if (this->_ptr) this->deleter (this->_ptr, this->_darg);
// no destructor as *this->_ref is a plain type.
::operator delete (this->_ref, this->getMmgr());
}
this->mmgr = ptr.getMmgr(); // memory manager must be copied
this->_ptr = ptr._ptr;
this->_darg = ptr._darg;
this->_ref = ptr._ref;
(*this->_ref)++;
}
return *this;
}
T& operator* ()
{
QSE_ASSERT (this->_ptr != (T*)QSE_NULL);
return *this->_ptr;
}
const T& operator* () const
{
QSE_ASSERT (this->_ptr != (T*)QSE_NULL);
return *this->_ptr;
}
T* operator-> ()
{
QSE_ASSERT (this->_ptr != (T*)QSE_NULL);
return this->_ptr;
}
const T* operator-> () const
{
QSE_ASSERT (this->_ptr != (T*)QSE_NULL);
return this->_ptr;
}
bool operator! () const
{
return this->_ptr == (T*)QSE_NULL;
}
T& operator[] (qse_size_t idx)
{
QSE_ASSERT (this->_ptr != (T*)QSE_NULL);
return this->_ptr[idx];
}
T* get ()
{
return this->_ptr;
}
const T* get () const
{
return this->_ptr;
}
protected:
qse_size_t* _ref;
T* _ptr;
void* _darg;
DELETER deleter;
};
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////
#endif

View File

@ -209,27 +209,6 @@ struct ScopedPtrMmgrDeleter
}
};
// Customized deleter for SharedPtr
template <typename T>
struct SharedPtrMmgrDeleter
{
void operator() (T* ptr, void* arg)
{
ptr->~T ();
::operator delete (ptr, (QSE::Mmgr*)arg);
}
};
template <typename T>
struct MmgedSharedPtrMmgrDeleter
{
void operator() (T* ptr, void* arg)
{
ptr->~T ();
::operator delete (ptr, (QSE::Mmgr*)arg);
}
};
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////

View File

@ -24,11 +24,10 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _QSE_SHAREDPTR_HPP_
#define _QSE_SHAREDPTR_HPP_
#ifndef _QSE_CMN_SHAREDPTR_HPP_
#define _QSE_CMN_SHAREDPTR_HPP_
#include <qse/types.h>
#include <qse/macros.h>
#include <qse/cmn/Mmged.hpp>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
@ -52,54 +51,78 @@ struct SharedPtrArrayDeleter
}
};
template <typename T>
struct SharedPtrMmgrDeleter
{
void operator() (T* ptr, void* arg)
{
ptr->~T ();
::operator delete (ptr, (QSE::Mmgr*)arg);
}
};
///
/// use QSE::MmgedSharedPtr if you want the instance of this class itself
/// to be memory managed.
/// The SharedPtr class provides a smart pointer that can be shared
/// using reference counting.
///
template<typename T, typename DELETER = SharedPtrDeleter<T> >
class QSE_EXPORT SharedPtr
class QSE_EXPORT SharedPtr: public Mmged
{
public:
typedef SharedPtr<T,DELETER> SelfType;
typedef SharedPtrDeleter<T> DefaultDeleter;
SharedPtr (T* ptr = (T*)QSE_NULL, void* darg = (void*)QSE_NULL): _ptr (ptr), _darg (darg)
SharedPtr (T* ptr = (T*)QSE_NULL, void* darg = (void*)QSE_NULL): Mmged(QSE_NULL)
{
this->_ref = new qse_size_t;
(*this->_ref) = 1;
this->item = new (this->getMmgr()) item_t;
this->item->ref = 1;
this->item->ptr = ptr;
this->item->darg = darg;
}
SharedPtr (const SelfType& ptr): _ref(ptr._ref), _ptr (ptr._ptr), _darg (ptr._darg)
SharedPtr (Mmgr* mmgr, T* ptr = (T*)QSE_NULL, void* darg = (void*)QSE_NULL): Mmged(mmgr)
{
(*this->_ref)++;
this->item = new (this->getMmgr()) item_t;
this->item->ref = 1;
this->item->ptr = ptr;
this->item->darg = darg;
}
SharedPtr (const SelfType& sp): Mmged(sp), item (sp.item)
{
this->item->ref++;
}
~SharedPtr ()
{
(*this->_ref)--;
if (*this->_ref <= 0)
this->item->ref--;
if (this->item->ref <= 0)
{
if (this->_ptr) this->deleter (this->_ptr, this->_darg);
delete this->_ref;
if (this->item->ptr) this->item->deleter (this->item->ptr, this->item->darg);
// no destructor as *this->_ref is a plain type.
::operator delete (this->item, this->getMmgr());
}
}
SelfType& operator= (const SelfType& ptr)
SelfType& operator= (const SelfType& sp)
{
if (this != &ptr)
if (this != &sp)
{
(*this->_ref)--;
if (*this->_ref <= 0)
this->item->ref--;
if (this->item->ref <= 0)
{
if (this->_ptr) this->deleter (this->_ptr, this->_darg);
delete this->_ref;
if (this->item->ptr) this->item->deleter (this->item->ptr, this->item->darg);
// no destructor as *this->_ref is a plain type.
::operator delete (this->item, this->getMmgr());
}
this->_ptr = ptr._ptr;
this->_darg = ptr._darg;
this->_ref = ptr._ref;
(*this->_ref)++;
// must copy the memory manager pointer as the item
// to be copied is allocated using the memory manager of sp.
this->mmgr = sp.getMmgr();
this->item = sp.item;
this->item->ref++;
}
return *this;
@ -107,55 +130,59 @@ public:
T& operator* ()
{
QSE_ASSERT (this->_ptr != (T*)QSE_NULL);
return *this->_ptr;
QSE_ASSERT (this->item->ptr != (T*)QSE_NULL);
return *this->item->ptr;
}
const T& operator* () const
{
QSE_ASSERT (this->_ptr != (T*)QSE_NULL);
return *this->_ptr;
QSE_ASSERT (this->item->ptr != (T*)QSE_NULL);
return *this->item->ptr;
}
T* operator-> ()
{
QSE_ASSERT (this->_ptr != (T*)QSE_NULL);
return this->_ptr;
QSE_ASSERT (this->item->ptr != (T*)QSE_NULL);
return this->item->ptr;
}
const T* operator-> () const
{
QSE_ASSERT (this->_ptr != (T*)QSE_NULL);
return this->_ptr;
QSE_ASSERT (this->item->ptr != (T*)QSE_NULL);
return this->item->ptr;
}
bool operator! () const
{
return this->_ptr == (T*)QSE_NULL;
return this->item->ptr == (T*)QSE_NULL;
}
T& operator[] (qse_size_t idx)
{
QSE_ASSERT (this->_ptr != (T*)QSE_NULL);
return this->_ptr[idx];
QSE_ASSERT (this->item->ptr != (T*)QSE_NULL);
return this->item->ptr[idx];
}
T* get ()
T* getPtr ()
{
return this->_ptr;
return this->item->ptr;
}
const T* get () const
const T* getPtr () const
{
return this->_ptr;
return this->item->ptr;
}
protected:
qse_size_t* _ref;
T* _ptr;
void* _darg;
DELETER deleter;
struct item_t
{
qse_size_t ref;
T* ptr;
void* darg;
DELETER deleter;
};
item_t* item;
};
/////////////////////////////////