|
| 1 | +/* MIT License |
| 2 | + * |
| 3 | + * Copyright (c) 2018 Gilang Mentari Hamidy ([email protected]) |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +#include <iostream> |
| 25 | +#include <tuple> |
| 26 | +#include <type_traits> |
| 27 | + |
| 28 | +// Metaprogram to detect if a type has a call operator overload |
| 29 | +template<typename T> |
| 30 | +class HasCallOperator |
| 31 | +{ |
| 32 | + typedef char Correct; |
| 33 | + typedef long Incorrect; |
| 34 | + template<typename TTest> static Correct Test(decltype(&TTest::operator())); |
| 35 | + template<typename TTest> static Incorrect Test(...); |
| 36 | +public: |
| 37 | + static constexpr bool Value = sizeof(Test<T>(0)) == sizeof(Correct); |
| 38 | +}; |
| 39 | + |
| 40 | +// Metaprogram to detect if a type is a member function pointer |
| 41 | +template<typename TFuncType> |
| 42 | +struct MemberFunction { }; |
| 43 | + |
| 44 | +template<typename TClass, typename TReturn, typename... TArgs> |
| 45 | +struct MemberFunction<TReturn (TClass::*)(TArgs...)> |
| 46 | +{ |
| 47 | + // Arity is the total number of parameter |
| 48 | + static constexpr auto Arity = sizeof...(TArgs); |
| 49 | + |
| 50 | + typedef TReturn ReturnType; |
| 51 | + typedef TClass DeclaringType; |
| 52 | + |
| 53 | + // Args will be used to obtain the parameter type of specified index |
| 54 | + template<size_t idx> |
| 55 | + using Args = typename std::tuple_element<idx, std::tuple<TArgs...>>::type; |
| 56 | + |
| 57 | + typedef std::tuple<TArgs...> ArgsTuple; |
| 58 | + typedef std::tuple<typename std::decay<TArgs>::type...> ArgsTupleDecay; |
| 59 | +}; |
| 60 | + |
| 61 | +template<typename TClass, typename TReturn> |
| 62 | +struct MemberFunction<TReturn (TClass::*)()> |
| 63 | +{ |
| 64 | +// Arity is the total number of parameter |
| 65 | + static constexpr auto Arity = 0; |
| 66 | + |
| 67 | + typedef TReturn ReturnType; |
| 68 | + typedef TClass DeclaringType; |
| 69 | + |
| 70 | + // Args will be used to obtain the parameter type of specified index |
| 71 | + template<size_t idx> |
| 72 | + using Args = void; |
| 73 | +}; |
| 74 | + |
| 75 | +template<typename TClass, typename TReturn, typename... TArgs> |
| 76 | +struct MemberFunction<TReturn (TClass::*)(TArgs...) const> : |
| 77 | + MemberFunction<TReturn (TClass::*)(TArgs...)> { }; |
| 78 | + |
| 79 | +// Metaprogram to detect if a type is a callable object |
| 80 | +// Rule: If a type has a a call operator, then it is considered a callable object |
| 81 | +template<typename T, bool = HasCallOperator<T>::Value> |
| 82 | +struct CallableObject; |
| 83 | + |
| 84 | +template<typename T> |
| 85 | +struct CallableObject<T, false> |
| 86 | +{ |
| 87 | + static constexpr bool Callable = false; |
| 88 | +}; |
| 89 | + |
| 90 | +template<typename T> |
| 91 | +struct CallableObject<T, true> : MemberFunction<decltype(&T::operator())> |
| 92 | +{ |
| 93 | + static constexpr bool Callable = true; |
| 94 | +}; |
| 95 | + |
| 96 | + |
| 97 | +// EDSL Definitions |
| 98 | + |
| 99 | +template<typename TLambda, typename TCompleteLambda> |
| 100 | +struct AsyncOperand |
| 101 | +{ |
| 102 | + using IntrospectLambda = CallableObject<TLambda>; // <--- Include the introspect metaprogram here |
| 103 | + using IntrospectCompleteLambda = CallableObject<TCompleteLambda>; |
| 104 | + |
| 105 | + // Validation rules in the class definition |
| 106 | + static constexpr bool validation = (std::is_same_v<typename IntrospectLambda::ReturnType, void> && IntrospectCompleteLambda::Arity == 0) |
| 107 | + || std::is_same_v<typename IntrospectLambda::ReturnType, typename IntrospectCompleteLambda::template Args<0>>; |
| 108 | + |
| 109 | + static_assert(validation, "The asynchronous return value and complete lambda has different parameter"); |
| 110 | + |
| 111 | + TLambda lambda; |
| 112 | + TCompleteLambda completeLambda; |
| 113 | +}; |
| 114 | + |
| 115 | +template<typename TCompleteLambda> |
| 116 | +struct CompleteOperand |
| 117 | +{ |
| 118 | + using Introspect = CallableObject<TCompleteLambda>; // <--- Include the introspect metaprogram here |
| 119 | + |
| 120 | + // Validation rules in the class definition |
| 121 | + static_assert(Introspect::Arity == 1 || Introspect::Arity == 0, "Incorrect amount of parameter in complete lambda"); |
| 122 | + static_assert(std::is_same_v<typename Introspect::ReturnType, void>, "The complete lambda cannot return any value"); |
| 123 | + |
| 124 | + TCompleteLambda lambda; |
| 125 | +}; |
| 126 | + |
| 127 | +class AsyncBuilder |
| 128 | +{ |
| 129 | +public: |
| 130 | + template<typename TLambda> |
| 131 | + void operator&(TLambda l) |
| 132 | + { |
| 133 | + std::cout << "Async with lambda\n"; |
| 134 | + } |
| 135 | + |
| 136 | + template<typename TLambda, typename TCompleteLambda> |
| 137 | + void operator&(AsyncOperand<TLambda, TCompleteLambda> operand) |
| 138 | + { |
| 139 | + std::cout << "Async with AsyncOperand\n"; |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +class CompleteBuilder |
| 144 | +{ |
| 145 | +public: |
| 146 | + template<typename TCompleteLambda> |
| 147 | + CompleteOperand<TCompleteLambda> operator*(TCompleteLambda complete) |
| 148 | + { |
| 149 | + std::cout << "Complete with lambda\n"; |
| 150 | + return { complete }; |
| 151 | + } |
| 152 | +}; |
| 153 | + |
| 154 | +template<typename TLambda, typename TCompleteLambda> |
| 155 | +AsyncOperand<TLambda, TCompleteLambda> operator>>(TLambda l, CompleteOperand<TCompleteLambda> c) |
| 156 | +{ |
| 157 | + std::cout << "Combining CompleteOperand and async lambda to AsyncOperand\n"; |
| 158 | + return { l, c.lambda }; |
| 159 | +} |
| 160 | + |
| 161 | +#define tfc_async AsyncBuilder() & [] () |
| 162 | +#define tfc_async_complete >> CompleteBuilder() * [] |
| 163 | + |
| 164 | +int main() |
| 165 | +{ |
| 166 | + std::cout << "Hello" << std::endl; |
| 167 | + |
| 168 | + tfc_async |
| 169 | + { |
| 170 | + std::cout << "Hello" << std::endl; |
| 171 | + } |
| 172 | + tfc_async_complete |
| 173 | + { |
| 174 | + |
| 175 | + }; |
| 176 | + |
| 177 | + std::cout << "Done" << std::endl; |
| 178 | + |
| 179 | + return 0; |
| 180 | +} |
| 181 | + |
| 182 | + |
0 commit comments