1 //===- MsgPackWriter.h - Simple MsgPack writer ------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file contains a MessagePack writer.
12 /// See https://github.com/msgpack/msgpack/blob/master/spec.md for the full
17 /// raw_ostream output = GetOutputStream();
18 /// msgpack::Writer MPWriter(output);
19 /// MPWriter.writeNil();
20 /// MPWriter.write(false);
21 /// MPWriter.write("string");
26 //===----------------------------------------------------------------------===//
28 #ifndef LLVM_SUPPORT_MSGPACKPARSER_H
29 #define LLVM_SUPPORT_MSGPACKPARSER_H
31 #include "llvm/BinaryFormat/MsgPack.h"
32 #include "llvm/Support/EndianStream.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/raw_ostream.h"
39 /// Writes MessagePack objects to an output stream, one at a time.
42 /// Construct a writer, optionally enabling "Compatibility Mode" as defined
43 /// in the MessagePack specification.
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.
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);
52 Writer(const Writer
&) = delete;
53 Writer
&operator=(const Writer
&) = delete;
55 /// Write a \em Nil to the output stream.
57 /// The output will be the \em nil format.
60 /// Write a \em Boolean to the output stream.
62 /// The output will be a \em bool format.
65 /// Write a signed integer to the output stream.
67 /// The output will be in the smallest possible \em int format.
69 /// The format chosen may be for an unsigned integer.
70 void write(int64_t i
);
72 /// Write an unsigned integer to the output stream.
74 /// The output will be in the smallest possible \em int format.
75 void write(uint64_t u
);
77 /// Write a floating point number to the output stream.
79 /// The output will be in the smallest possible \em float format.
82 /// Write a string to the output stream.
84 /// The output will be in the smallest possible \em str format.
85 void write(StringRef s
);
87 /// Write a memory buffer to the output stream.
89 /// The output will be in the smallest possible \em bin format.
91 /// \warning Do not use this overload if in \c Compatible mode.
92 void write(MemoryBufferRef Buffer
);
94 /// Write the header for an \em Array of the given size.
96 /// The output will be in the smallest possible \em array format.
98 /// The header contains an identifier for the \em array format used, as well
99 /// as an encoding of the size of the array.
101 /// N.B. The caller must subsequently call \c Write an additional \p Size
102 /// times to complete the array.
103 void writeArraySize(uint32_t Size
);
105 /// Write the header for a \em Map of the given size.
107 /// The output will be in the smallest possible \em map format.
109 /// The header contains an identifier for the \em map format used, as well
110 /// as an encoding of the size of the map.
112 /// N.B. The caller must subsequently call \c Write and additional \c Size*2
113 /// times to complete the map. Each even numbered call to \c Write defines a
114 /// new key, and each odd numbered call defines the previous key's value.
115 void writeMapSize(uint32_t Size
);
117 /// Write a typed memory buffer (an extension type) to the output stream.
119 /// The output will be in the smallest possible \em ext format.
120 void writeExt(int8_t Type
, MemoryBufferRef Buffer
);
123 support::endian::Writer EW
;
127 } // end namespace msgpack
128 } // end namespace llvm
130 #endif // LLVM_SUPPORT_MSGPACKPARSER_H