Nebula
Loading...
Searching...
No Matches
componentserialization.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
13//------------------------------------------------------------------------------
14#include "io/memorystream.h"
15#include "io/binarywriter.h"
16#include "io/binaryreader.h"
17#include "io/jsonreader.h"
18#include "io/jsonwriter.h"
19#include "util/stringatom.h"
20#include "util/dictionary.h"
21#include "util/delegate.h"
22#include "game/componentid.h"
23
24namespace Game
25{
26
27//------------------------------------------------------------------------------
31{
32public:
33 using DeserializeJsonFunc = Util::Delegate<void(Ptr<IO::JsonReader> const&, const char* name, void*)>;
34 using SerializeJsonFunc = Util::Delegate<void(Ptr<IO::JsonWriter> const&, const char* name, void*)>;
35
37 static void Destroy();
38
39 template<typename TYPE>
40 static void Register(ComponentId component);
41
43 static void Deserialize(Ptr<IO::JsonReader> const& reader, ComponentId component, void* ptr);
45 static void Serialize(Ptr<IO::JsonWriter> const& writer, ComponentId component, void* ptr);
46
47private:
50
52
54 bool ValidateTypeSize(ComponentId component, uint32_t size);
55
61
63};
64
65//------------------------------------------------------------------------------
68template<typename TYPE>
69inline void
71{
72 // Setup a type-specific read function
73 const auto read = [](Ptr<IO::JsonReader> const& reader, const char* name, void* ptr)
74 {
75 TYPE& value = *(static_cast<TYPE*>(ptr));
76 reader->Get(value, name);
77 };
78
79 // Setup a type-specific write function
80 const auto write = [](Ptr<IO::JsonWriter> const& writer, const char* name, void* ptr)
81 {
82 TYPE& value = *(static_cast<TYPE*>(ptr));
83 writer->Add(value, name);
84 };
85
86 // TODO: Serialization
87
88 auto* reg = Instance();
89 if (!reg->ValidateTypeSize(component, sizeof(TYPE)))
90 {
91 n_error("Trying to add a serializer that works on different sized component compared to what is registered in the MemDb::AttributeRegistry!");
92 return;
93 }
94
95 Serializer s;
96 s.deserializeJson = read;
97 s.serializeJson = write;
98 while (reg->serializers.Size() <= component.id)
99 {
100 reg->serializers.Grow();
101 reg->serializers.Resize(reg->serializers.Capacity());
102 }
103
104 reg->serializers[component.id] = s;
105}
106
107} // namespace Game
Component serialization functions.
Definition componentserialization.h:31
static void Destroy()
This static method is used to destroy the registry object and should be called right before the main ...
Definition componentserialization.cc:39
ComponentSerialization()
Definition componentserialization.cc:73
static void Serialize(Ptr< IO::JsonWriter > const &writer, ComponentId component, void *ptr)
ptr points to the value that should be stored
Definition componentserialization.cc:63
static void Deserialize(Ptr< IO::JsonReader > const &reader, ComponentId component, void *ptr)
ptr points to the location where the value should be stored. Make sure you have room for it!
Definition componentserialization.cc:52
~ComponentSerialization()
Definition componentserialization.cc:81
Util::Array< Serializer > serializers
Definition componentserialization.h:62
static void Register(ComponentId component)
Definition componentserialization.h:70
static ComponentSerialization * Instance()
The registry's constructor is called by the Instance() method, and nobody else.
Definition componentserialization.cc:22
static ComponentSerialization * Singleton
Definition componentserialization.h:51
bool ValidateTypeSize(ComponentId component, uint32_t size)
validate that the size of MemDb::AttributeRegistry's component named 'name' has typesize of 'size'
Definition componentserialization.cc:90
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
Nebula's dynamic array class.
Definition array.h:60
Nebula delegate class, allows to store a function, method or lambda call into a C++ object for later ...
Definition delegate.h:39
void __cdecl n_error(const char *msg,...)
This function is called when a serious situation is encountered which requires abortion of the applic...
Definition debug.cc:138
Game::EditorState.
Definition orientation.h:7
Definition componentserialization.h:57
DeserializeJsonFunc deserializeJson
Definition componentserialization.h:58
SerializeJsonFunc serializeJson
Definition componentserialization.h:59
Definition attributeid.h:19