Remove map_utils.hpp

Rationale:
- The at() emulation is only used in one file and will become redundant once we move to C++11 soon
- The map_get_value_default function is also only used in one file, and removing it actually makes the logic clearer
- It was moved to map/utils.hpp despite having nothing to do with game maps; removing it is slightly easier than moving it again
This commit is contained in:
Celtic Minstrel 2016-03-21 12:49:19 -04:00
parent d49ef6be41
commit 931376954b
6 changed files with 38 additions and 80 deletions

View file

@ -820,7 +820,6 @@
<Unit filename="../../src/map/location.hpp" />
<Unit filename="../../src/map/map.cpp" />
<Unit filename="../../src/map/map.hpp" />
<Unit filename="../../src/map/utils.hpp" />
<Unit filename="../../src/marked-up_text.cpp" />
<Unit filename="../../src/marked-up_text.hpp" />
<Unit filename="../../src/md5.cpp" />

View file

@ -24339,10 +24339,6 @@
RelativePath="..\..\src\util.hpp"
>
</File>
<File
RelativePath="..\..\src\map\utils.hpp"
>
</File>
<File
RelativePath="..\..\src\variable.cpp"
>

View file

@ -1684,7 +1684,6 @@
B55999F00EC62181008DD061 /* md5.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = md5.cpp; path = ../src/md5.cpp; sourceTree = "<group>"; };
B55999F10EC62181008DD061 /* marked-up_text.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = "marked-up_text.hpp"; path = "../src/marked-up_text.hpp"; sourceTree = "<group>"; };
B55999F20EC62181008DD061 /* marked-up_text.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = "marked-up_text.cpp"; path = "../src/marked-up_text.cpp"; sourceTree = "<group>"; };
B55999F70EC62181008DD061 /* utils.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = utils.hpp; sourceTree = "<group>"; };
B55999F80EC62181008DD061 /* location.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = location.hpp; sourceTree = "<group>"; };
B55999F90EC62181008DD061 /* location.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = location.cpp; sourceTree = "<group>"; };
B55999FA0EC62181008DD061 /* label.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = label.hpp; sourceTree = "<group>"; };
@ -3344,7 +3343,6 @@
B55999F80EC62181008DD061 /* location.hpp */,
B5599A000EC62181008DD061 /* map.cpp */,
B55999FF0EC62181008DD061 /* map.hpp */,
B55999F70EC62181008DD061 /* utils.hpp */,
);
path = map;
sourceTree = "<group>";

View file

@ -18,7 +18,6 @@
#include "formula/callable.hpp"
#include "formula/function.hpp"
#include "map/utils.hpp"
#include "random_new.hpp"
#include "serialization/string_utils.hpp"
@ -48,8 +47,14 @@ map_formula_callable& map_formula_callable::add(const std::string& key,
variant map_formula_callable::get_value(const std::string& key) const
{
return map_get_value_default(values_, key,
fallback_ ? fallback_->query_value(key) : variant());
std::map<std::string,variant>::const_iterator it = values_.find(key);
if(it != values_.end()) {
return it->second;
} else if(fallback_) {
return fallback_->query_value(key);
} else {
return variant();
}
}
void map_formula_callable::get_inputs(std::vector<formula_input>* inputs) const

View file

@ -20,11 +20,14 @@
#ifndef GUI_AUXILIARY_FILTER_HPP_INCLUDED
#define GUI_AUXILIARY_FILTER_HPP_INCLUDED
#include "global.hpp"
#include "gui/widgets/text_box.hpp"
#include "map/utils.hpp"
#include "util.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/unicode.hpp"
#ifndef HAVE_CXX11
#include <stdexcept> // needed for the at() emulation
#endif
namespace gui2
{
@ -35,13 +38,26 @@ inline bool sort(const tpane::titem& lhs,
const std::string& tag,
const bool ascending)
{
#ifdef HAVE_CXX11
if(ascending) {
return lexical_cast<T>(at(lhs.tags, tag))
< lexical_cast<T>(at(rhs.tags, tag));
return lexical_cast<T>(lhs.tags.at(tag))
< lexical_cast<T>(rhs.tags.at(tag));
} else {
return lexical_cast<T>(at(lhs.tags, tag))
> lexical_cast<T>(at(rhs.tags, tag));
return lexical_cast<T>(lhs.tags.at(tag))
> lexical_cast<T>(rhs.tags.at(tag));
}
#else
typedef std::map<std::string,std::string>::const_iterator iterator;
iterator lhs_it = lhs.tags.find(tag), rhs_it = rhs.tags.find(tag);
if(lhs_it == lhs.tags.end() || rhs_it == rhs.tags.end()) {
throw std::out_of_range("Key »" + tag + "« doesn't exist.");
}
if(ascending) {
return lexical_cast<T>(*lhs_it) < lexical_cast<T>(*rhs_it);
} else {
return lexical_cast<T>(*lhs_it) > lexical_cast<T>(*rhs_it);
}
#endif
}
/**
@ -67,8 +83,16 @@ inline bool contains(const tpane::titem& item,
const std::string& tag,
const ttext_box& text_box)
{
return at(item.tags, tag).find(utf8::lowercase(text_box.text()))
#ifdef HAVE_CXX11
return item.tags.at(tag).find(utf8::lowercase(text_box.text()))
!= std::string::npos;
#else
std::map<std::string,std::string>::const_iterator it = item.tags.find(tag);
if(it == item.tags.end()) {
throw std::out_of_range("Key »" + tag + "« doesn't exist.");
}
return it->second.find(utf8::lowercase(text_box.text())) != std::string::npos;
#endif
}
} // namespace gui2

View file

@ -1,64 +0,0 @@
/*
Copyright (C) 2007 - 2016 by David White <dave.net>
Part of the Silver Tree Project
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by or later.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef MAP_UTILS_HPP_INCLUDED
#define MAP_UTILS_HPP_INCLUDED
#include <map>
#include <stdexcept>
template<typename K, typename V>
const V& map_get_value_default(const std::map<K,V>& m, const K& key, const V& val) {
typename std::map<K,V>::const_iterator i = m.find(key);
if(i != m.end()) {
return i->second;
} else {
return val;
}
}
/**
* Emulation for C++11's std::map::at().
*
* Acts like return map[key], but can be used on const map, and if the key
* doesn't exist will throw an exception instead of adding the key.
*
* A non-official reference can be found here:
* http://en.cppreference.com/w/cpp/container/map/at
*
* @note Didn't use template<class K, class V> since that has a problem when
* deducting the type when the key is a std::string and the type send is a
* character string, e.g. "foo". Letting the map deduct the K and V types works.
*
* @throw std::out_of_range When the key is not in the map.
*
* @param map The map search into.
* @param key The key to search for.
*
* @returns A copy of the value of key. @note C++11 uses
* a reference, but it's not possible to create
* a reference from an iterator.
*/
template<class M>
inline typename M::mapped_type at(
const M& map
, const typename M::key_type& key)
{
typename M::const_iterator itor = map.find(key);
if(itor == map.end()) {
throw std::out_of_range("Key »" + key + "« doesn't exist.");
}
return itor->second;
}
#endif