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