EndianStream.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #ifndef LLVM_SUPPORT_ENDIANSTREAM_H
  15. #define LLVM_SUPPORT_ENDIANSTREAM_H
  16. #include "llvm/Support/Endian.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. namespace llvm {
  19. namespace support {
  20. namespace endian {
  21. /// Adapter to write values to a stream in a particular byte order.
  22. template <endianness endian> struct Writer {
  23. raw_ostream &OS;
  24. Writer(raw_ostream &OS) : OS(OS) {}
  25. template <typename value_type> void write(value_type Val) {
  26. Val = byte_swap<value_type, endian>(Val);
  27. OS.write((const char *)&Val, sizeof(value_type));
  28. }
  29. };
  30. template <>
  31. template <>
  32. inline void Writer<little>::write<float>(float Val) {
  33. write(FloatToBits(Val));
  34. }
  35. template <>
  36. template <>
  37. inline void Writer<little>::write<double>(double Val) {
  38. write(DoubleToBits(Val));
  39. }
  40. template <>
  41. template <>
  42. inline void Writer<big>::write<float>(float Val) {
  43. write(FloatToBits(Val));
  44. }
  45. template <>
  46. template <>
  47. inline void Writer<big>::write<double>(double Val) {
  48. write(DoubleToBits(Val));
  49. }
  50. } // end namespace endian
  51. } // end namespace support
  52. } // end namespace llvm
  53. #endif