Skip to content

Commit 609d2e6

Browse files
committed
Updating index
1 parent 9a661c9 commit 609d2e6

File tree

7 files changed

+721
-0
lines changed

7 files changed

+721
-0
lines changed
Binary file not shown.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <iostream>
2+
3+
class AsyncOperand
4+
{
5+
6+
};
7+
8+
class CompleteOperand
9+
{
10+
11+
};
12+
13+
class AsyncBuilder
14+
{
15+
public:
16+
template<typename TLambda>
17+
void operator&(TLambda l)
18+
{
19+
std::cout << "Async with lambda\n";
20+
}
21+
22+
void operator&(AsyncOperand operand)
23+
{
24+
std::cout << "Async with AsyncOperand\n";
25+
}
26+
};
27+
28+
class CompleteBuilder
29+
{
30+
public:
31+
template<typename TLambda>
32+
CompleteOperand operator*(TLambda complete)
33+
{
34+
std::cout << "Complete with lambda\n";
35+
return { };
36+
}
37+
};
38+
39+
template<typename TLambda>
40+
AsyncOperand operator>>(TLambda l, CompleteOperand c)
41+
{
42+
std::cout << "Combining CompleteOperand and async lambda to AsyncOperand\n";
43+
return { };
44+
}
45+
46+
#define tfc_async AsyncBuilder() & [] ()
47+
#define tfc_async_complete >> CompleteBuilder() * []
48+
49+
int main()
50+
{
51+
std::cout << "Hello" << std::endl;
52+
53+
AsyncBuilder() & [] () { } >> CompleteBuilder() * [] () { };
54+
55+
tfc_async
56+
{
57+
58+
}
59+
tfc_async_complete
60+
{
61+
62+
};
63+
64+
std::cout << "Done" << std::endl;
65+
//std::getchar();
66+
return 0;
67+
}
68+
69+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
26+
template<typename TLambda, typename TCompleteLambda>
27+
struct AsyncOperand
28+
{
29+
TLambda lambda;
30+
TCompleteLambda completeLambda;
31+
};
32+
33+
template<typename TCompleteLambda>
34+
struct CompleteOperand
35+
{
36+
TCompleteLambda lambda;
37+
};
38+
39+
class AsyncBuilder
40+
{
41+
public:
42+
template<typename TLambda>
43+
void operator&(TLambda l)
44+
{
45+
std::cout << "Async with lambda\n";
46+
}
47+
48+
template<typename TLambda, typename TCompleteLambda>
49+
void operator&(AsyncOperand<TLambda, TCompleteLambda> operand)
50+
{
51+
std::cout << "Async with AsyncOperand\n";
52+
}
53+
};
54+
55+
class CompleteBuilder
56+
{
57+
public:
58+
template<typename TCompleteLambda>
59+
CompleteOperand<TCompleteLambda> operator*(TCompleteLambda complete)
60+
{
61+
std::cout << "Complete with lambda\n";
62+
return { complete };
63+
}
64+
};
65+
66+
template<typename TLambda, typename TCompleteLambda>
67+
AsyncOperand<TLambda, TCompleteLambda> operator>>(TLambda l, CompleteOperand<TCompleteLambda> c)
68+
{
69+
std::cout << "Combining CompleteOperand and async lambda to AsyncOperand\n";
70+
return { l, c.lambda };
71+
}
72+
73+
74+
#define tfc_async AsyncBuilder() & [] ()
75+
#define tfc_async_complete >> CompleteBuilder() * []
76+
77+
int main()
78+
{
79+
std::cout << "Hello" << std::endl;
80+
81+
tfc_async
82+
{
83+
84+
}
85+
tfc_async_complete
86+
{
87+
88+
};
89+
90+
std::cout << "Done" << std::endl;
91+
92+
return 0;
93+
}
94+
95+
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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

Comments
 (0)