Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: device/hid/hid_platform_resource_bidi_map.h

Issue 161823002: Clean up HID backend and API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Many cleanup, such device ID, woww. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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 DEVICE_HID_HID_DEVICE_RESOURCE_BIDI_MAP_H_
6 #define DEVICE_HID_HID_DEVICE_RESOURCE_BIDI_MAP_H_
7
8 #include <map>
9
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "device/hid/hid_device_info.h"
13
14 namespace device {
15
16 // This is a utility class which platform implementations can use to manage
17 // a 1:1 mapping between a generic HidService-generated device ID and some
18 // platform-dependent resource identifier.
19 template <typename PlatformResourceType>
20 class HidPlatformResourceBidiMap {
21 public:
22 // Add a unique (ID, resource) entry to the map. Iff either the ID or the
23 // given resource is already stored in the map, the map will remain
24 // unchanged and this will return |false|.
25 bool Add(HidDeviceId device_id, PlatformResourceType resource) {
26 if (ContainsKey(device_resources_, device_id) ||
27 ContainsKey(device_ids_, resource)) {
28 return false;
29 }
30 device_resources_[device_id] = resource;
31 device_ids_[resource] = device_id;
32 return true;
33 }
34
35 // Remove an entry by its resource value. Returns |false| if the resource was
36 // not present in the map.
37 bool RemoveByResource(PlatformResourceType resource) {
38 typename DeviceResourceToIdMap::iterator id_iter =
39 device_ids_.find(resource);
40 if (id_iter == device_ids_.end())
41 return false;
42
43 HidDeviceId device_id = id_iter->second;
44 typename DeviceIdToResourceMap::iterator resource_iter =
45 device_resources_.find(device_id);
46
47 // This should never fail if the corresponding resource was found.
48 DCHECK(resource_iter != device_resources_.end());
49
50 device_ids_.erase(id_iter);
51 device_resources_.erase(resource_iter);
52 return true;
53 }
54
55 // Remove an entry by its device ID. Returns |false| if the ID was not present
56 // in the map.
57 bool RemoveById(HidDeviceId device_id) {
58 typename DeviceIdToResourceMap::iterator resource_iter =
59 device_resources_.find(device_id);
60 if (resource_iter == device_resources_.end())
61 return false;
62
63 PlatformResourceType resource = resource_iter->second;
64 typename DeviceResourceToIdMap::iterator id_iter =
65 device_ids_.find(resource);
66
67 // This should never fail if the corresponding ID was found.
68 DCHECK(id_iter != device_ids_.end());
69
70 device_ids_.erase(id_iter);
71 device_resources_.erase(resource_iter);
72 return true;
73 }
74
75 // Get a device ID by its resource value. Returns |false| if the resource was
76 // not present in the map.
77 bool GetIdByResource(PlatformResourceType resource, HidDeviceId* device_id) {
78 typename DeviceResourceToIdMap::iterator id_iter =
79 device_ids_.find(resource);
80 if (id_iter == device_ids_.end())
81 return false;
82 *device_id = id_iter->second;
83 return true;
84 }
85
86 // Get a resource value by its device ID. Returns |false| if the ID was not
87 // present in the map.
88 bool GetResourceById(HidDeviceId device_id, PlatformResourceType* resource) {
89 typename DeviceIdToResourceMap::iterator resource_iter =
90 device_resources_.find(device_id);
91 if (resource_iter == device_resources_.end())
92 return false;
93 *resource = resource_iter->second;
94 return true;
95 }
96
97 private:
98 typedef std::map<HidDeviceId, PlatformResourceType> DeviceIdToResourceMap;
99 DeviceIdToResourceMap device_resources_;
100
101 typedef std::map<PlatformResourceType, HidDeviceId> DeviceResourceToIdMap;
102 DeviceResourceToIdMap device_ids_;
103 };
104
105 } // namespace device
106
107 #endif // DEVICE_HID_HID_DEVICE_RESOURCE_BIDI_MAP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698