mirror of
https://github.com/BlackMATov/invoke.hpp.git
synced 2025-12-13 05:56:31 +07:00
initial commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
build/*
|
||||
CMakeLists.txt.user
|
||||
12
CMakeLists.txt
Normal file
12
CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
|
||||
project(invoke)
|
||||
|
||||
file(GLOB test_sources "*.cpp" "*.hpp")
|
||||
add_executable(${PROJECT_NAME} ${test_sources})
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
CXX_STANDARD 14
|
||||
CXX_STANDARD_REQUIRED YES
|
||||
CXX_EXTENSIONS NO)
|
||||
|
||||
enable_testing()
|
||||
add_test(${PROJECT_NAME} ${PROJECT_NAME})
|
||||
21
LICENSE.md
Normal file
21
LICENSE.md
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Matvey Cherevko
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
27
README.md
Normal file
27
README.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# invoke.hpp
|
||||
|
||||
> std::invoke/std::apply analogs for C++14
|
||||
|
||||
[invoke]: https://github.com/BlackMATov/invoke.hpp
|
||||
|
||||
## Installation
|
||||
|
||||
[invoke.hpp][invoke] is a single header library. All you need to do is copy the header file into your project and include this file:
|
||||
|
||||
```cpp
|
||||
#include "invoke.hpp"
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### `invoke_hpp::invoke(F&& f, Args&&... args)`
|
||||
|
||||
Analog of [std::invoke](https://en.cppreference.com/w/cpp/utility/functional/invoke) from C++17
|
||||
|
||||
### `invoke_hpp::invoke_result<F, Args...>`
|
||||
|
||||
Analog of [std::invoke_result](https://en.cppreference.com/w/cpp/types/result_of) from C++17
|
||||
|
||||
### `invoke_hpp::apply(F&& f, Tuple&& args)`
|
||||
|
||||
Analog of [std::apply](https://en.cppreference.com/w/cpp/utility/apply) from C++17
|
||||
212
invoke.hpp
Normal file
212
invoke.hpp
Normal file
@@ -0,0 +1,212 @@
|
||||
#pragma once
|
||||
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
#define INVOKE_HPP_NOEXCEPT_RETURN(...) \
|
||||
noexcept(noexcept(__VA_ARGS__)) { return __VA_ARGS__; }
|
||||
|
||||
#define INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN(...) \
|
||||
noexcept(noexcept(__VA_ARGS__)) -> decltype (__VA_ARGS__) { return __VA_ARGS__; }
|
||||
|
||||
//
|
||||
// void_t
|
||||
//
|
||||
|
||||
namespace invoke_hpp
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
template < typename... Args >
|
||||
struct make_void {
|
||||
using type = void;
|
||||
};
|
||||
}
|
||||
|
||||
template < typename... Args >
|
||||
using void_t = typename impl::make_void<Args...>::type;
|
||||
}
|
||||
|
||||
//
|
||||
// is_reference_wrapper
|
||||
//
|
||||
|
||||
namespace invoke_hpp
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
template < typename T >
|
||||
struct is_reference_wrapper_impl
|
||||
: std::false_type {};
|
||||
|
||||
template < typename U >
|
||||
struct is_reference_wrapper_impl<std::reference_wrapper<U>>
|
||||
: std::true_type {};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
struct is_reference_wrapper
|
||||
: impl::is_reference_wrapper_impl<std::remove_cv_t<T>> {};
|
||||
}
|
||||
|
||||
//
|
||||
// invoke
|
||||
//
|
||||
|
||||
namespace invoke_hpp
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
//
|
||||
// invoke_member_object_impl
|
||||
//
|
||||
|
||||
template
|
||||
<
|
||||
typename Base, typename F, typename Derived,
|
||||
typename std::enable_if_t<std::is_base_of<Base, std::decay_t<Derived>>::value, int> = 0
|
||||
>
|
||||
constexpr auto invoke_member_object_impl(F Base::* f, Derived&& ref)
|
||||
INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN(
|
||||
std::forward<Derived>(ref).*f)
|
||||
|
||||
template
|
||||
<
|
||||
typename Base, typename F, typename RefWrap,
|
||||
typename std::enable_if_t<is_reference_wrapper<std::decay_t<RefWrap>>::value, int> = 0
|
||||
>
|
||||
constexpr auto invoke_member_object_impl(F Base::* f, RefWrap&& ref)
|
||||
INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN(
|
||||
ref.get().*f)
|
||||
|
||||
template
|
||||
<
|
||||
typename Base, typename F, typename Pointer,
|
||||
typename std::enable_if_t<
|
||||
!std::is_base_of<Base, std::decay_t<Pointer>>::value &&
|
||||
!is_reference_wrapper<std::decay_t<Pointer>>::value
|
||||
, int> = 0
|
||||
>
|
||||
constexpr auto invoke_member_object_impl(F Base::* f, Pointer&& ptr)
|
||||
INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN(
|
||||
(*std::forward<Pointer>(ptr)).*f)
|
||||
|
||||
//
|
||||
// invoke_member_function_impl
|
||||
//
|
||||
|
||||
template
|
||||
<
|
||||
typename Base, typename F, typename Derived, typename... Args,
|
||||
typename std::enable_if_t<std::is_base_of<Base, std::decay_t<Derived>>::value, int> = 0
|
||||
>
|
||||
constexpr auto invoke_member_function_impl(F Base::* f, Derived&& ref, Args&&... args)
|
||||
INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN(
|
||||
(std::forward<Derived>(ref).*f)(std::forward<Args>(args)...))
|
||||
|
||||
template
|
||||
<
|
||||
typename Base, typename F, typename RefWrap, typename... Args,
|
||||
typename std::enable_if_t<is_reference_wrapper<std::decay_t<RefWrap>>::value, int> = 0
|
||||
>
|
||||
constexpr auto invoke_member_function_impl(F Base::* f, RefWrap&& ref, Args&&... args)
|
||||
INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN(
|
||||
(ref.get().*f)(std::forward<Args>(args)...))
|
||||
|
||||
template
|
||||
<
|
||||
typename Base, typename F, typename Pointer, typename... Args,
|
||||
typename std::enable_if_t<
|
||||
!std::is_base_of<Base, std::decay_t<Pointer>>::value &&
|
||||
!is_reference_wrapper<std::decay_t<Pointer>>::value
|
||||
, int> = 0
|
||||
>
|
||||
constexpr auto invoke_member_function_impl(F Base::* f, Pointer&& ptr, Args&&... args)
|
||||
INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN(
|
||||
((*std::forward<Pointer>(ptr)).*f)(std::forward<Args>(args)...))
|
||||
}
|
||||
|
||||
template
|
||||
<
|
||||
typename F, typename... Args,
|
||||
typename std::enable_if_t<!std::is_member_pointer<std::decay_t<F>>::value, int> = 0
|
||||
>
|
||||
constexpr auto invoke(F&& f, Args&&... args)
|
||||
INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN(
|
||||
std::forward<F>(f)(std::forward<Args>(args)...))
|
||||
|
||||
template
|
||||
<
|
||||
typename F, typename T,
|
||||
typename std::enable_if_t<std::is_member_object_pointer<std::decay_t<F>>::value, int> = 0
|
||||
>
|
||||
constexpr auto invoke(F&& f, T&& t)
|
||||
INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN(
|
||||
impl::invoke_member_object_impl(std::forward<F>(f), std::forward<T>(t)))
|
||||
|
||||
template
|
||||
<
|
||||
typename F, typename... Args,
|
||||
typename std::enable_if_t<std::is_member_function_pointer<std::decay_t<F>>::value, int> = 0
|
||||
>
|
||||
constexpr auto invoke(F&& f, Args&&... args)
|
||||
INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN(
|
||||
impl::invoke_member_function_impl(std::forward<F>(f), std::forward<Args>(args)...))
|
||||
}
|
||||
|
||||
//
|
||||
// invoke_result
|
||||
//
|
||||
|
||||
namespace invoke_hpp
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
struct invoke_result_impl_tag {};
|
||||
|
||||
template < typename Void, typename F, typename... Args >
|
||||
struct invoke_result_impl {};
|
||||
|
||||
template < typename F, typename... Args >
|
||||
struct invoke_result_impl<void_t<invoke_result_impl_tag, decltype(invoke_hpp::invoke(std::declval<F>(), std::declval<Args>()...))>, F, Args...> {
|
||||
using type = decltype(invoke_hpp::invoke(std::declval<F>(), std::declval<Args>()...));
|
||||
};
|
||||
}
|
||||
|
||||
template < typename F, typename... Args >
|
||||
struct invoke_result
|
||||
: impl::invoke_result_impl<void, F, Args...> {};
|
||||
|
||||
template < typename F, typename... Args >
|
||||
using invoke_result_t = typename invoke_result<F, Args...>::type;
|
||||
}
|
||||
|
||||
//
|
||||
// apply
|
||||
//
|
||||
|
||||
namespace invoke_hpp
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
template < typename F, typename Tuple, std::size_t... I >
|
||||
constexpr decltype(auto) apply_impl(F&& f, Tuple&& args, std::index_sequence<I...>)
|
||||
INVOKE_HPP_NOEXCEPT_RETURN(
|
||||
invoke_hpp::invoke(
|
||||
std::forward<F>(f),
|
||||
std::get<I>(std::forward<Tuple>(args))...))
|
||||
}
|
||||
|
||||
template < typename F, typename Tuple >
|
||||
constexpr decltype(auto) apply(F&& f, Tuple&& args)
|
||||
INVOKE_HPP_NOEXCEPT_RETURN(
|
||||
impl::apply_impl(
|
||||
std::forward<F>(f),
|
||||
std::forward<Tuple>(args),
|
||||
std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>()))
|
||||
}
|
||||
|
||||
#undef INVOKE_HPP_NOEXCEPT_RETURN
|
||||
#undef INVOKE_HPP_NOEXCEPT_DECLTYPE_RETURN
|
||||
189
tests.cpp
Normal file
189
tests.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include "catch.hpp"
|
||||
|
||||
#include "invoke.hpp"
|
||||
namespace inv = invoke_hpp;
|
||||
|
||||
namespace
|
||||
{
|
||||
void simple_static_function() {
|
||||
}
|
||||
|
||||
int simple_static_function_r() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
int simple_static_function_r_with_arg(int v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
const int& simple_static_function_r_with_ref_arg(const int& v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
class obj_t {
|
||||
public:
|
||||
int value = 42;
|
||||
const int value_c = 42;
|
||||
|
||||
void member() {
|
||||
}
|
||||
|
||||
int member_r() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
int member_r_with_arg(int v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
const int& member_r_with_ref_arg(const int& v) {
|
||||
return v;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
TEST_CASE("invoke"){
|
||||
SECTION("invoke_functions"){
|
||||
inv::invoke(simple_static_function);
|
||||
REQUIRE(inv::invoke(simple_static_function_r) == 42);
|
||||
REQUIRE(inv::invoke(simple_static_function_r_with_arg, 42) == 42);
|
||||
{
|
||||
int v = 42;
|
||||
REQUIRE(&inv::invoke(simple_static_function_r_with_ref_arg, v) == &v);
|
||||
}
|
||||
}
|
||||
SECTION("invoke_members"){
|
||||
obj_t o;
|
||||
|
||||
inv::invoke(&obj_t::member, o);
|
||||
inv::invoke(&obj_t::member, &o);
|
||||
inv::invoke(&obj_t::member, std::ref(o));
|
||||
|
||||
REQUIRE(inv::invoke(&obj_t::member_r, o) == 42);
|
||||
REQUIRE(inv::invoke(&obj_t::member_r, &o) == 42);
|
||||
REQUIRE(inv::invoke(&obj_t::member_r, std::ref(o)) == 42);
|
||||
|
||||
REQUIRE(inv::invoke(&obj_t::member_r_with_arg, o, 42) == 42);
|
||||
REQUIRE(inv::invoke(&obj_t::member_r_with_arg, &o, 42) == 42);
|
||||
REQUIRE(inv::invoke(&obj_t::member_r_with_arg, std::ref(o), 42) == 42);
|
||||
|
||||
{
|
||||
int v = 42;
|
||||
REQUIRE(&inv::invoke(&obj_t::member_r_with_ref_arg, o, std::ref(v)) == &v);
|
||||
REQUIRE(&inv::invoke(&obj_t::member_r_with_ref_arg, &o, std::ref(v)) == &v);
|
||||
REQUIRE(&inv::invoke(&obj_t::member_r_with_ref_arg, std::ref(o), std::ref(v)) == &v);
|
||||
}
|
||||
}
|
||||
SECTION("invoke_member_objects"){
|
||||
obj_t o;
|
||||
|
||||
REQUIRE(inv::invoke(&obj_t::value, o) == 42);
|
||||
REQUIRE(inv::invoke(&obj_t::value, &o) == 42);
|
||||
REQUIRE(inv::invoke(&obj_t::value, std::ref(o)) == 42);
|
||||
|
||||
REQUIRE(inv::invoke(&obj_t::value_c, o) == 42);
|
||||
REQUIRE(inv::invoke(&obj_t::value_c, &o) == 42);
|
||||
REQUIRE(inv::invoke(&obj_t::value_c, std::ref(o)) == 42);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("invoke_result"){
|
||||
SECTION("invoke_result_functions"){
|
||||
static_assert(
|
||||
std::is_same<
|
||||
void,
|
||||
inv::invoke_result_t<decltype(simple_static_function)>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
std::is_same<
|
||||
int,
|
||||
inv::invoke_result_t<decltype(simple_static_function_r)>>::value,
|
||||
"unit test fail");
|
||||
}
|
||||
SECTION("invoke_result_members"){
|
||||
static_assert(
|
||||
std::is_same<void,
|
||||
inv::invoke_result_t<decltype(&obj_t::member), obj_t>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
std::is_same<void,
|
||||
inv::invoke_result_t<decltype(&obj_t::member), obj_t*>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
std::is_same<void,
|
||||
inv::invoke_result_t<decltype(&obj_t::member), std::reference_wrapper<obj_t>>>::value,
|
||||
"unit test fail");
|
||||
|
||||
static_assert(
|
||||
std::is_same<int,
|
||||
inv::invoke_result_t<decltype(&obj_t::member_r), obj_t>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
std::is_same<int,
|
||||
inv::invoke_result_t<decltype(&obj_t::member_r), obj_t*>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
std::is_same<int,
|
||||
inv::invoke_result_t<decltype(&obj_t::member_r), std::reference_wrapper<obj_t>>>::value,
|
||||
"unit test fail");
|
||||
|
||||
static_assert(
|
||||
std::is_same<int,
|
||||
inv::invoke_result_t<decltype(&obj_t::member_r_with_arg), obj_t, int>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
std::is_same<int,
|
||||
inv::invoke_result_t<decltype(&obj_t::member_r_with_arg), obj_t*, int>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
std::is_same<int,
|
||||
inv::invoke_result_t<decltype(&obj_t::member_r_with_arg), std::reference_wrapper<obj_t>, int>>::value,
|
||||
"unit test fail");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("apply"){
|
||||
SECTION("apply_functions"){
|
||||
inv::apply(simple_static_function, std::make_tuple());
|
||||
REQUIRE(inv::apply(simple_static_function_r, std::make_tuple()) == 42);
|
||||
REQUIRE(inv::apply(simple_static_function_r_with_arg, std::make_tuple(42)) == 42);
|
||||
{
|
||||
int v = 42;
|
||||
REQUIRE(&inv::apply(simple_static_function_r_with_ref_arg, std::make_tuple(std::ref(v))) == &v);
|
||||
}
|
||||
}
|
||||
SECTION("apply_members"){
|
||||
obj_t o;
|
||||
|
||||
inv::apply(&obj_t::member, std::make_tuple(o));
|
||||
inv::apply(&obj_t::member, std::make_tuple(&o));
|
||||
inv::apply(&obj_t::member, std::make_tuple(std::ref(o)));
|
||||
|
||||
REQUIRE(inv::apply(&obj_t::member_r, std::make_tuple(o)) == 42);
|
||||
REQUIRE(inv::apply(&obj_t::member_r, std::make_tuple(&o)) == 42);
|
||||
REQUIRE(inv::apply(&obj_t::member_r, std::make_tuple(std::ref(o))) == 42);
|
||||
|
||||
REQUIRE(inv::apply(&obj_t::member_r_with_arg, std::make_tuple(o, 42)) == 42);
|
||||
REQUIRE(inv::apply(&obj_t::member_r_with_arg, std::make_tuple(&o, 42)) == 42);
|
||||
REQUIRE(inv::apply(&obj_t::member_r_with_arg, std::make_tuple(std::ref(o), 42)) == 42);
|
||||
|
||||
{
|
||||
int v = 42;
|
||||
REQUIRE(&inv::apply(&obj_t::member_r_with_ref_arg, std::make_tuple(o, std::ref(v))) == &v);
|
||||
REQUIRE(&inv::apply(&obj_t::member_r_with_ref_arg, std::make_tuple(&o, std::ref(v))) == &v);
|
||||
REQUIRE(&inv::apply(&obj_t::member_r_with_ref_arg, std::make_tuple(std::ref(o), std::ref(v))) == &v);
|
||||
}
|
||||
}
|
||||
SECTION("apply_member_objects"){
|
||||
obj_t o;
|
||||
|
||||
REQUIRE(inv::apply(&obj_t::value, std::make_tuple(o)) == 42);
|
||||
REQUIRE(inv::apply(&obj_t::value, std::make_tuple(&o)) == 42);
|
||||
REQUIRE(inv::apply(&obj_t::value, std::make_tuple(std::ref(o))) == 42);
|
||||
|
||||
REQUIRE(inv::apply(&obj_t::value_c, std::make_tuple(o)) == 42);
|
||||
REQUIRE(inv::apply(&obj_t::value_c, std::make_tuple(&o)) == 42);
|
||||
REQUIRE(inv::apply(&obj_t::value_c, std::make_tuple(std::ref(o))) == 42);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user