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. |
| 23 // Using this function requires discipline. Don't use for types that have a |
| 24 // v-table or virtual bases. |
| 25 template <typename T> |
| 26 inline void SetZero(T& p) { |
| 27 // Guard against the easy mistake of |
| 28 // foo(int *p) { SetZero(p); } instead of |
| 29 // SetZero(*p); |
| 30 // which it should be. |
| 31 STATIC_ASSERT(sizeof(T) != sizeof(void*)); |
| 32 |
| 33 // A POD (plain old data) object has one of these data types: |
| 34 // a fundamental type, union, struct, array, |
| 35 // or class--with no constructor. PODs don't have virtual functions or |
| 36 // virtual bases. |
| 37 |
| 38 // Test to see if the type has constructors. |
| 39 union CtorTest { |
| 40 T t; |
| 41 int i; |
| 42 }; |
| 43 |
| 44 // TODO(sync) There might be a way to test if the type has virtuals |
| 45 // For now, if we zero a type with virtuals by mistake, it is going to crash |
| 46 // predictable at run-time when the virtuals are called. |
| 47 memset(&p, 0, sizeof(T)); |
| 48 } |
| 49 |
| 50 // Used to delete each element in a vector<T*>/deque<T*> |
| 51 // (and then empty the sequence). |
| 52 template <class T> |
| 53 void CleanupSequence(T* items) { |
| 54 for (typename T::iterator it(items->begin()); it != items->end(); ++it) { |
| 55 delete (*it); |
| 56 } |
| 57 items->clear(); |
| 58 } |
| 59 |
| 60 // Typically used to clean up values used in a hash_map |
| 61 // that had Type* as values. |
| 62 // |
| 63 // WARNING: This function assumes that T::clear will not access the values |
| 64 // (or the keys if they are the same as the values). This is true |
| 65 // for hash_map. |
| 66 template <class T> |
| 67 void CleanupMap(T* items) { |
| 68 // This is likely slower than a for loop, but we have to do it this way. In |
| 69 // some of the maps we use, deleting it->second causes it->first to be deleted |
| 70 // as well, and that seems to send the iterator in a tizzy. |
| 71 typename T::iterator it = items->begin(); |
| 72 while (it != items->end()) { |
| 73 items->erase(it->first); |
| 74 delete it->second; |
| 75 it = items->begin(); |
| 76 } |
| 77 } |
| 78 |
| 79 // Get the value of an element in the map with the specified name |
| 80 template <class T> |
| 81 void GetMapElement(const std::map<const std::string, const T>& m, |
| 82 const char* name, |
| 83 T* value) { |
| 84 typename std::map<const std::string, const T>::const_iterator iter( |
| 85 m.find(name)); |
| 86 if (iter != m.end()) { |
| 87 *value = iter->second; |
| 88 } |
| 89 } |
| 90 |
| 91 #endif // CHROME_BROWSER_SYNC_NOTIFIER_BASE_UTILS_H_ |
OLD | NEW |