mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-04 00:03:28 +00:00
Fix -Wshorten-64-to-32: config.hpp and config.cpp
Use std::size_t instead of unsigned int for container sizes. There's one place where it still uses signed int, for using a negative index to access children from the end of the array. Overflow shouldn't be a worry here, as it would require 2**31 objects to be in the container, implying the container and children would use at least 32GB even if every child object was empty. Co-authored by: Pentarctagon
This commit is contained in:
parent
c5763af4e3
commit
60c840bc16
@ -298,7 +298,7 @@ config::const_child_itors config::child_range(config_key_type key) const
|
||||
return const_child_itors(const_child_iterator(p->begin()), const_child_iterator(p->end()));
|
||||
}
|
||||
|
||||
unsigned config::child_count(config_key_type key) const
|
||||
std::size_t config::child_count(config_key_type key) const
|
||||
{
|
||||
child_map::const_iterator i = children_.find(key);
|
||||
if(i != children_.end()) {
|
||||
@ -308,12 +308,12 @@ unsigned config::child_count(config_key_type key) const
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned config::all_children_count() const
|
||||
std::size_t config::all_children_count() const
|
||||
{
|
||||
return ordered_children.size();
|
||||
}
|
||||
|
||||
unsigned config::attribute_count() const
|
||||
std::size_t config::attribute_count() const
|
||||
{
|
||||
return std::count_if(values_.begin(), values_.end(), [](const attribute& v) { return !v.second.blank(); });
|
||||
}
|
||||
@ -336,7 +336,7 @@ auto get_child_impl(Tchildren& children, config_key_type key, int n) -> optional
|
||||
}
|
||||
|
||||
if(n < 0) {
|
||||
n = i->second.size() + n;
|
||||
n = static_cast<int>(i->second.size()) + n;
|
||||
}
|
||||
|
||||
try {
|
||||
@ -468,7 +468,7 @@ config& config::add_child(config_key_type key, config&& val)
|
||||
return *v.back();
|
||||
}
|
||||
|
||||
config& config::add_child_at(config_key_type key, const config& val, unsigned index)
|
||||
config& config::add_child_at(config_key_type key, const config& val, std::size_t index)
|
||||
{
|
||||
child_list& v = map_get(children_, key);
|
||||
if(index > v.size()) {
|
||||
@ -514,7 +514,7 @@ size_t config::find_total_first_of(config_key_type key, size_t start)
|
||||
return static_cast<size_t>(pos - ordered_begin());
|
||||
}
|
||||
|
||||
config& config::add_child_at_total(config_key_type key, const config &val, size_t pos)
|
||||
config& config::add_child_at_total(config_key_type key, const config &val, std::size_t pos)
|
||||
{
|
||||
assert(pos <= ordered_children.size());
|
||||
if(pos == ordered_children.size()) {
|
||||
@ -535,7 +535,7 @@ config& config::add_child_at_total(config_key_type key, const config &val, size_
|
||||
|
||||
auto pl = next->pos;
|
||||
child_list& l = pl->second;
|
||||
unsigned int index = next->index;
|
||||
const auto index = next->index;
|
||||
config& res = **(l.emplace(l.begin() + index, new config(val)));
|
||||
|
||||
for(auto ord = next; ord != end; ++ord) {
|
||||
@ -596,12 +596,12 @@ void config::splice_children(config& src, const std::string& key)
|
||||
child_list& dst = map_get(children_, key);
|
||||
child_map::iterator i_dst = children_.find(key);
|
||||
|
||||
unsigned before = dst.size();
|
||||
const auto before = dst.size();
|
||||
dst.insert(dst.end(), std::make_move_iterator(i_src->second.begin()), std::make_move_iterator(i_src->second.end()));
|
||||
src.children_.erase(i_src);
|
||||
// key might be a reference to i_src->first, so it is no longer usable.
|
||||
|
||||
for(unsigned j = before; j < dst.size(); ++j) {
|
||||
for(std::size_t j = before; j < dst.size(); ++j) {
|
||||
ordered_children.emplace_back(i_dst, j);
|
||||
}
|
||||
}
|
||||
@ -617,11 +617,11 @@ void config::recursive_clear_value(config_key_type key)
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<config::child_pos>::iterator config::remove_child(const child_map::iterator& pos, unsigned index)
|
||||
std::vector<config::child_pos>::iterator config::remove_child(const child_map::iterator& pos, std::size_t index)
|
||||
{
|
||||
/* Find the position with the correct index and decrement all the
|
||||
indices in the ordering that are above this index. */
|
||||
unsigned found = 0;
|
||||
std::size_t found = 0;
|
||||
for(child_pos& p : ordered_children) {
|
||||
if(p.pos != pos) {
|
||||
continue;
|
||||
@ -646,7 +646,7 @@ config::all_children_iterator config::erase(const config::all_children_iterator&
|
||||
return all_children_iterator(remove_child(i.i_->pos, i.i_->index));
|
||||
}
|
||||
|
||||
void config::remove_child(config_key_type key, unsigned index)
|
||||
void config::remove_child(config_key_type key, std::size_t index)
|
||||
{
|
||||
child_map::iterator i = children_.find(key);
|
||||
if(i == children_.end() || index >= i->second.size()) {
|
||||
@ -671,7 +671,7 @@ void config::remove_children(config_key_type key, std::function<bool(const confi
|
||||
|
||||
auto child_it = std::find_if(pos->second.begin(), pos->second.end(), predicate);
|
||||
while(child_it != pos->second.end()) {
|
||||
unsigned index = std::distance(pos->second.begin(), child_it);
|
||||
const auto index = std::distance(pos->second.begin(), child_it);
|
||||
remove_child(pos, index);
|
||||
child_it = std::find_if(pos->second.begin() + index, pos->second.end(), predicate);
|
||||
}
|
||||
@ -1041,7 +1041,7 @@ void config::apply_diff(const config& diff, bool track /* = false */)
|
||||
}
|
||||
|
||||
for(const config& i : diff.child_range("insert_child")) {
|
||||
const std::size_t index = lexical_cast<std::size_t>(i["index"].str());
|
||||
const auto index = lexical_cast<std::size_t>(i["index"].str());
|
||||
for(const any_child item : i.all_children_range()) {
|
||||
config& inserted = add_child_at(item.key, item.cfg, index);
|
||||
if(track) {
|
||||
@ -1051,7 +1051,7 @@ void config::apply_diff(const config& diff, bool track /* = false */)
|
||||
}
|
||||
|
||||
for(const config& i : diff.child_range("delete_child")) {
|
||||
const std::size_t index = lexical_cast<std::size_t>(i["index"].str());
|
||||
const auto index = lexical_cast<std::size_t>(i["index"].str());
|
||||
for(const any_child item : i.all_children_range()) {
|
||||
if(!track) {
|
||||
remove_child(item.key, index);
|
||||
@ -1071,7 +1071,7 @@ void config::clear_diff_track(const config& diff)
|
||||
{
|
||||
remove_attribute(diff_track_attribute);
|
||||
for(const config& i : diff.child_range("delete_child")) {
|
||||
const std::size_t index = lexical_cast<std::size_t>(i["index"].str());
|
||||
const auto index = lexical_cast<std::size_t>(i["index"].str());
|
||||
for(const any_child item : i.all_children_range()) {
|
||||
remove_child(item.key, index);
|
||||
}
|
||||
@ -1143,10 +1143,10 @@ void config::merge_with(const config& c)
|
||||
}
|
||||
|
||||
// Remove those marked so
|
||||
std::map<std::string, unsigned> removals;
|
||||
std::map<std::string, std::size_t> removals;
|
||||
for(const child_pos& pos : to_remove) {
|
||||
const std::string& tag = pos.pos->first;
|
||||
unsigned& removes = removals[tag];
|
||||
auto& removes = removals[tag];
|
||||
remove_child(tag, pos.index - removes++);
|
||||
}
|
||||
}
|
||||
|
@ -200,10 +200,10 @@ public:
|
||||
{
|
||||
typedef config value_type;
|
||||
typedef std::random_access_iterator_tag iterator_category;
|
||||
typedef int difference_type;
|
||||
typedef config *pointer;
|
||||
typedef config &reference;
|
||||
typedef child_list::iterator Itor;
|
||||
typedef Itor::difference_type difference_type;
|
||||
typedef child_iterator this_type;
|
||||
explicit child_iterator(const Itor &i): i_(i) {}
|
||||
|
||||
@ -242,10 +242,10 @@ public:
|
||||
{
|
||||
typedef const config value_type;
|
||||
typedef std::random_access_iterator_tag iterator_category;
|
||||
typedef int difference_type;
|
||||
typedef const config *pointer;
|
||||
typedef const config &reference;
|
||||
typedef child_list::const_iterator Itor;
|
||||
typedef Itor::difference_type difference_type;
|
||||
typedef const_child_iterator this_type;
|
||||
explicit const_child_iterator(const Itor &i): i_(i) {}
|
||||
const_child_iterator(const child_iterator &i): i_(i.i_) {}
|
||||
@ -305,10 +305,10 @@ public:
|
||||
{
|
||||
typedef attribute value_type;
|
||||
typedef std::bidirectional_iterator_tag iterator_category;
|
||||
typedef int difference_type;
|
||||
typedef attribute *pointer;
|
||||
typedef attribute &reference;
|
||||
typedef attribute_map::iterator Itor;
|
||||
typedef Itor::difference_type difference_type;
|
||||
explicit attribute_iterator(const Itor &i): i_(i) {}
|
||||
|
||||
attribute_iterator &operator++() { ++i_; return *this; }
|
||||
@ -333,10 +333,10 @@ public:
|
||||
{
|
||||
typedef const attribute value_type;
|
||||
typedef std::bidirectional_iterator_tag iterator_category;
|
||||
typedef int difference_type;
|
||||
typedef const attribute *pointer;
|
||||
typedef const attribute &reference;
|
||||
typedef attribute_map::const_iterator Itor;
|
||||
typedef Itor::difference_type difference_type;
|
||||
explicit const_attribute_iterator(const Itor &i): i_(i) {}
|
||||
const_attribute_iterator(attribute_iterator& i): i_(i.i_) {}
|
||||
|
||||
@ -363,10 +363,10 @@ public:
|
||||
|
||||
child_itors child_range(config_key_type key);
|
||||
const_child_itors child_range(config_key_type key) const;
|
||||
unsigned child_count(config_key_type key) const;
|
||||
unsigned all_children_count() const;
|
||||
std::size_t child_count(config_key_type key) const;
|
||||
std::size_t all_children_count() const;
|
||||
/** Count the number of non-blank attributes */
|
||||
unsigned attribute_count() const;
|
||||
std::size_t attribute_count() const;
|
||||
|
||||
/**
|
||||
* Determine whether a config has a child or not.
|
||||
@ -463,7 +463,7 @@ public:
|
||||
* @param val the contents of the tag
|
||||
* @param index is the index of the new child within all children of type key.
|
||||
*/
|
||||
config& add_child_at(config_key_type key, const config &val, unsigned index);
|
||||
config& add_child_at(config_key_type key, const config &val, std::size_t index);
|
||||
|
||||
config &add_child(config_key_type key, config &&val);
|
||||
|
||||
@ -638,7 +638,7 @@ public:
|
||||
*/
|
||||
void splice_children(config &src, const std::string &key);
|
||||
|
||||
void remove_child(config_key_type key, unsigned index);
|
||||
void remove_child(config_key_type key, std::size_t index);
|
||||
/**
|
||||
* Removes all children with tag @a key for which @a p returns true.
|
||||
*/
|
||||
@ -659,9 +659,9 @@ public:
|
||||
|
||||
struct child_pos
|
||||
{
|
||||
child_pos(child_map::iterator p, unsigned i) : pos(p), index(i) {}
|
||||
child_pos(child_map::iterator p, std::size_t i) : pos(p), index(i) {}
|
||||
child_map::iterator pos;
|
||||
unsigned index;
|
||||
std::size_t index;
|
||||
|
||||
bool operator==(const child_pos& o) const { return pos == o.pos && index == o.index; }
|
||||
bool operator!=(const child_pos& o) const { return !operator==(o); }
|
||||
@ -687,10 +687,10 @@ public:
|
||||
|
||||
typedef any_child value_type;
|
||||
typedef std::random_access_iterator_tag iterator_category;
|
||||
typedef int difference_type;
|
||||
typedef arrow_helper pointer;
|
||||
typedef any_child reference;
|
||||
typedef std::vector<child_pos>::iterator Itor;
|
||||
typedef Itor::difference_type difference_type;
|
||||
typedef all_children_iterator this_type;
|
||||
explicit all_children_iterator(const Itor &i): i_(i) {}
|
||||
|
||||
@ -739,10 +739,10 @@ public:
|
||||
|
||||
typedef const any_child value_type;
|
||||
typedef std::random_access_iterator_tag iterator_category;
|
||||
typedef int difference_type;
|
||||
typedef const arrow_helper pointer;
|
||||
typedef const any_child reference;
|
||||
typedef std::vector<child_pos>::const_iterator Itor;
|
||||
typedef Itor::difference_type difference_type;
|
||||
typedef const_all_children_iterator this_type;
|
||||
explicit const_all_children_iterator(const Itor &i): i_(i) {}
|
||||
const_all_children_iterator(const all_children_iterator& i): i_(i.i_) {}
|
||||
@ -785,8 +785,8 @@ public:
|
||||
* @param val the contents of the tag
|
||||
* @param pos is the index of the new child in _all_ children.
|
||||
*/
|
||||
config& add_child_at_total(config_key_type key, const config &val, size_t pos);
|
||||
size_t find_total_first_of(config_key_type key, size_t start = 0);
|
||||
config& add_child_at_total(config_key_type key, const config &val, std::size_t pos);
|
||||
std::size_t find_total_first_of(config_key_type key, std::size_t start = 0);
|
||||
|
||||
typedef boost::iterator_range<all_children_iterator> all_children_itors;
|
||||
typedef boost::iterator_range<const_all_children_iterator> const_all_children_itors;
|
||||
@ -906,7 +906,7 @@ private:
|
||||
/**
|
||||
* Removes the child at position @a pos of @a l.
|
||||
*/
|
||||
std::vector<child_pos>::iterator remove_child(const child_map::iterator &l, unsigned pos);
|
||||
std::vector<child_pos>::iterator remove_child(const child_map::iterator &l, std::size_t pos);
|
||||
|
||||
/** All the attributes of this node. */
|
||||
attribute_map values_;
|
||||
|
Loading…
x
Reference in New Issue
Block a user