Allocator Builder
Policy Based C++ Template Allocator Library
 All Classes Functions Variables Enumerations Enumerator Groups Pages
stack.hpp
1 //
3 // Copyright 2014 Felix Petriconi
4 //
5 // License: http://boost.org/LICENSE_1_0.txt, Boost License 1.0
6 //
7 // Authors: http://petriconi.net, Felix Petriconi
8 //
10 #pragma once
11 
12 #include <type_traits>
13 #include <boost/type_traits.hpp>
14 
15 namespace alb {
16  inline namespace v_100 {
17  namespace internal {
18 
28  template <typename T, unsigned MaxSize>
29  class stack {
30  static_assert(boost::has_trivial_assign<T>::value, "T must be trivially copyable");
31  static_assert(boost::has_trivial_destructor<T>::value, "T must be trivially destroyable");
32 
33  T elements_[MaxSize];
34  int pos_;
35 
36  public:
37  using value_type = T;
38  static const size_t max_size = MaxSize;
39 
40  stack() noexcept
41  : pos_(-1)
42  {}
43 
44  bool push(T v) noexcept
45  {
46  if (pos_ < static_cast<int>(MaxSize) - 1) {
47  pos_++;
48  elements_[pos_] = std::move(v);
49  return true;
50  }
51  return false;
52  }
53 
54  bool pop(T &v) noexcept
55  {
56  if (pos_ >= 0) {
57  v = std::move(elements_[pos_]);
58  pos_--;
59  return true;
60  }
61  return false;
62  }
63 
64  bool empty() const noexcept
65  {
66  return pos_ == -1;
67  }
68  };
69  }
70  }
71 
72  using namespace v_100;
73 }