Value.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibPDF/Object.h>
  7. #include <LibPDF/Value.h>
  8. namespace PDF {
  9. Value::~Value()
  10. {
  11. if (is_object())
  12. m_as_object->unref();
  13. }
  14. Value& Value::operator=(const Value& other)
  15. {
  16. m_type = other.m_type;
  17. switch (m_type) {
  18. case Type::Null:
  19. break;
  20. case Type::Bool:
  21. m_as_bool = other.m_as_bool;
  22. break;
  23. case Type::Int:
  24. m_as_int = other.m_as_int;
  25. break;
  26. case Type::Float:
  27. m_as_float = other.m_as_float;
  28. break;
  29. case Type::Ref:
  30. m_as_ref = other.m_as_ref;
  31. break;
  32. case Type::Object:
  33. m_as_object = other.m_as_object;
  34. if (m_as_object)
  35. m_as_object->ref();
  36. break;
  37. }
  38. return *this;
  39. }
  40. String Value::to_string(int indent) const
  41. {
  42. switch (m_type) {
  43. case Type::Null:
  44. return "null";
  45. case Type::Bool:
  46. return as_bool() ? "true" : "false";
  47. case Type::Int:
  48. return String::number(as_int());
  49. case Type::Float:
  50. return String::number(as_float());
  51. case Type::Ref:
  52. return String::formatted("{} {} R", as_ref_index(), as_ref_generation_index());
  53. case Type::Object:
  54. return as_object()->to_string(indent);
  55. }
  56. VERIFY_NOT_REACHED();
  57. }
  58. }