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 /// This class encapsulates the recall list of a team. 16 17 #pragma once 18 19 #include "units/ptr.hpp" 20 21 #include <string> 22 #include <vector> 23 24 namespace ai { 25 class readonly_context_impl; 26 } 27 28 class recall_list_manager { 29 public: 30 typedef std::vector<unit_ptr >::iterator iterator; 31 typedef std::vector<unit_ptr >::const_iterator const_iterator; 32 begin()33 iterator begin() { return recall_list_.begin();} //!< begin iterator end()34 iterator end() { return recall_list_.end(); } //!< end iterator 35 begin() const36 const_iterator begin() const { return recall_list_.begin();} //!< begin const iterator end() const37 const_iterator end() const { return recall_list_.end(); } //!< end const iterator 38 operator [](size_t index)39 unit_ptr operator[](size_t index) { return recall_list_[index]; } //!< vector style dereference operator [](size_t index) const40 unit_const_ptr operator[](size_t index) const { return recall_list_[index]; } //!< vector style dereference 41 42 unit_ptr find_if_matches_id(const std::string & unit_id); //!< Find a unit by id. Null pointer if not found. 43 /// Find a unit by id, and extract from this object if found. Null if not found. 44 /// @a pos an output paramter, to know in which position the unit was. 45 unit_ptr extract_if_matches_id(const std::string & unit_id, int * pos = nullptr); 46 unit_const_ptr find_if_matches_id(const std::string & unit_id) const; //!< Const find by id. 47 void erase_if_matches_id(const std::string & unit_id); //!< Erase any unit with this id. 48 49 unit_ptr find_if_matches_underlying_id(size_t uid); //!< Find a unit by underlying id. Null pointer if not found. 50 unit_ptr extract_if_matches_underlying_id(size_t uid); //!< Find a unit by underlying id, and extract if found. Null if not found. 51 unit_const_ptr find_if_matches_underlying_id(size_t uid) const; //!< Const find by underlying id. 52 void erase_by_underlying_id(size_t uid); //!< Erase any unit with this underlying id. 53 54 iterator erase_index(size_t index); //!< Erase by index. 55 iterator erase(iterator it); //!< Erase an iterator to this object. 56 57 size_t find_index(const std::string & unit_id) const; //!< Find the index of a unit by its id. size() const58 size_t size() const { return recall_list_.size(); } //!< Get the number of units on the list. empty() const59 bool empty() const { return recall_list_.empty(); } //!< Is it empty? 60 61 /// Add a unit to the list. 62 /// @a pos the location where to insert the unit, -1 for 'at end' 63 void add(const unit_ptr & ptr, int pos = -1); 64 65 private: 66 std::vector<unit_ptr > recall_list_; //!< The underlying data struture. TODO: Should this be a map based on underlying id instead? 67 68 friend class ai::readonly_context_impl; //!< Friend AI module for ease of implementation there. 69 }; 70