Skip to content

Commit

Permalink
xrGame: Refactor alife_registry_container's helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
Zegeri committed Sep 16, 2018
1 parent 038e76c commit cace3a3
Showing 1 changed file with 19 additions and 58 deletions.
77 changes: 19 additions & 58 deletions src/xrGame/alife_registry_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,74 +12,35 @@
#include "alife_space.h"
#include "Common/object_type_traits.h"

template <typename TContainer, typename TList>
class RegistryHelper
{
private:
ASSERT_TYPELIST(TList);
using Head = typename TList::Head;
using Tail = typename TList::Tail;

static const bool isSerializable = object_type_traits::is_base_and_derived<ISerializable, Head>::value;

template <bool serializable>
static void LoadHead(TContainer* self, IReader& reader)
{
}

template <>
static void LoadHead<true>(TContainer* self, IReader& reader)
{
self->Head::load(reader);
}
template <typename, typename>
struct RegistryHelper;

template <bool serializable>
static void SaveHead(TContainer* self, IWriter& writer)
{
}

template <>
static void SaveHead<true>(TContainer* self, IWriter& writer)
{
self->Head::save(writer);
}

static void LoadHead(TContainer* self, IReader& reader) { LoadHead<isSerializable>(self, reader); }
static void SaveHead(TContainer* self, IWriter& writer) { SaveHead<isSerializable>(self, writer); }
template <typename TListTail>
static void Load(TContainer* self, IReader& reader)
{
RegistryHelper<TContainer, Tail>::Load(self, reader);
}
template <typename TContainer>
struct RegistryHelper<TContainer, Loki::Typelist<>>
{
static void Save(TContainer*, IWriter&) {};
static void Load(TContainer*, IReader&) {};
};

template <>
static void Load<Loki::EmptyType>(TContainer* self, IReader& reader)
{
}
template <typename TContainer, typename T, typename... Ts>
struct RegistryHelper<TContainer, Loki::Typelist<T, Ts...>>
{
static constexpr bool isSerializable = object_type_traits::is_base_and_derived<ISerializable, T>::value;

template <typename TListTail>
static void Save(TContainer* self, IWriter& writer)
{
RegistryHelper<TContainer, Tail>::Save(self, writer);
}

template <>
static void Save<Loki::EmptyType>(TContainer* self, IWriter& writer)
{
}
if constexpr (isSerializable)
self->T::save(writer);
RegistryHelper<TContainer, Loki::Typelist<Ts...>>::Save(self, writer);
};

public:
static void Load(TContainer* self, IReader& reader)
{
Load<Tail>(self, reader);
LoadHead(self, reader);
if constexpr (isSerializable)
self->T::load(reader);
RegistryHelper<TContainer, Loki::Typelist<Ts...>>::Load(self, reader);
}

static void Save(TContainer* self, IWriter& writer)
{
Save<Tail>(self, writer);
SaveHead(self, writer);
}
};

void CALifeRegistryContainer::load(IReader& file_stream)
Expand Down

0 comments on commit cace3a3

Please sign in to comment.