The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
recall_list_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 static bool find_if_matches_helper(const unit_ptr & ptr, const std::string & unit_id)
26 {
27  return ptr->matches_id(unit_id);
28 }
29 
30 /**
31  * Used to find units in vectors by their ID.
32  */
34 {
35  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
36  std::bind(&find_if_matches_helper, _1, unit_id));
37  if (it != recall_list_.end()) {
38  return *it;
39  } else {
40  return unit_ptr();
41  }
42 }
43 
44 /**
45  * Used to find units in vectors by their ID.
46  */
48 {
49  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
50  std::bind(&find_if_matches_helper, _1, unit_id));
51  if (it != recall_list_.end()) {
52  return *it;
53  } else {
54  return unit_ptr();
55  }
56 }
57 
58 /**
59  * Used to erase units from vectors by their ID.
60  */
62 {
63  recall_list_.erase(std::remove_if(recall_list_.begin(), recall_list_.end(),
64  std::bind(&find_if_matches_helper, _1, unit_id)),
65  recall_list_.end());
66 }
67 
69 {
70  recall_list_.push_back(ptr);
71 }
72 
73 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  std::bind(&find_if_matches_helper, _1, unit_id));
77 
78  return it - recall_list_.begin();
79 }
80 
82 {
83  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
84  std::bind(&find_if_matches_helper, _1, unit_id));
85  if (it != recall_list_.end()) {
86  unit_ptr ret = *it;
87  recall_list_.erase(it);
88  return ret;
89  } else {
90  return unit_ptr();
91  }
92 }
93 
94 static bool find_if_matches_uid_helper(const unit_ptr & ptr, size_t uid)
95 {
96  return ptr->underlying_id() == uid;
97 }
98 
100 {
101  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
102  std::bind(&find_if_matches_uid_helper, _1, uid));
103  if (it != recall_list_.end()) {
104  return *it;
105  } else {
106  return unit_ptr();
107  }
108 }
109 
111 {
112  std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
113  std::bind(&find_if_matches_uid_helper, _1, uid));
114  if (it != recall_list_.end()) {
115  return *it;
116  } else {
117  return unit_ptr();
118  }
119 }
120 
122 {
123  recall_list_.erase(std::remove_if(recall_list_.begin(), recall_list_.end(),
124  std::bind(&find_if_matches_uid_helper, _1, uid)),
125  recall_list_.end());
126 }
127 
129 {
130  std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
131  std::bind(&find_if_matches_uid_helper, _1, uid));
132  if (it != recall_list_.end()) {
133  unit_ptr ret = *it;
134  recall_list_.erase(it);
135  return ret;
136  } else {
137  return unit_ptr();
138  }
139 }
140 
142  assert(idx < recall_list_.size());
143  return recall_list_.erase(recall_list_.begin()+idx);
144 }
145 
147  return recall_list_.erase(it);
148 }
static bool find_if_matches_helper(const unit_ptr &ptr, const std::string &unit_id)
unit_ptr find_if_matches_underlying_id(size_t uid)
Find a unit by underlying id. Null pointer if not found.
unit_ptr extract_if_matches_id(const std::string &unit_id)
Find a unit by id, and extract from this object if found. Null if not found.
unit_ptr extract_if_matches_underlying_id(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. Null pointer if not found.
static bool find_if_matches_uid_helper(const unit_ptr &ptr, size_t uid)
void erase_if_matches_id(const std::string &unit_id)
Erase any unit with this id.
iterator erase_index(size_t index)
Erase by index.
void erase_by_underlying_id(size_t uid)
Erase any unit with this underlying id.
iterator erase(iterator it)
Erase an iterator to this object.
size_t find_index(const std::string &unit_id) const
Find the index of a unit by its id.
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
void add(const unit_ptr &ptr)
Add a unit to the list.
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::vector< unit_ptr > recall_list_
The underlying data struture. TODO: Should this be a map based on underlying id instead?