aboutsummaryrefslogtreecommitdiff
path: root/include/llvm/BinaryFormat/MsgPackWriter.h
blob: 3b610b774f77112e4d3323f432024a5a7dc81822 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//===- MsgPackWriter.h - Simple MsgPack writer ------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
///  \file
///  This file contains a MessagePack writer.
///
///  See https://github.com/msgpack/msgpack/blob/master/spec.md for the full
///  specification.
///
///  Typical usage:
///  \code
///  raw_ostream output = GetOutputStream();
///  msgpack::Writer MPWriter(output);
///  MPWriter.writeNil();
///  MPWriter.write(false);
///  MPWriter.write("string");
///  // ...
///  \endcode
///
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_MSGPACKPARSER_H
#define LLVM_SUPPORT_MSGPACKPARSER_H

#include "llvm/BinaryFormat/MsgPack.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"

namespace llvm {
namespace msgpack {

/// Writes MessagePack objects to an output stream, one at a time.
class Writer {
public:
  /// Construct a writer, optionally enabling "Compatibility Mode" as defined
  /// in the MessagePack specification.
  ///
  /// When in \p Compatible mode, the writer will write \c Str16 formats
  /// instead of \c Str8 formats, and will refuse to write any \c Bin formats.
  ///
  /// \param OS stream to output MessagePack objects to.
  /// \param Compatible when set, write in "Compatibility Mode".
  Writer(raw_ostream &OS, bool Compatible = false);

  Writer(const Writer &) = delete;
  Writer &operator=(const Writer &) = delete;

  /// Write a \em Nil to the output stream.
  ///
  /// The output will be the \em nil format.
  void writeNil();

  /// Write a \em Boolean to the output stream.
  ///
  /// The output will be a \em bool format.
  void write(bool b);

  /// Write a signed integer to the output stream.
  ///
  /// The output will be in the smallest possible \em int format.
  ///
  /// The format chosen may be for an unsigned integer.
  void write(int64_t i);

  /// Write an unsigned integer to the output stream.
  ///
  /// The output will be in the smallest possible \em int format.
  void write(uint64_t u);

  /// Write a floating point number to the output stream.
  ///
  /// The output will be in the smallest possible \em float format.
  void write(double d);

  /// Write a string to the output stream.
  ///
  /// The output will be in the smallest possible \em str format.
  void write(StringRef s);

  /// Write a memory buffer to the output stream.
  ///
  /// The output will be in the smallest possible \em bin format.
  ///
  /// \warning Do not use this overload if in \c Compatible mode.
  void write(MemoryBufferRef Buffer);

  /// Write the header for an \em Array of the given size.
  ///
  /// The output will be in the smallest possible \em array format.
  //
  /// The header contains an identifier for the \em array format used, as well
  /// as an encoding of the size of the array.
  ///
  /// N.B. The caller must subsequently call \c Write an additional \p Size
  /// times to complete the array.
  void writeArraySize(uint32_t Size);

  /// Write the header for a \em Map of the given size.
  ///
  /// The output will be in the smallest possible \em map format.
  //
  /// The header contains an identifier for the \em map format used, as well
  /// as an encoding of the size of the map.
  ///
  /// N.B. The caller must subsequently call \c Write and additional \c Size*2
  /// times to complete the map. Each even numbered call to \c Write defines a
  /// new key, and each odd numbered call defines the previous key's value.
  void writeMapSize(uint32_t Size);

  /// Write a typed memory buffer (an extension type) to the output stream.
  ///
  /// The output will be in the smallest possible \em ext format.
  void writeExt(int8_t Type, MemoryBufferRef Buffer);

private:
  support::endian::Writer EW;
  bool Compatible;
};

} // end namespace msgpack
} // end namespace llvm

#endif // LLVM_SUPPORT_MSGPACKPARSER_H