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 * a record in the initializer list for a LookupTable
19 * In case it is wished to extend the structure of a LookupTable's initializer
20 * list, it can be done by using a custom struct which must match
21 * LookupTableRecord's signature plus any extra custom fields the user may
22 * wish to add; the extended record type must then be passed as RecordType
23 * template parameter to LookupTable.
25 template <typename EnumType
>
26 struct LookupTableRecord
33 * SBuf -> case-insensitive enum lookup table
36 * enum enum_type { ... };
37 * static const LookupTable<enum_type>::Record initializerTable[] = {
38 * {"key1", ENUM_1}, {"key2", ENUM_2}, ... {nullptr, ENUM_INVALID_VALUE}
40 * LookupTable<enum_type> lookupTableInstance(ENUM_INVALID_VALUE, initializerTable);
43 * SBuf s(string_to_lookup);
44 * enum_type item = lookupTableInstance.lookup(s);
45 * if (item != ENUM_INVALID_VALUE) { // do stuff }
49 struct SBufCaseInsensitiveLess
: public std::binary_function
<SBuf
, SBuf
, bool> {
50 bool operator() (const SBuf
&x
, const SBuf
&y
) const {
51 return x
.caseCmp(y
) < 0;
54 template<typename EnumType
, typename RecordType
= LookupTableRecord
<EnumType
> >
58 /// element of the lookup table initialization list
59 typedef RecordType Record
;
61 LookupTable(const EnumType theInvalid
, const Record data
[]) :
62 invalidValue(theInvalid
)
64 for (auto i
= 0; data
[i
].name
!= nullptr; ++i
) {
65 lookupTable
[SBuf(data
[i
].name
)] = data
[i
].id
;
69 EnumType
lookup(const SBuf
&key
) const {
70 auto r
= lookupTable
.find(key
);
71 if (r
== lookupTable
.end())
77 typedef std::map
<const SBuf
, EnumType
, SBufCaseInsensitiveLess
> lookupTable_t
;
78 lookupTable_t lookupTable
;
79 EnumType invalidValue
;
82 #endif /* SQUID_LOOKUPTABLE_H_ */