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"
26 /// Adapter to write values to a stream in a particular byte order.
27 template <endianness endian> struct Writer {
29 Writer(raw_ostream &OS) : OS(OS) {}
30 template <typename value_type> void write(ArrayRef<value_type> Vals) {
31 for (value_type V : Vals)
34 template <typename value_type> void write(value_type Val) {
35 Val = byte_swap<value_type, endian>(Val);
36 OS.write((const char *)&Val, sizeof(value_type));
42 inline void Writer<little>::write<float>(float Val) {
43 write(FloatToBits(Val));
48 inline void Writer<little>::write<double>(double Val) {
49 write(DoubleToBits(Val));
54 inline void Writer<big>::write<float>(float Val) {
55 write(FloatToBits(Val));
60 inline void Writer<big>::write<double>(double Val) {
61 write(DoubleToBits(Val));
64 } // end namespace endian
66 } // end namespace support
67 } // end namespace llvm