MsgPackWriter.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MsgPackWriter.h - Simple MsgPack writer ------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file contains a MessagePack writer.
  16. ///
  17. /// See https://github.com/msgpack/msgpack/blob/master/spec.md for the full
  18. /// specification.
  19. ///
  20. /// Typical usage:
  21. /// \code
  22. /// raw_ostream output = GetOutputStream();
  23. /// msgpack::Writer MPWriter(output);
  24. /// MPWriter.writeNil();
  25. /// MPWriter.write(false);
  26. /// MPWriter.write("string");
  27. /// // ...
  28. /// \endcode
  29. ///
  30. ///
  31. //===----------------------------------------------------------------------===//
  32. #ifndef LLVM_BINARYFORMAT_MSGPACKWRITER_H
  33. #define LLVM_BINARYFORMAT_MSGPACKWRITER_H
  34. #include "llvm/Support/EndianStream.h"
  35. #include "llvm/Support/MemoryBufferRef.h"
  36. namespace llvm {
  37. class raw_ostream;
  38. namespace msgpack {
  39. /// Writes MessagePack objects to an output stream, one at a time.
  40. class Writer {
  41. public:
  42. /// Construct a writer, optionally enabling "Compatibility Mode" as defined
  43. /// in the MessagePack specification.
  44. ///
  45. /// When in \p Compatible mode, the writer will write \c Str16 formats
  46. /// instead of \c Str8 formats, and will refuse to write any \c Bin formats.
  47. ///
  48. /// \param OS stream to output MessagePack objects to.
  49. /// \param Compatible when set, write in "Compatibility Mode".
  50. Writer(raw_ostream &OS, bool Compatible = false);
  51. Writer(const Writer &) = delete;
  52. Writer &operator=(const Writer &) = delete;
  53. /// Write a \em Nil to the output stream.
  54. ///
  55. /// The output will be the \em nil format.
  56. void writeNil();
  57. /// Write a \em Boolean to the output stream.
  58. ///
  59. /// The output will be a \em bool format.
  60. void write(bool b);
  61. /// Write a signed integer to the output stream.
  62. ///
  63. /// The output will be in the smallest possible \em int format.
  64. ///
  65. /// The format chosen may be for an unsigned integer.
  66. void write(int64_t i);
  67. /// Write an unsigned integer to the output stream.
  68. ///
  69. /// The output will be in the smallest possible \em int format.
  70. void write(uint64_t u);
  71. /// Write a floating point number to the output stream.
  72. ///
  73. /// The output will be in the smallest possible \em float format.
  74. void write(double d);
  75. /// Write a string to the output stream.
  76. ///
  77. /// The output will be in the smallest possible \em str format.
  78. void write(StringRef s);
  79. /// Write a memory buffer to the output stream.
  80. ///
  81. /// The output will be in the smallest possible \em bin format.
  82. ///
  83. /// \warning Do not use this overload if in \c Compatible mode.
  84. void write(MemoryBufferRef Buffer);
  85. /// Write the header for an \em Array of the given size.
  86. ///
  87. /// The output will be in the smallest possible \em array format.
  88. //
  89. /// The header contains an identifier for the \em array format used, as well
  90. /// as an encoding of the size of the array.
  91. ///
  92. /// N.B. The caller must subsequently call \c Write an additional \p Size
  93. /// times to complete the array.
  94. void writeArraySize(uint32_t Size);
  95. /// Write the header for a \em Map of the given size.
  96. ///
  97. /// The output will be in the smallest possible \em map format.
  98. //
  99. /// The header contains an identifier for the \em map format used, as well
  100. /// as an encoding of the size of the map.
  101. ///
  102. /// N.B. The caller must subsequently call \c Write and additional \c Size*2
  103. /// times to complete the map. Each even numbered call to \c Write defines a
  104. /// new key, and each odd numbered call defines the previous key's value.
  105. void writeMapSize(uint32_t Size);
  106. /// Write a typed memory buffer (an extension type) to the output stream.
  107. ///
  108. /// The output will be in the smallest possible \em ext format.
  109. void writeExt(int8_t Type, MemoryBufferRef Buffer);
  110. private:
  111. support::endian::Writer EW;
  112. bool Compatible;
  113. };
  114. } // end namespace msgpack
  115. } // end namespace llvm
  116. #endif // LLVM_BINARYFORMAT_MSGPACKWRITER_H
  117. #ifdef __GNUC__
  118. #pragma GCC diagnostic pop
  119. #endif