summaryrefslogtreecommitdiffhomepage
path: root/compat/meta.hpp
blob: 11c5ca0fd6599820b153346f3af6df627ba99cd0 (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
#pragma once

/* Copyright (c) 2017 Stanislaw Halik
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

#define OTR_META_INST_FAIL(x)                                        \
    static_assert(sizeof...(x) == ~0ul);                             \
    static_assert(sizeof...(x) == 0u)

namespace meta::detail {

    template<typename... xs>
    struct tuple;

    template<typename... xs>
    struct reverse_
    {
        OTR_META_INST_FAIL(xs);
    };

    template<typename x0, typename... xs, template<typename...> class x, typename... ys>
    struct reverse_<x<x0, xs...>, x<ys...>>
    {
        using type = typename reverse_<x<xs...>, x<x0, ys...>>::type;
    };

    template<template<typename...> class x, typename... ys>
    struct reverse_<x<>, x<ys...>>
    {
        using type = x<ys...>;
    };

    template<template<typename...> class inst, typename... xs>
    struct lift_
    {
        OTR_META_INST_FAIL(xs);
    };

    template<template<typename...> class to, template<typename...> class from, typename... xs>
    struct lift_<to, from<xs...>>
    {
        using type = to<xs...>;
    };
} // ns meta::detail

namespace meta {
    template<typename... xs>
    using reverse = typename detail::reverse_<detail::tuple<xs...>, detail::tuple<>>::type;

    // the to/from order is awkward but mimics function composition
    template<template<typename...> class to, typename from>
    using lift = typename detail::lift_<to, from>::type;

    template<typename x, typename... xs>
    using first = x;

    template<typename x, typename... xs>
    using rest = detail::tuple<xs...>;

    template<typename... xs>
    using butlast = reverse<rest<reverse<xs...>>>;

    template<typename... xs>
    using last = lift<first, reverse<xs...>>;
} // ns meta