1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-22 12:33:33 +02:00
llvm-mirror/include/llvm/Support/BinaryStreamWriter.h
Zachary Turner 28d3fba1b3 Resubmit r301986 and r301987 "Add codeview::StringTable"
This was reverted due to a "missing" file, but in reality
what happened was that I renamed a file, and then due to
a merge conflict both the old file and the new file got
added to the repository.  This led to an unused cpp file
being in the repo and not referenced by any CMakeLists.txt
but #including a .h file that wasn't in the repo.  In an
even more unfortunate coincidence, CMake didn't report the
unused cpp file because it was in a subdirectory of the
folder with the CMakeLists.txt, and not in the same directory
as any CMakeLists.txt.

The presence of the unused file was then breaking certain
tools that determine file lists by globbing rather than
by what's specified in CMakeLists.txt

In any case, the fix is to just remove the unused file from
the patch set.

llvm-svn: 302042
2017-05-03 15:58:37 +00:00

171 lines
6.9 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 Stream);
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