dawn-cmake/src/common/SerialMap.h
Austin Eng e06d57d338 Factor SerialQueue into SerialQueue and SerialMap
This change moves most of SerialQueue into SerialStorage
and factors it to make SerialMap and SerialQueue. SerialMap
does not enforce that items are Enqueue'd in monotonically
increasing order. This is useful for implement timeline fences
because OnCompletion callbacks may be added in an arbitrary order.

Bug: dawn:26
Change-Id: I03376117311112b0b94ed887a31974f36c4a5464
Reviewed-on: https://dawn-review.googlesource.com/c/2720
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
2018-11-28 17:30:12 +00:00

76 lines
2.2 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 T>
class SerialMap;
template <typename T>
struct SerialStorageTraits<SerialMap<T>> {
using Value = T;
using Storage = std::map<Serial, std::vector<T>>;
using StorageIterator = typename Storage::iterator;
using ConstStorageIterator = typename Storage::const_iterator;
};
// SerialMap stores a map from Serial to T.
// Unlike SerialQueue, items may be enqueued with Serials in any
// arbitrary order. SerialMap provides useful iterators for iterating
// through T items in order of increasing Serial.
template <typename T>
class SerialMap : public SerialStorage<SerialMap<T>> {
public:
void Enqueue(const T& value, Serial serial);
void Enqueue(T&& value, Serial serial);
void Enqueue(const std::vector<T>& values, Serial serial);
void Enqueue(std::vector<T>&& values, Serial serial);
};
// SerialMap
template <typename T>
void SerialMap<T>::Enqueue(const T& value, Serial serial) {
this->mStorage[serial].emplace_back(value);
}
template <typename T>
void SerialMap<T>::Enqueue(T&& value, Serial serial) {
this->mStorage[serial].emplace_back(value);
}
template <typename T>
void SerialMap<T>::Enqueue(const std::vector<T>& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
for (const T& value : values) {
Enqueue(value, serial);
}
}
template <typename T>
void SerialMap<T>::Enqueue(std::vector<T>&& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
for (const T& value : values) {
Enqueue(value, serial);
}
}
#endif // COMMON_SERIALMAP_H_