Skip to content

Commit

Permalink
Move interface class into namespace interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
aentinger committed Apr 11, 2023
1 parent 31440b5 commit d8660c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ enum class Error : std::uint8_t
Internal, ///< Internal failure in the filesystem (storage corruption or logic error).
};

namespace interface
{

/// Key-value storage provides a very simple API for storing and retrieving named blobs.
/// The underlying storage implementation is required to be power-loss tolerant and to
/// validate data integrity per key (e.g., using CRC and such).
/// This interface is fully blocking and should only be used during initialization and shutdown,
/// never during normal operation. Non-blocking adapters can be built on top of it.
class KeyValueStorageBase
class KeyValueStorage
{
public:
KeyValueStorageBase() = default;
KeyValueStorageBase(const KeyValueStorageBase&) = delete;
KeyValueStorageBase(KeyValueStorageBase&&) = delete;
auto operator=(const KeyValueStorageBase&) -> KeyValueStorageBase& = delete;
auto operator=(KeyValueStorageBase&&) -> KeyValueStorageBase& = delete;
virtual ~KeyValueStorageBase() = default;
KeyValueStorage() = default;
KeyValueStorage(const KeyValueStorage&) = delete;
KeyValueStorage(KeyValueStorage&&) = delete;
auto operator=(const KeyValueStorage&) -> KeyValueStorage& = delete;
auto operator=(KeyValueStorage&&) -> KeyValueStorage& = delete;
virtual ~KeyValueStorage() = default;

/// The return value is the number of bytes read into the buffer or the error.
[[nodiscard]] virtual auto get(const std::string_view key, const std::size_t size, void* const data) const
Expand All @@ -53,6 +56,8 @@ class KeyValueStorageBase
[[nodiscard]] virtual auto drop(const std::string_view key) -> std::optional<Error> = 0;
};

} /* interface */

} /* cyphal::support::platform::storage */

#endif /* __GNUC__ >= 11 */
8 changes: 4 additions & 4 deletions src/util/storage/register_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#if __GNUC__ >= 11

#include "KeyValueStorageBase.hpp"
#include "KeyValueStorage.hpp"

#include <functional>

Expand All @@ -23,7 +23,7 @@ namespace cyphal::support
/// Stored registers that are not present in the registry will not be loaded.
/// The serialization format is simply the Cyphal DSDL.
/// In case of error, only part of the registers may be loaded and the registry will be left in an inconsistent state.
[[nodiscard]] inline std::optional<platform::storage::Error> load(const platform::storage::KeyValueStorageBase& kv,
[[nodiscard]] inline std::optional<platform::storage::Error> load(const platform::storage::interface::KeyValueStorage& kv,
registry::IIntrospectableRegistry& rgy)
{
for (std::size_t index = 0; index < rgy.size(); index++)
Expand Down Expand Up @@ -82,7 +82,7 @@ namespace cyphal::support
/// The removal predicate, if provided, allows the caller to specify which registers need to be removed from the
/// storage instead of being saved. This is useful for implementing the "factory reset" feature.
template <typename ResetPredicate>
[[nodiscard]] std::optional<platform::storage::Error> save(platform::storage::KeyValueStorageBase& kv,
[[nodiscard]] std::optional<platform::storage::Error> save(platform::storage::interface::KeyValueStorage& kv,
const registry::IIntrospectableRegistry& rgy,
ResetPredicate const reset_predicate)
{
Expand Down Expand Up @@ -127,7 +127,7 @@ template <typename ResetPredicate>
}
return std::nullopt;
}
[[nodiscard]] inline std::optional<platform::storage::Error> save(platform::storage::KeyValueStorageBase& kv,
[[nodiscard]] inline std::optional<platform::storage::Error> save(platform::storage::interface::KeyValueStorage& kv,
const registry::IIntrospectableRegistry& rgy)
{
return save(kv, rgy, [](std::string_view) { return false; });
Expand Down

0 comments on commit d8660c8

Please sign in to comment.