Object.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Debug.h>
  8. #include <AK/FlyString.h>
  9. #include <AK/Format.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/RefCounted.h>
  12. #include <AK/SourceLocation.h>
  13. #include <LibPDF/Forward.h>
  14. #include <LibPDF/Value.h>
  15. namespace PDF {
  16. class Object : public RefCounted<Object> {
  17. public:
  18. virtual ~Object() = default;
  19. [[nodiscard]] ALWAYS_INLINE u32 generation_index() const { return m_generation_index; }
  20. ALWAYS_INLINE void set_generation_index(u32 generation_index) { m_generation_index = generation_index; }
  21. #define DEFINE_ID(_, name) \
  22. virtual bool is_##name() const { return false; }
  23. ENUMERATE_OBJECT_TYPES(DEFINE_ID)
  24. #undef DEFINE_ID
  25. virtual const char* type_name() const = 0;
  26. virtual String to_string(int indent) const = 0;
  27. private:
  28. u32 m_generation_index { 0 };
  29. };
  30. class StringObject final : public Object {
  31. public:
  32. StringObject(String string, bool is_binary)
  33. : m_string(move(string))
  34. , m_is_binary(is_binary)
  35. {
  36. }
  37. ~StringObject() override = default;
  38. [[nodiscard]] ALWAYS_INLINE String const& string() const { return m_string; }
  39. [[nodiscard]] ALWAYS_INLINE bool is_binary() const { return m_is_binary; }
  40. ALWAYS_INLINE bool is_string() const override { return true; }
  41. ALWAYS_INLINE const char* type_name() const override { return "string"; }
  42. String to_string(int indent) const override;
  43. private:
  44. String m_string;
  45. bool m_is_binary;
  46. };
  47. class NameObject final : public Object {
  48. public:
  49. explicit NameObject(FlyString name)
  50. : m_name(move(name))
  51. {
  52. }
  53. ~NameObject() override = default;
  54. [[nodiscard]] ALWAYS_INLINE FlyString const& name() const { return m_name; }
  55. ALWAYS_INLINE bool is_name() const override { return true; }
  56. ALWAYS_INLINE const char* type_name() const override { return "name"; }
  57. String to_string(int indent) const override;
  58. private:
  59. FlyString m_name;
  60. };
  61. class ArrayObject final : public Object {
  62. public:
  63. explicit ArrayObject(Vector<Value> elements)
  64. : m_elements(move(elements))
  65. {
  66. }
  67. ~ArrayObject() override = default;
  68. [[nodiscard]] ALWAYS_INLINE size_t size() const { return m_elements.size(); }
  69. [[nodiscard]] ALWAYS_INLINE Vector<Value> elements() const { return m_elements; }
  70. ALWAYS_INLINE auto begin() const { return m_elements.begin(); }
  71. ALWAYS_INLINE auto end() const { return m_elements.end(); }
  72. ALWAYS_INLINE Value const& operator[](size_t index) const { return at(index); }
  73. ALWAYS_INLINE Value const& at(size_t index) const { return m_elements[index]; }
  74. NonnullRefPtr<Object> get_object_at(Document*, size_t index) const;
  75. #define DEFINE_INDEXER(class_name, snake_name) \
  76. NonnullRefPtr<class_name> get_##snake_name##_at(Document*, size_t index) const;
  77. ENUMERATE_OBJECT_TYPES(DEFINE_INDEXER)
  78. #undef DEFINE_INDEXER
  79. ALWAYS_INLINE bool is_array() const override
  80. {
  81. return true;
  82. }
  83. ALWAYS_INLINE const char* type_name() const override { return "array"; }
  84. String to_string(int indent) const override;
  85. private:
  86. Vector<Value> m_elements;
  87. };
  88. class DictObject final : public Object {
  89. public:
  90. explicit DictObject(HashMap<FlyString, Value> map)
  91. : m_map(move(map))
  92. {
  93. }
  94. ~DictObject() override = default;
  95. [[nodiscard]] ALWAYS_INLINE HashMap<FlyString, Value> const& map() const { return m_map; }
  96. template<typename... Args>
  97. bool contains(Args&&... keys) const { return (m_map.contains(keys) && ...); }
  98. ALWAYS_INLINE Optional<Value> get(FlyString const& key) const { return m_map.get(key); }
  99. Value get_value(FlyString const& key) const
  100. {
  101. auto value = get(key);
  102. VERIFY(value.has_value());
  103. return value.value();
  104. }
  105. NonnullRefPtr<Object> get_object(Document*, FlyString const& key) const;
  106. #define DEFINE_GETTER(class_name, snake_name) \
  107. NonnullRefPtr<class_name> get_##snake_name(Document*, FlyString const& key) const;
  108. ENUMERATE_OBJECT_TYPES(DEFINE_GETTER)
  109. #undef DEFINE_GETTER
  110. ALWAYS_INLINE bool is_dict() const override
  111. {
  112. return true;
  113. }
  114. ALWAYS_INLINE const char* type_name() const override { return "dict"; }
  115. String to_string(int indent) const override;
  116. private:
  117. HashMap<FlyString, Value> m_map;
  118. };
  119. class StreamObject : public Object {
  120. public:
  121. explicit StreamObject(NonnullRefPtr<DictObject> const& dict)
  122. : m_dict(dict)
  123. {
  124. }
  125. virtual ~StreamObject() override = default;
  126. [[nodiscard]] ALWAYS_INLINE NonnullRefPtr<DictObject> dict() const { return m_dict; }
  127. [[nodiscard]] virtual ReadonlyBytes bytes() const = 0;
  128. ALWAYS_INLINE bool is_stream() const override { return true; }
  129. ALWAYS_INLINE const char* type_name() const override { return "stream"; }
  130. String to_string(int indent) const override;
  131. private:
  132. NonnullRefPtr<DictObject> m_dict;
  133. };
  134. class PlainTextStreamObject final : public StreamObject {
  135. public:
  136. PlainTextStreamObject(NonnullRefPtr<DictObject> const& dict, ReadonlyBytes const& bytes)
  137. : StreamObject(dict)
  138. , m_bytes(bytes)
  139. {
  140. }
  141. virtual ~PlainTextStreamObject() override = default;
  142. [[nodiscard]] ALWAYS_INLINE virtual ReadonlyBytes bytes() const override { return m_bytes; }
  143. private:
  144. ReadonlyBytes m_bytes;
  145. };
  146. class EncodedStreamObject final : public StreamObject {
  147. public:
  148. EncodedStreamObject(NonnullRefPtr<DictObject> const& dict, ByteBuffer&& buffer)
  149. : StreamObject(dict)
  150. , m_buffer(buffer)
  151. {
  152. }
  153. virtual ~EncodedStreamObject() override = default;
  154. [[nodiscard]] ALWAYS_INLINE virtual ReadonlyBytes bytes() const override { return m_buffer.bytes(); }
  155. private:
  156. ByteBuffer m_buffer;
  157. };
  158. class IndirectValue final : public Object {
  159. public:
  160. IndirectValue(u32 index, u32 generation_index, Value const& value)
  161. : m_index(index)
  162. , m_value(value)
  163. {
  164. set_generation_index(generation_index);
  165. }
  166. ~IndirectValue() override = default;
  167. [[nodiscard]] ALWAYS_INLINE u32 index() const { return m_index; }
  168. [[nodiscard]] ALWAYS_INLINE Value const& value() const { return m_value; }
  169. ALWAYS_INLINE bool is_indirect_value() const override { return true; }
  170. ALWAYS_INLINE const char* type_name() const override { return "indirect_object"; }
  171. String to_string(int indent) const override;
  172. private:
  173. u32 m_index;
  174. Value m_value;
  175. };
  176. template<IsObject To, IsObject From>
  177. [[nodiscard]] ALWAYS_INLINE static NonnullRefPtr<To> object_cast(NonnullRefPtr<From> obj
  178. #ifdef PDF_DEBUG
  179. ,
  180. SourceLocation loc = SourceLocation::current()
  181. #endif
  182. )
  183. {
  184. #ifdef PDF_DEBUG
  185. # define ENUMERATE_TYPES(class_name, snake_name) \
  186. if constexpr (IsSame<To, class_name>) { \
  187. if (!obj->is_##snake_name()) { \
  188. dbgln("{} invalid cast from type {} to type " #snake_name, loc, obj->type_name()); \
  189. } \
  190. }
  191. ENUMERATE_OBJECT_TYPES(ENUMERATE_TYPES)
  192. # undef ENUMERATE_TYPES
  193. #endif
  194. return static_cast<NonnullRefPtr<To>>(obj);
  195. }
  196. }
  197. namespace AK {
  198. template<PDF::IsObject T>
  199. struct Formatter<T> : Formatter<StringView> {
  200. void format(FormatBuilder& builder, T const& object)
  201. {
  202. Formatter<StringView>::format(builder, object.to_string(0));
  203. }
  204. };
  205. template<PDF::IsObject T>
  206. struct Formatter<NonnullRefPtr<T>> : Formatter<T> {
  207. void format(FormatBuilder& builder, NonnullRefPtr<T> const& object)
  208. {
  209. Formatter<T>::format(builder, *object);
  210. }
  211. };
  212. }