The Battle for Wesnoth  1.15.1+dev
recall_list_manager.cpp
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 #include "recall_list_manager.hpp"
16 #include "units/unit.hpp"
17 #include "units/ptr.hpp"
18 
19 #include <algorithm>
20 #include <string>
21 #include <vector>
22 
23 #include "utils/functional.hpp"
24 
25 /**
26  * Used to find units in vectors by their ID.
27  */
29 {
30  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
31  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
32  if (it != recall_list_.end()) {
33  return *it;
34  } else {
35  return unit_ptr();
36  }
37 }
38 
39 /**
40  * Used to find units in vectors by their ID.
41  */
43 {
44  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
45  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
46  if (it != recall_list_.end()) {
47  return *it;
48  } else {
49  return unit_ptr();
50  }
51 }
52 
53 /**
54  * Used to erase units from vectors by their ID.
55  */
56 void recall_list_manager::erase_if_matches_id(const std::string &unit_id)
57 {
58  recall_list_.erase(std::remove_if(recall_list_.begin(), recall_list_.end(),
59  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; }),
60  recall_list_.end());
61 }
62 
63 void recall_list_manager::add(const unit_ptr & ptr, int pos)
64 {
65  if (pos < 0 || pos >= static_cast<int>(recall_list_.size())) {
66  recall_list_.push_back(ptr);
67  }
68  else {
69  recall_list_.insert(recall_list_.begin() + pos, ptr);
70  }
71 }
72 
73 std::size_t recall_list_manager::find_index(const std::string & unit_id) const
74 {
75  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
76  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
77 
78  return std::distance(recall_list_.begin(), it);
79 }
80 
81 unit_ptr recall_list_manager::extract_if_matches_id(const std::string &unit_id, int * pos)
82 {
83  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
84  [&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });
85  if (it != recall_list_.end()) {
86  unit_ptr ret = *it;
87  if(pos) {
88  *pos = it - recall_list_.begin();
89  }
90  recall_list_.erase(it);
91  return ret;
92  } else {
93  return unit_ptr();
94  }
95 }
96 
98 {
99  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
100  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; });
101  if (it != recall_list_.end()) {
102  return *it;
103  } else {
104  return unit_ptr();
105  }
106 }
107 
109 {
110  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
111  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; });
112  if (it != recall_list_.end()) {
113  return *it;
114  } else {
115  return unit_ptr();
116  }
117 }
118 
120 {
121  recall_list_.erase(std::remove_if(recall_list_.begin(), recall_list_.end(),
122  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; }),
123  recall_list_.end());
124 }
125 
127 {
128  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
129  [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; });
130  if (it != recall_list_.end()) {
131  unit_ptr ret = *it;
132  recall_list_.erase(it);
133  return ret;
134  } else {
135  return unit_ptr();
136  }
137 }
138 
140  assert(idx < recall_list_.size());
141  return recall_list_.erase(recall_list_.begin()+idx);
142 }
143 
145  return recall_list_.erase(it);
146 }
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
unit_ptr extract_if_matches_underlying_id(std::size_t uid)
Find a unit by underlying id, and extract if found. Null if not found.
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_ptr find_if_matches_underlying_id(std::size_t uid)
Find a unit by underlying id. Null pointer if not found.
void erase_if_matches_id(const std::string &unit_id)
Erase any unit with this id.
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.
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
void erase_by_underlying_id(std::size_t uid)
Erase any unit with this underlying id.
void add(const unit_ptr &ptr, int pos=-1)
Add a unit to the list.
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
std::vector< unit_ptr > recall_list_
The underlying data struture. TODO: Should this be a map based on underlying id instead?
std::size_t find_index(const std::string &unit_id) const
Find the index of a unit by its id.