Skip to content

Commit 3b2bc61

Browse files
committed
ContainersSerialization: Remove Serialization dependency on Containers
1 parent 03852af commit 3b2bc61

30 files changed

+496
-290
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@page library_containers_serialization Containers Serialization
2+
3+
@brief 🟨 Containers specializations for [Reflection](@ref library_reflection) and Serialization.
4+
5+
[TOC]
6+
7+
This is a support library holding all partial specializations for [Reflection](@ref library_reflection), [Serialization Binary](@ref library_serialization_binary) and [Serialization Text](@ref library_serialization_text).
8+
Its headers are only meant to be included anywhere the reflection / serialization systems are being used so that both systems "know" how to handle things like `SC::Vector` or `SC::String`.
9+
10+
@note The reason for this library to exist is only to allow [Reflection](@ref library_reflection), [Serialization Binary](@ref library_serialization_binary) and [Serialization Text](@ref library_serialization_text) libraries not to be depending on [Containers](@ref library_containers) and [Memory](@ref library_memory).
11+
12+
# Dependencies
13+
- Dependencies: [Containers](@ref library_containers), [Reflection](@ref library_reflection)
14+
- All dependencies: [Containers](@ref library_containers), [Foundation](@ref library_foundation), [Memory](@ref library_memory), [Reflection](@ref library_reflection)
15+
16+
# Statistics
17+
| Type | Lines Of Code | Comments | Sum |
18+
|-----------|---------------|-----------|-------|
19+
| Headers | 97 | 129 | 226 |
20+
| Sources | 639 | 106 | 745 |
21+
| Sum | 736 | 235 | 971 |

Documentation/Libraries/Reflection.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
@copydetails group_reflection
88

9+
@note You need to include headers from [Serialization Adapters](@ref library_containers_serialization) in order to use `SC::Vector` or `SC::String` or any other reflect-able class provided by other libraries with this one.
10+
911
# Dependencies
1012
- Direct dependencies: [Containers](@ref library_containers), [Foundation](@ref library_foundation), [Memory](@ref library_memory)
1113
- All dependencies: [Containers](@ref library_containers), [Foundation](@ref library_foundation), [Memory](@ref library_memory)

Documentation/Libraries/SerializationBinary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
@copydetails group_serialization_binary
88

9+
@note You need to include headers from [Serialization Adapters](@ref library_containers_serialization) in order to use `SC::Vector` or `SC::String` or any other serializable class provided by other libraries with this one.
10+
911
# Dependencies
1012
- Direct dependencies: [Foundation](@ref library_foundation), [Memory](@ref library_memory), [Reflection](@ref library_reflection)
1113
- All dependencies: [Containers](@ref library_containers), [Foundation](@ref library_foundation), [Memory](@ref library_memory), [Reflection](@ref library_reflection)

Documentation/Libraries/SerializationText.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
@copydetails group_serialization_text
88

9+
@note You need to include headers from [Serialization Adapters](@ref library_containers_serialization) in order to use `SC::Vector` or `SC::String` or any other serializable class provided by other libraries with this one.
10+
911
# Dependencies
1012
- Direct dependencies: [Foundation](@ref library_foundation), [Memory](@ref library_memory), [Reflection](@ref library_reflection), [Strings](@ref library_strings)
1113
- All dependencies: [Containers](@ref library_containers), [Foundation](@ref library_foundation), [Memory](@ref library_memory), [Reflection](@ref library_reflection), [Strings](@ref library_strings)

Documentation/Pages/Libraries.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Library | Description
77
@subpage library_async | @copybrief library_async | 5726
88
@subpage library_async_streams | @copybrief library_async_streams | 1977
99
@subpage library_containers | @copybrief library_containers | 874
10+
@subpage library_containers_serialization | @copybrief library_containers_serialization | 0
1011
@subpage library_file | @copybrief library_file | 736
1112
@subpage library_file_system | @copybrief library_file_system | 1332
1213
@subpage library_file_system_iterator | @copybrief library_file_system_iterator | 428

Examples/SCExample/Examples/SerializationExample/SerializationExample.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ struct SerializationTextReadVersioned<TextStream, ImVector<T>> : public Serializ
8989
} // namespace Serialization
9090
} // namespace SC
9191

92+
#include "Libraries/ContainersSerialization/ContainersSerialization.h"
93+
#include "Libraries/ContainersSerialization/MemorySerialization.h"
9294
#include "Libraries/FileSystem/FileSystem.h"
9395
#include "Libraries/Memory/String.h"
9496
#include "Libraries/Plugin/PluginMacros.h"

Examples/SCExample/Examples/WebServerExample/WebServerExample.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "../ImguiHelpers.h"
1616
#include "imgui.h"
1717

18+
#include "Libraries/ContainersSerialization/ContainersSerialization.h"
19+
#include "Libraries/ContainersSerialization/MemorySerialization.h"
1820
#include "Libraries/Http/HttpWebServer.h"
1921
#include "Libraries/Plugin/PluginMacros.h"
2022
#include "Libraries/SerializationBinary/SerializationBinary.h"
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) Stefano Cristiano
2+
// SPDX-License-Identifier: MIT
3+
#pragma once
4+
#include "../Containers/Array.h"
5+
#include "../Containers/Vector.h"
6+
#include "../Containers/VectorMap.h"
7+
#include "../Reflection/Reflection.h"
8+
9+
namespace SC
10+
{
11+
namespace Reflection
12+
{
13+
// Forward Declarations (to avoid depending on Reflection)
14+
template <typename T, typename SFINAESelector>
15+
struct ExtendedTypeInfo;
16+
17+
template <typename T>
18+
struct Reflect;
19+
20+
template <typename T, int N>
21+
struct ExtendedTypeInfo<SC::Array<T, N>, void>
22+
{
23+
static constexpr bool IsPacked = false;
24+
25+
static auto size(const SC::Array<T, N>& object) { return object.size(); }
26+
static auto data(SC::Array<T, N>& object) { return object.data(); }
27+
static bool resizeWithoutInitializing(SC::Array<T, N>& object, size_t newSize)
28+
{
29+
return object.resizeWithoutInitializing(min(newSize, static_cast<size_t>(N)));
30+
}
31+
static bool resize(SC::Array<T, N>& object, size_t newSize)
32+
{
33+
return object.resize(min(newSize, static_cast<size_t>(N)));
34+
}
35+
};
36+
37+
template <typename T, int N>
38+
struct Reflect<SC::Array<T, N>>
39+
{
40+
static constexpr TypeCategory getCategory() { return TypeCategory::TypeVector; }
41+
42+
template <typename MemberVisitor>
43+
static constexpr bool build(MemberVisitor& builder)
44+
{
45+
// TODO: Figure out a way to get rid of calling VectorArrayVTable here
46+
if (not VectorArrayVTable<MemberVisitor, SC::Array<T, N>, T, N>::build(builder))
47+
return false;
48+
49+
// Add Array type
50+
constexpr TypeInfo::ArrayInfo arrayInfo = {false, N}; // false == not packed
51+
if (not builder.addType(MemberVisitor::Type::template createArray<SC::Array<T, N>>("SC::Array", 1, arrayInfo)))
52+
return false;
53+
54+
// Add dependent item type
55+
return builder.addType(MemberVisitor::Type::template createGeneric<T>());
56+
}
57+
};
58+
59+
template <typename T>
60+
struct Reflect<SC::Vector<T>>
61+
{
62+
static constexpr TypeCategory getCategory() { return TypeCategory::TypeVector; }
63+
64+
template <typename MemberVisitor>
65+
static constexpr bool build(MemberVisitor& builder)
66+
{
67+
// TODO: Figure out a way to get rid of calling VectorArrayVTable here
68+
if (not VectorArrayVTable<MemberVisitor, SC::Vector<T>, T, -1>::build(builder))
69+
return false;
70+
71+
// Add Vector type
72+
constexpr TypeInfo::ArrayInfo arrayInfo = {false, 0}; // false == not packed
73+
if (not builder.addType(MemberVisitor::Type::template createArray<SC::Vector<T>>("SC::Vector", 1, arrayInfo)))
74+
return false;
75+
76+
// Add dependent item type
77+
return builder.addType(MemberVisitor::Type::template createGeneric<T>());
78+
}
79+
};
80+
81+
template <typename T>
82+
struct ExtendedTypeInfo<SC::Vector<T>, void>
83+
{
84+
static constexpr bool IsPacked = false;
85+
86+
static auto size(const SC::Vector<T>& object) { return object.size(); }
87+
static auto data(SC::Vector<T>& object) { return object.data(); }
88+
static bool resizeWithoutInitializing(SC::Vector<T>& object, size_t newSize)
89+
{
90+
return object.resizeWithoutInitializing(newSize);
91+
}
92+
static bool resize(SC::Vector<T>& object, size_t newSize) { return object.resize(newSize); }
93+
};
94+
95+
template <typename Key, typename Value, typename Container>
96+
struct Reflect<VectorMap<Key, Value, Container>> : ReflectStruct<VectorMap<Key, Value, Container>>
97+
{
98+
using T = typename SC::VectorMap<Key, Value, Container>;
99+
100+
template <typename MemberVisitor>
101+
static constexpr bool visit(MemberVisitor&& builder)
102+
{
103+
return builder(0, "items", &T::items, SC_COMPILER_OFFSETOF(T, items));
104+
}
105+
};
106+
107+
} // namespace Reflection
108+
} // namespace SC
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Stefano Cristiano
2+
// SPDX-License-Identifier: MIT
3+
#pragma once
4+
#include "../ContainersSerialization/ContainersReflection.h"
5+
6+
namespace SC
7+
{
8+
namespace Serialization
9+
{
10+
11+
// Forward Declarations (to avoid depending on SerializationBinary)
12+
template <typename BinaryStream, typename T, typename SFINAESelector>
13+
struct SerializerBinaryReadWriteExact;
14+
template <typename BinaryStream, typename Container, typename T>
15+
struct SerializerBinaryExactVector;
16+
template <typename BinaryStream, typename T, typename SFINAESelector>
17+
struct SerializerBinaryReadVersioned;
18+
template <typename BinaryStream, typename Container, typename T, size_t NumMaxItems>
19+
struct SerializationBinaryVersionedVector;
20+
21+
// Forward Declarations (to avoid depending on SerializationText)
22+
template <typename SerializerStream, typename Container, typename T>
23+
struct SerializationTextVersionedVector;
24+
template <typename TextStream, typename T, typename SFINAESelector>
25+
struct SerializationTextReadWriteExact;
26+
template <typename TextStream, typename Container, typename T>
27+
struct SerializationTextExactVector;
28+
template <typename SerializerStream, typename T, typename SFINAESelector>
29+
struct SerializationTextReadVersioned;
30+
31+
// clang-format off
32+
/// @brief Specialization for `SC::Vector<T>` types
33+
template <typename BinaryStream, typename T>
34+
struct SerializerBinaryReadWriteExact<BinaryStream, SC::Vector<T>, void> : public SerializerBinaryExactVector<BinaryStream, SC::Vector<T>, T> { };
35+
36+
/// @brief Specialization for `SC::Array<T, N>` types
37+
template <typename BinaryStream, typename T, int N>
38+
struct SerializerBinaryReadWriteExact<BinaryStream, SC::Array<T, N>, void> : public SerializerBinaryExactVector<BinaryStream, SC::Array<T, N>, T> { };
39+
40+
template <typename BinaryStream, typename T>
41+
struct SerializerBinaryReadVersioned<BinaryStream, SC::Vector<T>, void> : public SerializationBinaryVersionedVector<BinaryStream, SC::Vector<T>, T, 0xffffffff> { };
42+
43+
template <typename BinaryStream, typename T, int N>
44+
struct SerializerBinaryReadVersioned<BinaryStream, SC::Array<T, N>, void> : public SerializationBinaryVersionedVector<BinaryStream, SC::Array<T, N>, T, N> { };
45+
46+
47+
48+
template <typename TextStream, typename T>
49+
struct SerializationTextReadWriteExact<TextStream, SC::Vector<T>, void>
50+
: public SerializationTextExactVector<TextStream, SC::Vector<T>, T>
51+
{
52+
};
53+
54+
template <typename TextStream, typename T, int N>
55+
struct SerializationTextReadWriteExact<TextStream, SC::Array<T, N>, void>
56+
: public SerializationTextExactVector<TextStream, SC::Array<T, N>, T>
57+
{
58+
};
59+
60+
61+
62+
template <typename SerializerStream, typename T>
63+
struct SerializationTextReadVersioned<SerializerStream, SC::Vector<T>, void>
64+
: public SerializationTextVersionedVector<SerializerStream, SC::Vector<T>, T>
65+
{
66+
};
67+
68+
template <typename SerializerStream, typename T, int N>
69+
struct SerializationTextReadVersioned<SerializerStream, SC::Array<T, N>, void>
70+
: public SerializationTextVersionedVector<SerializerStream, SC::Array<T, N>, T>
71+
{
72+
};
73+
74+
// clang-format on
75+
} // namespace Serialization
76+
} // namespace SC
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) Stefano Cristiano
2+
// SPDX-License-Identifier: MIT
3+
#pragma once
4+
#include "../Memory/Buffer.h"
5+
#include "../Memory/String.h"
6+
#include "../Reflection/Reflection.h"
7+
8+
namespace SC
9+
{
10+
namespace Reflection
11+
{
12+
// Forward Declarations (to avoid depending on Reflection)
13+
template <typename T, typename SFINAESelector>
14+
struct ExtendedTypeInfo;
15+
16+
template <typename T>
17+
struct Reflect;
18+
19+
template <>
20+
struct Reflect<SC::Buffer>
21+
{
22+
static constexpr TypeCategory getCategory() { return TypeCategory::TypeVector; }
23+
24+
template <typename MemberVisitor>
25+
static constexpr bool build(MemberVisitor& builder)
26+
{
27+
// TODO: Figure out a way to get rid of calling VectorArrayVTable here
28+
if (not VectorArrayVTable<MemberVisitor, SC::Buffer, char, -1>::build(builder))
29+
return false;
30+
31+
// Add Vector type
32+
constexpr TypeInfo::ArrayInfo arrayInfo = {false, 0}; // false == not packed
33+
if (not builder.addType(MemberVisitor::Type::template createArray<SC::Buffer>("SC::Buffer", 1, arrayInfo)))
34+
return false;
35+
36+
// Add dependent item type
37+
return builder.addType(MemberVisitor::Type::template createGeneric<char>());
38+
}
39+
};
40+
template <>
41+
struct ExtendedTypeInfo<SC::Buffer, void>
42+
{
43+
static constexpr bool IsPacked = false;
44+
45+
static auto size(const Buffer& object) { return object.size(); }
46+
static auto data(Buffer& object) { return object.data(); }
47+
static bool resizeWithoutInitializing(Buffer& object, size_t newSize)
48+
{
49+
return object.resizeWithoutInitializing(newSize);
50+
}
51+
static bool resize(Buffer& object, size_t newSize) { return object.resize(newSize, 0); }
52+
};
53+
54+
// TODO: Rethink if enumerations should not be collapsed to their underlying primitive type
55+
template <>
56+
struct Reflect<SC::StringEncoding> : Reflect<uint8_t>
57+
{
58+
static_assert(sizeof(SC::StringEncoding) == sizeof(uint8_t), "size");
59+
};
60+
61+
} // namespace Reflection
62+
} // namespace SC
63+
64+
SC_REFLECT_STRUCT_VISIT(SC::String)
65+
SC_REFLECT_STRUCT_FIELD(0, encoding) // TODO: Maybe encoding should be merged in data header
66+
SC_REFLECT_STRUCT_FIELD(1, data)
67+
SC_REFLECT_STRUCT_LEAVE()

0 commit comments

Comments
 (0)