Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_types.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_TYPES_H
7 #define TB_TYPES_H
8 
9 // Include <tb_config.h> so it may be overridden in application directory.
10 // The default "tb_config.h" (local) will be used if there is no other match.
11 #include <tb_config.h>
12 
13 #include <string.h>
14 #include <cstdint>
15 
16 namespace tb {
17 
18 template <class T>
19 T Max(const T& left, const T& right) { return left > right ? left : right; }
20 
21 template <class T>
22 T Min(const T& left, const T& right) { return left < right ? left : right; }
23 
24 template <class T>
25 T Abs(const T& value) { return value < 0 ? -value : value; }
26 
27 template <class T>
28 T Clamp(const T& value, const T& min, const T& max)
29  { return (value > max) ? max : ((value < min) ? min : value); }
30 
33 template <class T>
34 T ClampClipMax(const T& value, const T& min, const T& max)
35 {
36  return (value > max)
37  ? (max > min ? max : min)
38  : ((value < min) ? min : value);
39 }
40 
41 #ifndef MAX
42 
43 #define MAX(a, b) Max(a, b)
44 #endif
45 
46 #ifndef MIN
47 
48 #define MIN(a, b) Min(a, b)
49 #endif
50 
51 #ifndef ABS
52 
53 #define ABS(value) Abs(value)
54 #endif
55 
56 #ifndef _WIN32
57 
58 #define TB_POST_FORMAT(a,b) __attribute__ ((format (printf, a, b)))
59 #else
60 
61 #define TB_POST_FORMAT(a,b)
62 #endif
63 
64 #ifndef CLAMP
65 
66 #define CLAMP(value, min, max) Clamp(value, min, max)
67 #endif
68 
72 #define MAKE_ENUM_FLAG_COMBO(Enum) \
73  inline Enum operator | (Enum a, Enum b) { return static_cast<Enum>(static_cast<uint32_t>(a) | static_cast<uint32_t>(b)); } \
74  inline Enum operator & (Enum a, Enum b) { return static_cast<Enum>(static_cast<uint32_t>(a) & static_cast<uint32_t>(b)); } \
75  inline Enum operator ^ (Enum a, Enum b) { return static_cast<Enum>(static_cast<uint32_t>(a) ^ static_cast<uint32_t>(b)); } \
76  inline void operator |= (Enum &a, Enum b) { a = static_cast<Enum>(static_cast<uint32_t>(a) | static_cast<uint32_t>(b)); } \
77  inline void operator &= (Enum &a, Enum b) { a = static_cast<Enum>(static_cast<uint32_t>(a) & static_cast<uint32_t>(b)); } \
78  inline void operator ^= (Enum &a, Enum b) { a = static_cast<Enum>(static_cast<uint32_t>(a) ^ static_cast<uint32_t>(b)); } \
79  inline Enum operator ~ (Enum a) { return static_cast<Enum>(~static_cast<uint32_t>(a)); }
80 
81 } // namespace tb
82 
83 #endif // TB_TYPES_H
T ClampClipMax(const T &value, const T &min, const T &max)
Returns value clamped to min and max.
Definition: tb_types.h:34