The Battle for Wesnoth  1.19.5+dev
recall_list_manager.hpp
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 #pragma once
17 
18 #include "units/ptr.hpp"
19 
20 #include <string>
21 #include <vector>
22 
23 /** This class encapsulates the recall list of a team. */
25 public:
27  typedef std::vector<unit_ptr >::const_iterator const_iterator;
28 
29  /** begin iterator */
30  iterator begin() { return recall_list_.begin();}
31  /** end iterator */
32  iterator end() { return recall_list_.end(); }
33 
34  /** begin const iterator */
35  const_iterator begin() const { return recall_list_.begin();}
36  /** end const iterator */
37  const_iterator end() const { return recall_list_.end(); }
38 
39  /** vector style dereference */
40  unit_ptr operator[](std::size_t index) { return recall_list_[index]; }
41  /** vector style dereference */
42  unit_const_ptr operator[](std::size_t index) const { return recall_list_[index]; }
43 
44  /** Find a unit by id. Null pointer if not found. */
45  unit_ptr find_if_matches_id(const std::string & unit_id);
46  /**
47  * Find a unit by id, and extract from this object if found. Null if not found.
48  * @a pos an output paramter, to know in which position the unit was.
49  */
50  unit_ptr extract_if_matches_id(const std::string & unit_id, int * pos = nullptr);
51  /** Const find by id. */
52  unit_const_ptr find_if_matches_id(const std::string & unit_id) const;
53  /** Erase any unit with this id. */
54  void erase_if_matches_id(const std::string & unit_id);
55 
56  /** Find a unit by underlying id. Null pointer if not found. */
57  unit_ptr find_if_matches_underlying_id(std::size_t uid);
58  /** Find a unit by underlying id, and extract if found. Null if not found. */
60  /** Const find by underlying id. */
61  unit_const_ptr find_if_matches_underlying_id(std::size_t uid) const;
62  /** Erase any unit with this underlying id. */
63  void erase_by_underlying_id(std::size_t uid);
64 
65  /** Erase by index. */
66  iterator erase_index(std::size_t index);
67  /** Erase an iterator to this object. */
69 
70  /** Find the index of a unit by its id. */
71  std::size_t find_index(const std::string & unit_id) const;
72  /** Get the number of units on the list. */
73  std::size_t size() const { return recall_list_.size(); }
74  /** Is it empty? */
75  bool empty() const { return recall_list_.empty(); }
76 
77  /**
78  * Add a unit to the list.
79  * @a pos the location where to insert the unit, -1 for 'at end'
80  */
81  void add(const unit_ptr & ptr, int pos = -1);
82 
83 private:
84  /**
85  * The underlying data struture.
86  * TODO: Should this be a map based on underlying id instead?
87  */
88  std::vector<unit_ptr > recall_list_;
89 };
This class encapsulates the recall list of a team.
std::size_t size() const
Get the number of units on the list.
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.
bool empty() const
Is it empty?
unit_ptr find_if_matches_underlying_id(std::size_t uid)
Find a unit by underlying id.
std::vector< unit_ptr >::const_iterator const_iterator
const_iterator end() const
end const iterator
std::vector< unit_ptr >::iterator iterator
unit_ptr operator[](std::size_t index)
vector style dereference
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.
unit_const_ptr operator[](std::size_t index) const
vector style dereference
iterator end()
end iterator
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.
iterator begin()
begin iterator
void erase_if_matches_id(const std::string &unit_id)
Erase any unit with this id.
const_iterator begin() const
begin const iterator
unit_ptr extract_if_matches_underlying_id(std::size_t uid)
Find a unit by underlying id, and extract if found.
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
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