LLVM API Documentation

StringTableBuilder.h
Go to the documentation of this file.
00001 //===-- StringTableBuilder.h - String table building utility ------*- C++ -*-=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_MC_STRINGTABLEBUILDER_H
00011 #define LLVM_MC_STRINGTABLEBUILDER_H
00012 
00013 #include "llvm/ADT/SmallString.h"
00014 #include "llvm/ADT/StringMap.h"
00015 #include <cassert>
00016 
00017 namespace llvm {
00018 
00019 /// \brief Utility for building string tables with deduplicated suffixes.
00020 class StringTableBuilder {
00021   SmallString<256> StringTable;
00022   StringMap<size_t> StringIndexMap;
00023 
00024 public:
00025   /// \brief Add a string to the builder. Returns a StringRef to the internal
00026   /// copy of s. Can only be used before the table is finalized.
00027   StringRef add(StringRef s) {
00028     assert(!isFinalized());
00029     return StringIndexMap.GetOrCreateValue(s, 0).getKey();
00030   }
00031 
00032   /// \brief Analyze the strings and build the final table. No more strings can
00033   /// be added after this point.
00034   void finalize();
00035 
00036   /// \brief Retrieve the string table data. Can only be used after the table
00037   /// is finalized.
00038   StringRef data() {
00039     assert(isFinalized());
00040     return StringTable;
00041   }
00042 
00043   /// \brief Get the offest of a string in the string table. Can only be used
00044   /// after the table is finalized.
00045   size_t getOffset(StringRef s) {
00046     assert(isFinalized());
00047     assert(StringIndexMap.count(s) && "String is not in table!");
00048     return StringIndexMap[s];
00049   }
00050 
00051 private:
00052   bool isFinalized() {
00053     return !StringTable.empty();
00054   }
00055 };
00056 
00057 } // end llvm namespace
00058 
00059 #endif