/* * Copyright (c) 2021-2022, Matthew Olsson * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #ifdef PDF_DEBUG namespace { template char const* object_name() { # define ENUMERATE_TYPE(class_name, snake_name) \ if constexpr (IsSame) { \ return #class_name; \ } ENUMERATE_OBJECT_TYPES(ENUMERATE_TYPE) # undef ENUMERATE_TYPE VERIFY_NOT_REACHED(); } } #endif namespace PDF { class Object : public RefCounted { public: virtual ~Object() = default; [[nodiscard]] ALWAYS_INLINE u32 generation_index() const { return m_generation_index; } ALWAYS_INLINE void set_generation_index(u32 generation_index) { m_generation_index = generation_index; } template bool is() const requires(!IsSame) { #define ENUMERATE_TYPE(class_name, snake_name) \ if constexpr (IsSame) { \ return is_##snake_name(); \ } ENUMERATE_OBJECT_TYPES(ENUMERATE_TYPE) #undef ENUMERATE_TYPE VERIFY_NOT_REACHED(); } template [[nodiscard]] ALWAYS_INLINE NonnullRefPtr cast( #ifdef PDF_DEBUG SourceLocation loc = SourceLocation::current() #endif ) const requires(!IsSame) { #ifdef PDF_DEBUG if (!is()) { dbgln("{} invalid cast from {} to {}", loc, type_name(), object_name()); VERIFY_NOT_REACHED(); } #endif return NonnullRefPtr(static_cast(*this)); } virtual char const* type_name() const = 0; virtual DeprecatedString to_deprecated_string(int indent) const = 0; protected: #define ENUMERATE_TYPE(_, name) \ virtual bool is_##name() const \ { \ return false; \ } ENUMERATE_OBJECT_TYPES(ENUMERATE_TYPE) #undef ENUMERATE_TYPE private: u32 m_generation_index { 0 }; }; } namespace AK { template struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, T const& object) { return Formatter::format(builder, object.to_deprecated_string(0)); } }; template struct Formatter> : Formatter { ErrorOr format(FormatBuilder& builder, NonnullRefPtr const& object) { return Formatter::format(builder, *object); } }; }