Skip to content

Commit

Permalink
Make flags in LfsWrapper unsigned
Browse files Browse the repository at this point in the history
Flag varibales are one of the few cases where unsigned types should be
used because they do not represent a real quantity/number but just a
bunch of states/bits.
  • Loading branch information
PatrickKa committed Jun 21, 2024
1 parent 62d473c commit dd4690a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
16 changes: 10 additions & 6 deletions Sts1CobcSw/FileSystem/LfsWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ auto Unmount() -> Result<void>
}


auto Open(std::string_view path, int flags) -> Result<File>
auto Open(std::string_view path, unsigned int flags) -> Result<File>
{
auto file = File();
auto error = lfs_file_opencfg(&lfs, &file.lfsFile_, path.data(), flags, &file.lfsFileConfig_);
auto error = lfs_file_opencfg(
&lfs, &file.lfsFile_, path.data(), static_cast<int>(flags), &file.lfsFileConfig_);
if(error == 0)
{
file.path_ = Path(path.data(), path.size());
Expand All @@ -66,8 +67,8 @@ File::File(File && other) noexcept
{
return;
}
auto error =
lfs_file_opencfg(&lfs, &lfsFile_, other.path_.c_str(), other.openFlags_, &lfsFileConfig_);
auto error = lfs_file_opencfg(
&lfs, &lfsFile_, other.path_.c_str(), static_cast<int>(other.openFlags_), &lfsFileConfig_);
if(error == 0)
{
path_ = other.path_;
Expand All @@ -86,8 +87,11 @@ auto File::operator=(File && other) noexcept -> File &
// TODO: Use copy and swap idiom to prevent code duplication from move constructor
if(this != &other and not other.path_.empty())
{
auto error = lfs_file_opencfg(
&lfs, &lfsFile_, other.path_.c_str(), other.openFlags_, &lfsFileConfig_);
auto error = lfs_file_opencfg(&lfs,
&lfsFile_,
other.path_.c_str(),
static_cast<int>(other.openFlags_),
&lfsFileConfig_);
if(error == 0)
{
path_ = other.path_;
Expand Down
6 changes: 3 additions & 3 deletions Sts1CobcSw/FileSystem/LfsWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class File;

[[nodiscard]] auto Mount() -> Result<void>;
[[nodiscard]] auto Unmount() -> Result<void>;
[[nodiscard]] auto Open(std::string_view path, int flags) -> Result<File>;
[[nodiscard]] auto Open(std::string_view path, unsigned int flags) -> Result<File>;


// FIXME: Make File const-correct (only Write() should be non-const)
Expand All @@ -51,15 +51,15 @@ class File
[[nodiscard]] auto Size() -> Result<int>;
[[nodiscard]] auto Close() -> Result<void>;

friend auto Open(std::string_view path, int flags) -> Result<File>;
friend auto Open(std::string_view path, unsigned int flags) -> Result<File>;

private:
// Only allow creation of File class through friend function Open()
File() = default;

Path path_ = "";
int openFlags_ = 0;
bool isOpen_ = false;
unsigned int openFlags_ = 0;
std::array<Byte, lfsCacheSize> buffer_ = {};
lfs_file_t lfsFile_ = {};
lfs_file_config lfsFileConfig_ = {.buffer = buffer_.data()};
Expand Down
4 changes: 2 additions & 2 deletions Sts1CobcSw/FileSystem/LfsWrapper.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ auto File::Read(T * t) -> Result<int>
{
return ErrorCode::fileNotOpen;
}
if(not(static_cast<uint>(openFlags_) & LFS_O_RDONLY))
if(not(openFlags_ & LFS_O_RDONLY))
{
return ErrorCode::unsupportedOperation;
}
Expand All @@ -36,7 +36,7 @@ auto File::Write(T const & t) -> Result<int>
{
return ErrorCode::fileNotOpen;
}
if(not(static_cast<uint>(openFlags_) & LFS_O_WRONLY))
if(not(openFlags_ & LFS_O_WRONLY))
{
return ErrorCode::unsupportedOperation;
}
Expand Down

0 comments on commit dd4690a

Please sign in to comment.