Encoder.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <LibCore/SharedCircularQueue.h>
  11. #include <LibIPC/Forward.h>
  12. #include <LibIPC/Message.h>
  13. namespace IPC {
  14. template<typename T>
  15. bool encode(Encoder&, T&)
  16. {
  17. static_assert(DependentFalse<T>, "Base IPC::encode() was instantiated");
  18. VERIFY_NOT_REACHED();
  19. }
  20. class Encoder {
  21. public:
  22. explicit Encoder(MessageBuffer& buffer)
  23. : m_buffer(buffer)
  24. {
  25. }
  26. Encoder& operator<<(bool);
  27. Encoder& operator<<(u8);
  28. Encoder& operator<<(u16);
  29. Encoder& operator<<(unsigned);
  30. Encoder& operator<<(unsigned long);
  31. Encoder& operator<<(unsigned long long);
  32. Encoder& operator<<(i8);
  33. Encoder& operator<<(i16);
  34. Encoder& operator<<(i32);
  35. Encoder& operator<<(i64);
  36. Encoder& operator<<(float);
  37. Encoder& operator<<(double);
  38. Encoder& operator<<(char const*);
  39. Encoder& operator<<(StringView);
  40. Encoder& operator<<(String const&);
  41. Encoder& operator<<(ByteBuffer const&);
  42. Encoder& operator<<(URL const&);
  43. Encoder& operator<<(Dictionary const&);
  44. Encoder& operator<<(File const&);
  45. template<typename K, typename V>
  46. Encoder& operator<<(HashMap<K, V> const& hashmap)
  47. {
  48. *this << (u32)hashmap.size();
  49. for (auto it : hashmap) {
  50. *this << it.key;
  51. *this << it.value;
  52. }
  53. return *this;
  54. }
  55. template<typename K, typename V>
  56. Encoder& operator<<(OrderedHashMap<K, V> const& hashmap)
  57. {
  58. *this << (u32)hashmap.size();
  59. for (auto it : hashmap) {
  60. *this << it.key;
  61. *this << it.value;
  62. }
  63. return *this;
  64. }
  65. template<typename T>
  66. Encoder& operator<<(Vector<T> const& vector)
  67. {
  68. *this << (u64)vector.size();
  69. for (auto& value : vector)
  70. *this << value;
  71. return *this;
  72. }
  73. template<typename T, size_t Size>
  74. Encoder& operator<<(Core::SharedSingleProducerCircularQueue<T, Size> const& queue)
  75. {
  76. *this << IPC::File(queue.fd());
  77. return *this;
  78. }
  79. template<Enum T>
  80. Encoder& operator<<(T const& enum_value)
  81. {
  82. *this << AK::to_underlying(enum_value);
  83. return *this;
  84. }
  85. template<typename T>
  86. Encoder& operator<<(T const& value)
  87. {
  88. encode(value);
  89. return *this;
  90. }
  91. template<typename T>
  92. Encoder& operator<<(Optional<T> const& optional)
  93. {
  94. *this << optional.has_value();
  95. if (optional.has_value())
  96. *this << optional.value();
  97. return *this;
  98. }
  99. template<typename T>
  100. void encode(T const& value)
  101. {
  102. IPC::encode(*this, value);
  103. }
  104. private:
  105. void encode_u32(u32);
  106. void encode_u64(u64);
  107. MessageBuffer& m_buffer;
  108. };
  109. }