Skip to content

Commit

Permalink
Added missing copyright headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Reid committed Nov 5, 2024
1 parent 295839b commit 680c104
Show file tree
Hide file tree
Showing 17 changed files with 331 additions and 229 deletions.
154 changes: 80 additions & 74 deletions src/Skybolt/SkyboltCommon/Expected.h
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
46 changes: 26 additions & 20 deletions src/Skybolt/SkyboltEngine/FactoryRegistries.h
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 src/Skybolt/SkyboltEnginePlugins/Python/PythonInterpreter.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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/. */

#include "PythonInterpreter.h"
#include <SkyboltEngine/EngineRoot.h>

Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltEnginePlugins/Python/PythonInterpreter.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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 <SkyboltEngine/SkyboltEngineFwd.h>
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltEnginePlugins/Python/PythonPlugin.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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/. */

#include "PythonInterpreter.h"
#include "PythonPluginUtil.h"
#include <SkyboltEngine/EngineRoot.h>
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltEnginePlugins/Python/PythonPluginUtil.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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/. */

#include "PythonPluginUtil.h"

#include <boost/log/trivial.hpp>
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltEnginePlugins/Python/PythonPluginUtil.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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 <pybind11/pybind11.h>
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltQt/Engine/FindPython.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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/. */

#include "FindPython.h"

#include <string>
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltQt/Engine/FindPython.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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 <string>
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltQt/Widgets/ErrorLogModel.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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/. */

#include "ErrorLogModel.h"

#include <assert.h>
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltQt/Widgets/ErrorLogModel.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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 <QDateTime>
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltQt/Widgets/ErrorLogWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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/. */

#include "ErrorLogWidget.h"

#include <QDateTime>
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltQt/Widgets/ErrorLogWidget.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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 "ErrorLogModel.h"
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltQt/Widgets/StatusBar.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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/. */

#include "StatusBar.h"
#include "ErrorLogModel.h"
#include "ErrorLogWidget.h"
Expand Down
6 changes: 6 additions & 0 deletions src/Skybolt/SkyboltQt/Widgets/StatusBar.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* 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

class ErrorLogModel;
Expand Down
Loading

0 comments on commit 680c104

Please sign in to comment.