|
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 |
|
3 // found in the LICENSE file. |
|
4 |
|
5 // This file defines the type used to provide sources for NotificationService |
|
6 // notifications. |
|
7 |
|
8 #ifndef CHROME_COMMON_NOTIFICATION_SOURCE_H__ |
|
9 #define CHROME_COMMON_NOTIFICATION_SOURCE_H__ |
|
10 |
|
11 #include "base/basictypes.h" |
|
12 |
|
13 // Do not declare a NotificationSource directly--use either |
|
14 // "Source<sourceclassname>(sourceclasspointer)" or |
|
15 // NotificationService::AllSources(). |
|
16 class NotificationSource { |
|
17 public: |
|
18 NotificationSource(const NotificationSource& other) : ptr_(other.ptr_) { } |
|
19 ~NotificationSource() {} |
|
20 |
|
21 // NotificationSource can be used as the index for a map; this method |
|
22 // returns the pointer to the current source as an identifier, for use as a |
|
23 // map index. |
|
24 uintptr_t map_key() const { return reinterpret_cast<uintptr_t>(ptr_); } |
|
25 |
|
26 bool operator!=(const NotificationSource& other) const { |
|
27 return ptr_ != other.ptr_; |
|
28 } |
|
29 bool operator==(const NotificationSource& other) const { |
|
30 return ptr_ == other.ptr_; |
|
31 } |
|
32 |
|
33 protected: |
|
34 NotificationSource(void* ptr) : ptr_(ptr) {} |
|
35 |
|
36 void* ptr_; |
|
37 }; |
|
38 |
|
39 template <class T> |
|
40 class Source : public NotificationSource { |
|
41 public: |
|
42 Source(T* ptr) : NotificationSource(ptr) {} |
|
43 |
|
44 Source(const NotificationSource& other) |
|
45 : NotificationSource(other) {} |
|
46 |
|
47 T* operator->() const { return ptr(); } |
|
48 T* ptr() const { return static_cast<T*>(ptr_); } |
|
49 }; |
|
50 |
|
51 #endif // CHROME_COMMON_NOTIFICATION_SOURCE_H__ |