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_BITS_FUNCTIONAL_REFERENCE_WRAPPER
|
---|
30 | #define LIBCPP_BITS_FUNCTIONAL_REFERENCE_WRAPPER
|
---|
31 |
|
---|
32 | #include <__bits/functional/conditional_function_typedefs.hpp>
|
---|
33 | #include <__bits/functional/invoke.hpp>
|
---|
34 | #include <type_traits>
|
---|
35 |
|
---|
36 | namespace std
|
---|
37 | {
|
---|
38 | /**
|
---|
39 | * 20.9.4, reference_wrapper:
|
---|
40 | */
|
---|
41 |
|
---|
42 | template<class T>
|
---|
43 | class reference_wrapper
|
---|
44 | : public aux::conditional_function_typedefs<remove_cv_t<remove_reference_t<T>>>
|
---|
45 | {
|
---|
46 | public:
|
---|
47 | using type = T;
|
---|
48 |
|
---|
49 | reference_wrapper(type& val) noexcept
|
---|
50 | : data_{&val}
|
---|
51 | { /* DUMMY BODY */ }
|
---|
52 |
|
---|
53 | reference_wrapper(type&&) = delete;
|
---|
54 |
|
---|
55 | reference_wrapper(const reference_wrapper& other) noexcept
|
---|
56 | : data_{other.data_}
|
---|
57 | { /* DUMMY BODY */ }
|
---|
58 |
|
---|
59 | reference_wrapper& operator=(const reference_wrapper& other) noexcept
|
---|
60 | {
|
---|
61 | data_ = other.data_;
|
---|
62 |
|
---|
63 | return *this;
|
---|
64 | }
|
---|
65 |
|
---|
66 | operator type&() const noexcept
|
---|
67 | {
|
---|
68 | return *data_;
|
---|
69 | }
|
---|
70 |
|
---|
71 | type& get() const noexcept
|
---|
72 | {
|
---|
73 | return *data_;
|
---|
74 | }
|
---|
75 |
|
---|
76 | template<class... Args>
|
---|
77 | result_of_t<type&(Args&&...)> operator()(Args&&... args) const
|
---|
78 | {
|
---|
79 | return invoke(*data_, std::forward<Args>(args)...);
|
---|
80 | }
|
---|
81 |
|
---|
82 | private:
|
---|
83 | type* data_;
|
---|
84 | };
|
---|
85 |
|
---|
86 | template<class T>
|
---|
87 | reference_wrapper<T> ref(T& t) noexcept
|
---|
88 | {
|
---|
89 | return reference_wrapper<T>{t};
|
---|
90 | }
|
---|
91 |
|
---|
92 | template<class T>
|
---|
93 | reference_wrapper<const T> cref(const T& t) noexcept
|
---|
94 | {
|
---|
95 | return reference_wrapper<const T>{t};
|
---|
96 | }
|
---|
97 |
|
---|
98 | template<class T>
|
---|
99 | void ref(const T&&) = delete;
|
---|
100 |
|
---|
101 | template<class T>
|
---|
102 | void cref(const T&&) = delete;
|
---|
103 |
|
---|
104 | template<class T>
|
---|
105 | reference_wrapper<T> ref(reference_wrapper<T> t) noexcept
|
---|
106 | {
|
---|
107 | return ref(t.get());
|
---|
108 | }
|
---|
109 |
|
---|
110 | template<class T>
|
---|
111 | reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept
|
---|
112 | {
|
---|
113 | return cref(t.get());
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | #endif
|
---|