Encoder.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Concepts.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/StdLibExtras.h>
  10. #include <AK/Variant.h>
  11. #include <LibCore/SharedCircularQueue.h>
  12. #include <LibIPC/Forward.h>
  13. #include <LibIPC/Message.h>
  14. namespace IPC {
  15. template<typename T>
  16. bool encode(Encoder&, T const&)
  17. {
  18. static_assert(DependentFalse<T>, "Base IPC::encode() was instantiated");
  19. VERIFY_NOT_REACHED();
  20. }
  21. class Encoder {
  22. public:
  23. explicit Encoder(MessageBuffer& buffer)
  24. : m_buffer(buffer)
  25. {
  26. }
  27. Encoder& operator<<(bool);
  28. Encoder& operator<<(u8);
  29. Encoder& operator<<(u16);
  30. Encoder& operator<<(unsigned);
  31. Encoder& operator<<(unsigned long);
  32. Encoder& operator<<(unsigned long long);
  33. Encoder& operator<<(i8);
  34. Encoder& operator<<(i16);
  35. Encoder& operator<<(i32);
  36. Encoder& operator<<(i64);
  37. Encoder& operator<<(float);
  38. Encoder& operator<<(double);
  39. Encoder& operator<<(char const*);
  40. Encoder& operator<<(StringView);
  41. Encoder& operator<<(DeprecatedString const&);
  42. Encoder& operator<<(ByteBuffer const&);
  43. Encoder& operator<<(JsonValue const&);
  44. Encoder& operator<<(URL const&);
  45. Encoder& operator<<(Dictionary const&);
  46. Encoder& operator<<(File const&);
  47. Encoder& operator<<(AK::Empty const&);
  48. template<typename K, typename V>
  49. Encoder& operator<<(HashMap<K, V> const& hashmap)
  50. {
  51. *this << (u32)hashmap.size();
  52. for (auto it : hashmap) {
  53. *this << it.key;
  54. *this << it.value;
  55. }
  56. return *this;
  57. }
  58. template<typename K, typename V>
  59. Encoder& operator<<(OrderedHashMap<K, V> const& hashmap)
  60. {
  61. *this << (u32)hashmap.size();
  62. for (auto it : hashmap) {
  63. *this << it.key;
  64. *this << it.value;
  65. }
  66. return *this;
  67. }
  68. template<typename T>
  69. Encoder& operator<<(Vector<T> const& vector)
  70. {
  71. *this << (u64)vector.size();
  72. for (auto& value : vector)
  73. *this << value;
  74. return *this;
  75. }
  76. template<typename T, size_t Size>
  77. Encoder& operator<<(Core::SharedSingleProducerCircularQueue<T, Size> const& queue)
  78. {
  79. *this << IPC::File(queue.fd());
  80. return *this;
  81. }
  82. // Note: We require any encodeable variant to have Empty as its first variant, as only possibly-empty variants can be default constructed.
  83. // The default constructability is required by generated IPC message marshalling code.
  84. template<typename... VariantTypes>
  85. Encoder& operator<<(AK::Variant<AK::Empty, VariantTypes...> const& variant)
  86. {
  87. // Note: This might be either u8 or size_t depending on the size of the variant; both are encodeable.
  88. *this << variant.index();
  89. variant.visit([this](auto const& underlying_value) { *this << underlying_value; });
  90. return *this;
  91. }
  92. template<Enum T>
  93. Encoder& operator<<(T const& enum_value)
  94. {
  95. *this << AK::to_underlying(enum_value);
  96. return *this;
  97. }
  98. template<typename T>
  99. Encoder& operator<<(T const& value)
  100. {
  101. encode(value);
  102. return *this;
  103. }
  104. template<typename T>
  105. Encoder& operator<<(Optional<T> const& optional)
  106. {
  107. *this << optional.has_value();
  108. if (optional.has_value())
  109. *this << optional.value();
  110. return *this;
  111. }
  112. template<typename T>
  113. void encode(T const& value)
  114. {
  115. IPC::encode(*this, value);
  116. }
  117. private:
  118. void encode_u32(u32);
  119. void encode_u64(u64);
  120. MessageBuffer& m_buffer;
  121. };
  122. }