Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright (c) 2007, Google Inc. | 1 // Copyright (c) 2007, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
11 // copyright notice, this list of conditions and the following disclaimer | 11 // copyright notice, this list of conditions and the following disclaimer |
12 // in the documentation and/or other materials provided with the | 12 // in the documentation and/or other materials provided with the |
13 // distribution. | 13 // distribution. |
14 // * Neither the name of Google Inc. nor the names of its | 14 // * Neither the name of Google Inc. nor the names of its |
15 // contributors may be used to endorse or promote products derived from | 15 // contributors may be used to endorse or promote products derived from |
16 // this software without specific prior written permission. | 16 // this software without specific prior written permission. |
17 // | 17 // |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 #include <assert.h> | |
31 | |
32 #include "common/simple_string_dictionary.h" | 30 #include "common/simple_string_dictionary.h" |
33 | 31 |
34 namespace google_breakpad { | 32 namespace google_breakpad { |
35 | 33 |
36 //============================================================================== | 34 namespace { |
37 const KeyValueEntry *SimpleStringDictionary::GetEntry(int i) const { | |
38 return (i >= 0 && i < MAX_NUM_ENTRIES) ? &entries_[i] : NULL; | |
39 } | |
40 | 35 |
41 //============================================================================== | 36 // In C++98 (ISO 14882), section 9.5.1 says that a union cannot have a member |
42 int SimpleStringDictionary::GetCount() const { | 37 // with a non-trivial ctor, copy ctor, dtor, or assignment operator. Use this |
43 int count = 0; | 38 // property to ensure that Entry remains POD. |
44 for (int i = 0; i < MAX_NUM_ENTRIES; ++i) { | 39 union { |
Mark Mentovai
2013/04/24 15:15:57
Some compiler some day (today?) might warn about a
rsesek
2013/04/24 15:44:55
Done.
| |
45 if (entries_[i].IsActive() ) { | 40 NonAllocatingMap<1, 1, 1>::Entry Compile_Assert__entry_must_be_pod; |
46 ++count; | 41 }; |
47 } | |
48 } | |
49 | 42 |
50 return count; | |
51 } | |
52 | |
53 //============================================================================== | |
54 const char *SimpleStringDictionary::GetValueForKey(const char *key) const { | |
55 assert(key); | |
56 if (!key) | |
57 return NULL; | |
58 | |
59 for (int i = 0; i < MAX_NUM_ENTRIES; ++i) { | |
60 const KeyValueEntry &entry = entries_[i]; | |
61 if (entry.IsActive() && !strcmp(entry.GetKey(), key)) { | |
62 return entry.GetValue(); | |
63 } | |
64 } | |
65 | |
66 return NULL; | |
67 } | |
68 | |
69 //============================================================================== | |
70 void SimpleStringDictionary::SetKeyValue(const char *key, | |
71 const char *value) { | |
72 if (!value) { | |
73 RemoveKey(key); | |
74 return; | |
75 } | |
76 | |
77 // key must not be NULL | |
78 assert(key); | |
79 if (!key) | |
80 return; | |
81 | |
82 // key must not be empty string | |
83 assert(key[0] != '\0'); | |
84 if (key[0] == '\0') | |
85 return; | |
86 | |
87 int free_index = -1; | |
88 | |
89 // check if key already exists | |
90 for (int i = 0; i < MAX_NUM_ENTRIES; ++i) { | |
91 KeyValueEntry &entry = entries_[i]; | |
92 | |
93 if (entry.IsActive()) { | |
94 if (!strcmp(entry.GetKey(), key)) { | |
95 entry.SetValue(value); | |
96 return; | |
97 } | |
98 } else { | |
99 // Make a note of an empty slot | |
100 if (free_index == -1) { | |
101 free_index = i; | |
102 } | |
103 } | |
104 } | |
105 | |
106 // check if we've run out of space | |
107 assert(free_index != -1); | |
108 | |
109 // Put new key into an empty slot (if found) | |
110 if (free_index != -1) { | |
111 entries_[free_index].SetKeyValue(key, value); | |
112 } | |
113 } | |
114 | |
115 //============================================================================== | |
116 void SimpleStringDictionary::RemoveKey(const char *key) { | |
117 assert(key); | |
118 if (!key) | |
119 return; | |
120 | |
121 for (int i = 0; i < MAX_NUM_ENTRIES; ++i) { | |
122 if (!strcmp(entries_[i].GetKey(), key)) { | |
123 entries_[i].Clear(); | |
124 return; | |
125 } | |
126 } | |
127 } | 43 } |
128 | 44 |
129 } // namespace google_breakpad | 45 } // namespace google_breakpad |
Mark Mentovai
2013/04/24 15:15:57
Do you want to provide an explicit instantiation o
rsesek
2013/04/24 15:44:55
Yeah, I thought about that but decided maybe not.
| |
OLD | NEW |