mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 16:56:44 +08:00
Fix decorators
This commit is contained in:
parent
9c30f38f52
commit
98f335690f
127
NextGen.cpp
127
NextGen.cpp
@ -14,7 +14,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
/*
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -46,9 +46,9 @@ namespace detail {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct ReturnTypeToContinuableConverter;
|
struct ReturnTypeToContinuableConverter;
|
||||||
|
|
||||||
/*template<typename... Args>
|
template<typename... Args>
|
||||||
struct
|
struct
|
||||||
*/
|
|
||||||
|
|
||||||
template<typename... Args, typename CallbackType>
|
template<typename... Args, typename CallbackType>
|
||||||
class ContinuableBase<Identity<Args...>, CallbackType> {
|
class ContinuableBase<Identity<Args...>, CallbackType> {
|
||||||
@ -60,14 +60,14 @@ namespace detail {
|
|||||||
|
|
||||||
void via() { }
|
void via() { }
|
||||||
|
|
||||||
/*template<typename C>
|
template<typename C>
|
||||||
auto then(C&& continuation) {
|
auto then(C&& continuation) {
|
||||||
// The type the callback will evaluate to
|
// The type the callback will evaluate to
|
||||||
using EvaluatedTo = decltype(std::declval<C>()(std::declval<Args>()...));
|
using EvaluatedTo = decltype(std::declval<C>()(std::declval<Args>()...));
|
||||||
|
|
||||||
|
|
||||||
return EvaluatedTo{ };
|
return EvaluatedTo{ };
|
||||||
}*/
|
}
|
||||||
};
|
};
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
@ -81,19 +81,19 @@ struct Callback {
|
|||||||
void operator() (Args... ) { }
|
void operator() (Args... ) { }
|
||||||
};
|
};
|
||||||
|
|
||||||
/*template <typename... Args>
|
template <typename... Args>
|
||||||
auto make_continuable(Args&&...) {
|
auto make_continuable(Args&&...) {
|
||||||
return Continuable<> { };
|
return Continuable<> { };
|
||||||
}*/
|
}
|
||||||
|
|
||||||
/*auto http_request(std::string url) {
|
auto http_request(std::string url) {
|
||||||
return make_continuable([url](auto& callback) {
|
return make_continuable([url](auto& callback) {
|
||||||
|
|
||||||
callback("<br>hi<br>");
|
callback("<br>hi<br>");
|
||||||
});
|
});
|
||||||
}*/
|
}
|
||||||
|
|
||||||
/* template<typename Continuation, typename Handler>
|
template<typename Continuation, typename Handler>
|
||||||
auto appendHandlerToContinuation(Continuation&& cont, Handler&& handler) {
|
auto appendHandlerToContinuation(Continuation&& cont, Handler&& handler) {
|
||||||
return [cont = std::forward<Continuation>(cont),
|
return [cont = std::forward<Continuation>(cont),
|
||||||
handler = std::forward<Handler>(handler)](auto&& continuation) {
|
handler = std::forward<Handler>(handler)](auto&& continuation) {
|
||||||
@ -108,28 +108,68 @@ auto appendHandlerToContinuation(Continuation&& cont, Handler&& handler) {
|
|||||||
};
|
};
|
||||||
} */
|
} */
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
auto createEmptyContinuation() {
|
||||||
|
return [](auto&& callback) { callback(); };
|
||||||
|
}
|
||||||
|
|
||||||
|
auto createEmptyCallback() {
|
||||||
|
return [](auto&&...) { };
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Result>
|
||||||
|
struct CallbackResultDecorator {
|
||||||
|
template<typename Callback>
|
||||||
|
static auto decorate(Callback&& callback) {
|
||||||
|
return std::forward<Callback>(callback);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct CallbackResultDecorator<void> {
|
||||||
|
template<typename Callback>
|
||||||
|
static auto decorate(Callback&& callback) {
|
||||||
|
return [callback = std::forward<Callback>(callback)](auto&&... args) {
|
||||||
|
callback(std::forward<decltype(args)>(args)...);
|
||||||
|
return createEmptyContinuation();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create the proxy callback that is responsible for invoking
|
||||||
|
// the real callback and passing the next continuation into
|
||||||
|
// to the result of the callback.
|
||||||
|
template<typename Callback, typename Next>
|
||||||
|
auto createProxyCallback(Callback&& callback,
|
||||||
|
Next&& next) {
|
||||||
|
return [callback = std::forward<Callback>(callback),
|
||||||
|
next = std::forward<Next>(next)] (auto&&... args) mutable {
|
||||||
|
using Result = decltype(callback(std::forward<decltype(args)>(args)...));
|
||||||
|
using Decorator = CallbackResultDecorator<Result>;
|
||||||
|
Decorator::decorate(std::move(callback))
|
||||||
|
(std::forward<decltype(args)>(args)...)(std::move(next));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Continuation, typename Callback>
|
template<typename Continuation, typename Callback>
|
||||||
auto appendHandlerToContinuation(Continuation&& continuation,
|
auto appendHandlerToContinuation(Continuation&& continuation,
|
||||||
Callback&& callback) {
|
Callback&& callback) {
|
||||||
|
|
||||||
return [continuation = std::forward<Continuation>(continuation),
|
return [continuation = std::forward<Continuation>(continuation),
|
||||||
callback = std::forward<Callback>(callback)](auto&& next) mutable {
|
callback = std::forward<Callback>(callback)](auto&& next) mutable {
|
||||||
|
|
||||||
// Create the proxy callback that passes the next continuation into
|
|
||||||
// the callback.
|
|
||||||
auto proxy = [callback = std::forward<Callback>(callback),
|
|
||||||
next = std::forward<decltype(next)>(next)]
|
|
||||||
(auto&&... args) mutable {
|
|
||||||
callback(std::forward<decltype(args)>(args)...)(std::move(next));
|
|
||||||
};
|
|
||||||
// Invoke the next invocation handler
|
// Invoke the next invocation handler
|
||||||
std::move(continuation)(std::move(proxy));
|
std::move(continuation)(createProxyCallback(
|
||||||
|
std::move(callback), std::forward<decltype(next)>(next)));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Continuation>
|
template<typename Continuation>
|
||||||
void invokeContinuation(Continuation&& continuation) {
|
void invokeContinuation(Continuation&& continuation) {
|
||||||
// Pass an empty callback to the continuation to invoke it
|
// Pass an empty callback to the continuation to invoke it
|
||||||
std::forward<Continuation>(continuation)([](auto&&...) {});
|
std::forward<Continuation>(continuation)(createEmptyCallback());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto makeTestContinuation() {
|
auto makeTestContinuation() {
|
||||||
@ -138,63 +178,20 @@ auto makeTestContinuation() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void testNextGen() {
|
int main(int, char**) {
|
||||||
auto continuation = makeTestContinuation();
|
auto continuation = makeTestContinuation();
|
||||||
|
|
||||||
auto then1 = [](std::string) {
|
auto then1 = [](std::string) {
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
return makeTestContinuation();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
auto then2 = [](std::string) {
|
auto then2 = []() {
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
return makeTestContinuation();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
auto f1 = appendHandlerToContinuation(continuation, then1);
|
auto f1 = appendHandlerToContinuation(continuation, then1);
|
||||||
auto f2 = appendHandlerToContinuation(f1, then2);
|
auto f2 = appendHandlerToContinuation(f1, then2);
|
||||||
|
|
||||||
invokeContinuation(f2);
|
invokeContinuation(f2);
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
/*http_request("github.com")
|
|
||||||
.then([](std::string content) {
|
|
||||||
|
|
||||||
})
|
|
||||||
.then([] {
|
|
||||||
|
|
||||||
});*/
|
|
||||||
|
|
||||||
auto c1 = [](auto&& callback) {
|
|
||||||
|
|
||||||
callback("");
|
|
||||||
};
|
|
||||||
|
|
||||||
auto r1 = [](auto&& continuation) {
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
auto u1 = [](std::string str) {
|
|
||||||
// do sth
|
|
||||||
(void)str;
|
|
||||||
|
|
||||||
return [](auto&& callback) { // next
|
|
||||||
callback(0);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
auto proxy = [u1 = std::move(u1)](auto&& tunnel) {
|
|
||||||
|
|
||||||
auto c2 = u1(tunnel);
|
|
||||||
|
|
||||||
|
|
||||||
// c2()
|
|
||||||
};
|
|
||||||
|
|
||||||
c1(std::move(proxy));
|
|
||||||
}
|
}
|
||||||
|
|||||||
6
Test.cpp
6
Test.cpp
@ -21,13 +21,13 @@
|
|||||||
|
|
||||||
#include "Continuable.h"
|
#include "Continuable.h"
|
||||||
|
|
||||||
void testNextGen();
|
// void testNextGen();
|
||||||
void test_mockup();
|
void test_mockup();
|
||||||
void test_incubator();
|
void test_incubator();
|
||||||
|
|
||||||
int main(int argc, char* const argv[])
|
int tttt(int argc, char* const argv[])
|
||||||
{
|
{
|
||||||
testNextGen();
|
// testNextGen();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
test_mockup();
|
test_mockup();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user