sfizz/sources/CCMap.h

44 lines
1.1 KiB
C
Raw Normal View History

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-25 14:01:03 +02:00
if (it == end(container)) {
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-07-29 02:13:03 +02:00
bool contains(int index) const noexcept { return container.find(index) != end(container); }
2019-08-04 01:38:31 +02:00
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
};
}