Encoder.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. template<typename... VariantTypes>
  83. Encoder& operator<<(AK::Variant<VariantTypes...> const& variant)
  84. {
  85. *this << variant.index();
  86. variant.visit([this](auto const& underlying_value) { *this << underlying_value; });
  87. return *this;
  88. }
  89. template<Enum T>
  90. Encoder& operator<<(T const& enum_value)
  91. {
  92. *this << AK::to_underlying(enum_value);
  93. return *this;
  94. }
  95. template<typename T>
  96. Encoder& operator<<(T const& value)
  97. {
  98. encode(value);
  99. return *this;
  100. }
  101. template<typename T>
  102. Encoder& operator<<(Optional<T> const& optional)
  103. {
  104. *this << optional.has_value();
  105. if (optional.has_value())
  106. *this << optional.value();
  107. return *this;
  108. }
  109. template<typename T>
  110. void encode(T const& value)
  111. {
  112. IPC::encode(*this, value);
  113. }
  114. private:
  115. void encode_u32(u32);
  116. void encode_u64(u64);
  117. MessageBuffer& m_buffer;
  118. };
  119. }