1 //===- EndianStream.h - Stream ops with endian specific data ----*- 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 // This file defines utilities for operating on streams that have endian 11 // specific data. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_SUPPORT_ENDIANSTREAM_H 16 #define LLVM_SUPPORT_ENDIANSTREAM_H 17 18 #include "llvm/Support/Endian.h" 19 #include "llvm/Support/raw_ostream.h" 20 21 namespace llvm { 22 namespace support { 23 24 namespace endian { 25 /// Adapter to write values to a stream in a particular byte order. 26 template <endianness endian> struct Writer { 27 raw_ostream &OS; WriterWriter28 Writer(raw_ostream &OS) : OS(OS) {} writeWriter29 template <typename value_type> void write(value_type Val) { 30 Val = byte_swap<value_type, endian>(Val); 31 OS.write((const char *)&Val, sizeof(value_type)); 32 } 33 }; 34 35 template <> 36 template <> 37 inline void Writer<little>::write<float>(float Val) { 38 write(FloatToBits(Val)); 39 } 40 41 template <> 42 template <> 43 inline void Writer<little>::write<double>(double Val) { 44 write(DoubleToBits(Val)); 45 } 46 47 template <> 48 template <> 49 inline void Writer<big>::write<float>(float Val) { 50 write(FloatToBits(Val)); 51 } 52 53 template <> 54 template <> 55 inline void Writer<big>::write<double>(double Val) { 56 write(DoubleToBits(Val)); 57 } 58 59 } // end namespace endian 60 61 } // end namespace support 62 } // end namespace llvm 63 64 #endif 65