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
17namespace mcld {
18
19class ResolveInfo;
20
21/** \class SymbolEntryMap
22 *  \brief SymbolEntryMap is a <const ResolveInfo*, ENTRY*> map.
23 */
24template<typename ENTRY>
25class SymbolEntryMap
26{
27public:
28  typedef ENTRY EntryType;
29
30private:
31  struct Mapping {
32    const ResolveInfo* symbol;
33    EntryType*   entry;
34  };
35
36  typedef std::vector<Mapping> SymbolEntryPool;
37
38public:
39  typedef typename SymbolEntryPool::iterator iterator;
40  typedef typename SymbolEntryPool::const_iterator const_iterator;
41
42public:
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
58private:
59  SymbolEntryPool m_Pool;
60
61};
62
63template<typename EntryType>
64const EntryType*
65SymbolEntryMap<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
77template<typename EntryType>
78EntryType*
79SymbolEntryMap<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
91template<typename EntryType>
92void
93SymbolEntryMap<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