OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ | |
6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ | |
7 #pragma once | |
8 | |
9 #include <iostream> | |
10 | |
11 #include "base/hash_tables.h" | |
12 | |
13 class DownloadManager; | |
14 | |
15 // DownloadId combines per-profile Download ids with an indication of which | |
16 // profile in order to be globally unique. DownloadIds are not persistent across | |
17 // sessions, but their local() field is. | |
18 class DownloadId { | |
19 public: | |
20 explicit DownloadId(const DownloadManager* manager = NULL, | |
willchan no longer on Chromium
2011/08/06 14:08:36
http://google-styleguide.googlecode.com/svn/trunk/
benjhayden
2011/08/08 16:50:19
Done.
| |
21 int32 local_id = -1) | |
22 : manager_(manager), | |
23 local_id_(local_id) { | |
24 } | |
25 | |
26 // Return the per-profile and persistent part of this DownloadId. | |
27 int32 local() const { return local_id_; } | |
28 | |
29 // Returns true if this DownloadId has been allocated and could possibly refer | |
30 // to a DownloadItem that exists. | |
31 bool IsValid() const { return ((manager_ != NULL) && (local_id_ >= 0)); } | |
32 | |
33 // Returns true if this DownloadId was allocated by |manager|. | |
34 bool BelongsTo(const DownloadManager* manager) const { | |
35 return manager == manager_; | |
36 } | |
37 | |
38 // The following methods (operator==, operator<, hash(), copy, and assign) | |
39 // provide support for STL containers such as hash_map and set. | |
40 | |
41 // Returns true if this refers to the same DownloadItem as that. | |
willchan no longer on Chromium
2011/08/06 14:08:36
Where do you need this operator==?
benjhayden
2011/08/08 16:50:19
It looks like hash_map uses operator== on its keys
| |
42 bool operator==(const DownloadId& that) const { | |
43 return ((that.local_id_ == local_id_) && | |
44 (that.manager_ == manager_)); | |
45 } | |
46 bool operator<(const DownloadId& that) const { | |
willchan no longer on Chromium
2011/08/06 14:08:36
operator< does not make sense and I'm not sure you
benjhayden
2011/08/08 16:50:19
Done.
| |
47 return ((manager_ < that.manager_) || | |
48 (local_id_ < that.local_id_)); | |
49 } | |
50 | |
51 size_t hash() const { | |
52 // The top half of manager is unlikely to be distinct, and the user is | |
53 // unlikely to have >64K downloads. If these assumptions are incorrect, then | |
54 // DownloadFileManager's hash_map might have a few collisions, but it will | |
55 // use operator== to safely disambiguate. | |
56 return reinterpret_cast<size_t>(manager_) + | |
57 (static_cast<size_t>(local_id_) << (4 * sizeof(size_t))); | |
58 } | |
59 | |
60 private: | |
61 // DownloadId is used mostly off the UI thread, so manager's methods can't be | |
62 // called, but the pointer can be compared. | |
63 const DownloadManager* manager_; | |
64 | |
65 int32 local_id_; | |
66 | |
67 friend std::ostream& operator<<(std::ostream& out, | |
68 const DownloadId& global_id); | |
69 | |
70 // Allow copy and assign. | |
71 }; | |
72 | |
73 // Allow logging DownloadIds. Looks like "0x01234567:42". | |
74 std::ostream& operator<<(std::ostream& out, const DownloadId& global_id); | |
75 | |
76 // Allow using DownloadIds as keys in hash_maps. | |
77 namespace BASE_HASH_NAMESPACE { | |
78 #if defined(COMPILER_GCC) | |
79 template<> struct hash<DownloadId> { | |
80 std::size_t operator()(const DownloadId& id) const { | |
81 return id.hash(); | |
82 } | |
83 }; | |
84 #elif defined(COMPILER_MSVC) | |
85 inline size_t hash_value(const DownloadId& id) { | |
86 return id.hash(); | |
87 } | |
88 #endif // COMPILER | |
89 } | |
90 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ | |
OLD | NEW |