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