1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-20 03:23:01 +02:00
llvm-mirror/include/llvm/Support/BinaryStreamWriter.h
Zachary Turner 5fd8290a6c [BinaryStream] Reduce the amount of boiler plate needed to use.
Often you have an array and you just want to use it.  With the current
design, you have to first construct a `BinaryByteStream`, and then create
a `BinaryStreamRef` from it.  Worse, the `BinaryStreamRef` holds a pointer
to the `BinaryByteStream`, so you can't just create a temporary one to
appease the compiler, you have to actually hold onto both the `ArrayRef`
as well as the `BinaryByteStream` *AND* the `BinaryStreamReader` on top of
that.  This makes for very cumbersome code, often requiring one to store a
`BinaryByteStream` in a class just to circumvent this.

At the cost of some added complexity (not exposed to users, but internal
to the library), we can do better than this.  This patch allows us to
construct `BinaryStreamReaders` and `BinaryStreamWriters` directly from
source data (e.g. `StringRef`, `MutableArrayRef<uint8_t>`, etc).  Not only
does this reduce the amount of code you have to type and make it more
obvious how to use it, but it solves real lifetime issues when it's
inconvenient to hold onto a `BinaryByteStream` for a long time.

The additional complexity is in the form of an added layer of indirection.
Whereas before we simply stored a `BinaryStream*` in the ref, we now store
both a `BinaryStream*` **and** a `std::shared_ptr<BinaryStream>`.  When
the user wants to construct a `BinaryStreamRef` directly from an
`ArrayRef` etc, we allocate an internal object that holds ownership over a
`BinaryByteStream` and forwards all calls, and store this in the
`shared_ptr<>`.  This also maintains the ref semantics, as you can copy it
by value and references refer to the same underlying stream -- the one
being held in the object stored in the `shared_ptr`.

Differential Revision: https://reviews.llvm.org/D33293

llvm-svn: 303294
2017-05-17 20:23:31 +00:00

184 lines
7.3 KiB
C++

//===- BinaryStreamWriter.h - Writes objects to a BinaryStream ---*- C++-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_BINARYSTREAMWRITER_H
#define LLVM_SUPPORT_BINARYSTREAMWRITER_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/BinaryStreamArray.h"
#include "llvm/Support/BinaryStreamError.h"
#include "llvm/Support/BinaryStreamRef.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include <cstdint>
#include <type_traits>
#include <utility>
namespace llvm {
/// \brief Provides write only access to a subclass of `WritableBinaryStream`.
/// Provides bounds checking and helpers for writing certain common data types
/// such as null-terminated strings, integers in various flavors of endianness,
/// etc. Can be subclassed to provide reading and writing of custom datatypes,
/// although no methods are overridable.
class BinaryStreamWriter {
public:
BinaryStreamWriter() = default;
explicit BinaryStreamWriter(WritableBinaryStreamRef Ref);
explicit BinaryStreamWriter(WritableBinaryStream &Stream);
explicit BinaryStreamWriter(MutableArrayRef<uint8_t> Data,
llvm::support::endianness Endian);
BinaryStreamWriter(const BinaryStreamWriter &Other)
: Stream(Other.Stream), Offset(Other.Offset) {}
BinaryStreamWriter &operator=(const BinaryStreamWriter &Other) {
Stream = Other.Stream;
Offset = Other.Offset;
return *this;
}
virtual ~BinaryStreamWriter() {}
/// Write the bytes specified in \p Buffer to the underlying stream.
/// On success, updates the offset so that subsequent writes will occur
/// at the next unwritten position.
///
/// \returns a success error code if the data was successfully written,
/// otherwise returns an appropriate error code.
Error writeBytes(ArrayRef<uint8_t> Buffer);
/// Write the the integer \p Value to the underlying stream in the
/// specified endianness. On success, updates the offset so that
/// subsequent writes occur at the next unwritten position.
///
/// \returns a success error code if the data was successfully written,
/// otherwise returns an appropriate error code.
template <typename T> Error writeInteger(T Value) {
static_assert(std::is_integral<T>::value,
"Cannot call writeInteger with non-integral value!");
uint8_t Buffer[sizeof(T)];
llvm::support::endian::write<T, llvm::support::unaligned>(
Buffer, Value, Stream.getEndian());
return writeBytes(Buffer);
}
/// Similar to writeInteger
template <typename T> Error writeEnum(T Num) {
static_assert(std::is_enum<T>::value,
"Cannot call writeEnum with non-Enum type");
using U = typename std::underlying_type<T>::type;
return writeInteger<U>(static_cast<U>(Num));
}
/// Write the the string \p Str to the underlying stream followed by a null
/// terminator. On success, updates the offset so that subsequent writes
/// occur at the next unwritten position. \p Str need not be null terminated
/// on input.
///
/// \returns a success error code if the data was successfully written,
/// otherwise returns an appropriate error code.
Error writeCString(StringRef Str);
/// Write the the string \p Str to the underlying stream without a null
/// terminator. On success, updates the offset so that subsequent writes
/// occur at the next unwritten position.
///
/// \returns a success error code if the data was successfully written,
/// otherwise returns an appropriate error code.
Error writeFixedString(StringRef Str);
/// Efficiently reads all data from \p Ref, and writes it to this stream.
/// This operation will not invoke any copies of the source data, regardless
/// of the source stream's implementation.
///
/// \returns a success error code if the data was successfully written,
/// otherwise returns an appropriate error code.
Error writeStreamRef(BinaryStreamRef Ref);
/// Efficiently reads \p Size bytes from \p Ref, and writes it to this stream.
/// This operation will not invoke any copies of the source data, regardless
/// of the source stream's implementation.
///
/// \returns a success error code if the data was successfully written,
/// otherwise returns an appropriate error code.
Error writeStreamRef(BinaryStreamRef Ref, uint32_t Size);
/// Writes the object \p Obj to the underlying stream, as if by using memcpy.
/// It is up to the caller to ensure that type of \p Obj can be safely copied
/// in this fashion, as no checks are made to ensure that this is safe.
///
/// \returns a success error code if the data was successfully written,
/// otherwise returns an appropriate error code.
template <typename T> Error writeObject(const T &Obj) {
static_assert(!std::is_pointer<T>::value,
"writeObject should not be used with pointers, to write "
"the pointed-to value dereference the pointer before calling "
"writeObject");
return writeBytes(
ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(&Obj), sizeof(T)));
}
/// Writes an array of objects of type T to the underlying stream, as if by
/// using memcpy. It is up to the caller to ensure that type of \p Obj can
/// be safely copied in this fashion, as no checks are made to ensure that
/// this is safe.
///
/// \returns a success error code if the data was successfully written,
/// otherwise returns an appropriate error code.
template <typename T> Error writeArray(ArrayRef<T> Array) {
if (Array.empty())
return Error::success();
if (Array.size() > UINT32_MAX / sizeof(T))
return make_error<BinaryStreamError>(
stream_error_code::invalid_array_size);
return writeBytes(
ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Array.data()),
Array.size() * sizeof(T)));
}
/// Writes all data from the array \p Array to the underlying stream.
///
/// \returns a success error code if the data was successfully written,
/// otherwise returns an appropriate error code.
template <typename T, typename U>
Error writeArray(VarStreamArray<T, U> Array) {
return writeStreamRef(Array.getUnderlyingStream());
}
/// Writes all elements from the array \p Array to the underlying stream.
///
/// \returns a success error code if the data was successfully written,
/// otherwise returns an appropriate error code.
template <typename T> Error writeArray(FixedStreamArray<T> Array) {
return writeStreamRef(Array.getUnderlyingStream());
}
/// Splits the Writer into two Writers at a given offset.
std::pair<BinaryStreamWriter, BinaryStreamWriter> split(uint32_t Off) const;
void setOffset(uint32_t Off) { Offset = Off; }
uint32_t getOffset() const { return Offset; }
uint32_t getLength() const { return Stream.getLength(); }
uint32_t bytesRemaining() const { return getLength() - getOffset(); }
Error padToAlignment(uint32_t Align);
protected:
WritableBinaryStreamRef Stream;
uint32_t Offset = 0;
};
} // end namespace llvm
#endif // LLVM_SUPPORT_BINARYSTREAMWRITER_H