summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--CMakeLists.txt2
-rw-r--r--include/fud_c_file.hpp8
-rw-r--r--include/fud_directory.hpp6
-rw-r--r--include/fud_result.hpp52
-rw-r--r--include/fud_sqlite.hpp8
-rw-r--r--include/fud_string.hpp4
-rw-r--r--include/fud_utf8.hpp4
-rw-r--r--source/fud_c_file.cpp26
-rw-r--r--source/fud_directory.cpp23
-rw-r--r--source/fud_sqlite.cpp6
-rw-r--r--source/fud_string.cpp12
-rw-r--r--source/fud_utf8.cpp15
13 files changed, 101 insertions, 66 deletions
diff --git a/.gitignore b/.gitignore
index b374a73..40a320a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,3 +11,4 @@ dist/
.semgrepignore
.#*
fudenv/
+CMakeLists.txt.user
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2bbd779..45387ce 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,7 +7,7 @@ endif()
project(libfud
VERSION 0.42.0
DESCRIPTION "Library of FUD"
- LANGUAGES CXX C)
+ LANGUAGES CXX)
set(CXX_CPPCHECK "project=build/compile_commands.json;enable=information;force")
diff --git a/include/fud_c_file.hpp b/include/fud_c_file.hpp
index 0908841..1c7c304 100644
--- a/include/fud_c_file.hpp
+++ b/include/fud_c_file.hpp
@@ -393,13 +393,13 @@ class CBinaryFile : public detail::CFile<CBinaryFile> {
CBinaryFile(const CBinaryFile& rhs) = delete;
- CBinaryFile(CBinaryFile&& rhs);
+ CBinaryFile(CBinaryFile&& rhs) noexcept;
~CBinaryFile();
CBinaryFile& operator=(const CBinaryFile& rhs) = delete;
- CBinaryFile& operator=(CBinaryFile&& rhs);
+ CBinaryFile& operator=(CBinaryFile&& rhs) noexcept;
private:
String m_filename;
@@ -419,13 +419,13 @@ class CTextFile : public detail::CFile<CTextFile> {
CTextFile(const CTextFile& rhs) = delete;
- CTextFile(CTextFile&& rhs);
+ CTextFile(CTextFile&& rhs) noexcept;
~CTextFile();
CTextFile& operator=(const CTextFile& rhs) = delete;
- CTextFile& operator=(CTextFile&& rhs);
+ CTextFile& operator=(CTextFile&& rhs) noexcept;
private:
String m_filename;
diff --git a/include/fud_directory.hpp b/include/fud_directory.hpp
index cd3576e..ca94528 100644
--- a/include/fud_directory.hpp
+++ b/include/fud_directory.hpp
@@ -91,12 +91,12 @@ struct DirectoryEntry {
class Directory {
public:
- explicit Directory(String name);
+ explicit Directory(const String& name);
Directory(const Directory& rhs) = delete;
- Directory(Directory&& rhs);
+ Directory(Directory&& rhs) noexcept;
~Directory();
Directory& operator=(const Directory& rhs) = delete;
- Directory& operator=(Directory&& rhs) = delete;
+ Directory& operator=(Directory&& rhs) noexcept = delete;
constexpr const String& name() const {
return m_name;
diff --git a/include/fud_result.hpp b/include/fud_result.hpp
index 9b96399..076af21 100644
--- a/include/fud_result.hpp
+++ b/include/fud_result.hpp
@@ -23,6 +23,30 @@
namespace fud {
+namespace detail {
+template <typename T>
+class CopyMove {
+ public:
+ explicit constexpr CopyMove(T value) : m_value{std::move(value)}
+ {
+ }
+
+ constexpr T&& take()
+ {
+ return std::move(m_value);
+ }
+
+ constexpr T copy() const
+ {
+ return m_value;
+ }
+
+ private:
+ T m_value;
+};
+
+} // namespace detail
+
/** \brief A result type which contains either a T on success or an E on error. */
template <typename T, typename E>
class [[nodiscard]] Result {
@@ -58,35 +82,45 @@ class [[nodiscard]] Result {
return (m_value.index() == 1);
}
- T getOkay()
+ [[nodiscard]] T getOkay() const
+ {
+ return std::get<detail::CopyMove<T>>(m_value).copy();
+ }
+
+ [[nodiscard]] E getError() const
+ {
+ return std::get<detail::CopyMove<E>>(m_value).copy();
+ }
+
+ [[nodiscard]] T&& getOkay()
{
- return std::get<T>(m_value);
+ return std::get<detail::CopyMove<T>>(m_value).take();
}
- E getError()
+ [[nodiscard]] E&& getError()
{
- return std::get<E>(m_value);
+ return std::get<detail::CopyMove<E>>(m_value).take();
}
private:
explicit Result() : m_value()
{
}
- explicit Result(const T& value) : m_value(value)
+ explicit Result(const T& value) : m_value{detail::CopyMove<T>{value}}
{
}
- explicit Result(const E& value) : m_value(value)
+ explicit Result(const E& value) : m_value{detail::CopyMove<E>{value}}
{
}
- explicit Result(T&& value) : m_value(std::move(value))
+ explicit Result(T&& value) : m_value{detail::CopyMove<T>{std::move(value)}}
{
}
- explicit Result(E&& value) : m_value(std::move(value))
+ explicit Result(E&& value) : m_value{detail::CopyMove<E>{std::move(value)}}
{
}
- std::variant<T, E> m_value;
+ std::variant<detail::CopyMove<T>, detail::CopyMove<E>> m_value;
};
} // namespace fud
diff --git a/include/fud_sqlite.hpp b/include/fud_sqlite.hpp
index 555e487..d879ed0 100644
--- a/include/fud_sqlite.hpp
+++ b/include/fud_sqlite.hpp
@@ -42,13 +42,13 @@ class SqliteDb {
SqliteDb(const SqliteDb&) = delete;
- SqliteDb(SqliteDb&& rhs);
+ SqliteDb(SqliteDb&& rhs) noexcept;
~SqliteDb();
SqliteDb& operator=(const SqliteDb&) = delete;
- SqliteDb& operator=(SqliteDb&& rhs);
+ SqliteDb& operator=(SqliteDb&& rhs) noexcept;
bool valid() const;
@@ -99,13 +99,13 @@ class SqliteStatement {
SqliteStatement(const SqliteStatement&) = delete;
- SqliteStatement(SqliteStatement&& rhs);
+ SqliteStatement(SqliteStatement&& rhs) noexcept;
~SqliteStatement();
SqliteStatement& operator=(const SqliteStatement&) = delete;
- SqliteStatement& operator=(SqliteStatement&& rhs) = delete;
+ SqliteStatement& operator=(SqliteStatement&& rhs) noexcept = delete;
bool valid() const;
diff --git a/include/fud_string.hpp b/include/fud_string.hpp
index 1a38d43..1939c7d 100644
--- a/include/fud_string.hpp
+++ b/include/fud_string.hpp
@@ -38,11 +38,11 @@ class String {
explicit String(const utf8* cString);
explicit String(const char* cString);
String(const String& rhs);
- String(String&& rhs);
+ String(String&& rhs) noexcept;
~String();
String& operator=(const String& rhs);
- String& operator=(String&& rhs);
+ String& operator=(String&& rhs) noexcept;
[[nodiscard]] constexpr size_t length() const
{
diff --git a/include/fud_utf8.hpp b/include/fud_utf8.hpp
index 5b84a3a..058e4f9 100644
--- a/include/fud_utf8.hpp
+++ b/include/fud_utf8.hpp
@@ -251,8 +251,8 @@ struct FudUtf8 {
static constexpr Ascii invalidAsciiCode{Ascii{0xFF}};
static FudUtf8 fromString(const String& fudString, size_t index) noexcept;
- static FudUtf8 fromStringView(StringView&& view, size_t index) noexcept;
- static FudUtf8 fromStringView(const StringView& view, size_t index) noexcept;
+ static FudUtf8 fromStringView(StringView view, size_t index) noexcept;
+ // static FudUtf8 fromStringView(const StringView& view, size_t index) noexcept;
static constexpr FudUtf8 makeUtf8(const Array<utf8, 4>& data)
{
diff --git a/source/fud_c_file.cpp b/source/fud_c_file.cpp
index e2e71bf..3961042 100644
--- a/source/fud_c_file.cpp
+++ b/source/fud_c_file.cpp
@@ -34,12 +34,9 @@ CBinaryFile::CBinaryFile(const String& filename, CFileMode mode, const String& e
{
}
-CBinaryFile::CBinaryFile(CBinaryFile&& rhs) :
- m_filename{std::move(rhs.m_filename)},
- m_extraFlags{std::move(rhs.m_extraFlags)},
- m_mode{std::move(rhs.m_mode)},
- m_modeFlags{std::move(rhs.m_modeFlags)},
- m_file{rhs.m_file}
+CBinaryFile::CBinaryFile(CBinaryFile&& rhs) noexcept :
+ m_filename{std::move(rhs.m_filename)}, m_extraFlags{std::move(rhs.m_extraFlags)}, m_mode{std::move(rhs.m_mode)},
+ m_modeFlags{rhs.m_modeFlags}, m_file{rhs.m_file}
{
}
@@ -47,14 +44,14 @@ CBinaryFile::~CBinaryFile() {
close();
}
-CBinaryFile& CBinaryFile::operator=(CBinaryFile&& rhs)
+CBinaryFile& CBinaryFile::operator=(CBinaryFile&& rhs) noexcept
{
close();
m_filename = std::move(rhs.m_filename);
m_extraFlags = std::move(rhs.m_extraFlags);
m_mode = std::move(rhs.m_mode);
- m_modeFlags = std::move(rhs.m_modeFlags);
+ m_modeFlags = rhs.m_modeFlags;
m_file = rhs.m_file;
return *this;
@@ -75,12 +72,9 @@ CTextFile::CTextFile(const String& filename, CFileMode mode, const String& extra
{
}
-CTextFile::CTextFile(CTextFile&& rhs) :
- m_filename{std::move(rhs.m_filename)},
- m_extraFlags{std::move(rhs.m_extraFlags)},
- m_mode{std::move(rhs.m_mode)},
- m_modeFlags{std::move(rhs.m_modeFlags)},
- m_file{rhs.m_file}
+CTextFile::CTextFile(CTextFile&& rhs) noexcept :
+ m_filename{std::move(rhs.m_filename)}, m_extraFlags{std::move(rhs.m_extraFlags)}, m_mode{std::move(rhs.m_mode)},
+ m_modeFlags{rhs.m_modeFlags}, m_file{rhs.m_file}
{
}
@@ -88,14 +82,14 @@ CTextFile::~CTextFile() {
close();
}
-CTextFile& CTextFile::operator=(CTextFile&& rhs)
+CTextFile& CTextFile::operator=(CTextFile&& rhs) noexcept
{
close();
m_filename = std::move(rhs.m_filename);
m_extraFlags = std::move(rhs.m_extraFlags);
m_mode = std::move(rhs.m_mode);
- m_modeFlags = std::move(rhs.m_modeFlags);
+ m_modeFlags = rhs.m_modeFlags;
m_file = rhs.m_file;
return *this;
diff --git a/source/fud_directory.cpp b/source/fud_directory.cpp
index 5772c95..1fcebe1 100644
--- a/source/fud_directory.cpp
+++ b/source/fud_directory.cpp
@@ -41,11 +41,10 @@ Result<DirectoryEntry, FudStatus> DirectoryEntry::fromStat(const String& name, c
size_t size{0};
if (statBuffer.st_size < 0) {
return RetType::error(FudStatus::Failure);
- } else {
- size = static_cast<size_t>(statBuffer.st_size);
}
+ size = static_cast<size_t>(statBuffer.st_size);
- DirectoryEntryType entryType;
+ DirectoryEntryType entryType{};
switch (statBuffer.st_mode & S_IFMT) {
case S_IFBLK:
entryType = DirectoryEntryType::Block;
@@ -73,11 +72,16 @@ Result<DirectoryEntry, FudStatus> DirectoryEntry::fromStat(const String& name, c
break;
}
- return RetType::okay(
- DirectoryEntry{name, statBuffer.st_ino, size, static_cast<size_t>(statBuffer.st_nlink), statBuffer.st_mtime, entryType});
+ return RetType::okay(DirectoryEntry{
+ name,
+ statBuffer.st_ino,
+ size,
+ static_cast<size_t>(statBuffer.st_nlink),
+ statBuffer.st_mtime,
+ entryType});
}
-Directory::Directory(String name) : m_name{name}
+Directory::Directory(const String& name) : m_name{name}
{
if (!m_name.valid()) {
return;
@@ -108,7 +112,7 @@ Directory::Directory(String name) : m_name{name}
m_status = FudStatus::Success;
}
-Directory::Directory(Directory&& rhs) :
+Directory::Directory(Directory&& rhs) noexcept :
m_name{std::move(rhs.m_name)}, m_directory{rhs.m_directory}, m_dirFd{rhs.m_dirFd}
{
rhs.m_directory = nullptr;
@@ -173,9 +177,8 @@ Result<std::optional<DirectoryEntry>, FudStatus> Directory::getNextEntry()
m_errorCode = errno;
m_status = FudStatus::Failure;
return RetType::error(m_status);
- } else {
- return RetType::okay(std::nullopt);
}
+ return RetType::okay(std::nullopt);
}
const char* entryNameCString = dirEntry->d_name;
@@ -202,7 +205,7 @@ Result<std::optional<DirectoryEntry>, FudStatus> Directory::getNextEntry()
if (retValue.isOkay()) {
m_errorCode = 0;
m_status = FudStatus::Success;
- return RetType::okay(retValue.getOkay());
+ return RetType::okay(std::move(retValue.getOkay()));
}
m_status = retValue.getError();
diff --git a/source/fud_sqlite.cpp b/source/fud_sqlite.cpp
index e07523d..3e39474 100644
--- a/source/fud_sqlite.cpp
+++ b/source/fud_sqlite.cpp
@@ -31,7 +31,7 @@ SqliteDb::SqliteDb(const char* name, SqliteOpenMode mode, int extraFlags) :
initialize();
}
-SqliteDb::SqliteDb(SqliteDb&& rhs) :
+SqliteDb::SqliteDb(SqliteDb&& rhs) noexcept :
m_name{std::move(rhs.m_name)}, m_nameValid{rhs.m_nameValid}, m_dbHandle{rhs.m_dbHandle},
m_errorCode{rhs.m_errorCode}, m_mode{rhs.m_mode}, m_extraFlags{rhs.m_extraFlags}
{
@@ -47,7 +47,7 @@ SqliteDb::~SqliteDb()
}
}
-SqliteDb& SqliteDb::operator=(SqliteDb&& rhs)
+SqliteDb& SqliteDb::operator=(SqliteDb&& rhs) noexcept
{
if (m_dbHandle != nullptr) {
sqlite3_close(m_dbHandle);
@@ -164,7 +164,7 @@ SqliteStatement::SqliteStatement(const SqliteDb& sqliteDb, const String& input)
}
}
-SqliteStatement::SqliteStatement(SqliteStatement&& rhs) :
+SqliteStatement::SqliteStatement(SqliteStatement&& rhs) noexcept :
m_input{std::move(rhs.m_input)}, m_tail{rhs.m_tail}, m_status{rhs.m_status}, m_errorCode{rhs.m_errorCode},
m_preparedStatement{rhs.m_preparedStatement}
{
diff --git a/source/fud_string.cpp b/source/fud_string.cpp
index 4cffb60..3a9aca0 100644
--- a/source/fud_string.cpp
+++ b/source/fud_string.cpp
@@ -46,9 +46,11 @@ ssize_t cStringLength(const char* str, size_t maxLength)
return size;
}
+// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
String::String(const utf8* cString) : String(reinterpret_cast<const char*>(cString))
{
}
+// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
String::String(const char* cString)
{
@@ -82,7 +84,7 @@ String::String(const String& rhs) : m_length{rhs.m_length}, m_capacity{rhs.m_cap
}
}
-String::String(String&& rhs) : m_length{rhs.m_length}, m_capacity{rhs.m_capacity}
+String::String(String&& rhs) noexcept : m_length{rhs.m_length}, m_capacity{rhs.m_capacity}
{
if (isLarge()) {
cleanup();
@@ -91,7 +93,7 @@ String::String(String&& rhs) : m_length{rhs.m_length}, m_capacity{rhs.m_capacity
m_data = rhs.m_data;
rhs.m_data = nullptr;
} else {
- m_buffer = std::move(rhs.m_buffer);
+ m_buffer = rhs.m_buffer;
fudAssert(nullTerminate() == FudStatus::Success);
}
}
@@ -122,7 +124,7 @@ String& String::operator=(const String& rhs)
return *this;
}
-String& String::operator=(String&& rhs)
+String& String::operator=(String&& rhs) noexcept
{
cleanup();
@@ -132,7 +134,7 @@ String& String::operator=(String&& rhs)
m_data = rhs.m_data;
rhs.m_data = nullptr;
} else {
- m_buffer = std::move(rhs.m_buffer);
+ m_buffer = rhs.m_buffer;
fudAssert(nullTerminate() == FudStatus::Success);
}
return *this;
@@ -172,7 +174,7 @@ FudStatus String::resize(size_t newCapacity)
return FudStatus::Success;
}
- utf8* newData;
+ utf8* newData = nullptr;
if (isLarge()) {
newData = static_cast<utf8*>(fudRealloc(m_data, newCapacity));
if (newData == nullptr) {
diff --git a/source/fud_utf8.cpp b/source/fud_utf8.cpp
index 50b5d31..0804531 100644
--- a/source/fud_utf8.cpp
+++ b/source/fud_utf8.cpp
@@ -33,15 +33,16 @@ FudUtf8 FudUtf8::fromString(const String& fudString, size_t index) noexcept
return fromStringView(StringView{fudString}, index);
}
-FudUtf8 FudUtf8::fromStringView(const StringView& view, size_t index) noexcept
-{
- return fromStringView(StringView{view}, index);
-}
+// FudUtf8 FudUtf8::fromStringView(const StringView& view, size_t index) noexcept
+// {
+// return fromStringView(StringView{view}, index);
+// }
-FudUtf8 FudUtf8::fromStringView(StringView&& view, size_t index) noexcept
+FudUtf8 FudUtf8::fromStringView(StringView view, size_t index) noexcept
{
- auto len = view.length();
- const auto* vData = view.data();
+ auto viewLocal{view};
+ auto len = viewLocal.length();
+ const auto* vData = viewLocal.data();
if (vData == nullptr) {
return invalidAscii();
}