dawn-cmake/src/common/SerialMap.h
Corentin Wallez 145f115c54 SerialQueue/Map: Take the serial type as type paramater.
This is in preparation for follow-up CLs that will use typed integers
for the various serial types.

Bug: dawn:442

Change-Id: I5296546e96acd6ac9f7a0bfc46dc7eba40cb3cf5
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/28921
Reviewed-by: Jiawei Shao <jiawei.shao@intel.com>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
2020-09-28 12:28:13 +00:00

77 lines
2.5 KiB
C++

// Copyright 2018 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef COMMON_SERIALMAP_H_
#define COMMON_SERIALMAP_H_
#include "common/SerialStorage.h"
#include <map>
#include <vector>
template <typename Serial, typename Value>
class SerialMap;
template <typename SerialT, typename ValueT>
struct SerialStorageTraits<SerialMap<SerialT, ValueT>> {
using Serial = SerialT;
using Value = ValueT;
using Storage = std::map<Serial, std::vector<Value>>;
using StorageIterator = typename Storage::iterator;
using ConstStorageIterator = typename Storage::const_iterator;
};
// SerialMap stores a map from Serial to Value.
// Unlike SerialQueue, items may be enqueued with Serials in any
// arbitrary order. SerialMap provides useful iterators for iterating
// through Value items in order of increasing Serial.
template <typename Serial, typename Value>
class SerialMap : public SerialStorage<SerialMap<Serial, Value>> {
public:
void Enqueue(const Value& value, Serial serial);
void Enqueue(Value&& value, Serial serial);
void Enqueue(const std::vector<Value>& values, Serial serial);
void Enqueue(std::vector<Value>&& values, Serial serial);
};
// SerialMap
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(const Value& value, Serial serial) {
this->mStorage[serial].emplace_back(value);
}
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(Value&& value, Serial serial) {
this->mStorage[serial].emplace_back(value);
}
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(const std::vector<Value>& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
for (const Value& value : values) {
Enqueue(value, serial);
}
}
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(std::vector<Value>&& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
for (const Value& value : values) {
Enqueue(value, serial);
}
}
#endif // COMMON_SERIALMAP_H_