The Battle for Wesnoth  1.15.12+dev
general.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include <algorithm>
17 #include <cctype>
18 #include <functional>
19 
20 namespace utils
21 {
22 inline bool chars_equal_insensitive(char a, char b) { return tolower(a) == tolower(b); }
23 inline bool chars_less_insensitive(char a, char b) { return tolower(a) < tolower(b); }
24 
25 /**
26  * Equivalent to as @c std::is_same_v except it uses the decayed form of V.
27  *
28  * @tparam T1 The first type to compare.
29  * @tparam T2 The second type to compare. This will be passed through @c std::decay .
30  */
31 template<typename T1, typename T2>
32 inline constexpr bool decayed_is_same = std::is_same_v<T1, std::decay_t<T2>>;
33 
34 namespace detail
35 {
36 /**
37  * A struct that exists to implement a generic wrapper for std::find.
38  * Container should "look like" an STL container of Values.
39  */
40 template<typename Container, typename Value>
42 {
43  static bool eval(const Container& container, const Value& value)
44  {
45  typename Container::const_iterator end = container.end();
46  return std::find(container.begin(), end, value) != end;
47  }
48 };
49 
50 /**
51  * A struct that exists to implement a generic wrapper for the find()
52  * member of associative containers.
53  * Container should "look like" an STL associative container.
54  */
55 template<typename Container>
56 struct contains_impl<Container, typename Container::key_type>
57 {
58  static bool eval(const Container& container, const typename Container::key_type& value)
59  {
60  return container.find(value) != container.end();
61  }
62 };
63 
64 } // namespace detail
65 
66 /**
67  * Returns true iff @a value is found in @a container.
68  *
69  * This should work whenever Container "looks like" an STL container of Values.
70  * Normally this uses std::find(), but a simulated partial template specialization
71  * exists when Value is Container::key_type. In this case, Container is assumed
72  * an associative container, and the member function find() is used.
73  */
74 template<typename Container, typename Value>
75 inline bool contains(const Container& container, const Value& value)
76 {
77  return detail::contains_impl<Container, Value>::eval(container, value);
78 }
79 
80 } // namespace utils
static bool eval(const Container &container, const typename Container::key_type &value)
Definition: general.hpp:58
#define a
bool chars_equal_insensitive(char a, char b)
Definition: general.hpp:22
Definition: lobject.h:49
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:75
#define b
constexpr bool decayed_is_same
Equivalent to as std::is_same_v except it uses the decayed form of V.
Definition: general.hpp:32
static bool eval(const Container &container, const Value &value)
Definition: general.hpp:43
A struct that exists to implement a generic wrapper for std::find.
Definition: general.hpp:41
bool chars_less_insensitive(char a, char b)
Definition: general.hpp:23