The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
recall_list_manager.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2014 - 2025
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
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 * Used to find units in vectors by their ID.
27 */
29{
30 auto it = utils::ranges::find(recall_list_, unit_id, &unit::id);
31 if (it != recall_list_.end()) {
32 return *it;
33 } else {
34 return unit_ptr();
35 }
36}
37
38/**
39 * Used to find units in vectors by their ID.
40 */
42{
43 auto it = utils::ranges::find(recall_list_, unit_id, &unit::id);
44 if (it != recall_list_.end()) {
45 return *it;
46 } else {
47 return unit_ptr();
48 }
49}
50
51/**
52 * Used to erase units from vectors by their ID.
53 */
54void recall_list_manager::erase_if_matches_id(const std::string &unit_id)
55{
56 // using unit_id as reference has potential to cause a crash if the underlying unit becomes invald
57 // https://github.com/wesnoth/wesnoth/issues/6603
58 utils::erase_if(recall_list_, [unit_id](const unit_ptr& ptr) { return ptr->id() == unit_id; });
59}
60
61void recall_list_manager::add(const unit_ptr & ptr, int pos)
62{
63 if (pos < 0 || pos >= static_cast<int>(recall_list_.size())) {
64 recall_list_.push_back(ptr);
65 }
66 else {
67 recall_list_.insert(recall_list_.begin() + pos, ptr);
68 }
69}
70
71std::size_t recall_list_manager::find_index(const std::string & unit_id) const
72{
73 auto it = utils::ranges::find(recall_list_, unit_id, &unit::id);
74 return std::distance(recall_list_.begin(), it);
75}
76
77unit_ptr recall_list_manager::extract_if_matches_id(const std::string &unit_id, int * pos)
78{
79 auto it = utils::ranges::find(recall_list_, unit_id, &unit::id);
80 if (it != recall_list_.end()) {
81 unit_ptr ret = *it;
82 if(pos) {
83 *pos = it - recall_list_.begin();
84 }
85 recall_list_.erase(it);
86 return ret;
87 } else {
88 return unit_ptr();
89 }
90}
91
93{
95 if (it != recall_list_.end()) {
96 return *it;
97 } else {
98 return unit_ptr();
99 }
100}
101
103{
105 if (it != recall_list_.end()) {
106 return *it;
107 } else {
108 return unit_ptr();
109 }
110}
111
113{
114 utils::erase_if(recall_list_, [uid](const unit_ptr& ptr) { return ptr->underlying_id() == uid; });
115}
116
118{
120 if (it != recall_list_.end()) {
121 unit_ptr ret = *it;
122 recall_list_.erase(it);
123 return ret;
124 } else {
125 return unit_ptr();
126 }
127}
128
129std::vector<unit_ptr>::iterator recall_list_manager::erase_index(std::size_t idx) {
130 assert(idx < recall_list_.size());
131 return recall_list_.erase(recall_list_.begin()+idx);
132}
133
134std::vector<unit_ptr>::iterator recall_list_manager::erase(const std::vector<unit_ptr>::iterator& it) {
135 return recall_list_.erase(it);
136}
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.
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.
iterator erase(const iterator &it)
Erase an iterator to this object.
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.
const std::string & id() const
Gets this unit's id.
Definition unit.hpp:286
std::size_t underlying_id() const
This unit's unique internal ID.
Definition unit.hpp:298
auto find(Container &container, const Value &value, const Projection &projection={})
Definition general.hpp:213
void erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
Definition general.hpp:108
std::shared_ptr< const unit > unit_const_ptr
Definition ptr.hpp:27
std::shared_ptr< unit > unit_ptr
Definition ptr.hpp:26