sfizz/sources/CCMap.h

68 lines
2.6 KiB
C
Raw Normal View History

2019-08-30 00:49:58 +02:00
// Copyright (c) 2019, Paul Ferrand
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2019-08-22 23:39:26 +02:00
#pragma once
#include "Helpers.h"
2019-08-25 14:01:03 +02:00
#include <map>
2019-07-29 02:13:03 +02:00
2019-08-25 14:01:03 +02:00
namespace sfz {
template <class ValueType>
class CCMap {
2019-07-29 02:13:03 +02:00
public:
2019-08-04 01:38:31 +02:00
CCMap() = delete;
2019-08-25 14:01:03 +02:00
CCMap(const ValueType& defaultValue)
: defaultValue(defaultValue)
{
}
2019-08-04 01:38:31 +02:00
CCMap(CCMap&&) = default;
CCMap(const CCMap&) = default;
~CCMap() = default;
2019-07-29 02:13:03 +02:00
2019-08-25 14:01:03 +02:00
const ValueType& getWithDefault(int index) const noexcept
2019-07-29 02:13:03 +02:00
{
auto it = container.find(index);
2019-08-29 19:15:06 +02:00
if (it == container.end()) {
2019-07-29 02:13:03 +02:00
return defaultValue;
2019-08-25 14:01:03 +02:00
} else {
2019-07-29 02:13:03 +02:00
return it->second;
}
}
2019-08-25 14:01:03 +02:00
ValueType& operator[](const int& key) noexcept
2019-07-29 02:13:03 +02:00
{
if (!contains(key))
container.emplace(key, defaultValue);
return container.operator[](key);
}
inline bool empty() const { return container.empty(); }
2019-08-25 14:01:03 +02:00
const ValueType& at(int index) const { return container.at(index); }
2019-08-29 19:15:06 +02:00
bool contains(int index) const noexcept { return container.find(index) != container.end(); }
typename std::map<int, ValueType>::iterator begin() { return container.begin(); }
typename std::map<int, ValueType>::iterator end() { return container.end(); }
2019-07-29 02:13:03 +02:00
private:
const ValueType defaultValue;
std::map<int, ValueType> container;
LEAK_DETECTOR(CCMap);
2019-07-29 02:13:03 +02:00
};
}