pmt: Add new type (class) to libhelper, etc.

- Add Helper::Capsule.
 - Fix some compilation errors.
This commit is contained in:
2025-09-28 21:45:40 +03:00
parent 397b95466d
commit 066080c999
4 changed files with 88 additions and 10 deletions

View File

@@ -480,6 +480,81 @@ public:
}
};
// Provides a capsule structure to store variable references and values.
template <typename _Type> 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;

View File

@@ -89,8 +89,8 @@ public:
Info operator[](int index) const;
BasicInf operator[](const std::string_view &name) const;
operator std::vector<Info>() const;
operator int() const;
explicit operator std::vector<Info>() 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<Info>() const;
[[nodiscard]] explicit operator std::vector<Info>() 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;

View File

@@ -425,10 +425,12 @@ basic_partition_map_builder::operator[](const std::string_view &name) const {
}
basic_partition_map_builder::operator std::vector<Info>() const {
return _current_map;
return static_cast<std::vector<Info>>(_current_map);
}
basic_partition_map_builder::operator int() const { return _current_map; }
basic_partition_map_builder::operator int() const {
return static_cast<int>(_current_map);
}
basic_partition_map_builder::operator std::string() const { return _workdir; }

View File

@@ -55,9 +55,11 @@ int main() {
for (const auto &name : *physicals)
std::cout << " - " << name << std::endl;
if (const std::vector<PartitionMap::Info> parts = MyMap; parts.empty())
if (const std::vector<PartitionMap::Info> parts =
static_cast<std::vector<PartitionMap::Info>>(MyMap);
parts.empty())
throw PartitionMap::Error(
"operator std::vector>PartitionMap::Info>() returned empty vector");
"operator std::vector<PartitionMap::Info>() returned empty vector");
auto func = [](const std::string &partition,
const PartitionMap::BasicInf props) -> bool {