The Battle for Wesnoth  1.15.1+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 "global.hpp"
17 
18 #include <algorithm>
19 #include <cctype>
20 
21 inline bool chars_equal_insensitive(char a, char b) { return tolower(a) == tolower(b); }
22 inline bool chars_less_insensitive(char a, char b) { return tolower(a) < tolower(b); }
23 
24 namespace utils {
25 
26 #ifdef HAVE_CXX17
27 using std::clamp;
28 #else
29 // NOTE: remove once we have C++17 support and can use std::clamp
30 template<typename T>
31 constexpr const T& clamp(const T& value, const T& min, const T& max)
32 {
33  return std::max<T>(std::min<T>(value, max), min);
34 }
35 #endif
36 
37 namespace detail
38 {
39 /**
40  * A struct that exists to implement a generic wrapper for std::find.
41  * Container should "look like" an STL container of Values.
42  */
43 template<typename Container, typename Value>
45 {
46  static bool eval(const Container& container, const Value& value)
47  {
48  typename Container::const_iterator end = container.end();
49  return std::find(container.begin(), end, value) != end;
50  }
51 };
52 
53 /**
54  * A struct that exists to implement a generic wrapper for the find()
55  * member of associative containers.
56  * Container should "look like" an STL associative container.
57  */
58 template<typename Container>
59 struct contains_impl<Container, typename Container::key_type>
60 {
61  static bool eval(const Container& container, const typename Container::key_type& value)
62  {
63  return container.find(value) != container.end();
64  }
65 };
66 
67 } // namespace detail
68 
69 /**
70  * Returns true iff @a value is found in @a container.
71  *
72  * This should work whenever Container "looks like" an STL container of Values.
73  * Normally this uses std::find(), but a simulated partial template specialization
74  * exists when Value is Container::key_type. In this case, Container is assumed
75  * an associative container, and the member function find() is used.
76  */
77 template<typename Container, typename Value>
78 inline bool contains(const Container& container, const Value& value)
79 {
80  return detail::contains_impl<Container, Value>::eval(container, value);
81 }
82 
83 } // namespace utils
constexpr const T & clamp(const T &value, const T &min, const T &max)
Definition: general.hpp:31
static bool eval(const Container &container, const typename Container::key_type &value)
Definition: general.hpp:61
#define a
bool chars_equal_insensitive(char a, char b)
Definition: general.hpp:21
bool chars_less_insensitive(char a, char b)
Definition: general.hpp:22
Definition: lobject.h:100
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:78
#define b
static bool eval(const Container &container, const Value &value)
Definition: general.hpp:46
A struct that exists to implement a generic wrapper for std::find.
Definition: general.hpp:44
bool find(E event, F functor)
Tests whether an event handler is available.