| Index: device/hid/hid_platform_resource_bidi_map.h
|
| diff --git a/device/hid/hid_platform_resource_bidi_map.h b/device/hid/hid_platform_resource_bidi_map.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..05330a49b6cb5906ed9fb3cd61551baec79771bc
|
| --- /dev/null
|
| +++ b/device/hid/hid_platform_resource_bidi_map.h
|
| @@ -0,0 +1,107 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef DEVICE_HID_HID_DEVICE_RESOURCE_BIDI_MAP_H_
|
| +#define DEVICE_HID_HID_DEVICE_RESOURCE_BIDI_MAP_H_
|
| +
|
| +#include <map>
|
| +
|
| +#include "base/logging.h"
|
| +#include "base/stl_util.h"
|
| +#include "device/hid/hid_device_info.h"
|
| +
|
| +namespace device {
|
| +
|
| +// This is a utility class which platform implementations can use to manage
|
| +// a 1:1 mapping between a generic HidService-generated device ID and some
|
| +// platform-dependent resource identifier.
|
| +template <typename PlatformResourceType>
|
| +class HidPlatformResourceBidiMap {
|
| + public:
|
| + // Add a unique (ID, resource) entry to the map. Iff either the ID or the
|
| + // given resource is already stored in the map, the map will remain
|
| + // unchanged and this will return |false|.
|
| + bool Add(HidDeviceId device_id, PlatformResourceType resource) {
|
| + if (ContainsKey(device_resources_, device_id) ||
|
| + ContainsKey(device_ids_, resource)) {
|
| + return false;
|
| + }
|
| + device_resources_[device_id] = resource;
|
| + device_ids_[resource] = device_id;
|
| + return true;
|
| + }
|
| +
|
| + // Remove an entry by its resource value. Returns |false| if the resource was
|
| + // not present in the map.
|
| + bool RemoveByResource(PlatformResourceType resource) {
|
| + typename DeviceResourceToIdMap::iterator id_iter =
|
| + device_ids_.find(resource);
|
| + if (id_iter == device_ids_.end())
|
| + return false;
|
| +
|
| + HidDeviceId device_id = id_iter->second;
|
| + typename DeviceIdToResourceMap::iterator resource_iter =
|
| + device_resources_.find(device_id);
|
| +
|
| + // This should never fail if the corresponding resource was found.
|
| + DCHECK(resource_iter != device_resources_.end());
|
| +
|
| + device_ids_.erase(id_iter);
|
| + device_resources_.erase(resource_iter);
|
| + return true;
|
| + }
|
| +
|
| + // Remove an entry by its device ID. Returns |false| if the ID was not present
|
| + // in the map.
|
| + bool RemoveById(HidDeviceId device_id) {
|
| + typename DeviceIdToResourceMap::iterator resource_iter =
|
| + device_resources_.find(device_id);
|
| + if (resource_iter == device_resources_.end())
|
| + return false;
|
| +
|
| + PlatformResourceType resource = resource_iter->second;
|
| + typename DeviceResourceToIdMap::iterator id_iter =
|
| + device_ids_.find(resource);
|
| +
|
| + // This should never fail if the corresponding ID was found.
|
| + DCHECK(id_iter != device_ids_.end());
|
| +
|
| + device_ids_.erase(id_iter);
|
| + device_resources_.erase(resource_iter);
|
| + return true;
|
| + }
|
| +
|
| + // Get a device ID by its resource value. Returns |false| if the resource was
|
| + // not present in the map.
|
| + bool GetIdByResource(PlatformResourceType resource, HidDeviceId* device_id) {
|
| + typename DeviceResourceToIdMap::iterator id_iter =
|
| + device_ids_.find(resource);
|
| + if (id_iter == device_ids_.end())
|
| + return false;
|
| + *device_id = id_iter->second;
|
| + return true;
|
| + }
|
| +
|
| + // Get a resource value by its device ID. Returns |false| if the ID was not
|
| + // present in the map.
|
| + bool GetResourceById(HidDeviceId device_id, PlatformResourceType* resource) {
|
| + typename DeviceIdToResourceMap::iterator resource_iter =
|
| + device_resources_.find(device_id);
|
| + if (resource_iter == device_resources_.end())
|
| + return false;
|
| + *resource = resource_iter->second;
|
| + return true;
|
| + }
|
| +
|
| + private:
|
| + typedef std::map<HidDeviceId, PlatformResourceType> DeviceIdToResourceMap;
|
| + DeviceIdToResourceMap device_resources_;
|
| +
|
| + typedef std::map<PlatformResourceType, HidDeviceId> DeviceResourceToIdMap;
|
| + DeviceResourceToIdMap device_ids_;
|
| +};
|
| +
|
| +} // namespace device
|
| +
|
| +#endif // DEVICE_HID_HID_DEVICE_RESOURCE_BIDI_MAP_H_
|
|
|