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