1 //===- StringEntry.h -------------------------------------------------------===// 2 // 3 // The MCLinker Project 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #ifndef MCLD_ADT_STRINGENTRY_H 10 #define MCLD_ADT_STRINGENTRY_H 11 #include <llvm/ADT/StringRef.h> 12 #include <llvm/Support/DataTypes.h> 13 #include <cstdlib> 14 #include <cstring> 15 #include <cassert> 16 17 namespace mcld 18 { 19 template<typename DataType> 20 class StringEntryFactory; 21 22 /** \class StringEntry 23 * \brief StringEntry is a pair of strings which is designed for high locality. 24 */ 25 template<typename DataType> 26 class StringEntry 27 { 28 public: 29 typedef llvm::StringRef key_type; 30 typedef DataType value_type; 31 32 public: 33 key_type key() 34 { return key_type(m_Key, m_KeyLen); } 35 36 const key_type key() const 37 { return key_type(m_Key, m_KeyLen); } 38 39 value_type& value() 40 { return m_Value; } 41 42 const value_type& value() const 43 { return m_Value; } 44 45 size_t getKeyLength() const 46 { return m_KeyLen; } 47 48 size_t getValueLength() const 49 { return m_Value.size(); } 50 51 void setValue(const DataType& pVal) 52 { m_Value = pVal; } 53 54 bool compare(const llvm::StringRef& pX) 55 { return (0 == key().compare(pX)); } 56 57 bool compare(const llvm::StringRef& pX) const 58 { return (0 == key().compare(pX)); } 59 60 private: 61 StringEntry(); 62 StringEntry(const key_type& pKey); 63 StringEntry(const StringEntry& pCopy); 64 ~StringEntry(); 65 66 private: 67 DataType m_Value; 68 uint16_t m_KeyLen; 69 char m_Key[]; 70 71 friend class StringEntryFactory<DataType>; 72 }; 73 74 75 template<> 76 class StringEntry<llvm::StringRef> 77 { 78 public: 79 typedef llvm::StringRef key_type; 80 typedef llvm::StringRef value_type; 81 82 public: 83 key_type key() 84 { return key_type(m_Key, m_KeyLen); } 85 86 const key_type key() const 87 { return key_type(m_Key, m_KeyLen); } 88 89 value_type& value() 90 { return m_Value; } 91 92 const value_type& value() const 93 { return m_Value; } 94 95 size_t getKeyLength() const 96 { return m_KeyLen; } 97 98 size_t getValueLength() const 99 { return m_Value.size(); } 100 101 void setValue(const std::string& pVal) 102 { setValue(pVal.c_str()); } 103 104 void setValue(const char* pVal); 105 106 void setValue(llvm::StringRef& pVal); 107 108 bool compare(const llvm::StringRef& pX) 109 { return (0 == key().compare(pX)); } 110 111 bool compare(const llvm::StringRef& pX) const 112 { return (0 == key().compare(pX)); } 113 114 private: 115 StringEntry(); 116 StringEntry(const key_type& pKey); 117 StringEntry(const StringEntry& pCopy); 118 ~StringEntry(); 119 120 private: 121 llvm::StringRef m_Value; 122 uint16_t m_KeyLen; 123 char m_Key[]; 124 125 friend class StringEntryFactory<llvm::StringRef>; 126 }; 127 128 template<typename DataType> 129 class StringEntryFactory 130 { 131 public: 132 typedef StringEntry<DataType> entry_type; 133 typedef typename StringEntry<DataType>::key_type key_type; 134 typedef typename StringEntry<DataType>::value_type value_type; 135 136 public: 137 StringEntryFactory(); 138 ~StringEntryFactory(); 139 140 StringEntry<DataType>* produce(const key_type& pKey); 141 void destroy(StringEntry<DataType>* pEntry); 142 }; 143 144 #include "StringEntry.tcc" 145 146 } // namespace of mcld 147 148 #endif 149 150