blob: 904a0951716e8ccd0bf5c3c96f1864b6a10abfd5 [file] [log] [blame]
// Copyright (c) 2016 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 GPU_COMMAND_BUFFER_SERVICE_CLIENT_SERVICE_MAP_H_
#define GPU_COMMAND_BUFFER_SERVICE_CLIENT_SERVICE_MAP_H_
#include <limits>
#include <unordered_map>
namespace gpu {
namespace gles2 {
template <typename ClientType, typename ServiceType>
class ClientServiceMap {
public:
ClientServiceMap() : client_to_service_() {}
void SetIDMapping(ClientType client_id, ServiceType service_id) {
DCHECK(client_to_service_.find(client_id) == client_to_service_.end());
DCHECK(service_id != invalid_service_id());
client_to_service_[client_id] = service_id;
}
void RemoveClientID(ClientType client_id) {
client_to_service_.erase(client_id);
}
void Clear() { client_to_service_.clear(); }
bool GetServiceID(ClientType client_id, ServiceType* service_id) const {
if (client_id == 0) {
if (service_id) {
*service_id = 0;
}
return true;
}
auto iter = client_to_service_.find(client_id);
if (iter != client_to_service_.end()) {
if (service_id) {
*service_id = iter->second;
}
return true;
}
return false;
}
ServiceType GetServiceIDOrInvalid(ClientType client_id) {
ServiceType service_id;
if (GetServiceID(client_id, &service_id)) {
return service_id;
}
return invalid_service_id();
}
bool GetClientID(ServiceType service_id, ClientType* client_id) const {
if (service_id == 0) {
if (client_id) {
*client_id = 0;
}
return true;
}
for (auto mapping : client_to_service_) {
if (mapping.second == service_id) {
if (client_id) {
*client_id = mapping.first;
}
return true;
}
}
return false;
}
ServiceType invalid_service_id() const {
return std::numeric_limits<ServiceType>::max();
}
typedef typename std::unordered_map<ClientType, ServiceType>::const_iterator
const_iterator;
const_iterator begin() const { return client_to_service_.begin(); }
const_iterator end() const { return client_to_service_.end(); }
private:
std::unordered_map<ClientType, ServiceType> client_to_service_;
};
} // namespace gles2
} // namespace gpu
#endif // GPU_COMMAND_BUFFER_SERVICE_CLIENT_SERVICE_MAP_H_