Skip to content

Commit

Permalink
2.3.0
Browse files Browse the repository at this point in the history
* reworked serialization
  • Loading branch information
andrew-gresyk authored Jun 6, 2023
1 parent a2d249f commit 8359a8e
Show file tree
Hide file tree
Showing 19 changed files with 1,111 additions and 368 deletions.
21 changes: 16 additions & 5 deletions development/hfsm2/detail/containers/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ template <typename T>
HFSM2_CONSTEXPR(11)
T
filler() noexcept {
return T{INVALID_SHORT};
return T{};
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

template <>
HFSM2_CONSTEXPR(11)
Short
filler<Short>() noexcept {
return INVALID_SHORT;
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -40,6 +49,7 @@ class StaticArrayT final {

HFSM2_CONSTEXPR(14) void fill(const Item filler) noexcept;
HFSM2_CONSTEXPR(14) void clear() noexcept { fill(filler<Item>()); }
HFSM2_CONSTEXPR(14) bool empty() const noexcept;

HFSM2_CONSTEXPR(14) Iterator begin() noexcept { return Iterator(*this, first()); }
HFSM2_CONSTEXPR(11) CIterator begin() const noexcept { return CIterator(*this, first()); }
Expand Down Expand Up @@ -85,8 +95,6 @@ class ArrayT final {
static constexpr Index CAPACITY = NCapacity;

public:
HFSM2_CONSTEXPR(14) void clear() noexcept { _count = 0; }

template <typename... TArgs>
HFSM2_CONSTEXPR(14) Index emplace(const TArgs &... args) noexcept;

Expand All @@ -99,7 +107,10 @@ class ArrayT final {
template <typename N>
HFSM2_CONSTEXPR(14) const Item& operator[] (const N index) const noexcept;

HFSM2_CONSTEXPR(11) Index count() const noexcept { return _count; }
HFSM2_CONSTEXPR(11) Index count() const noexcept { return _count; }

HFSM2_CONSTEXPR(14) void clear() noexcept { _count = 0; }
HFSM2_CONSTEXPR(11) bool empty() const noexcept { return _count == 0; }

template <Long N>
HFSM2_CONSTEXPR(14) ArrayT& operator += (const ArrayT<Item, N>& other) noexcept;
Expand Down Expand Up @@ -132,7 +143,7 @@ class ArrayT final {
#endif
};

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//------------------------------------------------------------------------------

template <typename T>
class ArrayT<T, 0> final {
Expand Down
13 changes: 13 additions & 0 deletions development/hfsm2/detail/containers/array.inl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ StaticArrayT<T, NC>::fill(const Item filler) noexcept {
item = filler;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

template <typename T, Long NC>
HFSM2_CONSTEXPR(14)
bool
StaticArrayT<T, NC>::empty() const noexcept {
for (const Item& item : _items)
if (item != filler<Item>())
return false;

return true;
}

////////////////////////////////////////////////////////////////////////////////

template <typename T, Long NC>
Expand Down
8 changes: 6 additions & 2 deletions development/hfsm2/detail/containers/bit_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Units final {
template <unsigned NCapacity>
class BitArrayT final {
public:
using Index = UCapacity<NCapacity>;
using Index = UCapacity<NCapacity>;

static constexpr Index CAPACITY = NCapacity;
static constexpr Index UNIT_COUNT = contain(CAPACITY, 8);
Expand Down Expand Up @@ -102,6 +102,8 @@ class BitArrayT final {
template <Short NIndex>
HFSM2_CONSTEXPR(14) void clear() noexcept;

HFSM2_CONSTEXPR(14) bool empty() const noexcept;

template <typename TIndex>
HFSM2_CONSTEXPR(14) bool get (const TIndex index) const noexcept;

Expand All @@ -121,7 +123,7 @@ class BitArrayT final {
HFSM2_CONSTEXPR(14) CBits cbits(const Units& units) const noexcept;

private:
uint8_t _storage[UNIT_COUNT];
uint8_t _storage[UNIT_COUNT] {};
};

//------------------------------------------------------------------------------
Expand All @@ -130,6 +132,8 @@ template <>
class BitArrayT<0> final {
public:
HFSM2_CONSTEXPR(14) void clear() noexcept {}

HFSM2_CONSTEXPR(11) bool empty() const noexcept { return true; }
};

////////////////////////////////////////////////////////////////////////////////
Expand Down
13 changes: 13 additions & 0 deletions development/hfsm2/detail/containers/bit_array.inl
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ BitArrayT<NCapacity>::clear() noexcept {

//------------------------------------------------------------------------------

template <unsigned NCapacity>
HFSM2_CONSTEXPR(14)
bool
BitArrayT<NCapacity>::empty() const noexcept {
for (const uint8_t& unit : _storage)
if (unit != uint8_t{0})
return false;

return true;
}

//------------------------------------------------------------------------------

template <unsigned NCapacity>
template <Short NIndex>
HFSM2_CONSTEXPR(14)
Expand Down
11 changes: 7 additions & 4 deletions development/hfsm2/detail/containers/task_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class TaskListT {
using Item = TaskT<Payload>;

public:
HFSM2_CONSTEXPR(14) void clear() noexcept;

template <typename... TArgs>
HFSM2_CONSTEXPR(14) Index emplace(TArgs&&... args) noexcept;

Expand All @@ -133,17 +135,18 @@ class TaskListT {
HFSM2_CONSTEXPR(14) Item& operator[] (const Index i) noexcept;
HFSM2_CONSTEXPR(11) const Item& operator[] (const Index i) const noexcept;

HFSM2_CONSTEXPR(11) Index count() const noexcept { return _count; }
HFSM2_CONSTEXPR(11) Index count() const noexcept { return _count; }
HFSM2_CONSTEXPR(11) bool empty() const noexcept { return _count == 0; }

private:
HFSM2_IF_ASSERT(void verifyStructure(const Index occupied = INVALID) const noexcept);

private:
Index _vacantHead = 0;
Index _vacantTail = 0;
Index _last = 0;
Index _count = 0;
Item _items[CAPACITY];
Index _last = 0;
Index _count = 0;
Item _items[CAPACITY] {};
};

//------------------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions development/hfsm2/detail/containers/task_list.inl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ namespace detail {

////////////////////////////////////////////////////////////////////////////////

template <typename TP, Long NC>
HFSM2_CONSTEXPR(14)
void
TaskListT<TP, NC>::clear() noexcept {
_vacantHead = 0;
_vacantTail = 0;
_last = 0;
_count = 0;
}

//------------------------------------------------------------------------------

template <typename TP, Long NC>
template <typename... TA>
HFSM2_CONSTEXPR(14)
Expand Down
109 changes: 86 additions & 23 deletions development/hfsm2/detail/root.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,27 +517,6 @@ class R_ {

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#if HFSM2_SERIALIZATION_AVAILABLE()

/// @brief Buffer for serialization
/// @see https://doc.hfsm.dev/user-guide/debugging-and-tools/serialization
/// @see HFSM2_ENABLE_SERIALIZATION
using SerialBuffer = typename Args::SerialBuffer;

/// @brief Serialize FSM into 'buffer'
/// @param buffer 'SerialBuffer' to serialize to
/// @see HFSM2_ENABLE_SERIALIZATION
HFSM2_CONSTEXPR(14) void save( SerialBuffer& buffer) const noexcept;

/// @brief De-serialize FSM from 'buffer'
/// @param buffer 'SerialBuffer' to de-serialize from
/// @see HFSM2_ENABLE_SERIALIZATION
HFSM2_CONSTEXPR(14) void load(const SerialBuffer& buffer) noexcept;

#endif

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#if HFSM2_TRANSITION_HISTORY_AVAILABLE()

/// @brief Get the list of transitions recorded during last 'update()'
Expand Down Expand Up @@ -642,6 +621,11 @@ class R_ {
HFSM2_CONSTEXPR(14) bool cancelledByGuards(const TransitionSets& currentTransitions,
const TransitionSet& pendingTransitions) noexcept;

#if HFSM2_SERIALIZATION_AVAILABLE()
HFSM2_CONSTEXPR(14) void save(WriteStream& stream) const noexcept;
HFSM2_CONSTEXPR(14) void load( ReadStream& stream) noexcept;
#endif

#if HFSM2_TRANSITION_HISTORY_AVAILABLE()
HFSM2_CONSTEXPR(14) bool applyRequests(Control& control,
const Transition* const transitions,
Expand Down Expand Up @@ -701,6 +685,12 @@ class RV_ <G_<NFeatureTag, TContext, TActivation HFSM2_IF_UTILITY_THEORY(, T
using typename Base::RNG;
#endif

#if HFSM2_SERIALIZATION_AVAILABLE()
using typename Base::Args;
using typename Base::WriteStream;
using typename Base::ReadStream;
#endif

#if HFSM2_LOG_INTERFACE_AVAILABLE()
using typename Base::Logger;
#endif
Expand All @@ -719,6 +709,34 @@ class RV_ <G_<NFeatureTag, TContext, TActivation HFSM2_IF_UTILITY_THEORY(, T

HFSM2_CONSTEXPR(20) ~RV_() noexcept;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#if HFSM2_SERIALIZATION_AVAILABLE()

/// @brief Buffer for serialization
/// @see `HFSM2_ENABLE_SERIALIZATION`
using SerialBuffer = typename Args::SerialBuffer;

/// @brief Serialize FSM into 'buffer'
/// @param buffer `SerialBuffer` to serialize to
/// @see `HFSM2_ENABLE_SERIALIZATION`
HFSM2_CONSTEXPR(14) void save( SerialBuffer& buffer) const noexcept;

/// @brief De-serialize FSM from 'buffer'
/// @param buffer `SerialBuffer` to de-serialize from
/// @see `HFSM2_ENABLE_SERIALIZATION`
HFSM2_CONSTEXPR(14) void load(const SerialBuffer& buffer) noexcept;

#endif

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

private:
#if HFSM2_SERIALIZATION_AVAILABLE()
using Base::save;
using Base::load;
#endif

private:
using Base::initialEnter;
using Base::finalExit;
Expand Down Expand Up @@ -751,6 +769,15 @@ class RV_ <G_<NFeatureTag, TContext, Manual HFSM2_IF_UTILITY_THEORY(, TRank,
protected:
HFSM2_IF_UTILITY_THEORY(using typename Base::RNG);

#if HFSM2_SERIALIZATION_AVAILABLE()
using typename Base::PlanControl;
using typename Base::TransitionSets;

using typename Base::Args;
using typename Base::WriteStream;
using typename Base::ReadStream;
#endif

#if HFSM2_TRANSITION_HISTORY_AVAILABLE()
using typename Base::PlanControl;
using typename Base::TransitionSets;
Expand All @@ -759,6 +786,8 @@ class RV_ <G_<NFeatureTag, TContext, Manual HFSM2_IF_UTILITY_THEORY(, TRank,
public:
using Base::Base;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/// @brief Check if FSM is active
/// @return FSM active status
HFSM2_CONSTEXPR(11) bool isActive() const noexcept { return _core.registry.isActive(); }
Expand All @@ -773,6 +802,28 @@ class RV_ <G_<NFeatureTag, TContext, Manual HFSM2_IF_UTILITY_THEORY(, TRank,
/// Can be used with UE4 to start / stop the FSM in BeginPlay() / EndPlay()
HFSM2_CONSTEXPR(14) void exit() noexcept { finalExit(); }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#if HFSM2_SERIALIZATION_AVAILABLE()

/// @brief Buffer for serialization
/// @see `HFSM2_ENABLE_SERIALIZATION`
using SerialBuffer = typename Args::SerialBuffer;

/// @brief Serialize FSM into 'buffer'
/// @param buffer `SerialBuffer` to serialize to
/// @see `HFSM2_ENABLE_SERIALIZATION`
HFSM2_CONSTEXPR(14) void save( SerialBuffer& buffer) const noexcept;

/// @brief De-serialize FSM from 'buffer'
/// @param buffer `SerialBuffer` to de-serialize from
/// @see `HFSM2_ENABLE_SERIALIZATION`
HFSM2_CONSTEXPR(14) void load(const SerialBuffer& buffer) noexcept;

#endif

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#if HFSM2_TRANSITION_HISTORY_AVAILABLE()

/// @brief Start the FSM from a specific state
Expand All @@ -798,17 +849,29 @@ class RV_ <G_<NFeatureTag, TContext, Manual HFSM2_IF_UTILITY_THEORY(, TRank,

#endif

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

private:
#if HFSM2_SERIALIZATION_AVAILABLE()
using Base::save;
using Base::load;

HFSM2_CONSTEXPR(14) void loadEnter(ReadStream& stream) noexcept;
#endif

protected:
using Base::initialEnter;
using Base::finalExit;

using Base::_core;

#if HFSM2_SERIALIZATION_AVAILABLE() || HFSM2_TRANSITION_HISTORY_AVAILABLE()
using Base::_apex;
#endif

#if HFSM2_TRANSITION_HISTORY_AVAILABLE()
using Base::applyRequests;
HFSM2_IF_STRUCTURE_REPORT(using Base::udpateActivity);

using Base::_apex;
#endif
};

Expand Down
Loading

0 comments on commit 8359a8e

Please sign in to comment.