1 //===- HashEntry.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 
10 #ifndef MCLD_ADT_HASHENTRY_H_
11 #define MCLD_ADT_HASHENTRY_H_
12 
13 namespace mcld {
14 
15 /** forward declaration **/
16 template <typename HashEntryTy>
17 class EntryFactory;
18 
19 /** \class HashEntry
20  *  \brief HashEntry is the item in the bucket of hash table.
21  *
22  *  mcld::HashEntry illustrates the demand from mcld::HashTable.
23  *  Since HashTable can change the definition of the HashEntry by changing
24  *  the template argument. class mcld::HashEntry here is used to show the
25  *  basic interfaces that HashTable requests. You can define your own entry
26  *  of the hash table which has no relation to mcld::HashEntry
27  *
28  *  Since mcld::HashEntry here is a special class whose size is changing,
29  *  derive a new class from it is risky. Make sure you understand what you
30  *  are doing when you let a new class inherit from mcld::HashEntry.
31  */
32 template <typename KeyType, typename ValueType, typename KeyCompare>
33 class HashEntry {
34  public:
35   typedef KeyType key_type;
36   typedef ValueType value_type;
37   typedef KeyCompare key_compare;
38 
39  private:
40   typedef HashEntry<KeyType, ValueType, KeyCompare> Self;
41   friend class EntryFactory<Self>;
42 
43  private:
44   explicit HashEntry(const KeyType& pKey);
45   ~HashEntry();
46 
47  public:
key()48   KeyType& key() { return m_Key; }
49 
key()50   const KeyType& key() const { return m_Key; }
51 
value()52   ValueType& value() { return m_Value; }
53 
value()54   const ValueType& value() const { return m_Value; }
55 
setValue(const ValueType & pValue)56   void setValue(const ValueType& pValue) { m_Value = pValue; }
57 
58   bool compare(const key_type& pKey);
59 
60  public:
61   KeyType m_Key;
62   ValueType m_Value;
63 };
64 
65 template <typename HashEntryTy>
66 class EntryFactory {
67  public:
68   typedef HashEntryTy entry_type;
69   typedef typename HashEntryTy::key_type key_type;
70   typedef typename HashEntryTy::value_type value_type;
71 
72  public:
73   EntryFactory();
74   ~EntryFactory();
75 
76   HashEntryTy* produce(const key_type& pKey);
77   void destroy(HashEntryTy* pEntry);
78 };
79 
80 #include "HashEntry.tcc"
81 
82 }  // namespace mcld
83 
84 #endif  // MCLD_ADT_HASHENTRY_H_
85