Commit 5a81b5df authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Move futures helper types into folly::futures::detail

Summary:
[Folly] Move futures helper types into `folly::futures::detail`.

From `folly::detail`, where it would be easier to collide. Especially for names like `Core`.

Reviewed By: WillerZ

Differential Revision: D5460766

fbshipit-source-id: 3f7bff784bbb89c7c86d2f1824323d71321c7ad6
parent 05a686a7
This diff is collapsed.
......@@ -38,6 +38,7 @@ struct isTry : std::false_type {};
template <typename T>
struct isTry<Try<T>> : std::true_type {};
namespace futures {
namespace detail {
template <class> class Core;
......@@ -154,7 +155,8 @@ struct FunctionReferenceToPointer<R (&)(Args...)> {
using type = R (*)(Args...);
};
} // detail
} // namespace detail
} // namespace futures
class Timekeeper;
......
......@@ -210,8 +210,9 @@ class Future {
// replaced with F.
template <
typename F,
typename FF = typename detail::FunctionReferenceToPointer<F>::type,
typename R = detail::callableResult<T, FF>>
typename FF =
typename futures::detail::FunctionReferenceToPointer<F>::type,
typename R = futures::detail::callableResult<T, FF>>
typename R::Return then(F&& func) {
typedef typename R::Arg Arguments;
return thenImplementation<FF, R>(std::forward<FF>(func), Arguments());
......@@ -271,33 +272,33 @@ class Future {
/// });
template <class F>
typename std::enable_if<
!detail::callableWith<F, exception_wrapper>::value &&
!detail::Extract<F>::ReturnsFuture::value,
Future<T>>::type
!futures::detail::callableWith<F, exception_wrapper>::value &&
!futures::detail::Extract<F>::ReturnsFuture::value,
Future<T>>::type
onError(F&& func);
/// Overload of onError where the error callback returns a Future<T>
template <class F>
typename std::enable_if<
!detail::callableWith<F, exception_wrapper>::value &&
detail::Extract<F>::ReturnsFuture::value,
Future<T>>::type
!futures::detail::callableWith<F, exception_wrapper>::value &&
futures::detail::Extract<F>::ReturnsFuture::value,
Future<T>>::type
onError(F&& func);
/// Overload of onError that takes exception_wrapper and returns Future<T>
template <class F>
typename std::enable_if<
detail::callableWith<F, exception_wrapper>::value &&
detail::Extract<F>::ReturnsFuture::value,
Future<T>>::type
futures::detail::callableWith<F, exception_wrapper>::value &&
futures::detail::Extract<F>::ReturnsFuture::value,
Future<T>>::type
onError(F&& func);
/// Overload of onError that takes exception_wrapper and returns T
template <class F>
typename std::enable_if<
detail::callableWith<F, exception_wrapper>::value &&
!detail::Extract<F>::ReturnsFuture::value,
Future<T>>::type
futures::detail::callableWith<F, exception_wrapper>::value &&
!futures::detail::Extract<F>::ReturnsFuture::value,
Future<T>>::type
onError(F&& func);
/// func is like std::function<void()> and is executed unconditionally, and
......@@ -482,7 +483,7 @@ class Future {
}
protected:
typedef detail::Core<T>* corePtr;
typedef futures::detail::Core<T>* corePtr;
// shared core state object
corePtr core_;
......@@ -490,7 +491,7 @@ class Future {
explicit
Future(corePtr obj) : core_(obj) {}
explicit Future(detail::EmptyConstruct) noexcept;
explicit Future(futures::detail::EmptyConstruct) noexcept;
void detach();
......@@ -529,13 +530,13 @@ class Future {
// e.g. f.then([](Try<T> t){ return t.value(); });
template <typename F, typename R, bool isTry, typename... Args>
typename std::enable_if<!R::ReturnsFuture::value, typename R::Return>::type
thenImplementation(F&& func, detail::argResult<isTry, F, Args...>);
thenImplementation(F&& func, futures::detail::argResult<isTry, F, Args...>);
// Variant: returns a Future
// e.g. f.then([](Try<T> t){ return makeFuture<T>(t); });
template <typename F, typename R, bool isTry, typename... Args>
typename std::enable_if<R::ReturnsFuture::value, typename R::Return>::type
thenImplementation(F&& func, detail::argResult<isTry, F, Args...>);
thenImplementation(F&& func, futures::detail::argResult<isTry, F, Args...>);
Executor* getExecutor() { return core_->getExecutor(); }
void setExecutor(Executor* x, int8_t priority = Executor::MID_PRI) {
......
......@@ -26,12 +26,12 @@ namespace folly {
template <class T>
Promise<T> Promise<T>::makeEmpty() noexcept {
return Promise<T>(detail::EmptyConstruct{});
return Promise<T>(futures::detail::EmptyConstruct{});
}
template <class T>
Promise<T>::Promise() : retrieved_(false), core_(new detail::Core<T>())
{}
Promise<T>::Promise()
: retrieved_(false), core_(new futures::detail::Core<T>()) {}
template <class T>
Promise<T>::Promise(Promise<T>&& other) noexcept
......@@ -65,7 +65,7 @@ void Promise<T>::throwIfRetrieved() {
}
template <class T>
Promise<T>::Promise(detail::EmptyConstruct) noexcept
Promise<T>::Promise(futures::detail::EmptyConstruct) noexcept
: retrieved_(false), core_(nullptr) {}
template <class T>
......
......@@ -25,11 +25,13 @@ namespace folly {
// forward declaration
template <class T> class Future;
namespace futures {
namespace detail {
struct EmptyConstruct {};
template <typename T, typename F>
class CoreCallbackState;
}
} // namespace detail
} // namespace futures
template <class T>
class Promise {
......@@ -107,7 +109,7 @@ class Promise {
typedef typename Future<T>::corePtr corePtr;
template <class> friend class Future;
template <class, class>
friend class detail::CoreCallbackState;
friend class futures::detail::CoreCallbackState;
// Whether the Future has been retrieved (a one-time operation).
bool retrieved_;
......@@ -115,7 +117,7 @@ class Promise {
// shared core state object
corePtr core_;
explicit Promise(detail::EmptyConstruct) noexcept;
explicit Promise(futures::detail::EmptyConstruct) noexcept;
void throwIfFulfilled();
void throwIfRetrieved();
......
......@@ -34,7 +34,9 @@
#include <folly/io/async/Request.h>
namespace folly { namespace detail {
namespace folly {
namespace futures {
namespace detail {
/*
OnlyCallback
......@@ -450,4 +452,6 @@ void collectVariadicHelper(const std::shared_ptr<T<Ts...>>& ctx,
collectVariadicHelper(ctx, std::forward<TTail>(tail)...);
}
}} // folly::detail
} // namespace detail
} // namespace futures
} // namespace folly
......@@ -21,7 +21,9 @@
#include <folly/MicroSpinLock.h>
namespace folly { namespace detail {
namespace folly {
namespace futures {
namespace detail {
/// Finite State Machine helper base class.
/// Inherit from this.
......@@ -126,5 +128,6 @@ public:
#define FSM_BREAK done = true; break;
#define FSM_END }}}
}} // folly::detail
} // namespace detail
} // namespace futures
} // namespace folly
......@@ -26,6 +26,7 @@
namespace folly {
namespace futures {
namespace detail {
template <typename... Ts>
struct CollectAllVariadicContext {
......@@ -65,7 +66,8 @@ struct CollectVariadicContext {
std::atomic<bool> threw{false};
typedef Future<std::tuple<Ts...>> type;
};
}
} // namespace detail
} // namespace futures
/// This namespace is for utility functions that would usually be static
/// members of Future, except they don't make sense there because they don't
......@@ -228,17 +230,16 @@ auto collectAll(Collection&& c) -> decltype(collectAll(c.begin(), c.end())) {
/// is a Future<std::tuple<Try<T1>, Try<T2>, ...>>.
/// The Futures are moved in, so your copies are invalid.
template <typename... Fs>
typename detail::CollectAllVariadicContext<
typename std::decay<Fs>::type::value_type...>::type
typename futures::detail::CollectAllVariadicContext<
typename std::decay<Fs>::type::value_type...>::type
collectAll(Fs&&... fs);
/// Like collectAll, but will short circuit on the first exception. Thus, the
/// type of the returned Future is std::vector<T> instead of
/// std::vector<Try<T>>
template <class InputIterator>
Future<typename detail::CollectContext<
typename std::iterator_traits<InputIterator>::value_type::value_type
>::result_type>
Future<typename futures::detail::CollectContext<typename std::iterator_traits<
InputIterator>::value_type::value_type>::result_type>
collect(InputIterator first, InputIterator last);
/// Sugar for the most common case
......@@ -251,8 +252,8 @@ auto collect(Collection&& c) -> decltype(collect(c.begin(), c.end())) {
/// type of the returned Future is std::tuple<T1, T2, ...> instead of
/// std::tuple<Try<T1>, Try<T2>, ...>
template <typename... Fs>
typename detail::CollectVariadicContext<
typename std::decay<Fs>::type::value_type...>::type
typename futures::detail::CollectVariadicContext<
typename std::decay<Fs>::type::value_type...>::type
collect(Fs&&... fs);
/** The result is a pair of the index of the first Future to complete and
......@@ -317,16 +318,19 @@ auto collectN(Collection&& c, size_t n)
func must return a Future for each value in input
*/
template <class Collection, class F,
class ItT = typename std::iterator_traits<
typename Collection::iterator>::value_type,
class Result = typename detail::resultOf<F, ItT&&>::value_type>
std::vector<Future<Result>>
window(Collection input, F func, size_t n);
template <
class Collection,
class F,
class ItT = typename std::iterator_traits<
typename Collection::iterator>::value_type,
class Result = typename futures::detail::resultOf<F, ItT&&>::value_type>
std::vector<Future<Result>> window(Collection input, F func, size_t n);
template <typename F, typename T, typename ItT>
using MaybeTryArg = typename std::conditional<
detail::callableWith<F, T&&, Try<ItT>&&>::value, Try<ItT>, ItT>::type;
futures::detail::callableWith<F, T&&, Try<ItT>&&>::value,
Try<ItT>,
ItT>::type;
template<typename F, typename T, typename Arg>
using isFutureResult = isFuture<typename std::result_of<F(T&&, Arg&&)>::type>;
......
......@@ -26,7 +26,7 @@ TEST(Core, size) {
typename std::aligned_storage<lambdaBufSize>::type lambdaBuf_;
folly::Optional<Try<Unit>> result_;
std::function<void(Try<Unit>&&)> callback_;
detail::FSM<detail::State> fsm_;
futures::detail::FSM<futures::detail::State> fsm_;
std::atomic<unsigned char> attached_;
std::atomic<bool> active_;
std::atomic<bool> interruptHandlerSet_;
......@@ -40,5 +40,5 @@ TEST(Core, size) {
};
// If this number goes down, it's fine!
// If it goes up, please seek professional advice ;-)
EXPECT_GE(sizeof(Gold), sizeof(detail::Core<Unit>));
EXPECT_GE(sizeof(Gold), sizeof(futures::detail::Core<Unit>));
}
......@@ -17,7 +17,7 @@
#include <folly/futures/detail/FSM.h>
#include <folly/portability/GTest.h>
using namespace folly::detail;
using namespace folly::futures::detail;
enum class State { A, B };
......
......@@ -648,7 +648,7 @@ TEST(Future, finishBigLambda) {
// bulk_data, to be captured in the lambda passed to Future::then.
// This is meant to force that the lambda can't be stored inside
// the Future object.
std::array<char, sizeof(detail::Core<int>)> bulk_data = {{0}};
std::array<char, sizeof(futures::detail::Core<int>)> bulk_data = {{0}};
// suppress gcc warning about bulk_data not being used
EXPECT_EQ(bulk_data[0], 0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment