mirror of
https://github.com/Naios/continuable.git
synced 2025-12-07 09:16:46 +08:00
fix the template approach
This commit is contained in:
parent
edf494518f
commit
9c30f38f52
131
NextGen.cpp
131
NextGen.cpp
@ -46,66 +46,155 @@ 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...>, typename CallbackType> {
|
class ContinuableBase<Identity<Args...>, CallbackType> {
|
||||||
CallbackType callback_;
|
CallbackType callback_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ContinuableBase(CallbackType&& callback)
|
explicit ContinuableBase(CallbackType&& callback)
|
||||||
: callback_(std::move(callback)) { }
|
: callback_(std::move(callback)) { }
|
||||||
|
|
||||||
template<typename C>
|
void via() { }
|
||||||
|
|
||||||
|
/*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
|
||||||
|
|
||||||
using namespace detail;
|
using namespace detail;
|
||||||
|
|
||||||
template<typename... Args>
|
// template<typename... Args>
|
||||||
using Continuable = detail::ContinuableBase<detail::Identity<Args...>>;
|
// using Continuable = detail::ContinuableBase<detail::Identity<Args...>>;
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
struct Callback {
|
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>
|
||||||
|
auto appendHandlerToContinuation(Continuation&& cont, Handler&& handler) {
|
||||||
|
return [cont = std::forward<Continuation>(cont),
|
||||||
|
handler = std::forward<Handler>(handler)](auto&& continuation) {
|
||||||
|
using T = decltype(continuation);
|
||||||
|
return [continuation = std::forward<T>(continuation)](auto&&... arg) {
|
||||||
|
continuation(std::forward<decltype(arg)>(arg)...);
|
||||||
|
};
|
||||||
|
|
||||||
|
current([continuation = std::forward<T>(continuation)](auto&&... arg) {
|
||||||
|
continuation(std::forward<decltype(arg)>(arg)...);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
} */
|
||||||
|
|
||||||
|
template<typename Continuation, typename Callback>
|
||||||
|
auto appendHandlerToContinuation(Continuation&& continuation,
|
||||||
|
Callback&& callback) {
|
||||||
|
return [continuation = std::forward<Continuation>(continuation),
|
||||||
|
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
|
||||||
|
std::move(continuation)(std::move(proxy));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Continuation>
|
||||||
|
void invokeContinuation(Continuation&& continuation) {
|
||||||
|
// Pass an empty callback to the continuation to invoke it
|
||||||
|
std::forward<Continuation>(continuation)([](auto&&...) {});
|
||||||
|
}
|
||||||
|
|
||||||
|
auto makeTestContinuation() {
|
||||||
|
return [](auto&& callback) {
|
||||||
|
callback("<br>hi<br>");
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void testNextGen() {
|
void testNextGen() {
|
||||||
using t = GetNamedParameterOrDefault<
|
auto continuation = makeTestContinuation();
|
||||||
NamedParameterId::NAMED_PARAMATER_CALLBACK,
|
|
||||||
Identity<>,
|
|
||||||
void,
|
|
||||||
int,
|
|
||||||
NamedParameter<NamedParameterId::NAMED_PARAMATER_CALLBACK, std::string>,
|
|
||||||
float
|
|
||||||
>;
|
|
||||||
|
|
||||||
http_request("github.com")
|
auto then1 = [](std::string) {
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
return makeTestContinuation();
|
||||||
|
};
|
||||||
|
|
||||||
|
auto then2 = [](std::string) {
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
return makeTestContinuation();
|
||||||
|
};
|
||||||
|
|
||||||
|
auto f1 = appendHandlerToContinuation(continuation, then1);
|
||||||
|
auto f2 = appendHandlerToContinuation(f1, then2);
|
||||||
|
|
||||||
|
invokeContinuation(f2);
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*http_request("github.com")
|
||||||
.then([](std::string content) {
|
.then([](std::string content) {
|
||||||
|
|
||||||
return
|
|
||||||
})
|
})
|
||||||
.then([] {
|
.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));
|
||||||
}
|
}
|
||||||
|
|||||||
4
Test.cpp
4
Test.cpp
@ -21,11 +21,15 @@
|
|||||||
|
|
||||||
#include "Continuable.h"
|
#include "Continuable.h"
|
||||||
|
|
||||||
|
void testNextGen();
|
||||||
void test_mockup();
|
void test_mockup();
|
||||||
void test_incubator();
|
void test_incubator();
|
||||||
|
|
||||||
int main(int argc, char* const argv[])
|
int main(int argc, char* const argv[])
|
||||||
{
|
{
|
||||||
|
testNextGen();
|
||||||
|
return 0;
|
||||||
|
|
||||||
test_mockup();
|
test_mockup();
|
||||||
|
|
||||||
test_incubator();
|
test_incubator();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user