The Battle for Wesnoth  1.19.0-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 
20 #include <algorithm>
21 #include <string>
22 #include <vector>
23 
24 
25 /**
26  * Used to find units in vectors by their ID.
27  */
29 {
30  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
31  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
32  if (it != recall_list_.end()) {
33  return *it;
34  } else {
35  return unit_ptr();
36  }
37 }
38 
39 /**
40  * Used to find units in vectors by their ID.
41  */
43 {
44  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
45  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
46  if (it != recall_list_.end()) {
47  return *it;
48  } else {
49  return unit_ptr();
50  }
51 }
52 
53 /**
54  * Used to erase units from vectors by their ID.
55  */
56 void recall_list_manager::erase_if_matches_id(const std::string &unit_id)
57 {
58  // using unit_id as reference has potential to cause a crash if the underlying unit becomes invald
59  // https://github.com/wesnoth/wesnoth/issues/6603
60  recall_list_.erase(std::remove_if(recall_list_.begin(), recall_list_.end(),
61  [unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; }),
62  recall_list_.end());
63 }
64 
65 void recall_list_manager::add(const unit_ptr & ptr, int pos)
66 {
67  if (pos < 0 || pos >= static_cast<int>(recall_list_.size())) {
68  recall_list_.push_back(ptr);
69  }
70  else {
71  recall_list_.insert(recall_list_.begin() + pos, ptr);
72  }
73 }
74 
75 std::size_t recall_list_manager::find_index(const std::string & unit_id) const
76 {
77  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
78  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
79 
80  return std::distance(recall_list_.begin(), it);
81 }
82 
83 unit_ptr recall_list_manager::extract_if_matches_id(const std::string &unit_id, int * pos)
84 {
85  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
86  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
87  if (it != recall_list_.end()) {
88  unit_ptr ret = *it;
89  if(pos) {
90  *pos = it - recall_list_.begin();
91  }
92  recall_list_.erase(it);
93  return ret;
94  } else {
95  return unit_ptr();
96  }
97 }
98 
100 {
101  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
102  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; });
103  if (it != recall_list_.end()) {
104  return *it;
105  } else {
106  return unit_ptr();
107  }
108 }
109 
111 {
112  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
113  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; });
114  if (it != recall_list_.end()) {
115  return *it;
116  } else {
117  return unit_ptr();
118  }
119 }
120 
122 {
123  recall_list_.erase(std::remove_if(recall_list_.begin(), recall_list_.end(),
124  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; }),
125  recall_list_.end());
126 }
127 
129 {
130  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
131  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; });
132  if (it != recall_list_.end()) {
133  unit_ptr ret = *it;
134  recall_list_.erase(it);
135  return ret;
136  } else {
137  return unit_ptr();
138  }
139 }
140 
142  assert(idx < recall_list_.size());
143  return recall_list_.erase(recall_list_.begin()+idx);
144 }
145 
147  return recall_list_.erase(it);
148 }
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.
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