TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_numeric_type.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
5namespace tachyon
6{
9
10 // ------------------------------------------------------------------------
11 // Numeric Value Union/Type - For editable numeric text fields
12 // ------------------------------------------------------------------------
13
35
37 {
38 uint8_t value_uint8;
39 uint16_t value_uint16;
40 uint32_t value_uint32;
41 uint64_t value_uint64;
42
43 int8_t value_int8;
44 int16_t value_int16;
45 int32_t value_int32;
46 int64_t value_int64;
47
50 };
51
52 template<typename T> constexpr NumericType GetNumericType(const T& /*value*/) { return NumericType::None; }
53 template<> constexpr NumericType GetNumericType(const int8_t& /*value*/) { return NumericType::TypeInt8; }
54 template<> constexpr NumericType GetNumericType(const int16_t& /*value*/) { return NumericType::TypeInt16; }
55 template<> constexpr NumericType GetNumericType(const int32_t& /*value*/) { return NumericType::TypeInt32; }
56 template<> constexpr NumericType GetNumericType(const int64_t& /*value*/) { return NumericType::TypeInt64; }
57 template<> constexpr NumericType GetNumericType(const uint8_t& /*value*/) { return NumericType::TypeUInt8; }
58 template<> constexpr NumericType GetNumericType(const uint16_t& /*value*/) { return NumericType::TypeUInt16; }
59 template<> constexpr NumericType GetNumericType(const uint32_t& /*value*/) { return NumericType::TypeUInt32; }
60 template<> constexpr NumericType GetNumericType(const uint64_t& /*value*/) { return NumericType::TypeUInt64; }
61 template<> constexpr NumericType GetNumericType(const float& /*value*/) { return NumericType::TypeFloat; }
62 template<> constexpr NumericType GetNumericType(const double& /*value*/) { return NumericType::TypeDouble; }
63
64 template<typename T> NumericType CopyNumericTypeTo(const T& /*src*/, NumericValue& dst) { dst = {}; return NumericType::None; }
65 template<> inline NumericType CopyNumericTypeTo(const int8_t& src, NumericValue& dst) { dst.value_int8 = src; return NumericType::TypeInt8; }
66 template<> inline NumericType CopyNumericTypeTo(const int16_t& src, NumericValue& dst) { dst.value_int16 = src; return NumericType::TypeInt16; }
67 template<> inline NumericType CopyNumericTypeTo(const int32_t& src, NumericValue& dst) { dst.value_int32 = src; return NumericType::TypeInt32; }
68 template<> inline NumericType CopyNumericTypeTo(const int64_t& src, NumericValue& dst) { dst.value_int64 = src; return NumericType::TypeInt64; }
69 template<> inline NumericType CopyNumericTypeTo(const uint8_t& src, NumericValue& dst) { dst.value_uint8 = src; return NumericType::TypeUInt8; }
70 template<> inline NumericType CopyNumericTypeTo(const uint16_t& src, NumericValue& dst) { dst.value_uint16 = src; return NumericType::TypeUInt16; }
71 template<> inline NumericType CopyNumericTypeTo(const uint32_t& src, NumericValue& dst) { dst.value_uint32 = src; return NumericType::TypeUInt32; }
72 template<> inline NumericType CopyNumericTypeTo(const uint64_t& src, NumericValue& dst) { dst.value_uint64 = src; return NumericType::TypeUInt64; }
73 template<> inline NumericType CopyNumericTypeTo(const float& src, NumericValue& dst) { dst.value_float = src; return NumericType::TypeFloat; }
74 template<> inline NumericType CopyNumericTypeTo(const double& src, NumericValue& dst) { dst.value_double = src; return NumericType::TypeDouble; }
75
76 template<typename T> bool CopyNumericTypeTo(NumericType /*type*/, const NumericValue& /*src*/, T& /*dst*/) { return false; }
77 template<> inline bool CopyNumericTypeTo(NumericType type, const NumericValue& src, int8_t& dst) { if (type != NumericType::TypeInt8 ) return false; dst = src.value_int8 ; return true; }
78 template<> inline bool CopyNumericTypeTo(NumericType type, const NumericValue& src, int16_t& dst) { if (type != NumericType::TypeInt16 ) return false; dst = src.value_int16 ; return true; }
79 template<> inline bool CopyNumericTypeTo(NumericType type, const NumericValue& src, int32_t& dst) { if (type != NumericType::TypeInt32 ) return false; dst = src.value_int32 ; return true; }
80 template<> inline bool CopyNumericTypeTo(NumericType type, const NumericValue& src, int64_t& dst) { if (type != NumericType::TypeInt64 ) return false; dst = src.value_int64 ; return true; }
81 template<> inline bool CopyNumericTypeTo(NumericType type, const NumericValue& src, uint8_t& dst) { if (type != NumericType::TypeUInt8 ) return false; dst = src.value_uint8 ; return true; }
82 template<> inline bool CopyNumericTypeTo(NumericType type, const NumericValue& src, uint16_t& dst) { if (type != NumericType::TypeUInt16) return false; dst = src.value_uint16; return true; }
83 template<> inline bool CopyNumericTypeTo(NumericType type, const NumericValue& src, uint32_t& dst) { if (type != NumericType::TypeUInt32) return false; dst = src.value_uint32; return true; }
84 template<> inline bool CopyNumericTypeTo(NumericType type, const NumericValue& src, uint64_t& dst) { if (type != NumericType::TypeUInt64) return false; dst = src.value_uint64; return true; }
85 template<> inline bool CopyNumericTypeTo(NumericType type, const NumericValue& src, float& dst) { if (type != NumericType::TypeFloat ) return false; dst = src.value_float ; return true; }
86 template<> inline bool CopyNumericTypeTo(NumericType type, const NumericValue& src, double& dst) { if (type != NumericType::TypeDouble) return false; dst = src.value_double; return true; }
87
88 template<typename T> bool EqualsNumericType(NumericType /*type*/, NumericValue& /*left*/, const T& /*right*/) { return false; }
89 template<> inline bool EqualsNumericType(NumericType type, NumericValue& left, const int8_t& right) { return type == NumericType::TypeInt8 && left.value_int8 == right; }
90 template<> inline bool EqualsNumericType(NumericType type, NumericValue& left, const int16_t& right) { return type == NumericType::TypeInt16 && left.value_int16 == right; }
91 template<> inline bool EqualsNumericType(NumericType type, NumericValue& left, const int32_t& right) { return type == NumericType::TypeInt32 && left.value_int32 == right; }
92 template<> inline bool EqualsNumericType(NumericType type, NumericValue& left, const int64_t& right) { return type == NumericType::TypeInt64 && left.value_int64 == right; }
93 template<> inline bool EqualsNumericType(NumericType type, NumericValue& left, const uint8_t& right) { return type == NumericType::TypeUInt8 && left.value_uint8 == right; }
94 template<> inline bool EqualsNumericType(NumericType type, NumericValue& left, const uint16_t& right) { return type == NumericType::TypeUInt16 && left.value_uint16 == right; }
95 template<> inline bool EqualsNumericType(NumericType type, NumericValue& left, const uint32_t& right) { return type == NumericType::TypeUInt32 && left.value_uint32 == right; }
96 template<> inline bool EqualsNumericType(NumericType type, NumericValue& left, const uint64_t& right) { return type == NumericType::TypeUInt64 && left.value_uint64 == right; }
97 template<> inline bool EqualsNumericType(NumericType type, NumericValue& left, const float& right) { return type == NumericType::TypeFloat && left.value_float == right; }
98 template<> inline bool EqualsNumericType(NumericType type, NumericValue& left, const double& right) { return type == NumericType::TypeDouble && left.value_double == right; }
99
100 inline bool EqualsNumericType(NumericType leftType, NumericValue& left, NumericType rightType, const NumericValue& right)
101 {
102 if (leftType != rightType) return false;
103 switch (leftType)
104 {
105 case NumericType::TypeFloat: return left.value_float == right.value_float;
106 case NumericType::TypeDouble: return left.value_double == right.value_double;
107
108 case NumericType::TypeUInt64: return left.value_uint64 == right.value_uint64;
109 case NumericType::TypeUInt32: return left.value_uint32 == right.value_uint32;
110 case NumericType::TypeUInt16: return left.value_uint16 == right.value_uint16;
111 case NumericType::TypeUInt8: return left.value_uint8 == right.value_uint8;
112
113 case NumericType::TypeInt64: return left.value_int64 == right.value_int64;
114 case NumericType::TypeInt32: return left.value_int32 == right.value_int32;
115 case NumericType::TypeInt16: return left.value_int16 == right.value_int16;
116 case NumericType::TypeInt8: return left.value_int8 == right.value_int8;
117
118 default: return false;
119 }
120 }
121
122}
@ None
Definition gui_element_flags.h:19
NumericType CopyNumericTypeTo(const T &, NumericValue &dst)
Definition gui_numeric_type.h:64
NumericType
Definition gui_numeric_type.h:15
bool EqualsNumericType(NumericType, NumericValue &, const T &)
Definition gui_numeric_type.h:88
constexpr NumericType GetNumericType(const T &)
Definition gui_numeric_type.h:52
@ TypeFloat
Definition gui_numeric_type.h:32
@ TypeInt64
Definition gui_numeric_type.h:30
@ TypeInt16
Definition gui_numeric_type.h:28
@ TypeInt32
Definition gui_numeric_type.h:29
@ None
not a numeric type
Definition gui_numeric_type.h:16
@ TypeUInt32
Definition gui_numeric_type.h:24
@ TypeDouble
Definition gui_numeric_type.h:33
@ TypeUInt64
Definition gui_numeric_type.h:25
@ TypeUInt16
Definition gui_numeric_type.h:23
@ TypeSignedBit
Definition gui_numeric_type.h:19
@ TypeFloatBit
Definition gui_numeric_type.h:20
@ TypeInt8
Definition gui_numeric_type.h:27
@ TypeUInt8
Definition gui_numeric_type.h:22
@ TypeUnsignedBit
Definition gui_numeric_type.h:18
Definition gui_logging.h:24
Definition gui_numeric_type.h:37
int32_t value_int32
Definition gui_numeric_type.h:45
uint16_t value_uint16
Definition gui_numeric_type.h:39
uint64_t value_uint64
Definition gui_numeric_type.h:41
int8_t value_int8
Definition gui_numeric_type.h:43
int16_t value_int16
Definition gui_numeric_type.h:44
uint8_t value_uint8
Definition gui_numeric_type.h:38
float value_float
Definition gui_numeric_type.h:48
double value_double
Definition gui_numeric_type.h:49
uint32_t value_uint32
Definition gui_numeric_type.h:40
int64_t value_int64
Definition gui_numeric_type.h:46