LookupTable.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SRC_BASE_LOOKUPTABLE_H
10 #define SQUID_SRC_BASE_LOOKUPTABLE_H
11 
12 #include "sbuf/Algorithms.h"
13 #include "sbuf/SBuf.h"
14 
15 #include <unordered_map>
16 
26 template <typename EnumType>
28 {
29  const char *name;
30  EnumType id;
31 };
32 
50 template<typename EnumType, typename RecordType = LookupTableRecord<EnumType>, typename Hasher = CaseInsensitiveSBufHash >
52 {
53 public:
55  typedef RecordType Record;
56 
57  LookupTable(const EnumType theInvalid, const Record data[]) :
58  invalidValue(theInvalid)
59  {
60  for (auto i = 0; data[i].name != nullptr; ++i) {
61  lookupTable[SBuf(data[i].name)] = data[i].id;
62  }
63  }
64 
65  EnumType lookup(const SBuf &key) const {
66  auto r = lookupTable.find(key);
67  if (r == lookupTable.end())
68  return invalidValue;
69  return r->second;
70  }
71 
72 private:
73  using lookupTable_t = std::unordered_map<const SBuf, EnumType, Hasher, CaseInsensitiveSBufEqual>;
75  EnumType invalidValue;
76 };
77 
78 #endif /* SQUID_SRC_BASE_LOOKUPTABLE_H */
79 
Definition: SBuf.h:93
EnumType invalidValue
Definition: LookupTable.h:75
EnumType lookup(const SBuf &key) const
Definition: LookupTable.h:65
std::unordered_map< const SBuf, EnumType, Hasher, CaseInsensitiveSBufEqual > lookupTable_t
Definition: LookupTable.h:73
lookupTable_t lookupTable
Definition: LookupTable.h:74
LookupTable(const EnumType theInvalid, const Record data[])
Definition: LookupTable.h:57
const char * name
Definition: LookupTable.h:29
RecordType Record
element of the lookup table initialization list
Definition: LookupTable.h:55

 

Introduction

Documentation

Support

Miscellaneous