Split util.hpp into utils/math.hpp and utils/general.hpp
This commit is contained in:
parent
fb9831cfe5
commit
f90e466360
26 changed files with 104 additions and 75 deletions
|
@ -1105,17 +1105,18 @@
|
|||
<Unit filename="../../src/units/udisplay.hpp" />
|
||||
<Unit filename="../../src/units/unit.cpp" />
|
||||
<Unit filename="../../src/units/unit.hpp" />
|
||||
<Unit filename="../../src/util.hpp" />
|
||||
<Unit filename="../../src/utils/const_clone.hpp" />
|
||||
<Unit filename="../../src/utils/context_free_grammar_generator.cpp" />
|
||||
<Unit filename="../../src/utils/context_free_grammar_generator.hpp" />
|
||||
<Unit filename="../../src/utils/functional.hpp" />
|
||||
<Unit filename="../../src/utils/general.hpp" />
|
||||
<Unit filename="../../src/utils/iterable_pair.hpp" />
|
||||
<Unit filename="../../src/utils/iterator.hpp" />
|
||||
<Unit filename="../../src/utils/make_enum.cpp" />
|
||||
<Unit filename="../../src/utils/make_enum.hpp" />
|
||||
<Unit filename="../../src/utils/markov_generator.cpp" />
|
||||
<Unit filename="../../src/utils/markov_generator.hpp" />
|
||||
<Unit filename="../../src/utils/math.hpp" />
|
||||
<Unit filename="../../src/utils/name_generator.hpp" />
|
||||
<Unit filename="../../src/utils/name_generator_factory.cpp" />
|
||||
<Unit filename="../../src/utils/name_generator_factory.hpp" />
|
||||
|
|
|
@ -42,6 +42,7 @@
|
|||
#include "game_config.hpp"
|
||||
#include "preferences.hpp"
|
||||
#include "random_new.hpp"
|
||||
#include "utils/general.hpp"
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <numeric>
|
||||
|
|
|
@ -20,9 +20,10 @@
|
|||
#ifndef BUFFERED_ISTREAM_HPP_INCLUDED
|
||||
#define BUFFERED_ISTREAM_HPP_INCLUDED
|
||||
|
||||
#include "util.hpp"
|
||||
#include "utils/general.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <sstream>
|
||||
|
||||
/**
|
||||
* Helper class for buffering a @c std::istream.
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include "scripting/plugins/manager.hpp"
|
||||
#include "sdl/surface.hpp"
|
||||
#include "sdl/rect.hpp"
|
||||
#include "utils/general.hpp"
|
||||
|
||||
#include "utils/functional.hpp"
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
#include "formatter.hpp"
|
||||
#include "formula/callable.hpp"
|
||||
#include "formula/function.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/math.hpp"
|
||||
|
||||
namespace {
|
||||
std::string variant_type_to_string(variant::TYPE type) {
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include "config.hpp"
|
||||
#include "gettext.hpp"
|
||||
#include "log.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/general.hpp"
|
||||
#include "version.hpp"
|
||||
#include "wesconfig.h"
|
||||
#include "serialization/string_utils.hpp"
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include "formula/string_utils.hpp"
|
||||
#include "gettext.hpp"
|
||||
#include "log.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/general.hpp"
|
||||
|
||||
#include "gui/dialogs/depcheck_confirm_change.hpp"
|
||||
#include "gui/dialogs/depcheck_select_new.hpp"
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include "map/map.hpp"
|
||||
#include "map/exception.hpp"
|
||||
#include "terrain/type_data.hpp"
|
||||
#include "utils/general.hpp"
|
||||
#include "wml_exception.hpp"
|
||||
#include "version.hpp"
|
||||
|
||||
|
|
|
@ -48,6 +48,7 @@
|
|||
#include "formula/string_utils.hpp"
|
||||
#include "preferences.hpp"
|
||||
#include "strftime.hpp"
|
||||
#include "utils/general.hpp"
|
||||
|
||||
#include "config.hpp"
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
#include "image.hpp"
|
||||
#include "language.hpp"
|
||||
#include "serialization/string_utils.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/general.hpp"
|
||||
|
||||
#include <cctype>
|
||||
#include "utils/functional.hpp"
|
||||
|
|
|
@ -43,6 +43,7 @@
|
|||
#include "units/types.hpp"
|
||||
#include "units/unit.hpp"
|
||||
#include "units/ptr.hpp"
|
||||
#include "utils/general.hpp"
|
||||
|
||||
#include "utils/functional.hpp"
|
||||
#include <boost/dynamic_bitset.hpp>
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
#include "gui/dialogs/advanced_graphics_options.hpp"
|
||||
#include "preferences.hpp"
|
||||
#include "sdl/rect.hpp"
|
||||
#include "utils/general.hpp"
|
||||
|
||||
#ifdef HAVE_LIBPNG
|
||||
#include "SDL_SavePNG/savepng.h"
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
#include "preferences.hpp"
|
||||
#include "log.hpp"
|
||||
#include "sdl/surface.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/math.hpp"
|
||||
#include <boost/math/constants/constants.hpp>
|
||||
using namespace boost::math::constants;
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
#include "formula/string_utils.hpp"
|
||||
#include "gettext.hpp"
|
||||
#include "log.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/math.hpp"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
#include "sound.hpp"
|
||||
#include "video.hpp" // non_interactive()
|
||||
#include "serialization/parser.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/general.hpp"
|
||||
|
||||
#include <sys/stat.h> // for setting the permissions of the preferences file
|
||||
#ifndef _WIN32
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#include "color_range.hpp"
|
||||
#include "color.hpp"
|
||||
#include "sdl/surface.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/math.hpp"
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include "log.hpp"
|
||||
#include "serialization/string_utils.hpp"
|
||||
#include "serialization/unicode.hpp"
|
||||
#include "utils/general.hpp"
|
||||
#include <cassert>
|
||||
#include <array>
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
#define SERIALIZATION_UCS4_CONVERT_IMPL_HPP_INCLUDED
|
||||
|
||||
#include "unicode_types.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/math.hpp"
|
||||
#include <cassert>
|
||||
|
||||
namespace ucs4_convert_impl
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
#include "settings.hpp"
|
||||
|
||||
#include "serialization/string_utils.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/general.hpp"
|
||||
|
||||
namespace settings {
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
#include "config.hpp"
|
||||
#include "terrain/translation.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/math.hpp"
|
||||
|
||||
class terrain_type
|
||||
{
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include "util.hpp"
|
||||
#include "utils/math.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#include "global.hpp"
|
||||
|
||||
#include "tstring.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/general.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#include "movetype.hpp"
|
||||
#include "units/race.hpp"
|
||||
#include "units/attack_type.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/math.hpp"
|
||||
#include "game_errors.hpp"
|
||||
|
||||
#include <map>
|
||||
|
|
76
src/utils/general.hpp
Normal file
76
src/utils/general.hpp
Normal file
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
Copyright (C) 2003 - 2016 the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program 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; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#ifndef GENERAL_UTILS_INCLUDED
|
||||
#define GENERAL_UTILS_INCLUDED
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define LIKELY(a) __builtin_expect((a),1) // Tells GCC to optimize code so that if is likely to happen
|
||||
#define UNLIKELY(a) __builtin_expect((a),0) // Tells GCC to optimize code so that if is unlikely to happen
|
||||
#else
|
||||
#define LIKELY(a) a
|
||||
#define UNLIKELY(a) a
|
||||
#endif
|
||||
|
||||
inline bool chars_equal_insensitive(char a, char b) { return tolower(a) == tolower(b); }
|
||||
inline bool chars_less_insensitive(char a, char b) { return tolower(a) < tolower(b); }
|
||||
|
||||
namespace util {
|
||||
|
||||
// NOTE: remove once we have C++17 support and can use std::clamp
|
||||
template<typename T>
|
||||
CONSTEXPR const T& clamp(const T& value, const T& min, const T& max)
|
||||
{
|
||||
return std::max<T>(std::min<T>(value, max), min);
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
/// A struct that exists to implement a generic wrapper for std::find.
|
||||
/// Container should "look like" an STL container of Values.
|
||||
template<typename Container, typename Value> struct contains_impl {
|
||||
static bool eval(const Container & container, const Value & value)
|
||||
{
|
||||
typename Container::const_iterator end = container.end();
|
||||
return std::find(container.begin(), end, value) != end;
|
||||
}
|
||||
};
|
||||
/// A struct that exists to implement a generic wrapper for the find()
|
||||
/// member of associative containers.
|
||||
/// Container should "look like" an STL associative container.
|
||||
template<typename Container>
|
||||
struct contains_impl<Container, typename Container::key_type> {
|
||||
static bool eval(const Container & container,
|
||||
const typename Container::key_type & value)
|
||||
{
|
||||
return container.find(value) != container.end();
|
||||
}
|
||||
};
|
||||
}//namespace detail
|
||||
|
||||
/// Returns true iff @a value is found in @a container.
|
||||
/// This should work whenever Container "looks like" an STL container of Values.
|
||||
/// Normally this uses std::find(), but a simulated partial template specialization
|
||||
/// exists when Value is Container::key_type. In this case, Container is assumed
|
||||
/// an associative container, and the member function find() is used.
|
||||
template<typename Container, typename Value>
|
||||
inline bool contains(const Container & container, const Value & value)
|
||||
{
|
||||
return detail::contains_impl<Container, Value>::eval(container, value);
|
||||
}
|
||||
|
||||
}//namespace util
|
||||
|
||||
#endif
|
|
@ -14,20 +14,20 @@
|
|||
|
||||
/**
|
||||
* @file
|
||||
* Templates and utility-routines for strings and numbers.
|
||||
* General math utility functions.
|
||||
*/
|
||||
|
||||
#ifndef UTIL_H_INCLUDED
|
||||
#define UTIL_H_INCLUDED
|
||||
|
||||
#include "global.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <math.h> // cmath may not provide round()
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
template<typename T>
|
||||
|
@ -89,9 +89,6 @@ bool in_ranges(const Cmp c, const std::vector<std::pair<Cmp, Cmp> >&ranges) {
|
|||
return false;
|
||||
}
|
||||
|
||||
inline bool chars_equal_insensitive(char a, char b) { return tolower(a) == tolower(b); }
|
||||
inline bool chars_less_insensitive(char a, char b) { return tolower(a) < tolower(b); }
|
||||
|
||||
/**
|
||||
* Returns the size, in bits, of an instance of type `T`, providing a
|
||||
* convenient and self-documenting name for the underlying expression:
|
||||
|
@ -304,60 +301,6 @@ inline unsigned int count_leading_ones(N n) {
|
|||
return count_leading_zeros<N>(~n);
|
||||
}
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define LIKELY(a) __builtin_expect((a),1) // Tells GCC to optimize code so that if is likely to happen
|
||||
#define UNLIKELY(a) __builtin_expect((a),0) // Tells GCC to optimize code so that if is unlikely to happen
|
||||
#else
|
||||
#define LIKELY(a) a
|
||||
#define UNLIKELY(a) a
|
||||
#endif
|
||||
|
||||
namespace util {
|
||||
|
||||
// NOTE: remove once we have C++17 support and can use std::clamp
|
||||
template<typename T>
|
||||
CONSTEXPR const T& clamp(const T& value, const T& min, const T& max)
|
||||
{
|
||||
return std::max<T>(std::min<T>(value, max), min);
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
/// A struct that exists to implement a generic wrapper for std::find.
|
||||
/// Container should "look like" an STL container of Values.
|
||||
template<typename Container, typename Value> struct contains_impl {
|
||||
static bool eval(const Container & container, const Value & value)
|
||||
{
|
||||
typename Container::const_iterator end = container.end();
|
||||
return std::find(container.begin(), end, value) != end;
|
||||
}
|
||||
};
|
||||
/// A struct that exists to implement a generic wrapper for the find()
|
||||
/// member of associative containers.
|
||||
/// Container should "look like" an STL associative container.
|
||||
template<typename Container>
|
||||
struct contains_impl<Container, typename Container::key_type> {
|
||||
static bool eval(const Container & container,
|
||||
const typename Container::key_type & value)
|
||||
{
|
||||
return container.find(value) != container.end();
|
||||
}
|
||||
};
|
||||
}//namespace detail
|
||||
|
||||
/// Returns true iff @a value is found in @a container.
|
||||
/// This should work whenever Container "looks like" an STL container of Values.
|
||||
/// Normally this uses std::find(), but a simulated partial template specialization
|
||||
/// exists when Value is Container::key_type. In this case, Container is assumed
|
||||
/// an associative container, and the member function find() is used.
|
||||
template<typename Container, typename Value>
|
||||
inline bool contains(const Container & container, const Value & value)
|
||||
{
|
||||
return detail::contains_impl<Container, Value>::eval(container, value);
|
||||
}
|
||||
|
||||
}//namespace util
|
||||
|
||||
|
||||
#if 1
|
||||
typedef int32_t fixed_t;
|
||||
# define fxp_shift 8
|
|
@ -26,6 +26,7 @@
|
|||
#include "font/marked-up_text.hpp"
|
||||
#include "sdl/rect.hpp"
|
||||
#include "sound.hpp"
|
||||
#include "utils/general.hpp"
|
||||
#include "video.hpp"
|
||||
#include "wml_separators.hpp"
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue