The Battle for Wesnoth  1.15.9+dev
recall_list_manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "units/ptr.hpp"
18 
19 #include <string>
20 #include <vector>
21 
22 namespace ai {
23  class readonly_context_impl;
24 }
25 
26 /** This class encapsulates the recall list of a team. */
28 public:
30  typedef std::vector<unit_ptr >::const_iterator const_iterator;
31 
32  /** begin iterator */
33  iterator begin() { return recall_list_.begin();}
34  /** end iterator */
35  iterator end() { return recall_list_.end(); }
36 
37  /** begin const iterator */
38  const_iterator begin() const { return recall_list_.begin();}
39  /** end const iterator */
40  const_iterator end() const { return recall_list_.end(); }
41 
42  /** vector style dereference */
43  unit_ptr operator[](std::size_t index) { return recall_list_[index]; }
44  /** vector style dereference */
45  unit_const_ptr operator[](std::size_t index) const { return recall_list_[index]; }
46 
47  /** Find a unit by id. Null pointer if not found. */
48  unit_ptr find_if_matches_id(const std::string & unit_id);
49  /**
50  * Find a unit by id, and extract from this object if found. Null if not found.
51  * @a pos an output paramter, to know in which position the unit was.
52  */
53  unit_ptr extract_if_matches_id(const std::string & unit_id, int * pos = nullptr);
54  /** Const find by id. */
55  unit_const_ptr find_if_matches_id(const std::string & unit_id) const;
56  /** Erase any unit with this id. */
57  void erase_if_matches_id(const std::string & unit_id);
58 
59  /** Find a unit by underlying id. Null pointer if not found. */
60  unit_ptr find_if_matches_underlying_id(std::size_t uid);
61  /** Find a unit by underlying id, and extract if found. Null if not found. */
62  unit_ptr extract_if_matches_underlying_id(std::size_t uid);
63  /** Const find by underlying id. */
64  unit_const_ptr find_if_matches_underlying_id(std::size_t uid) const;
65  /** Erase any unit with this underlying id. */
66  void erase_by_underlying_id(std::size_t uid);
67 
68  /** Erase by index. */
69  iterator erase_index(std::size_t index);
70  /** Erase an iterator to this object. */
71  iterator erase(iterator it);
72 
73  /** Find the index of a unit by its id. */
74  std::size_t find_index(const std::string & unit_id) const;
75  /** Get the number of units on the list. */
76  std::size_t size() const { return recall_list_.size(); }
77  /** Is it empty? */
78  bool empty() const { return recall_list_.empty(); }
79 
80  /**
81  * Add a unit to the list.
82  * @a pos the location where to insert the unit, -1 for 'at end'
83  */
84  void add(const unit_ptr & ptr, int pos = -1);
85 
86 private:
87  /**
88  * The underlying data struture.
89  * TODO: Should this be a map based on underlying id instead?
90  */
91  std::vector<unit_ptr > recall_list_;
92 };
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:25
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:26
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
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:218
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:71
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:24
std::vector< unit_ptr > recall_list_
The underlying data struture.