Skip to content

Commit

Permalink
[C++] 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
uselessgoddess committed Jul 6, 2021
1 parent abf56d2 commit 4df40cf
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 138 deletions.
13 changes: 13 additions & 0 deletions cpp/CMakeLists.txt
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)
11 changes: 5 additions & 6 deletions cpp/Platform.Exceptions.Tests/AllTests.cpp
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"
7 changes: 2 additions & 5 deletions cpp/Platform.Exceptions.Tests/EnsuranceTests.cpp
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);
};
}
9 changes: 0 additions & 9 deletions cpp/Platform.Exceptions.Tests/EnsureExtensions.cpp

This file was deleted.

10 changes: 4 additions & 6 deletions cpp/Platform.Exceptions.Tests/Ignore/EnsureExtensions.cpp
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)
{
}
};
}
}
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"));
};
}
10 changes: 0 additions & 10 deletions cpp/Platform.Exceptions.Tests/IgnoredEnsuranceTests.cpp

This file was deleted.

10 changes: 1 addition & 9 deletions cpp/Platform.Exceptions/Ensure.h
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
65 changes: 36 additions & 29 deletions cpp/Platform.Exceptions/EnsureExtensions.h
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, {});
}
}
49 changes: 27 additions & 22 deletions cpp/Platform.Exceptions/ExceptionExtensions.h
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'); }
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
namespace Platform::Exceptions::ExtensionRoots
{
class EnsureAlwaysExtensionRoot
{
};
}
/// No need
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
namespace Platform::Exceptions::ExtensionRoots
{
class EnsureOnDebugExtensionRoot
{
};
}
/// No need
7 changes: 1 addition & 6 deletions cpp/Platform.Exceptions/ExtensionRoots/ThrowExtensionRoot.h
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
namespace Platform::Exceptions::ExtensionRoots
{
class ThrowExtensionRoot
{
};
}
/// No need
2 changes: 1 addition & 1 deletion cpp/Platform.Exceptions/IgnoredExceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Platform::Exceptions

public: static void RaiseExceptionIgnoredEvent(const std::exception& exception) { ExceptionIgnored({}, exception); }

private: static void OnExceptionIgnored(void *sender, const std::exception& exception)
private: static void OnExceptionIgnored(void* sender, const std::exception& exception)
{
if (CollectExceptions)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<title>LinksPlatform's Platform.Exceptions Template Library</title>
<summary>LinksPlatform's Platform.Exceptions is a Template Library what contains abstract class templates.</summary>
<description>LinksPlatform's Platform.Exceptions is a Template Library what contains set of C++ abstract class templates. Use Platform.Exceptions.h file to include the library.</description>
<releaseNotes>Platform.Delegates.TemplateLibrary dependency updated from 0.0.14 to 0.0.15.
Fixed compile error.</releaseNotes>
<version>0.0.16</version>
<releaseNotes>Platform.Delegates.TemplateLibrary dependency updated from 0.0.15 to 0.1.3
Modernized C++ code</releaseNotes>
<version>0.1.0</version>
<authors>Konstantin Diachenko</authors>
<owners>Konstantin Diachenko</owners>
<copyright>Konstantin Diachenko</copyright>
Expand All @@ -19,7 +19,7 @@
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<dependencies>
<group targetFramework="native">
<dependency id="Platform.Delegates.TemplateLibrary" version="0.0.15" />
<dependency id="Platform.Delegates.TemplateLibrary" version="0.1.3" />
</group>
</dependencies>
</metadata>
Expand Down
8 changes: 1 addition & 7 deletions cpp/Platform.Exceptions/Throw.h
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
11 changes: 4 additions & 7 deletions cpp/Platform.Exceptions/ThrowExtensions.h
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."); }
}
6 changes: 6 additions & 0 deletions cpp/conanfile.txt
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

0 comments on commit 4df40cf

Please sign in to comment.