2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_LOOKUPTABLE_H_
10 #define SQUID_LOOKUPTABLE_H_
17 * SBuf -> enum lookup table
20 * enum enum_type { ... };
21 * static const LookupTable<enum_type>::Record initializerTable[] {
22 * {"key1", ENUM_1}, {"key2", ENUM_2}, ... {nullptr, ENUM_INVALID_VALUE}
24 * LookupTable<enum_type> lookupTableInstance(ENUM_INVALID_VALUE, initializerTable);
27 * SBuf s(string_to_lookup);
28 * enum_type item = lookupTableInstance.lookup(s);
29 * if (item != ENUM_INVALID_VALUE) { // do stuff }
32 template<typename EnumType
>
36 /// element of the lookup table initialization list
42 LookupTable(const EnumType theInvalid
, const Record data
[]) :
43 invalidValue(theInvalid
)
45 for (auto i
= 0; data
[i
].name
!= nullptr; ++i
) {
46 lookupTable
[SBuf(data
[i
].name
)] = data
[i
].id
;
49 EnumType
lookup(const SBuf
&key
) const {
50 auto r
= lookupTable
.find(key
);
51 if (r
== lookupTable
.end())
57 typedef std::map
<const SBuf
, EnumType
> lookupTable_t
;
58 lookupTable_t lookupTable
;
59 EnumType invalidValue
;
62 #endif /* SQUID_LOOKUPTABLE_H_ */