summaryrefslogtreecommitdiff
path: root/src/mbgl/util/literal.hpp
blob: d4fec49b35f4f15efda9cc30d1f7886acf4c8e4d (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
#pragma once

#include <utility>
#include <cstddef>

namespace mbgl {

// Using structs instead of constexpr templates because GCC 4.9 doesn't support these yet.

template <char... Cs>
struct string_literal {
    static constexpr const char chars[] = { Cs..., 0 };

    static constexpr const char* value() {
        return chars;
    }
};

template <char... Cs>
constexpr const char string_literal<Cs...>::chars[];

namespace detail {

using string_fn = const char* (*)();

template <string_fn name, std::size_t... Is>
string_literal<name()[Is]...> to_string_literal(std::index_sequence<Is...>);

constexpr std::size_t string_length(const char* const str, std::size_t len = 0) {
    return str[0] ? string_length(str + 1, len + 1) : len;
}

template <class...>
struct concat_literals;

template <>
struct concat_literals<> {
    static constexpr auto value() {
        return "";
    }
};

template <char... As, char... Bs, class... Rest>
struct concat_literals<string_literal<As...>, string_literal<Bs...>, Rest...> {
    static constexpr auto value() {
        return concat_literals<string_literal<As..., Bs...>, Rest...>::value();
    }
};

template <char... Cs>
struct concat_literals<string_literal<Cs...>> {
    static constexpr auto value() {
        return string_literal<Cs...>::value();
    }
};

} // namespace detail

template <detail::string_fn... str>
struct concat_literals {
    static constexpr auto value() {
        return detail::concat_literals<decltype(detail::to_string_literal<str>(
            std::make_index_sequence<detail::string_length(str())>()))...>::value();
    }
};

} // namespace mbgl