HashEntry.h revision 5460a1f25d9ddecb5c70667267d66d51af177a99
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
16namespace mcld {
17
18/** forward declaration **/
19template<typename HashEntryTy>
20class 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 */
35template <typename KeyType, typename ValueType, typename KeyCompare>
36class HashEntry
37{
38public:
39  typedef KeyType key_type;
40  typedef ValueType value_type;
41  typedef KeyCompare key_compare;
42
43private:
44  typedef HashEntry<KeyType, ValueType, KeyCompare> Self;
45  friend class EntryFactory<Self>;
46
47private:
48  HashEntry(const KeyType& pKey);
49  ~HashEntry();
50
51public:
52  KeyType& key()
53  { return m_Key; }
54
55  const KeyType& key() const
56  { return m_Key; }
57
58  ValueType& value()
59  { return m_Value; }
60
61  const ValueType& value() const
62  { return m_Value; }
63
64  void setValue(const ValueType& pValue)
65  { m_Value = pValue; }
66
67  bool compare(const key_type& pKey);
68
69public:
70  KeyType m_Key;
71  ValueType m_Value;
72};
73
74template <typename HashEntryTy>
75class EntryFactory
76{
77public:
78  typedef HashEntryTy                      entry_type;
79  typedef typename HashEntryTy::key_type   key_type;
80  typedef typename HashEntryTy::value_type value_type;
81
82public:
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