The Battle for Wesnoth  1.19.5+dev
recall_list_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "recall_list_manager.hpp"
17 #include "units/unit.hpp"
18 #include "units/ptr.hpp"
19 #include "utils/general.hpp"
20 
21 #include <algorithm>
22 #include <string>
23 #include <vector>
24 
25 
26 /**
27  * Used to find units in vectors by their ID.
28  */
30 {
31  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
32  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
33  if (it != recall_list_.end()) {
34  return *it;
35  } else {
36  return unit_ptr();
37  }
38 }
39 
40 /**
41  * Used to find units in vectors by their ID.
42  */
44 {
45  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
46  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
47  if (it != recall_list_.end()) {
48  return *it;
49  } else {
50  return unit_ptr();
51  }
52 }
53 
54 /**
55  * Used to erase units from vectors by their ID.
56  */
57 void recall_list_manager::erase_if_matches_id(const std::string &unit_id)
58 {
59  // using unit_id as reference has potential to cause a crash if the underlying unit becomes invald
60  // https://github.com/wesnoth/wesnoth/issues/6603
61  utils::erase_if(recall_list_, [unit_id](const unit_ptr& ptr) { return ptr->id() == unit_id; });
62 }
63 
64 void recall_list_manager::add(const unit_ptr & ptr, int pos)
65 {
66  if (pos < 0 || pos >= static_cast<int>(recall_list_.size())) {
67  recall_list_.push_back(ptr);
68  }
69  else {
70  recall_list_.insert(recall_list_.begin() + pos, ptr);
71  }
72 }
73 
74 std::size_t recall_list_manager::find_index(const std::string & unit_id) const
75 {
76  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
77  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
78 
79  return std::distance(recall_list_.begin(), it);
80 }
81 
82 unit_ptr recall_list_manager::extract_if_matches_id(const std::string &unit_id, int * pos)
83 {
84  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
85  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
86  if (it != recall_list_.end()) {
87  unit_ptr ret = *it;
88  if(pos) {
89  *pos = it - recall_list_.begin();
90  }
91  recall_list_.erase(it);
92  return ret;
93  } else {
94  return unit_ptr();
95  }
96 }
97 
99 {
100  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
101  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; });
102  if (it != recall_list_.end()) {
103  return *it;
104  } else {
105  return unit_ptr();
106  }
107 }
108 
110 {
111  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
112  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; });
113  if (it != recall_list_.end()) {
114  return *it;
115  } else {
116  return unit_ptr();
117  }
118 }
119 
121 {
122  utils::erase_if(recall_list_, [uid](const unit_ptr& ptr) { return ptr->underlying_id() == uid; });
123 }
124 
126 {
127  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
128  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; });
129  if (it != recall_list_.end()) {
130  unit_ptr ret = *it;
131  recall_list_.erase(it);
132  return ret;
133  } else {
134  return unit_ptr();
135  }
136 }
137 
139  assert(idx < recall_list_.size());
140  return recall_list_.erase(recall_list_.begin()+idx);
141 }
142 
144  return recall_list_.erase(it);
145 }
unit_ptr extract_if_matches_id(const std::string &unit_id, int *pos=nullptr)
Find a unit by id, and extract from this object if found.
iterator erase(iterator it)
Erase an iterator to this object.
unit_ptr find_if_matches_underlying_id(std::size_t uid)
Find a unit by underlying id.
void add(const unit_ptr &ptr, int pos=-1)
Add a unit to the list.
std::vector< unit_ptr > recall_list_
The underlying data struture.
unit_ptr find_if_matches_id(const std::string &unit_id)
Find a unit by id.
iterator erase_index(std::size_t index)
Erase by index.
std::size_t find_index(const std::string &unit_id) const
Find the index of a unit by its id.
void erase_by_underlying_id(std::size_t uid)
Erase any unit with this underlying id.
void erase_if_matches_id(const std::string &unit_id)
Erase any unit with this id.
unit_ptr extract_if_matches_underlying_id(std::size_t uid)
Find a unit by underlying id, and extract if found.
void erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
Definition: general.hpp:100
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26