From e966f815d45cbb4d1204e4f4ec770af947e45890 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Sat, 7 Jul 2007 10:09:42 +0000 Subject: [PATCH] changed wxVectorBase and WX_DECLARE_BASE into wxVector template git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47212 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/clntdata.h | 8 +- include/wx/vector.h | 264 +++++++++++++++++--------------------- tests/Makefile.in | 6 +- tests/makefile.bcc | 6 +- tests/makefile.gcc | 6 +- tests/makefile.vc | 6 +- tests/makefile.wat | 6 +- tests/test.bkl | 1 + tests/test_test.dsp | 4 + tests/vectors/vectors.cpp | 66 ++++++++++ 10 files changed, 217 insertions(+), 156 deletions(-) create mode 100644 tests/vectors/vectors.cpp diff --git a/include/wx/clntdata.h b/include/wx/clntdata.h index 33fae4c104..c1d09e7c68 100644 --- a/include/wx/clntdata.h +++ b/include/wx/clntdata.h @@ -169,12 +169,6 @@ struct WXDLLIMPEXP_BASE wxClientDataDictionaryPair wxClientData* data; }; -_WX_DECLARE_VECTOR( - wxClientDataDictionaryPair, - wxClientDataDictionaryPairVector, - WXDLLIMPEXP_BASE -); - // this class is used internally to maintain the association between items // of (some subclasses of) wxControlWithItems and their client data // NOTE: this class does not keep track of whether it contains @@ -266,7 +260,7 @@ private: return m_vec.size(); } - wxClientDataDictionaryPairVector m_vec; + wxVector m_vec; }; #endif // _WX_CLNTDATAH__ diff --git a/include/wx/vector.h b/include/wx/vector.h index f8aa5625eb..1c181c2c18 100644 --- a/include/wx/vector.h +++ b/include/wx/vector.h @@ -2,9 +2,10 @@ // Name: wx/vector.h // Purpose: STL vector clone // Author: Lindsay Mathieson -// Modified by: +// Modified by: Vaclav Slavik - make it a template // Created: 30.07.2001 -// Copyright: (c) 2001 Lindsay Mathieson +// Copyright: (c) 2001 Lindsay Mathieson , +// 2007 Vaclav Slavik // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// @@ -13,17 +14,109 @@ #include "wx/defs.h" -class WXDLLIMPEXP_BASE wxVectorBase +#if 0 // wxUSE_STL + +// FIXME: can't do this yet, wxVector::erase() is different (takes index, +// not iterator) +#include +#define wxVector std::vector + +#else // !wxUSE_STL + +template +class wxVector { public: typedef size_t size_type; -private: - size_type m_allocsize; - size_type m_size, - m_capacity; - void **m_objects; + typedef T value_type; + + wxVector() : m_allocsize(16), m_size(0), m_capacity(0), m_objects(0) {} + + wxVector(const wxVector& c) + { + wxCHECK2(Copy(c), return); + } + + ~wxVector() + { + clear(); + } + + void clear() + { + for (size_type i = 0; i < size(); i++) + delete m_objects[i]; + free(m_objects); + m_objects = 0; + m_size = m_capacity = 0; + } + + void reserve(size_type n) + { + if ( !Alloc(n) ) + { + wxFAIL_MSG( _T("out of memory in wxVector::reserve()") ); + } + } + + size_type size() const + { + return m_size; + } + + size_type capacity() const + { + return m_capacity; + } + + bool empty() const + { + return size() == 0; + } + + wxVector& operator=(const wxVector& vb) + { + wxCHECK(Copy(vb), *this); + return *this; + } + + void push_back(const value_type& o) + { + wxCHECK2(Alloc(size() + 1), return); + Append(new value_type(o)); + } + + void pop_back() + { + RemoveAt(size() - 1); + } + + const value_type& at(size_type idx) const + { + wxASSERT(idx < m_size); + return *m_objects[idx]; + } + + value_type& at(size_type idx) + { + wxASSERT(idx < m_size); + return *m_objects[idx]; + } -protected: + const value_type& operator[](size_type idx) const { return at(idx); } + value_type& operator[](size_type idx) { return at(idx); } + const value_type& front() const { return at(0); } + value_type& front() { return at(0); } + const value_type& back() const { return at(size() - 1); } + value_type& back() { return at(size() - 1); } + + size_type erase(size_type idx) + { + RemoveAt(idx); + return idx; + } + +private: bool Alloc(size_type sz) { // work in multiples of m_allocsize; @@ -32,27 +125,16 @@ protected: return true; // try to realloc - void *mem = realloc(m_objects, sizeof(void *) * sz); + void *mem = realloc(m_objects, sizeof(value_type*) * sz); if (! mem) return false; // failed // success - m_objects = (void **) mem; + m_objects = (value_type **) mem; m_capacity = sz; return true; } - // untyped destructor of elements - must be overriden - virtual void Free(void *) = 0; - // untyped copy constructor of elements - must be overriden - virtual void *Copy(const void *) const = 0; - - const void *GetItem(size_type idx) const - { - wxASSERT(idx < m_size); - return m_objects[idx]; - } - - void Append(void *obj) + void Append(value_type *obj) { wxASSERT(m_size < m_capacity); m_objects[m_size] = obj; @@ -62,7 +144,7 @@ protected: void RemoveAt(size_type idx) { wxASSERT(idx < m_size); - Free(m_objects[idx]); + delete m_objects[idx]; if (idx < m_size - 1) memcpy( m_objects + idx, @@ -71,7 +153,7 @@ protected: m_size--; } - bool copy(const wxVectorBase& vb) + bool Copy(const wxVector& vb) { clear(); if (! Alloc(vb.size())) @@ -79,7 +161,7 @@ protected: for (size_type i = 0; i < vb.size(); i++) { - void *o = vb.Copy(vb.GetItem(i)); + value_type *o = new value_type(vb.at(i)); if (! o) return false; Append(o); @@ -88,125 +170,19 @@ protected: return true; } -public: - wxVectorBase() : m_allocsize(16), m_size(0), m_capacity(0), m_objects(0) {} - virtual ~wxVectorBase() {} // calm down GCC - - void clear() - { - for (size_type i = 0; i < size(); i++) - Free(m_objects[i]); - free(m_objects); - m_objects = 0; - m_size = m_capacity = 0; - } - - void reserve(size_type n) - { - if ( !Alloc(n) ) - { - wxFAIL_MSG( _T("out of memory in wxVector::reserve()") ); - } - } - - size_type size() const - { - return m_size; - } - - size_type capacity() const - { - return m_capacity; - } - - bool empty() const - { - return size() == 0; - } - - wxVectorBase& operator = (const wxVectorBase& vb) - { - wxCHECK(copy(vb), *this); - return *this; - } +private: + size_type m_allocsize; + size_type m_size, + m_capacity; + value_type **m_objects; }; -#define WX_DECLARE_VECTORBASE(obj, cls)\ -protected:\ - virtual void Free(void *o)\ - {\ - delete (obj *) o;\ - }\ - virtual void *Copy(const void *o) const\ - {\ - return new obj(*(obj *) o);\ - }\ -public:\ - cls() {}\ - cls(const cls& c) : wxVectorBase()\ - {\ - wxCHECK2(copy(c), return);\ - }\ - ~cls()\ - {\ - clear();\ - } - -#define _WX_DECLARE_VECTOR(obj, cls, exp)\ -class exp cls : public wxVectorBase\ -{\ - WX_DECLARE_VECTORBASE(obj, cls)\ -public:\ - void push_back(const obj& o)\ - {\ - wxCHECK2(Alloc(size() + 1), return);\ - Append(new obj(o));\ - }\ - void pop_back()\ - {\ - RemoveAt(size() - 1);\ - }\ - const obj& at(size_type idx) const\ - {\ - return *(obj *) GetItem(idx);\ - }\ - obj& at(size_type idx)\ - {\ - return *(obj *) GetItem(idx);\ - }\ - const obj& operator[](size_type idx) const\ - {\ - return at(idx);\ - }\ - obj& operator[](size_type idx)\ - {\ - return at(idx);\ - }\ - const obj& front() const\ - {\ - return at(0);\ - }\ - obj& front()\ - {\ - return at(0);\ - }\ - const obj& back() const\ - {\ - return at(size() - 1);\ - }\ - obj& back()\ - {\ - return at(size() - 1);\ - }\ - size_type erase(size_type idx)\ - {\ - RemoveAt(idx);\ - return idx;\ - }\ -} - -#define WX_DECLARE_VECTOR(obj, cls) \ - _WX_DECLARE_VECTOR(obj, cls, WXDLLEXPORT) +#endif // wxUSE_STL/!wxUSE_STL -#endif // _WX_VECTOR_H_ +#if WXWIN_COMPATIBILITY_2_8 + #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector cls + #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls) + #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls) +#endif // WXWIN_COMPATIBILITY_2_8 +#endif // _WX_VECTOR_H_ diff --git a/tests/Makefile.in b/tests/Makefile.in index 41c9b375ef..6e25a8e5e8 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -91,7 +91,8 @@ TEST_OBJECTS = \ test_zlibstream.o \ test_textfiletest.o \ test_atomic.o \ - test_uris.o + test_uris.o \ + test_vectors.o TEST_ODEP = $(___pch_testprec_test_testprec_h_gch___depname) TEST_GUI_CXXFLAGS = $(__test_gui_PCH_INC) -D__WX$(TOOLKIT)__ \ $(__WXUNIV_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) \ @@ -437,6 +438,9 @@ test_atomic.o: $(srcdir)/thread/atomic.cpp $(TEST_ODEP) test_uris.o: $(srcdir)/uris/uris.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/uris/uris.cpp +test_vectors.o: $(srcdir)/vectors/vectors.cpp $(TEST_ODEP) + $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/vectors/vectors.cpp + test_gui_sample_rc.o: $(srcdir)/../samples/sample.rc $(TEST_GUI_ODEP) $(WINDRES) -i$< -o$@ --define __WX$(TOOLKIT)__ $(__WXUNIV_DEFINE_p_5) $(__EXCEPTIONS_DEFINE_p_5) $(__RTTI_DEFINE_p_5) $(__THREAD_DEFINE_p_5) --include-dir $(srcdir) $(__DLLFLAG_p_5) --include-dir $(srcdir)/../samples $(__RCDEFDIR_p_1) --include-dir $(top_srcdir)/include diff --git a/tests/makefile.bcc b/tests/makefile.bcc index a55004cd19..0baf188f9f 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -79,7 +79,8 @@ TEST_OBJECTS = \ $(OBJS)\test_zlibstream.obj \ $(OBJS)\test_textfiletest.obj \ $(OBJS)\test_atomic.obj \ - $(OBJS)\test_uris.obj + $(OBJS)\test_uris.obj \ + $(OBJS)\test_vectors.obj TEST_GUI_CXXFLAGS = $(__RUNTIME_LIBS) -I$(BCCDIR)\include $(__DEBUGINFO) \ $(__OPTIMIZEFLAG) $(__THREADSFLAG_1) -D__WXMSW__ $(__WXUNIV_DEFINE_p) \ $(__DEBUG_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) \ @@ -465,6 +466,9 @@ $(OBJS)\test_atomic.obj: .\thread\atomic.cpp $(OBJS)\test_uris.obj: .\uris\uris.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) $** +$(OBJS)\test_vectors.obj: .\vectors\vectors.cpp + $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) $** + $(OBJS)\test_gui_sample.res: .\..\samples\sample.rc brcc32 -32 -r -fo$@ -i$(BCCDIR)\include -d__WXMSW__ $(__WXUNIV_DEFINE_p_3) $(__DEBUG_DEFINE_p_3) $(__EXCEPTIONS_DEFINE_p_3) $(__RTTI_DEFINE_p_3) $(__THREAD_DEFINE_p_3) $(__UNICODE_DEFINE_p_3) $(__MSLU_DEFINE_p_3) $(__GFXCTX_DEFINE_p_3) -i$(SETUPHDIR) -i.\..\include -i. $(__DLLFLAG_p_3) -i.\..\samples -dNOPCH $** diff --git a/tests/makefile.gcc b/tests/makefile.gcc index 6dd6d2ac5b..b24fac7557 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -71,7 +71,8 @@ TEST_OBJECTS = \ $(OBJS)\test_zlibstream.o \ $(OBJS)\test_textfiletest.o \ $(OBJS)\test_atomic.o \ - $(OBJS)\test_uris.o + $(OBJS)\test_uris.o \ + $(OBJS)\test_vectors.o TEST_GUI_CXXFLAGS = $(__DEBUGINFO) $(__OPTIMIZEFLAG) $(__THREADSFLAG) \ $(GCCFLAGS) -DHAVE_W32API_H -D__WXMSW__ $(__WXUNIV_DEFINE_p) \ $(__DEBUG_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) \ @@ -443,6 +444,9 @@ $(OBJS)\test_atomic.o: ./thread/atomic.cpp $(OBJS)\test_uris.o: ./uris/uris.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_vectors.o: ./vectors/vectors.cpp + $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_gui_sample_rc.o: ./../samples/sample.rc windres --use-temp-file -i$< -o$@ --define __WXMSW__ $(__WXUNIV_DEFINE_p_3) $(__DEBUG_DEFINE_p_3) $(__EXCEPTIONS_DEFINE_p_3) $(__RTTI_DEFINE_p_3) $(__THREAD_DEFINE_p_3) $(__UNICODE_DEFINE_p_3) $(__MSLU_DEFINE_p_3) $(__GFXCTX_DEFINE_p_3) --include-dir $(SETUPHDIR) --include-dir ./../include --include-dir . $(__DLLFLAG_p_3) --include-dir ./../samples --define NOPCH diff --git a/tests/makefile.vc b/tests/makefile.vc index 38eb98b933..75962edb86 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -72,7 +72,8 @@ TEST_OBJECTS = \ $(OBJS)\test_zlibstream.obj \ $(OBJS)\test_textfiletest.obj \ $(OBJS)\test_atomic.obj \ - $(OBJS)\test_uris.obj + $(OBJS)\test_uris.obj \ + $(OBJS)\test_vectors.obj TEST_GUI_CXXFLAGS = /M$(__RUNTIME_LIBS_23)$(__DEBUGRUNTIME) /DWIN32 \ $(__DEBUGINFO) /Fd$(OBJS)\test_gui.pdb $(____DEBUGRUNTIME) \ $(__OPTIMIZEFLAG) $(__NO_VC_CRTDBG_p) /D__WXMSW__ $(__WXUNIV_DEFINE_p) \ @@ -550,6 +551,9 @@ $(OBJS)\test_atomic.obj: .\thread\atomic.cpp $(OBJS)\test_uris.obj: .\uris\uris.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) $** +$(OBJS)\test_vectors.obj: .\vectors\vectors.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) $** + $(OBJS)\test_gui_dummy.obj: .\dummy.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) /Yctestprec.h $** diff --git a/tests/makefile.wat b/tests/makefile.wat index b200472846..666adb58d1 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -276,7 +276,8 @@ TEST_OBJECTS = & $(OBJS)\test_zlibstream.obj & $(OBJS)\test_textfiletest.obj & $(OBJS)\test_atomic.obj & - $(OBJS)\test_uris.obj + $(OBJS)\test_uris.obj & + $(OBJS)\test_vectors.obj TEST_GUI_CXXFLAGS = $(__DEBUGINFO) $(__OPTIMIZEFLAG) $(__THREADSFLAG) & $(__RUNTIME_LIBS) -d__WXMSW__ $(__WXUNIV_DEFINE_p) $(__DEBUG_DEFINE_p) & $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) $(__THREAD_DEFINE_p) & @@ -498,6 +499,9 @@ $(OBJS)\test_atomic.obj : .AUTODEPEND .\thread\atomic.cpp $(OBJS)\test_uris.obj : .AUTODEPEND .\uris\uris.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< +$(OBJS)\test_vectors.obj : .AUTODEPEND .\vectors\vectors.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< + $(OBJS)\test_gui_sample.res : .AUTODEPEND .\..\samples\sample.rc wrc -q -ad -bt=nt -r -fo=$^@ -d__WXMSW__ $(__WXUNIV_DEFINE_p) $(__DEBUG_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) $(__THREAD_DEFINE_p) $(__UNICODE_DEFINE_p) $(__GFXCTX_DEFINE_p) -i=$(SETUPHDIR) -i=.\..\include -i=. $(__DLLFLAG_p) -i=.\..\samples -dNOPCH $< diff --git a/tests/test.bkl b/tests/test.bkl index 925a6556dc..410c9fc718 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -66,6 +66,7 @@ textfile/textfiletest.cpp thread/atomic.cpp uris/uris.cpp + vectors/vectors.cpp net base diff --git a/tests/test_test.dsp b/tests/test_test.dsp index cc3c4d0407..647f81e221 100644 --- a/tests/test_test.dsp +++ b/tests/test_test.dsp @@ -597,6 +597,10 @@ SOURCE=.\strings\vararg.cpp # End Source File # Begin Source File +SOURCE=.\vectors\vectors.cpp +# End Source File +# Begin Source File + SOURCE=.\strings\vsnprintf.cpp # End Source File # Begin Source File diff --git a/tests/vectors/vectors.cpp b/tests/vectors/vectors.cpp new file mode 100644 index 0000000000..ca75e21942 --- /dev/null +++ b/tests/vectors/vectors.cpp @@ -0,0 +1,66 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/vectors/vectors.cpp +// Purpose: wxVector unit test +// Author: Vaclav Slavik +// Created: 2007-07-07 +// RCS-ID: $Id$ +// Copyright: (c) 2007 Vaclav Slavik +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/wx.h" +#endif // WX_PRECOMP + +#include "wx/vector.h" + +// -------------------------------------------------------------------------- +// test class +// -------------------------------------------------------------------------- + +class VectorsTestCase : public CppUnit::TestCase +{ +public: + VectorsTestCase() {} + +private: + CPPUNIT_TEST_SUITE( VectorsTestCase ); + CPPUNIT_TEST( PushTest ); + CPPUNIT_TEST_SUITE_END(); + + void PushTest(); + + DECLARE_NO_COPY_CLASS(VectorsTestCase) +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( VectorsTestCase ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VectorsTestCase, "VectorsTestCase" ); + +void VectorsTestCase::PushTest() +{ + wxVector v; + + CPPUNIT_ASSERT( v.size() == 0 ); + v.push_back(1); + CPPUNIT_ASSERT( v.size() == 1 ); + v.push_back(2); + CPPUNIT_ASSERT( v.size() == 2 ); + v.push_back(42); + CPPUNIT_ASSERT( v.size() == 3 ); + + CPPUNIT_ASSERT( v[0] == 1 ); + CPPUNIT_ASSERT( v[1] == 2 ); + CPPUNIT_ASSERT( v[2] == 42 ); +} -- 2.45.2