|
| 1 | +/**************************************************************************/ |
| 2 | +/* required_ptr.h */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +#pragma once |
| 32 | + |
| 33 | +#include "core/variant/variant.h" |
| 34 | + |
| 35 | +template <typename T> |
| 36 | +class RequiredResult { |
| 37 | + static_assert(!is_fully_defined_v<T> || std::is_base_of_v<Object, T>, "T must be an Object subtype"); |
| 38 | + |
| 39 | +public: |
| 40 | + using element_type = T; |
| 41 | + using ptr_type = std::conditional_t<std::is_base_of_v<RefCounted, T>, Ref<T>, T *>; |
| 42 | + |
| 43 | +private: |
| 44 | + ptr_type _value = ptr_type(); |
| 45 | + |
| 46 | + _FORCE_INLINE_ RequiredResult() = default; |
| 47 | + |
| 48 | +public: |
| 49 | + RequiredResult(const RequiredResult &p_other) = default; |
| 50 | + RequiredResult(RequiredResult &&p_other) = default; |
| 51 | + RequiredResult &operator=(const RequiredResult &p_other) = default; |
| 52 | + RequiredResult &operator=(RequiredResult &&p_other) = default; |
| 53 | + |
| 54 | + _FORCE_INLINE_ RequiredResult(std::nullptr_t) : |
| 55 | + RequiredResult() {} |
| 56 | + _FORCE_INLINE_ RequiredResult &operator=(std::nullptr_t) { _value = nullptr; } |
| 57 | + |
| 58 | + // These functions should not be called directly, they are only for internal use. |
| 59 | + _FORCE_INLINE_ ptr_type _internal_ptr_dont_use() const { return _value; } |
| 60 | + _FORCE_INLINE_ static RequiredResult<T> _err_return_dont_use() { return RequiredResult<T>(); } |
| 61 | + |
| 62 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 63 | + _FORCE_INLINE_ RequiredResult(const RequiredResult<T_Other> &p_other) : |
| 64 | + _value(p_other._value) {} |
| 65 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 66 | + _FORCE_INLINE_ RequiredResult &operator=(const RequiredResult<T_Other> &p_other) { |
| 67 | + _value = p_other._value; |
| 68 | + return *this; |
| 69 | + } |
| 70 | + |
| 71 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 72 | + _FORCE_INLINE_ RequiredResult(T_Other *p_ptr) : |
| 73 | + _value(p_ptr) {} |
| 74 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 75 | + _FORCE_INLINE_ RequiredResult &operator=(T_Other *p_ptr) { |
| 76 | + _value = p_ptr; |
| 77 | + return *this; |
| 78 | + } |
| 79 | + |
| 80 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 81 | + _FORCE_INLINE_ RequiredResult(const Ref<T_Other> &p_ref) : |
| 82 | + _value(p_ref) {} |
| 83 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 84 | + _FORCE_INLINE_ RequiredResult &operator=(const Ref<T_Other> &p_ref) { |
| 85 | + _value = p_ref; |
| 86 | + return *this; |
| 87 | + } |
| 88 | + |
| 89 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 90 | + _FORCE_INLINE_ RequiredResult(Ref<T_Other> &&p_ref) : |
| 91 | + _value(std::move(p_ref)) {} |
| 92 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 93 | + _FORCE_INLINE_ RequiredResult &operator=(Ref<T_Other> &&p_ref) { |
| 94 | + _value = std::move(p_ref); |
| 95 | + return &this; |
| 96 | + } |
| 97 | + |
| 98 | + template <typename U = T, std::enable_if_t<std::is_base_of_v<RefCounted, U>, int> = 0> |
| 99 | + _FORCE_INLINE_ RequiredResult(const Variant &p_variant) : |
| 100 | + _value(static_cast<T *>(p_variant.get_validated_object())) {} |
| 101 | + template <typename U = T, std::enable_if_t<std::is_base_of_v<RefCounted, U>, int> = 0> |
| 102 | + _FORCE_INLINE_ RequiredResult &operator=(const Variant &p_variant) { |
| 103 | + _value = static_cast<T *>(p_variant.get_validated_object()); |
| 104 | + return *this; |
| 105 | + } |
| 106 | + |
| 107 | + template <typename U = T, std::enable_if_t<!std::is_base_of_v<RefCounted, U>, int> = 0> |
| 108 | + _FORCE_INLINE_ RequiredResult(const Variant &p_variant) : |
| 109 | + _value(static_cast<T *>(p_variant.operator Object *())) {} |
| 110 | + template <typename U = T, std::enable_if_t<!std::is_base_of_v<RefCounted, U>, int> = 0> |
| 111 | + _FORCE_INLINE_ RequiredResult &operator=(const Variant &p_variant) { |
| 112 | + _value = static_cast<T *>(p_variant.operator Object *()); |
| 113 | + return *this; |
| 114 | + } |
| 115 | + |
| 116 | + template <typename U = T, std::enable_if_t<std::is_base_of_v<RefCounted, U>, int> = 0> |
| 117 | + _FORCE_INLINE_ element_type *ptr() const { |
| 118 | + return *_value; |
| 119 | + } |
| 120 | + |
| 121 | + template <typename U = T, std::enable_if_t<!std::is_base_of_v<RefCounted, U>, int> = 0> |
| 122 | + _FORCE_INLINE_ element_type *ptr() const { |
| 123 | + return _value; |
| 124 | + } |
| 125 | + |
| 126 | + _FORCE_INLINE_ operator ptr_type() { |
| 127 | + return _value; |
| 128 | + } |
| 129 | + |
| 130 | + _FORCE_INLINE_ operator Variant() const { |
| 131 | + return Variant(_value); |
| 132 | + } |
| 133 | + |
| 134 | + _FORCE_INLINE_ element_type *operator*() const { |
| 135 | + return ptr(); |
| 136 | + } |
| 137 | + |
| 138 | + _FORCE_INLINE_ element_type *operator->() const { |
| 139 | + return ptr(); |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +template <typename T> |
| 144 | +class RequiredParam { |
| 145 | + static_assert(!is_fully_defined_v<T> || std::is_base_of_v<Object, T>, "T must be an Object subtype"); |
| 146 | + |
| 147 | +public: |
| 148 | + using element_type = T; |
| 149 | + using ptr_type = std::conditional_t<std::is_base_of_v<RefCounted, T>, Ref<T>, T *>; |
| 150 | + |
| 151 | +private: |
| 152 | + ptr_type _value; |
| 153 | + |
| 154 | + _FORCE_INLINE_ RequiredParam() { |
| 155 | + if constexpr (!std::is_base_of_v<RefCounted, T>) { |
| 156 | + _value = nullptr; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +public: |
| 161 | + // These functions should not be called directly, they are only for internal use. |
| 162 | + _FORCE_INLINE_ ptr_type _internal_ptr_dont_use() const { return _value; } |
| 163 | + _FORCE_INLINE_ bool _is_null_dont_use() const { |
| 164 | + if constexpr (std::is_base_of_v<RefCounted, T>) { |
| 165 | + return _value.is_null(); |
| 166 | + } else { |
| 167 | + return _value == nullptr; |
| 168 | + } |
| 169 | + } |
| 170 | + _FORCE_INLINE_ static RequiredParam<T> _err_return_dont_use() { return RequiredParam<T>(); } |
| 171 | + |
| 172 | + // Prevent erroneously assigning null values by explicitly removing nullptr constructor/assignment. |
| 173 | + RequiredParam(std::nullptr_t) = delete; |
| 174 | + RequiredParam &operator=(std::nullptr_t) = delete; |
| 175 | + |
| 176 | + RequiredParam(const RequiredParam &p_other) = default; |
| 177 | + RequiredParam(RequiredParam &&p_other) = default; |
| 178 | + RequiredParam &operator=(const RequiredParam &p_other) = default; |
| 179 | + RequiredParam &operator=(RequiredParam &&p_other) = default; |
| 180 | + |
| 181 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 182 | + _FORCE_INLINE_ RequiredParam(const RequiredParam<T_Other> &p_other) : |
| 183 | + _value(p_other._internal_ptr_dont_use()) {} |
| 184 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 185 | + _FORCE_INLINE_ RequiredParam &operator=(const RequiredParam<T_Other> &p_other) { |
| 186 | + _value = p_other._internal_ptr_dont_use(); |
| 187 | + return *this; |
| 188 | + } |
| 189 | + |
| 190 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 191 | + _FORCE_INLINE_ RequiredParam(T_Other *p_ptr) : |
| 192 | + _value(p_ptr) {} |
| 193 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 194 | + _FORCE_INLINE_ RequiredParam &operator=(T_Other *p_ptr) { |
| 195 | + _value = p_ptr; |
| 196 | + return *this; |
| 197 | + } |
| 198 | + |
| 199 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 200 | + _FORCE_INLINE_ RequiredParam(const Ref<T_Other> &p_ref) : |
| 201 | + _value(p_ref) {} |
| 202 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 203 | + _FORCE_INLINE_ RequiredParam &operator=(const Ref<T_Other> &p_ref) { |
| 204 | + _value = p_ref; |
| 205 | + return *this; |
| 206 | + } |
| 207 | + |
| 208 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 209 | + _FORCE_INLINE_ RequiredParam(Ref<T_Other> &&p_ref) : |
| 210 | + _value(std::move(p_ref)) {} |
| 211 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 212 | + _FORCE_INLINE_ RequiredParam &operator=(Ref<T_Other> &&p_ref) { |
| 213 | + _value = std::move(p_ref); |
| 214 | + return &this; |
| 215 | + } |
| 216 | + |
| 217 | + template <typename U = T, std::enable_if_t<std::is_base_of_v<RefCounted, U>, int> = 0> |
| 218 | + _FORCE_INLINE_ RequiredParam(const Variant &p_variant) : |
| 219 | + _value(static_cast<T *>(p_variant.get_validated_object())) {} |
| 220 | + template <typename U = T, std::enable_if_t<std::is_base_of_v<RefCounted, U>, int> = 0> |
| 221 | + _FORCE_INLINE_ RequiredParam &operator=(const Variant &p_variant) { |
| 222 | + _value = static_cast<T *>(p_variant.get_validated_object()); |
| 223 | + return *this; |
| 224 | + } |
| 225 | + |
| 226 | + template <typename U = T, std::enable_if_t<!std::is_base_of_v<RefCounted, U>, int> = 0> |
| 227 | + _FORCE_INLINE_ RequiredParam(const Variant &p_variant) : |
| 228 | + _value(static_cast<T *>(p_variant.operator Object *())) {} |
| 229 | + template <typename U = T, std::enable_if_t<!std::is_base_of_v<RefCounted, U>, int> = 0> |
| 230 | + _FORCE_INLINE_ RequiredParam &operator=(const Variant &p_variant) { |
| 231 | + _value = static_cast<T *>(p_variant.operator Object *()); |
| 232 | + return *this; |
| 233 | + } |
| 234 | +}; |
| 235 | + |
| 236 | +#define TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, m_retval, m_msg, m_editor) \ |
| 237 | + if (unlikely(m_param._is_null_dont_use())) { \ |
| 238 | + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Required object \"" _STR(m_param) "\" is null.", m_msg, m_editor); \ |
| 239 | + return m_retval; \ |
| 240 | + } \ |
| 241 | + typename std::decay_t<decltype(m_param)>::ptr_type m_name = m_param._internal_ptr_dont_use(); \ |
| 242 | + static_assert(true) |
| 243 | + |
| 244 | +// These macros are equivalent to the ERR_FAIL_NULL*() family of macros, only for RequiredParam<T> instead of raw pointers. |
| 245 | +#define EXTRACT_PARAM_OR_FAIL(m_name, m_param) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, void(), "", false) |
| 246 | +#define EXTRACT_PARAM_OR_FAIL_MSG(m_name, m_param, m_msg) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, void(), m_msg, false) |
| 247 | +#define EXTRACT_PARAM_OR_FAIL_EDMSG(m_name, m_param, m_msg) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, void(), m_msg, true) |
| 248 | +#define EXTRACT_PARAM_OR_FAIL_V(m_name, m_param, m_retval) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, m_retval, "", false) |
| 249 | +#define EXTRACT_PARAM_OR_FAIL_V_MSG(m_name, m_param, m_retval, m_msg) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, m_retval, m_msg, false) |
| 250 | +#define EXTRACT_PARAM_OR_FAIL_V_EDMSG(m_name, m_param, m_retval, m_msg) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, m_retval, m_msg, true) |
0 commit comments