1 // Copyright (c) 2012 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 // This file defines the type used to provide details for NotificationService 6 // notifications. 7 8 #ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_DETAILS_H_ 9 #define CONTENT_PUBLIC_BROWSER_NOTIFICATION_DETAILS_H_ 10 11 #include "base/basictypes.h" 12 #include "content/common/content_export.h" 13 14 namespace content { 15 16 // Do not declare a NotificationDetails directly--use either 17 // "Details<detailsclassname>(detailsclasspointer)" or 18 // NotificationService::NoDetails(). 19 class CONTENT_EXPORT NotificationDetails { 20 public: 21 NotificationDetails() : ptr_(NULL) {} 22 NotificationDetails(const NotificationDetails& other) : ptr_(other.ptr_) {} 23 ~NotificationDetails() {} 24 25 // NotificationDetails can be used as the index for a map; this method 26 // returns the pointer to the current details as an identifier, for use as a 27 // map index. 28 uintptr_t map_key() const { return reinterpret_cast<uintptr_t>(ptr_); } 29 30 bool operator!=(const NotificationDetails& other) const { 31 return ptr_ != other.ptr_; 32 } 33 34 bool operator==(const NotificationDetails& other) const { 35 return ptr_ == other.ptr_; 36 } 37 38 protected: 39 explicit NotificationDetails(const void* ptr) : ptr_(ptr) {} 40 41 // Declaring this const allows Details<T> to be used with both T = Foo and 42 // T = const Foo. 43 const void* ptr_; 44 }; 45 46 template <class T> 47 class Details : public NotificationDetails { 48 public: 49 // TODO(erg): Our code hard relies on implicit conversion 50 Details(T* ptr) : NotificationDetails(ptr) {} // NOLINT 51 Details(const NotificationDetails& other) // NOLINT 52 : NotificationDetails(other) {} 53 54 T* operator->() const { return ptr(); } 55 // The casts here allow this to compile with both T = Foo and T = const Foo. 56 T* ptr() const { return static_cast<T*>(const_cast<void*>(ptr_)); } 57 }; 58 59 } // namespace content 60 61 #endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_DETAILS_H_ 62