summaryrefslogtreecommitdiffstats
path: root/include/Templates.h
blob: 6a225458eff309af17dcaf85bdc5783b8af76c37 (plain) (blame)
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
/**
 * Copyright (c) 2026 Alexey Gavrilov <alexey.gavrilov@mail.com>
 *
 * This file is part of ObjectiveOS.
 *
 * ObjectiveOS is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * ObjectiveOS is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * ObjectiveOS. If not, see <https://www.gnu.org/licenses/>. 
 */

#ifndef TEMPLATES_H
#define TEMPLATES_H


#include <Types.h>

template<typename T, T val>
struct constant
{
    static constexpr T value = val;
    T& operator()() {return value;}
};

template<typename T, typename U>
struct isSame
{
    using cnst = constant<bool, false>;
    bool static inline constexpr val(){return cnst::value;};
    constexpr operator bool() const {return cnst::value;}
    bool constexpr operator()() {return cnst::value;}
};

template<typename T>
struct isSame<T, T>
{
    using cnst = constant<bool, true>;
    constexpr operator bool() const {return cnst::value;}
    bool static inline constexpr val(){return cnst::value;};
    bool constexpr operator()() {return cnst::value;}
};

template <typename ... params>
struct parametersOperations
{
    template<typename Head, typename ... Tail>
    using first = Head;

    template<typename Head, typename ... Tail>
    using tail = parametersOperations<Tail ...>;

    // Append operation
    template<typename Head, typename ... Tail>
    struct append
    {
        using result = parametersOperations<params ..., Head>::template append<Tail ...>::result;
    };

    template<template <typename ... NestedList> typename NestedType, typename ... NestedList, typename ... OuterList>
    requires(isSame<NestedType<>, parametersOperations<>>::val())
    struct append<NestedType<NestedList ...>, OuterList ...>
    {
        using result = parametersOperations<params..., NestedList ...>::template append<OuterList ...>::result;
    };

    template<template <typename ... NestedList> typename NestedType, typename ... NestedList>
    requires(isSame<NestedType<>, parametersOperations<>>::val())
    struct append<NestedType<NestedList ...>>
    {
        using result = parametersOperations<params..., NestedList ...>;
    };

    template<typename One>
    struct append<One> {using result = parametersOperations<params ..., One>;};
};

template< unsigned I, typename T>
struct cut
{
    using process = cut<I - 1, typename T::baseType>::process;
};

template<typename T>
struct cut<0, T>
{
    using process = cut<0, T>;
    using result = T;
};

template<uint16 N, typename Head, typename ... Args>
struct reper
{
    using toList = typename reper<N-1, Head, Head, Args ...>::toList;
};

template<uint16 N, typename Type>
struct repeat
{
    using toList = typename reper<N-1, Type>::toList;
};

template<typename Type>
struct repeat<0, Type> {};

template<typename Head, typename ... Args>
struct reper<0, Head, Args ...>
{
    using toList = parametersOperations<Head, Args ...>;
};

template<typename Root, typename toType>
struct switchRoot {using result = toType;};

template<template <typename ... RootList> typename RootType, template <typename ... toTypeListArgs> typename toType, typename ... RootList, typename ... toTypeListArgs>
struct switchRoot<RootType<RootList ...>, toType<toTypeListArgs ...>>{using result = toType<RootList ...>;};

template<typename arg, template<typename ... toTypeListArgs> typename toType, typename ... toTypeListArgs>
struct switchRoot<arg, toType<toTypeListArgs ...>>{using result = toType<arg>;};

template<typename arg, typename ... args>
struct parameters
{
    public:
        using baseType = parameters<args ...>;
        using types = parametersOperations<>::template append<arg, typename baseType::types>::result;
        template<template <typename ... > typename T>
        using submitTo = switchRoot<types, T<>>::result;
};

template<typename arg>
struct parameters<arg>
{
    public:
        using types = arg;
        template<template <typename ... > typename T>
        using submitTo = switchRoot<types, T<>>::result;
};
#endif