File Index Symbol Index

/*********************************************************************** Author: Zihan Chen (vczh) Licensed under https://github.com/vczh-libraries/License ***********************************************************************/
#include <limits.h> #include <float.h>
#include "GuiTypeDescriptorReflection.h" #include <VlppRegex.h>
namespace
vl
{
using
namespace
collections
;
using
namespace
regex
;
namespace
reflection
{
namespace
description
{
/*********************************************************************** TypeName ***********************************************************************/
#ifndef VCZH_DEBUG_NO_REFLECTION
#endif
/*********************************************************************** TypedValueSerializerProvider ***********************************************************************/
#define DEFINE_COMPARE(TYPENAME)\
IBoxedValue::CompareResult TypedValueSerializerProvider<TYPENAME>::Compare(const TYPENAME& a, const TYPENAME& b)\
{\
if (a < b) return IBoxedValue::Smaller;\
if (a > b) return IBoxedValue::Greater;\
return IBoxedValue::Equal;\
}\
#undef DEFINE_COMPARE
vuint8_t
TypedValueSerializerProvider
<
vuint8_t
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
vuint8_t
>::
Serialize
(
const
vuint8_t
&
input
,
WString
&
output
) {
output
=
u64tow
(
input
);
return
true
; }
bool
TypedValueSerializerProvider
<
vuint8_t
>::
Deserialize
(
const
WString
&
input
,
vuint8_t
&
output
) {
bool
success
=
false
;
vuint64_t
result
=
wtou64_test
(
input
,
success
);
if
(!
success
)
return
false
;
output
= (
vuint8_t
)
result
;
return
true
; }
//---------------------------------------
vuint16_t
TypedValueSerializerProvider
<
vuint16_t
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
vuint16_t
>::
Serialize
(
const
vuint16_t
&
input
,
WString
&
output
) {
output
=
u64tow
(
input
);
return
true
; }
bool
TypedValueSerializerProvider
<
vuint16_t
>::
Deserialize
(
const
WString
&
input
,
vuint16_t
&
output
) {
bool
success
=
false
;
vuint64_t
result
=
wtou64_test
(
input
,
success
);
if
(!
success
)
return
false
;
output
= (
vuint16_t
)
result
;
return
true
; }
//---------------------------------------
vuint32_t
TypedValueSerializerProvider
<
vuint32_t
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
vuint32_t
>::
Serialize
(
const
vuint32_t
&
input
,
WString
&
output
) {
output
=
u64tow
(
input
);
return
true
; }
bool
TypedValueSerializerProvider
<
vuint32_t
>::
Deserialize
(
const
WString
&
input
,
vuint32_t
&
output
) {
bool
success
=
false
;
vuint64_t
result
=
wtou64_test
(
input
,
success
);
if
(!
success
)
return
false
;
output
= (
vuint32_t
)
result
;
return
true
; }
//---------------------------------------
vuint64_t
TypedValueSerializerProvider
<
vuint64_t
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
vuint64_t
>::
Serialize
(
const
vuint64_t
&
input
,
WString
&
output
) {
output
=
u64tow
(
input
);
return
true
; }
bool
TypedValueSerializerProvider
<
vuint64_t
>::
Deserialize
(
const
WString
&
input
,
vuint64_t
&
output
) {
bool
success
=
false
;
vuint64_t
result
=
wtou64_test
(
input
,
success
);
if
(!
success
)
return
false
;
output
=
result
;
return
true
; }
//---------------------------------------
vint8_t
TypedValueSerializerProvider
<
vint8_t
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
vint8_t
>::
Serialize
(
const
vint8_t
&
input
,
WString
&
output
) {
output
=
i64tow
(
input
);
return
true
; }
bool
TypedValueSerializerProvider
<
vint8_t
>::
Deserialize
(
const
WString
&
input
,
vint8_t
&
output
) {
bool
success
=
false
;
vint64_t
result
=
wtoi64_test
(
input
,
success
);
if
(!
success
)
return
false
;
output
= (
vint8_t
)
result
;
return
true
; }
//---------------------------------------
vint16_t
TypedValueSerializerProvider
<
vint16_t
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
vint16_t
>::
Serialize
(
const
vint16_t
&
input
,
WString
&
output
) {
output
=
i64tow
(
input
);
return
true
; }
bool
TypedValueSerializerProvider
<
vint16_t
>::
Deserialize
(
const
WString
&
input
,
vint16_t
&
output
) {
bool
success
=
false
;
vint64_t
result
=
wtoi64_test
(
input
,
success
);
if
(!
success
)
return
false
;
output
= (
vint16_t
)
result
;
return
true
; }
//---------------------------------------
vint32_t
TypedValueSerializerProvider
<
vint32_t
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
vint32_t
>::
Serialize
(
const
vint32_t
&
input
,
WString
&
output
) {
output
=
i64tow
(
input
);
return
true
; }
bool
TypedValueSerializerProvider
<
vint32_t
>::
Deserialize
(
const
WString
&
input
,
vint32_t
&
output
) {
bool
success
=
false
;
vint64_t
result
=
wtoi64_test
(
input
,
success
);
if
(!
success
)
return
false
;
output
= (
vint32_t
)
result
;
return
true
; }
//---------------------------------------
vint64_t
TypedValueSerializerProvider
<
vint64_t
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
vint64_t
>::
Serialize
(
const
vint64_t
&
input
,
WString
&
output
) {
output
=
i64tow
(
input
);
return
true
; }
bool
TypedValueSerializerProvider
<
vint64_t
>::
Deserialize
(
const
WString
&
input
,
vint64_t
&
output
) {
bool
success
=
false
;
vint64_t
result
=
wtoi64_test
(
input
,
success
);
if
(!
success
)
return
false
;
output
=
result
;
return
true
; }
//---------------------------------------
float
TypedValueSerializerProvider
<
float
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
float
>::
Serialize
(
const
float
&
input
,
WString
&
output
) {
output
=
ftow
(
input
);
if
(
output
==
L"-0"
)
output
=
L"0"
;
return
true
; }
bool
TypedValueSerializerProvider
<
float
>::
Deserialize
(
const
WString
&
input
,
float
&
output
) {
bool
success
=
false
;
double
result
=
wtof_test
(
input
,
success
);
if
(!
success
)
return
false
;
output
= (
float
)
result
;
return
true
; }
//---------------------------------------
double
TypedValueSerializerProvider
<
double
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
double
>::
Serialize
(
const
double
&
input
,
WString
&
output
) {
output
=
ftow
(
input
);
if
(
output
==
L"-0"
)
output
=
L"0"
;
return
true
; }
bool
TypedValueSerializerProvider
<
double
>::
Deserialize
(
const
WString
&
input
,
double
&
output
) {
bool
success
=
false
;
double
result
=
wtof_test
(
input
,
success
);
if
(!
success
)
return
false
;
output
=
result
;
return
true
; }
//---------------------------------------
wchar_t
TypedValueSerializerProvider
<
wchar_t
>::
GetDefaultValue
() {
return
0
; }
bool
TypedValueSerializerProvider
<
wchar_t
>::
Serialize
(
const
wchar_t
&
input
,
WString
&
output
) {
output
=
input
?
WString
(
input
) :
L""
;
return
true
; }
bool
TypedValueSerializerProvider
<
wchar_t
>::
Deserialize
(
const
WString
&
input
,
wchar_t
&
output
) {
if
(
input
.
Length
()>
1
)
return
false
;
output
=
input
.
Length
() ==
0
?
0
:
input
[
0
];
return
true
; }
//---------------------------------------
WString
TypedValueSerializerProvider
<
WString
>::
GetDefaultValue
() {
return
L""
; }
bool
TypedValueSerializerProvider
<
WString
>::
Serialize
(
const
WString
&
input
,
WString
&
output
) {
output
=
input
;
return
true
; }
bool
TypedValueSerializerProvider
<
WString
>::
Deserialize
(
const
WString
&
input
,
WString
&
output
) {
output
=
input
;
return
true
; }
//---------------------------------------
bool
TypedValueSerializerProvider
<
bool
>::
GetDefaultValue
() {
return
false
; }
bool
TypedValueSerializerProvider
<
bool
>::
Serialize
(
const
bool
&
input
,
WString
&
output
) {
output
=
input
?
L"true"
:
L"false"
;
return
true
; }
bool
TypedValueSerializerProvider
<
bool
>::
Deserialize
(
const
WString
&
input
,
bool
&
output
) {
output
=
input
=
=
L"true"
;
return
input
=
=
L"true"
||
input
==
L"false"
; }
//---------------------------------------
Locale
TypedValueSerializerProvider
<
Locale
>::
GetDefaultValue
() {
return
Locale
(); }
bool
TypedValueSerializerProvider
<
Locale
>::
Serialize
(
const
Locale
&
input
,
WString
&
output
) {
output
=
input
.
GetName
();
return
true
; }
bool
TypedValueSerializerProvider
<
Locale
>::
Deserialize
(
const
WString
&
input
,
Locale
&
output
) {
output
=
Locale
(
input
);
return
true
; }
/*********************************************************************** DateTimeValueSerializer ***********************************************************************/
Regex
*
regexDateTime
=
nullptr
;
regexDateTime
=
new
Regex
(
L"(<Y>/d/d/d/d)-(<M>/d/d)-(<D>/d/d) (<h>/d/d):(<m>/d/d):(<s>/d/d).(<ms>/d/d/d)"
);
delete
regexDateTime
;
regexDateTime
=
nullptr
;
DateTime
TypedValueSerializerProvider
<
DateTime
>::
GetDefaultValue
() {
return
DateTime
(); }
WString
FormatDigits
(
vint
number
,
vint
length
) {
WString
result
=
itow
(
number
);
while
(
result
.
Length
() <
length
) {
result
=
L"0"
+
result
; }
return
result
; }
bool
TypedValueSerializerProvider
<
DateTime
>::
Serialize
(
const
DateTime
&
input
,
WString
&
output
) {
output
=
FormatDigits
(
input
.
year
,
4
) +
L"-"
+
FormatDigits
(
input
.
month
,
2
) +
L"-"
+
FormatDigits
(
input
.
day
,
2
) +
L" "
+
FormatDigits
(
input
.
hour
,
2
) +
L":"
+
FormatDigits
(
input
.
minute
,
2
) +
L":"
+
FormatDigits
(
input
.
second
,
2
) +
L"."
+
FormatDigits
(
input
.
milliseconds
,
3
);
return
true
; }
bool
TypedValueSerializerProvider
<
DateTime
>::
Deserialize
(
const
WString
&
input
,
DateTime
&
output
) {
Ptr
<
RegexMatch
>
match
=
GetDateTimeSerializerStorage
().
regexDateTime
->
Match
(
input
);
if
(!
match
)
return
false
;
if
(!
match
-
>
Success
())
return
false
;
if
(
match
-
>
Result
().
Start
() !=
0
)
return
false
;
if
(
match
-
>
Result
().
Length
() !=
input
.
Length
())
return
false
;
vint
year
=
wtoi
(
match
-
>
Groups
()[
L"Y"
].Get(
0
).
Value
());
vint
month
=
wtoi
(
match
-
>
Groups
()[
L"M"
].Get(
0
).
Value
());
vint
day
=
wtoi
(
match
-
>
Groups
()[
L"D"
].Get(
0
).
Value
());
vint
hour
=
wtoi
(
match
-
>
Groups
()[
L"h"
].Get(
0
).
Value
());
vint
minute
=
wtoi
(
match
-
>
Groups
()[
L"m"
].Get(
0
).
Value
());
vint
second
=
wtoi
(
match
-
>
Groups
()[
L"s"
].Get(
0
).
Value
());
vint
milliseconds
=
wtoi
(
match
-
>
Groups
()[
L"ms"
].Get(
0
).
Value
());
output
=
DateTime
::
FromDateTime
(
year
,
month
,
day
,
hour
,
minute
,
second
,
milliseconds
);
return
true
; }
IBoxedValue
::
CompareResult
TypedValueSerializerProvider
<
DateTime
>::
Compare
(
const
DateTime
&
a
,
const
DateTime
&
b
) {
auto
ta
=
a
.
filetime
;
auto
tb
=
b
.
filetime
;
if
(
ta
<
tb
)
return
IBoxedValue
::
Smaller
;
if
(
ta
>
tb
)
return
IBoxedValue
::
Greater
;
return
IBoxedValue
::
Equal
; }
/*********************************************************************** Helper Functions ***********************************************************************/
#ifndef VCZH_DEBUG_NO_REFLECTION
vint
ITypeDescriptor_GetTypeDescriptorCount
() {
return
GetGlobalTypeManager
()->
GetTypeDescriptorCount
(); }
ITypeDescriptor
*
ITypeDescriptor_GetTypeDescriptor
(
vint
index
) {
return
GetGlobalTypeManager
()->
GetTypeDescriptor
(
index
); }
ITypeDescriptor
*
ITypeDescriptor_GetTypeDescriptor
(
const
WString
&
name
) {
return
GetGlobalTypeManager
()->
GetTypeDescriptor
(
name
); }
#else
vint ITypeDescriptor_GetTypeDescriptorCount()
{
return 0;
}
ITypeDescriptor* ITypeDescriptor_GetTypeDescriptor(vint index)
{
return nullptr;
}
ITypeDescriptor* ITypeDescriptor_GetTypeDescriptor(const WString& name)
{
return nullptr;
}
#endif
#ifdef VCZH_DESCRIPTABLEOBJECT_WITH_METADATA
ITypeDescriptor
*
ITypeDescriptor_GetTypeDescriptor
(
const
Value
&
value
) {
return
value
.
GetTypeDescriptor
(); }
#else
ITypeDescriptor* ITypeDescriptor_GetTypeDescriptor(const Value& value)
{
return nullptr;
} #endif
/*********************************************************************** LoadPredefinedTypes ***********************************************************************/
#ifdef VCZH_DESCRIPTABLEOBJECT_WITH_METADATA
#define _ ,
template
<>
struct
CustomTypeDescriptorSelector
<
DescriptableObject
> {
public
:
class
CustomTypeDescriptorImpl
:
public
TypeDescriptorImpl
{
public
:
CustomTypeDescriptorImpl
() :
TypeDescriptorImpl
(
TypeDescriptorFlags
::
Class
, &
TypeInfo
<
DescriptableObject
>::
content
) {
Description
<
DescriptableObject
>::
SetAssociatedTypeDescriptor
(
this
); }
~
CustomTypeDescriptorImpl
() {
Description
<
DescriptableObject
>::
SetAssociatedTypeDescriptor
(
0
); }
protected
:
void
LoadInternal
()
override
{ } }; };
valueType
=
new
SerializableValueType
<
DateTime
>();
serializableType
=
new
SerializableType
<
DateTime
>();
#pragma push_macro("GetMessage")
#if defined GetMessage
#undef GetMessage #endif
#pragma pop_macro("GetMessage")
#undef _
class
PredefinedTypeLoader
:
public
Object
,
public
ITypeLoader
{
public
:
void
Load
(
ITypeManager
*
manager
)
override
{
manager
->
SetTypeDescriptor
(
TypeInfo
<
Value
>::
content
.typeName,
new
TypedValueTypeDescriptorBase
<
Value
,
TypeDescriptorFlags
::
Object
>);
#define ADD_PRIMITIVE_TYPE(TYPE) manager->SetTypeDescriptor(TypeInfo<TYPE>::content.typeName, new PrimitiveTypeDescriptor<TYPE>());
#undef ADD_PRIMITIVE_TYPE
}
void
Unload
(
ITypeManager
*
manager
)
override
{ } };
#endif
bool
LoadPredefinedTypes
() {
#ifdef VCZH_DESCRIPTABLEOBJECT_WITH_METADATA
ITypeManager
*
manager
=
GetGlobalTypeManager
();
if
(
manager
) {
Ptr
<
ITypeLoader
>
loader
=
new
PredefinedTypeLoader
;
return
manager
->
AddTypeLoader
(
loader
); }
#endif
return
false
; } } } }