OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 // | |
5 // Utility functions. | |
6 | |
7 #ifndef CHROME_BROWSER_SYNC_NOTIFIER_BASE_UTILS_H_ | |
8 #define CHROME_BROWSER_SYNC_NOTIFIER_BASE_UTILS_H_ | |
9 | |
10 #include <map> | |
11 #include <string> | |
12 | |
13 #include "chrome/browser/sync/notifier/base/static_assert.h" | |
14 | |
15 // Return error if the first argument evaluates to false. | |
16 #define RET_IF_FALSE(x) do { if (!(x)) return false; } while (false) | |
17 | |
18 // Protocol constants. | |
19 const char kHttpProto[] = "http://"; | |
20 const char kHttpsProto[] = "https://"; | |
21 | |
22 // Initialize a POD to zero. Using this function requires discipline. Don't | |
23 // use for types that have a v-table or virtual bases. | |
24 template <typename T> | |
25 inline void SetZero(T& p) { | |
26 // Guard against the easy mistake of | |
27 // foo(int *p) { SetZero(p); } instead of | |
28 // SetZero(*p); | |
29 // which it should be. | |
30 STATIC_ASSERT(sizeof(T) != sizeof(void*)); | |
31 | |
32 // A POD (plain old data) object has one of these data types: | |
33 // a fundamental type, union, struct, array, | |
34 // or class--with no constructor. PODs don't have virtual functions or | |
35 // virtual bases. | |
36 | |
37 // Test to see if the type has constructors. | |
38 union CtorTest { | |
39 T t; | |
40 int i; | |
41 }; | |
42 | |
43 // TODO(sync) There might be a way to test if the type has virtuals. | |
44 // For now, if we zero a type with virtuals by mistake, it is going to crash | |
45 // predictable at run-time when the virtuals are called. | |
46 memset(&p, 0, sizeof(T)); | |
47 } | |
48 | |
49 // Used to delete each element in a vector<T*>/deque<T*> (and then empty the | |
50 // sequence). | |
51 template <class T> | |
52 void CleanupSequence(T* items) { | |
53 for (typename T::iterator it(items->begin()); it != items->end(); ++it) { | |
54 delete (*it); | |
55 } | |
56 items->clear(); | |
57 } | |
58 | |
59 // Typically used to clean up values used in a hash_map that had Type* as | |
60 // values. | |
61 // | |
62 // WARNING: This function assumes that T::clear will not access the values | |
63 // (or the keys if they are the same as the values). This is true for | |
64 // hash_map. | |
65 template <class T> | |
66 void CleanupMap(T* items) { | |
67 // This is likely slower than a for loop, but we have to do it this way. In | |
68 // some of the maps we use, deleting it->second causes it->first to be | |
69 // deleted as well, and that seems to send the iterator in a tizzy. | |
70 typename T::iterator it = items->begin(); | |
71 while (it != items->end()) { | |
72 items->erase(it->first); | |
73 delete it->second; | |
74 it = items->begin(); | |
75 } | |
76 } | |
77 | |
78 // Get the value of an element in the map with the specified name. | |
79 template <class T> | |
80 void GetMapElement(const std::map<const std::string, const T>& m, | |
81 const char* name, | |
82 T* value) { | |
83 typename std::map<const std::string, const T>::const_iterator iter( | |
84 m.find(name)); | |
85 if (iter != m.end()) { | |
86 *value = iter->second; | |
87 } | |
88 } | |
89 | |
90 #endif // CHROME_BROWSER_SYNC_NOTIFIER_BASE_UTILS_H_ | |
OLD | NEW |