Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use _v shorthand for type traits and if constexpr where appropriate #87871

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions core/object/class_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class ClassDB {
template <class T>
static void register_class(bool p_virtual = false) {
GLOBAL_LOCK_FUNCTION;
static_assert(TypesAreSame<typename T::self_type, T>::value, "Class not declared properly, please use GDCLASS.");
static_assert(types_are_same_v<typename T::self_type, T>, "Class not declared properly, please use GDCLASS.");
T::initialize_class();
ClassInfo *t = classes.getptr(T::get_class_static());
ERR_FAIL_NULL(t);
Expand All @@ -203,7 +203,7 @@ class ClassDB {
template <class T>
static void register_abstract_class() {
GLOBAL_LOCK_FUNCTION;
static_assert(TypesAreSame<typename T::self_type, T>::value, "Class not declared properly, please use GDCLASS.");
static_assert(types_are_same_v<typename T::self_type, T>, "Class not declared properly, please use GDCLASS.");
T::initialize_class();
ClassInfo *t = classes.getptr(T::get_class_static());
ERR_FAIL_NULL(t);
Expand All @@ -216,7 +216,7 @@ class ClassDB {
template <class T>
static void register_internal_class() {
GLOBAL_LOCK_FUNCTION;
static_assert(TypesAreSame<typename T::self_type, T>::value, "Class not declared properly, please use GDCLASS.");
static_assert(types_are_same_v<typename T::self_type, T>, "Class not declared properly, please use GDCLASS.");
T::initialize_class();
ClassInfo *t = classes.getptr(T::get_class_static());
ERR_FAIL_NULL(t);
Expand All @@ -239,7 +239,7 @@ class ClassDB {
template <class T>
static void register_custom_instance_class() {
GLOBAL_LOCK_FUNCTION;
static_assert(TypesAreSame<typename T::self_type, T>::value, "Class not declared properly, please use GDCLASS.");
static_assert(types_are_same_v<typename T::self_type, T>, "Class not declared properly, please use GDCLASS.");
T::initialize_class();
ClassInfo *t = classes.getptr(T::get_class_static());
ERR_FAIL_NULL(t);
Expand Down Expand Up @@ -296,7 +296,7 @@ class ClassDB {
argptrs[i] = &args[i];
}
MethodBind *bind = create_method_bind(p_method);
if constexpr (std::is_same<typename member_function_traits<M>::return_type, Object *>::value) {
if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
bind->set_return_type_is_raw_object_ptr(true);
}
return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, false, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
Expand All @@ -311,7 +311,7 @@ class ClassDB {
}
MethodBind *bind = create_static_method_bind(p_method);
bind->set_instance_class(p_class);
if constexpr (std::is_same<typename member_function_traits<M>::return_type, Object *>::value) {
if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
bind->set_return_type_is_raw_object_ptr(true);
}
return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, false, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
Expand All @@ -325,7 +325,7 @@ class ClassDB {
argptrs[i] = &args[i];
}
MethodBind *bind = create_method_bind(p_method);
if constexpr (std::is_same<typename member_function_traits<M>::return_type, Object *>::value) {
if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
bind->set_return_type_is_raw_object_ptr(true);
}
return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, true, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
Expand All @@ -340,7 +340,7 @@ class ClassDB {
}
MethodBind *bind = create_static_method_bind(p_method);
bind->set_instance_class(p_class);
if constexpr (std::is_same<typename member_function_traits<M>::return_type, Object *>::value) {
if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
bind->set_return_type_is_raw_object_ptr(true);
}
return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, true, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
Expand All @@ -353,7 +353,7 @@ class ClassDB {
MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant);
ERR_FAIL_NULL_V(bind, nullptr);

if constexpr (std::is_same<typename member_function_traits<M>::return_type, Object *>::value) {
if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
bind->set_return_type_is_raw_object_ptr(true);
}
return _bind_vararg_method(bind, p_name, p_default_args, false);
Expand All @@ -366,7 +366,7 @@ class ClassDB {
MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant);
ERR_FAIL_NULL_V(bind, nullptr);

if constexpr (std::is_same<typename member_function_traits<M>::return_type, Object *>::value) {
if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
bind->set_return_type_is_raw_object_ptr(true);
}
return _bind_vararg_method(bind, p_name, p_default_args, true);
Expand Down
8 changes: 4 additions & 4 deletions core/os/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void memdelete(T *p_class) {
if (!predelete_handler(p_class)) {
return; // doesn't want to be deleted
}
if (!std::is_trivially_destructible<T>::value) {
if constexpr (!std::is_trivially_destructible_v<T>) {
p_class->~T();
}

Expand All @@ -117,7 +117,7 @@ void memdelete_allocator(T *p_class) {
if (!predelete_handler(p_class)) {
return; // doesn't want to be deleted
}
if (!std::is_trivially_destructible<T>::value) {
if constexpr (!std::is_trivially_destructible_v<T>) {
p_class->~T();
}

Expand Down Expand Up @@ -147,7 +147,7 @@ T *memnew_arr_template(size_t p_elements) {
ERR_FAIL_NULL_V(mem, failptr);
*(mem - 1) = p_elements;

if (!std::is_trivially_constructible<T>::value) {
if constexpr (!std::is_trivially_constructible_v<T>) {
T *elems = (T *)mem;

/* call operator new */
Expand All @@ -174,7 +174,7 @@ template <typename T>
void memdelete_arr(T *p_class) {
uint64_t *ptr = (uint64_t *)p_class;

if (!std::is_trivially_destructible<T>::value) {
if constexpr (!std::is_trivially_destructible_v<T>) {
uint64_t elem_count = *(ptr - 1);

for (uint64_t i = 0; i < elem_count; i++) {
Expand Down
8 changes: 4 additions & 4 deletions core/templates/cowdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void CowData<T>::_unref(void *p_data) {
}
// clean up

if (!std::is_trivially_destructible<T>::value) {
if constexpr (!std::is_trivially_destructible_v<T>) {
USize *count = _get_size();
T *data = (T *)(count + 1);

Expand Down Expand Up @@ -269,7 +269,7 @@ typename CowData<T>::USize CowData<T>::_copy_on_write() {
T *_data = (T *)(mem_new);

// initialize new elements
if (std::is_trivially_copyable<T>::value) {
if constexpr (std::is_trivially_copyable_v<T>) {
memcpy(mem_new, _ptr, current_size * sizeof(T));

} else {
Expand Down Expand Up @@ -335,7 +335,7 @@ Error CowData<T>::resize(Size p_size) {

// construct the newly created elements

if (!std::is_trivially_constructible<T>::value) {
if constexpr (!std::is_trivially_constructible_v<T>) {
for (Size i = *_get_size(); i < p_size; i++) {
memnew_placement(&_ptr[i], T);
}
Expand All @@ -346,7 +346,7 @@ Error CowData<T>::resize(Size p_size) {
*_get_size() = p_size;

} else if (p_size < current_size) {
if (!std::is_trivially_destructible<T>::value) {
if constexpr (!std::is_trivially_destructible_v<T>) {
// deinitialize no longer needed elements
for (USize i = p_size; i < *_get_size(); i++) {
T *t = &_ptr[i];
Expand Down
10 changes: 5 additions & 5 deletions core/templates/local_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class LocalVector {
CRASH_COND_MSG(!data, "Out of memory");
}

if constexpr (!std::is_trivially_constructible<T>::value && !force_trivial) {
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
memnew_placement(&data[count++], T(p_elem));
} else {
data[count++] = p_elem;
Expand All @@ -77,7 +77,7 @@ class LocalVector {
for (U i = p_index; i < count; i++) {
data[i] = data[i + 1];
}
if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) {
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
data[count].~T();
}
}
Expand All @@ -90,7 +90,7 @@ class LocalVector {
if (count > p_index) {
data[p_index] = data[count];
}
if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) {
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
data[count].~T();
}
}
Expand Down Expand Up @@ -133,7 +133,7 @@ class LocalVector {
_FORCE_INLINE_ U size() const { return count; }
void resize(U p_size) {
if (p_size < count) {
if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) {
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
for (U i = p_size; i < count; i++) {
data[i].~T();
}
Expand All @@ -145,7 +145,7 @@ class LocalVector {
data = (T *)memrealloc(data, capacity * sizeof(T));
CRASH_COND_MSG(!data, "Out of memory");
}
if constexpr (!std::is_trivially_constructible<T>::value && !force_trivial) {
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
for (U i = count; i < p_size; i++) {
memnew_placement(&data[i], T);
}
Expand Down
2 changes: 1 addition & 1 deletion core/templates/paged_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class PagedAllocator {

private:
void _reset(bool p_allow_unfreed) {
if (!p_allow_unfreed || !std::is_trivially_destructible<T>::value) {
if (!p_allow_unfreed || !std::is_trivially_destructible_v<T>) {
ERR_FAIL_COND(allocs_available < pages_allocated * page_size);
}
if (pages_allocated) {
Expand Down
10 changes: 5 additions & 5 deletions core/templates/paged_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class PagedArray {
uint32_t page = count >> page_size_shift;
uint32_t offset = count & page_size_mask;

if (!std::is_trivially_constructible<T>::value) {
if constexpr (!std::is_trivially_constructible_v<T>) {
memnew_placement(&page_data[page][offset], T(p_value));
} else {
page_data[page][offset] = p_value;
Expand All @@ -214,7 +214,7 @@ class PagedArray {
_FORCE_INLINE_ void pop_back() {
ERR_FAIL_COND(count == 0);

if (!std::is_trivially_destructible<T>::value) {
if constexpr (!std::is_trivially_destructible_v<T>) {
uint32_t page = (count - 1) >> page_size_shift;
uint32_t offset = (count - 1) & page_size_mask;
page_data[page][offset].~T();
Expand All @@ -237,7 +237,7 @@ class PagedArray {

void clear() {
//destruct if needed
if (!std::is_trivially_destructible<T>::value) {
if constexpr (!std::is_trivially_destructible_v<T>) {
for (uint64_t i = 0; i < count; i++) {
uint32_t page = i >> page_size_shift;
uint32_t offset = i & page_size_mask;
Expand Down Expand Up @@ -318,13 +318,13 @@ class PagedArray {
uint32_t to_copy = MIN(page_size - new_remainder, remainder);

for (uint32_t i = 0; i < to_copy; i++) {
if (!std::is_trivially_constructible<T>::value) {
if constexpr (!std::is_trivially_constructible_v<T>) {
memnew_placement(&dst_page[i + new_remainder], T(remainder_page[i + remainder - to_copy]));
} else {
dst_page[i + new_remainder] = remainder_page[i + remainder - to_copy];
}

if (!std::is_trivially_destructible<T>::value) {
if constexpr (!std::is_trivially_destructible_v<T>) {
remainder_page[i + remainder - to_copy].~T();
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/templates/safe_refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#define SAFE_NUMERIC_TYPE_PUN_GUARANTEES(m_type) \
static_assert(sizeof(SafeNumeric<m_type>) == sizeof(m_type)); \
static_assert(alignof(SafeNumeric<m_type>) == alignof(m_type)); \
static_assert(std::is_trivially_destructible<std::atomic<m_type>>::value);
static_assert(std::is_trivially_destructible_v<std::atomic<m_type>>);
#define SAFE_FLAG_TYPE_PUN_GUARANTEES \
static_assert(sizeof(SafeFlag) == sizeof(bool)); \
static_assert(alignof(SafeFlag) == alignof(bool));
Expand Down
8 changes: 4 additions & 4 deletions core/variant/binder_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ template <class T>
struct VariantCaster {
static _FORCE_INLINE_ T cast(const Variant &p_variant) {
using TStripped = std::remove_pointer_t<T>;
if constexpr (std::is_base_of<Object, TStripped>::value) {
if constexpr (std::is_base_of_v<Object, TStripped>) {
return Object::cast_to<TStripped>(p_variant);
} else {
return p_variant;
Expand All @@ -63,7 +63,7 @@ template <class T>
struct VariantCaster<T &> {
static _FORCE_INLINE_ T cast(const Variant &p_variant) {
using TStripped = std::remove_pointer_t<T>;
if constexpr (std::is_base_of<Object, TStripped>::value) {
if constexpr (std::is_base_of_v<Object, TStripped>) {
return Object::cast_to<TStripped>(p_variant);
} else {
return p_variant;
Expand All @@ -75,7 +75,7 @@ template <class T>
struct VariantCaster<const T &> {
static _FORCE_INLINE_ T cast(const Variant &p_variant) {
using TStripped = std::remove_pointer_t<T>;
if constexpr (std::is_base_of<Object, TStripped>::value) {
if constexpr (std::is_base_of_v<Object, TStripped>) {
return Object::cast_to<TStripped>(p_variant);
} else {
return p_variant;
Expand Down Expand Up @@ -226,7 +226,7 @@ template <typename T>
struct VariantObjectClassChecker {
static _FORCE_INLINE_ bool check(const Variant &p_variant) {
using TStripped = std::remove_pointer_t<T>;
if constexpr (std::is_base_of<Object, TStripped>::value) {
if constexpr (std::is_base_of_v<Object, TStripped>) {
Object *obj = p_variant;
return Object::cast_to<TStripped>(p_variant) || !obj;
} else {
Expand Down
12 changes: 4 additions & 8 deletions core/variant/type_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,10 @@ struct EnableIf<false, T> {
};

template <typename, typename>
struct TypesAreSame {
static bool const value = false;
};
inline constexpr bool types_are_same_v = false;

template <typename A>
struct TypesAreSame<A, A> {
static bool const value = true;
};
template <typename T>
inline constexpr bool types_are_same_v<T, T> = true;

template <typename B, typename D>
struct TypeInherits {
Expand All @@ -60,7 +56,7 @@ struct TypeInherits {
static char (&test(...))[2];

static bool const value = sizeof(test(get_d())) == sizeof(char) &&
!TypesAreSame<B volatile const, void volatile const>::value;
!types_are_same_v<B volatile const, void volatile const>;
};

namespace GodotTypeInfo {
Expand Down
6 changes: 3 additions & 3 deletions servers/rendering/storage/variant_converters.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ inline bool is_convertible_array(Variant::Type type) {
}

template <class, class = void>
struct is_vector_type : std::false_type {};
inline constexpr bool is_vector_type_v = false;

template <class T>
struct is_vector_type<T, std::void_t<decltype(T::AXIS_COUNT)>> : std::true_type {};
inline constexpr bool is_vector_type_v<T, std::void_t<decltype(T::AXIS_COUNT)>> = true;

template <typename T, typename P>
void convert_item_std140(const T &p_item, P *p_write, bool p_compact = false) {
Expand Down Expand Up @@ -274,7 +274,7 @@ Vector<P> convert_array_std140(const Variant &p_variant, [[maybe_unused]] bool p
const Variant &item = array.get(i);
P *offset = write + (i * elements);

if constexpr (is_vector_type<T>::value) {
if constexpr (is_vector_type_v<T>) {
const T &vec = convert_to_vector<T>(item, p_linear_color);
convert_item_std140<T, P>(vec, offset, true);
} else {
Expand Down
Loading