Skip to content
This repository was archived by the owner on Apr 16, 2026. It is now read-only.

Commit b34639e

Browse files
committed
Wrap magic_enum lib
1 parent 3567dd8 commit b34639e

20 files changed

Lines changed: 89 additions & 79 deletions

include/cloysterhpc/functions.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ std::string enumToString(T enumValue)
252252
return static_cast<std::string>(magic_enum::enum_name<T>(enumValue));
253253
}
254254

255+
template <typename T>
256+
std::vector<std::string> enumToStrings()
257+
{
258+
auto array = magic_enum::enum_names<T>();
259+
std::vector<std::string> output(array.begin(), array.end());
260+
return output;
261+
}
262+
263+
template <typename T>
264+
constexpr std::size_t enumCount()
265+
{
266+
return magic_enum::enum_count<T>() ;
267+
}
268+
255269
/**
256270
* @brief Converts a string to an enum if possible
257271
* @desc The comparison is made case insensitive

include/cloysterhpc/presenter/PresenterNetwork.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class PresenterNetwork : public Presenter {
6363
Network::Type type, Network::Profile profile)
6464
{
6565
return fmt::format("Select your {} ({}) network interface",
66-
magic_enum::enum_name(profile),
67-
magic_enum::enum_name(type));
66+
cloyster::utils::enumToString(profile),
67+
cloyster::utils::enumToString(type));
6868
}
6969

7070
static constexpr const auto help

src/cloyster.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <cloysterhpc/cloyster.h>
2+
#include <cloysterhpc/functions.h>
23

34
namespace cloyster {
45
bool showVersion = false;
@@ -9,7 +10,7 @@ bool enableCLI = false;
910
bool runAsDaemon = false;
1011

1112
std::string logLevelInput
12-
= fmt::format("{}", magic_enum::enum_name(Log::Level::Info));
13+
= fmt::format("{}", cloyster::utils::enumToString(Log::Level::Info));
1314
std::string answerfile = "";
1415
std::string customRepofilePath = "";
1516

src/connection.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <boost/system/detail/errc.hpp>
7+
#include <cloysterhpc/functions.h>
78
#include <cloysterhpc/connection.h>
89
#include <cloysterhpc/network.h>
910
#include <cloysterhpc/services/log.h>
@@ -316,8 +317,8 @@ void Connection::dumpConnection() const
316317
{
317318
LOG_DEBUG("Dumping Connection Info:")
318319
LOG_DEBUG("Connection with Network: {} ({})",
319-
magic_enum::enum_name(m_network->getProfile()),
320-
magic_enum::enum_name(m_network->getType()));
320+
cloyster::utils::enumToString(m_network->getProfile()),
321+
cloyster::utils::enumToString(m_network->getType()));
321322

322323
LOG_DEBUG("Interface: {}", m_interface.value_or("NONE"))
323324
LOG_DEBUG("MAC Address: {}", m_mac.value_or("NONE"))

src/main.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,9 @@ int main(int argc, const char** argv)
129129

130130

131131
cloyster::logLevelInput
132-
= fmt::format("{}", magic_enum::enum_name(Log::Level::Info));
133-
constexpr std::size_t logLevels { magic_enum::enum_count<Log::Level>() };
134-
135-
const std::vector<std::string> logLevelVector = []() {
136-
constexpr const auto& logLevelNames {
137-
magic_enum::enum_names<Log::Level>()
138-
};
139-
return std::vector<std::string> { logLevelNames.begin(),
140-
logLevelNames.end() };
141-
}();
132+
= fmt::format("{}", cloyster::utils::enumToString(Log::Level::Info));
133+
constexpr std::size_t logLevels = cloyster::utils::enumCount<Log::Level>();
134+
const std::vector<std::string> logLevelVector = cloyster::utils::enumToStrings<Log::Level>();
142135

143136
app.add_option("-l, --log-level", cloyster::logLevelInput,
144137
[&logLevelVector]() {
@@ -194,12 +187,12 @@ int main(int argc, const char** argv)
194187

195188
Log::init([]() {
196189
if (std::regex_match(cloyster::logLevelInput, std::regex("^[0-9]+$"))) {
197-
return magic_enum::enum_cast<Log::Level>(
198-
stoi(cloyster::logLevelInput))
190+
return cloyster::utils::enumOfStringOpt<Log::Level>(
191+
cloyster::logLevelInput)
199192
.value();
200193
} else {
201-
return magic_enum::enum_cast<Log::Level>(
202-
cloyster::logLevelInput, magic_enum::case_insensitive)
194+
return cloyster::utils::enumOfStringOpt<Log::Level>(
195+
cloyster::logLevelInput)
203196
.value();
204197
}
205198
}());

src/models/answerfile.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#include <cloysterhpc/models/answerfile.h>
1111
#include <cloysterhpc/services/log.h>
1212
#include <cloysterhpc/tools/nvhpc.h>
13+
#include <cloysterhpc/functions.h>
1314
#include <cstddef>
1415
#include <fmt/core.h>
1516
#include <iterator>
16-
#include <magic_enum/magic_enum.hpp>
1717
#include <ranges>
1818

1919
namespace cloyster::models {
@@ -125,7 +125,7 @@ void AnswerFile::dumpHostnameSettings()
125125

126126
void AnswerFile::dumpSystemSettings()
127127
{
128-
auto distroName = magic_enum::enum_name(system.distro);
128+
auto distroName = cloyster::utils::enumToString(system.distro);
129129

130130
m_ini.setValue("system", "disk_image", system.disk_image);
131131
m_ini.setValue("system", "distro", std::string { distroName });
@@ -183,7 +183,7 @@ void AnswerFile::dumpPostfix()
183183
return;
184184
}
185185

186-
auto profileName = magic_enum::enum_name(postfix.profile);
186+
auto profileName = cloyster::utils::enumToString(postfix.profile);
187187
m_ini.setValue("postfix", "profile", std::string { profileName });
188188
m_ini.setValue(
189189
"postfix", "smtpd_tls_cert_file", postfix.cert_file.string());
@@ -394,8 +394,8 @@ void AnswerFile::loadSystemSettings()
394394

395395
// Verify supported distros
396396
auto afDistro = m_ini.getValue("system", "distro", false, false);
397-
if (const auto& formatDistro = magic_enum::enum_cast<OS::Distro>(
398-
afDistro, magic_enum::case_insensitive)) {
397+
if (const auto& formatDistro = cloyster::utils::enumOfStringOpt<OS::Distro>(
398+
afDistro)) {
399399
system.distro = formatDistro.value();
400400
} else {
401401
if (cloyster::dryRun) {
@@ -574,9 +574,8 @@ void AnswerFile::loadPostfix()
574574
m_ini.getValue("postfix", "destination", false), boost::is_any_of(", "),
575575
boost::token_compress_on);
576576

577-
auto castProfile = magic_enum::enum_cast<Postfix::Profile>(
578-
m_ini.getValue("postfix", "profile", false),
579-
magic_enum::case_insensitive);
577+
auto castProfile = cloyster::utils::enumOfStringOpt<Postfix::Profile>(
578+
m_ini.getValue("postfix", "profile", false));
580579

581580
if (castProfile.has_value())
582581
postfix.profile = castProfile.value();

src/models/cluster.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Network& Cluster::getNetwork(Network::Profile profile)
127127

128128
throw std::runtime_error(
129129
fmt::format("Cannot get any network with the profile {}",
130-
magic_enum::enum_name(profile)));
130+
cloyster::utils::enumToString(profile)));
131131
}
132132

133133
#if 0
@@ -287,7 +287,7 @@ void Cluster::printNetworks(
287287
for (size_t i = 0; const auto& network : networks) {
288288
#endif
289289
LOG_DEBUG("Network [{}]", i++)
290-
LOG_DEBUG("Profile: {}", magic_enum::enum_name(network->getProfile()))
290+
LOG_DEBUG("Profile: {}", cloyster::utils::enumToString(network->getProfile()))
291291
LOG_DEBUG("Address: {}", network->getAddress().to_string())
292292
LOG_DEBUG("Subnet Mask: {}", network->getSubnetMask().to_string())
293293
LOG_DEBUG("Gateway: {}", network->getGateway().to_string())
@@ -565,7 +565,7 @@ void Cluster::fillData(const std::filesystem::path& answerfilePath)
565565

566566
// OS and Information
567567

568-
LOG_INFO("Distro: {}", magic_enum::enum_name(answerfil.system.distro));
568+
LOG_INFO("Distro: {}", cloyster::utils::enumToString(answerfil.system.distro));
569569
LOG_INFO("Kernel: {}", answerfil.system.kernel);
570570
LOG_INFO("Version: {}", answerfil.system.version);
571571

@@ -599,7 +599,7 @@ void Cluster::fillData(const std::filesystem::path& answerfilePath)
599599
throw std::runtime_error(
600600
fmt::format("Invalid OFED kind, expected one of {}, found {}. Edit the anwerfile {} [ofed] sectino and try again.",
601601
cloyster::answerfile,
602-
fmt::join(magic_enum::enum_names<OFED::Kind>(), ", "),
602+
fmt::join(cloyster::utils::enumToStrings<OFED::Kind>(), ", "),
603603
answerfil.ofed.kind
604604
));
605605
}

src/models/os.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void OS::setFamily(Family family) { m_family = family; }
109109

110110
void OS::setFamily(std::string_view family)
111111
{
112-
if (const auto& rv = magic_enum::enum_cast<Family>(family))
112+
if (const auto& rv = cloyster::utils::enumOfStringOpt<Family>(family))
113113
setFamily(rv.value());
114114
else
115115
throw std::runtime_error(fmt::format("Unsupported OS: {}", family));
@@ -128,8 +128,8 @@ void OS::setPlatform(std::string_view platform)
128128
std::transform(lowercasePlatform.begin(), lowercasePlatform.end(),
129129
lowercasePlatform.begin(), ::tolower);
130130

131-
for (const auto& enumValue : magic_enum::enum_values<Platform>()) {
132-
if (lowercasePlatform == magic_enum::enum_name(enumValue)) {
131+
for (const auto& enumValue : cloyster::utils::enumToStrings<Platform>()) {
132+
if (lowercasePlatform == enumValue) {
133133
setPlatform(enumValue);
134134
return;
135135
}
@@ -265,15 +265,16 @@ std::string OS::getValueFromKey(const std::string& line)
265265
std::shared_ptr<package_manager> OS::factoryPackageManager(
266266
OS::Platform platform)
267267
{
268-
for (const auto& supportedPlatform : magic_enum::enum_values<Platform>()) {
269-
if (platform == supportedPlatform) {
268+
auto platformName = cloyster::utils::enumToString(platform);
269+
for (const auto& supportedPlatform : cloyster::utils::enumToStrings<Platform>()) {
270+
if (platformName == supportedPlatform) {
270271
m_packageManager = std::make_shared<dnf>();
271272
return m_packageManager;
272273
}
273274
}
274275

275276
throw std::runtime_error(fmt::format(
276-
"Unsupported OS platform: {}", magic_enum::enum_name(platform)));
277+
"Unsupported OS platform: {}", cloyster::utils::enumToString(platform)));
277278
}
278279

279280
gsl::not_null<package_manager*> OS::packageManager() const
@@ -284,13 +285,13 @@ gsl::not_null<package_manager*> OS::packageManager() const
284285
void OS::printData() const
285286
{
286287
#ifndef NDEBUG
287-
LOG_DEBUG("Architecture: {}", magic_enum::enum_name(std::get<Arch>(m_arch)))
288-
LOG_DEBUG("Family: {}", magic_enum::enum_name(std::get<Family>(m_family)))
288+
LOG_DEBUG("Architecture: {}", cloyster::utils::enumToString(std::get<Arch>(m_arch)))
289+
LOG_DEBUG("Family: {}", cloyster::utils::enumToString(std::get<Family>(m_family)))
289290
LOG_DEBUG("Kernel Release: {}", m_kernel)
290291
LOG_DEBUG(
291-
"Platform: {}", magic_enum::enum_name(std::get<Platform>(m_platform)))
292+
"Platform: {}", cloyster::utils::enumToString(std::get<Platform>(m_platform)))
292293
LOG_DEBUG(
293-
"Distribution: {}", magic_enum::enum_name(std::get<Distro>(m_distro)))
294+
"Distribution: {}", cloyster::utils::enumToString(std::get<Distro>(m_distro)))
294295
LOG_DEBUG("Major Version: {}", m_majorVersion)
295296
LOG_DEBUG("Minor Version: {}", m_minorVersion)
296297
#endif

src/models/server.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <cloysterhpc/models/server.h>
8+
#include <cloysterhpc/functions.h>
89
#include <cloysterhpc/services/log.h>
910
#include <regex>
1011
#include <string_view>
@@ -120,7 +121,7 @@ const Connection& Server::getConnection(Network::Profile profile) const
120121

121122
throw std::runtime_error(
122123
fmt::format("Cannot get any connection with profile {}",
123-
magic_enum::enum_name(profile)));
124+
cloyster::utils::enumToString(profile)));
124125
}
125126

126127
void Server::setConnection(const std::list<Connection>& connection)

src/network.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <cloysterhpc/connection.h>
7+
#include <cloysterhpc/functions.h>
78
#include <cloysterhpc/network.h>
89
#include <cloysterhpc/services/log.h>
910

@@ -95,7 +96,7 @@ void Network::setAddress(const address& ip)
9596
throw std::runtime_error("IP address cannot be 0.0.0.0");
9697

9798
m_address = ip;
98-
LOG_TRACE("{} network address set to {}", magic_enum::enum_name(m_profile),
99+
LOG_TRACE("{} network address set to {}", cloyster::utils::enumToString(m_profile),
99100
m_address.to_string());
100101
}
101102

@@ -402,7 +403,7 @@ std::vector<address> Network::fetchNameservers()
402403
#ifndef NDEBUG
403404
void Network::dumpNetwork() const
404405
{
405-
LOG_DEBUG("Profile: {}", magic_enum::enum_name(m_profile))
406-
LOG_DEBUG("Type: {}", magic_enum::enum_name(m_type))
406+
LOG_DEBUG("Profile: {}", cloyster::utils::enumToString(m_profile))
407+
LOG_DEBUG("Type: {}", cloyster::utils::enumToString(m_type))
407408
}
408409
#endif

0 commit comments

Comments
 (0)