changed the behavior of member functions of RefCounted.

Changed SharedPtr to make use of RefCounted.
Added StrBase and String.
This commit is contained in:
hyung-hwan 2015-03-14 00:54:56 +00:00
parent 36e75b3bb2
commit b4fd70b1e4
9 changed files with 1179 additions and 32 deletions

View File

@ -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;

View File

@ -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

View File

@ -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/||"`;; \

View File

@ -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;

File diff suppressed because it is too large Load Diff

View 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

View File

@ -98,7 +98,7 @@ if ENABLE_CXX
#rex02_CXXFLAGS = -I/usr/lib/wx/include/gtk2-unicode-release-2.8 -I/usr/include/wx-2.8 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D__WXGTK__ -pthread
#rex02_LDFLAGS = -pthread -Wl,-Bsymbolic-functions -lwx_gtk2ud_richtext-2.8 -lwx_gtk2ud_aui-2.8 -lwx_gtk2ud_xrc-2.8 -lwx_gtk2ud_qa-2.8 -lwx_gtk2ud_html-2.8 -lwx_gtk2ud_adv-2.8 -lwx_gtk2ud_core-2.8 -lwx_baseud_xml-2.8 -lwx_baseud_net-2.8 -lwx_baseud-2.8
bin_PROGRAMS += arr01 bh01 hl01 htb02 rbt02 rbt03 sp01 sp02
bin_PROGRAMS += arr01 bh01 hl01 htb02 rbt02 rbt03 sp01 sp02 str02
arr01_SOURCES = arr01.cpp
arr01_LDADD = $(LDADD) -lqsecmnxx
@ -124,4 +124,7 @@ sp01_LDADD = $(LDADD) -lqsecmnxx
sp02_SOURCES = sp02.cpp # SharedPtr
sp02_LDADD = $(LDADD) -lqsecmnxx
str02_SOURCES = str02.cpp # SharedPtr
str02_LDADD = $(LDADD) -lqsecmnxx
endif

View File

@ -68,7 +68,7 @@ bin_PROGRAMS = chr01$(EXEEXT) dir01$(EXEEXT) dll$(EXEEXT) \
#rex02_SOURCES = rex02.cpp
#rex02_CXXFLAGS = -I/usr/lib/wx/include/gtk2-unicode-release-2.8 -I/usr/include/wx-2.8 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D__WXGTK__ -pthread
#rex02_LDFLAGS = -pthread -Wl,-Bsymbolic-functions -lwx_gtk2ud_richtext-2.8 -lwx_gtk2ud_aui-2.8 -lwx_gtk2ud_xrc-2.8 -lwx_gtk2ud_qa-2.8 -lwx_gtk2ud_html-2.8 -lwx_gtk2ud_adv-2.8 -lwx_gtk2ud_core-2.8 -lwx_baseud_xml-2.8 -lwx_baseud_net-2.8 -lwx_baseud-2.8
@ENABLE_CXX_TRUE@am__append_2 = arr01 bh01 hl01 htb02 rbt02 rbt03 sp01 sp02
@ENABLE_CXX_TRUE@am__append_2 = arr01 bh01 hl01 htb02 rbt02 rbt03 sp01 sp02 str02
subdir = samples/cmn
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@ -88,7 +88,8 @@ CONFIG_CLEAN_FILES =
CONFIG_CLEAN_VPATH_FILES =
@ENABLE_CXX_TRUE@am__EXEEXT_1 = arr01$(EXEEXT) bh01$(EXEEXT) \
@ENABLE_CXX_TRUE@ hl01$(EXEEXT) htb02$(EXEEXT) rbt02$(EXEEXT) \
@ENABLE_CXX_TRUE@ rbt03$(EXEEXT) sp01$(EXEEXT) sp02$(EXEEXT)
@ENABLE_CXX_TRUE@ rbt03$(EXEEXT) sp01$(EXEEXT) sp02$(EXEEXT) \
@ENABLE_CXX_TRUE@ str02$(EXEEXT)
am__installdirs = "$(DESTDIR)$(bindir)"
PROGRAMS = $(bin_PROGRAMS)
am__arr01_SOURCES_DIST = arr01.cpp
@ -269,6 +270,10 @@ am_str01_OBJECTS = str01.$(OBJEXT)
str01_OBJECTS = $(am_str01_OBJECTS)
str01_LDADD = $(LDADD)
str01_DEPENDENCIES = $(am__DEPENDENCIES_2)
am__str02_SOURCES_DIST = str02.cpp
@ENABLE_CXX_TRUE@am_str02_OBJECTS = str02.$(OBJEXT)
str02_OBJECTS = $(am_str02_OBJECTS)
@ENABLE_CXX_TRUE@str02_DEPENDENCIES = $(am__DEPENDENCIES_3)
task01_SOURCES = task01.c
task01_OBJECTS = task01.$(OBJEXT)
task01_LDADD = $(LDADD)
@ -343,8 +348,9 @@ SOURCES = $(arr01_SOURCES) $(bh01_SOURCES) $(chr01_SOURCES) dir01.c \
$(rbt01_SOURCES) $(rbt02_SOURCES) $(rbt03_SOURCES) \
$(rex01_SOURCES) $(sio01_SOURCES) $(sio02_SOURCES) \
$(sio03_SOURCES) $(sll_SOURCES) $(slmb01_SOURCES) \
$(sp01_SOURCES) $(sp02_SOURCES) $(str01_SOURCES) task01.c \
$(time_SOURCES) $(tre01_SOURCES) uri01.c $(xma_SOURCES)
$(sp01_SOURCES) $(sp02_SOURCES) $(str01_SOURCES) \
$(str02_SOURCES) task01.c $(time_SOURCES) $(tre01_SOURCES) \
uri01.c $(xma_SOURCES)
DIST_SOURCES = $(am__arr01_SOURCES_DIST) $(am__bh01_SOURCES_DIST) \
$(chr01_SOURCES) dir01.c $(dll_SOURCES) $(env01_SOURCES) \
$(fio01_SOURCES) $(fio02_SOURCES) $(fma_SOURCES) \
@ -358,8 +364,9 @@ DIST_SOURCES = $(am__arr01_SOURCES_DIST) $(am__bh01_SOURCES_DIST) \
$(am__rbt03_SOURCES_DIST) $(rex01_SOURCES) $(sio01_SOURCES) \
$(sio02_SOURCES) $(sio03_SOURCES) $(sll_SOURCES) \
$(slmb01_SOURCES) $(am__sp01_SOURCES_DIST) \
$(am__sp02_SOURCES_DIST) $(str01_SOURCES) task01.c \
$(time_SOURCES) $(tre01_SOURCES) uri01.c $(xma_SOURCES)
$(am__sp02_SOURCES_DIST) $(str01_SOURCES) \
$(am__str02_SOURCES_DIST) task01.c $(time_SOURCES) \
$(tre01_SOURCES) uri01.c $(xma_SOURCES)
am__can_run_installinfo = \
case $$AM_UPDATE_INFO_DIR in \
n|no|NO) false;; \
@ -599,6 +606,8 @@ xma_SOURCES = xma.c
@ENABLE_CXX_TRUE@sp01_LDADD = $(LDADD) -lqsecmnxx
@ENABLE_CXX_TRUE@sp02_SOURCES = sp02.cpp # SharedPtr
@ENABLE_CXX_TRUE@sp02_LDADD = $(LDADD) -lqsecmnxx
@ENABLE_CXX_TRUE@str02_SOURCES = str02.cpp # SharedPtr
@ENABLE_CXX_TRUE@str02_LDADD = $(LDADD) -lqsecmnxx
all: all-am
.SUFFIXES:
@ -808,6 +817,9 @@ sp02$(EXEEXT): $(sp02_OBJECTS) $(sp02_DEPENDENCIES) $(EXTRA_sp02_DEPENDENCIES)
str01$(EXEEXT): $(str01_OBJECTS) $(str01_DEPENDENCIES) $(EXTRA_str01_DEPENDENCIES)
@rm -f str01$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(str01_OBJECTS) $(str01_LDADD) $(LIBS)
str02$(EXEEXT): $(str02_OBJECTS) $(str02_DEPENDENCIES) $(EXTRA_str02_DEPENDENCIES)
@rm -f str02$(EXEEXT)
$(AM_V_CXXLD)$(CXXLINK) $(str02_OBJECTS) $(str02_LDADD) $(LIBS)
task01$(EXEEXT): $(task01_OBJECTS) $(task01_DEPENDENCIES) $(EXTRA_task01_DEPENDENCIES)
@rm -f task01$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(task01_OBJECTS) $(task01_LDADD) $(LIBS)
@ -873,6 +885,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sp01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sp02.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str02.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/task01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/time.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tre01.Po@am__quote@

35
qse/samples/cmn/str02.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <qse/cmn/sio.h>
#include <qse/cmn/String.hpp>
#include <qse/cmn/HeapMmgr.hpp>
void t1 ()
{
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
QSE::String* z = new QSE::String();
{
QSE::String x (&heap_mmgr, QSE_T("this is a sample string"));
QSE::String y (x);
*z = y;
qse_printf (QSE_T("[%s]\n"), x.getBuffer());
}
qse_printf (QSE_T("-----------------\n"));
delete z;
}
int main ()
{
qse_openstdsios ();
t1 ();
qse_printf (QSE_T("=================\n"));
qse_closestdsios ();
return 0;
}