Line data Source code
1 : //===- StreamWriter.h - Writes bytes and objects to a stream ----*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 :
10 : #ifndef LLVM_DEBUGINFO_MSF_STREAMWRITER_H
11 : #define LLVM_DEBUGINFO_MSF_STREAMWRITER_H
12 :
13 : #include "llvm/ADT/ArrayRef.h"
14 : #include "llvm/DebugInfo/Msf/MsfError.h"
15 : #include "llvm/DebugInfo/Msf/StreamArray.h"
16 : #include "llvm/DebugInfo/Msf/StreamInterface.h"
17 : #include "llvm/DebugInfo/Msf/StreamRef.h"
18 : #include "llvm/Support/Endian.h"
19 : #include "llvm/Support/Error.h"
20 :
21 : #include <string>
22 :
23 : namespace llvm {
24 : namespace msf {
25 :
26 : class StreamWriter {
27 : public:
28 : StreamWriter(WritableStreamRef Stream);
29 :
30 : Error writeBytes(ArrayRef<uint8_t> Buffer);
31 : Error writeInteger(uint16_t Dest);
32 : Error writeInteger(uint32_t Dest);
33 : Error writeZeroString(StringRef Str);
34 : Error writeFixedString(StringRef Str);
35 : Error writeStreamRef(ReadableStreamRef Ref);
36 : Error writeStreamRef(ReadableStreamRef Ref, uint32_t Size);
37 :
38 : template <typename T> Error writeEnum(T Num) {
39 : return writeInteger(
40 : static_cast<typename std::underlying_type<T>::type>(Num));
41 : }
42 :
43 : template <typename T> Error writeObject(const T &Obj) {
44 : static_assert(!std::is_pointer<T>::value,
45 : "writeObject should not be used with pointers, to write "
46 : "the pointed-to value dereference the pointer before calling "
47 : "writeObject");
48 : return writeBytes(
49 20 : ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(&Obj), sizeof(T)));
50 : }
51 :
52 19 : template <typename T> Error writeArray(ArrayRef<T> Array) {
53 19 : if (Array.size() == 0)
54 : return Error::success();
55 :
56 17 : if (Array.size() > UINT32_MAX / sizeof(T))
57 0 : return make_error<MsfError>(msf_error_code::insufficient_buffer);
58 :
59 17 : return writeBytes(
60 : ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Array.data()),
61 34 : Array.size() * sizeof(T)));
62 : }
63 :
64 : template <typename T, typename U>
65 : Error writeArray(VarStreamArray<T, U> Array) {
66 : return writeStreamRef(Array.getUnderlyingStream());
67 : }
68 :
69 : template <typename T> Error writeArray(FixedStreamArray<T> Array) {
70 : return writeStreamRef(Array.getUnderlyingStream());
71 : }
72 :
73 1 : void setOffset(uint32_t Off) { Offset = Off; }
74 : uint32_t getOffset() const { return Offset; }
75 0 : uint32_t getLength() const { return Stream.getLength(); }
76 0 : uint32_t bytesRemaining() const { return getLength() - getOffset(); }
77 :
78 : private:
79 : WritableStreamRef Stream;
80 : uint32_t Offset;
81 : };
82 : } // namespace msf
83 : } // namespace llvm
84 :
85 : #endif // LLVM_DEBUGINFO_MSF_STREAMWRITER_H
|