Home | History | Annotate | Download | only in Target
      1 //===- SymbolEntryMap.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_TARGET_SYMBOL_ENTRY_MAP_H
     10 #define MCLD_TARGET_SYMBOL_ENTRY_MAP_H
     11 #ifdef ENABLE_UNITTEST
     12 #include <gtest.h>
     13 #endif
     14 
     15 #include <vector>
     16 
     17 namespace mcld {
     18 
     19 class ResolveInfo;
     20 
     21 /** \class SymbolEntryMap
     22  *  \brief SymbolEntryMap is a <const ResolveInfo*, ENTRY*> map.
     23  */
     24 template<typename ENTRY>
     25 class SymbolEntryMap
     26 {
     27 public:
     28   typedef ENTRY EntryType;
     29 
     30 private:
     31   struct Mapping {
     32     const ResolveInfo* symbol;
     33     EntryType*   entry;
     34   };
     35 
     36   typedef std::vector<Mapping> SymbolEntryPool;
     37 
     38 public:
     39   typedef typename SymbolEntryPool::iterator iterator;
     40   typedef typename SymbolEntryPool::const_iterator const_iterator;
     41 
     42 public:
     43   const EntryType* lookUp(const ResolveInfo& pSymbol) const;
     44   EntryType*       lookUp(const ResolveInfo& pSymbol);
     45 
     46   void record(const ResolveInfo& pSymbol, EntryType& pEntry);
     47 
     48   bool   empty() const { return m_Pool.empty(); }
     49   size_t size () const { return m_Pool.size(); }
     50 
     51   const_iterator begin() const { return m_Pool.begin(); }
     52   iterator       begin()       { return m_Pool.begin(); }
     53   const_iterator end  () const { return m_Pool.end();   }
     54   iterator       end  ()       { return m_Pool.end();   }
     55 
     56   void reserve(size_t pSize) { m_Pool.reserve(pSize); }
     57 
     58 private:
     59   SymbolEntryPool m_Pool;
     60 
     61 };
     62 
     63 template<typename EntryType>
     64 const EntryType*
     65 SymbolEntryMap<EntryType>::lookUp(const ResolveInfo& pSymbol) const
     66 {
     67   const_iterator mapping, mEnd = m_Pool.end();
     68   for (mapping = m_Pool.begin(); mapping != mEnd; ++mapping) {
     69     if (mapping->symbol == &pSymbol) {
     70       return mapping->entry;
     71     }
     72   }
     73 
     74   return NULL;
     75 }
     76 
     77 template<typename EntryType>
     78 EntryType*
     79 SymbolEntryMap<EntryType>::lookUp(const ResolveInfo& pSymbol)
     80 {
     81   iterator mapping, mEnd = m_Pool.end();
     82   for (mapping = m_Pool.begin(); mapping != mEnd; ++mapping) {
     83     if (mapping->symbol == &pSymbol) {
     84       return mapping->entry;
     85     }
     86   }
     87 
     88   return NULL;
     89 }
     90 
     91 template<typename EntryType>
     92 void
     93 SymbolEntryMap<EntryType>::record(const ResolveInfo& pSymbol, EntryType& pEntry)
     94 {
     95   Mapping mapping;
     96   mapping.symbol = &pSymbol;
     97   mapping.entry = &pEntry;
     98   m_Pool.push_back(mapping);
     99 }
    100 
    101 } // namespace of mcld
    102 
    103 #endif
    104 
    105