-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matthew Reid
committed
Nov 5, 2024
1 parent
295839b
commit 680c104
Showing
17 changed files
with
331 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,81 @@ | ||
#pragma once | ||
|
||
#include "Exception.h" | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <variant> | ||
#include <boost/log/trivial.hpp> | ||
|
||
namespace skybolt { | ||
|
||
struct UnexpectedMessage { std::string str; }; | ||
|
||
//! This could be replaced by std::expected (c++23) | ||
template <typename T> | ||
using Expected = std::variant<T, UnexpectedMessage>; | ||
|
||
template <typename T> | ||
std::optional<T> value(const Expected<T>& expected) | ||
{ | ||
if (std::holds_alternative<T>(expected)) | ||
{ | ||
return std::get<T>(expected); | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
template <typename T, typename FunctionT> | ||
std::optional<T> valueOrElse(const Expected<T>& expected, const FunctionT& f) | ||
{ | ||
static_assert(std::is_convertible<FunctionT, std::function<void(const UnexpectedMessage&)>>::value, "Function must take UnexpectedMessage as an argument"); | ||
|
||
if (std::holds_alternative<T>(expected)) | ||
{ | ||
return std::get<T>(expected); | ||
} | ||
|
||
f(std::get<UnexpectedMessage>(expected)); | ||
return std::nullopt; | ||
} | ||
|
||
template <typename T> | ||
std::optional<T> valueOrLogWarning(const Expected<T>& expected) | ||
{ | ||
return valueOrElse(expected, [](const auto& m) { | ||
BOOST_LOG_TRIVIAL(warning) << m.str; | ||
}); | ||
} | ||
|
||
template <typename T> | ||
std::optional<T> valueOrLogError(const Expected<T>& expected) | ||
{ | ||
return valueOrElse(expected, [](const auto& m) { | ||
BOOST_LOG_TRIVIAL(error) << m.str; | ||
}); | ||
} | ||
|
||
template <typename T, class ExceptionT> | ||
T valueOrThrow(const Expected<T>& expected) | ||
{ | ||
if (std::holds_alternative<T>(expected)) | ||
{ | ||
return std::get<T>(expected); | ||
} | ||
throw ExceptionT(std::get<UnexpectedMessage>(expected).str); | ||
} | ||
|
||
template <typename T> | ||
T valueOrThrowException(const Expected<T>& expected) | ||
{ | ||
return valueOrThrow<T, Exception>(expected); | ||
} | ||
|
||
/* Copyright Matthew Reid | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#pragma once | ||
|
||
#include "Exception.h" | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <variant> | ||
#include <boost/log/trivial.hpp> | ||
|
||
namespace skybolt { | ||
|
||
struct UnexpectedMessage { std::string str; }; | ||
|
||
//! This could be replaced by std::expected (c++23) | ||
template <typename T> | ||
using Expected = std::variant<T, UnexpectedMessage>; | ||
|
||
template <typename T> | ||
std::optional<T> value(const Expected<T>& expected) | ||
{ | ||
if (std::holds_alternative<T>(expected)) | ||
{ | ||
return std::get<T>(expected); | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
template <typename T, typename FunctionT> | ||
std::optional<T> valueOrElse(const Expected<T>& expected, const FunctionT& f) | ||
{ | ||
static_assert(std::is_convertible<FunctionT, std::function<void(const UnexpectedMessage&)>>::value, "Function must take UnexpectedMessage as an argument"); | ||
|
||
if (std::holds_alternative<T>(expected)) | ||
{ | ||
return std::get<T>(expected); | ||
} | ||
|
||
f(std::get<UnexpectedMessage>(expected)); | ||
return std::nullopt; | ||
} | ||
|
||
template <typename T> | ||
std::optional<T> valueOrLogWarning(const Expected<T>& expected) | ||
{ | ||
return valueOrElse(expected, [](const auto& m) { | ||
BOOST_LOG_TRIVIAL(warning) << m.str; | ||
}); | ||
} | ||
|
||
template <typename T> | ||
std::optional<T> valueOrLogError(const Expected<T>& expected) | ||
{ | ||
return valueOrElse(expected, [](const auto& m) { | ||
BOOST_LOG_TRIVIAL(error) << m.str; | ||
}); | ||
} | ||
|
||
template <typename T, class ExceptionT> | ||
T valueOrThrow(const Expected<T>& expected) | ||
{ | ||
if (std::holds_alternative<T>(expected)) | ||
{ | ||
return std::get<T>(expected); | ||
} | ||
throw ExceptionT(std::get<UnexpectedMessage>(expected).str); | ||
} | ||
|
||
template <typename T> | ||
T valueOrThrowException(const Expected<T>& expected) | ||
{ | ||
return valueOrThrow<T, Exception>(expected); | ||
} | ||
|
||
} // namespace skybolt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,27 @@ | ||
#pragma once | ||
|
||
#include <SkyboltCommon/Expected.h> | ||
#include <SkyboltCommon/TypedItemContainer.h> | ||
|
||
namespace skybolt { | ||
|
||
using FactoryRegistries = TypedItemContainer<Registry>; | ||
|
||
template <class T> | ||
Expected<std::shared_ptr<T>> getExpectedRegistry(const FactoryRegistries& registries) | ||
{ | ||
auto registry = registries.getFirstItemOfType<T>(); | ||
if (!registry) | ||
{ | ||
return UnexpectedMessage{ "Could not find factory registry of type: " + std::string(typeid(T).name()) }; | ||
} | ||
return registry; | ||
} | ||
|
||
/* Copyright Matthew Reid | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#pragma once | ||
|
||
#include <SkyboltCommon/Expected.h> | ||
#include <SkyboltCommon/TypedItemContainer.h> | ||
|
||
namespace skybolt { | ||
|
||
using FactoryRegistries = TypedItemContainer<Registry>; | ||
|
||
template <class T> | ||
Expected<std::shared_ptr<T>> getExpectedRegistry(const FactoryRegistries& registries) | ||
{ | ||
auto registry = registries.getFirstItemOfType<T>(); | ||
if (!registry) | ||
{ | ||
return UnexpectedMessage{ "Could not find factory registry of type: " + std::string(typeid(T).name()) }; | ||
} | ||
return registry; | ||
} | ||
|
||
} // namespace skybolt |
6 changes: 6 additions & 0 deletions
6
src/Skybolt/SkyboltEnginePlugins/Python/PythonInterpreter.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.