Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rc::compat::return_type for both std::result_of and std::invoke_result #269

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/rapidcheck/Compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#include "rapidcheck/Compat.hpp"
17 changes: 17 additions & 0 deletions include/rapidcheck/Compat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <type_traits>

namespace rc {
namespace compat {

#if __cplusplus <= 201703L
template <typename Fn, typename ...Args>
using return_type = typename std::result_of<Fn(Args...)>;
#else
template <typename Fn, typename ...Args>
using return_type = typename std::invoke_result<Fn,Args...>;
#endif

}
}
3 changes: 2 additions & 1 deletion include/rapidcheck/Gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#include "rapidcheck/detail/ImplicitParam.h"
#include "rapidcheck/gen/detail/GenerationHandler.h"
#include "rapidcheck/shrinkable/Create.h"
#include "rapidcheck/Compat.h"

namespace rc {
namespace gen {

// Forward declare this so we don't need to include Transform.h
template <typename T, typename Mapper>
Gen<Decay<typename std::result_of<Mapper(T)>::type>> map(Gen<T> gen,
Gen<Decay<typename rc::compat::return_type<Mapper,T>::type>> map(Gen<T> gen,
Mapper &&mapper);

} // namespace gen
Expand Down
3 changes: 2 additions & 1 deletion include/rapidcheck/Seq.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "rapidcheck/Nothing.h"
#include "rapidcheck/Maybe.h"
#include "rapidcheck/Traits.h"
#include "rapidcheck/Compat.h"

namespace rc {

Expand Down Expand Up @@ -34,7 +35,7 @@ class Seq {
/// Creates a new `Seq` using the implementation class specificed by the
/// type parameter constructed by forwarding the given arguments.
template <typename Impl, typename... Args>
friend Seq<typename std::result_of<Impl()>::type::ValueType>
friend Seq<typename rc::compat::return_type<Impl>::type::ValueType>
makeSeq(Args &&... args);

public:
Expand Down
5 changes: 3 additions & 2 deletions include/rapidcheck/Seq.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "rapidcheck/Show.h"
#include "rapidcheck/Compat.h"

namespace rc {

Expand Down Expand Up @@ -59,8 +60,8 @@ Seq<T> &Seq<T>::operator=(const Seq &rhs) {
}

template <typename Impl, typename... Args>
Seq<typename std::result_of<Impl(void)>::type::ValueType> makeSeq(Args &&... args) {
using SeqT = Seq<typename std::result_of<Impl(void)>::type::ValueType>;
Seq<typename rc::compat::return_type<Impl>::type::ValueType> makeSeq(Args &&... args) {
using SeqT = Seq<typename rc::compat::return_type<Impl>::type::ValueType>;
using ImplT = typename SeqT::template SeqImpl<Impl>;

SeqT seq;
Expand Down
7 changes: 4 additions & 3 deletions include/rapidcheck/detail/ApplyTuple.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "rapidcheck/detail/IntSequence.h"
#include "rapidcheck/Compat.h"

namespace rc {
namespace detail {
Expand All @@ -13,7 +14,7 @@ struct ApplyTupleImpl;

template <typename... Ts, std::size_t... Indexes, typename Callable>
struct ApplyTupleImpl<std::tuple<Ts...>, Callable, IndexSequence<Indexes...>> {
using ReturnType = typename std::result_of<Callable(Ts &&...)>::type;
using ReturnType = typename rc::compat::return_type<Callable,Ts &&...>::type;

static ReturnType apply(std::tuple<Ts...> &&tuple, Callable &&callable) {
return callable(std::move(std::get<Indexes>(tuple))...);
Expand All @@ -24,7 +25,7 @@ template <typename... Ts, std::size_t... Indexes, typename Callable>
struct ApplyTupleImpl<std::tuple<Ts...> &,
Callable,
IndexSequence<Indexes...>> {
using ReturnType = typename std::result_of<Callable(Ts &...)>::type;
using ReturnType = typename rc::compat::return_type<Callable,Ts &...>::type;

static ReturnType apply(std::tuple<Ts...> &tuple, Callable &&callable) {
return callable(std::get<Indexes>(tuple)...);
Expand All @@ -35,7 +36,7 @@ template <typename... Ts, std::size_t... Indexes, typename Callable>
struct ApplyTupleImpl<const std::tuple<Ts...> &,
Callable,
IndexSequence<Indexes...>> {
using ReturnType = typename std::result_of<Callable(const Ts &...)>::type;
using ReturnType = typename rc::compat::return_type<Callable,const Ts &...>::type;

static ReturnType apply(const std::tuple<Ts...> &tuple, Callable &&callable) {
return callable(std::get<Indexes>(tuple)...);
Expand Down
5 changes: 3 additions & 2 deletions include/rapidcheck/gen/Container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "rapidcheck/gen/detail/ShrinkValueIterator.h"
#include "rapidcheck/shrink/Shrink.h"
#include "rapidcheck/shrinkable/Create.h"
#include "rapidcheck/Compat.h"

namespace rc {
namespace gen {
Expand Down Expand Up @@ -244,7 +245,7 @@ class UniqueContainerStrategy {
int size,
std::size_t count,
const Gen<T> &gen) const {
using Key = Decay<typename std::result_of<F(T)>::type>;
using Key = Decay<typename rc::compat::return_type<F,T>::type>;
std::set<Key> values;
return detail::generateShrinkables(random,
size,
Expand All @@ -259,7 +260,7 @@ class UniqueContainerStrategy {
template <typename T>
Seq<Shrinkables<T>>
shrinkElements(const Shrinkables<T> &shrinkables) const {
using Key = Decay<typename std::result_of<F(T)>::type>;
using Key = Decay<typename rc::compat::return_type<F,T>::type>;
const auto keys = std::make_shared<std::set<Key>>();
for (const auto &shrinkable : shrinkables) {
keys->insert(m_f(shrinkable.value()));
Expand Down
3 changes: 2 additions & 1 deletion include/rapidcheck/gen/Create.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "rapidcheck/Gen.h"
#include "rapidcheck/Compat.h"

namespace rc {
namespace gen {
Expand All @@ -13,7 +14,7 @@ Gen<Decay<T>> just(T &&value);
/// called lazily on actual generation. This is useful when implementing
/// recursive generators where a generator must reference itself.
template <typename Callable>
Gen<typename std::result_of<Callable()>::type::ValueType>
Gen<typename rc::compat::return_type<Callable>::type::ValueType>
lazy(Callable &&callable);

} // namespace gen
Expand Down
3 changes: 2 additions & 1 deletion include/rapidcheck/gen/Create.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "rapidcheck/fn/Common.h"
#include "rapidcheck/shrinkable/Create.h"
#include "rapidcheck/Compat.h"

namespace rc {
namespace gen {
Expand All @@ -12,7 +13,7 @@ Gen<Decay<T>> just(T &&value) {
}

template <typename Callable>
Gen<typename std::result_of<Callable()>::type::ValueType>
Gen<typename rc::compat::return_type<Callable>::type::ValueType>
lazy(Callable &&callable) {
return
[=](const Random &random, int size) { return callable()(random, size); };
Expand Down
11 changes: 6 additions & 5 deletions include/rapidcheck/gen/Transform.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#pragma once

#include "rapidcheck/Gen.h"
#include "rapidcheck/Compat.h"

namespace rc {
namespace gen {

/// Returns a generator based on the given generator but mapped with the given
/// mapping function.
template <typename T, typename Mapper>
Gen<Decay<typename std::result_of<Mapper(T)>::type>> map(Gen<T> gen,
Gen<Decay<typename rc::compat::return_type<Mapper,T>::type>> map(Gen<T> gen,
Mapper &&mapper);

/// Convenience function which calls `map(Gen<T>, Mapper)` with
/// `gen::arbitrary<T>`
template <typename T, typename Mapper>
Gen<Decay<typename std::result_of<Mapper(T)>::type>> map(Mapper &&mapper);
Gen<Decay<typename rc::compat::return_type<Mapper,T>::type>> map(Mapper &&mapper);

/// Monadic bind. Takes a `Gen<T>` and a function from a `T` to a `Gen<U>` and
/// returns a `Gen<U>`. When shrinking, the value generated by the first
/// generator will be shrunk first, then the second.
template <typename T, typename Mapper>
Gen<typename std::result_of<Mapper(T)>::type::ValueType>
Gen<typename rc::compat::return_type<Mapper,T>::type::ValueType>
mapcat(Gen<T> gen, Mapper &&mapper);

/// Flattens a generator of generators of `T` into a generator of `T`. This is
Expand All @@ -31,7 +32,7 @@ Gen<T> join(Gen<Gen<T>> gen);
/// Calls the given callable with values generated by the given generators. Has
/// tuple semantics when shrinking.
template <typename Callable, typename... Ts>
Gen<typename std::result_of<Callable(Ts...)>::type>
Gen<typename rc::compat::return_type<Callable,Ts...>::type>
apply(Callable &&callable, Gen<Ts>... gens);

/// Returns a generator that casts the generated values to `T` using
Expand All @@ -52,7 +53,7 @@ Gen<T> scale(double scale, Gen<T> gen);
/// Creates a generator by taking a callable which gets passed the current size
/// and is expected to return a generator.
template <typename Callable>
Gen<typename std::result_of<Callable(int)>::type::ValueType>
Gen<typename rc::compat::return_type<Callable,int>::type::ValueType>
withSize(Callable &&callable);

/// Use this to disable shrinking for a given generator. When a failing case is
Expand Down
15 changes: 8 additions & 7 deletions include/rapidcheck/gen/Transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "rapidcheck/gen/Tuple.h"
#include "rapidcheck/GenerationFailure.h"
#include "rapidcheck/Random.h"
#include "rapidcheck/Compat.h"

namespace rc {
namespace gen {
Expand All @@ -13,7 +14,7 @@ namespace detail {
template <typename T, typename Mapper>
class MapGen {
public:
using U = Decay<typename std::result_of<Mapper(T)>::type>;
using U = Decay<typename rc::compat::return_type<Mapper,T>::type>;

template <typename MapperArg>
MapGen(Gen<T> gen, MapperArg &&mapper)
Expand All @@ -32,7 +33,7 @@ class MapGen {
template <typename T, typename Mapper>
class MapcatGen {
public:
using U = typename std::result_of<Mapper(T)>::type::ValueType;
using U = typename rc::compat::return_type<Mapper,T>::type::ValueType;

template <typename MapperArg>
explicit MapcatGen(Gen<T> gen, MapperArg &&mapper)
Expand Down Expand Up @@ -73,19 +74,19 @@ class JoinGen {
} // namespace detail

template <typename T, typename Mapper>
Gen<Decay<typename std::result_of<Mapper(T)>::type>> map(Gen<T> gen,
Gen<Decay<typename rc::compat::return_type<Mapper,T>::type>> map(Gen<T> gen,
Mapper &&mapper) {
return detail::MapGen<T, Decay<Mapper>>(std::move(gen),
std::forward<Mapper>(mapper));
}

template <typename T, typename Mapper>
Gen<Decay<typename std::result_of<Mapper(T)>::type>> map(Mapper &&mapper) {
Gen<Decay<typename rc::compat::return_type<Mapper,T>::type>> map(Mapper &&mapper) {
return gen::map(gen::arbitrary<T>(), std::forward<Mapper>(mapper));
}

template <typename T, typename Mapper>
Gen<typename std::result_of<Mapper(T)>::type::ValueType>
Gen<typename rc::compat::return_type<Mapper,T>::type::ValueType>
mapcat(Gen<T> gen, Mapper &&mapper) {
return detail::MapcatGen<T, Decay<Mapper>>(std::move(gen),
std::forward<Mapper>(mapper));
Expand All @@ -97,7 +98,7 @@ Gen<T> join(Gen<Gen<T>> gen) {
}

template <typename Callable, typename... Ts>
Gen<typename std::result_of<Callable(Ts...)>::type> apply(Callable &&callable,
Gen<typename rc::compat::return_type<Callable,Ts...>::type> apply(Callable &&callable,
Gen<Ts>... gens) {
return gen::map(gen::tuple(std::move(gens)...),
[=](std::tuple<Ts...> &&tuple) {
Expand All @@ -124,7 +125,7 @@ Gen<T> scale(double scale, Gen<T> gen) {
}

template <typename Callable>
Gen<typename std::result_of<Callable(int)>::type::ValueType>
Gen<typename rc::compat::return_type<Callable,int>::type::ValueType>
withSize(Callable &&callable) {
return [=](const Random &random, int size) {
return callable(size)(random, size);
Expand Down
9 changes: 5 additions & 4 deletions include/rapidcheck/seq/Transform.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "rapidcheck/Seq.h"
#include "rapidcheck/Compat.h"

namespace rc {
namespace seq {
Expand All @@ -23,7 +24,7 @@ Seq<T> takeWhile(Seq<T> seq, Predicate &&pred);

/// Maps the elements of the given `Seq` using the given callable.
template <typename T, typename Mapper>
Seq<Decay<typename std::result_of<Mapper(T)>::type>> map(Seq<T> seq,
Seq<Decay<typename rc::compat::return_type<Mapper,T>::type>> map(Seq<T> seq,
Mapper &&mapper);

/// Takes elements from the given `Seq`s and passes them as arguments to the
Expand All @@ -33,7 +34,7 @@ Seq<Decay<typename std::result_of<Mapper(T)>::type>> map(Seq<T> seq,
/// Fun fact: Also works with no sequences and in that case returns an infinite
/// sequence of the return values of calling the given callable.
template <typename... Ts, typename Zipper>
Seq<Decay<typename std::result_of<Zipper(Ts...)>::type>>
Seq<Decay<typename rc::compat::return_type<Zipper,Ts...>::type>>
zipWith(Zipper &&zipper, Seq<Ts>... seqs);

/// Skips elements not matching the given predicate from the given stream.
Expand All @@ -51,14 +52,14 @@ Seq<T> concat(Seq<T> seq, Seq<Ts>... seqs);
/// Maps each tuple elements of the given to `Seq`s to further `Seq`s and
/// concatenates them into one `Seq`. Sometimes called a "flat map".
template <typename T, typename Mapper>
Seq<typename std::result_of<Mapper(T)>::type::ValueType>
Seq<typename rc::compat::return_type<Mapper,T>::type::ValueType>
mapcat(Seq<T> seq, Mapper &&mapper);

/// Like `map` but expects the mapping functor to return a `Maybe`. If `Nothing`
/// is returned, the element is skipped. Otherwise, the `Maybe` is unwrapped and
/// included in the resulting `Seq`.
template <typename T, typename Mapper>
Seq<typename std::result_of<Mapper(T)>::type::ValueType>
Seq<typename rc::compat::return_type<Mapper,T>::type::ValueType>
mapMaybe(Seq<T> seq, Mapper &&mapper);

/// Creates a `Seq` which infinitely repeats the given `Seq`.
Expand Down
17 changes: 9 additions & 8 deletions include/rapidcheck/seq/Transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "rapidcheck/detail/ApplyTuple.h"
#include "rapidcheck/seq/Create.h"
#include "rapidcheck/Compat.h"

namespace rc {
namespace seq {
Expand Down Expand Up @@ -117,7 +118,7 @@ class TakeWhileSeq {
template <typename Mapper, typename T>
class MapSeq {
public:
using U = Decay<typename std::result_of<Mapper(T)>::type>;
using U = Decay<typename rc::compat::return_type<Mapper,T>::type>;

template <typename MapperArg>
MapSeq(Seq<T> seq, MapperArg &&mapper)
Expand All @@ -142,7 +143,7 @@ class MapSeq {
template <typename Zipper, typename... Ts>
class ZipWithSeq {
public:
using U = Decay<typename std::result_of<Zipper(Ts...)>::type>;
using U = Decay<typename rc::compat::return_type<Zipper,Ts...>::type>;

template <typename ZipperArg>
ZipWithSeq(ZipperArg &&zipper, Seq<Ts>... seqs)
Expand Down Expand Up @@ -263,7 +264,7 @@ class ConcatSeq {
template <typename Mapper, typename T>
class MapcatSeq {
public:
using U = typename std::result_of<Mapper(T)>::type::ValueType;
using U = typename rc::compat::return_type<Mapper,T>::type::ValueType;

template <typename MapperArg>
MapcatSeq(Seq<T> seq, MapperArg &&mapper)
Expand Down Expand Up @@ -325,14 +326,14 @@ Seq<T> takeWhile(Seq<T> seq, Predicate &&pred) {
}

template <typename T, typename Mapper>
Seq<Decay<typename std::result_of<Mapper(T)>::type>> map(Seq<T> seq,
Seq<Decay<typename rc::compat::return_type<Mapper,T>::type>> map(Seq<T> seq,
Mapper &&mapper) {
return makeSeq<detail::MapSeq<Decay<Mapper>, T>>(
std::move(seq), std::forward<Mapper>(mapper));
}

template <typename... Ts, typename Zipper>
Seq<Decay<typename std::result_of<Zipper(Ts...)>::type>>
Seq<Decay<typename rc::compat::return_type<Zipper,Ts...>::type>>
zipWith(Zipper &&zipper, Seq<Ts>... seqs) {
return makeSeq<detail::ZipWithSeq<Decay<Zipper>, Ts...>>(
std::forward<Zipper>(zipper), std::move(seqs)...);
Expand All @@ -356,16 +357,16 @@ Seq<T> concat(Seq<T> seq, Seq<Ts>... seqs) {
}

template <typename T, typename Mapper>
Seq<typename std::result_of<Mapper(T)>::type::ValueType>
Seq<typename rc::compat::return_type<Mapper,T>::type::ValueType>
mapcat(Seq<T> seq, Mapper &&mapper) {
return makeSeq<detail::MapcatSeq<Decay<Mapper>, T>>(
std::move(seq), std::forward<Mapper>(mapper));
}

template <typename T, typename Mapper>
Seq<typename std::result_of<Mapper(T)>::type::ValueType>
Seq<typename rc::compat::return_type<Mapper,T>::type::ValueType>
mapMaybe(Seq<T> seq, Mapper &&mapper) {
using U = typename std::result_of<Mapper(T)>::type::ValueType;
using U = typename rc::compat::return_type<Mapper,T>::type::ValueType;
return seq::map(
seq::filter(seq::map(std::move(seq), std::forward<Mapper>(mapper)),
[](const Maybe<U> &x) { return !!x; }),
Expand Down
Loading