-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Hilst
committed
Feb 12, 2025
1 parent
f202b6b
commit 086f661
Showing
11 changed files
with
424 additions
and
229 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef CLOYSTER_CONCEPTS_H | ||
#define CLOYSTER_CONCEPTS_H | ||
|
||
#include <concepts> | ||
#include <expected> | ||
#include <iostream> | ||
|
||
namespace cloyster::concepts { | ||
// @TODO: Move this to its own file | ||
template <typename T> | ||
concept IsCopyable | ||
= std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T>; | ||
|
||
template <typename T> | ||
concept IsMoveable | ||
= std::is_move_constructible_v<T> && std::is_move_assignable_v<T>; | ||
|
||
template <typename T> | ||
concept NotCopiableMoveable = !IsMoveable<T> && !IsCopyable<T>; | ||
/** | ||
* @brief Parser<P, T> means: P can parse and unparse Ts from streams. The | ||
* parsers are allowed to throw exceptions | ||
*/ | ||
template <typename Parser_, typename T> | ||
concept Parser = requires(Parser_ parser, std::istream& parseInput, | ||
std::ostream& unparseOutput, const T& unparseInput, T& parseOutput) { | ||
{ parser.parse(parseInput, parseOutput) } -> std::same_as<void>; | ||
{ parser.unparse(unparseInput, unparseOutput) } -> std::same_as<void>; | ||
}; | ||
|
||
// @TODO: Move this to its own file | ||
/** | ||
* @brief ParserNoExc<P, T, E> means: P can parse and unparse Ts from streams. | ||
* The parsers are not allowed to throw exceptions, it must return errors of | ||
* type E | ||
*/ | ||
template <typename Parser_, typename T, typename E> | ||
concept ParserNoExc = requires(Parser_ parser, std::istream& input, | ||
std::ostream& output, const T& parsed) { | ||
{ parser.parse(input) } noexcept -> std::same_as<std::expected<T, E>>; | ||
{ | ||
parser.unparse(parsed, output) | ||
} noexcept -> std::same_as<std::expected<void, E>>; | ||
}; | ||
|
||
// @TODO: Move this to its own file | ||
/** | ||
* @brief Stored<T> means that T represends data in a disk that need to be | ||
* saved and restored after change. | ||
*/ | ||
template <typename File_> | ||
concept IsSaveable = requires(File_ file) { | ||
{ file.load() } -> std::same_as<void>; | ||
{ file.save() } -> std::same_as<void>; | ||
}; | ||
|
||
} | ||
|
||
#endif // CLOYSTER_CONCEPTS_H |
This file was deleted.
Oops, something went wrong.
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,70 @@ | ||
#ifndef CLOYSTER_SERVICES_FILES_H | ||
#define CLOYSTER_SERVICES_FILES_H | ||
|
||
#include <filesystem> | ||
#include <string> | ||
#include <concepts> | ||
#include <vector> | ||
|
||
#include <cloysterhpc/concepts.h> | ||
|
||
namespace cloyster::services::files { | ||
|
||
template <typename File> | ||
concept IsKeyFileReadable = requires(const File& file, const std::string& group, const std::string& key) | ||
{ | ||
{ file.getGroups() } -> std::convertible_to<std::vector<std::string>>; | ||
{ file.getString(group, key) } -> std::same_as<std::string>; | ||
{ file.getBoolean(group, key) } -> std::same_as<bool>; | ||
{ file.getStringOpt(group, key) } -> std::same_as<std::optional<std::string>>; | ||
}; | ||
|
||
template <typename File> | ||
concept IsKeyFileWriteable = requires(File& file, const std::string& group, const std::string& key, bool bvalue, const std::string& svalue, const std::optional<std::string>& soptvalue) | ||
{ | ||
file.setString(group, key, svalue); | ||
file.setString(group, key, soptvalue); | ||
file.setBoolean(group, key, bvalue); | ||
}; | ||
|
||
/** | ||
* @brief Represents a KeyFile hiding the implementation details behind Impl | ||
*/ | ||
class KeyFile { | ||
struct Impl; // Pointer to Implementation (PIMPL) pattern | ||
std::unique_ptr<Impl> m_impl; | ||
explicit KeyFile(Impl&& impl); | ||
public: | ||
~KeyFile(); // The destructor is required to be defined in | ||
// the .cpp file where the size of Impl is known | ||
|
||
// Moveable, not Copiable | ||
KeyFile(const KeyFile&) = delete; | ||
KeyFile(KeyFile&& file) = default; | ||
KeyFile& operator=(const KeyFile&) = delete; | ||
KeyFile& operator=(KeyFile&&) = default; | ||
|
||
[[nodiscard]] std::vector<std::string> getGroups() const; | ||
[[nodiscard]] std::string getString(const std::string& group, const std::string& key) const; | ||
[[nodiscard]] bool getBoolean(const std::string& group, const std::string& key) const; | ||
[[nodiscard]] std::optional<std::string> getStringOpt(const std::string& group, const std::string& key) const; | ||
|
||
void setString(const std::string& group, const std::string& key, const std::string& value); | ||
void setString(const std::string& group, const std::string& key, const std::optional<std::string>& value); | ||
void setBoolean(const std::string& group, const std::string& key, const bool value); | ||
|
||
void save(); | ||
void load(); | ||
|
||
explicit KeyFile(const std::filesystem::path& path); | ||
explicit KeyFile(std::istream& istream); | ||
}; | ||
static_assert(IsKeyFileReadable<KeyFile>); | ||
static_assert(IsKeyFileWriteable<KeyFile>); | ||
static_assert(cloyster::concepts::IsSaveable<KeyFile>); | ||
static_assert(concepts::IsMoveable<KeyFile>); | ||
static_assert(!concepts::IsCopyable<KeyFile>); | ||
|
||
}; | ||
|
||
#endif |
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
Empty file.
Oops, something went wrong.