Index: chrome/browser/common/web_contents_user_data.h |
diff --git a/chrome/browser/common/web_contents_user_data.h b/chrome/browser/common/web_contents_user_data.h |
index 2cffb588e864a5829bf989fe13b4586ca352d3a8..364997ebd93c428d62979db2e0ca0d1d3344c2e5 100644 |
--- a/chrome/browser/common/web_contents_user_data.h |
+++ b/chrome/browser/common/web_contents_user_data.h |
@@ -18,12 +18,11 @@ |
// // ... more public stuff here ... |
// private: |
// explicit FooTabHelper(content::WebContents* contents); |
-// static int kUserDataKey; |
// friend class WebContentsUserData<FooTabHelper>; |
// // ... more private stuff here ... |
// } |
// --- in foo_tab_helper.cc --- |
-// int FooTabHelper::kUserDataKey; |
+// DEFINE_WEB_CONTENTS_USER_DATA_KEY(FooTabHelper) |
// |
template <typename T> |
class WebContentsUserData : public base::SupportsUserData::Data { |
@@ -32,18 +31,33 @@ class WebContentsUserData : public base::SupportsUserData::Data { |
// If an instance is already attached, does nothing. |
static void CreateForWebContents(content::WebContents* contents) { |
if (!FromWebContents(contents)) |
- contents->SetUserData(&T::kUserDataKey, new T(contents)); |
+ contents->SetUserData(&kLocatorKey, new T(contents)); |
} |
// Retrieves the instance of type T that was attached to the specified |
// WebContents (via CreateForWebContents above) and returns it. If no instance |
// of the type was attached, returns NULL. |
static T* FromWebContents(content::WebContents* contents) { |
- return static_cast<T*>(contents->GetUserData(&T::kUserDataKey)); |
+ return static_cast<T*>(contents->GetUserData(&kLocatorKey)); |
} |
static const T* FromWebContents(const content::WebContents* contents) { |
- return static_cast<const T*>(contents->GetUserData(&T::kUserDataKey)); |
+ return static_cast<const T*>(contents->GetUserData(&kLocatorKey)); |
} |
+ |
awong
2012/10/10 17:03:46
Can this be private?
Avi (use Gerrit)
2012/10/10 18:18:54
I don't know. Let me try. I'd be happy if it were.
|
+ // The user data key. |
+ static int kLocatorKey; |
}; |
+// The macro to define the locator key. This key must be defined in the .cc file |
+// of the tab helper otherwise different instances for different template types |
+// will be collapsed by the Visual Studio linker. |
awong
2012/10/10 17:03:46
This comment still makes me a bit uneasy. Since w
Avi (use Gerrit)
2012/10/10 18:18:54
I wish I knew. I'm trying to remember who I had th
|
+// |
+// The "= 0" is surprising, but is required to effect a definition rather than |
+// a declaration. Without it, this would be merely a declaration of a template |
+// specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13) |
+// |
+#define DEFINE_WEB_CONTENTS_USER_DATA_KEY(TYPE) \ |
+template<> \ |
+int WebContentsUserData<TYPE>::kLocatorKey = 0; |
+ |
#endif // CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_ |