//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<typenameT,typenameO>
classsafemap{
std::map<T,O>map;
boolinitialized=false;
public:
O&operator[](Tkey){
if(initialized&&map.count(key)==0){
std::cout<<"WARNING! Trying to get non-existent key "<<key<<"!"<<std::endl;
std::cout<<"WARNING! A previously set value has been overwritten! Key: "<<key<<std::endl;
throw;
}
returnval;
}else{
returnmap[key];
}
}
O&at(Tkey){
returnmap.at(key);
}
size_tcount(Tkey){
returnmap.count(key);
}
voidSetInitialized(){
initialized=true;
}
size_tsize(){
returnmap.size();
}
//Clears the entire map and unlocks the map so items can be added to it again.
voidReset(){
initialized=false;
map.clear();
}
autobegin()const{
returnmap.begin();
}
autoend()const{
returnmap.end();
}
};
//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<typenameT,typenameO>
classsafeunorderedmap{
std::unordered_map<T,O>map;
boolinitialized=false;
public:
O&operator[](Tkey){
if(initialized&&map.count(key)==0){
std::cout<<"WARNING! Trying to get non-existent key "<<key<<"!"<<std::endl;
throw;
}
if(!initialized){
size_toriginalSize=map.size();
O&val=map[key];
if(originalSize==map.size()){
std::cout<<"WARNING! A previously set value has been overwritten! Key: "<<key<<std::endl;