OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_API_WEBDATA_WEB_DATA_RESULTS_H_ | 5 #ifndef CHROME_BROWSER_API_WEBDATA_WEB_DATA_RESULTS_H_ |
6 #define CHROME_BROWSER_API_WEBDATA_WEB_DATA_RESULTS_H_ | 6 #define CHROME_BROWSER_API_WEBDATA_WEB_DATA_RESULTS_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 } WDResultType; | 33 } WDResultType; |
34 | 34 |
35 | 35 |
36 typedef base::Callback<void(const WDTypedResult*)> DestroyCallback; | 36 typedef base::Callback<void(const WDTypedResult*)> DestroyCallback; |
37 | 37 |
38 // | 38 // |
39 // The top level class for a result. | 39 // The top level class for a result. |
40 // | 40 // |
41 class WDTypedResult { | 41 class WDTypedResult { |
42 public: | 42 public: |
43 virtual ~WDTypedResult(); | 43 virtual ~WDTypedResult() { |
| 44 } |
44 | 45 |
45 // Return the result type. | 46 // Return the result type. |
46 WDResultType GetType() const { | 47 WDResultType GetType() const { |
47 return type_; | 48 return type_; |
48 } | 49 } |
49 | 50 |
50 void Destroy() const { | 51 virtual void Destroy() { |
51 if (!callback_.is_null()) { | |
52 callback_.Run(this); | |
53 } | |
54 } | 52 } |
55 | 53 |
56 protected: | 54 protected: |
57 explicit WDTypedResult(WDResultType type); | 55 explicit WDTypedResult(WDResultType type) |
58 | 56 : type_(type) { |
59 WDTypedResult(WDResultType type, const DestroyCallback& callback); | 57 } |
60 | 58 |
61 private: | 59 private: |
62 WDResultType type_; | 60 WDResultType type_; |
63 DestroyCallback callback_; | |
64 DISALLOW_COPY_AND_ASSIGN(WDTypedResult); | 61 DISALLOW_COPY_AND_ASSIGN(WDTypedResult); |
65 }; | 62 }; |
66 | 63 |
67 // A result containing one specific pointer or literal value. | 64 // A result containing one specific pointer or literal value. |
68 template <class T> class WDResult : public WDTypedResult { | 65 template <class T> class WDResult : public WDTypedResult { |
69 public: | 66 public: |
70 WDResult(WDResultType type, const T& v) | 67 WDResult(WDResultType type, const T& v) |
71 : WDTypedResult(type), value_(v) { | 68 : WDTypedResult(type), value_(v) { |
72 } | 69 } |
73 | 70 |
74 WDResult(WDResultType type, const DestroyCallback& callback, const T& v) | |
75 : WDTypedResult(type, callback), value_(v) { | |
76 } | |
77 | |
78 virtual ~WDResult() { | 71 virtual ~WDResult() { |
79 } | 72 } |
80 | 73 |
81 // Return a single value result. | 74 // Return a single value result. |
82 T GetValue() const { | 75 T GetValue() const { |
83 return value_; | 76 return value_; |
84 } | 77 } |
85 | 78 |
86 private: | 79 private: |
87 T value_; | 80 T value_; |
88 | 81 |
89 DISALLOW_COPY_AND_ASSIGN(WDResult); | 82 DISALLOW_COPY_AND_ASSIGN(WDResult); |
90 }; | 83 }; |
91 | 84 |
| 85 template <class T> class WDDestroyableResult : public WDTypedResult { |
| 86 public: |
| 87 WDDestroyableResult( |
| 88 WDResultType type, |
| 89 const T& v, |
| 90 const DestroyCallback& callback) |
| 91 : WDTypedResult(type), |
| 92 value_(v), |
| 93 callback_(callback) { |
| 94 } |
| 95 |
| 96 virtual ~WDDestroyableResult() { |
| 97 } |
| 98 |
| 99 |
| 100 virtual void Destroy() OVERRIDE { |
| 101 if (!callback_.is_null()) { |
| 102 callback_.Run(this); |
| 103 } |
| 104 } |
| 105 |
| 106 // Return a single value result. |
| 107 T GetValue() const { |
| 108 return value_; |
| 109 } |
| 110 |
| 111 private: |
| 112 T value_; |
| 113 DestroyCallback callback_; |
| 114 |
| 115 DISALLOW_COPY_AND_ASSIGN(WDDestroyableResult); |
| 116 }; |
| 117 |
92 template <class T> class WDObjectResult : public WDTypedResult { | 118 template <class T> class WDObjectResult : public WDTypedResult { |
93 public: | 119 public: |
94 explicit WDObjectResult(WDResultType type) | 120 explicit WDObjectResult(WDResultType type) |
95 : WDTypedResult(type) { | 121 : WDTypedResult(type) { |
96 } | 122 } |
97 | 123 |
98 WDObjectResult(WDResultType type, const DestroyCallback& callback) | |
99 : WDTypedResult(type, callback) { | |
100 } | |
101 | |
102 T* GetValue() const { | 124 T* GetValue() const { |
103 return &value_; | 125 return &value_; |
104 } | 126 } |
105 | 127 |
106 private: | 128 private: |
107 // mutable to keep GetValue() const. | 129 // mutable to keep GetValue() const. |
108 mutable T value_; | 130 mutable T value_; |
109 DISALLOW_COPY_AND_ASSIGN(WDObjectResult); | 131 DISALLOW_COPY_AND_ASSIGN(WDObjectResult); |
110 }; | 132 }; |
111 | 133 |
112 #endif // CHROME_BROWSER_API_WEBDATA_WEB_DATA_RESULTS_H_ | 134 #endif // CHROME_BROWSER_API_WEBDATA_WEB_DATA_RESULTS_H_ |
OLD | NEW |