NiHu  2.0
stack.hpp
Go to the documentation of this file.
1 // This file is a part of NiHu, a C++ BEM template library.
2 //
3 // Copyright (C) 2012-2014 Peter Fiala <fiala@hit.bme.hu>
4 // Copyright (C) 2012-2014 Peter Rucz <rucz@hit.bme.hu>
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
24 #ifndef STACK_HPP_INCLUDED
25 #define STACK_HPP_INCLUDED
26 
27 #include "integer.hpp"
28 #include "sequence.hpp"
29 
30 namespace tmp
31 {
32 struct none;
33 struct stack_tag;
34 
35 namespace internal
36 {
37  template <class Head = none, class Tail = none>
38  struct stack_impl
39  {
40  typedef stack_impl type;
41 
42  typedef stack_tag tag;
43 
44  typedef Head head;
45  typedef Tail tail;
46  };
47 }
48 
50 typedef internal::stack_impl<none, none> empty_stack;
51 
53 template <class Seq>
55 {
58 };
59 
61 template <class Seq>
62 struct next<stack_iterator<Seq> > : stack_iterator<typename Seq::tail> {};
63 
64 namespace internal
65 {
66  template <class Stack>
67  struct stack_size : integer<int, stack_size<typename Stack::tail>::type::value + 1> {};
68 
69  template <>
70  struct stack_size<empty_stack> : integer<int, 0> {};
71 
72  template <>
73  struct size_impl<stack_tag>
74  {
75  template <class Seq>
76  struct apply : stack_size<Seq> {};
77  };
78 }
79 
81 template <class Seq>
82 struct deref<stack_iterator<Seq> >
83 {
84  typedef typename Seq::head type;
85 };
86 
87 namespace internal
88 {
89 template <>
90 struct begin_impl<stack_tag>
91 {
92  template <class Stack>
93  struct apply : stack_iterator<Stack> {};
94 };
95 
96 template <>
97 struct end_impl<stack_tag>
98 {
99  template <class Stack>
100  struct apply : stack_iterator<empty_stack> {};
101 };
102 
103 template <>
104 struct clear_impl<stack_tag>
105 {
106  template <class Seq>
107  struct apply : empty_stack {};
108 };
109 
110 template <>
111 struct push_front_impl<stack_tag>
112 {
113  template <class Stack, class T>
114  struct apply : internal::stack_impl<T, Stack> {};
115 };
116 
117 template <>
118 struct pop_front_impl<stack_tag>
119 {
120  template <class Stack>
121  struct apply : Stack::tail {};
122 };
123 } // namespace internal
124 } // namespace tmp
125 
126 #endif
127 
tmp::integer
integer type representation
Definition: integer.hpp:54
tmp::stack_iterator
the stack iterator type used by tmp::begin, tmp::end and tmp::deref
Definition: stack.hpp:54
tmp::deref
metafunction to dereference an iterator
Definition: sequence.hpp:114
tmp
template metaprogramming functions
Definition: asymptotic_types.hpp:101
integer.hpp
integer type representation and basic integer arithmetics
tmp::apply
The apply metafunction shortcut for lambda evaluation.
Definition: lambda.hpp:239
tmp::empty_stack
internal::stack_impl< none, none > empty_stack
an empty stack
Definition: stack.hpp:50
sequence.hpp
implementation of compile time sequences
tmp::stack_iterator::type
stack_iterator type
self-returning matafunction
Definition: stack.hpp:57
tmp::next
increment operator
Definition: operator.hpp:35