Skip to content

Commit

Permalink
Remove the use of Boost.Variant and Boost.Optional (#317)
Browse files Browse the repository at this point in the history
- removed the use of Boost.Variant and Boost.Optional since they are no longer relevant after moving to C++17 and GCC 9+.
  • Loading branch information
dnzbk committed Jul 19, 2024
1 parent b09d1f4 commit a7ac9a9
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 107 deletions.
16 changes: 8 additions & 8 deletions daemon/extension/Extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ namespace Extension
optionJson["Multi"] = option.section.multi;
optionJson["Prefix"] = option.section.prefix;

if (const std::string* val = boost::variant2::get_if<std::string>(&option.value))
if (const std::string* val = std::get_if<std::string>(&option.value))
{
optionJson["Value"] = *val;
}
else if (const double* val = boost::variant2::get_if<double>(&option.value))
else if (const double* val = std::get_if<double>(&option.value))
{
optionJson["Value"] = *val;
}
Expand All @@ -287,11 +287,11 @@ namespace Extension

for (const auto& value : option.select)
{
if (const std::string* val = boost::variant2::get_if<std::string>(&value))
if (const std::string* val = std::get_if<std::string>(&value))
{
selectJson.push_back(Json::JsonValue(*val));
}
else if (const double* val = boost::variant2::get_if<double>(&value))
else if (const double* val = std::get_if<double>(&value))
{
selectJson.push_back(Json::JsonValue(*val));
}
Expand Down Expand Up @@ -396,23 +396,23 @@ namespace Extension
AddNewNode(optionsNode, "Section", "string", option.section.name.c_str());
AddNewNode(optionsNode, "Prefix", "string", option.section.prefix.c_str());

if (const std::string* val = boost::variant2::get_if<std::string>(&option.value))
if (const std::string* val = std::get_if<std::string>(&option.value))
{
AddNewNode(optionsNode, "Value", "string", val->c_str());
}
else if (const double* val = boost::variant2::get_if<double>(&option.value))
else if (const double* val = std::get_if<double>(&option.value))
{
AddNewNode(optionsNode, "Value", "number", std::to_string(*val).c_str());
}

xmlNodePtr selectNode = xmlNewNode(NULL, BAD_CAST "Select");
for (const auto& selectOption : option.select)
{
if (const std::string* val = boost::variant2::get_if<std::string>(&selectOption))
if (const std::string* val = std::get_if<std::string>(&selectOption))
{
AddNewNode(selectNode, "Value", "string", val->c_str());
}
else if (const double* val = boost::variant2::get_if<double>(&selectOption))
else if (const double* val = std::get_if<double>(&selectOption))
{
AddNewNode(selectNode, "Value", "number", std::to_string(*val).c_str());
}
Expand Down
4 changes: 2 additions & 2 deletions daemon/extension/ExtensionLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ namespace ExtensionLoader
{
ManifestFile::Option option{};
ParseSectionAndSet<ManifestFile::Option>(option, currSectionName, line, eqPos);
bool canBeNum = !selectOpts.empty() && boost::variant2::get_if<double>(&selectOpts[0]);
bool canBeNum = !selectOpts.empty() && std::get_if<double>(&selectOpts[0]);
std::string value = line.substr(eqPos + 1);
Util::Trim(value);
option.value = GetSelectOpt(value, canBeNum);
Expand Down Expand Up @@ -338,7 +338,7 @@ namespace ExtensionLoader
auto result = Util::StrToNum(val);
if (result.has_value())
{
return ManifestFile::SelectOption(result.get());
return ManifestFile::SelectOption(result.value());
}

return ManifestFile::SelectOption(val);
Expand Down
26 changes: 13 additions & 13 deletions daemon/extension/ExtensionManager.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2023 Denis <denis@nzbget.com>
* Copyright (C) 2023-2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -55,7 +55,7 @@ namespace ExtensionManager
return std::make_pair(status, std::move(tmpFileName));
}

boost::optional<std::string>
std::optional<std::string>
Manager::UpdateExtension(const std::string& filename, const std::string& extName)
{
std::unique_lock<std::shared_mutex> lock{m_mutex};
Expand Down Expand Up @@ -89,10 +89,10 @@ namespace ExtensionManager
}

m_extensions.erase(extensionIt);
return boost::none;
return std::nullopt;
}

boost::optional<std::string>
std::optional<std::string>
Manager::InstallExtension(const std::string& filename, const std::string& dest)
{
if (Util::EmptyStr(g_Options->GetSevenZipCmd()))
Expand Down Expand Up @@ -129,10 +129,10 @@ namespace ExtensionManager
return "Failed to delete temp file: " + filename;
}

return boost::none;
return std::nullopt;
}

boost::optional<std::string>
std::optional<std::string>
Manager::DeleteExtension(const std::string& name)
{
std::unique_lock<std::shared_mutex> lock{m_mutex};
Expand All @@ -155,10 +155,10 @@ namespace ExtensionManager
}

m_extensions.erase(extensionIt);
return boost::none;
return std::nullopt;
}

boost::optional<std::string>
std::optional<std::string>
Manager::LoadExtensions()
{
const char* scriptDir = g_Options->GetScriptDir();
Expand All @@ -181,10 +181,10 @@ namespace ExtensionManager
CreateTasks();
m_extensions.shrink_to_fit();

return boost::none;
return std::nullopt;
}

boost::optional<std::string>
std::optional<std::string>
Manager::DeleteExtension(const Extension::Script& ext)
{
const char* location = ext.GetLocation();
Expand All @@ -208,14 +208,14 @@ namespace ExtensionManager
CString err;
if (!FileSystem::DeleteDirectoryWithContent(location, err))
{
return boost::optional<std::string>(err.Str());
return std::optional<std::string>(err.Str());
}

return boost::none;
return std::nullopt;
}
else if (FileSystem::FileExists(location) && FileSystem::DeleteFile(location))
{
return boost::none;
return std::nullopt;
}

return std::string("Failed to delete ") + location;
Expand Down
12 changes: 6 additions & 6 deletions daemon/extension/ExtensionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <utility>
#include <memory>
#include <shared_mutex>
#include <boost/optional.hpp>
#include <optional>
#include "WebDownloader.h"
#include "Options.h"
#include "Extension.h"
Expand All @@ -45,16 +45,16 @@ namespace ExtensionManager
Manager(Manager&&) = delete;
Manager& operator=(Manager&&) = delete;

boost::optional<std::string>
std::optional<std::string>
InstallExtension(const std::string& filename, const std::string& dest);

boost::optional<std::string>
std::optional<std::string>
UpdateExtension(const std::string& filename, const std::string& extName);

boost::optional<std::string>
std::optional<std::string>
DeleteExtension(const std::string& name);

boost::optional<std::string>
std::optional<std::string>
LoadExtensions();

std::pair<WebDownloader::EStatus, std::string>
Expand All @@ -69,7 +69,7 @@ namespace ExtensionManager
bool Exists(const std::string& name) const;
void Sort(const char* order);
std::string GetExtensionName(const std::string& fileName) const;
boost::optional<std::string>
std::optional<std::string>
DeleteExtension(const Extension::Script& ext);

Extensions m_extensions;
Expand Down
6 changes: 3 additions & 3 deletions daemon/extension/ManifestFile.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2023 Denis <denis@nzbget.com>
* Copyright (C) 2023-2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,13 +22,13 @@

#include <string>
#include <vector>
#include <boost/variant2.hpp>
#include <variant>
#include "Json.h"
#include "Util.h"

namespace ManifestFile
{
using SelectOption = boost::variant2::variant<double, std::string>;
using SelectOption = std::variant<double, std::string>;

extern const char* MANIFEST_FILE;
extern const char* DEFAULT_SECTION_NAME;
Expand Down
3 changes: 3 additions & 0 deletions daemon/main/nzbget.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2007-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2023-2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -210,6 +211,8 @@ compiled */
#include <shared_mutex>
#include <condition_variable>
#include <chrono>
#include <optional>
#include <variant>

#include <libxml/parser.h>
#include <libxml/xmlreader.h>
Expand Down
9 changes: 5 additions & 4 deletions daemon/remote/XmlRpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2007-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2023-2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -2731,7 +2732,7 @@ void LoadExtensionsXmlCommand::Execute()
const auto& error = g_ExtensionManager->LoadExtensions();
if (error)
{
BuildErrorResponse(3, error.get().c_str());
BuildErrorResponse(3, error.value().c_str());
return;
}
}
Expand Down Expand Up @@ -2797,7 +2798,7 @@ void DownloadExtensionXmlCommand::Execute()
const auto error = g_ExtensionManager->InstallExtension(filename, scriptDir);
if (error)
{
BuildErrorResponse(3, error.get().c_str());
BuildErrorResponse(3, error.value().c_str());
return;
}

Expand Down Expand Up @@ -2834,7 +2835,7 @@ void UpdateExtensionXmlCommand::Execute()
const auto error = g_ExtensionManager->UpdateExtension(filename, extName);
if (error)
{
BuildErrorResponse(3, error.get().c_str());
BuildErrorResponse(3, error.value().c_str());
return;
}

Expand All @@ -2854,7 +2855,7 @@ void DeleteExtensionXmlCommand::Execute()
const auto error = g_ExtensionManager->DeleteExtension(extName);
if (error)
{
BuildErrorResponse(2, error.get().c_str());
BuildErrorResponse(2, error.value().c_str());
return;
}

Expand Down
4 changes: 2 additions & 2 deletions daemon/util/ScriptController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ void ScriptController::PrepareArgs()
}
else
{
strncpy(m_cmdLine, found.get().c_str(), sizeof(m_cmdLine) - 1);
m_cmdArgs.emplace_back(found.get().c_str());
strncpy(m_cmdLine, found.value().c_str(), sizeof(m_cmdLine) - 1);
m_cmdArgs.emplace_back(found.value().c_str());
}
}
else
Expand Down
20 changes: 10 additions & 10 deletions daemon/util/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2007-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2023 Denis <denis@nzbget.com>
* Copyright (C) 2023-2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -114,7 +114,7 @@ void Util::Init()
CurrentTicks();
}

boost::optional<std::string>
std::optional<std::string>
Util::FindExecutorProgram(const std::string& filename, const std::string& customPath)
{
size_t idx = filename.find_last_of(".");
Expand Down Expand Up @@ -164,7 +164,7 @@ Util::FindExecutorProgram(const std::string& filename, const std::string& custom
{
return std::string("py");
}
return boost::none;
return std::nullopt;
}

if (fileExt == ".sh")
Expand All @@ -179,7 +179,7 @@ Util::FindExecutorProgram(const std::string& filename, const std::string& custom
{
return std::string("sh");
}
return boost::none;
return std::nullopt;
}

if (fileExt == ".js")
Expand All @@ -189,7 +189,7 @@ Util::FindExecutorProgram(const std::string& filename, const std::string& custom
{
return std::string("node");
}
return boost::none;
return std::nullopt;
}

if (fileExt == ".cmd" || fileExt == ".bat")
Expand All @@ -199,7 +199,7 @@ Util::FindExecutorProgram(const std::string& filename, const std::string& custom
{
return filename;
}
return boost::none;
return std::nullopt;
}

if (fileExt == ".exe")
Expand All @@ -221,7 +221,7 @@ void Util::SplitInt64(int64 Int64, uint32* Hi, uint32* Lo)
*Lo = (uint32)(Int64 & 0xFFFFFFFF);
}

boost::optional<double>
std::optional<double>
Util::StrToNum(const std::string& str)
{
std::istringstream ss(str);
Expand All @@ -231,13 +231,13 @@ Util::StrToNum(const std::string& str)
{
if (!ss.eof())
{
return boost::none;
return std::nullopt;
}

return boost::optional<double>{ num };
return { num };
}

return boost::none;
return std::nullopt;
}

/* Base64 decryption is taken from
Expand Down
Loading

0 comments on commit a7ac9a9

Please sign in to comment.