• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 sources for NotificationService
6 // notifications.
7 
8 #ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_SOURCE_H_
9 #define CONTENT_PUBLIC_BROWSER_NOTIFICATION_SOURCE_H_
10 
11 #include "base/basictypes.h"
12 #include "content/common/content_export.h"
13 
14 namespace content {
15 
16 // Do not declare a NotificationSource directly--use either
17 // "Source<sourceclassname>(sourceclasspointer)" or
18 // NotificationService::AllSources().
19 class CONTENT_EXPORT NotificationSource {
20  public:
NotificationSource(const NotificationSource & other)21    NotificationSource(const NotificationSource& other) : ptr_(other.ptr_) {}
~NotificationSource()22   ~NotificationSource() {}
23 
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.
map_key()27   uintptr_t map_key() const { return reinterpret_cast<uintptr_t>(ptr_); }
28 
29   bool operator!=(const NotificationSource& other) const {
30     return ptr_ != other.ptr_;
31   }
32   bool operator==(const NotificationSource& other) const {
33     return ptr_ == other.ptr_;
34   }
35 
36  protected:
NotificationSource(const void * ptr)37   explicit NotificationSource(const void* ptr) : ptr_(ptr) {}
38 
39   // Declaring this const allows Source<T> to be used with both T = Foo and
40   // T = const Foo.
41   const void* ptr_;
42 };
43 
44 template <class T>
45 class Source : public NotificationSource {
46  public:
47   // TODO(erg): Our code hard relies on implicit conversion
Source(const T * ptr)48   Source(const T* ptr) : NotificationSource(ptr) {}  // NOLINT
Source(const NotificationSource & other)49   Source(const NotificationSource& other)      // NOLINT
50     : NotificationSource(other) {}
51 
52   T* operator->() const { return ptr(); }
53   // The casts here allow this to compile with both T = Foo and T = const Foo.
ptr()54   T* ptr() const { return static_cast<T*>(const_cast<void*>(ptr_)); }
55 };
56 
57 }  // namespace content
58 
59 #endif  // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SOURCE_H_
60