-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #675 from ohmtech-rdi/erb-persistent-base
Separate concerns between value integrity and rate limiting
- Loading branch information
Showing
5 changed files
with
186 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters