Function meta_setter

Synopsis

#include <src/entt/meta/utility.hpp>

template <typename Type, auto Data>
bool meta_setter([[maybe_unused]] meta_handle instance, [[maybe_unused]] meta_any value)

Description

Sets the value of a given variable.

Template Parameters

Type - Reflected type to which the variable is associated.

Data - The actual variable to set.

Parameters

instance - An opaque instance of the underlying type, if required.

value - Parameter to use to set the variable.

Returns
True in case of success, false otherwise.

Source

Lines 166-205 in src/entt/meta/utility.hpp.

template<typename Type, auto Data>
[[nodiscard]] bool meta_setter([[maybe_unused]] meta_handle instance, [[maybe_unused]] meta_any value) {
    if constexpr(!std::is_same_v<decltype(Data), Type> && !std::is_same_v<decltype(Data), std::nullptr_t>) {
        if constexpr(std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>>) {
            using data_type = type_list_element_t<1u, typename meta_function_helper_t<Type, decltype(Data)>::args_type>;

            if(auto * const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
                Data(*clazz, value.cast<data_type>());
                return true;
            }
        } else if constexpr(std::is_member_function_pointer_v<decltype(Data)>) {
            using data_type = type_list_element_t<0u, typename meta_function_helper_t<Type, decltype(Data)>::args_type>;

            if(auto * const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
                (clazz->*Data)(value.cast<data_type>());
                return true;
            }
        } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
            using data_type = std::remove_reference_t<decltype(std::declval<Type>().*Data)>;

            if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
                if(auto * const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
                    clazz->*Data = value.cast<data_type>();
                    return true;
                }
            }
        } else {
            using data_type = std::remove_reference_t<decltype(*Data)>;

            if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
                if(value.allow_cast<data_type>()) {
                    *Data = value.cast<data_type>();
                    return true;
                }
            }
        }
    }

    return false;
}





Add Discussion

Log in to comment