The Battle for Wesnoth  1.17.0-dev
recall_list_manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
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 namespace ai {
24  class readonly_context_impl;
25 }
26 
27 /** This class encapsulates the recall list of a team. */
29 public:
31  typedef std::vector<unit_ptr >::const_iterator const_iterator;
32 
33  /** begin iterator */
34  iterator begin() { return recall_list_.begin();}
35  /** end iterator */
36  iterator end() { return recall_list_.end(); }
37 
38  /** begin const iterator */
39  const_iterator begin() const { return recall_list_.begin();}
40  /** end const iterator */
41  const_iterator end() const { return recall_list_.end(); }
42 
43  /** vector style dereference */
44  unit_ptr operator[](std::size_t index) { return recall_list_[index]; }
45  /** vector style dereference */
46  unit_const_ptr operator[](std::size_t index) const { return recall_list_[index]; }
47 
48  /** Find a unit by id. Null pointer if not found. */
49  unit_ptr find_if_matches_id(const std::string & unit_id);
50  /**
51  * Find a unit by id, and extract from this object if found. Null if not found.
52  * @a pos an output paramter, to know in which position the unit was.
53  */
54  unit_ptr extract_if_matches_id(const std::string & unit_id, int * pos = nullptr);
55  /** Const find by id. */
56  unit_const_ptr find_if_matches_id(const std::string & unit_id) const;
57  /** Erase any unit with this id. */
58  void erase_if_matches_id(const std::string & unit_id);
59 
60  /** Find a unit by underlying id. Null pointer if not found. */
61  unit_ptr find_if_matches_underlying_id(std::size_t uid);
62  /** Find a unit by underlying id, and extract if found. Null if not found. */
63  unit_ptr extract_if_matches_underlying_id(std::size_t uid);
64  /** Const find by underlying id. */
65  unit_const_ptr find_if_matches_underlying_id(std::size_t uid) const;
66  /** Erase any unit with this underlying id. */
67  void erase_by_underlying_id(std::size_t uid);
68 
69  /** Erase by index. */
70  iterator erase_index(std::size_t index);
71  /** Erase an iterator to this object. */
72  iterator erase(iterator it);
73 
74  /** Find the index of a unit by its id. */
75  std::size_t find_index(const std::string & unit_id) const;
76  /** Get the number of units on the list. */
77  std::size_t size() const { return recall_list_.size(); }
78  /** Is it empty? */
79  bool empty() const { return recall_list_.empty(); }
80 
81  /**
82  * Add a unit to the list.
83  * @a pos the location where to insert the unit, -1 for 'at end'
84  */
85  void add(const unit_ptr & ptr, int pos = -1);
86 
87 private:
88  /**
89  * The underlying data struture.
90  * TODO: Should this be a map based on underlying id instead?
91  */
92  std::vector<unit_ptr > recall_list_;
93 };
bool empty() const
Is it empty?
This class encapsulates the recall list of a team.
unit_ptr operator[](std::size_t index)
vector style dereference
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
unit_const_ptr operator[](std::size_t index) const
vector style dereference
std::vector< unit_ptr >::const_iterator const_iterator
iterator begin()
begin iterator
void erase(const std::string &key)
Definition: general.cpp:201
iterator end()
end iterator
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:72
std::size_t size() const
Get the number of units on the list.
const_iterator begin() const
begin const iterator
const_iterator end() const
end const iterator
std::vector< unit_ptr >::iterator iterator
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
std::vector< unit_ptr > recall_list_
The underlying data struture.