1 //===- EndianStream.h - Stream ops with endian specific data ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines utilities for operating on streams that have endian
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_ENDIANSTREAM_H
16 #define LLVM_SUPPORT_ENDIANSTREAM_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/raw_ostream.h"
27 template <typename value_type>
28 inline void write(raw_ostream &os, value_type value, endianness endian) {
29 value = byte_swap<value_type>(value, endian);
30 os.write((const char *)&value, sizeof(value_type));
34 inline void write<float>(raw_ostream &os, float value, endianness endian) {
35 write(os, FloatToBits(value), endian);
39 inline void write<double>(raw_ostream &os, double value,
41 write(os, DoubleToBits(value), endian);
44 template <typename value_type>
45 inline void write(raw_ostream &os, ArrayRef<value_type> vals,
47 for (value_type v : vals)
51 /// Adapter to write values to a stream in a particular byte order.
55 Writer(raw_ostream &OS, endianness Endian) : OS(OS), Endian(Endian) {}
56 template <typename value_type> void write(ArrayRef<value_type> Val) {
57 endian::write(OS, Val, Endian);
59 template <typename value_type> void write(value_type Val) {
60 endian::write(OS, Val, Endian);
64 } // end namespace endian
66 } // end namespace support
67 } // end namespace llvm