mirror of
https://github.com/Naios/continuable.git
synced 2025-12-07 01:06:44 +08:00
112 lines
2.7 KiB
C++
112 lines
2.7 KiB
C++
|
|
/**
|
|
* Copyright 2015-2016 Denis Blank <denis.blank@outlook.com>
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include <tuple>
|
|
#include <type_traits>
|
|
#include <string>
|
|
|
|
namespace detail {
|
|
template<typename... T>
|
|
struct Identity { };
|
|
|
|
template<typename T, typename... Rest>
|
|
struct IdentityInheritenceWrapper
|
|
: Identity<T>, IdentityInheritenceWrapper<Rest...> { };
|
|
|
|
enum class NamedParameterId {
|
|
NAMED_PARAMATER_CALLBACK,
|
|
NAMED_PARAMATER_REJECTOR
|
|
};
|
|
|
|
template <NamedParameterId Value, typename T>
|
|
struct NamedParameter
|
|
: std::integral_constant<NamedParameterId, Value>,
|
|
std::common_type<T> { };
|
|
|
|
template <NamedParameterId Value, typename Default, typename... Args>
|
|
using GetNamedParameterOrDefault = void;
|
|
|
|
template<typename, typename>
|
|
class ContinuableBase;
|
|
|
|
template<typename T>
|
|
struct ReturnTypeToContinuableConverter;
|
|
|
|
template<typename... Args>
|
|
struct
|
|
|
|
template<typename... Args, typename CallbackType>
|
|
class ContinuableBase<Identity<Args...>, typename CallbackType> {
|
|
CallbackType callback_;
|
|
|
|
public:
|
|
explicit ContinuableBase(CallbackType&& callback)
|
|
: callback_(std::move(callback)) { }
|
|
|
|
template<typename C>
|
|
auto then(C&& continuation) {
|
|
// The type the callback will evaluate to
|
|
using EvaluatedTo = decltype(std::declval<C>()(std::declval<Args>()...));
|
|
|
|
|
|
return EvaluatedTo{ };
|
|
}
|
|
};
|
|
} // namespace detail
|
|
|
|
using namespace detail;
|
|
|
|
template<typename... Args>
|
|
using Continuable = detail::ContinuableBase<detail::Identity<Args...>>;
|
|
|
|
template <typename... Args>
|
|
struct Callback {
|
|
void operator() (Args... ) { }
|
|
};
|
|
|
|
template <typename... Args>
|
|
auto make_continuable(Args&&...) {
|
|
return Continuable<> { };
|
|
}
|
|
|
|
auto http_request(std::string url) {
|
|
return make_continuable([url](auto&& callback) {
|
|
|
|
callback("<br>hi<br>");
|
|
});
|
|
}
|
|
|
|
void testNextGen() {
|
|
using t = GetNamedParameterOrDefault<
|
|
NamedParameterId::NAMED_PARAMATER_CALLBACK,
|
|
Identity<>,
|
|
void,
|
|
int,
|
|
NamedParameter<NamedParameterId::NAMED_PARAMATER_CALLBACK, std::string>,
|
|
float
|
|
>;
|
|
|
|
http_request("github.com")
|
|
.then([](std::string content) {
|
|
|
|
return
|
|
})
|
|
.then([] {
|
|
|
|
});
|
|
}
|