Move file_wrapper and make testable. (#1085)

* WIP Move file_wrapper and make testable.

* More tests, get text_editor compiling

* Back to working

* Run formatter

---------

Co-authored-by: kallanreed <kallanreed@outlook.com>
This commit is contained in:
Kyle Reed
2023-05-28 08:44:21 -07:00
committed by GitHub
parent 23c24355ab
commit e50d8dc148
7 changed files with 731 additions and 308 deletions

View File

@@ -288,6 +288,7 @@ class File {
using Timestamp = uint32_t;
using Error = std::filesystem::filesystem_error;
// TODO: move to common.
template <typename T>
struct Result {
enum class Type {
@@ -303,11 +304,15 @@ class File {
return type == Type::Success;
}
operator bool() const {
return is_ok();
}
bool is_error() const {
return type == Type::Error;
}
const T& value() const {
const T& value() const& {
return value_;
}
@@ -315,6 +320,15 @@ class File {
return value_;
}
/* Allows value to be moved out of the Result. */
T take() {
if (is_error())
return {};
T temp;
std::swap(temp, value_);
return temp;
}
Error error() const {
return error_;
}
@@ -322,9 +336,9 @@ class File {
Result() = delete;
constexpr Result(
T value)
T&& value)
: type{Type::Success},
value_{value} {
value_{std::forward<T>(value)} {
}
constexpr Result(
@@ -334,17 +348,21 @@ class File {
}
~Result() {
if (type == Type::Success) {
if (is_ok())
value_.~T();
}
}
};
File(){};
~File();
File(File&&) = default;
File& operator=(File&&) = default;
File(File&& other) {
std::swap(f, other.f);
}
File& operator=(File&& other) {
std::swap(f, other.f);
return *this;
}
/* Prevent copies */
File(const File&) = delete;
@@ -355,10 +373,10 @@ class File {
Optional<Error> append(const std::filesystem::path& filename);
Optional<Error> create(const std::filesystem::path& filename);
Result<Size> read(void* const data, const Size bytes_to_read);
Result<Size> write(const void* const data, const Size bytes_to_write);
Result<Size> read(void* data, const Size bytes_to_read);
Result<Size> write(const void* data, Size bytes_to_write);
Result<Offset> seek(const uint64_t Offset);
Result<Offset> seek(uint64_t Offset);
// Timestamp created_date() const;
Size size() const;