source: mainline/uspace/lib/cpp/include/internal/functional/invoke.hpp@ c866a83

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c866a83 was c866a83, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: split too big files into smaller (loosely related) sub files

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_INTERNAL_FUNCTIONAL_INVOKE
30#define LIBCPP_INTERNAL_FUNCTIONAL_INVOKE
31
32#include <internal/utility/declval.hpp>
33#include <internal/utility/forward_move.hpp>
34#include <type_traits>
35
36namespace std::aux
37{
38 /**
39 * 20.9.2, requirements:
40 */
41
42 template<class R, class T, class T1, class... Ts>
43 decltype(auto) invoke(R T::* f, T1&& t1, Ts&&... args)
44 {
45 if constexpr (is_member_function_pointer_v<decltype(f)>)
46 {
47 if constexpr (is_base_of_v<T, remove_reference_t<T1>>)
48 // (1.1)
49 return (t1.*f)(forward<Ts>(args)...);
50 else
51 // (1.2)
52 return ((*t1).*f)(forward<Ts>(args)...);
53 }
54 else if constexpr (is_member_object_pointer_v<decltype(f)> && sizeof...(args) == 0)
55 {
56 /**
57 * Note: Standard requires to N be equal to 1, but we take t1 directly
58 * so we need sizeof...(args) to be 0.
59 */
60 if constexpr (is_base_of_v<T, remove_reference_t<T1>>)
61 // (1.3)
62 return t1.*f;
63 else
64 // (1.4)
65 return (*t1).*f;
66 }
67
68 /**
69 * Note: If this condition holds this will not be reachable,
70 * but a new addition to the standard (17.7 point (8.1))
71 * prohibits us from simply using false as the condition here,
72 * so we use this because we know it is false here.
73 */
74 static_assert(is_member_function_pointer_v<decltype(f)>, "invalid invoke");
75 }
76
77 template<class F, class... Args>
78 decltype(auto) invoke(F&& f, Args&&... args)
79 {
80 // (1.5)
81 return f(forward<Args>(args)...);
82 }
83}
84
85#endif
Note: See TracBrowser for help on using the repository browser.