summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2020-01-20 13:29:21 +0100
committerDavid Lönnhager <david.l@mullvad.net>2020-01-20 17:04:56 +0100
commite6ea3bd0d617a91645d96e89459f988bd67b4117 (patch)
treefe7fa25de9f95c66b1da8e2730fdfcb253f6cd31
parentc0833b0589424bf21e14afec6b57ab742886a763 (diff)
downloadmullvadvpn-e6ea3bd0d617a91645d96e89459f988bd67b4117.tar.xz
mullvadvpn-e6ea3bd0d617a91645d96e89459f988bd67b4117.zip
Refactor ValueMapper
-rw-r--r--windows/libshared/src/libshared/logging/logsinkadapter.cpp32
-rw-r--r--windows/nsis-plugins/src/driverlogic/driverlogic.cpp15
-rw-r--r--windows/winnet/src/winnet/winnet.cpp25
-rw-r--r--windows/winutil/src/winutil/winutil.cpp15
4 files changed, 22 insertions, 65 deletions
diff --git a/windows/libshared/src/libshared/logging/logsinkadapter.cpp b/windows/libshared/src/libshared/logging/logsinkadapter.cpp
index f506aba096..03244cb423 100644
--- a/windows/libshared/src/libshared/logging/logsinkadapter.cpp
+++ b/windows/libshared/src/libshared/logging/logsinkadapter.cpp
@@ -1,4 +1,5 @@
#include "stdafx.h"
+#include <libcommon/valuemapper.h>
#include "logsinkadapter.h"
namespace shared::logging
@@ -19,30 +20,15 @@ common::logging::LogTarget LogSinkAdapter::MakeAdapter(MullvadLogSink target, vo
return;
}
- //
- // TODO: Replace manual mapping with ValueMapper once the updated
- // ValueMapper reaches libcommon.
- //
+ const std::optional<MULLVAD_LOG_LEVEL> translatedLevel = common::ValueMapper::TryMap<>(level, {
+ std::make_pair(common::logging::LogLevel::Warning, MULLVAD_LOG_LEVEL_WARNING),
+ std::make_pair(common::logging::LogLevel::Info, MULLVAD_LOG_LEVEL_INFO),
+ std::make_pair(common::logging::LogLevel::Trace, MULLVAD_LOG_LEVEL_TRACE),
+ std::make_pair(common::logging::LogLevel::Debug, MULLVAD_LOG_LEVEL_DEBUG),
+ std::make_pair(common::logging::LogLevel::Error, MULLVAD_LOG_LEVEL_ERROR),
+ });
- const MULLVAD_LOG_LEVEL translatedLevel = [level]()
- {
- switch (level)
- {
- case common::logging::LogLevel::Warning:
- return MULLVAD_LOG_LEVEL_WARNING;
- case common::logging::LogLevel::Info:
- return MULLVAD_LOG_LEVEL_INFO;
- case common::logging::LogLevel::Trace:
- return MULLVAD_LOG_LEVEL_TRACE;
- case common::logging::LogLevel::Debug:
- return MULLVAD_LOG_LEVEL_DEBUG;
- case common::logging::LogLevel::Error:
- default:
- return MULLVAD_LOG_LEVEL_ERROR;
- }
- }();
-
- target(translatedLevel, msg, context);
+ target(translatedLevel.value_or(MULLVAD_LOG_LEVEL_ERROR), msg, context);
};
}
diff --git a/windows/nsis-plugins/src/driverlogic/driverlogic.cpp b/windows/nsis-plugins/src/driverlogic/driverlogic.cpp
index e57b08327b..d82cde2f4d 100644
--- a/windows/nsis-plugins/src/driverlogic/driverlogic.cpp
+++ b/windows/nsis-plugins/src/driverlogic/driverlogic.cpp
@@ -127,16 +127,11 @@ void __declspec(dllexport) NSISCALL EstablishBaseline
try
{
- using value_type = common::ValueMapper<Context::BaselineStatus, EstablishBaselineStatus>::value_type;
-
- const common::ValueMapper<Context::BaselineStatus, EstablishBaselineStatus> mapper =
- {
- value_type(Context::BaselineStatus::NO_TAP_ADAPTERS_PRESENT, EstablishBaselineStatus::NO_TAP_ADAPTERS_PRESENT),
- value_type(Context::BaselineStatus::SOME_TAP_ADAPTERS_PRESENT, EstablishBaselineStatus::SOME_TAP_ADAPTERS_PRESENT),
- value_type(Context::BaselineStatus::MULLVAD_ADAPTER_PRESENT, EstablishBaselineStatus::MULLVAD_ADAPTER_PRESENT)
- };
-
- const auto status = mapper.map(g_context->establishBaseline());
+ const auto status = common::ValueMapper::Map(g_context->establishBaseline(), {
+ std::make_pair(Context::BaselineStatus::NO_TAP_ADAPTERS_PRESENT, EstablishBaselineStatus::NO_TAP_ADAPTERS_PRESENT),
+ std::make_pair(Context::BaselineStatus::SOME_TAP_ADAPTERS_PRESENT, EstablishBaselineStatus::SOME_TAP_ADAPTERS_PRESENT),
+ std::make_pair(Context::BaselineStatus::MULLVAD_ADAPTER_PRESENT, EstablishBaselineStatus::MULLVAD_ADAPTER_PRESENT)
+ });
pushstring(L"");
pushint(status);
diff --git a/windows/winnet/src/winnet/winnet.cpp b/windows/winnet/src/winnet/winnet.cpp
index 3c8f133d57..55bfca5b39 100644
--- a/windows/winnet/src/winnet/winnet.cpp
+++ b/windows/winnet/src/winnet/winnet.cpp
@@ -7,6 +7,7 @@
#include <libshared/logging/unwind.h>
#include <libshared/network/interfaceutils.h>
#include <libcommon/error.h>
+#include <libcommon/valuemapper.h>
#include <libcommon/network.h>
#include <cstdint>
#include <stdexcept>
@@ -522,26 +523,6 @@ WinNet_DeleteRoute(
}
}
-//
-// TODO: Move to libcommon.
-//
-struct ValueMapper
-{
- template<typename T, typename U, std::size_t S>
- static U map(T t, const std::pair<T, U> (&dictionary)[S])
- {
- for (const auto &entry : dictionary)
- {
- if (t == entry.first)
- {
- return entry.second;
- }
- }
-
- throw std::runtime_error("Could not map between values");
- }
-};
-
extern "C"
WINNET_LINKAGE
bool
@@ -577,7 +558,7 @@ WinNet_RegisterDefaultRouteChangedCallback(
{ from_t::Removed, WINNET_DEFAULT_ROUTE_CHANGED_EVENT_TYPE_REMOVED }
};
- const auto translatedEventType = ValueMapper::map<>(eventType, eventTypeMap);
+ const auto translatedEventType = common::ValueMapper::Map<>(eventType, eventTypeMap);
//
// Translate the family type.
@@ -589,7 +570,7 @@ WinNet_RegisterDefaultRouteChangedCallback(
{ static_cast<ADDRESS_FAMILY>(AF_INET6), WINNET_IP_FAMILY_V6 }
};
- const auto translatedFamily = ValueMapper::map<>(family, familyMap);
+ const auto translatedFamily = common::ValueMapper::Map<>(family, familyMap);
//
// Determine which LUID to forward.
diff --git a/windows/winutil/src/winutil/winutil.cpp b/windows/winutil/src/winutil/winutil.cpp
index 4ea0ac27e3..b5b809a216 100644
--- a/windows/winutil/src/winutil/winutil.cpp
+++ b/windows/winutil/src/winutil/winutil.cpp
@@ -16,16 +16,11 @@ WinUtil_MigrateAfterWindowsUpdate(
{
try
{
- using value_type = common::ValueMapper<migration::MigrationStatus, WINUTIL_MIGRATION_STATUS>::value_type;
-
- const common::ValueMapper<migration::MigrationStatus, WINUTIL_MIGRATION_STATUS> mapper =
- {
- value_type(migration::MigrationStatus::Success, WINUTIL_MIGRATION_STATUS_SUCCESS),
- value_type(migration::MigrationStatus::Aborted, WINUTIL_MIGRATION_STATUS_ABORTED),
- value_type(migration::MigrationStatus::NothingToMigrate, WINUTIL_MIGRATION_STATUS_NOTHING_TO_MIGRATE),
- };
-
- return mapper.map(migration::MigrateAfterWindowsUpdate());
+ return common::ValueMapper::Map(migration::MigrateAfterWindowsUpdate(), {
+ std::make_pair(migration::MigrationStatus::Success, WINUTIL_MIGRATION_STATUS_SUCCESS),
+ std::make_pair(migration::MigrationStatus::Aborted, WINUTIL_MIGRATION_STATUS_ABORTED),
+ std::make_pair(migration::MigrationStatus::NothingToMigrate, WINUTIL_MIGRATION_STATUS_NOTHING_TO_MIGRATE),
+ });
}
catch (const std::exception &err)
{