stack.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/stack.h
  3. // Purpose: STL stack clone
  4. // Author: Lindsay Mathieson, Vadim Zeitlin
  5. // Created: 30.07.2001
  6. // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org> (WX_DECLARE_STACK)
  7. // 2011 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_STACK_H_
  11. #define _WX_STACK_H_
  12. #include "wx/vector.h"
  13. #if wxUSE_STD_CONTAINERS
  14. #include <stack>
  15. #define wxStack std::stack
  16. #else // !wxUSE_STD_CONTAINERS
  17. // Notice that unlike std::stack, wxStack currently always uses wxVector and
  18. // can't be used with any other underlying container type.
  19. //
  20. // Another difference is that comparison operators between stacks are not
  21. // implemented (but they should be, see 23.2.3.3 of ISO/IEC 14882:1998).
  22. template <typename T>
  23. class wxStack
  24. {
  25. public:
  26. typedef wxVector<T> container_type;
  27. typedef typename container_type::size_type size_type;
  28. typedef typename container_type::value_type value_type;
  29. wxStack() { }
  30. explicit wxStack(const container_type& cont) : m_cont(cont) { }
  31. // Default copy ctor, assignment operator and dtor are ok.
  32. bool empty() const { return m_cont.empty(); }
  33. size_type size() const { return m_cont.size(); }
  34. value_type& top() { return m_cont.back(); }
  35. const value_type& top() const { return m_cont.back(); }
  36. void push(const value_type& val) { m_cont.push_back(val); }
  37. void pop() { m_cont.pop_back(); }
  38. private:
  39. container_type m_cont;
  40. };
  41. #endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS
  42. // Deprecated macro-based class for compatibility only, don't use any more.
  43. #define WX_DECLARE_STACK(obj, cls) \
  44. class cls : public wxVector<obj> \
  45. {\
  46. public:\
  47. void push(const obj& o)\
  48. {\
  49. push_back(o); \
  50. };\
  51. \
  52. void pop()\
  53. {\
  54. pop_back(); \
  55. };\
  56. \
  57. obj& top()\
  58. {\
  59. return at(size() - 1);\
  60. };\
  61. const obj& top() const\
  62. {\
  63. return at(size() - 1); \
  64. };\
  65. }
  66. #endif // _WX_STACK_H_