forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_types.h
50 lines (41 loc) · 1.31 KB
/
common_types.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once
#ifndef CATA_SRC_COMMON_TYPES_H
#define CATA_SRC_COMMON_TYPES_H
#include <limits>
#include <type_traits>
class JsonIn;
/**
* An interval of numeric values between @ref min and @ref max (including both).
* By default it's [0, 0].
*/
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
struct numeric_interval {
T min = static_cast<T>( 0 );
T max = static_cast<T>( 0 );
numeric_interval() = default;
numeric_interval( T min, T max ) : min( min ), max( max ) { }
numeric_interval( T middle, T lower_margin, T upper_margin ) :
min( middle - lower_margin ), max( middle + upper_margin ) {
}
bool operator==( const numeric_interval<T> &rhs ) const {
return min == rhs.min && max == rhs.max;
}
bool contains( T val ) const {
return val >= min && val <= max;
}
bool empty() const {
return max == 0 || min > max;
}
void deserialize( const JsonValue &jsin ) {
JsonArray ja = jsin.get_array();
if( ja.size() != 2 ) {
ja.throw_error( "Intervals should be in format [min, max]." );
}
ja.read_next( min );
ja.read_next( max );
if( max < min ) {
max = std::numeric_limits<T>::max();
}
}
};
#endif // CATA_SRC_COMMON_TYPES_H