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 DownloadId(const DownloadManager* manager, int32 local_id) | |
21 : manager_(manager), | |
22 local_id_(local_id) { | |
23 } | |
24 | |
25 // Return the per-profile and persistent part of this DownloadId. | |
26 int32 local() const { return local_id_; } | |
27 | |
28 // Returns true if this DownloadId has been allocated and could possibly refer | |
29 // to a DownloadItem that exists. | |
30 bool IsValid() const { return ((manager_ != NULL) && (local_id_ >= 0)); } | |
31 | |
32 // The following methods (operator==, hash(), copy, and assign) provide | |
33 // support for STL containers such as hash_map. | |
34 | |
35 bool operator==(const DownloadId& that) const { | |
36 return ((that.local_id_ == local_id_) && | |
37 (that.manager_ == manager_)); | |
38 } | |
39 bool operator<(const DownloadId& that) const { | |
40 // Even though DownloadManager* < DownloadManager* is not well defined and | |
41 // GCC does not require it for hash_map, MSVC requires operator< for | |
42 // hash_map. We don't ifdef it out here because we will probably make a | |
43 // set<DownloadId> at some point, when GCC will require it. | |
44 return ((that.local_id_ < local_id_) && | |
45 (that.manager_ < manager_)); | |
46 } | |
47 | |
48 size_t hash() const { | |
49 // The top half of manager is unlikely to be distinct, and the user is | |
50 // unlikely to have >64K downloads. If these assumptions are incorrect, then | |
51 // DownloadFileManager's hash_map might have a few collisions, but it will | |
52 // use operator== to safely disambiguate. | |
53 return reinterpret_cast<size_t>(manager_) + | |
54 (static_cast<size_t>(local_id_) << (4 * sizeof(size_t))); | |
55 } | |
56 | |
57 private: | |
58 // DownloadId is used mostly off the UI thread, so manager's methods can't be | |
59 // called, but the pointer can be compared. | |
60 const DownloadManager* manager_; | |
61 | |
62 int32 local_id_; | |
63 | |
64 friend std::ostream& operator<<(std::ostream& out, | |
65 const DownloadId& global_id); | |
66 | |
67 // Allow copy and assign. | |
68 }; | |
69 | |
70 // Allow logging DownloadIds. Looks like "0x01234567:42". | |
71 std::ostream& operator<<(std::ostream& out, const DownloadId& global_id); | |
72 | |
73 // Allow using DownloadIds as keys in hash_maps. | |
74 namespace BASE_HASH_NAMESPACE { | |
75 #if defined(COMPILER_GCC) | |
76 template<> struct hash<DownloadId> { | |
77 std::size_t operator()(const DownloadId& id) const { | |
78 return id.hash(); | |
79 } | |
80 }; | |
81 #elif defined(COMPILER_MSVC) | |
82 inline size_t hash_value(const DownloadId& id) { | |
83 return id.hash(); | |
84 } | |
85 #endif // COMPILER | |
86 } | |
87 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ | |
OLD | NEW |