Comment BinaryRef::Data.
[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   bool isBinary;
31
32 public:
33   BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), isBinary(true) {}
34   BinaryRef(StringRef Data)
35       : Data(reinterpret_cast<const uint8_t *>(Data.data()), Data.size()),
36         isBinary(false) {}
37   BinaryRef() : isBinary(false) {}
38   StringRef getHex() const {
39     assert(!isBinary);
40     return StringRef(reinterpret_cast<const char *>(Data.data()), Data.size());
41   }
42   ArrayRef<uint8_t> getBinary() const {
43     assert(isBinary);
44     return Data;
45   }
46   bool operator==(const BinaryRef &Ref) {
47     // Special case for default constructed BinaryRef.
48     if (Ref.Data.empty() && Data.empty())
49       return true;
50
51     return Ref.isBinary == isBinary && Ref.Data == Data;
52   }
53   /// \brief Write the contents (regardless of whether it is binary or a
54   /// hex string) as binary to the given raw_ostream.
55   void writeAsBinary(raw_ostream &OS) const;
56 };
57
58 }
59 }
60
61 namespace yaml {
62 template <> struct ScalarTraits<object::yaml::BinaryRef> {
63   static void output(const object::yaml::BinaryRef &, void *,
64                      llvm::raw_ostream &);
65   static StringRef input(StringRef, void *, object::yaml::BinaryRef &);
66 };
67 }
68
69 }
70
71 #endif