LLVM 22.0.0git
MsgPackWriter.h
Go to the documentation of this file.
1//===- MsgPackWriter.h - Simple MsgPack writer ------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains a MessagePack writer.
11///
12/// See https://github.com/msgpack/msgpack/blob/master/spec.md for the full
13/// specification.
14///
15/// Typical usage:
16/// \code
17/// raw_ostream output = GetOutputStream();
18/// msgpack::Writer MPWriter(output);
19/// MPWriter.writeNil();
20/// MPWriter.write(false);
21/// MPWriter.write("string");
22/// // ...
23/// \endcode
24///
25///
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_BINARYFORMAT_MSGPACKWRITER_H
29#define LLVM_BINARYFORMAT_MSGPACKWRITER_H
30
34
35namespace llvm {
36
37class raw_ostream;
38
39namespace msgpack {
40
41/// Writes MessagePack objects to an output stream, one at a time.
42class Writer {
43public:
44 /// Construct a writer, optionally enabling "Compatibility Mode" as defined
45 /// in the MessagePack specification.
46 ///
47 /// When in \p Compatible mode, the writer will write \c Str16 formats
48 /// instead of \c Str8 formats, and will refuse to write any \c Bin formats.
49 ///
50 /// \param OS stream to output MessagePack objects to.
51 /// \param Compatible when set, write in "Compatibility Mode".
52 LLVM_ABI Writer(raw_ostream &OS, bool Compatible = false);
53
54 Writer(const Writer &) = delete;
55 Writer &operator=(const Writer &) = delete;
56
57 /// Write a \em Nil to the output stream.
58 ///
59 /// The output will be the \em nil format.
60 LLVM_ABI void writeNil();
61
62 /// Write a \em Boolean to the output stream.
63 ///
64 /// The output will be a \em bool format.
65 LLVM_ABI void write(bool b);
66
67 /// Write a signed integer to the output stream.
68 ///
69 /// The output will be in the smallest possible \em int format.
70 ///
71 /// The format chosen may be for an unsigned integer.
72 LLVM_ABI void write(int64_t i);
73
74 /// Write an unsigned integer to the output stream.
75 ///
76 /// The output will be in the smallest possible \em int format.
77 LLVM_ABI void write(uint64_t u);
78
79 /// Write a floating point number to the output stream.
80 ///
81 /// The output will be in the smallest possible \em float format.
82 LLVM_ABI void write(double d);
83
84 /// Write a string to the output stream.
85 ///
86 /// The output will be in the smallest possible \em str format.
87 LLVM_ABI void write(StringRef s);
88
89 /// Write a memory buffer to the output stream.
90 ///
91 /// The output will be in the smallest possible \em bin format.
92 ///
93 /// \warning Do not use this overload if in \c Compatible mode.
94 LLVM_ABI void write(MemoryBufferRef Buffer);
95
96 /// Write the header for an \em Array of the given size.
97 ///
98 /// The output will be in the smallest possible \em array format.
99 //
100 /// The header contains an identifier for the \em array format used, as well
101 /// as an encoding of the size of the array.
102 ///
103 /// N.B. The caller must subsequently call \c Write an additional \p Size
104 /// times to complete the array.
106
107 /// Write the header for a \em Map of the given size.
108 ///
109 /// The output will be in the smallest possible \em map format.
110 //
111 /// The header contains an identifier for the \em map format used, as well
112 /// as an encoding of the size of the map.
113 ///
114 /// N.B. The caller must subsequently call \c Write and additional \c Size*2
115 /// times to complete the map. Each even numbered call to \c Write defines a
116 /// new key, and each odd numbered call defines the previous key's value.
118
119 /// Write a typed memory buffer (an extension type) to the output stream.
120 ///
121 /// The output will be in the smallest possible \em ext format.
122 LLVM_ABI void writeExt(int8_t Type, MemoryBufferRef Buffer);
123
124private:
126 bool Compatible;
127};
128
129} // end namespace msgpack
130} // end namespace llvm
131
132#endif // LLVM_BINARYFORMAT_MSGPACKWRITER_H
#define LLVM_ABI
Definition: Compiler.h:213
uint64_t Size
raw_pwrite_stream & OS
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Writes MessagePack objects to an output stream, one at a time.
Definition: MsgPackWriter.h:42
LLVM_ABI void writeNil()
Write a Nil to the output stream.
Writer & operator=(const Writer &)=delete
LLVM_ABI void writeMapSize(uint32_t Size)
Write the header for a Map of the given size.
Writer(const Writer &)=delete
LLVM_ABI void writeArraySize(uint32_t Size)
Write the header for an Array of the given size.
LLVM_ABI void writeExt(int8_t Type, MemoryBufferRef Buffer)
Write a typed memory buffer (an extension type) to the output stream.
LLVM_ABI void write(bool b)
Write a Boolean to the output stream.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:54
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:67