]> git.cworth.org Git - apitrace/blob - common/trace_lookup.hpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / common / trace_lookup.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 /*
27  * Helper code for function name indexed lookup tables.
28  */
29
30 #ifndef _TRACE_LOOKUP_HPP_
31 #define _TRACE_LOOKUP_HPP_
32
33
34 #include <assert.h>
35 #include <string.h>
36
37 #include <algorithm>
38 #include <iostream>
39
40
41 namespace trace {
42
43
44 /**
45  * Generic type for (name, value) pairs.
46  */
47 template< class T >
48 struct Entry
49 {
50     const char *name;
51     T value;
52 };
53
54
55 /**
56  * Function object which compare entries by name.
57  */
58 template< class T >
59 struct EntryCompare {
60     inline bool
61     operator() (const Entry<T> & a, const Entry<T> & b) const {
62         return strcmp(a.name, b.name) < 0;
63     }
64 };
65
66
67 /**
68  * Lookup the entry with the given name, .
69  *
70  * The entry table must be sorted alphabetically (the same rules used by
71  * strcmp).
72  */
73 template< class T, std::size_t n >
74 inline const T &
75 entryLookup(const char *name, const Entry<T> (& entries)[n], const T & default_)
76 {
77     typedef const Entry<T> * ConstIterator;
78
79     ConstIterator first = &entries[0];
80     ConstIterator last = &entries[n];
81
82     assert(first != last);
83
84     Entry<T> reference;
85     reference.name = name;
86
87     EntryCompare<T> compare;
88
89 #ifndef NDEBUG
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";
94             assert(0);
95         }
96     }
97 #endif
98     
99     first = std::lower_bound(first, last, reference, compare);
100   
101     if (first == last || compare(reference, *first)) {
102         return default_;
103     }
104
105     return first->value;
106 }
107
108
109 } /* namespace trace */
110
111 #endif /* _TRACE_LOOKUP_HPP_ */