From b1bf7dc722e2dba0632bd15bb407ae9d0a584ba5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 27 Feb 2011 12:46:34 +0000 Subject: [PATCH] Add a simple wxStack<> template class based on wxVector<>. This is still simpler than std::stack<> which can be used with any container and not just wxVector<> but better than the WX_DECLARE_STACK() macro which was all that we had before. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67047 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/stack.h | 50 +++++++++++++++++++++++++++-- interface/wx/stack.h | 75 ++++++++++++++++++++++++++++++++++++++++++++ src/msw/graphics.cpp | 4 +-- 4 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 interface/wx/stack.h diff --git a/docs/changes.txt b/docs/changes.txt index 93013b73ad..52638ef724 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -431,6 +431,7 @@ All: - Added wxNumberFormatter for dealing with thousands separators. - Added wxIntegerValidator<> and wxFloatingPointValidator<> validators. - Added wxIMAGE_OPTION_GIF_COMMENT to read and write GIF comments (troelsk). +- Added wxStack<> template class. Unix: diff --git a/include/wx/stack.h b/include/wx/stack.h index 835b736a63..1ca3cd657a 100644 --- a/include/wx/stack.h +++ b/include/wx/stack.h @@ -1,10 +1,10 @@ /////////////////////////////////////////////////////////////////////////////// // Name: wx/stack.h // Purpose: STL stack clone -// Author: Lindsay Mathieson -// Modified by: +// Author: Lindsay Mathieson, Vadim Zeitlin // Created: 30.07.2001 -// Copyright: (c) 2001 Lindsay Mathieson +// Copyright: (c) 2001 Lindsay Mathieson (WX_DECLARE_STACK) +// 2011 Vadim Zeitlin // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// @@ -13,6 +13,50 @@ #include "wx/vector.h" +#if wxUSE_STL + +#include +#define wxStack std::stack + +#else // !wxUSE_STL + +// Notice that unlike std::stack, wxStack currently always uses wxVector and +// can't be used with any other underlying container type. +// +// Another difference is that comparison operators between stacks are not +// implemented (but they should be, see 23.2.3.3 of ISO/IEC 14882:1998). + +template +class wxStack +{ +public: + typedef wxVector container_type; + typedef typename container_type::size_type size_type; + typedef typename container_type::value_type value_type; + + wxStack() { } + explicit wxStack(const container_type& cont) : m_cont(cont) { } + + // Default copy ctor, assignment operator and dtor are ok. + + + bool empty() const { return m_cont.empty(); } + size_type size() const { return m_cont.size(); } + + value_type& top() { return m_cont.back(); } + const value_type& top() const { return m_cont.back(); } + + void push(const value_type& val) { m_cont.push_back(val); } + void pop() { m_cont.pop_back(); } + +private: + container_type m_cont; +}; + +#endif // wxUSE_STL/!wxUSE_STL + + +// Deprecated macro-based class for compatibility only, don't use any more. #define WX_DECLARE_STACK(obj, cls) \ class cls : public wxVector \ {\ diff --git a/interface/wx/stack.h b/interface/wx/stack.h new file mode 100644 index 0000000000..fbf5b3b737 --- /dev/null +++ b/interface/wx/stack.h @@ -0,0 +1,75 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/stack.h +// Purpose: interface of wxStack +// Author: Vadim Zeitlin +// RCS-ID: $Id$ +// Copyright: (c) 2011 Vadim Zeitlin +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +/** + wxStack is similar to @c std::stack and can be used exactly like it. + + If wxWidgets is compiled in STL mode, wxStack will just be a typedef to + @c std::stack but the advantage of this class is that it is also available + on the (rare) platforms where STL is not, so using it makes the code + marginally more portable. If you only target the standard desktop + platforms, please always use @c std::stack directly instead. + + The main difference of this class compared to the standard version is that + it always uses wxVector as the underlying container and doesn't allow + specifying an alternative container type. Another missing part is that the + comparison operators between wxStacks are not currently implemented. Other + than that, this class is exactly the same as @c std::stack, so please refer + to the STL documentation for further information. + + @nolibrary + @category{containers} + + @see @ref overview_container, wxVector + + @since 2.9.2 +*/ +template +class wxSort +{ +public: + /// Type of the underlying container used. + typedef wxVector container_type; + + /// Type returned by size() method. + typedef typename container_type::size_type size_type; + + /// Type of the elements stored in the stack. + typedef typename container_type::value_type value_type; + + + /** + Stack can be created either empty or initialized with the contents of + an existing compatible container. + */ + //@{ + wxStack(); + explicit wxStack(const container_type& cont); + //@} + + /// Return whether the stack is currently empty. + bool empty() const; + + /// Return the number of elements in the stack. + size_type size() const; + + /** + Return the element on top of the stack. + */ + //@{ + value_type& top(); + const value_type& top(); + //@} + + /// Adds an element to the stack. + void push(const value_type& val); + + /// Removes the element currently on top of the stack. + void pop(); +}; diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index 984df21971..05bc7d863b 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -53,8 +53,6 @@ #include "wx/stack.h" -WX_DECLARE_STACK(GraphicsState, GraphicsStates); - namespace { @@ -385,7 +383,7 @@ private: const wxGraphicsBrush& backgroundBrush); Graphics* m_context; - GraphicsStates m_stateStack; + wxStack m_stateStack; GraphicsState m_state1; GraphicsState m_state2;