plugify 1.2.8
Loading...
Searching...
No Matches
value_type.hpp
1#pragma once
2
3#include <cstdint>
4#include <memory>
5#include <string>
6#include <vector>
7
8#include "plg/any.hpp"
9
10namespace plugify {
15 enum class ValueType : uint8_t {
16 Invalid,
17
18 // C types
19 Void,
20 Bool,
21 Char8,
22 Char16,
23 Int8,
24 Int16,
25 Int32,
26 Int64,
27 UInt8,
28 UInt16,
29 UInt32,
30 UInt64,
31 Pointer,
32 Float,
33 Double,
34
35 Function,
36
37 // Objects
38 String,
39 Any,
40
41 ArrayBool,
42 ArrayChar8,
43 ArrayChar16,
44 ArrayInt8,
45 ArrayInt16,
46 ArrayInt32,
47 ArrayInt64,
48 ArrayUInt8,
49 ArrayUInt16,
50 ArrayUInt32,
51 ArrayUInt64,
52 ArrayPointer,
53 ArrayFloat,
54 ArrayDouble,
55 ArrayString,
56 ArrayAny,
57 ArrayVector2,
58 ArrayVector3,
59 ArrayVector4,
60 ArrayMatrix4x4,
61
62 // Structs
63 Vector2,
64 Vector3,
65 Vector4,
66
67 Matrix4x4,
68 // Matrix2x2,
69 // Matrix2x3,
70 // Matrix2x4,
71 // Matrix3x2,
72 // Matrix3x3,
73 // Matrix3x4,
74 // Matrix4x2,
75 // Matrix4x3,
76
78
79 _BaseStart = Void,
80 _BaseEnd = Function,
81
82 _FloatStart = Float,
83 _FloatEnd = Double,
84
85 _ObjectStart = String,
86 _ObjectEnd = ArrayMatrix4x4,
87
88 _ArrayStart = ArrayBool,
89 _ArrayEnd = ArrayMatrix4x4,
90
91 _StructStart = Vector2,
92 _StructEnd = Matrix4x4,
93
94 // First struct which return as hidden parameter
95#if _WIN32 && !_M_ARM64 || INTPTR_MAX == INT32_MAX
96 _HiddenParamStart = Vector3,
97#else
98 _HiddenParamStart = Matrix4x4,
99#endif
100 _LastAssigned = Matrix4x4,
101 };
102
106 struct ValueName {
107 static constexpr std::string_view Void = "void";
108 static constexpr std::string_view Bool = "bool";
109 static constexpr std::string_view Char8 = "char8";
110 static constexpr std::string_view Char16 = "char16";
111 static constexpr std::string_view Int8 = "int8";
112 static constexpr std::string_view Int16 = "int16";
113 static constexpr std::string_view Int32 = "int32";
114 static constexpr std::string_view Int64 = "int64";
115 static constexpr std::string_view UInt8 = "uint8";
116 static constexpr std::string_view UInt16 = "uint16";
117 static constexpr std::string_view UInt32 = "uint32";
118 static constexpr std::string_view UInt64 = "uint64";
119 static constexpr std::string_view Float = "float";
120 static constexpr std::string_view Double = "double";
121 static constexpr std::string_view Function = "function";
122 static constexpr std::string_view String = "string";
123 static constexpr std::string_view Any = "any";
124 static constexpr std::string_view ArrayBool = "bool[]";
125 static constexpr std::string_view ArrayChar8 = "char8[]";
126 static constexpr std::string_view ArrayChar16 = "char16[]";
127 static constexpr std::string_view ArrayInt8 = "int8[]";
128 static constexpr std::string_view ArrayInt16 = "int16[]";
129 static constexpr std::string_view ArrayInt32 = "int32[]";
130 static constexpr std::string_view ArrayInt64 = "int64[]";
131 static constexpr std::string_view ArrayUInt8 = "uint8[]";
132 static constexpr std::string_view ArrayUInt16 = "uint16[]";
133 static constexpr std::string_view ArrayUInt32 = "uint32[]";
134 static constexpr std::string_view ArrayUInt64 = "uint64[]";
135 static constexpr std::string_view ArrayFloat = "float[]";
136 static constexpr std::string_view ArrayDouble = "double[]";
137 static constexpr std::string_view ArrayString = "string[]";
138 static constexpr std::string_view ArrayAny = "any[]";
139 static constexpr std::string_view ArrayVector2 = "vec2[]";
140 static constexpr std::string_view ArrayVector3 = "vec3[]";
141 static constexpr std::string_view ArrayVector4 = "vec4[]";
142 static constexpr std::string_view ArrayMatrix4x4 = "mat4x4[]";
143 static constexpr std::string_view Vector2 = "vec2";
144 static constexpr std::string_view Vector3 = "vec3";
145 static constexpr std::string_view Vector4 = "vec4";
146 static constexpr std::string_view Matrix4x4 = "mat4x4";
147 static constexpr std::string_view Invalid = "invalid";
148#if INTPTR_MAX == INT32_MAX
149 static constexpr std::string_view Pointer = "ptr32";
150 static constexpr std::string_view ArrayPointer = "ptr32[]";
151#elif INTPTR_MAX == INT64_MAX
152 static constexpr std::string_view Pointer = "ptr64";
153 static constexpr std::string_view ArrayPointer = "ptr64[]";
154#else
155#error "Environment not 32 or 64-bit."
156#endif
157 };
158
162 struct ValueUtils {
163
173 template <typename T>
174 static constexpr bool IsBetween(T x, T a, T b) noexcept {
175 return x >= a && x <= b;
176 }
177
184 static constexpr bool IsVoid(ValueType type) noexcept {
185 return type == ValueType::Void;
186 }
187
194 static constexpr bool IsValid(ValueType type) noexcept {
195 return IsBetween(type, ValueType::Void, ValueType::_LastAssigned);
196 }
197
204 static constexpr bool IsScalar(ValueType type) noexcept {
205 return IsBetween(type, ValueType::_BaseStart, ValueType::_BaseEnd);
206 }
207
214 static constexpr bool IsFloating(ValueType type) noexcept {
215 return IsBetween(type, ValueType::_FloatStart, ValueType::_FloatEnd);
216 }
217
224 static constexpr bool IsBool(ValueType type) noexcept {
225 return type == ValueType::Bool;
226 }
227
234 static constexpr bool IsChar8(ValueType type) noexcept {
235 return type == ValueType::Char8;
236 }
237
244 static constexpr bool IsChar16(ValueType type) noexcept {
245 return type == ValueType::Char16;
246 }
247
254 static constexpr bool IsInt8(ValueType type) noexcept {
255 return type == ValueType::Int8;
256 }
257
264 static constexpr bool IsUInt8(ValueType type) noexcept {
265 return type == ValueType::UInt8;
266 }
267
274 static constexpr bool IsInt16(ValueType type) noexcept {
275 return type == ValueType::Int16;
276 }
277
284 static constexpr bool IsUInt16(ValueType type) noexcept {
285 return type == ValueType::UInt16;
286 }
287
294 static constexpr bool IsInt32(ValueType type) noexcept {
295 return type == ValueType::Int32;
296 }
297
304 static constexpr bool IsUInt32(ValueType type) noexcept {
305 return type == ValueType::UInt32;
306 }
307
314 static constexpr bool IsInt64(ValueType type) noexcept {
315 return type == ValueType::Int64;
316 }
317
324 static constexpr bool IsUInt64(ValueType type) noexcept {
325 return type == ValueType::UInt64;
326 }
327
334 static constexpr bool IsPointer(ValueType type) noexcept {
335 return type == ValueType::Pointer;
336 }
337
344 static constexpr bool IsFloat(ValueType type) noexcept {
345 return type == ValueType::Float;
346 }
347
354 static constexpr bool IsDouble(ValueType type) noexcept {
355 return type == ValueType::Double;
356 }
357
364 static constexpr bool IsFunction(ValueType type) noexcept {
365 return type == ValueType::Function;
366 }
367
374 static constexpr bool IsString(ValueType type) noexcept {
375 return type == ValueType::String;
376 }
377
384 static constexpr bool IsAny(ValueType type) noexcept {
385 return type == ValueType::Any;
386 }
387
394 static constexpr bool IsObject(ValueType type) noexcept {
395 return IsBetween(type, ValueType::_ObjectStart, ValueType::_ObjectEnd);
396 }
397
404 static constexpr bool IsArray(ValueType type) noexcept {
405 return IsBetween(type, ValueType::_ArrayStart, ValueType::_ArrayEnd);
406 }
407
414 static constexpr bool IsStruct(ValueType type) noexcept {
415 return IsBetween(type, ValueType::_StructStart, ValueType::_StructEnd);
416 }
417
429 static constexpr bool IsHiddenParam(ValueType type) noexcept {
430 return IsObject(type) || IsBetween(type, ValueType::_HiddenParamStart, ValueType::_StructEnd);
431 }
432
442 static constexpr size_t SizeOf(ValueType type) noexcept {
443 switch (type) {
444 case ValueType::Bool:
445 return sizeof(bool);
446 case ValueType::Char8:
447 return sizeof(char);
448 case ValueType::Char16:
449 return sizeof(char16_t);
450 case ValueType::Int8:
451 return sizeof(int8_t);
452 case ValueType::Int16:
453 return sizeof(int16_t);
454 case ValueType::Int32:
455 return sizeof(int32_t);
456 case ValueType::Int64:
457 return sizeof(int64_t);
458 case ValueType::UInt8:
459 return sizeof(uint8_t);
460 case ValueType::UInt16:
461 return sizeof(uint16_t);
462 case ValueType::UInt32:
463 return sizeof(uint32_t);
464 case ValueType::UInt64:
465 return sizeof(uint64_t);
466 case ValueType::Pointer:
467 return sizeof(void*);
468 case ValueType::Float:
469 return sizeof(float);
470 case ValueType::Double:
471 return sizeof(double);
472
473 case ValueType::String:
474 return sizeof(plg::string);
475 case ValueType::Any:
476 return sizeof(plg::any);
477
478 // Arrays -> plg::vector overhead (not element size!)
479 case ValueType::ArrayBool:
480 return sizeof(plg::vector<bool>);
481 case ValueType::ArrayChar8:
482 return sizeof(plg::vector<char>);
483 case ValueType::ArrayChar16:
484 return sizeof(plg::vector<char16_t>);
485 case ValueType::ArrayInt8:
486 return sizeof(plg::vector<int8_t>);
487 case ValueType::ArrayInt16:
488 return sizeof(plg::vector<int16_t>);
489 case ValueType::ArrayInt32:
490 return sizeof(plg::vector<int32_t>);
491 case ValueType::ArrayInt64:
492 return sizeof(plg::vector<int64_t>);
493 case ValueType::ArrayUInt8:
494 return sizeof(plg::vector<uint8_t>);
495 case ValueType::ArrayUInt16:
496 return sizeof(plg::vector<uint16_t>);
497 case ValueType::ArrayUInt32:
498 return sizeof(plg::vector<uint32_t>);
499 case ValueType::ArrayUInt64:
500 return sizeof(plg::vector<uint64_t>);
501 case ValueType::ArrayPointer:
502 return sizeof(plg::vector<void*>);
503 case ValueType::ArrayFloat:
504 return sizeof(plg::vector<float>);
505 case ValueType::ArrayDouble:
506 return sizeof(plg::vector<double>);
507 case ValueType::ArrayString:
508 return sizeof(plg::vector<plg::string>);
509 case ValueType::ArrayAny:
510 return sizeof(plg::vector<plg::any>);
511 case ValueType::ArrayVector2:
512 return sizeof(plg::vector<plg::vec2>);
513 case ValueType::ArrayVector3:
514 return sizeof(plg::vector<plg::vec3>);
515 case ValueType::ArrayVector4:
516 return sizeof(plg::vector<plg::vec4>);
517 case ValueType::ArrayMatrix4x4:
518 return sizeof(plg::vector<plg::mat4x4>);
519
520 // Structs
521 case ValueType::Vector2:
522 return sizeof(plg::vec2);
523 case ValueType::Vector3:
524 return sizeof(plg::vec3);
525 case ValueType::Vector4:
526 return sizeof(plg::vec4);
527 case ValueType::Matrix4x4:
528 return sizeof(plg::mat4x4);
529
530 // Special cases
531 case ValueType::Function:
532 return sizeof(plg::function);
533 case ValueType::Void:
534 return 0;
535
536 default:
537 return 0; // Invalid / unsupported
538 }
539 }
540 };
541
542 // Test if ValueType enum values match the indices in plg::any
543 static_assert(ValueType::Void == static_cast<ValueType>(plg::any::index_of<plg::none>));
544 static_assert(ValueType::Bool == static_cast<ValueType>(plg::any::index_of<bool>));
545 static_assert(ValueType::Char8 == static_cast<ValueType>(plg::any::index_of<char>));
546 static_assert(ValueType::Char16 == static_cast<ValueType>(plg::any::index_of<char16_t>));
547 static_assert(ValueType::Int8 == static_cast<ValueType>(plg::any::index_of<int8_t>));
548 static_assert(ValueType::Int16 == static_cast<ValueType>(plg::any::index_of<int16_t>));
549 static_assert(ValueType::Int32 == static_cast<ValueType>(plg::any::index_of<int32_t>));
550 static_assert(ValueType::Int64 == static_cast<ValueType>(plg::any::index_of<int64_t>));
551 static_assert(ValueType::UInt8 == static_cast<ValueType>(plg::any::index_of<uint8_t>));
552 static_assert(ValueType::UInt16 == static_cast<ValueType>(plg::any::index_of<uint16_t>));
553 static_assert(ValueType::UInt32 == static_cast<ValueType>(plg::any::index_of<uint32_t>));
554 static_assert(ValueType::UInt64 == static_cast<ValueType>(plg::any::index_of<uint64_t>));
555 static_assert(ValueType::Pointer == static_cast<ValueType>(plg::any::index_of<void*>));
556 static_assert(ValueType::Float == static_cast<ValueType>(plg::any::index_of<float>));
557 static_assert(ValueType::Double == static_cast<ValueType>(plg::any::index_of<double>));
558 static_assert(ValueType::Function == static_cast<ValueType>(plg::any::index_of<plg::function>));
559 static_assert(ValueType::String == static_cast<ValueType>(plg::any::index_of<plg::string>));
560 static_assert(ValueType::Any == static_cast<ValueType>(plg::any::index_of<plg::variant<plg::none>>));
561 static_assert(ValueType::ArrayBool == static_cast<ValueType>(plg::any::index_of<plg::vector<bool>>));
562 static_assert( ValueType::ArrayChar8 == static_cast<ValueType>(plg::any::index_of<plg::vector<char>>));
563 static_assert( ValueType::ArrayChar16 == static_cast<ValueType>(plg::any::index_of<plg::vector<char16_t>>));
564 static_assert( ValueType::ArrayInt8 == static_cast<ValueType>(plg::any::index_of<plg::vector<int8_t>>));
565 static_assert( ValueType::ArrayInt16 == static_cast<ValueType>(plg::any::index_of<plg::vector<int16_t>>));
566 static_assert( ValueType::ArrayInt32 == static_cast<ValueType>(plg::any::index_of<plg::vector<int32_t>>));
567 static_assert( ValueType::ArrayInt64 == static_cast<ValueType>(plg::any::index_of<plg::vector<int64_t>>));
568 static_assert( ValueType::ArrayUInt8 == static_cast<ValueType>(plg::any::index_of<plg::vector<uint8_t>>));
569 static_assert( ValueType::ArrayUInt16 == static_cast<ValueType>(plg::any::index_of<plg::vector<uint16_t>>));
570 static_assert( ValueType::ArrayUInt32 == static_cast<ValueType>(plg::any::index_of<plg::vector<uint32_t>>));
571 static_assert( ValueType::ArrayUInt64 == static_cast<ValueType>(plg::any::index_of<plg::vector<uint64_t>>));
572 static_assert( ValueType::ArrayPointer == static_cast<ValueType>(plg::any::index_of<plg::vector<void*>>));
573 static_assert( ValueType::ArrayFloat == static_cast<ValueType>(plg::any::index_of<plg::vector<float>>));
574 static_assert( ValueType::ArrayDouble == static_cast<ValueType>(plg::any::index_of<plg::vector<double>>));
575 static_assert( ValueType::ArrayString == static_cast<ValueType>(plg::any::index_of<plg::vector<plg::string>>));
576 static_assert( ValueType::ArrayAny == static_cast<ValueType>(plg::any::index_of<plg::vector<plg::variant<plg::none>>>));
577 static_assert( ValueType::ArrayVector2 == static_cast<ValueType>(plg::any::index_of<plg::vector<plg::vec2>>));
578 static_assert( ValueType::ArrayVector3 == static_cast<ValueType>(plg::any::index_of<plg::vector<plg::vec3>>));
579 static_assert( ValueType::ArrayVector4 == static_cast<ValueType>(plg::any::index_of<plg::vector<plg::vec4>>));
580 static_assert( ValueType::ArrayMatrix4x4 == static_cast<ValueType>(plg::any::index_of<plg::vector<plg::mat4x4>>));
581 static_assert(ValueType::Vector2 == static_cast<ValueType>(plg::any::index_of<plg::vec2>));
582 static_assert(ValueType::Vector3 == static_cast<ValueType>(plg::any::index_of<plg::vec3>));
583 static_assert(ValueType::Vector4 == static_cast<ValueType>(plg::any::index_of<plg::vec4>));
584} // namespace plugify
Namespace containing string representations of ValueType enum values.
Namespace containing utility functions of ValueType enum.
static constexpr bool IsFloating(ValueType type) noexcept
Tests whether a given type is a scalar floating point of any size.
static constexpr bool IsValid(ValueType type) noexcept
Tests whether a given type is a valid non-void type.
static constexpr bool IsHiddenParam(ValueType type) noexcept
Checks if a given ValueType is considered a hidden object parameter.
static constexpr size_t SizeOf(ValueType type) noexcept
Returns the size in bytes of the given ValueType.
static constexpr bool IsObject(ValueType type) noexcept
Tests whether a given type is an object of any size.
static constexpr bool IsAny(ValueType type) noexcept
Tests whether a given type is an any.
static constexpr bool IsString(ValueType type) noexcept
Tests whether a given type is a string.
static constexpr bool IsFunction(ValueType type) noexcept
Tests whether a given type is a C-function pointer.
static constexpr bool IsBool(ValueType type) noexcept
Tests whether a given type is a 1-bit boolean.
static constexpr bool IsUInt32(ValueType type) noexcept
Tests whether a given type is a 32-bit unsigned integer.
static constexpr bool IsScalar(ValueType type) noexcept
Tests whether a given type is scalar (has no vector part).
static constexpr bool IsUInt8(ValueType type) noexcept
Tests whether a given type is an 8-bit unsigned integer.
static constexpr bool IsInt8(ValueType type) noexcept
Tests whether a given type is an 8-bit integer.
static constexpr bool IsArray(ValueType type) noexcept
Tests whether a given type is an array of any size.
static constexpr bool IsInt32(ValueType type) noexcept
Tests whether a given type is a 32-bit integer.
static constexpr bool IsPointer(ValueType type) noexcept
Tests whether a given type is a pointer.
static constexpr bool IsVoid(ValueType type) noexcept
Tests whether a given type is ValueType::Void.
static constexpr bool IsUInt64(ValueType type) noexcept
Tests whether a given type is a 64-bit unsigned integer.
static constexpr bool IsInt64(ValueType type) noexcept
Tests whether a given type is a 64-bit integer.
static constexpr bool IsChar16(ValueType type) noexcept
Tests whether a given type is a 16-bit character.
static constexpr bool IsDouble(ValueType type) noexcept
Tests whether a given type is a double.
static constexpr bool IsFloat(ValueType type) noexcept
Tests whether a given type is a float.
static constexpr bool IsStruct(ValueType type) noexcept
Tests whether a given type is a POD (plain old data) structure of any size.
static constexpr bool IsInt16(ValueType type) noexcept
Tests whether a given type is a 16-bit integer.
static constexpr bool IsChar8(ValueType type) noexcept
Tests whether a given type is an 8-bit character.
static constexpr bool IsBetween(T x, T a, T b) noexcept
Checks if a value is between two other values.
static constexpr bool IsUInt16(ValueType type) noexcept
Tests whether a given type is a 16-bit unsigned integer.
Represents a function pointer as a union for flexibility.
Definition any.hpp:11