1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
27 * Helper code for function name indexed lookup tables.
30 #ifndef _TRACE_LOOKUP_HPP_
31 #define _TRACE_LOOKUP_HPP_
45 * Generic type for (name, value) pairs.
56 * Function object which compare entries by name.
61 operator() (const Entry<T> & a, const Entry<T> & b) const {
62 return strcmp(a.name, b.name) < 0;
68 * Lookup the entry with the given name, .
70 * The entry table must be sorted alphabetically (the same rules used by
73 template< class T, std::size_t n >
75 entryLookup(const char *name, const Entry<T> (& entries)[n], const T & default_)
77 typedef const Entry<T> * ConstIterator;
79 ConstIterator first = &entries[0];
80 ConstIterator last = &entries[n];
82 assert(first != last);
85 reference.name = name;
87 EntryCompare<T> compare;
90 for (ConstIterator it = first; it != last; ++it) {
91 ConstIterator next = it + 1;
92 if (next != last && !compare(*it, *next)) {
93 std::cerr << "error: " << it->name << " and " << next->name << " not properly sorted\n";
99 first = std::lower_bound(first, last, reference, compare);
101 if (first == last || compare(reference, *first)) {
109 } /* namespace trace */
111 #endif /* _TRACE_LOOKUP_HPP_ */