summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx/grammar/counter.hpp
blob: ad051ea8a34d154ef1270342865c151ac0dc1603 (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
#ifndef EOLIAN_CXX_COUNTER_HH_HH
#define EOLIAN_CXX_COUNTER_HH_HH

#include <cstdlib>
#include <cstring>

#include "generator.hpp"
#include "attributes.hpp"
#include "case.hpp"
#include "integral.hpp"

namespace efl { namespace eolian { namespace grammar {

namespace detail {

}

struct counter_generator
{
   std::shared_ptr<std::size_t> count;

   template <typename OutputIterator, typename Attribute, typename Context>
   bool generate(OutputIterator sink, Attribute const&, Context const&) const
   {
      detail::generate_integral(sink, *count);
      ++*count;
      return true;
   }
};

struct counter_terminal
{
  counter_generator operator()(std::size_t initial) const
  {
    return {std::shared_ptr<std::size_t>{new std::size_t{initial}}};
  }
} const counter = {};

counter_generator as_generator(counter_terminal) { return {std::shared_ptr<std::size_t>{new std::size_t{0u}}}; }

template <>
struct is_eager_generator<counter_generator> : std::true_type {};
template <>
struct is_eager_generator<counter_terminal> : std::true_type {};
template <>
struct is_generator<counter_terminal> : std::true_type {};
template <>
struct is_generator<counter_generator> : std::true_type {};

namespace type_traits {
template <>
struct attributes_needed<counter_generator> : std::integral_constant<int, 0> {};
template <>
struct attributes_needed<counter_terminal> : std::integral_constant<int, 0> {};
}

} } }

#endif