diff --git a/srclib/libhelper/include/libhelper/lib.hpp b/srclib/libhelper/include/libhelper/lib.hpp index 4f54836..b6e0ba4 100644 --- a/srclib/libhelper/include/libhelper/lib.hpp +++ b/srclib/libhelper/include/libhelper/lib.hpp @@ -480,6 +480,81 @@ public: } }; +// Provides a capsule structure to store variable references and values. +template class Capsule { +public: + _Type &value; + + // The value to be stored is taken as a reference as an argument + explicit Capsule(_Type &value) noexcept : value(value) {} + + // Set the value. + void set(const _Type &_value) noexcept { this->value = _value; } + void set(_Type &_value) noexcept { this->value = _value; } + + // Get reference of the value. + _Type &get() noexcept { return this->value; } + const _Type &get() const noexcept { return this->value; } + + // You can get the reference of the stored value in the input type (casting is + // required). + operator _Type &() noexcept { return this->value; } + operator const _Type &() const noexcept { return this->value; } + explicit operator _Type *() noexcept { return &this->value; } + + // The value of another capsule is taken. + Capsule &operator=(const Capsule &other) noexcept { + this->value = other.value; + return *this; + } + + // Assign another value. + Capsule &operator=(const _Type &_value) noexcept { + this->value = _value; + return *this; + } + + // Check if this capsule and another capsule hold the same data. + bool operator==(const Capsule &other) const noexcept { + return this->value == other.value; + } + + // Check if this capsule value and another capsule value hold the same data. + bool operator==(const _Type &_value) const noexcept { + return this->value == _value; + } + + // Check that this capsule and another capsule do not hold the same data. + bool operator!=(const Capsule &other) const noexcept { + return !(*this == other); + } + + // Check that this capsule value and another capsule value do not hold the + // same data. + bool operator!=(const _Type &_value) const noexcept { + return !(*this == _value); + } + + // Check if the current held value is actually empty. + explicit operator bool() const noexcept { return this->value != _Type{}; } + + // Check that the current held value is actually empty. + bool operator!() const noexcept { return this->value == _Type{}; } + + // Change the value with the input operator. + friend Capsule &operator>>(const _Type &_value, Capsule &_capsule) noexcept { + _capsule.value = _value; + return _capsule; + } + + // Get the reference of the value held. + _Type &operator()() noexcept { return value; } + const _Type &operator()() const noexcept { return value; } + + // Set the value. + void operator()(const _Type &_value) noexcept { this->value = _value; } +}; + namespace LoggingProperties { extern std::string_view FILE, NAME; extern bool PRINT, DISABLE; diff --git a/srclib/libpartition_map/include/libpartition_map/lib.hpp b/srclib/libpartition_map/include/libpartition_map/lib.hpp index b4bc9b9..2113f78 100644 --- a/srclib/libpartition_map/include/libpartition_map/lib.hpp +++ b/srclib/libpartition_map/include/libpartition_map/lib.hpp @@ -89,8 +89,8 @@ public: Info operator[](int index) const; BasicInf operator[](const std::string_view &name) const; - operator std::vector() const; - operator int() const; + explicit operator std::vector() const; + explicit operator int() const; class iterator { public: @@ -166,7 +166,6 @@ public: /** * Returns the current list content in Map_t type. - * If no list is created, returns std::nullopt. */ [[nodiscard]] Map_t getAll() const; @@ -368,17 +367,17 @@ public: /** * Get map contents as vector (PartitionManager::Info type). */ - [[nodiscard]] operator std::vector() const; + [[nodiscard]] explicit operator std::vector() const; /** * Get total partition count in map (int type). */ - [[nodiscard]] operator int() const; + [[nodiscard]] explicit operator int() const; /** * Get current working directory. */ - [[nodiscard]] operator std::string() const; + [[nodiscard]] explicit operator std::string() const; }; using Error = Helper::Error; diff --git a/srclib/libpartition_map/src/PartitionMap.cpp b/srclib/libpartition_map/src/PartitionMap.cpp index 29eae5b..1d5e68b 100644 --- a/srclib/libpartition_map/src/PartitionMap.cpp +++ b/srclib/libpartition_map/src/PartitionMap.cpp @@ -425,10 +425,12 @@ basic_partition_map_builder::operator[](const std::string_view &name) const { } basic_partition_map_builder::operator std::vector() const { - return _current_map; + return static_cast>(_current_map); } -basic_partition_map_builder::operator int() const { return _current_map; } +basic_partition_map_builder::operator int() const { + return static_cast(_current_map); +} basic_partition_map_builder::operator std::string() const { return _workdir; } diff --git a/srclib/libpartition_map/tests/test.cpp b/srclib/libpartition_map/tests/test.cpp index 6cef785..d4e7326 100644 --- a/srclib/libpartition_map/tests/test.cpp +++ b/srclib/libpartition_map/tests/test.cpp @@ -55,9 +55,11 @@ int main() { for (const auto &name : *physicals) std::cout << " - " << name << std::endl; - if (const std::vector parts = MyMap; parts.empty()) + if (const std::vector parts = + static_cast>(MyMap); + parts.empty()) throw PartitionMap::Error( - "operator std::vector>PartitionMap::Info>() returned empty vector"); + "operator std::vector() returned empty vector"); auto func = [](const std::string &partition, const PartitionMap::BasicInf props) -> bool {