AMay 195924932b
Some checks failed
Emscripten Build / UnitTesting (push) Has been cancelled
Emscripten Build / Build_and_Deploy_Web_Build (push) Has been cancelled
Dynamically load Visual Novel assets. Release Build 13670.
2026-05-26 14:31:08 -05:00

205 lines
5.7 KiB
C++

#pragma region License
/*
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2026 Amy Sigona <sigonasr2@gmail.com>
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.
Portions of this software are copyright © 2024 The FreeType
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
All rights reserved.
*/
#pragma endregion
#pragma once
#include "Error.h"
#include <functional>
#include <map>
#include <unordered_map>
#include<algorithm>
#include<ranges>
//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:
O&operator[](T key){
if(initialized&&map.count(key)==0){
ERR("WARNING! Trying to get non-existent key "<<key<<"!")
}
if(!initialized){
size_t originalSize=map.size();
O&val=map[key];
if(originalSize==map.size()){
ERR("WARNING! A previously set value has been overwritten! Key: "<<key)
}
return val;
}else{
return map[key];
}
}
O&at(T key){
return map.at(key);
}
const O&at(T key)const{
return map.at(key);
}
auto insert(T key,O obj){
return map.insert({key,obj});
}
size_t count(T key)const{
return map.count(key);
}
void SetInitialized(){
initialized=true;
}
size_t size(){
return map.size();
}
//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;
}
//Clears the entire map and unlocks the map so items can be added to it again.
void Reset(){
initialized=false;
map.clear();
}
auto find(const T&key)const{
return map.find(key);
}
auto find(T&key){
return map.find(key);
}
auto begin()const{
return map.begin();
}
auto end()const{
return map.end();
}
size_t erase(const T&key){
return map.erase(key);
}
auto erase(std::map<T,O>::iterator it){
return map.erase(it);
}
auto erase_if(std::function<bool(std::pair<T,O>)>lambda){
return std::erase_if(map,lambda);
}
auto contains(const T&key)const{
return map.contains(key);
}
};
//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.
//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!
template<typename T,typename O>
class safeunorderedmap{
std::unordered_map<T,int>map;
std::vector<O>items;
bool initialized=false;
public:
O&operator[](T key){
if(initialized&&map.count(key)==0){
ERR("WARNING! Trying to get non-existent key "<<key<<"!")
}
if(!initialized){
size_t originalSize=map.size();
map[key]=int(items.size());
if(originalSize==map.size()){
ERR("WARNING! A previously set value has been overwritten! Key: "<<key)
}
items.push_back({});
return items[map[key]];
}else{
return items[map[key]];
}
}
const O&operator[](T key)const{
if(initialized&&map.count(key)==0){
ERR("WARNING! Trying to get non-existent key "<<key<<"!")
}
return items.at(map.at(key));
}
O&at(T key){
return items.at(map.at(key));
}
const O&at(T key)const{
return items.at(map.at(key));
}
size_t count(T key)const{
return map.count(key);
}
void SetInitialized(){
initialized=true;
}
//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;
}
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();
items.clear();
}
auto begin(){
return items.begin();
}
auto end(){
return items.end();
}
auto begin()const{
return items.begin();
}
auto end()const{
return items.end();
}
auto contains(T&key){
return count(key);
}
auto contains(const T&key){
return count(key);
}
auto contains(O&obj){
return std::ranges::contains(items,obj);
}
auto contains(const O&obj){
return std::ranges::contains(items,obj);
}
void insert(T key,O obj){
(*this)[key]=obj;
}
auto find(const O&obj)const{
return std::find(items.begin(),items.end(),obj);
}
};