1 //===- EndianStream.h - Stream ops with endian specific data ----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines utilities for operating on streams that have endian
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_ENDIANSTREAM_H
15 #define LLVM_SUPPORT_ENDIANSTREAM_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/raw_ostream.h"
26 template <typename value_type>
27 inline void write(raw_ostream &os, value_type value, endianness endian) {
28 value = byte_swap<value_type>(value, endian);
29 os.write((const char *)&value, sizeof(value_type));
33 inline void write<float>(raw_ostream &os, float value, endianness endian) {
34 write(os, FloatToBits(value), endian);
38 inline void write<double>(raw_ostream &os, double value,
40 write(os, DoubleToBits(value), endian);
43 template <typename value_type>
44 inline void write(raw_ostream &os, ArrayRef<value_type> vals,
46 for (value_type v : vals)
50 /// Adapter to write values to a stream in a particular byte order.
54 Writer(raw_ostream &OS, endianness Endian) : OS(OS), Endian(Endian) {}
55 template <typename value_type> void write(ArrayRef<value_type> Val) {
56 endian::write(OS, Val, Endian);
58 template <typename value_type> void write(value_type Val) {
59 endian::write(OS, Val, Endian);
63 } // end namespace endian
65 } // end namespace support
66 } // end namespace llvm