Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / core / container_func.hpp
blob5a713649aafe32bc4f8775b613cc4ec6908a3c05
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file container_func.hpp Some simple functions to help with accessing containers. */
10 #ifndef CONTAINER_FUNC_HPP
11 #define CONTAINER_FUNC_HPP
13 /**
14 * Helper function to append an item to a container if it is not already contained.
15 * The container must have a \c emplace_back function.
16 * Consider using std::set, std::unordered_set or std::flat_set in new code.
18 * @param container A reference to the container to be extended
19 * @param item Reference to the item to be copy-constructed if not found
21 * @return Whether the item was already present
23 template <typename Container>
24 inline bool include(Container &container, typename Container::const_reference &item)
26 const bool is_member = std::find(container.begin(), container.end(), item) != container.end();
27 if (!is_member) container.emplace_back(item);
28 return is_member;
31 /**
32 * Helper function to get the index of an item
33 * Consider using std::set, std::unordered_set or std::flat_set in new code.
35 * @param container A reference to the container to be searched.
36 * @param item Reference to the item to be search for
38 * @return Index of element if found, otherwise -1
40 template <typename Container>
41 int find_index(Container const &container, typename Container::const_reference item)
43 auto const it = std::find(container.begin(), container.end(), item);
44 if (it != container.end()) return std::distance(container.begin(), it);
46 return -1;
49 #endif /* CONTAINER_FUNC_HPP */