Skip to content

Commit

Permalink
refactor(serialization): move to old namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 28, 2023
1 parent 7ad2603 commit 0d5fe48
Show file tree
Hide file tree
Showing 75 changed files with 253 additions and 244 deletions.
4 changes: 2 additions & 2 deletions core/include/cubos/core/data/old/binary_deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <cubos/core/data/old/deserializer.hpp>
#include <cubos/core/memory/stream.hpp>

namespace cubos::core::data
namespace cubos::core::data::old
{
/// Implementation of the abstract Deserializer class for deserializing from raw binary data.
/// This class allows data to be deserialized from both little and big endian formats.
Expand Down Expand Up @@ -39,4 +39,4 @@ namespace cubos::core::data
memory::Stream& mStream; ///< The stream to serialize from.
bool mReadLittleEndian; ///< Whether to write in little endian or big endian format.
};
} // namespace cubos::core::data
} // namespace cubos::core::data::old
4 changes: 2 additions & 2 deletions core/include/cubos/core/data/old/binary_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <cubos/core/data/old/serializer.hpp>
#include <cubos/core/memory/stream.hpp>

namespace cubos::core::data
namespace cubos::core::data::old
{
/// Implementation of the abstract Serializer class for serializing to binary data.
/// This class allows data to be serialized in both little and big endian formats.
Expand Down Expand Up @@ -40,4 +40,4 @@ namespace cubos::core::data
memory::Stream& mStream; ///< The stream to serialize to.
bool mWriteLittleEndian; ///< Whether to write in little endian or big endian format.
};
} // namespace cubos::core::data
} // namespace cubos::core::data::old
4 changes: 2 additions & 2 deletions core/include/cubos/core/data/old/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <typeindex>
#include <vector>

namespace cubos::core::data
namespace cubos::core::data::old
{
/// Stores the context necessary to serialize or deserialize data.
/// This is done through a type map, which stores an instance for each type added (akin to the
Expand Down Expand Up @@ -83,4 +83,4 @@ namespace cubos::core::data

std::vector<Entry> mEntries; ///< Entries in the context.
};
} // namespace cubos::core::data
} // namespace cubos::core::data::old
10 changes: 5 additions & 5 deletions core/include/cubos/core/data/old/debug_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <cubos/core/data/old/serializer.hpp>
#include <cubos/core/memory/buffer_stream.hpp>

namespace cubos::core::data
namespace cubos::core::data::old
{
/// Used to wrap a value to be printed using the debug serializer.
/// Can be pretty-printed using the `p` option. Types can be printed using the `t` option.
Expand Down Expand Up @@ -89,13 +89,13 @@ namespace cubos::core::data
bool mPretty; ///< Whether to pretty-print the output.
bool mTypeNames; ///< Whether to print the type names.
};
} // namespace cubos::core::data
} // namespace cubos::core::data::old

// Add a formatter for Debug<T>.

/// @cond
template <typename T>
struct fmt::formatter<cubos::core::data::Debug<T>> : formatter<string_view>
struct fmt::formatter<cubos::core::data::old::Debug<T>> : formatter<string_view>
{
bool pretty = false; ///< Whether to pretty print the data.
bool types = false; ///< Whether to print the type name.
Expand Down Expand Up @@ -128,10 +128,10 @@ struct fmt::formatter<cubos::core::data::Debug<T>> : formatter<string_view>
}

template <typename FormatContext>
inline auto format(const cubos::core::data::Debug<T>& dbg, FormatContext& ctx) -> decltype(ctx.out())
inline auto format(const cubos::core::data::old::Debug<T>& dbg, FormatContext& ctx) -> decltype(ctx.out())
{
auto stream = cubos::core::memory::BufferStream(32);
cubos::core::data::DebugSerializer serializer(stream, this->pretty, this->types);
cubos::core::data::old::DebugSerializer serializer(stream, this->pretty, this->types);
serializer.write(dbg.value, nullptr);
stream.put('\0');
// Skip the '?: ' prefix.
Expand Down
10 changes: 5 additions & 5 deletions core/include/cubos/core/data/old/deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <cubos/core/data/old/context.hpp>

namespace cubos::core::data
namespace cubos::core::data::old
{
class Deserializer;

Expand All @@ -25,15 +25,15 @@ namespace cubos::core::data
/// deserialization methods: `readI8`, `readString`, etc.
///
/// @details More complex types can be deserialized by implementing a specialization of the
/// `cubos::core::data::deserialize` function:
/// `cubos::core::data::old::deserialize` function:
///
/// struct MyType
/// {
/// int32_t a;
/// };
///
/// // In the corresponding .cpp file.
/// void cubos::core::data::deserialize<MyType>(Deserializer& d, MyType& obj)
/// void cubos::core::data::old::deserialize<MyType>(Deserializer& d, MyType& obj)
/// {
/// d.read(obj.a, "a");
/// }
Expand Down Expand Up @@ -118,7 +118,7 @@ namespace cubos::core::data
virtual void readString(std::string& value) = 0;

/// Deserializes an object.
/// The `cubos::core::data::deserialize` function must be implemented for the given type.
/// The `cubos::core::data::old::deserialize` function must be implemented for the given type.
/// The fail bit is set if the deserialization fails.
/// @tparam T The type of the object.
/// @param obj The object to deserialize.
Expand Down Expand Up @@ -247,4 +247,4 @@ namespace cubos::core::data
des.read(obj.second);
des.endObject();
}
} // namespace cubos::core::data
} // namespace cubos::core::data::old
4 changes: 2 additions & 2 deletions core/include/cubos/core/data/old/json_deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <cubos/core/data/old/deserializer.hpp>

namespace cubos::core::data
namespace cubos::core::data::old
{
/// Implementation of the abstract Deserializer class for deserializing from JSON.
class JSONDeserializer : public Deserializer
Expand Down Expand Up @@ -56,4 +56,4 @@ namespace cubos::core::data
std::stack<Frame> mFrame; ///< The current frame of the deserializer.
nlohmann::ordered_json mJson; ///< The current JSON value being read.
};
} // namespace cubos::core::data
} // namespace cubos::core::data::old
4 changes: 2 additions & 2 deletions core/include/cubos/core/data/old/json_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <cubos/core/data/old/serializer.hpp>
#include <cubos/core/memory/stream.hpp>

namespace cubos::core::data
namespace cubos::core::data::old
{
/// Implementation of the abstract Serializer class for serializing to JSON.
/// Each time a top-level primitive/object/array/dictionary is written, its JSON output is written to the underlying
Expand Down Expand Up @@ -64,4 +64,4 @@ namespace cubos::core::data
std::stack<Frame> mFrames; ///< The stack of frames.
int mIndent; ///< The indentation of the JSON output.
};
} // namespace cubos::core::data
} // namespace cubos::core::data::old
4 changes: 2 additions & 2 deletions core/include/cubos/core/data/old/package.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <cubos/core/data/old/serializer.hpp>
#include <cubos/core/log.hpp>

namespace cubos::core::data
namespace cubos::core::data::old
{
namespace impl
{
Expand Down Expand Up @@ -382,4 +382,4 @@ namespace cubos::core::data
return !unpackager.failed();
}

} // namespace cubos::core::data
} // namespace cubos::core::data::old
4 changes: 2 additions & 2 deletions core/include/cubos/core/data/old/serialization_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <cubos/core/log.hpp>

namespace cubos::core::data
namespace cubos::core::data::old
{
/// Class used to map between references and their serialized identifiers.
/// @tparam R Reference type.
Expand Down Expand Up @@ -133,4 +133,4 @@ namespace cubos::core::data
std::unordered_map<R, I> mRefToId; ///< Map of references to serialized IDs.
std::unordered_map<I, R> mIdToRef; ///< Map of serialized IDs to references.
};
} // namespace cubos::core::data
} // namespace cubos::core::data::old
10 changes: 5 additions & 5 deletions core/include/cubos/core/data/old/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <cubos/core/data/old/context.hpp>

namespace cubos::core::data
namespace cubos::core::data::old
{
class Serializer;

Expand All @@ -26,15 +26,15 @@ namespace cubos::core::data
/// serialization methods: `writeI8`, `writeString`, etc.
///
/// @details More complex types can be serialized by implementing a specialization of the
/// `cubos::core::data::serialize` function:
/// `cubos::core::data::old::serialize` function:
///
/// struct MyType
/// {
/// int32_t a;
/// };
///
/// // In the corresponding .cpp file.
/// void cubos::core::data::serialize<MyType>(Serializer& s, const MyType& obj, const char* name)
/// void cubos::core::data::old::serialize<MyType>(Serializer& s, const MyType& obj, const char* name)
/// {
/// s.write(obj.a, "a");
/// }
Expand Down Expand Up @@ -123,7 +123,7 @@ namespace cubos::core::data
virtual void writeString(const char* str, const char* name) = 0;

/// Serializes an object.
/// The `cubos::core::data::serialize` function must be implemented for the given type.
/// The `cubos::core::data::old::serialize` function must be implemented for the given type.
/// @tparam T The type of the object.
/// @param obj The object to serialize.
/// @param name The name of the object (optional).
Expand Down Expand Up @@ -256,4 +256,4 @@ namespace cubos::core::data
ser.write(obj.second, "second");
ser.endObject();
}
} // namespace cubos::core::data
} // namespace cubos::core::data::old
25 changes: 13 additions & 12 deletions core/include/cubos/core/ecs/blueprint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace cubos::core::ecs
/// @param entity Entity identifier.
/// @param name Component type name.
/// @param deserializer Ddeserializer to deserialize from.
bool addFromDeserializer(Entity entity, const std::string& name, data::Deserializer& deserializer);
bool addFromDeserializer(Entity entity, const std::string& name, data::old::Deserializer& deserializer);

/// @brief Returns an entity from its name.
/// @param name Entity name.
Expand Down Expand Up @@ -85,14 +85,15 @@ namespace cubos::core::ecs
/// @brief Adds all of the components stored in the buffer to the specified commands object.
/// @param commands Commands object to add the components to.
/// @param context Context to use when deserializing the components.
virtual void addAll(CommandBuffer& commands, data::Context& context) = 0;
virtual void addAll(CommandBuffer& commands, data::old::Context& context) = 0;

/// @brief Merges the data of another buffer of the same type into this one.
/// @param other Buffer to merge from.
/// @param prefix Prefix to add to the names of the components of the other buffer.
/// @param src Context to use when serializing the components from the other buffer.
/// @param dst Context to use when deserializing the components to this buffer.
virtual void merge(IBuffer* other, const std::string& prefix, data::Context& src, data::Context& dst) = 0;
virtual void merge(IBuffer* other, const std::string& prefix, data::old::Context& src,
data::old::Context& dst) = 0;

/// @brief Creates a new buffer of the same type as this one.
/// @return New buffer.
Expand All @@ -106,15 +107,15 @@ namespace cubos::core::ecs
{
// Interface methods implementation.

inline void addAll(CommandBuffer& commands, data::Context& context) override
inline void addAll(CommandBuffer& commands, data::old::Context& context) override
{
this->mutex.lock();
auto pos = this->stream.tell();
this->stream.seek(0, memory::SeekOrigin::Begin);
auto des = data::BinaryDeserializer(this->stream);
auto des = data::old::BinaryDeserializer(this->stream);
des.context().pushSubContext(context);

auto& map = des.context().get<data::SerializationMap<Entity, std::string>>();
auto& map = des.context().get<data::old::SerializationMap<Entity, std::string>>();
for (const auto& name : this->names)
{
ComponentType type;
Expand All @@ -131,16 +132,16 @@ namespace cubos::core::ecs
}
}

inline void merge(IBuffer* other, const std::string& prefix, data::Context& src,
data::Context& dst) override
inline void merge(IBuffer* other, const std::string& prefix, data::old::Context& src,
data::old::Context& dst) override
{
auto buffer = static_cast<Buffer<ComponentType>*>(other);

buffer->mutex.lock();
auto pos = buffer->stream.tell();
buffer->stream.seek(0, memory::SeekOrigin::Begin);
auto ser = data::BinarySerializer(this->stream);
auto des = data::BinaryDeserializer(buffer->stream);
auto ser = data::old::BinarySerializer(this->stream);
auto des = data::old::BinaryDeserializer(buffer->stream);
ser.context().pushSubContext(dst);
des.context().pushSubContext(src);
for (const auto& name : buffer->names)
Expand All @@ -164,7 +165,7 @@ namespace cubos::core::ecs
};

/// @brief Stores the entity handles and the associated names.
data::SerializationMap<Entity, std::string> mMap;
data::old::SerializationMap<Entity, std::string> mMap;

/// @brief Buffers which store the serialized components of each type in this blueprint.
memory::TypeMap<IBuffer*> mBuffers;
Expand Down Expand Up @@ -201,7 +202,7 @@ namespace cubos::core::ecs
buf = *ptr;
}

auto ser = data::BinarySerializer(buf->stream);
auto ser = data::old::BinarySerializer(buf->stream);
ser.context().push(mMap);
ser.write(components, "data");
buf->names.push_back(mMap.getId(entity));
Expand Down
6 changes: 3 additions & 3 deletions core/include/cubos/core/ecs/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ namespace cubos::core::ecs
private:
friend CommandBuffer;

data::SerializationMap<Entity, std::string> mMap; ///< Maps entity names to the instantiated entities.
CommandBuffer& mCommands; ///< Commands object that created this entity.
data::old::SerializationMap<Entity, std::string> mMap; ///< Maps entity names to the instantiated entities.
CommandBuffer& mCommands; ///< Commands object that created this entity.

/// @brief Constructs.
/// @param map Map of entity names to the instantiated entities.
/// @param commands Commands object that created this entity.
BlueprintBuilder(data::SerializationMap<Entity, std::string>&& map, CommandBuffer& commands);
BlueprintBuilder(data::old::SerializationMap<Entity, std::string>&& map, CommandBuffer& commands);
};

/// @brief Used to write ECS commands and execute them at a later time.
Expand Down
5 changes: 3 additions & 2 deletions core/include/cubos/core/ecs/component_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,16 @@ namespace cubos::core::ecs
/// @param componentId Component identifier.
/// @param context Optional context to use for serialization.
/// @return Package containing the component.
data::Package pack(uint32_t id, std::size_t componentId, data::Context* context) const;
data::old::Package pack(uint32_t id, std::size_t componentId, data::old::Context* context) const;

/// @brief Inserts a component into an entity, by unpacking a package.
/// @param id Entity index.
/// @param componentId Component identifier.
/// @param package Package to unpack.
/// @param context Optional context to use for deserialization.
/// @return Whether the unpacking was successful.
bool unpack(uint32_t id, std::size_t componentId, const data::Package& package, data::Context* context);
bool unpack(uint32_t id, std::size_t componentId, const data::old::Package& package,
data::old::Context* context);

private:
struct Entry
Expand Down
2 changes: 1 addition & 1 deletion core/include/cubos/core/ecs/entity_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace cubos::core::ecs
/// @brief Identifies an entity.
///
/// When serializing/deserializing, if there's a
/// data::SerializationMap<Entity, std::string> in the context, it will be used to
/// data::old::SerializationMap<Entity, std::string> in the context, it will be used to
/// (de)serialize strings representing the entities. Otherwise, the identifiers will be
/// (de)serialized as objects with two fields: their index and their generation.
///
Expand Down
6 changes: 3 additions & 3 deletions core/include/cubos/core/ecs/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace cubos::core::ecs
/// @param blueprint Blueprint to instantiate the component into.
/// @param id Blueprint entity to add the component to.
/// @return Whether the instantiation was successful.
static bool create(std::string_view name, data::Deserializer& des, Blueprint& blueprint, Entity id);
static bool create(std::string_view name, data::old::Deserializer& des, Blueprint& blueprint, Entity id);

/// @brief Instantiates a component storage for the given component type.
/// @param type Type index of the component.
Expand Down Expand Up @@ -63,7 +63,7 @@ namespace cubos::core::ecs
std::string name; ///< Name of the component.

/// Function for creating the component from a deserializer.
bool (*componentCreator)(data::Deserializer&, Blueprint&, Entity);
bool (*componentCreator)(data::old::Deserializer&, Blueprint&, Entity);

/// Function for creating the storage for the component.
std::unique_ptr<IStorage> (*storageCreator)();
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace cubos::core::ecs
.type = typeid(T),
.name = std::string(name),
.componentCreator =
[](data::Deserializer& des, Blueprint& blueprint, Entity id) {
[](data::old::Deserializer& des, Blueprint& blueprint, Entity id) {
T comp;
des.read(comp);
if (des.failed())
Expand Down
Loading

0 comments on commit 0d5fe48

Please sign in to comment.