Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / public / browser / notification_source.h
blob954b03c078e56ff8a04cdc32f2ebab05443daa7d
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.
5 // This file defines the type used to provide sources for NotificationService
6 // notifications.
8 #ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_SOURCE_H_
9 #define CONTENT_PUBLIC_BROWSER_NOTIFICATION_SOURCE_H_
11 #include "base/basictypes.h"
12 #include "content/common/content_export.h"
14 namespace content {
16 // Do not declare a NotificationSource directly--use either
17 // "Source<sourceclassname>(sourceclasspointer)" or
18 // NotificationService::AllSources().
19 class CONTENT_EXPORT NotificationSource {
20 public:
21 NotificationSource(const NotificationSource& other) : ptr_(other.ptr_) {}
22 ~NotificationSource() {}
24 // NotificationSource can be used as the index for a map; this method
25 // returns the pointer to the current source as an identifier, for use as a
26 // map index.
27 uintptr_t map_key() const { return reinterpret_cast<uintptr_t>(ptr_); }
29 bool operator!=(const NotificationSource& other) const {
30 return ptr_ != other.ptr_;
32 bool operator==(const NotificationSource& other) const {
33 return ptr_ == other.ptr_;
36 protected:
37 explicit NotificationSource(const void* ptr) : ptr_(ptr) {}
39 // Declaring this const allows Source<T> to be used with both T = Foo and
40 // T = const Foo.
41 const void* ptr_;
44 template <class T>
45 class Source : public NotificationSource {
46 public:
47 // TODO(erg): Our code hard relies on implicit conversion
48 Source(const T* ptr) : NotificationSource(ptr) {} // NOLINT
49 Source(const NotificationSource& other) // NOLINT
50 : NotificationSource(other) {}
52 T* operator->() const { return ptr(); }
53 // The casts here allow this to compile with both T = Foo and T = const Foo.
54 T* ptr() const { return static_cast<T*>(const_cast<void*>(ptr_)); }
57 } // namespace content
59 #endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SOURCE_H_