| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_COMMON_PROPERTY_BAG_H_ | 5 #ifndef CHROME_COMMON_PROPERTY_BAG_H_ |
| 6 #define CHROME_COMMON_PROPERTY_BAG_H_ | 6 #define CHROME_COMMON_PROPERTY_BAG_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/linked_ptr.h" | 11 #include "base/linked_ptr.h" |
| 12 | 12 |
| 13 class PropertyAccessorBase; | 13 class PropertyAccessorBase; |
| 14 | 14 |
| 15 // A property bag holds a generalized list of arbitrary metadata called | 15 // A property bag holds a generalized list of arbitrary metadata called |
| 16 // properties. Each property is a class type derived from PropertyBag::Prop | 16 // properties. Each property is a class type derived from PropertyBag::Prop |
| 17 // that can bet set and retrieved. | 17 // that can bet set and retrieved. |
| 18 // | 18 // |
| 19 // The property bag is not read or written directly. Instead, callers go | 19 // The property bag is not read or written directly. Instead, callers go |
| 20 // through a PropertyAccessor. The Accessor generates the unique IDs that | 20 // through a PropertyAccessor. The Accessor generates the unique IDs that |
| 21 // identify different properties, so uniquely identify a property. The Accessor | 21 // identify different properties. The Accessor is templatized to also provide |
| 22 // is templatized to also provide typesafety to the callers. | 22 // typesafety to the callers. |
| 23 // | 23 // |
| 24 // Example: | 24 // Example: |
| 25 // // Note: you don't want to use Singleton for your Accessor if you're using | 25 // // Note: you don't want to use Singleton for your Accessor if you're using |
| 26 // // a simple type like int or string as the data, since it will enforce that | 26 // // a simple type like int or string as the data, since it will enforce that |
| 27 // // there is only one singleton for that type, which will conflict. If | 27 // // there is only one singleton for that type, which will conflict. If |
| 28 // // you're putting in some struct that's unique to you, go ahead. | 28 // // you're putting in some struct that's unique to you, go ahead. |
| 29 // PropertyAccessor<int>* my_accessor() const { | 29 // PropertyAccessor<int>* my_accessor() const { |
| 30 // static PropertyAccessor<int>* accessor = NULL; | 30 // static PropertyAccessor<int>* accessor = NULL; |
| 31 // if (!accessor) accessor = new PropertyAccessor<int>; | 31 // if (!accessor) accessor = new PropertyAccessor<int>; |
| 32 // return accessor; | 32 // return accessor; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 // Identifier for this property. | 116 // Identifier for this property. |
| 117 PropertyBag::PropID prop_id_; | 117 PropertyBag::PropID prop_id_; |
| 118 | 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(PropertyAccessorBase); | 119 DISALLOW_COPY_AND_ASSIGN(PropertyAccessorBase); |
| 120 }; | 120 }; |
| 121 | 121 |
| 122 // PropertyAccessor ----------------------------------------------------------- | 122 // PropertyAccessor ----------------------------------------------------------- |
| 123 | 123 |
| 124 // Provides typesafe accessor functions for a property bag, and manages the | 124 // Provides typesafe accessor functions for a property bag, and manages the |
| 125 // unique identifiers for properties via the PropertyAccessorBase. | 125 // unique identifiers for properties via the PropertyAccessorBase. |
| 126 // | |
| 127 // Note that class T must be derived from PropertyBag::Prop. | |
| 128 template<class T> | 126 template<class T> |
| 129 class PropertyAccessor : public PropertyAccessorBase { | 127 class PropertyAccessor : public PropertyAccessorBase { |
| 130 public: | 128 public: |
| 131 PropertyAccessor() : PropertyAccessorBase() {} | 129 PropertyAccessor() : PropertyAccessorBase() {} |
| 132 virtual ~PropertyAccessor() {} | 130 virtual ~PropertyAccessor() {} |
| 133 | 131 |
| 134 // Makes a copy of the |prop| object for storage. | 132 // Makes a copy of the |prop| object for storage. |
| 135 void SetProperty(PropertyBag* bag, const T& prop) { | 133 void SetProperty(PropertyBag* bag, const T& prop) { |
| 136 SetPropertyInternal(bag, new Container(prop)); | 134 SetPropertyInternal(bag, new Container(prop)); |
| 137 } | 135 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 166 return new Container(data_); | 164 return new Container(data_); |
| 167 } | 165 } |
| 168 | 166 |
| 169 T data_; | 167 T data_; |
| 170 }; | 168 }; |
| 171 | 169 |
| 172 DISALLOW_COPY_AND_ASSIGN(PropertyAccessor); | 170 DISALLOW_COPY_AND_ASSIGN(PropertyAccessor); |
| 173 }; | 171 }; |
| 174 | 172 |
| 175 #endif // CHROME_COMMON_PROPERTY_BAG_H_ | 173 #endif // CHROME_COMMON_PROPERTY_BAG_H_ |
| OLD | NEW |