summaryrefslogtreecommitdiff
path: root/include/mcld/ADT/HashEntry.h
blob: c034783d2aabc637b213e8bac6ab2f99ce230859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//===- HashEntry.h ---------------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef MCLD_HASH_ENTRY_H
#define MCLD_HASH_ENTRY_H
#ifdef ENABLE_UNITTEST
#include <gtest.h>
#endif

namespace mcld {

/** forward declaration **/
template<typename HashEntryTy>
class EntryFactory;

/** \class HashEntry
 *  \brief HashEntry is the item in the bucket of hash table.
 *
 *  mcld::HashEntry illustrates the demand from mcld::HashTable.
 *  Since HashTable can change the definition of the HashEntry by changing
 *  the template argument. class mcld::HashEntry here is used to show the
 *  basic interfaces that HashTable requests. You can define your own entry
 *  of the hash table which has no relation to mcld::HashEntry
 *
 *  Since mcld::HashEntry here is a special class whose size is changing,
 *  derive a new class from it is risky. Make sure you understand what you
 *  are doing when you let a new class inherit from mcld::HashEntry.
 */
template <typename KeyType, typename ValueType, typename KeyCompare>
class HashEntry
{
public:
  typedef KeyType key_type;
  typedef ValueType value_type;
  typedef KeyCompare key_compare;

private:
  typedef HashEntry<KeyType, ValueType, KeyCompare> Self;
  friend class EntryFactory<Self>;

private:
  HashEntry(const KeyType& pKey);
  ~HashEntry();

public:
  KeyType& key()
  { return m_Key; }

  const KeyType& key() const
  { return m_Key; }

  ValueType& value()
  { return m_Value; }

  const ValueType& value() const
  { return m_Value; }

  void setValue(const ValueType& pValue)
  { m_Value = pValue; }

  bool compare(const key_type& pKey);

public:
  KeyType m_Key;
  ValueType m_Value;
};

template <typename HashEntryTy>
class EntryFactory
{
public:
  typedef HashEntryTy                      entry_type;
  typedef typename HashEntryTy::key_type   key_type;
  typedef typename HashEntryTy::value_type value_type;

public:
  EntryFactory();
  ~EntryFactory();

  HashEntryTy* produce(const key_type& pKey);
  void destroy(HashEntryTy* pEntry);
};

#include "HashEntry.tcc"

} // namespace of mcld

#endif