changed the behavior of member functions of RefCounted.
Changed SharedPtr to make use of RefCounted. Added StrBase and String.
This commit is contained in:
@ -33,6 +33,9 @@
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
/// The RefCounted class provides common functions required to
|
||||
/// implement a reference counted class.
|
||||
|
||||
class QSE_EXPORT RefCounted: public Uncopyable
|
||||
{
|
||||
protected:
|
||||
@ -41,29 +44,29 @@ protected:
|
||||
}
|
||||
|
||||
public:
|
||||
virtual ~RefCounted ()
|
||||
{
|
||||
QSE_ASSERT (this->_ref_count == 0);
|
||||
}
|
||||
|
||||
void ref () const
|
||||
/// The ref() function increments the reference count and returns
|
||||
/// the incremented count.
|
||||
qse_size_t ref () const
|
||||
{
|
||||
this->_ref_count++;
|
||||
return ++this->_ref_count;
|
||||
}
|
||||
|
||||
void deref (bool kill = true) const
|
||||
/// The deref() function decrements the reference count and returns
|
||||
/// the decremented count. The caller should kill the callee if it
|
||||
/// returns 0.
|
||||
qse_size_t deref () const
|
||||
{
|
||||
if (--this->_ref_count == 0 && kill)
|
||||
{
|
||||
delete this;
|
||||
}
|
||||
return --this->_ref_count;
|
||||
}
|
||||
|
||||
/// The getRefCount() function returns the current reference count.
|
||||
qse_size_t getRefCount () const
|
||||
{
|
||||
return this->_ref_count;
|
||||
}
|
||||
|
||||
/// The isShared() function returns true if the object is referenced
|
||||
/// more than twice and false otherwise.
|
||||
bool isShared () const
|
||||
{
|
||||
return this->_ref_count > 1;
|
||||
|
@ -53,6 +53,7 @@ if ENABLE_CXX
|
||||
pkginclude_HEADERS += \
|
||||
Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp \
|
||||
ScopedPtr.hpp SharedPtr.hpp \
|
||||
StrBase.hpp String.hpp \
|
||||
Mpool.hpp Association.hpp LinkedList.hpp HashList.hpp HashTable.hpp \
|
||||
RedBlackTree.hpp RedBlackTable.hpp \
|
||||
Array.hpp BinaryHeap.hpp
|
||||
|
@ -53,6 +53,7 @@ host_triplet = @host@
|
||||
@ENABLE_CXX_TRUE@am__append_1 = \
|
||||
@ENABLE_CXX_TRUE@ Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp \
|
||||
@ENABLE_CXX_TRUE@ ScopedPtr.hpp SharedPtr.hpp \
|
||||
@ENABLE_CXX_TRUE@ StrBase.hpp String.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
|
||||
@ -94,9 +95,9 @@ 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 \
|
||||
ScopedPtr.hpp SharedPtr.hpp Mpool.hpp Association.hpp \
|
||||
LinkedList.hpp HashList.hpp HashTable.hpp RedBlackTree.hpp \
|
||||
RedBlackTable.hpp Array.hpp BinaryHeap.hpp
|
||||
ScopedPtr.hpp SharedPtr.hpp StrBase.hpp String.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'`;
|
||||
am__vpath_adj = case $$p in \
|
||||
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \
|
||||
|
@ -77,28 +77,29 @@ public:
|
||||
SharedPtr (T* ptr = (T*)QSE_NULL, void* darg = (void*)QSE_NULL): Mmged(QSE_NULL)
|
||||
{
|
||||
this->_item = new (this->getMmgr()) item_t;
|
||||
this->_item->ref = 1;
|
||||
this->_item->ptr = ptr;
|
||||
this->_item->darg = darg;
|
||||
|
||||
this->_item->ref ();
|
||||
}
|
||||
|
||||
SharedPtr (Mmgr* mmgr, T* ptr = (T*)QSE_NULL, void* darg = (void*)QSE_NULL): Mmged(mmgr)
|
||||
{
|
||||
this->_item = new (this->getMmgr()) item_t;
|
||||
this->_item->ref = 1;
|
||||
this->_item->ptr = ptr;
|
||||
this->_item->darg = darg;
|
||||
|
||||
this->_item->ref ();
|
||||
}
|
||||
|
||||
SharedPtr (const SelfType& sp): Mmged(sp), _item(sp._item)
|
||||
{
|
||||
this->_item->ref++;
|
||||
this->_item->ref ();
|
||||
}
|
||||
|
||||
~SharedPtr ()
|
||||
{
|
||||
this->_item->ref--;
|
||||
if (this->_item->ref <= 0)
|
||||
if (this->_item->deref() <= 0)
|
||||
{
|
||||
if (this->_item->ptr) this->_item->deleter (this->_item->ptr, this->_item->darg);
|
||||
// no destructor as *this->_ref is a plain type.
|
||||
@ -110,8 +111,7 @@ public:
|
||||
{
|
||||
if (this != &sp)
|
||||
{
|
||||
this->_item->ref--;
|
||||
if (this->_item->ref <= 0)
|
||||
if (this->_item->deref() <= 0)
|
||||
{
|
||||
if (this->_item->ptr) this->_item->deleter (this->_item->ptr, this->_item->darg);
|
||||
// no destructor as *this->_ref is a plain type.
|
||||
@ -123,7 +123,7 @@ public:
|
||||
this->setMmgr (sp.getMmgr());
|
||||
|
||||
this->_item = sp._item;
|
||||
this->_item->ref++;
|
||||
this->_item->ref ();
|
||||
}
|
||||
|
||||
return *this;
|
||||
@ -175,9 +175,8 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
struct item_t
|
||||
struct item_t: public RefCounted
|
||||
{
|
||||
qse_size_t ref;
|
||||
T* ptr;
|
||||
void* darg;
|
||||
DELETER deleter;
|
||||
|
1012
qse/include/qse/cmn/StrBase.hpp
Normal file
1012
qse/include/qse/cmn/StrBase.hpp
Normal file
File diff suppressed because it is too large
Load Diff
80
qse/include/qse/cmn/String.hpp
Normal file
80
qse/include/qse/cmn/String.hpp
Normal file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* $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_STRING_HPP_
|
||||
#define _QSE_CMN_STRING_HPP_
|
||||
|
||||
#include <qse/cmn/StrBase.hpp>
|
||||
#include <qse/cmn/str.h>
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
struct WcStringOpset
|
||||
{
|
||||
qse_size_t copy (qse_wchar_t* dst, const qse_wchar_t* src, qse_size_t ssz)
|
||||
{
|
||||
return qse_wcsncpy(dst, src, ssz);
|
||||
}
|
||||
|
||||
// compare two strings of the same length
|
||||
int compare (const qse_wchar_t* str1, const qse_wchar_t* str2, qse_size_t len)
|
||||
{
|
||||
return qse_wcsxncmp(str1, len, str2, len);
|
||||
}
|
||||
|
||||
// compare a length-bound string with a null-terminated string.
|
||||
int compare (const qse_wchar_t* str1, qse_size_t len, const qse_wchar_t* str2)
|
||||
{
|
||||
return qse_wcsxcmp(str1, len, str2);
|
||||
}
|
||||
|
||||
int length (const qse_wchar_t* str)
|
||||
{
|
||||
return qse_strlen(str);
|
||||
}
|
||||
};
|
||||
|
||||
//struct MbStringOpset
|
||||
//{
|
||||
//};
|
||||
|
||||
typedef StrBase<qse_wchar_t, QSE_WT('\0'), WcStringOpset > WcString;
|
||||
//typedef StrBase<qse_mchar_t, QSE_MT('\0'), MbStringOpset > MbString;
|
||||
|
||||
#if defined(QSE_CHAR_IS_MCHAR)
|
||||
//typedef MbString String;
|
||||
#else
|
||||
typedef WcString String;
|
||||
#endif
|
||||
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user