From 1c1d9b4158535af0161b45eaf5cb2d37e1c08567 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 24 Mar 2015 09:34:57 +0000 Subject: [PATCH] changed code for old compilers --- qse/include/qse/Types.hpp | 51 ++++++++++++++++++++------- qse/include/qse/cmn/Array.hpp | 31 +++++++++------- qse/include/qse/cmn/BinaryHeap.hpp | 8 ++--- qse/include/qse/cmn/HashTable.hpp | 24 ++++--------- qse/include/qse/cmn/Mmgr.hpp | 4 +++ qse/include/qse/cmn/Mpool.hpp | 4 +++ qse/include/qse/cmn/RedBlackTable.hpp | 12 +++---- qse/include/qse/cmn/RedBlackTree.hpp | 2 +- qse/lib/cmn/Mmgr.cpp | 4 +++ qse/lib/cmn/Mpool.cpp | 13 ++++--- qse/samples/cmn/arr02.cpp | 17 +++++---- qse/samples/cmn/bh02.cpp | 4 +-- 12 files changed, 108 insertions(+), 66 deletions(-) diff --git a/qse/include/qse/Types.hpp b/qse/include/qse/Types.hpp index e0cc026e..8633a7d4 100644 --- a/qse/include/qse/Types.hpp +++ b/qse/include/qse/Types.hpp @@ -34,23 +34,26 @@ #include #include -// The QSE_CPP_CALL_DESTRUCTOR() macro calls a destructor explicitly. -// The QSE_CPP_CALL_PLACEMENT_DELETE1() macro calls the global operator delete -// with 1 extra argument given. - #if (__cplusplus >= 201103L) // C++11 - /// The QSE_ENABLE_CPP11_MOVE macro enables C++11 move semantics + /// The QSE_CPP_ENABLE_CPP1_MOVE macro enables C++11 move semantics /// in various classes. - #define QSE_ENABLE_CPP11_MOVE 1 + #define QSE_CPP_ENABLE_CPP1_MOVE 1 + // The QSE_CPP_CALL_DESTRUCTOR() macro calls a destructor explicitly. #define QSE_CPP_CALL_DESTRUCTOR(ptr, class_name) ((ptr)->~class_name()) - #define QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, arg1) (::operator delete ((ptr), (arg1))) + + // The QSE_CPP_CALL_PLACEMENT_DELETE1() macro calls the global operator delete + // with 1 extra argument given. + #define QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, arg1) (::operator delete((ptr), (arg1))) + + #define QSE_CPP_TEMPLATE_QUALIFIER template #elif (__cplusplus >= 199711L) // C++98 #define QSE_CPP_CALL_DESTRUCTOR(ptr, class_name) ((ptr)->~class_name()) - #define QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, arg1) (::operator delete ((ptr), (arg1))) + #define QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, arg1) (::operator delete((ptr), (arg1))) + #define QSE_CPP_TEMPLATE_QUALIFIER template #else @@ -68,7 +71,8 @@ // x->Node::~Node (); #define QSE_CPP_CALL_DESTRUCTOR(ptr, class_name) ((ptr)->class_name::~class_name()) - #define QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, arg1) (::operator delete ((ptr), (arg1))) + #define QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, arg1) (::operator delete((ptr), (arg1))) + #define QSE_CPP_TEMPLATE_QUALIFIER template #elif defined(__WATCOMC__) // WATCOM has a problem with this syntax. @@ -76,18 +80,41 @@ // But it doesn't support operator delete overloading. #define QSE_CPP_CALL_DESTRUCTOR(ptr, class_name) ((ptr)->~class_name()) - #define QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, arg1) (::operator_delete ((ptr), (arg1))) + #define QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, arg1) (::qse_operator_delete((ptr), (arg1))) + // When the name of a member template specialization appears after . or + // -> in a postfix-expression, or after :: in a qualified-id that explic- + // itly depends on a template-argument (_temp.dep_), the member template + // name must be prefixed by the keyword template. Otherwise the name is + // assumed to name a non-template. [Example: + // class X { + // public: + // template X* alloc(); + // }; + // void f(X* p) + // { + // X* p1 = p->alloc<200>(); + // // ill-formed: < means less than + // X* p2 = p->template alloc<200>(); + // // fine: < starts explicit qualification + // } + // --end example] + // + // WATCOM doesn't support this qualifier. + + #define QSE_CPP_TEMPLATE_QUALIFIER + #define QSE_CPP_NO_OPERATOR_DELETE_OVERLOADING 1 #else #define QSE_CPP_CALL_DESTRUCTOR(ptr, class_name) ((ptr)->~class_name()) - #define QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, arg1) (::operator delete ((ptr), (arg1))) + #define QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, arg1) (::operator delete((ptr), (arg1))) + #define QSE_CPP_TEMPLATE_QUALIFIER template #endif #endif -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) template struct QSE_CPP_RMREF { typedef T Type; }; template struct QSE_CPP_RMREF { typedef T Type; }; diff --git a/qse/include/qse/cmn/Array.hpp b/qse/include/qse/cmn/Array.hpp index 60da2729..0495afe7 100644 --- a/qse/include/qse/cmn/Array.hpp +++ b/qse/include/qse/cmn/Array.hpp @@ -27,6 +27,9 @@ #ifndef _QSE_CMN_ARRAY_HPP_ #define _QSE_CMN_ARRAY_HPP_ +/// \file +/// Provide the Array class. + #include #include @@ -66,6 +69,9 @@ struct ArrayResizer /// /// The Array class provides a dynamically resized array. +/// +/// With C++11, the Array class move-contructs values in various context. +/// The move constructor of the value must not raise an exception. /// template , typename RESIZER = ArrayResizer > class Array: public Mmged, public Growable @@ -122,7 +128,7 @@ public: } } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) Array (SelfType&& array): Mmged(array.getMmgr()), @@ -162,7 +168,7 @@ public: return *this; } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) SelfType& operator= (SelfType&& array) { if (this != &array) @@ -230,7 +236,7 @@ protected: return tmp; } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) T* clone_buffer_by_moving (T* srcbuf, qse_size_t capa, qse_size_t cnt) { QSE_ASSERT (capa > 0); @@ -262,7 +268,8 @@ protected: // if move-contruction ended up with an exception, // the original array can get into an unknown state eventually. // i don't attempt to restore the moved object as an exception - // may be raised during restoration. + // may be raised during restoration. i don't implement noexcept + // check yet. // // TODO: reconsider if this unwinding is needed //try { new((QSE::Mmgr*)QSE_NULL, &srcbuf[index]) T((T&&)tmp[index]); } @@ -299,7 +306,7 @@ protected: } } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) void put_item_by_moving (qse_size_t index, T&& value) { if (index >= this->count) @@ -425,7 +432,7 @@ public: this->update (index, value); } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) void setValueAt (qse_size_t index, T&& value) { this->update (index, QSE_CPP_RVREF(value)); @@ -459,7 +466,7 @@ protected: // shift the existing elements to the back by one slot. for (qse_size_t i = this->count; i > index; i--) { - #if defined(QSE_ENABLE_CPP11_MOVE) + #if defined(QSE_CPP_ENABLE_CPP1_MOVE) this->put_item_by_moving (i, QSE_CPP_RVREF(this->buffer[i - 1])); #else this->put_item (i, this->buffer[i - 1]); @@ -496,7 +503,7 @@ public: return index; } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) qse_size_t insert (qse_size_t index, T&& value) { // Unlike insert() in RedBlackTree and HashList, @@ -523,7 +530,7 @@ public: return index; } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) qse_size_t update (qse_size_t index, T&& value) { QSE_ASSERT (index < this->count); @@ -541,7 +548,7 @@ public: return this->insert (index, value); } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) qse_size_t upsert (qse_size_t index, T&& value) { if (index < this->count) @@ -559,7 +566,7 @@ public: return this->insert (index, value); } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) qse_size_t ensert (qse_size_t index, T&& value) { if (index < this->count) @@ -693,7 +700,7 @@ public: qse_size_t cnt = this->count; if (cnt > capa) cnt = capa; - #if defined(QSE_ENABLE_CPP11_MOVE) + #if defined(QSE_CPP_ENABLE_CPP1_MOVE) T* tmp = this->clone_buffer_by_moving (this->buffer, capa, cnt); #else T* tmp = this->clone_buffer (this->buffer, capa, cnt); diff --git a/qse/include/qse/cmn/BinaryHeap.hpp b/qse/include/qse/cmn/BinaryHeap.hpp index 8001859a..432485d6 100644 --- a/qse/include/qse/cmn/BinaryHeap.hpp +++ b/qse/include/qse/cmn/BinaryHeap.hpp @@ -135,7 +135,7 @@ public: { } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) BinaryHeap (SelfType& heap): ParentType (heap) { } @@ -154,7 +154,7 @@ public: return *this; } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) SelfType& operator= (SelfType&& heap) { if (this != &heap) @@ -212,7 +212,7 @@ public: return this->sift_up(index); } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) qse_size_t insert (T&& value) { qse_size_t index = this->count; @@ -234,7 +234,7 @@ public: return (this->greater_than(value, old))? this->sift_up(index): this->sift_down(index); } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) qse_size_t update (qse_size_t index, T&& value) { T old = QSE_CPP_RVREF(this->buffer[index]); diff --git a/qse/include/qse/cmn/HashTable.hpp b/qse/include/qse/cmn/HashTable.hpp index 0d4c1cc7..0df9a862 100644 --- a/qse/include/qse/cmn/HashTable.hpp +++ b/qse/include/qse/cmn/HashTable.hpp @@ -239,7 +239,7 @@ public: //if (!node) return QSE_NULL; //return &node->value; - PairNode* node = this->pair_list.template heterofindNode (key); + PairNode* node = this->pair_list.QSE_CPP_TEMPLATE_QUALIFIER heterofindNode (key); if (!node) return QSE_NULL; Pair& pair = node->value; pair.value = value; @@ -249,11 +249,7 @@ public: Pair* search (const K& key) { //PairNode* node = this->pair_list.search (Pair(key)); - #if defined(__WATCOMC__) - PairNode* node = this->pair_list.heterofindNode (key); - #else - PairNode* node = this->pair_list.template heterofindNode (key); - #endif + PairNode* node = this->pair_list.QSE_CPP_TEMPLATE_QUALIFIER heterofindNode (key); if (!node) return QSE_NULL; return &node->value; } @@ -261,22 +257,14 @@ public: int remove (const K& key) { //return this->pair_list.remove (Pair(key)); - #if defined(__WATCOMC__) - return this->pair_list.heteroremove (key); - #else - return this->pair_list.template heteroremove (key); - #endif + return this->pair_list.QSE_CPP_TEMPLATE_QUALIFIER heteroremove (key); } template Pair* heterosearch (const MK& key) { typedef MHeteroEqualer MEqualer; - #if defined(__WATCOMC__) - PairNode* node = this->pair_list.heterosearch (key); - #else - PairNode* node = this->pair_list.template heterosearch (key); - #endif + PairNode* node = this->pair_list.QSE_CPP_TEMPLATE_QUALIFIER heterosearch (key); if (!node) return QSE_NULL; return &node->value; } @@ -285,7 +273,7 @@ public: const Pair* heterosearch (const MK& key) const { typedef MHeteroEqualer MEqualer; - PairNode* node = this->pair_list.template heterosearch (key); + PairNode* node = this->pair_list.QSE_CPP_TEMPLATE_QUALIFIER heterosearch (key); if (!node) return QSE_NULL; return &node->value; } @@ -294,7 +282,7 @@ public: int heteroremove (const MK& key) { typedef MHeteroEqualer MEqualer; - return this->pair_list.template heteroremove (key); + return this->pair_list.QSE_CPP_TEMPLATE_QUALIFIER heteroremove (key); } void clear (bool clear_mpool = false) diff --git a/qse/include/qse/cmn/Mmgr.hpp b/qse/include/qse/cmn/Mmgr.hpp index 096f1eb2..6534726c 100644 --- a/qse/include/qse/cmn/Mmgr.hpp +++ b/qse/include/qse/cmn/Mmgr.hpp @@ -176,7 +176,11 @@ QSE_END_NAMESPACE(QSE) QSE_EXPORT void* operator new (qse_size_t size, QSE::Mmgr* mmgr); +#if defined(QSE_CPP_NO_OPERATOR_DELETE_OVERLOADING) +QSE_EXPORT void qse_operator_delete (void* ptr, QSE::Mmgr* mmgr); +#else QSE_EXPORT void operator delete (void* ptr, QSE::Mmgr* mmgr); +#endif QSE_EXPORT void* operator new (qse_size_t size, QSE::Mmgr* mmgr, void* existing_ptr); diff --git a/qse/include/qse/cmn/Mpool.hpp b/qse/include/qse/cmn/Mpool.hpp index c8125a1c..1e3dde9d 100644 --- a/qse/include/qse/cmn/Mpool.hpp +++ b/qse/include/qse/cmn/Mpool.hpp @@ -126,7 +126,11 @@ QSE_END_NAMESPACE(QSE) QSE_EXPORT void* operator new (qse_size_t size, QSE::Mpool* mp); +#if defined(QSE_CPP_NO_OPERATOR_DELETE_OVERLOADING) +QSE_EXPORT void qse_operator_delete (void* ptr, QSE::Mpool* mp); +#else QSE_EXPORT void operator delete (void* ptr, QSE::Mpool* mp); +#endif QSE_EXPORT void* operator new (qse_size_t size, QSE::Mpool* mp, void* existing_ptr); #endif diff --git a/qse/include/qse/cmn/RedBlackTable.hpp b/qse/include/qse/cmn/RedBlackTable.hpp index 14cdf582..cccd7284 100644 --- a/qse/include/qse/cmn/RedBlackTable.hpp +++ b/qse/include/qse/cmn/RedBlackTable.hpp @@ -206,7 +206,7 @@ public: //if (!node) return QSE_NULL; //return &node->value; - PairNode* node = this->pair_tree.template heterofindNode (key); + PairNode* node = this->pair_tree.QSE_CPP_TEMPLATE_QUALIFIER heterofindNode (key); if (!node) return QSE_NULL; Pair& pair = node->value; pair.value = value; @@ -216,7 +216,7 @@ public: Pair* search (const K& key) { //PairNode* node = this->pair_tree.update (Pair(key)); - PairNode* node = this->pair_tree.template heterofindNode (key); + PairNode* node = this->pair_tree.QSE_CPP_TEMPLATE_QUALIFIER heterofindNode (key); if (!node) return QSE_NULL; return &node->value; } @@ -224,14 +224,14 @@ public: int remove (const K& key) { //return this->pair_tree.remove (Pair(key)); - return this->pair_tree.template heteroremove (key); + return this->pair_tree.QSE_CPP_TEMPLATE_QUALIFIER heteroremove (key); } template Pair* heterosearch (const MK& key) { typedef MHeteroComparator MComparator; - PairNode* node = this->pair_tree.template heterosearch (key); + PairNode* node = this->pair_tree.QSE_CPP_TEMPLATE_QUALIFIER heterosearch (key); if (!node) return QSE_NULL; return &node->value; } @@ -240,7 +240,7 @@ public: const Pair* heterosearch (const MK& key) const { typedef MHeteroComparator MComparator; - PairNode* node = this->pair_tree.template heterosearch (key); + PairNode* node = this->pair_tree.QSE_CPP_TEMPLATE_QUALIFIER heterosearch (key); if (!node) return QSE_NULL; return &node->value; } @@ -249,7 +249,7 @@ public: int heteroremove (const MK& key) { typedef MHeteroComparator MComparator; - return this->pair_tree.template heteroremove (key); + return this->pair_tree.QSE_CPP_TEMPLATE_QUALIFIER heteroremove (key); } void clear (bool clear_mpool = false) diff --git a/qse/include/qse/cmn/RedBlackTree.hpp b/qse/include/qse/cmn/RedBlackTree.hpp index 05b2eb47..96c84036 100644 --- a/qse/include/qse/cmn/RedBlackTree.hpp +++ b/qse/include/qse/cmn/RedBlackTree.hpp @@ -971,7 +971,7 @@ public: template int heteroremove (const MT& datum) { - Node* node = this->template heterofind_node (datum); + Node* node = this->QSE_CPP_TEMPLATE_QUALIFIER heterofind_node (datum); if (node == QSE_NULL) return -1; this->remove_node (node); diff --git a/qse/lib/cmn/Mmgr.cpp b/qse/lib/cmn/Mmgr.cpp index 783becfc..e019075f 100644 --- a/qse/lib/cmn/Mmgr.cpp +++ b/qse/lib/cmn/Mmgr.cpp @@ -75,7 +75,11 @@ void* operator new (qse_size_t size, QSE::Mmgr* mmgr) return mmgr->allocate (size); } +#if defined(QSE_CPP_NO_OPERATOR_DELETE_OVERLOADING) +void qse_operator_delete (void* ptr, QSE::Mmgr* mmgr) +#else void operator delete (void* ptr, QSE::Mmgr* mmgr) +#endif { mmgr->dispose (ptr); } diff --git a/qse/lib/cmn/Mpool.cpp b/qse/lib/cmn/Mpool.cpp index f62375ca..acf0dfd9 100644 --- a/qse/lib/cmn/Mpool.cpp +++ b/qse/lib/cmn/Mpool.cpp @@ -196,21 +196,26 @@ void* operator new (qse_size_t size, QSE::Mpool* mp) // is never reached. goto use_mmgr; } - return mp->allocate (); + return mp->allocate(); } else { use_mmgr: // when the memory pool is not enabled, it goes through // the memory manager directly and it honors the size argument. - return ::operator new(size, mp->getMmgr()); + //return ::operator new(size, mp->getMmgr()); + return mp->getMmgr()->allocate(size); } } +#if defined(QSE_CPP_NO_OPERATOR_DELETE_OVERLOADING) +void qse_operator_delete (void* ptr, QSE::Mpool* mp) +#else void operator delete (void* ptr, QSE::Mpool* mp) +#endif { - if (mp->isEnabled()) mp->dispose (ptr); - else ::operator delete (ptr, mp->getMmgr()); + if (mp->isEnabled()) mp->dispose(ptr); + else mp->getMmgr()->dispose(ptr); } void* operator new (qse_size_t size, QSE::Mpool* mp, void* existing_ptr) diff --git a/qse/samples/cmn/arr02.cpp b/qse/samples/cmn/arr02.cpp index a5433591..0ab323e0 100644 --- a/qse/samples/cmn/arr02.cpp +++ b/qse/samples/cmn/arr02.cpp @@ -19,7 +19,8 @@ printf ("constructor %d\n", q); printf ("copy constructor %d\n", *q.x); } -#if defined(QSE_ENABLE_CPP11_MOVE) + +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) Julia (Julia&& q) { printf ("move constructor %d\n", *q.x); @@ -50,7 +51,7 @@ printf ("operator= %d\n", *q.x); return *this; } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) Julia& operator= (Julia&& q) { if (this != &q) @@ -68,9 +69,11 @@ printf ("move operator= %d\n", *q.x); int* x; }; +typedef QSE::Array JuliaArray; + int main () { - QSE::Array a0; + JuliaArray a0; for (int i = 0; i < 256; i++) { @@ -83,10 +86,10 @@ int main () a0.remove (2, 5); a0.update (5, Julia(9999)); -#if defined(QSE_ENABLE_CPP11_MOVE) - QSE::Array a1 ((QSE::Array&&)a0); +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) + JuliaArray a1 ((JuliaArray&&)a0); #else - QSE::Array a1 (a0); + JuliaArray a1 (a0); #endif printf ("OK: %d %d\n", (int)a0.getSize(), (int)a1.getSize()); @@ -96,7 +99,7 @@ int main () printf ("ITEM: %d => %d\n", (int)i, *a1[i].x); } printf ("----------------\n"); - a1.rotate (QSE::Array::ROTATE_LEFT, 2); + a1.rotate (JuliaArray::ROTATE_LEFT, 2); for (qse_size_t i = 0; i < a1.getSize(); i++) { printf ("ITEM: %d => %d\n", (int)i, *a1[i].x); diff --git a/qse/samples/cmn/bh02.cpp b/qse/samples/cmn/bh02.cpp index a035683c..61cfaaf3 100644 --- a/qse/samples/cmn/bh02.cpp +++ b/qse/samples/cmn/bh02.cpp @@ -19,7 +19,7 @@ public: this->x = new int (*q.x); } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) Julia (Julia&& q) { this->x = q.x; @@ -46,7 +46,7 @@ public: return *this; } -#if defined(QSE_ENABLE_CPP11_MOVE) +#if defined(QSE_CPP_ENABLE_CPP1_MOVE) Julia& operator= (Julia&& q) { if (this != &q)