32#include "NarrowCast.h"
50 template<
typename T>
inline constexpr T
Square( T val )
noexcept{
56 template<
typename T>
inline constexpr T
Cube( T val )
noexcept{
57 return val * val * val;
61 template<
int Y,
typename T>
inline constexpr T
pow( T val )
noexcept{
62 return static_cast<T
>(std::pow(val,Y));
66 template<
typename T>
inline constexpr bool Equals( T a, T b, T epsilon )
noexcept{
68 return abs( a - b ) <= epsilon;
72 template <
typename T>
inline constexpr auto Sign(T val)
noexcept ->
decltype(T{}/T{}){
73 return static_cast<decltype(T{}/T{})
>((T{0} < val) - (val < T{0}));
79 return (n == 1 || n == 0) ? 1 :
Factorial(n - 1) * n;
87 template<
typename T1,
typename T2>
inline
88 T1
Clip( T1& val,
const T2& min,
const T2& max )
noexcept{
90 return Clip( val, max, min );
93 val =
static_cast<T1
>(min);
95 val =
static_cast<T1
>(max);
105 constexpr const T&
Clamp(
const T& v,
const T& lo,
const T& hi )
noexcept{
106 if( v < lo )
return lo;
107 if( hi < v )
return hi;
117 template<
typename T1,
typename T2>
inline
118 T1
Wrap(
const T1& val,
const T2& min,
const T2& max )
noexcept{
123 return Wrap( val, max, min );
127 retVal =
static_cast<T1
>(std::fmod(min + (val - max), max - min));
129 retVal =
static_cast<T1
>(std::fmod(max - (min - val), max - min ));
139 template<
class T>
inline
140 T
Round( T value,
int toDigit )
noexcept
142 const float pow10{ std::pow( 10.f,
static_cast<float>(toDigit) ) };
143 return round( value * pow10 ) / pow10;
150 template<
typename Valtype>
inline
152 if( abs( val ) < (std::numeric_limits<Valtype>::min)() )
161 template<
typename Valtype>
162 inline constexpr Valtype
FirstPearl(
int cntPearls, Valtype distPearls )
noexcept{
163 return ( 1 - cntPearls ) * 0.5f * distPearls;
166 inline char to_uppercase(
unsigned char c)
171 inline char to_lowercase(
unsigned char c)
177 inline std::string
quoted(
const std::string& str ){
178 std::string quotedStr =
"\"";
188 std::string _str(str);
189 if(_str.size() >= 2 ){
190 if( (*_str.cbegin() ==
'\"' && *_str.crbegin() ==
'\"') ||
191 (*_str.cbegin() ==
'\'' && *_str.crbegin() ==
'\'') ){
192 _str.erase( _str.begin() );
193 _str.erase( _str.end()-1 );
203 for( std::size_t i = 0; i < str.size(); ++i ){
204 const char& c = str.at(i);
227 for(
int i = indent; i != 0; --i )
263 template<
typename Valtype>
inline
266 const Valtype dvalue = -dattenuation * value;
267 if( std::abs(value) < std::abs(dvalue) )
270 return value + dvalue;
276 std::set<T> m_OccupiedKeys;
281 return m_OccupiedKeys.insert( key ).second;
287 return m_OccupiedKeys.erase( key ) ? true :
false;
293 m_OccupiedKeys.clear();
300 for(
auto csiter = m_OccupiedKeys.begin();
301 csiter != m_OccupiedKeys.end(); ++x, ++csiter )
306 m_OccupiedKeys.insert( x );
310 m_OccupiedKeys.insert( x );
317 return (m_OccupiedKeys.find( key ) == m_OccupiedKeys.end());
329 template<
typename Valtype>
330 constexpr Valtype
Collision( Valtype m1, Valtype m2, Valtype v1, Valtype v2, Valtype k ){
331 return m1*m2/ (m1+m2) * (v2-v1) * (1+k);
334 inline void TranslateEscapeCharacters( std::string&
string ){
335 for( std::size_t pos =
string.find(
"[e]" ); pos !=
string.npos; pos =
string.find(
"[e]", pos ) )
336 string.replace( pos, 3,
"\n" );
340 inline std::string
utf8_to_string(
const std::string& utf8str,
const std::locale& loc = std::locale(
"") )
347# pragma warning(push)
348# pragma warning(disable: 4996)
350 std::wstring_convert<std::codecvt_utf8<wchar_t>> wconv;
351 std::wstring wstr = wconv.from_bytes(utf8str.c_str());
356 std::vector<char> buf(wstr.size());
357 std::use_facet<std::ctype<wchar_t>>(loc).narrow(wstr.data(), wstr.data() + wstr.size(),
'?', buf.data());
358 return std::string(buf.data(), buf.size());
368 FlagBlocker(
bool& blocker ) noexcept
373 FlagBlocker() =
delete;
374 FlagBlocker(
const FlagBlocker& ) =
delete;
375 FlagBlocker( FlagBlocker&& ) =
delete;
378 ~FlagBlocker()
noexcept{
382 FlagBlocker& operator=(
const FlagBlocker& ) =
delete;
383 FlagBlocker& operator=( FlagBlocker&& ) =
delete;
388 template<
typename ValueType>
390 ValueType& m_Variable;
391 ValueType m_OriginalValue;
392 bool m_bDismiss =
false;
395 Resetter(
const Resetter& ) =
delete;
396 Resetter( Resetter&& ) =
delete;
397 Resetter( ValueType& variable, ValueType toValue ) noexcept
398 : m_Variable { variable },
399 m_OriginalValue { variable }
401 m_Variable = toValue;
406 m_Variable = m_OriginalValue;
409 Resetter& operator=(
const Resetter& ) =
delete;
410 Resetter& operator=( Resetter&& ) =
delete;
420 template <
class ContainerAdapter>
423 typedef typename ContainerAdapter::container_type container_type;
425 const container_type& get_container()
const noexcept {
return this->c; }
stl container adapter extension for accessing the underlying container.
Definition Helpers.h:421
Set of unique keys.
Definition Helpers.h:275
bool IsFree(T key) const
Is the key already occupied?
Definition Helpers.h:316
bool Occupy(T key)
Reserve a key.
Definition Helpers.h:280
T GetFree()
Get a free key. The key will be occupied after the call.
Definition Helpers.h:298
bool Free(T key)
Make a key available in the future.
Definition Helpers.h:286
void FreeAll() noexcept
Release all the keys, making them available.
Definition Helpers.h:292
Namespace of common utility classes and methods.
Definition Helpers.h:43
constexpr Valtype Collision(Valtype m1, Valtype m2, Valtype v1, Valtype v2, Valtype k)
Calculates the impulse change of body 1 in a collision event.
Definition Helpers.h:330
constexpr T Cube(T val) noexcept
Definition Helpers.h:56
T1 Wrap(const T1 &val, const T2 &min, const T2 &max) noexcept
Wraps a val to a specified range as if max would be actually min.
Definition Helpers.h:118
constexpr T pow(T val) noexcept
power function with templated integer exponent.
Definition Helpers.h:61
constexpr T Square(T val) noexcept
This is usefull because it saves a temp.
Definition Helpers.h:50
std::string StripReservedCharacters(const std::string &str)
Removes characters that might not be allowed for file names.
Definition Helpers.h:201
constexpr auto Sign(T val) noexcept -> decltype(T{}/T{})
Definition Helpers.h:72
Valtype DealDenormalizedNumbers(Valtype val)
Make too small numbers zero.
Definition Helpers.h:151
constexpr int Factorial(int n)
Definition Helpers.h:77
T Round(T value, int toDigit) noexcept
Rounding of floating point number to a certain digit aftzer the point.
Definition Helpers.h:140
constexpr const T & Clamp(const T &v, const T &lo, const T &hi) noexcept
Clips a val to a specified range.
Definition Helpers.h:105
std::string StripApostrophes(const std::string &str)
Removes the outmost apostrophes (''' and '"') from a string. Does nothing if the string isn't properl...
Definition Helpers.h:187
constexpr bool Equals(T a, T b, T epsilon) noexcept
Tests equality in the sense |a-b| < epsilon.
Definition Helpers.h:66
T1 Clip(T1 &val, const T2 &min, const T2 &max) noexcept
Clips a val to a specified range.
Definition Helpers.h:88
std::string quoted(const std::string &str)
Construct a quoted string.
Definition Helpers.h:177
Target narrow_cast(Source v)
Safe cast for casting numeric values.
Definition NarrowCast.h:60
constexpr Valtype FirstPearl(int cntPearls, Valtype distPearls) noexcept
Center an equidistant row of elements (pearls).
Definition Helpers.h:162
Valtype AttenuateValue(Valtype value, Valtype dattenuation) noexcept
Attenuates value by the factor dattenuation.
Definition Helpers.h:264
std::string utf8_to_string(const std::string &utf8str, const std::locale &loc=std::locale(""))
convert multibyte character string (UTF-8) to ANSI string.
Definition Helpers.h:340
std::string Indent(int indent)
Needed since for unknown reason, the straightforward implementation std::string{ indent,...
Definition Helpers.h:224