Object/Archive: Give Child a operator < for map.
[oota-llvm.git] / include / llvm / Object / Archive.h
1 //===- Archive.h - ar archive file format -----------------------*- 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 the ar archive file format class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_ARCHIVE_H
15 #define LLVM_OBJECT_ARCHIVE_H
16
17 #include "llvm/Object/Binary.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20
21 namespace llvm {
22 namespace object {
23
24 class Archive : public Binary {
25 public:
26   class Child {
27     const Archive *Parent;
28     StringRef Data;
29
30   public:
31     Child(const Archive *p, StringRef d) : Parent(p), Data(d) {}
32
33     bool operator ==(const Child &other) const {
34       return (Parent == other.Parent) && (Data.begin() == other.Data.begin());
35     }
36
37     bool operator <(const Child &other) const {
38       return Data.begin() < other.Data.begin();
39     }
40
41     Child getNext() const;
42     error_code getName(StringRef &Result) const;
43     int getLastModified() const;
44     int getUID() const;
45     int getGID() const;
46     int getAccessMode() const;
47     ///! Return the size of the archive member without the header or padding.
48     uint64_t getSize() const;
49
50     MemoryBuffer *getBuffer() const;
51     error_code getAsBinary(OwningPtr<Binary> &Result) const;
52   };
53
54   class child_iterator {
55     Child child;
56   public:
57     child_iterator() : child(Child(0, StringRef())) {}
58     child_iterator(const Child &c) : child(c) {}
59     const Child* operator->() const {
60       return &child;
61     }
62
63     bool operator==(const child_iterator &other) const {
64       return child == other.child;
65     }
66
67     bool operator!=(const child_iterator &other) const {
68       return !(*this == other);
69     }
70
71     bool operator <(const child_iterator &other) const {
72       return child < other.child;
73     }
74
75     child_iterator& operator++() {  // Preincrement
76       child = child.getNext();
77       return *this;
78     }
79   };
80
81   class Symbol {
82     const Archive *Parent;
83     uint32_t SymbolIndex;
84     uint32_t StringIndex; // Extra index to the string.
85
86   public:
87     bool operator ==(const Symbol &other) const {
88       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
89     }
90
91     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
92       : Parent(p)
93       , SymbolIndex(symi)
94       , StringIndex(stri) {}
95     error_code getName(StringRef &Result) const;
96     error_code getMember(child_iterator &Result) const;
97     Symbol getNext() const;
98   };
99
100   class symbol_iterator {
101     Symbol symbol;
102   public:
103     symbol_iterator(const Symbol &s) : symbol(s) {}
104     const Symbol *operator->() const {
105       return &symbol;
106     }
107
108     bool operator==(const symbol_iterator &other) const {
109       return symbol == other.symbol;
110     }
111
112     bool operator!=(const symbol_iterator &other) const {
113       return !(*this == other);
114     }
115
116     symbol_iterator& operator++() {  // Preincrement
117       symbol = symbol.getNext();
118       return *this;
119     }
120   };
121
122   Archive(MemoryBuffer *source, error_code &ec);
123
124   child_iterator begin_children(bool skip_internal = true) const;
125   child_iterator end_children() const;
126
127   symbol_iterator begin_symbols() const;
128   symbol_iterator end_symbols() const;
129
130   // Cast methods.
131   static inline bool classof(Archive const *v) { return true; }
132   static inline bool classof(Binary const *v) {
133     return v->getType() == Binary::isArchive;
134   }
135
136 private:
137   child_iterator SymbolTable;
138   child_iterator StringTable;
139 };
140
141 }
142 }
143
144 #endif