The open source repository for the action RPG game in development by Sig Productions titled 'Adventures in Lestoria'!
https://forums.lestoria.net
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
4.6 KiB
154 lines
4.6 KiB
1 year ago
|
#pragma region License
|
||
1 year ago
|
/*
|
||
|
License (OLC-3)
|
||
|
~~~~~~~~~~~~~~~
|
||
|
|
||
11 months ago
|
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
|
||
1 year ago
|
|
||
|
Redistribution and use in source and binary forms, with or without modification,
|
||
|
are permitted provided that the following conditions are met:
|
||
|
|
||
|
1. Redistributions or derivations of source code must retain the above copyright
|
||
|
notice, this list of conditions and the following disclaimer.
|
||
|
|
||
|
2. Redistributions or derivative works in binary form must reproduce the above
|
||
|
copyright notice. This list of conditions and the following disclaimer must be
|
||
|
reproduced in the documentation and/or other materials provided with the distribution.
|
||
|
|
||
|
3. Neither the name of the copyright holder nor the names of its contributors may
|
||
|
be used to endorse or promote products derived from this software without specific
|
||
|
prior written permission.
|
||
|
|
||
|
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 HOLDER 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.
|
||
1 year ago
|
|
||
|
Portions of this software are copyright © 2023 The FreeType
|
||
|
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
|
||
|
All rights reserved.
|
||
1 year ago
|
*/
|
||
1 year ago
|
#pragma endregion
|
||
1 year ago
|
#pragma once
|
||
1 year ago
|
#include "Error.h"
|
||
1 year ago
|
#include <functional>
|
||
|
#include <map>
|
||
|
#include <unordered_map>
|
||
1 year ago
|
|
||
|
//A class that has an initialization lock so that when the lock is activated, any further gets that are missing items in it will report themselves for easier debugging detection.
|
||
|
template<typename T,typename O>
|
||
|
class safemap{
|
||
|
std::map<T,O>map;
|
||
|
bool initialized=false;
|
||
|
public:
|
||
1 year ago
|
O&operator[](T key){
|
||
1 year ago
|
if(initialized&&map.count(key)==0){
|
||
1 year ago
|
ERR("WARNING! Trying to get non-existent key "<<key<<"!")
|
||
1 year ago
|
}
|
||
1 year ago
|
if(!initialized){
|
||
|
size_t originalSize=map.size();
|
||
|
O&val=map[key];
|
||
|
if(originalSize==map.size()){
|
||
1 year ago
|
ERR("WARNING! A previously set value has been overwritten! Key: "<<key)
|
||
1 year ago
|
}
|
||
|
return val;
|
||
|
}else{
|
||
|
return map[key];
|
||
|
}
|
||
|
}
|
||
|
O&at(T key){
|
||
|
return map.at(key);
|
||
|
}
|
||
12 months ago
|
auto insert(T key,O obj){
|
||
|
return map.insert({key,obj});
|
||
|
}
|
||
1 year ago
|
size_t count(T key){
|
||
|
return map.count(key);
|
||
|
}
|
||
|
void SetInitialized(){
|
||
|
initialized=true;
|
||
|
}
|
||
|
size_t size(){
|
||
|
return map.size();
|
||
|
}
|
||
1 year ago
|
//Unlocks the map so items can be added to it again. USE WITH CAUTION! And make sure to lock the map again.
|
||
|
void Unlock(){
|
||
|
initialized=false;
|
||
|
}
|
||
1 year ago
|
//Clears the entire map and unlocks the map so items can be added to it again.
|
||
|
void Reset(){
|
||
|
initialized=false;
|
||
|
map.clear();
|
||
|
}
|
||
|
auto begin()const{
|
||
|
return map.begin();
|
||
|
}
|
||
|
auto end()const{
|
||
|
return map.end();
|
||
|
}
|
||
1 year ago
|
size_t erase(const T&key){
|
||
|
return map.erase(key);
|
||
|
}
|
||
1 year ago
|
auto erase(std::map<T,O>::iterator it){
|
||
|
return map.erase(it);
|
||
|
}
|
||
1 year ago
|
auto erase_if(std::function<bool(std::pair<T,O>)>lambda){
|
||
|
return std::erase_if(map,lambda);
|
||
|
}
|
||
1 year ago
|
};
|
||
|
|
||
|
//A class that has an initialization lock so that when the lock is activated, any further gets that are missing items in it will report themselves for easier debugging detection.
|
||
1 year ago
|
//This unordered map should return items inserted in order. Therefore internally, it's hosted as a vector. As such, if an item is added to this map it's possible the refernce becomes stale when another item is added due to a vector expanding. BEWARE!
|
||
1 year ago
|
template<typename T,typename O>
|
||
|
class safeunorderedmap{
|
||
1 year ago
|
std::unordered_map<T,int>map;
|
||
|
std::vector<O>items;
|
||
1 year ago
|
bool initialized=false;
|
||
|
public:
|
||
|
O&operator[](T key){
|
||
|
if(initialized&&map.count(key)==0){
|
||
1 year ago
|
ERR("WARNING! Trying to get non-existent key "<<key<<"!")
|
||
1 year ago
|
}
|
||
|
if(!initialized){
|
||
|
size_t originalSize=map.size();
|
||
1 year ago
|
map[key]=int(items.size());
|
||
1 year ago
|
if(originalSize==map.size()){
|
||
1 year ago
|
ERR("WARNING! A previously set value has been overwritten! Key: "<<key)
|
||
1 year ago
|
}
|
||
1 year ago
|
items.push_back({});
|
||
|
return items[map[key]];
|
||
1 year ago
|
}else{
|
||
1 year ago
|
return items[map[key]];
|
||
1 year ago
|
}
|
||
|
}
|
||
|
O&at(T key){
|
||
1 year ago
|
return items[map.at(key)];
|
||
1 year ago
|
}
|
||
1 year ago
|
size_t count(T key){
|
||
|
return map.count(key);
|
||
|
}
|
||
1 year ago
|
void SetInitialized(){
|
||
|
initialized=true;
|
||
|
}
|
||
1 year ago
|
size_t size(){
|
||
|
return map.size();
|
||
|
}
|
||
|
//Clears the entire map and unlocks the map so items can be added to it again.
|
||
|
void Reset(){
|
||
|
initialized=false;
|
||
|
map.clear();
|
||
1 year ago
|
items.clear();
|
||
1 year ago
|
}
|
||
1 year ago
|
auto begin()const{
|
||
1 year ago
|
return items.begin();
|
||
1 year ago
|
}
|
||
|
auto end()const{
|
||
1 year ago
|
return items.end();
|
||
1 year ago
|
}
|
||
1 year ago
|
};
|