LookupTable.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 
26 template <typename EnumType>
28 {
29  const char *name;
30  EnumType id;
31 };
32 
50 class SBufCaseInsensitiveLess : public std::binary_function<SBuf, SBuf, bool> {
51 public:
52  bool operator() (const SBuf &x, const SBuf &y) const {
53  return x.caseCmp(y) < 0;
54  }
55 };
56 
57 template<typename EnumType, typename RecordType = LookupTableRecord<EnumType>, typename Hasher = CaseInsensitiveSBufHash >
59 {
60 public:
62  typedef RecordType Record;
63 
64  LookupTable(const EnumType theInvalid, const Record data[]) :
65  invalidValue(theInvalid)
66  {
67  for (auto i = 0; data[i].name != nullptr; ++i) {
68  lookupTable[SBuf(data[i].name)] = data[i].id;
69  }
70  }
71 
72  EnumType lookup(const SBuf &key) const {
73  auto r = lookupTable.find(key);
74  if (r == lookupTable.end())
75  return invalidValue;
76  return r->second;
77  }
78 
79 private:
80  typedef std::unordered_map<const SBuf, EnumType, Hasher> lookupTable_t;
82  EnumType invalidValue;
83 };
84 
85 #endif /* SQUID_LOOKUPTABLE_H_ */
86 
int caseCmp(const SBuf &S, const size_type n) const
shorthand version for case-insensitive compare()
Definition: SBuf.h:273
Definition: SBuf.h:87
int i
Definition: membanger.c:49
LookupTable(const EnumType theInvalid, const Record data[])
Definition: LookupTable.h:64
bool operator()(const SBuf &x, const SBuf &y) const
Definition: LookupTable.h:52
lookupTable_t lookupTable
Definition: LookupTable.h:81
EnumType lookup(const SBuf &key) const
Definition: LookupTable.h:72
void const char HLPCB void * data
Definition: stub_helper.cc:16
RecordType Record
element of the lookup table initialization list
Definition: LookupTable.h:62
std::unordered_map< const SBuf, EnumType, Hasher > lookupTable_t
Definition: LookupTable.h:80
void const cache_key * key
EnumType invalidValue
Definition: LookupTable.h:82
const char * name
Definition: LookupTable.h:29

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors