LkEngine 0.1.2
 
Loading...
Searching...
No Matches
SerializationMacros.h
1#pragma once
2
3#include <stdarg.h>
4#include <string>
5
6#include "YamlSerialization.h"
7
8
9namespace LkEngine
10{
11 namespace Serialization::Internal
12 {
18 template <typename T>
19 static constexpr auto ToStringIfNeeded(T Value)
20 {
21 if constexpr (std::is_same_v<T, const char*> || std::is_array_v<T>)
22 {
23 /* Convert to std::string if it is const char* or const char[]. */
24 return std::string(Value);
25 }
26 else
27 {
28 return Value;
29 }
30 }
31 }
32}
33
34/* TODO: Possible to statically evaluate the 'PropertyName' argument and if it's an Lvalue or Rvalue? */
35
42#define LK_SERIALIZE_PROPERTY(PropertyName, PropertyValue, OutputNode) \
43 OutputNode << YAML::Key << #PropertyName << YAML::Value << PropertyValue
44
51#define LK_SERIALIZE_PROPERTY_VALUE(Property, PropertyValue, OutputNode) \
52 OutputNode << YAML::Key << Property << YAML::Value << PropertyValue
53
54#define LK_SERIALIZE_PROPERTY_ASSET(PropertyName, PropertyValue, OutputData) \
55 OutputData << YAML::Key << #PropertyName << YAML::Value << (PropertyValue ? static_cast<uint64_t>(PropertyValue->Handle) : 0);
56
63#define LK_DESERIALIZE_PROPERTY(PropertyName, Destination, Node, DefaultValue) \
64 if (Node.IsMap()) \
65 { \
66 if (auto FoundNode = Node[#PropertyName]) \
67 { \
68 try \
69 { \
70 Destination = FoundNode.as<decltype(::LkEngine::Serialization::Internal::ToStringIfNeeded(DefaultValue))>(); \
71 } \
72 catch (const std::exception& Exception) \
73 { \
74 LK_CORE_FATAL("LK_DESERIALIZE_PROPERTY: {}", Exception.what());\
75 Destination = DefaultValue; \
76 } \
77 } \
78 else \
79 { \
80 LK_CORE_ERROR_TAG("LK_DESERIALIZE_PROPERTY", "Failed to find property: '{}'", #PropertyName); \
81 Destination = DefaultValue; \
82 } \
83 } \
84 else \
85 { \
86 LK_CORE_ERROR_TAG("LK_DESERIALIZE_PROPERTY", "Root node used with property '{}' is not a YAML::Map", #PropertyName); \
87 Destination = DefaultValue; \
88 }
89
96#define LK_DESERIALIZE_PROPERTY_VALUE(PropertyValue, Destination, Node, DefaultValue) \
97 if (Node.IsMap()) \
98 { \
99 if (auto FoundNode = Node[PropertyValue]) \
100 { \
101 try \
102 { \
103 Destination = FoundNode.as<decltype(::LkEngine::Serialization::Internal::ToStringIfNeeded(DefaultValue))>(); \
104 } \
105 catch (const std::exception& Exception) \
106 { \
107 LK_CORE_FATAL("LK_DESERIALIZE_PROPERTY_VALUE: {}", Exception.what()); \
108 Destination = DefaultValue; \
109 } \
110 } \
111 else \
112 { \
113 LK_CORE_ERROR_TAG("LK_DESERIALIZE_PROPERTY_VALUE", "Failed to find property: '{}'", PropertyValue); \
114 Destination = DefaultValue; \
115 } \
116 } \
117 else \
118 { \
119 LK_CORE_ERROR_TAG("LK_DESERIALIZE_PROPERTY_VALUE", "Root node used with property '{}' is not a YAML::Map", PropertyValue); \
120 Destination = DefaultValue; \
121 }
122
123
124#define LK_SERIALIZE_BEGIN_GROUP(Name) Out << YAML::Key << #Name << YAML::Value << YAML::BeginMap;
125#define LK_SERIALIZE_END_GROUP() Out << YAML::EndMap;
126#define LK_SERIALIZE_VALUE(Name, InValue) Out << YAML::Key << #Name << YAML::Value << InValue;
127
Definition Asset.h:11