summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx/grammar/attributes.hpp
blob: d33fcf2de7555d5cd08771d636c25a56daa02f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef EOLIAN_CXX_ATTRIBUTES_HH
#define EOLIAN_CXX_ATTRIBUTES_HH

#include <tuple>
#include <type_traits>
#include "grammar/type_traits.hpp"

namespace efl { namespace eolian { namespace grammar {

namespace attributes {

struct unused_type {};
unused_type const unused = {};
        
template <int N, typename Tuple, typename Enable = void>
struct tuple_element;

template <int N, typename Tuple>
struct tuple_element<N, Tuple, typename std::enable_if
                     <type_traits::is_std_tuple<Tuple>::value>::type> : std::tuple_element<N, Tuple>
{};
        
template <int N, typename Tuple>
typename tuple_element<N, typename std::remove_const<Tuple>::type>::type get_adl(Tuple& tuple)
{
  using std::get;
  return get<N>(tuple);
}
  
template <int I, typename Tuple>
struct lazy_offset_tuple
{
   lazy_offset_tuple(Tuple& tuple)
     : tuple(tuple) {}

   template <int N>
   typename tuple_element<N+I, typename std::remove_const<Tuple>::type>::type get() const
   {
      return attributes::get_adl<N+I>(tuple);
   }
  
   Tuple& tuple;
};

}
namespace type_traits {
template <int N, typename T>
struct is_tuple<attributes::lazy_offset_tuple<N, T>> : std::true_type {};
template <int N,  typename T>
struct is_explicit_tuple<attributes::lazy_offset_tuple<N, T>> : std::true_type {};
}
namespace attributes {
  
template <typename T>
struct infinite_tuple
{
  T object;
};

template <typename T>
infinite_tuple<T> make_infinite_tuple(T&& object)
{
  return infinite_tuple<T>{std::forward<T>(object)};
}

template <int N, typename T>
struct tuple_element<N, infinite_tuple<T>>
{
  typedef T type;
  static type& get(infinite_tuple<T>& tuple) { return tuple.object; }
  static type const& get(infinite_tuple<T> const& tuple) { return tuple.object; }
};
template <int N, typename T>
struct tuple_element<N, infinite_tuple<T> const> : tuple_element<N, infinite_tuple<T>> {};
template <int N, typename T>
typename tuple_element<N, infinite_tuple<T>>::type&
get(infinite_tuple<T>& tuple)
{
  return tuple_element<N, infinite_tuple<T>>::get(tuple);
}
template <int N, typename T>
typename tuple_element<N, infinite_tuple<T>>::type const&
get(infinite_tuple<T> const& tuple)
{
  return tuple_element<N, infinite_tuple<T>>::get(tuple);
}
  
}

namespace type_traits {
template <typename T>
struct is_tuple<attributes::infinite_tuple<T>> : std::true_type {};
template <typename T>
struct is_explicit_tuple<attributes::infinite_tuple<T>> : std::true_type {};
}
namespace attributes {

template <int N, int I, typename Tuple>
struct tuple_element<N, lazy_offset_tuple<I, Tuple>> : tuple_element<N+I, typename std::remove_const<Tuple>::type>
{};
template <int N, int I, typename Tuple>
struct tuple_element<N, lazy_offset_tuple<I, Tuple> const> : tuple_element<N+I, typename std::remove_const<Tuple>::type>
{};

template <int N, int I, typename Tuple>
typename tuple_element<N, lazy_offset_tuple<I, Tuple> const>::type
 get(lazy_offset_tuple<I, Tuple> const& tuple)
{
   return tuple.template get<N>();
}
        
template <typename Tuple>
lazy_offset_tuple<1, Tuple> pop_front(Tuple& tuple, typename std::enable_if<type_traits::is_tuple<Tuple>::value>::type* = 0)
{
   return lazy_offset_tuple<1, Tuple>(tuple);
}

template <int N, typename Tuple>
lazy_offset_tuple<N + 1, Tuple> pop_front(lazy_offset_tuple<N, Tuple> tuple, typename std::enable_if<type_traits::is_tuple<Tuple>::value>::type* = 0)
{
   return lazy_offset_tuple<N + 1, Tuple>(tuple.tuple);
}

template <int I, typename Tuple>
lazy_offset_tuple<I, Tuple> pop_front_n(Tuple& tuple, typename std::enable_if<type_traits::is_tuple<Tuple>::value && I>::type* = 0)
{
   return lazy_offset_tuple<I, Tuple>(tuple);
}
template <int I, typename Tuple>
Tuple& pop_front_n(Tuple& tuple, typename std::enable_if<I == 0>::type* = 0)
{
   return tuple;
}

template <int I, int N, typename Tuple>
lazy_offset_tuple<N + I, Tuple> pop_front_n(lazy_offset_tuple<N, Tuple> tuple, typename std::enable_if<type_traits::is_tuple<Tuple>::value>::type* = 0)
{
   return lazy_offset_tuple<N + I, Tuple>(tuple.tuple);
}

template <typename Generator, typename OutputIterator, typename Attribute, typename Context>
bool generate(Generator const& gen, OutputIterator sink, Attribute const& attribute, Context const& context
              , typename std::enable_if
              <type_traits::is_explicit_tuple<Attribute>::value
              && !type_traits::accepts_tuple<Generator>::value
              && type_traits::attributes_needed<Generator>::value != 0
              >::type* = 0)
{
   return gen.generate(sink, get<0>(attribute), context);
}

template <typename Generator, typename OutputIterator, typename Attribute, typename Context>
bool generate(Generator const& gen, OutputIterator sink, Attribute const& attribute, Context const& context
              , typename std::enable_if
              <type_traits::is_explicit_tuple<Attribute>::value
              && type_traits::accepts_tuple<Generator>::value
              && type_traits::attributes_needed<Generator>::value != 0
              >::type* = 0)
{
   return gen.generate(sink, attribute, context);
}
  
template <typename Generator, typename OutputIterator, typename Attribute, typename Context>
bool generate(Generator const& gen, OutputIterator sink, Attribute const&
              , Context const& context
              , typename std::enable_if
              <type_traits::attributes_needed<Generator>::value == 0
              >::type* = 0)
{
   return gen.generate(sink, unused, context);
}

template <typename Generator, typename OutputIterator, typename Attribute, typename Context>
bool generate(Generator const& gen, OutputIterator sink, Attribute const& attribute, Context const& context
              , typename std::enable_if
              <!type_traits::is_explicit_tuple<Attribute>::value
              && type_traits::attributes_needed<Generator>::value != 0
              >::type* = 0)
{
   return gen.generate(sink, attribute, context);
}
  
} } } }

#endif