LookupTable.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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_LOOKUPTABLE_H_
10#define SQUID_LOOKUPTABLE_H_
11
12#include "sbuf/Algorithms.h"
13#include "sbuf/SBuf.h"
14
15#include <unordered_map>
16
26template <typename EnumType>
28{
29 const char *name;
30 EnumType id;
31};
32
50template<typename EnumType, typename RecordType = LookupTableRecord<EnumType>, typename Hasher = CaseInsensitiveSBufHash >
52{
53public:
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
72private:
73 typedef std::unordered_map<const SBuf, EnumType, Hasher> lookupTable_t;
75 EnumType invalidValue;
76};
77
78#endif /* SQUID_LOOKUPTABLE_H_ */
79
lookupTable_t lookupTable
Definition: LookupTable.h:74
RecordType Record
element of the lookup table initialization list
Definition: LookupTable.h:55
EnumType invalidValue
Definition: LookupTable.h:75
std::unordered_map< const SBuf, EnumType, Hasher > lookupTable_t
Definition: LookupTable.h:73
EnumType lookup(const SBuf &key) const
Definition: LookupTable.h:65
LookupTable(const EnumType theInvalid, const Record data[])
Definition: LookupTable.h:57
Definition: SBuf.h:94
const char * name
Definition: LookupTable.h:29

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors