-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
abf56d2
commit 4df40cf
Showing
18 changed files
with
109 additions
and
138 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
cmake_minimum_required(VERSION 3.15) | ||
project(Platform.Exceptions) | ||
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) | ||
conan_basic_setup(TARGETS) | ||
|
||
add_library(${PROJECT_NAME}.Library INTERFACE) | ||
target_include_directories(${PROJECT_NAME}.Library INTERFACE ${PROJECT_NAME}) | ||
target_link_libraries(${PROJECT_NAME}.Library INTERFACE CONAN_PKG::platform.delegates) | ||
|
||
add_executable(${PROJECT_NAME}.Tests ${PROJECT_NAME}.Tests/AllTests.cpp) | ||
target_link_libraries(${PROJECT_NAME}.Tests PRIVATE ${PROJECT_NAME}.Library) | ||
target_link_libraries(${PROJECT_NAME}.Tests PRIVATE CONAN_PKG::gtest) | ||
set_target_properties(${PROJECT_NAME}.Tests PROPERTIES CXX_STANDARD 20) |
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,7 +1,6 @@ | ||
#include "pch.h" | ||
#include "CppUnitTest.h" | ||
using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||
#include "pch.h" // linux-like | ||
#include <gtest/gtest.h> | ||
|
||
#include "EnsuranceTests.cpp" | ||
// Due to the nature of extension methods these tests do not apply to C++ | ||
//#include "Ignore/EnsureExtensions.cpp" | ||
//#include "Ignore/IgnoredEnsuranceTests.cpp" | ||
#include "Ignore/EnsureExtensions.cpp" | ||
#include "Ignore/IgnoredEnsuranceTests.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
namespace Platform::Exceptions::Tests | ||
{ | ||
TEST_CLASS(EnsuranceTests) | ||
TEST(EnsuranceTests, ArgumentNotNullEnsuranceTest) | ||
{ | ||
public: TEST_METHOD(ArgumentNotNullEnsuranceTest) | ||
{ | ||
Assert::ExpectException<std::invalid_argument>([&]()-> auto { return Platform::Exceptions::EnsureExtensions::ArgumentNotNull<void*>(Platform::Exceptions::Ensure::Always, {}, "object"); }); | ||
} | ||
EXPECT_THROW(Always::ArgumentNotNull(nullptr, "object"), std::invalid_argument); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
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,9 +1,7 @@ | ||
namespace Platform::Exceptions::Tests::Ignore | ||
namespace Platform::Exceptions::Tests::Ignore::Always | ||
{ | ||
class EnsureExtensions | ||
void ArgumentNotNull(auto argument, const std::string& argumentName) | ||
requires std::is_pointer_v<decltype(argument)> || std::is_null_pointer_v<decltype(argument)> | ||
{ | ||
public: template <typename TArgument> static void ArgumentNotNull(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument* argument, std::string argumentName) | ||
{ | ||
} | ||
}; | ||
} | ||
} |
7 changes: 2 additions & 5 deletions
7
cpp/Platform.Exceptions.Tests/Ignore/IgnoredEnsuranceTests.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
namespace Platform::Exceptions::Tests::Ignore | ||
{ | ||
TEST_CLASS(IgnoredEnsuranceTests) | ||
TEST(IgnoredEnsuranceTests, EnsuranceIgnoredTest) | ||
{ | ||
public: TEST_METHOD(EnsuranceIgnoredTest) | ||
{ | ||
Platform::Exceptions::EnsureExtensions::ArgumentNotNull<void*>(Platform::Exceptions::Ensure::Always, {}, "object"); | ||
} | ||
EXPECT_NO_THROW(Always::ArgumentNotNull(nullptr, "object")); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
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,9 +1 @@ | ||
namespace Platform::Exceptions | ||
{ | ||
class Ensure | ||
{ | ||
public: inline static Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot Always; | ||
|
||
public: inline static Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot OnDebug; | ||
}; | ||
} | ||
/// Not need |
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,41 +1,48 @@ | ||
namespace Platform::Exceptions | ||
namespace Platform::Exceptions::Always | ||
{ | ||
class EnsureExtensions | ||
void ArgumentNotNull(auto argument, const std::string& argumentName, const std::string& message) | ||
requires std::is_pointer_v<decltype(argument)> || std::is_null_pointer_v<decltype(argument)> | ||
{ | ||
public: template <typename TArgument> static void ArgumentNotNull(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument* argument, std::string argumentName, std::string message) | ||
if (argument == nullptr) | ||
{ | ||
if (argument == nullptr) | ||
{ | ||
throw std::invalid_argument(std::string("Argument ").append(argumentName).append(" is null: ").append(message).append(1, '.')); | ||
} | ||
throw std::invalid_argument(std::string("Argument ").append(argumentName).append(" is null: ").append(message).append(1, '.')); | ||
} | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentNotNull(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument* argument, std::string argumentName) { ArgumentNotNull(root, argument, argumentName, std::string("Argument ").append(argumentName).append(" is null.")); } | ||
|
||
public: template <typename TArgument> static void ArgumentNotNull(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument* argument) { ArgumentNotNull(root, argument, {}); } | ||
|
||
public: template <typename TArgument> static void ArgumentMeetsCriteria(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argument, std::function<bool(TArgument)> predicate, std::string argumentName, std::string message) | ||
void ArgumentNotNull(auto argument, const std::string& argumentName) | ||
requires std::is_pointer_v<decltype(argument)> || std::is_null_pointer_v<decltype(argument)> | ||
{ | ||
if (argument == nullptr) | ||
{ | ||
if (!predicate(argument)) | ||
{ | ||
throw std::invalid_argument(std::string("Invalid ").append(argumentName).append(" argument: ").append(message).append(1, '.')); | ||
} | ||
throw std::invalid_argument(std::string("Argument ").append(argumentName).append(" is null.")); | ||
} | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentMeetsCriteria(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argument, std::function<bool(TArgument)> predicate, std::string argumentName) { ArgumentMeetsCriteria(root, argument, predicate, argumentName, std::string("Argument ").append(argumentName).append(" does not meet the criteria.")); } | ||
|
||
public: template <typename TArgument> static void ArgumentMeetsCriteria(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argument, std::function<bool(TArgument)> predicate) { ArgumentMeetsCriteria(root, argument, predicate, {}); } | ||
|
||
public: template <typename TArgument> static void ArgumentNotNull(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument* argument, std::string argumentName, std::string message) { Platform::Exceptions::EnsureExtensions::ArgumentNotNull(Platform::Exceptions::Ensure::Always, argument, argumentName, message); } | ||
|
||
public: template <typename TArgument> static void ArgumentNotNull(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument* argument, std::string argumentName) { Platform::Exceptions::EnsureExtensions::ArgumentNotNull(Platform::Exceptions::Ensure::Always, argument, argumentName); } | ||
|
||
public: template <typename TArgument> static void ArgumentNotNull(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument* argument) { Platform::Exceptions::EnsureExtensions::ArgumentNotNull(Platform::Exceptions::Ensure::Always, argument); } | ||
void ArgumentNotNull(auto argument) | ||
requires std::is_pointer_v<decltype(argument)> || std::is_null_pointer_v<decltype(argument)> | ||
{ | ||
if (argument == nullptr) | ||
{ | ||
throw std::invalid_argument(std::string("Argument is null.")); | ||
} | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentMeetsCriteria(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument argument, std::function<bool(TArgument)> predicate, std::string argumentName, std::string message) { Platform::Exceptions::EnsureExtensions::ArgumentMeetsCriteria(Platform::Exceptions::Ensure::Always, argument, predicate, argumentName, message); } | ||
void ArgumentMeetsCriteria(auto&& argument, auto&& predicate, const std::string& argumentName, const std::string& message) | ||
requires requires { { predicate(argument) } -> std::integral; } | ||
{ | ||
if (not predicate(std::forward<decltype(argument)>(argument))) | ||
{ | ||
throw std::invalid_argument(std::string("Invalid ").append(argumentName).append(" argument: ").append(message).append(1, '.')); | ||
} | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentMeetsCriteria(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument argument, std::function<bool(TArgument)> predicate, std::string argumentName) { Platform::Exceptions::EnsureExtensions::ArgumentMeetsCriteria(Platform::Exceptions::Ensure::Always, argument, predicate, argumentName); } | ||
void ArgumentMeetsCriteria(auto&& argument, auto&& predicate, const std::string& argumentName) | ||
{ | ||
ArgumentMeetsCriteria(std::forward<decltype(argument)>(argument), predicate, argumentName, std::string("Argument ").append(argumentName).append(" does not meet the criteria.")); | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentMeetsCriteria(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument argument, std::function<bool(TArgument)> predicate) { Platform::Exceptions::EnsureExtensions::ArgumentMeetsCriteria(Platform::Exceptions::Ensure::Always, argument, predicate); } | ||
}; | ||
void ArgumentMeetsCriteria(auto&& argument, auto&& predicate) | ||
{ | ||
ArgumentMeetsCriteria(std::forward<decltype(argument)>(argument), predicate, {}); | ||
} | ||
} |
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,34 +1,39 @@ | ||
namespace Platform::Exceptions | ||
{ | ||
class ExceptionExtensions | ||
namespace Internal | ||
{ | ||
public: inline static std::string ExceptionContentsSeparator = "---"; | ||
|
||
public: inline static std::string ExceptionStringBuildingFailed = "Unable to format exception."; | ||
|
||
public: static void Ignore(const std::exception& exception) { IgnoredExceptions::RaiseExceptionIgnoredEvent(exception); } | ||
|
||
public: static std::string ToStringWithAllInnerExceptions(const std::exception& exception) | ||
void Indent(std::string& sb, std::integral auto level) | ||
{ | ||
try | ||
{ | ||
std::string sb; | ||
BuildExceptionString(sb, exception, 0); | ||
return sb; | ||
} | ||
catch (const std::exception& ex) | ||
{ | ||
Platform::Exceptions::ExceptionExtensions::Ignore(ex); | ||
return ExceptionStringBuildingFailed; | ||
} | ||
sb.append(level, '\t'); | ||
} | ||
|
||
private: static void BuildExceptionString(std::string& sb, const std::exception& exception, std::int32_t level) | ||
void BuildExceptionString(std::string& sb, const std::exception& exception, std::integral auto level) | ||
{ | ||
Indent(sb, level); | ||
sb.append(exception.what()).append(1, '\n'); | ||
} | ||
} | ||
|
||
static const std::string ExceptionContentsSeparator = "---"; | ||
|
||
static const std::string ExceptionStringBuildingFailed = "Unable to format exception."; | ||
|
||
static void Ignore(const std::exception& exception) { IgnoredExceptions::RaiseExceptionIgnoredEvent(exception); } | ||
|
||
static std::string ToStringWithAllInnerExceptions(const std::exception& exception) | ||
{ | ||
try | ||
{ | ||
std::string sb; | ||
Internal::BuildExceptionString(sb, exception, 0); | ||
return sb; | ||
} | ||
catch (const std::exception& ex) | ||
{ | ||
Ignore(ex); | ||
return ExceptionStringBuildingFailed; | ||
} | ||
} | ||
|
||
|
||
private: static void Indent(std::string& sb, std::int32_t level) { sb.append(level, '\t'); } | ||
}; | ||
} |
7 changes: 1 addition & 6 deletions
7
cpp/Platform.Exceptions/ExtensionRoots/EnsureAlwaysExtensionRoot.h
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,6 +1 @@ | ||
namespace Platform::Exceptions::ExtensionRoots | ||
{ | ||
class EnsureAlwaysExtensionRoot | ||
{ | ||
}; | ||
} | ||
/// No need |
7 changes: 1 addition & 6 deletions
7
cpp/Platform.Exceptions/ExtensionRoots/EnsureOnDebugExtensionRoot.h
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,6 +1 @@ | ||
namespace Platform::Exceptions::ExtensionRoots | ||
{ | ||
class EnsureOnDebugExtensionRoot | ||
{ | ||
}; | ||
} | ||
/// No need |
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,6 +1 @@ | ||
namespace Platform::Exceptions::ExtensionRoots | ||
{ | ||
class ThrowExtensionRoot | ||
{ | ||
}; | ||
} | ||
/// No need |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
namespace Platform::Exceptions | ||
{ | ||
class Throw | ||
{ | ||
public: inline static Platform::Exceptions::ExtensionRoots::ThrowExtensionRoot A; | ||
}; | ||
} | ||
/// No need |
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,13 +1,10 @@ | ||
namespace Platform::Exceptions | ||
{ | ||
class ThrowExtensions | ||
{ | ||
public: static void NotSupportedException(Platform::Exceptions::ExtensionRoots::ThrowExtensionRoot root) { throw std::logic_error("Not supported exception."); } | ||
void NotSupportedException() { throw std::logic_error("Not supported exception."); } | ||
|
||
public: template <typename TReturn> static TReturn NotSupportedExceptionAndReturn(Platform::Exceptions::ExtensionRoots::ThrowExtensionRoot root) { throw std::logic_error("Not supported exception."); } | ||
auto NotSupportedExceptionAndReturn() { throw std::logic_error("Not supported exception."); } | ||
|
||
public: static void NotImplementedException(Platform::Exceptions::ExtensionRoots::ThrowExtensionRoot root) { throw std::logic_error("Not implemented exception."); } | ||
void NotImplementedException() { throw std::logic_error("Not implemented exception."); } | ||
|
||
public: template <typename TReturn> static TReturn NotImplementedExceptionAndReturn(Platform::Exceptions::ExtensionRoots::ThrowExtensionRoot root) { throw std::logic_error("Not implemented exception."); } | ||
}; | ||
auto NotImplementedExceptionAndReturn() { throw std::logic_error("Not implemented exception."); } | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[requires] | ||
gtest/cci.20210126 | ||
platform.delegates/0.1.3 | ||
|
||
[generators] | ||
cmake |