Line data Source code
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/ADT/ArrayRef.h"
19 : #include "llvm/Support/Endian.h"
20 : #include "llvm/Support/raw_ostream.h"
21 :
22 : namespace llvm {
23 : namespace support {
24 :
25 : namespace endian {
26 :
27 : template <typename value_type>
28 : inline void write(raw_ostream &os, value_type value, endianness endian) {
29 75760357 : value = byte_swap<value_type>(value, endian);
30 75762283 : os.write((const char *)&value, sizeof(value_type));
31 : }
32 :
33 : template <>
34 : inline void write<float>(raw_ostream &os, float value, endianness endian) {
35 9 : write(os, FloatToBits(value), endian);
36 : }
37 :
38 : template <>
39 : inline void write<double>(raw_ostream &os, double value,
40 : endianness endian) {
41 5 : write(os, DoubleToBits(value), endian);
42 : }
43 :
44 : template <typename value_type>
45 3 : inline void write(raw_ostream &os, ArrayRef<value_type> vals,
46 : endianness endian) {
47 410 : for (value_type v : vals)
48 384 : write(os, v, endian);
49 3 : }
50 1 :
51 : /// Adapter to write values to a stream in a particular byte order.
52 3 : struct Writer {
53 : raw_ostream &OS;
54 1 : endianness Endian;
55 44204 : Writer(raw_ostream &OS, endianness Endian) : OS(OS), Endian(Endian) {}
56 0 : template <typename value_type> void write(ArrayRef<value_type> Val) {
57 6 : endian::write(OS, Val, Endian);
58 8 : }
59 27847698 : template <typename value_type> void write(value_type Val) {
60 76286112 : endian::write(OS, Val, Endian);
61 27847696 : }
62 760526 : };
63 760526 :
64 760526 : } // end namespace endian
65 253591 :
66 253591 : } // end namespace support
67 253594 : } // end namespace llvm
68 26833502 :
69 26833502 : #endif
|