LLVM  3.7.0
StringTableBuilder.h
Go to the documentation of this file.
1 //===-- StringTableBuilder.h - String table building utility ------*- 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 #ifndef LLVM_MC_STRINGTABLEBUILDER_H
11 #define LLVM_MC_STRINGTABLEBUILDER_H
12 
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringMap.h"
15 #include <cassert>
16 
17 namespace llvm {
18 
19 /// \brief Utility for building string tables with deduplicated suffixes.
21  SmallString<256> StringTable;
22  StringMap<size_t> StringIndexMap;
23 
24 public:
25  /// \brief Add a string to the builder. Returns a StringRef to the internal
26  /// copy of s. Can only be used before the table is finalized.
28  assert(!isFinalized());
29  return StringIndexMap.insert(std::make_pair(s, 0)).first->first();
30  }
31 
32  enum Kind {
33  ELF,
36  };
37 
38  /// \brief Analyze the strings and build the final table. No more strings can
39  /// be added after this point.
40  void finalize(Kind kind);
41 
42  /// \brief Retrieve the string table data. Can only be used after the table
43  /// is finalized.
45  assert(isFinalized());
46  return StringTable;
47  }
48 
49  /// \brief Get the offest of a string in the string table. Can only be used
50  /// after the table is finalized.
51  size_t getOffset(StringRef s) {
52  assert(isFinalized());
53  assert(StringIndexMap.count(s) && "String is not in table!");
54  return StringIndexMap[s];
55  }
56 
57  void clear();
58 
59 private:
60  bool isFinalized() {
61  return !StringTable.empty();
62  }
63 };
64 
65 } // end llvm namespace
66 
67 #endif
void finalize(Kind kind)
Analyze the strings and build the final table.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:291
Utility for building string tables with deduplicated suffixes.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
StringRef add(StringRef s)
Add a string to the builder.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:298
size_t getOffset(StringRef s)
Get the offest of a string in the string table.
StringRef data()
Retrieve the string table data.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40