The Battle for Wesnoth  1.17.0-dev
general.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 <algorithm>
18 #include <cctype>
19 #include <functional>
20 
21 namespace utils
22 {
23 inline bool chars_equal_insensitive(char a, char b) { return tolower(a) == tolower(b); }
24 inline bool chars_less_insensitive(char a, char b) { return tolower(a) < tolower(b); }
25 
26 /**
27  * Equivalent to as @c std::is_same_v except both types are passed throgh std::decay first.
28  *
29  * @tparam T1 The first type to compare.
30  * @tparam T2 The second type to compare.
31  */
32 template<typename T1, typename T2>
33 inline constexpr bool decayed_is_same = std::is_same_v<std::decay_t<T1>, std::decay_t<T2>>;
34 
35 namespace detail
36 {
37 /**
38  * A struct that exists to implement a generic wrapper for std::find.
39  * Container should "look like" an STL container of Values.
40  */
41 template<typename Container, typename Value>
43 {
44  static bool eval(const Container& container, const Value& value)
45  {
46  typename Container::const_iterator end = container.end();
47  return std::find(container.begin(), end, value) != end;
48  }
49 };
50 
51 /**
52  * A struct that exists to implement a generic wrapper for the find()
53  * member of associative containers.
54  * Container should "look like" an STL associative container.
55  */
56 template<typename Container>
57 struct contains_impl<Container, typename Container::key_type>
58 {
59  static bool eval(const Container& container, const typename Container::key_type& value)
60  {
61  return container.find(value) != container.end();
62  }
63 };
64 
65 } // namespace detail
66 
67 /**
68  * Returns true iff @a value is found in @a container.
69  *
70  * This should work whenever Container "looks like" an STL container of Values.
71  * Normally this uses std::find(), but a simulated partial template specialization
72  * exists when Value is Container::key_type. In this case, Container is assumed
73  * an associative container, and the member function find() is used.
74  */
75 template<typename Container, typename Value>
76 inline bool contains(const Container& container, const Value& value)
77 {
78  return detail::contains_impl<Container, Value>::eval(container, value);
79 }
80 
81 } // namespace utils
static bool eval(const Container &container, const typename Container::key_type &value)
Definition: general.hpp:59
#define a
bool chars_equal_insensitive(char a, char b)
Definition: general.hpp:23
Definition: lobject.h:49
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:76
#define b
constexpr bool decayed_is_same
Equivalent to as std::is_same_v except both types are passed throgh std::decay first.
Definition: general.hpp:33
static bool eval(const Container &container, const Value &value)
Definition: general.hpp:44
A struct that exists to implement a generic wrapper for std::find.
Definition: general.hpp:42
bool chars_less_insensitive(char a, char b)
Definition: general.hpp:24