Value.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/Format.h>
  8. namespace PDF {
  9. class Object;
  10. class Value {
  11. public:
  12. // We store refs as u32, with 18 bits for the index and 14 bits for the
  13. // generation index. The generation index is stored in the higher bits.
  14. // This may need to be rethought later, as the max generation index is
  15. // 2^16 and the max for the object index is probably 2^32 (I don't know
  16. // exactly)
  17. static constexpr auto max_ref_index = (1 << 19) - 1; // 2 ^ 18 - 1
  18. static constexpr auto max_ref_generation_index = (1 << 15) - 1; // 2 ^ 14 - 1
  19. Value()
  20. : m_type(Type::Empty)
  21. {
  22. }
  23. struct NullTag {
  24. };
  25. Value(NullTag)
  26. : m_type(Type::Null)
  27. {
  28. }
  29. Value(bool b)
  30. : m_type(Type::Bool)
  31. {
  32. m_as_bool = b;
  33. }
  34. Value(int i)
  35. : m_type(Type::Int)
  36. {
  37. m_as_int = i;
  38. }
  39. Value(float f)
  40. : m_type(Type::Float)
  41. {
  42. m_as_float = f;
  43. }
  44. Value(u32 index, u32 generation_index)
  45. : m_type(Type::Ref)
  46. {
  47. VERIFY(index < max_ref_index);
  48. VERIFY(generation_index < max_ref_generation_index);
  49. m_as_ref = (generation_index << 14) | index;
  50. }
  51. template<IsObject T>
  52. Value(RefPtr<T> obj)
  53. : m_type(obj ? Type::Object : Type::Empty)
  54. {
  55. if (obj) {
  56. obj->ref();
  57. m_as_object = obj;
  58. }
  59. }
  60. template<IsObject T>
  61. Value(NonnullRefPtr<T> obj)
  62. : m_type(Type::Object)
  63. {
  64. obj->ref();
  65. m_as_object = obj;
  66. }
  67. Value(Value const& other)
  68. {
  69. *this = other;
  70. }
  71. ~Value();
  72. Value& operator=(Value const& other);
  73. [[nodiscard]] ALWAYS_INLINE bool is_empty() const { return m_type == Type::Empty; }
  74. [[nodiscard]] ALWAYS_INLINE bool is_null() const { return m_type == Type::Null; }
  75. [[nodiscard]] ALWAYS_INLINE bool is_bool() const { return m_type == Type::Bool; }
  76. [[nodiscard]] ALWAYS_INLINE bool is_int() const { return m_type == Type::Int; }
  77. [[nodiscard]] ALWAYS_INLINE bool is_float() const { return m_type == Type::Float; }
  78. [[nodiscard]] ALWAYS_INLINE bool is_number() const { return is_int() || is_float(); }
  79. [[nodiscard]] ALWAYS_INLINE bool is_ref() const { return m_type == Type::Ref; }
  80. [[nodiscard]] ALWAYS_INLINE bool is_object() const { return m_type == Type::Object; }
  81. [[nodiscard]] ALWAYS_INLINE bool as_bool() const
  82. {
  83. VERIFY(is_bool());
  84. return m_as_bool;
  85. }
  86. [[nodiscard]] ALWAYS_INLINE int as_int() const
  87. {
  88. VERIFY(is_int());
  89. return m_as_int;
  90. }
  91. template<typename T>
  92. [[nodiscard]] ALWAYS_INLINE bool is_int_type() const
  93. {
  94. if (!is_int())
  95. return false;
  96. auto as_int = static_cast<T>(m_as_int);
  97. return as_int >= NumericLimits<T>::min() && as_int <= NumericLimits<T>::max();
  98. }
  99. template<typename T>
  100. [[nodiscard]] ALWAYS_INLINE T as_int_type() const
  101. {
  102. VERIFY(is_int_type<T>());
  103. return static_cast<T>(m_as_int);
  104. }
  105. [[nodiscard]] ALWAYS_INLINE int to_int() const
  106. {
  107. if (is_int())
  108. return as_int();
  109. return static_cast<int>(as_float());
  110. }
  111. [[nodiscard]] ALWAYS_INLINE float as_float() const
  112. {
  113. VERIFY(is_float());
  114. return m_as_float;
  115. }
  116. [[nodiscard]] ALWAYS_INLINE float to_float() const
  117. {
  118. if (is_float())
  119. return as_float();
  120. return static_cast<float>(as_int());
  121. }
  122. [[nodiscard]] ALWAYS_INLINE u32 as_ref_index() const
  123. {
  124. VERIFY(is_ref());
  125. return m_as_ref & 0x3ffff;
  126. }
  127. [[nodiscard]] ALWAYS_INLINE u32 as_ref_generation_index() const
  128. {
  129. VERIFY(is_ref());
  130. return m_as_ref >> 18;
  131. }
  132. [[nodiscard]] ALWAYS_INLINE NonnullRefPtr<Object> as_object() const { return *m_as_object; }
  133. [[nodiscard]] ALWAYS_INLINE explicit operator bool() const { return !is_empty(); }
  134. [[nodiscard]] String to_string(int indent = 0) const;
  135. private:
  136. enum class Type {
  137. Empty,
  138. Null,
  139. Bool,
  140. Int,
  141. Float,
  142. Ref,
  143. Object,
  144. };
  145. union {
  146. bool m_as_bool;
  147. int m_as_int;
  148. u32 m_as_ref;
  149. float m_as_float;
  150. Object* m_as_object;
  151. };
  152. Type m_type;
  153. };
  154. }
  155. namespace AK {
  156. template<>
  157. struct Formatter<PDF::Value> : Formatter<StringView> {
  158. void format(FormatBuilder& builder, PDF::Value const& value)
  159. {
  160. Formatter<StringView>::format(builder, value.to_string());
  161. }
  162. };
  163. }