xref: /llvm-project/llvm/include/llvm/BinaryFormat/MsgPackWriter.h (revision b58174d6248805c8994cfe685c826d3f14961202)
120f9cd88SScott Linder //===- MsgPackWriter.h - Simple MsgPack writer ------------------*- C++ -*-===//
220f9cd88SScott Linder //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
620f9cd88SScott Linder //
720f9cd88SScott Linder //===----------------------------------------------------------------------===//
820f9cd88SScott Linder ///
920f9cd88SScott Linder ///  \file
1020f9cd88SScott Linder ///  This file contains a MessagePack writer.
1120f9cd88SScott Linder ///
1220f9cd88SScott Linder ///  See https://github.com/msgpack/msgpack/blob/master/spec.md for the full
1320f9cd88SScott Linder ///  specification.
1420f9cd88SScott Linder ///
1520f9cd88SScott Linder ///  Typical usage:
1620f9cd88SScott Linder ///  \code
1720f9cd88SScott Linder ///  raw_ostream output = GetOutputStream();
1820f9cd88SScott Linder ///  msgpack::Writer MPWriter(output);
1920f9cd88SScott Linder ///  MPWriter.writeNil();
2020f9cd88SScott Linder ///  MPWriter.write(false);
2120f9cd88SScott Linder ///  MPWriter.write("string");
2220f9cd88SScott Linder ///  // ...
2320f9cd88SScott Linder ///  \endcode
2420f9cd88SScott Linder ///
2520f9cd88SScott Linder ///
2620f9cd88SScott Linder //===----------------------------------------------------------------------===//
2720f9cd88SScott Linder 
28aa5c09beSKazu Hirata #ifndef LLVM_BINARYFORMAT_MSGPACKWRITER_H
29aa5c09beSKazu Hirata #define LLVM_BINARYFORMAT_MSGPACKWRITER_H
3020f9cd88SScott Linder 
3120f9cd88SScott Linder #include "llvm/Support/EndianStream.h"
32*b58174d6Sserge-sans-paille #include "llvm/Support/MemoryBufferRef.h"
3320f9cd88SScott Linder 
3420f9cd88SScott Linder namespace llvm {
35*b58174d6Sserge-sans-paille 
36*b58174d6Sserge-sans-paille class raw_ostream;
37*b58174d6Sserge-sans-paille 
3820f9cd88SScott Linder namespace msgpack {
3920f9cd88SScott Linder 
4020f9cd88SScott Linder /// Writes MessagePack objects to an output stream, one at a time.
4120f9cd88SScott Linder class Writer {
4220f9cd88SScott Linder public:
4320f9cd88SScott Linder   /// Construct a writer, optionally enabling "Compatibility Mode" as defined
4420f9cd88SScott Linder   /// in the MessagePack specification.
4520f9cd88SScott Linder   ///
4620f9cd88SScott Linder   /// When in \p Compatible mode, the writer will write \c Str16 formats
4720f9cd88SScott Linder   /// instead of \c Str8 formats, and will refuse to write any \c Bin formats.
4820f9cd88SScott Linder   ///
4920f9cd88SScott Linder   /// \param OS stream to output MessagePack objects to.
5020f9cd88SScott Linder   /// \param Compatible when set, write in "Compatibility Mode".
5120f9cd88SScott Linder   Writer(raw_ostream &OS, bool Compatible = false);
5220f9cd88SScott Linder 
5320f9cd88SScott Linder   Writer(const Writer &) = delete;
5420f9cd88SScott Linder   Writer &operator=(const Writer &) = delete;
5520f9cd88SScott Linder 
5620f9cd88SScott Linder   /// Write a \em Nil to the output stream.
5720f9cd88SScott Linder   ///
5820f9cd88SScott Linder   /// The output will be the \em nil format.
5920f9cd88SScott Linder   void writeNil();
6020f9cd88SScott Linder 
6120f9cd88SScott Linder   /// Write a \em Boolean to the output stream.
6220f9cd88SScott Linder   ///
6320f9cd88SScott Linder   /// The output will be a \em bool format.
6420f9cd88SScott Linder   void write(bool b);
6520f9cd88SScott Linder 
6620f9cd88SScott Linder   /// Write a signed integer to the output stream.
6720f9cd88SScott Linder   ///
6820f9cd88SScott Linder   /// The output will be in the smallest possible \em int format.
6920f9cd88SScott Linder   ///
7020f9cd88SScott Linder   /// The format chosen may be for an unsigned integer.
7120f9cd88SScott Linder   void write(int64_t i);
7220f9cd88SScott Linder 
7320f9cd88SScott Linder   /// Write an unsigned integer to the output stream.
7420f9cd88SScott Linder   ///
7520f9cd88SScott Linder   /// The output will be in the smallest possible \em int format.
7620f9cd88SScott Linder   void write(uint64_t u);
7720f9cd88SScott Linder 
7820f9cd88SScott Linder   /// Write a floating point number to the output stream.
7920f9cd88SScott Linder   ///
8020f9cd88SScott Linder   /// The output will be in the smallest possible \em float format.
8120f9cd88SScott Linder   void write(double d);
8220f9cd88SScott Linder 
8320f9cd88SScott Linder   /// Write a string to the output stream.
8420f9cd88SScott Linder   ///
8520f9cd88SScott Linder   /// The output will be in the smallest possible \em str format.
8620f9cd88SScott Linder   void write(StringRef s);
8720f9cd88SScott Linder 
8820f9cd88SScott Linder   /// Write a memory buffer to the output stream.
8920f9cd88SScott Linder   ///
9020f9cd88SScott Linder   /// The output will be in the smallest possible \em bin format.
9120f9cd88SScott Linder   ///
9220f9cd88SScott Linder   /// \warning Do not use this overload if in \c Compatible mode.
9320f9cd88SScott Linder   void write(MemoryBufferRef Buffer);
9420f9cd88SScott Linder 
9520f9cd88SScott Linder   /// Write the header for an \em Array of the given size.
9620f9cd88SScott Linder   ///
9720f9cd88SScott Linder   /// The output will be in the smallest possible \em array format.
9820f9cd88SScott Linder   //
9920f9cd88SScott Linder   /// The header contains an identifier for the \em array format used, as well
10020f9cd88SScott Linder   /// as an encoding of the size of the array.
10120f9cd88SScott Linder   ///
10220f9cd88SScott Linder   /// N.B. The caller must subsequently call \c Write an additional \p Size
10320f9cd88SScott Linder   /// times to complete the array.
10420f9cd88SScott Linder   void writeArraySize(uint32_t Size);
10520f9cd88SScott Linder 
10620f9cd88SScott Linder   /// Write the header for a \em Map of the given size.
10720f9cd88SScott Linder   ///
10820f9cd88SScott Linder   /// The output will be in the smallest possible \em map format.
10920f9cd88SScott Linder   //
11020f9cd88SScott Linder   /// The header contains an identifier for the \em map format used, as well
11120f9cd88SScott Linder   /// as an encoding of the size of the map.
11220f9cd88SScott Linder   ///
11320f9cd88SScott Linder   /// N.B. The caller must subsequently call \c Write and additional \c Size*2
11420f9cd88SScott Linder   /// times to complete the map. Each even numbered call to \c Write defines a
11520f9cd88SScott Linder   /// new key, and each odd numbered call defines the previous key's value.
11620f9cd88SScott Linder   void writeMapSize(uint32_t Size);
11720f9cd88SScott Linder 
11820f9cd88SScott Linder   /// Write a typed memory buffer (an extension type) to the output stream.
11920f9cd88SScott Linder   ///
12020f9cd88SScott Linder   /// The output will be in the smallest possible \em ext format.
12120f9cd88SScott Linder   void writeExt(int8_t Type, MemoryBufferRef Buffer);
12220f9cd88SScott Linder 
12320f9cd88SScott Linder private:
12420f9cd88SScott Linder   support::endian::Writer EW;
12520f9cd88SScott Linder   bool Compatible;
12620f9cd88SScott Linder };
12720f9cd88SScott Linder 
12820f9cd88SScott Linder } // end namespace msgpack
12920f9cd88SScott Linder } // end namespace llvm
13020f9cd88SScott Linder 
131aa5c09beSKazu Hirata #endif // LLVM_BINARYFORMAT_MSGPACKWRITER_H
132