File: Reference.h

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (136 lines) | stat: -rw-r--r-- 4,656 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
//             2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024
//           Vladimír Vondruš <mosra@centrum.cz> and contributors
// Copyright © 2020-2024 Dan R.
//
// 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.

#pragma once

#include <type_traits>

namespace Death { namespace Containers {
//###==##====#=====--==~--~=~- --- -- -  -  -   -

	namespace Implementation
	{
		template<class, class> struct ReferenceConverter;
	}

	/**
		@brief Lightweight non-owning reference wrapper

		Equivalent to @ref std::reference_wrapper from C++11, provides a copyable non-owning wrapper over
		l-value references to allow storing them in containers. Unlike @ref std::reference_wrapper, this class
		does not provide @cpp operator() @ce and there are no equivalents to @ref std::ref() / @ref std::cref()
		as they are not deemed necessary --- in most contexts where @ref Reference is used, passing a plain reference
		works just as well. This class is trivially copyable (@ref std::reference_wrapper is guaranteed to be so
		only since C++17) and also works on incomplete types, which @ref std::reference_wrapper knows since C++20.

		This class is exclusively for l-value references.
	*/
	template<class T> class Reference
	{
	public:
		/** @brief Constructor */
		constexpr /*implicit*/ Reference(T& reference) noexcept : _reference{&reference} {}

		/**
		 * @brief Construct a reference from external representation
		 */
		template<class U, class = decltype(Implementation::ReferenceConverter<T, U>::from(std::declval<U>()))> constexpr /*implicit*/ Reference(U other) noexcept : Reference{Implementation::ReferenceConverter<T, U>::from(other)} { }

		/**
		 * @brief Construction from r-value references is not allowed
		 */
		Reference(T&&) = delete;

		/**
		 * @brief Construct a reference from another of a derived type
		 *
		 * Expects that @p T is a base of @p U.
		 */
		template<class U
#ifndef DOXYGEN_GENERATING_OUTPUT
			, typename std::enable_if<std::is_base_of<T, U>::value, int>::type = 0
#endif
		> constexpr /*implicit*/ Reference(Reference<U> other) noexcept : _reference{other._reference} { }

		/**
		 * @brief Convert the reference to external representation
		 */
		template<class U, class = decltype(Implementation::ReferenceConverter<T, U>::to(std::declval<Reference<T>>()))> constexpr /*implicit*/ operator U() const
		{
			return Implementation::ReferenceConverter<T, U>::to(*this);
		}

		/** @brief Underlying reference */
		constexpr /*implicit*/ operator T& () const
		{
			return *_reference;
		}
		/** @overload */
		constexpr /*implicit*/ operator Reference<const T>() const
		{
			return *_reference;
		}

		/** @brief Underlying reference */
		constexpr T& get() const
		{
			return *_reference;
		}

		/**
		 * @brief Access the underlying reference
		 */
		constexpr T* operator->() const
		{
			return _reference;
		}

		/**
		 * @brief Access the underlying reference
		 */
		constexpr T& operator*() const
		{
			return *_reference;
		}

	private:
		// For the conversion constructor
		template<class U> friend class Reference;

		T* _reference;
	};

	/** @relatesalso Reference
		@brief Make a reference wrapper

		Convenience alternative to @ref Reference::Reference(T&).

		Useful for example when iterating a list of variables that need to be modified
		in-place, and where the code would otherwise have to be extra verbose or would
		fall back to raw pointers and risk them getting @cpp nullptr @ce.
	*/
	template<class T> constexpr Reference<T> reference(T& reference) {
		return Reference<T>{reference};
	}

}}