Skip to content

Commit ac61a3f

Browse files
committed
Any container type, work on Event (de+)serialization
1 parent f2dc869 commit ac61a3f

11 files changed

Lines changed: 398 additions & 86 deletions

File tree

3DRadSpace/Engine3DRadSpace/Games/Game.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void Game::_initialize()
2929
RequireService(typeid(PostProcessCollection));
3030
RequireService(typeid(Physics::IPhysicsEngine));
3131
RequireService(typeid(Rendering::RenderingManager));
32+
RequireService(typeid(ObjectList));
3233

3334
Internal::RegisterDefaultTypes(Content.get());
3435

@@ -86,6 +87,14 @@ IService* Game::RequireService(const std::type_index& type)
8687
AddService(RenderingManager.get());
8788
return RenderingManager.get();
8889
}
90+
if (typeid(ObjectList) == type)
91+
{
92+
if (Objects) return Objects.get();
93+
Objects = std::make_unique<Objects::ObjectList>(this);
94+
AddService(Objects.get());
95+
return Objects.get();
96+
}
97+
8998
return nullptr;
9099
}
91100

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "EventInvocationRepresentation.hpp"
2+
#include "../Objects/Impl/Objects.hpp"
3+
4+
using namespace Engine3DRadSpace;
5+
using namespace Engine3DRadSpace::Projects;
6+
using namespace Engine3DRadSpace::Reflection;
7+
8+
EventInvocationRepresentation::EventInvocationRepresentation() :
9+
OwnerObject(std::numeric_limits<size_t>::max()),
10+
FunctionID(std::numeric_limits<size_t>::max())
11+
{
12+
}
13+
14+
IReflectedFunction* EventInvocationRepresentation::FindFunction(const Reflection::UUID& uuid, size_t idxFn)
15+
{
16+
auto reflObject = Internal::GetReflDataFromUUID(uuid);
17+
if (reflObject == nullptr) return nullptr;
18+
19+
size_t id = 0;
20+
21+
for (auto& f : *reflObject)
22+
{
23+
auto fn = dynamic_cast<IReflectedFunction*>(f);
24+
if (fn == nullptr) continue;
25+
26+
if (id++ == idxFn)
27+
{
28+
return fn;
29+
}
30+
}
31+
32+
return nullptr;
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
#include "../Reflection/Event.hpp"
3+
4+
namespace Engine3DRadSpace::Objects
5+
{
6+
class ObjectList;
7+
}
8+
9+
namespace Engine3DRadSpace::Projects
10+
{
11+
/// <summary>
12+
/// Serializable representation of a member function call.
13+
/// </summary>
14+
struct E3DRSP_PROJECTS_EXPORT EventInvocationRepresentation
15+
{
16+
/// <summary>
17+
/// ID of the object in the ObjectList.
18+
/// </summary>
19+
size_t OwnerObject;
20+
/// <summary>
21+
/// ID of the function, per reflection metadata.
22+
/// </summary>
23+
size_t FunctionID;
24+
/// <summary>
25+
/// Arguments pack.
26+
/// </summary>
27+
std::vector<std::any> Args;
28+
29+
EventInvocationRepresentation();
30+
31+
static Reflection::IReflectedFunction* FindFunction(const Reflection::UUID& uuid, size_t idxFn);
32+
};
33+
}
Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,26 @@
11
#include "EventRepresentation.hpp"
2-
#include "../Objects/ObjectList.hpp"
3-
#include "../Objects/Impl/Objects.hpp"
42

53
using namespace Engine3DRadSpace;
64
using namespace Engine3DRadSpace::Projects;
75
using namespace Engine3DRadSpace::Reflection;
8-
using namespace Engine3DRadSpace::Objects;
96

10-
EventInvocationRepresentation::EventInvocationRepresentation() :
11-
OwnerObject(std::numeric_limits<size_t>::max()),
12-
FunctionID(std::numeric_limits<size_t>::max())
13-
{
14-
}
15-
16-
std::any EventInvocationRepresentation::Invoke(ObjectList* list)
17-
{
18-
auto obj = list->operator[](OwnerObject);
19-
return Invoke(obj);
20-
}
21-
22-
IReflectedFunction* EventInvocationRepresentation::FindFunction(IObject* object, size_t idxFn)
23-
{
24-
auto reflObject = Internal::GetReflDataFromUUID(object->GetUUID());
25-
if (reflObject == nullptr) return nullptr;
26-
27-
size_t id = 0;
28-
29-
for (auto& f : *reflObject)
30-
{
31-
auto fn = dynamic_cast<IReflectedFunction*>(f);
32-
if (fn == nullptr) continue;
33-
34-
if (id++ == idxFn)
35-
{
36-
return fn;
37-
}
38-
}
39-
40-
return nullptr;
41-
}
42-
43-
std::any EventInvocationRepresentation::Invoke(IObject* obj)
44-
{
45-
auto fn = FindFunction(obj, FunctionID);
46-
if (fn != nullptr) return fn->Invoke(obj, Args);
47-
48-
return std::any();
49-
}
50-
51-
std::optional<Event> EventRepresentation::Reconstruct(ObjectList* list) const
7+
std::optional<Event> EventRepresentation::Reconstruct(const Reflection::UUID& uuid) const
528
{
539
Event e;
5410

11+
if (BoundFunctions.empty()) return std::nullopt;
12+
5513
for (auto& fn : BoundFunctions)
5614
{
57-
auto fnRelPtr = fn.FindFunction(list->operator[](fn.OwnerObject), fn.FunctionID);
58-
if(!fnRelPtr) continue;
15+
auto fnRelPtr = fn.FindFunction(uuid, fn.FunctionID);
16+
if (!fnRelPtr) continue;
5917

6018
auto clonedFn = static_cast<IReflectedFunction*>(fnRelPtr->Clone().release());
61-
6219
std::unique_ptr<IReflectedFunction> boundFn;
6320
boundFn.reset(clonedFn);
64-
21+
6522
e.Bind(std::move(boundFn));
6623
}
6724

68-
return std::nullopt;
69-
}
25+
return e;
26+
}
Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,15 @@
11
#pragma once
2-
#include "../Reflection/Event.hpp"
3-
4-
namespace Engine3DRadSpace::Objects
5-
{
6-
class ObjectList;
7-
}
2+
#include "EventInvocationRepresentation.hpp"
83

94
namespace Engine3DRadSpace::Projects
105
{
11-
/// <summary>
12-
/// Serializable representation of a member function call.
13-
/// </summary>
14-
struct E3DRSP_PROJECTS_EXPORT EventInvocationRepresentation
15-
{
16-
/// <summary>
17-
/// ID of the object in the ObjectList.
18-
/// </summary>
19-
size_t OwnerObject;
20-
/// <summary>
21-
/// ID of the function, per reflection metadata.
22-
/// </summary>
23-
size_t FunctionID;
24-
/// <summary>
25-
/// Arguments pack.
26-
/// </summary>
27-
std::vector<std::any> Args;
28-
29-
EventInvocationRepresentation();
30-
31-
std::any Invoke(Objects::ObjectList* list);
32-
std::any Invoke(Objects::IObject* obj);
33-
34-
static Reflection::IReflectedFunction* FindFunction(Objects::IObject* obj, size_t idxFn);
35-
};
36-
376
/// <summary>
387
/// Serializable representation of an <see cref="Event"/>.
398
/// </summary>
409
struct E3DRSP_PROJECTS_EXPORT EventRepresentation
4110
{
4211
std::vector<EventInvocationRepresentation> BoundFunctions;
4312

44-
std::optional<Reflection::Event> Reconstruct(Objects::ObjectList* list) const;
13+
std::optional<Reflection::Event> Reconstruct(const Reflection::UUID &uuid) const;
4514
};
4615
}

3DRadSpace/Engine3DRadSpace/Projects/Serialization.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "Serialization.hpp"
2+
#include "Serialization.hpp"
3+
#include "Serialization.hpp"
24
#include "../Content/AssetID.hpp"
35
#include "../Objects/Impl/Objects.hpp"
46
#include "../Objects/ObjectList.hpp"
@@ -10,6 +12,7 @@ using namespace Engine3DRadSpace::Graphics;
1012
using namespace Engine3DRadSpace::Internal;
1113
using namespace Engine3DRadSpace::Math;
1214
using namespace Engine3DRadSpace::Objects;
15+
using namespace Engine3DRadSpace::Projects;
1316
using namespace Engine3DRadSpace::Reflection;
1417

1518
static void deserializeObjectHierarchy(ObjectList* lst, IObject* obj, const json& j)
@@ -255,6 +258,14 @@ json Engine3DRadSpace::Projects::Serializer::SerializeObject(IObject* obj)
255258
offset += sizeof(Color);
256259
break;
257260
}
261+
case FieldRepresentationType::Event:
262+
{
263+
Projects::EventRepresentation e;
264+
jsonField[subFieldName][str_i] = e;
265+
266+
offset += sizeof(Event);
267+
break;
268+
}
258269
case FieldRepresentationType::ObjectID:
259270
{
260271
size_t id = field->GetAtOffset<size_t>(objPtr, offset);
@@ -474,6 +485,13 @@ json Engine3DRadSpace::Projects::Serializer::SerializeObject(IObject* obj)
474485
}
475486
case FieldRepresentationType::Event:
476487
{
488+
EventRepresentation eventData = jsonField.get<EventRepresentation>();
489+
Event event = eventData.Reconstruct(uuid).value();
490+
491+
void* dest = newStruct.get() + offset;
492+
new (dest) Event();
493+
offset += sizeof(Event);
494+
477495
break;
478496
}
479497
case FieldRepresentationType::ObjectID:
@@ -583,4 +601,49 @@ bool Engine3DRadSpace::Projects::Serializer::SaveProject(ObjectList* lst, Conten
583601

584602
file << std::setw(4) << j;
585603
return true;
586-
}
604+
}
605+
606+
void Engine3DRadSpace::Projects::to_json(json& j, const EventRepresentation& event)
607+
{
608+
auto size = event.BoundFunctions.size();
609+
j["numFn"] = size;
610+
611+
for (auto i = 0; i < size; i++)
612+
{
613+
auto& fn = event.BoundFunctions[i];
614+
json jFn;
615+
616+
jFn["OwnerObject"] = fn.OwnerObject;
617+
jFn["FunctionID"] = fn.FunctionID;
618+
619+
json jArgs = json::array();
620+
for (auto& arg : fn.Args)
621+
{
622+
jArgs.push_back(arg.type().name());
623+
}
624+
625+
jFn["Args"] = jArgs;
626+
j["BoundFunctions"].push_back(jFn);
627+
}
628+
}
629+
630+
void Engine3DRadSpace::Projects::from_json(const json& j, EventRepresentation& event)
631+
{
632+
auto size = j["numFn"].get<size_t>();
633+
634+
for (auto i = 0; i < size; i++)
635+
{
636+
auto& jFn = j["BoundFunctions"][i];
637+
EventInvocationRepresentation fn;
638+
639+
fn.OwnerObject = jFn["OwnerObject"].get<size_t>();
640+
fn.FunctionID = jFn["FunctionID"].get<size_t>();
641+
for (auto& jArg : jFn["Args"])
642+
{
643+
std::string typeName = jArg.get<std::string>();
644+
fn.Args.push_back(std::any(typeName));
645+
}
646+
647+
event.BoundFunctions.push_back(fn);
648+
}
649+
}

3DRadSpace/Engine3DRadSpace/Projects/Serialization.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include "../Reflection/Reflection.hpp"
33
#include "../Games/Game.hpp"
4+
#include "EventRepresentation.hpp"
45
#include <nlohmann/json.hpp>
56

67
using json = nlohmann::json;
@@ -56,4 +57,7 @@ namespace Engine3DRadSpace::Projects
5657
/// <returns>True if succeded.</returns>
5758
static bool SaveProject(Objects::ObjectList* lst, Content::ContentManager* content, const std::filesystem::path& projectPath);
5859
};
60+
61+
void to_json(json& j, const EventRepresentation& event);
62+
void from_json(const json& j, EventRepresentation& event);
5963
}

0 commit comments

Comments
 (0)