-
Notifications
You must be signed in to change notification settings - Fork 67
/
integer.hpp
44 lines (36 loc) · 1.11 KB
/
integer.hpp
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
// Copyright (C) 2020-2024 Jonathan Müller and lexy contributors
// SPDX-License-Identifier: BSL-1.0
#ifndef LEXY_CALLBACK_INTEGER_HPP_INCLUDED
#define LEXY_CALLBACK_INTEGER_HPP_INCLUDED
#include <lexy/callback/base.hpp>
#include <lexy/dsl/sign.hpp>
namespace lexy
{
template <typename T>
struct _int
{
using return_type = T;
// You don't actually produce an integer value.
constexpr T operator()(lexy::plus_sign) const = delete;
constexpr T operator()(lexy::minus_sign) const = delete;
template <typename Integer>
constexpr T operator()(const Integer& value) const
{
return T(value);
}
template <typename Integer>
constexpr T operator()(lexy::plus_sign, const Integer& value) const
{
return T(value);
}
template <typename Integer>
constexpr T operator()(lexy::minus_sign, const Integer& value) const
{
return T(-value);
}
};
// A callback that takes an optional sign and an integer and produces the signed integer.
template <typename T>
constexpr auto as_integer = _int<T>{};
} // namespace lexy
#endif // LEXY_CALLBACK_INTEGER_HPP_INCLUDED