Skip to content

Commit

Permalink
Merge pull request #675 from ohmtech-rdi/erb-persistent-base
Browse files Browse the repository at this point in the history
Separate concerns between value integrity and rate limiting
  • Loading branch information
ohmtech-rdi authored Mar 30, 2024
2 parents 5724fc8 + e154a07 commit 96e4195
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 16 deletions.
5 changes: 5 additions & 0 deletions include/erb/Persistent.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "erb/def.h"

#include "erb/PersistentBase.h"

#if defined (erb_TARGET_DAISY)
#include "erb/daisy/ClockHal.h"
#endif
Expand Down Expand Up @@ -71,6 +73,9 @@ class Persistent

#endif

PersistentBase <Type, Magic>
_base;

Type _value = {};

bool _need_commit_flag = false;
Expand Down
18 changes: 2 additions & 16 deletions include/erb/Persistent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,7 @@ template <typename Type, size_t Page, uint64_t Magic, size_t RateLimitMs>
template <typename Board>
Type Persistent <Type, Page, Magic, RateLimitMs>::load (Board & board)
{
const auto bytes = board.template load <sizeof (Magic) + sizeof (Type)> (Page);

uint64_t loaded_magic = 0;
std::memcpy (&loaded_magic, &bytes [0], sizeof (Magic));

if (loaded_magic == Magic)
{
std::memcpy (&_value, &bytes [sizeof (Magic)], sizeof (Type));
}
_value = _base.load (board, Page);

return _value;
}
Expand All @@ -70,13 +62,7 @@ void Persistent <Type, Page, Magic, RateLimitMs>::save (Board & board, Type val

if (_need_commit_flag && (Clock::now () > _earliest_commit_tp || RateLimitMs == 0))
{
uint64_t magic = Magic;
std::array <uint8_t, sizeof (Magic) + sizeof (Type)> data;

std::memcpy (&data [0], &magic, sizeof (Magic));
std::memcpy (&data [sizeof (Magic)], &_value, sizeof (Type));

board.save (Page, data);
_base.save (board, Page, _value);

_need_commit_flag = false;
}
Expand Down
88 changes: 88 additions & 0 deletions include/erb/PersistentBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*****************************************************************************
PersistentBase.h
Copyright (c) 2020 Raphael DINGE
*Tab=3***********************************************************************/



#pragma once



/*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

#include "erb/def.h"

#include <cstddef>
#include <cstdint>



namespace erb
{



template <typename Type, uint64_t Magic>
class PersistentBase
{

/*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

public:
inline PersistentBase () = default;
virtual ~PersistentBase () = default;

template <typename Board>
Type load (Board & board, size_t page);

template <typename Board>
void save (Board & board, size_t page, Type value);



/*\\\ INTERNAL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/



/*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

protected:



/*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

private:



/*\\\ FORBIDDEN MEMBER FUNCTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

private:
PersistentBase (const PersistentBase & rhs) = delete;
PersistentBase (PersistentBase && rhs) = delete;
PersistentBase &
operator = (const PersistentBase & rhs) = delete;
PersistentBase &
operator = (PersistentBase && rhs) = delete;
bool operator == (const PersistentBase & rhs) const = delete;
bool operator != (const PersistentBase & rhs) const = delete;



}; // class PersistentBase



} // namespace erb



#include "erb/PersistentBase.hpp"



/*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
89 changes: 89 additions & 0 deletions include/erb/PersistentBase.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*****************************************************************************
PersistentBase.hpp
Copyright (c) 2020 Raphael DINGE
*Tab=3***********************************************************************/



#pragma once



/*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

#include <array>

#include <cstring>



namespace erb
{



/*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

/*
==============================================================================
Name : load
==============================================================================
*/

template <typename Type, uint64_t Magic>
template <typename Board>
Type PersistentBase <Type, Magic>::load (Board & board, size_t page)
{
const auto bytes = board.template load <sizeof (Magic) + sizeof (Type)> (page);

uint64_t loaded_magic = 0;
std::memcpy (&loaded_magic, &bytes [0], sizeof (Magic));

Type value;

if (loaded_magic == Magic)
{
std::memcpy (&value, &bytes [sizeof (Magic)], sizeof (Type));
}

return value;
}



/*
==============================================================================
Name : save
==============================================================================
*/

template <typename Type, uint64_t Magic>
template <typename Board>
void PersistentBase <Type, Magic>::save (Board & board, size_t page, Type value)
{
uint64_t magic = Magic;
std::array <uint8_t, sizeof (Magic) + sizeof (Type)> data;

std::memcpy (&data [0], &magic, sizeof (Magic));
std::memcpy (&data [sizeof (Magic)], &value, sizeof (Type));

board.save (page, data);
}



/*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/



/*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/



} // namespace erb



/*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
2 changes: 2 additions & 0 deletions include/erb/erb-src.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
'LedRgb.hpp',
'Persistent.h',
'Persistent.hpp',
'PersistentBase.h',
'PersistentBase.hpp',
'PinType.h',
'Pot.h',
'Pot.hpp',
Expand Down

0 comments on commit 96e4195

Please sign in to comment.