LLVM  3.7.0
EndianStream.h
Go to the documentation of this file.
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"
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 {
28  Writer(raw_ostream &OS) : OS(OS) {}
29  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
void write(value_type Val)
Definition: EndianStream.h:29
uint32_t FloatToBits(float Float)
FloatToBits - This function takes a float and returns the bit equivalent 32-bit integer.
Definition: MathExtras.h:541
raw_ostream & write(unsigned char C)
uint64_t DoubleToBits(double Double)
DoubleToBits - This function takes a double and returns the bit equivalent 64-bit integer...
Definition: MathExtras.h:528
void write(void *memory, value_type value)
Write a value to memory with a particular endianness.
Definition: Endian.h:73
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:26
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38