enhanced constructors of some Mmged classes.

added Mmged:setMmgr() and changed the accessibility of the member manager variable
This commit is contained in:
2015-03-11 01:47:26 +00:00
parent 23b718f366
commit 3793785e7c
19 changed files with 135 additions and 71 deletions

View File

@ -75,7 +75,8 @@ public:
INVALID_INDEX = ~(qse_size_t)0
};
Array (Mmgr* mmgr = QSE_NULL, qse_size_t capacity = DEFAULT_CAPACITY): Mmged (mmgr)
private:
void init_array (int capacity)
{
if (capacity <= 0)
{
@ -92,6 +93,17 @@ public:
this->count = 0;
}
public:
Array (qse_size_t capacity = DEFAULT_CAPACITY): Mmged (QSE_NULL)
{
this->init_array (capacity);
}
Array (Mmgr* mmgr, qse_size_t capacity = DEFAULT_CAPACITY): Mmged (mmgr)
{
this->init_array (capacity);
}
Array (const SelfType& array):
Mmged (array.getMmgr()),
count (0), capacity (0), buffer (QSE_NULL)

View File

@ -117,7 +117,12 @@ public:
INVALID_INDEX = ParentType::INVALID_INDEX
};
BinaryHeap (Mmgr* mmgr = QSE_NULL, qse_size_t capacity = DEFAULT_CAPACITY):
BinaryHeap (qse_size_t capacity = DEFAULT_CAPACITY):
ParentType (QSE_NULL, capacity)
{
}
BinaryHeap (Mmgr* mmgr, qse_size_t capacity = DEFAULT_CAPACITY):
ParentType (mmgr, capacity)
{
}

View File

@ -99,11 +99,8 @@ public:
MIN_LOAD_FACTOR = 20
};
HashList (
Mmgr* mmgr = QSE_NULL,
qse_size_t node_capacity = DEFAULT_CAPACITY,
qse_size_t load_factor = DEFAULT_LOAD_FACTOR,
qse_size_t mpb_size = 0): Mmged(mmgr)
private:
void init_list (qse_size_t node_capacity, qse_size_t load_factor, qse_size_t mpb_size)
{
if (node_capacity < MIN_CAPACITY) node_capacity = MIN_CAPACITY;
if (load_factor < MIN_LOAD_FACTOR) load_factor = MIN_LOAD_FACTOR;
@ -151,7 +148,25 @@ public:
this->threshold = node_capacity * load_factor / 100;
}
HashList (const SelfType& list): Mmged (list)
public:
HashList (
qse_size_t node_capacity = DEFAULT_CAPACITY,
qse_size_t load_factor = DEFAULT_LOAD_FACTOR,
qse_size_t mpb_size = 0): Mmged(QSE_NULL)
{
this->init_list (node_capacity, load_factor, mpb_size);
}
HashList (
Mmgr* mmgr,
qse_size_t node_capacity = DEFAULT_CAPACITY,
qse_size_t load_factor = DEFAULT_LOAD_FACTOR,
qse_size_t mpb_size = 0): Mmged(mmgr)
{
this->init_list (node_capacity, load_factor, mpb_size);
}
HashList (const SelfType& list): Mmged(list)
{
this->nodes = QSE_NULL;
this->node_capacity = 0;

View File

@ -117,15 +117,22 @@ public:
MIN_LOAD_FACTOR = PairList::MIN_LOAD_FACTOR
};
HashTable (Mmgr* mmgr = QSE_NULL,
qse_size_t capacity = DEFAULT_CAPACITY,
HashTable (qse_size_t capacity = DEFAULT_CAPACITY,
qse_size_t load_factor = DEFAULT_LOAD_FACTOR,
qse_size_t mpb_size = 0):
Mmged(mmgr), pair_list (mmgr, capacity, load_factor, mpb_size)
Mmged(QSE_NULL), pair_list(QSE_NULL, capacity, load_factor, mpb_size)
{
}
HashTable (const SelfType& table): Mmged (table), pair_list (table.pair_list)
HashTable (Mmgr* mmgr,
qse_size_t capacity = DEFAULT_CAPACITY,
qse_size_t load_factor = DEFAULT_LOAD_FACTOR,
qse_size_t mpb_size = 0):
Mmged(mmgr), pair_list(mmgr, capacity, load_factor, mpb_size)
{
}
HashTable (const SelfType& table): Mmged(table), pair_list(table.pair_list)
{
}

View File

@ -211,8 +211,16 @@ public:
this->clear (true);
}
LinkedList (Mmgr* mmgr = QSE_NULL, qse_size_t mpb_size = 0):
Mmged(mmgr), mp (mmgr, QSE_SIZEOF(Node), mpb_size)
LinkedList (qse_size_t mpb_size = 0):
Mmged(QSE_NULL), mp(QSE_NULL, QSE_SIZEOF(Node), mpb_size)
{
this->node_count = 0;
this->head_node = QSE_NULL;
this->tail_node = QSE_NULL;
}
LinkedList (Mmgr* mmgr, qse_size_t mpb_size = 0):
Mmged(mmgr), mp(mmgr, QSE_SIZEOF(Node), mpb_size)
{
this->node_count = 0;
this->head_node = QSE_NULL;

View File

@ -51,7 +51,8 @@ pkginclude_HEADERS = \
if ENABLE_CXX
pkginclude_HEADERS += \
Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp SharedPtr.hpp \
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

View File

@ -51,7 +51,8 @@ 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 SharedPtr.hpp \
@ENABLE_CXX_TRUE@ Mmgr.hpp StdMmgr.hpp HeapMmgr.hpp Mmged.hpp \
@ENABLE_CXX_TRUE@ ScopedPtr.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,9 +94,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 \
SharedPtr.hpp Mpool.hpp Association.hpp LinkedList.hpp \
HashList.hpp HashTable.hpp RedBlackTree.hpp RedBlackTable.hpp \
Array.hpp BinaryHeap.hpp
ScopedPtr.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'`;
am__vpath_adj = case $$p in \
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \

View File

@ -46,10 +46,19 @@ public:
///
/// The getMmgr() function returns the memory manager associated.
///
Mmgr* getMmgr () const { return this->mmgr; }
Mmgr* getMmgr () const { return this->_mmgr; }
protected:
Mmgr* mmgr;
///
/// The setMmgr() function changes the memory manager.
/// Changing memory manager requires extra care to be taken
/// especially when you have some data allocated with the previous
/// manager. for this reason, i put this as a protected function.
///
void setMmgr(Mmgr* mmgr);
private:
Mmgr* _mmgr;
};
/////////////////////////////////

View File

@ -193,24 +193,4 @@ void* operator new[] (qse_size_t size, QSE::Mmgr* mmgr);
void operator delete[] (void* ptr, QSE::Mmgr* mmgr);
#endif
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
// Customized deleter for ScopedPtr
template <typename T>
struct ScopedPtrMmgrDeleter
{
void operator() (T* ptr, void* arg)
{
ptr->~T ();
::operator delete (ptr, (QSE::Mmgr*)arg);
}
};
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////
#endif

View File

@ -91,12 +91,17 @@ public:
typedef typename PairTree::ConstIterator ConstIterator;
RedBlackTable (Mmgr* mmgr = QSE_NULL, qse_size_t mpb_size = 0):
Mmged(mmgr), pair_tree (mmgr, mpb_size)
RedBlackTable (qse_size_t mpb_size = 0):
Mmged(QSE_NULL), pair_tree(QSE_NULL, mpb_size)
{
}
RedBlackTable (const SelfType& table): Mmged (table), pair_tree (table.pair_tree)
RedBlackTable (Mmgr* mmgr, qse_size_t mpb_size = 0):
Mmged(mmgr), pair_tree(mmgr, mpb_size)
{
}
RedBlackTable (const SelfType& table): Mmged(table), pair_tree(table.pair_tree)
{
}

View File

@ -319,10 +319,8 @@ public:
typedef RedBlackTreeComparator<T> DefaultComparator;
RedBlackTree (Mmgr* mmgr = QSE_NULL, qse_size_t mpb_size = 0):
Mmged (mmgr),
mp (mmgr, QSE_SIZEOF(Node), mpb_size),
node_count (0)
private:
void init_tree (qse_size_t mpb_size)
{
#if defined(QSE_REDBLACKTREE_ALLOCATE_NIL)
// create a nil object. note it doesn't go into the memory pool.
@ -338,6 +336,19 @@ public:
this->root = this->nil;
}
public:
RedBlackTree (qse_size_t mpb_size = 0):
Mmged(QSE_NULL), mp(QSE_NULL, QSE_SIZEOF(Node), mpb_size), node_count(0)
{
this->init_tree (mpb_size);
}
RedBlackTree (Mmgr* mmgr, qse_size_t mpb_size = 0):
Mmged (mmgr), mp (mmgr, QSE_SIZEOF(Node), mpb_size), node_count (0)
{
this->init_tree (mpb_size);
}
RedBlackTree (const SelfType& rbt):
Mmged (rbt.getMmgr()),
mp (rbt.getMmgr(), rbt.mp.getDatumSize(), rbt.mp.getBlockSize()),

View File

@ -0,0 +1,193 @@
/*
* $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_SCOPEDPTR_HPP_
#define _QSE_CMN_SCOPEDPTR_HPP_
#include <qse/Uncopyable.hpp>
#include <qse/cmn/Mmgr.hpp>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
template <typename T>
struct ScopedPtrDeleter
{
void operator() (T* ptr, void* arg)
{
delete ptr;
}
};
template <typename T>
struct ScopedPtrArrayDeleter
{
void operator() (T* ptr, void* arg)
{
delete[] ptr;
}
};
template <typename T>
struct ScopedPtrMmgrDeleter
{
void operator() (T* ptr, void* arg)
{
ptr->~T ();
::operator delete (ptr, (QSE::Mmgr*)arg);
}
};
/// The ScopedPtr class is a template class that destroys the object the
/// pointer points to when its destructor is called. You can use this class
/// to free a certain resource associated to the pointer when it goes out
/// of the current scope.
///
/// \code
/// #include <stdio.h>
/// #include <qse/cmn/ScopedPtr.hpp>
/// #include <qse/cmn/HeapMmgr.hpp>
///
///
/// class X
/// {
/// public:
/// X() { printf ("X constructured\n"); }
/// ~X() { printf ("X destructed\n"); }
/// };
///
///
/// int main ()
/// {
/// QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
///
/// {
/// QSE::ScopedPtr<X> x1 (new X);
/// QSE::ScopedPtr<X,QSE::ScopedPtrArrayDeleter<X> > x3 (new X[10]);
/// QSE::ScopedPtr<X,QSE::ScopedPtrMmgrDeleter<X> > x2 (new(&heap_mmgr) X, &heap_mmgr);
/// }
///
/// return 0;
/// }
/// \endcode
///
template<typename T, typename DELETER = ScopedPtrDeleter<T> >
class QSE_EXPORT ScopedPtr: public Uncopyable
{
public:
typedef ScopedPtr<T,DELETER> SelfType;
typedef ScopedPtrDeleter<T> DefaultDeleter;
ScopedPtr (T* ptr = (T*)QSE_NULL, void* darg = (void*)QSE_NULL): _ptr (ptr), _darg (darg)
{
}
~ScopedPtr ()
{
if (this->_ptr)
{
this->deleter (this->_ptr, this->_darg);
}
}
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;
}
T* release ()
{
T* t = this->_ptr;
this->_ptr = (T*)QSE_NULL;
this->_darg = QSE_NULL;
return t;
}
void reset (T* ptr = (T*)QSE_NULL, void* darg = (T*)QSE_NULL)
{
if (this->_ptr)
{
this->deleter (this->_ptr, this->_darg);
}
this->_ptr = ptr;
this->_darg = darg;
}
protected:
T* _ptr;
void* _darg;
DELETER deleter;
};
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////
#endif

View File

@ -119,7 +119,7 @@ public:
// 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->setMmgr (sp.getMmgr());
this->item = sp.item;
this->item->ref++;