|
| 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 [[nodiscard]] RequiredPtr { |
| 37 | + T *_value = nullptr; |
| 38 | + |
| 39 | + RequiredPtr(std::nullptr_t) {} |
| 40 | + |
| 41 | +public: |
| 42 | + using element_type = T; |
| 43 | + |
| 44 | + [[nodiscard, deprecated("Should not be called directly; only used in EXTRACT_REQUIRED_PTR_OR_FAIL* macros.")]] |
| 45 | + _FORCE_INLINE_ T *_internal_ptr() const { return _value; } |
| 46 | + [[nodiscard, deprecated("Should not be called directly; only used in EXTRACT_REQUIRED_PTR_OR_FAIL* macros.")]] |
| 47 | + _FORCE_INLINE_ bool _is_null() const { return _value == nullptr; } |
| 48 | + |
| 49 | + [[nodiscard, deprecated("Should not be called directly; only used internally.")]] |
| 50 | + _FORCE_INLINE_ static RequiredPtr<T> _create_null() { return RequiredPtr<T>(nullptr); } |
| 51 | + |
| 52 | + RequiredPtr() = delete; |
| 53 | + |
| 54 | + RequiredPtr(const RequiredPtr &p_other) = default; |
| 55 | + RequiredPtr(RequiredPtr &&p_other) = default; |
| 56 | + RequiredPtr &operator=(const RequiredPtr &p_other) = default; |
| 57 | + RequiredPtr &operator=(RequiredPtr &&p_other) = default; |
| 58 | + |
| 59 | + GODOT_DEPRECATED_BEGIN |
| 60 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 61 | + _FORCE_INLINE_ RequiredPtr(const RequiredPtr<T_Other> &p_other) : |
| 62 | + _value(p_other._internal_ptr()) {} |
| 63 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 64 | + _FORCE_INLINE_ RequiredPtr &operator=(const RequiredPtr<T_Other> &p_other) { |
| 65 | + _value = p_other._internal_ptr(); |
| 66 | + return *this; |
| 67 | + } |
| 68 | + GODOT_DEPRECATED_END |
| 69 | + |
| 70 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 71 | + _FORCE_INLINE_ RequiredPtr(const T_Other *p_ptr) : |
| 72 | + _value(const_cast<std::remove_const_t<T_Other> *>(p_ptr)) {} |
| 73 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 74 | + _FORCE_INLINE_ RequiredPtr &operator=(const T_Other *p_ptr) { |
| 75 | + _value = const_cast<std::remove_const_t<T_Other> *>(p_ptr); |
| 76 | + return *this; |
| 77 | + } |
| 78 | + |
| 79 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 80 | + _FORCE_INLINE_ RequiredPtr(const Ref<T_Other> &p_ref) : |
| 81 | + _value(p_ref.ptr()) {} |
| 82 | + template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0> |
| 83 | + _FORCE_INLINE_ RequiredPtr &operator=(const Ref<T_Other> &p_ref) { |
| 84 | + _value = p_ref.ptr(); |
| 85 | + return *this; |
| 86 | + } |
| 87 | + |
| 88 | + _FORCE_INLINE_ RequiredPtr(const Variant &p_variant) : |
| 89 | + _value(static_cast<T *>(p_variant.get_validated_object())) {} |
| 90 | + _FORCE_INLINE_ RequiredPtr &operator=(const Variant &p_variant) { |
| 91 | + _value = static_cast<T *>(p_variant.get_validated_object()); |
| 92 | + return *this; |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +#define TMPL_EXTRACT_REQUIRED_PTR_OR_FAIL(m_name, m_param, m_retval, m_msg, m_editor) \ |
| 97 | + GODOT_DEPRECATED_BEGIN \ |
| 98 | + if (unlikely(m_param._is_null())) { \ |
| 99 | + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Required object \"" _STR(m_param) "\" is null.", m_msg, m_editor); \ |
| 100 | + return m_retval; \ |
| 101 | + } \ |
| 102 | + std::conditional_t< \ |
| 103 | + std::is_base_of_v<RefCounted, std::decay_t<decltype(m_param)>::element_type>, \ |
| 104 | + Ref<std::decay_t<decltype(m_param)>::element_type>, \ |
| 105 | + std::decay_t<decltype(m_param)>::element_type *> \ |
| 106 | + m_name = m_param._internal_ptr(); \ |
| 107 | + GODOT_DEPRECATED_END \ |
| 108 | + static_assert(true) |
| 109 | + |
| 110 | +#define EXTRACT_REQUIRED_PTR_OR_FAIL(m_name, m_param) TMPL_EXTRACT_REQUIRED_PTR_OR_FAIL(m_name, m_param, void(), "", false) |
| 111 | +#define EXTRACT_REQUIRED_PTR_OR_FAIL_MSG(m_name, m_param, m_msg) TMPL_EXTRACT_REQUIRED_PTR_OR_FAIL(m_name, m_param, void(), m_msg, false) |
| 112 | +#define EXTRACT_REQUIRED_PTR_OR_FAIL_EDMSG(m_name, m_param, m_msg) TMPL_EXTRACT_REQUIRED_PTR_OR_FAIL(m_name, m_param, void(), m_msg, true) |
| 113 | +#define EXTRACT_REQUIRED_PTR_OR_FAIL_V(m_name, m_param, m_retval) TMPL_EXTRACT_REQUIRED_PTR_OR_FAIL(m_name, m_param, m_retval, "", false) |
| 114 | +#define EXTRACT_REQUIRED_PTR_OR_FAIL_V_MSG(m_name, m_param, m_retval, m_msg) TMPL_EXTRACT_REQUIRED_PTR_OR_FAIL(m_name, m_param, m_retval, m_msg, false) |
| 115 | +#define EXTRACT_REQUIRED_PTR_OR_FAIL_V_EDMSG(m_name, m_param, m_retval, m_msg) TMPL_EXTRACT_REQUIRED_PTR_OR_FAIL(m_name, m_param, m_retval, m_msg, true) |
0 commit comments