Class type_info

Synopsis

#include <src/entt/core/type_info.hpp>

class type_info final

Description

Implementation specific information about a type.

Mentioned in

Methods

type_info overloadDefault constructor.
type_info overloadDefault copy constructor.
type_info overloadDefault move constructor.
type_info overloadCreates a type info object for a given type.
hashType hash.
nameType name.
operator boolChecks if a type info object is properly initialized.
operator= overloadDefault copy assignment operator.
operator= overloadDefault move assignment operator.
operator==Compares the contents of two type info objects.
seqType sequential identifier.

Source

Lines 151-259 in src/entt/core/type_info.hpp.

class type_info final {
    template<typename>
    friend type_info type_id() ENTT_NOEXCEPT;

    enum class operation { SEQ, HASH, NAME };

    using vtable_type = id_type(const operation, void *);

    template<typename Type>
    static id_type basic_vtable(const operation op, void *to) {
        static_assert(std::is_same_v<std::remove_reference_t<std::remove_const_t<Type>>, Type>, "Invalid type");

        switch(op) {
        case operation::SEQ:
            return type_seq<Type>::value();
            break;
        case operation::HASH:
            return type_hash<Type>::value();
            break;
        case operation::NAME:
            *static_cast<std::string_view *>(to) = type_name<Type>::value();
            break;
        }

        return {};
    }

public:
    /*! @brief Default constructor. */
    constexpr type_info() ENTT_NOEXCEPT
        : vtable{}
    {}

    /*! @brief Default copy constructor. */
    type_info(const type_info &) ENTT_NOEXCEPT = default;
    /*! @brief Default move constructor. */
    type_info(type_info &&) ENTT_NOEXCEPT = default;

    /**
     * @brief Creates a type info object for a given type.
     * @tparam Type Type for which to generate a type info object.
     */
    template<typename Type>
    type_info(std::in_place_type_t<Type>) ENTT_NOEXCEPT
        : vtable{&basic_vtable<Type>}
    {}

    /**
     * @brief Default copy assignment operator.
     * @return This type info object.
     */
    type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;

    /**
     * @brief Default move assignment operator.
     * @return This type info object.
     */
    type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;

    /**
     * @brief Checks if a type info object is properly initialized.
     * @return True if the object is properly initialized, false otherwise.
     */
    [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
        return vtable != nullptr;
    }

    /**
     * @brief Type sequential identifier.
     * @return Type sequential identifier.
     */
    [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
        return vtable ? vtable(operation::SEQ, nullptr) : id_type{};
    }

    /**
     * @brief Type hash.
     * @return Type hash.
     */
    [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
        return vtable ? vtable(operation::HASH, nullptr) : id_type{};
    }

    /**
     * @brief Type name.
     * @return Type name.
     */
    [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
        std::string_view value{};

        if(vtable) {
            vtable(operation::NAME, &value);
        }

        return value;
    }

    /**
     * @brief Compares the contents of two type info objects.
     * @param other Object with which to compare.
     * @return False if the two contents differ, true otherwise.
     */
    [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
        return hash() == other.hash();
    }

private:
    vtable_type *vtable;
};





Add Discussion

Log in to comment