Add writeAsHex(raw_ostream &) method to BinaryRef.
[oota-llvm.git] / include / llvm / Object / YAML.h
1 //===- YAML.h - YAMLIO utilities for object files ---------------*- 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 declares utility classes for handling the YAML representation of
11 // object files.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_OBJECT_YAML_H
16 #define LLVM_OBJECT_YAML_H
17
18 #include "llvm/Support/YAMLTraits.h"
19
20 namespace llvm {
21 namespace object {
22 namespace yaml {
23
24 /// In an object file this is just a binary blob. In an yaml file it is an hex
25 /// string. Using this avoid having to allocate temporary strings.
26 class BinaryRef {
27   /// \brief Either raw binary data, or a string of hex bytes (must always
28   /// be an even number of characters).
29   ArrayRef<uint8_t> Data;
30   /// \brief Discriminator between the two states of the `Data` member.
31   bool DataIsHexString;
32
33 public:
34   BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), DataIsHexString(false) {}
35   BinaryRef(StringRef Data)
36       : Data(reinterpret_cast<const uint8_t *>(Data.data()), Data.size()),
37         DataIsHexString(true) {}
38   BinaryRef() : DataIsHexString(true) {}
39   StringRef getHex() const {
40     assert(DataIsHexString);
41     return StringRef(reinterpret_cast<const char *>(Data.data()), Data.size());
42   }
43   ArrayRef<uint8_t> getBinary() const {
44     assert(!DataIsHexString);
45     return Data;
46   }
47   /// \brief The number of bytes that are represented by this BinaryRef.
48   /// This is the number of bytes that writeAsBinary() will write.
49   ArrayRef<uint8_t>::size_type binary_size() const {
50     if (DataIsHexString)
51       return Data.size() / 2;
52     return Data.size();
53   }
54   bool operator==(const BinaryRef &Ref) {
55     // Special case for default constructed BinaryRef.
56     if (Ref.Data.empty() && Data.empty())
57       return true;
58
59     return Ref.DataIsHexString == DataIsHexString && Ref.Data == Data;
60   }
61   /// \brief Write the contents (regardless of whether it is binary or a
62   /// hex string) as binary to the given raw_ostream.
63   void writeAsBinary(raw_ostream &OS) const;
64   /// \brief Write the contents (regardless of whether it is binary or a
65   /// hex string) as hex to the given raw_ostream.
66   ///
67   /// For example, a possible output could be `DEADBEEFCAFEBABE`.
68   void writeAsHex(raw_ostream &OS) const;
69 };
70
71 }
72 }
73
74 namespace yaml {
75 template <> struct ScalarTraits<object::yaml::BinaryRef> {
76   static void output(const object::yaml::BinaryRef &, void *,
77                      llvm::raw_ostream &);
78   static StringRef input(StringRef, void *, object::yaml::BinaryRef &);
79 };
80 }
81
82 }
83
84 #endif