1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Define string em Exceções com std::optional

This commit is contained in:
Danilo Borges Santos 2024-12-04 09:28:03 -03:00
parent a3f7969d1a
commit 7bc21eec80
3 changed files with 110 additions and 119 deletions

View File

@ -127,25 +127,27 @@ namespace csharp {
static constexpr size_t HR_ERROR_DLL_INIT_FAILED = 0x8007045A; static constexpr size_t HR_ERROR_DLL_INIT_FAILED = 0x8007045A;
static constexpr size_t HR_MSEE_E_ASSEMBLYLOADINPROGRESS = 0x80131016; static constexpr size_t HR_MSEE_E_ASSEMBLYLOADINPROGRESS = 0x80131016;
static constexpr size_t HR_ERROR_FILE_INVALID = 0x800703EE; static constexpr size_t HR_ERROR_FILE_INVALID = 0x800703EE;
}; };
using OptinalString = std::optional<std::string>; using OptionalString = std::optional<std::string>;
#define EXCEPTION_SOURCE_LOCATION = std::source_location source = std::source_location::current()
class Exception : public std::runtime_error { class Exception : public std::runtime_error {
public: public:
Exception(std::source_location const& source = std::source_location::current()) Exception(OptionalString const& message = std::nullopt, std::source_location source = std::source_location::current())
: Source(source), std::runtime_error("") {}; : Source(source), message(message), std::runtime_error(message.value_or(""))
Exception(std::string const& message, std::source_location source = std::source_location::current()) {}
: Source(source), message(message), std::runtime_error(message) {} Exception(OptionalString const& message, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
Exception(std::string const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current()) : Source(source), message(message), innerException(innerException), std::runtime_error(message.value_or(""))
: Source(source), message(message), innerException(innerException), std::runtime_error(message) {} {}
constexpr virtual std::string Message() const { return message; } constexpr virtual std::string Message() const { return message.value_or(""); }
constexpr virtual std::string FullMessage() const { constexpr virtual std::string FullMessage() const {
std::string msg; std::string msg;
msg.append(message); msg.append(message.value_or(""));
msg.append(" In: "); msg.append(" In: ");
msg.append(Source.file_name()); msg.append(Source.file_name());
msg.append(" ("); msg.append(" (");
@ -171,112 +173,107 @@ namespace csharp {
size_t HRresult{ HResults::HR_COR_E_EXCEPTION }; size_t HRresult{ HResults::HR_COR_E_EXCEPTION };
private: private:
std::string message; OptionalString message;
std::shared_ptr<Exception> innerException; std::shared_ptr<Exception> innerException;
}; };
//The exception that is thrown when one of the arguments provided to a method is not valid. //The exception that is thrown when one of the arguments provided to a method is not valid.
class ArgumentException : public Exception { class ArgumentException : public Exception {
public: public:
ArgumentException(std::source_location const& source = std::source_location::current()): Exception(SR::Arg_ArgumentException, source) ArgumentException(OptionalString const& message = std::nullopt, std::source_location const& source = std::source_location::current())
{ : Exception(message.value_or(SR::Arg_ArgumentException), source)
HRresult = HResults::HR_COR_E_ARGUMENT;
}
ArgumentException(std::string const& message, std::source_location const& source = std::source_location::current())
: Exception(!message.empty() ? message : SR::Arg_ArgumentException, source)
{ {
HRresult = HResults::HR_COR_E_ARGUMENT; HRresult = HResults::HR_COR_E_ARGUMENT;
}; };
ArgumentException(std::string const& message, std::string const& paramName, std::source_location const& source = std::source_location::current()) ArgumentException(OptionalString const& paramName, OptionalString const& message, std::source_location const& source = std::source_location::current())
: paramName(paramName), Exception(!message.empty() ? message : SR::Arg_ArgumentException, source) : paramName(paramName), Exception(message.value_or(SR::Arg_ArgumentException), source)
{ {
HRresult = HResults::HR_COR_E_ARGUMENT; HRresult = HResults::HR_COR_E_ARGUMENT;
}; };
ArgumentException(std::string const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current()) ArgumentException(OptionalString const& message, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
: Exception(!message.empty() ? message : SR::Arg_ArgumentException, innerException, source) : Exception(message.value_or(SR::Arg_ArgumentException), innerException, source)
{ {
HRresult = HResults::HR_COR_E_ARGUMENT; HRresult = HResults::HR_COR_E_ARGUMENT;
} }
ArgumentException(std::string const& message, std::string const& paramName, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current()) ArgumentException(OptionalString const& paramName, OptionalString const& message, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
: paramName(paramName), Exception(!message.empty() ? message : SR::Arg_ArgumentException, innerException, source) : paramName(paramName), Exception(message.value_or(SR::Arg_ArgumentException), innerException, source)
{ {
HRresult = HResults::HR_COR_E_ARGUMENT; HRresult = HResults::HR_COR_E_ARGUMENT;
} }
constexpr std::string Message() const override { constexpr std::string Message() const override {
if(!paramName.empty()) if(paramName.has_value() && !paramName.value().empty())
return Exception::Message().append(" Parameter: " + paramName + "."); return Exception::Message().append(" Parameter: " + paramName.value() + ".");
return Exception::Message(); return Exception::Message();
} }
constexpr std::string FullMessage() const override { constexpr std::string FullMessage() const override {
if(!paramName.empty()) if (paramName.has_value() && !paramName.value().empty())
return Exception::FullMessage().append(" Parameter: " + paramName + "."); return Exception::FullMessage().append(" Parameter: " + paramName.value() + ".");
return Exception::Message(); return Exception::Message();
} }
private: private:
std::string paramName; std::optional<std::string> paramName;
}; };
//The exception that is thrown when one of the arguments provided to a method is null. //The exception that is thrown when one of the arguments provided to a method is null.
class ArgumentNullException : public ArgumentException { class ArgumentNullException : public ArgumentException {
public: public:
ArgumentNullException(std::source_location const& source = std::source_location::current()) : ArgumentException(SR::ArgumentNull_Generic, source) ArgumentNullException(OptionalString const& message = std::nullopt, std::source_location const& source = std::source_location::current())
{ : ArgumentException(message.value_or(SR::ArgumentNull_Generic), source)
HRresult = HResults::HR_E_POINTER;
}
ArgumentNullException(std::string const& message, std::source_location const& source = std::source_location::current())
: ArgumentException(!message.empty() ? message : SR::ArgumentNull_Generic, source)
{ {
HRresult = HResults::HR_E_POINTER; HRresult = HResults::HR_E_POINTER;
}; };
ArgumentNullException(std::string const& message, std::string const& paramName, std::source_location const& source = std::source_location::current()) ArgumentNullException(OptionalString const& paramName, OptionalString const& message, std::source_location const& source = std::source_location::current())
: ArgumentException(!message.empty() ? message : SR::ArgumentNull_Generic, paramName, source) : ArgumentException(paramName, message.value_or(SR::ArgumentNull_Generic), source)
{ {
HRresult = HResults::HR_E_POINTER; HRresult = HResults::HR_E_POINTER;
}; };
ArgumentNullException(std::string const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current()) ArgumentNullException(OptionalString const& message, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
: ArgumentException(!message.empty() ? message : SR::ArgumentNull_Generic, innerException, source) : ArgumentException(message.value_or(SR::ArgumentNull_Generic), innerException, source)
{ {
HRresult = HResults::HR_E_POINTER; HRresult = HResults::HR_E_POINTER;
} }
ArgumentNullException(std::string const& message, std::string const& paramName, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current()) ArgumentNullException(OptionalString const& paramName, OptionalString const& message, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
: ArgumentException(!message.empty() ? message : SR::ArgumentNull_Generic, paramName, innerException, source) : ArgumentException(paramName, message.value_or(SR::ArgumentNull_Generic), innerException, source)
{ {
HRresult = HResults::HR_E_POINTER; HRresult = HResults::HR_E_POINTER;
} }
static void ThrowIfNull(void* argument, OptionalString const& paramName, std::source_location const& source = std::source_location::current()) {
if (!argument)
{
throw ArgumentNullException(paramName, source);
}
}
}; };
//The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method. //The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.
class ArgumentOutOfRangeException : public ArgumentException { class ArgumentOutOfRangeException : public ArgumentException {
public: public:
ArgumentOutOfRangeException(std::source_location const& source = std::source_location::current()) : ArgumentException(SR::Arg_ArgumentOutOfRangeException, source) ArgumentOutOfRangeException(OptionalString const& message = std::nullopt, std::source_location const& source = std::source_location::current())
{ : ArgumentException(message.value_or(SR::Arg_ArgumentOutOfRangeException), source)
HRresult = HResults::HR_E_POINTER;
}
ArgumentOutOfRangeException(std::string const& message, std::source_location const& source = std::source_location::current())
: ArgumentException(!message.empty() ? message : SR::Arg_ArgumentOutOfRangeException, source)
{ {
HRresult = HResults::HR_E_POINTER; HRresult = HResults::HR_E_POINTER;
}; };
ArgumentOutOfRangeException(std::string const& message, std::string const& paramName, std::source_location const& source = std::source_location::current()) ArgumentOutOfRangeException(OptionalString const& message, OptionalString const& paramName, std::source_location const& source = std::source_location::current())
: ArgumentException(!message.empty() ? message : SR::Arg_ArgumentOutOfRangeException, paramName, source) : ArgumentException(message.value_or(SR::Arg_ArgumentOutOfRangeException), paramName, source)
{ {
HRresult = HResults::HR_E_POINTER; HRresult = HResults::HR_E_POINTER;
}; };
ArgumentOutOfRangeException(std::string const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current()) ArgumentOutOfRangeException(OptionalString const& message, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
: ArgumentException(!message.empty() ? message : SR::Arg_ArgumentOutOfRangeException, innerException, source) : ArgumentException(message.value_or(SR::Arg_ArgumentOutOfRangeException), innerException, source)
{ {
HRresult = HResults::HR_E_POINTER; HRresult = HResults::HR_E_POINTER;
} }
ArgumentOutOfRangeException(std::string const& message, std::string const& paramName, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current()) ArgumentOutOfRangeException(OptionalString const& message, OptionalString const& paramName, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
: ArgumentException(!message.empty() ? message : SR::Arg_ArgumentOutOfRangeException, paramName, innerException, source) : ArgumentException(message.value_or(SR::Arg_ArgumentOutOfRangeException), paramName, innerException, source)
{ {
HRresult = HResults::HR_E_POINTER; HRresult = HResults::HR_E_POINTER;
} }
@ -284,19 +281,14 @@ namespace csharp {
class SystemException : public Exception { class SystemException : public Exception {
public: public:
SystemException(std::source_location const& source = std::source_location::current()) : Exception(SR::Arg_SystemException, source) SystemException(OptionalString const& message = std::nullopt, std::source_location const& source = std::source_location::current())
: Exception(message.value_or(SR::Arg_SystemException), source)
{ {
HRresult = HResults::HR_COR_E_SYSTEM; HRresult = HResults::HR_COR_E_SYSTEM;
} }
SystemException(std::string const& message, std::source_location const& source = std::source_location::current()) SystemException(OptionalString const& message, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
: Exception(!message.empty() ? message : SR::Arg_SystemException, source) : Exception(message.value_or(SR::Arg_SystemException), innerException, source)
{
HRresult = HResults::HR_COR_E_SYSTEM;
}
SystemException(std::string const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current())
: Exception(!message.empty() ? message : SR::Arg_ArgumentException, innerException, source)
{ {
HRresult = HResults::HR_COR_E_SYSTEM; HRresult = HResults::HR_COR_E_SYSTEM;
} }
@ -305,20 +297,14 @@ namespace csharp {
//The exception that is thrown when a method call is invalid for the object's current state. //The exception that is thrown when a method call is invalid for the object's current state.
class InvalidOperationException : public SystemException { class InvalidOperationException : public SystemException {
public: public:
InvalidOperationException(std::source_location const& source = std::source_location::current()) InvalidOperationException(OptionalString const& message = std::nullopt, std::source_location const& source = std::source_location::current())
: SystemException(SR::Arg_SystemException, source) : SystemException(message.value_or(SR::Arg_InvalidOperationException), source)
{ {
HRresult = HResults::HR_COR_E_INVALIDOPERATION; HRresult = HResults::HR_COR_E_INVALIDOPERATION;
} }
InvalidOperationException(std::string const& message, std::source_location const& source = std::source_location::current()) InvalidOperationException(OptionalString const& message, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
: SystemException(!message.empty() ? message : SR::Arg_InvalidOperationException, source) : SystemException(message.value_or(SR::Arg_InvalidOperationException), innerException, source)
{
HRresult = HResults::HR_COR_E_INVALIDOPERATION;
}
InvalidOperationException(std::string const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current())
: SystemException(!message.empty() ? message : SR::Arg_InvalidOperationException, innerException, source)
{ {
HRresult = HResults::HR_COR_E_INVALIDOPERATION; HRresult = HResults::HR_COR_E_INVALIDOPERATION;
} }
@ -328,20 +314,14 @@ namespace csharp {
//typically because it should have been implemented on a subclass. //typically because it should have been implemented on a subclass.
class NotSupportedException : public SystemException { class NotSupportedException : public SystemException {
public: public:
NotSupportedException(std::source_location const& source = std::source_location::current()) NotSupportedException(OptionalString const& message = std::nullopt, std::source_location const& source = std::source_location::current())
: SystemException(SR::Arg_NotSupportedException, source) : SystemException(message.value_or(SR::Arg_NotSupportedException), source)
{ {
HRresult = HResults::HR_COR_E_INVALIDOPERATION; HRresult = HResults::HR_COR_E_INVALIDOPERATION;
} }
NotSupportedException(std::string const& message, std::source_location const& source = std::source_location::current()) NotSupportedException(OptionalString const& message, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
: SystemException(!message.empty() ? message : SR::Arg_NotSupportedException, source) : SystemException(message.value_or(SR::Arg_NotSupportedException), innerException, source)
{
HRresult = HResults::HR_COR_E_INVALIDOPERATION;
}
NotSupportedException(std::string const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current())
: SystemException(!message.empty() ? message : SR::Arg_NotSupportedException, innerException, source)
{ {
HRresult = HResults::HR_COR_E_INVALIDOPERATION; HRresult = HResults::HR_COR_E_INVALIDOPERATION;
} }
@ -350,20 +330,14 @@ namespace csharp {
//The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error. //The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error.
class UnauthorizedAccessException : public SystemException { class UnauthorizedAccessException : public SystemException {
public: public:
UnauthorizedAccessException(std::source_location const& source = std::source_location::current()) UnauthorizedAccessException(OptionalString const& message, std::source_location const& source = std::source_location::current())
: SystemException(SR::Arg_UnauthorizedAccessException, source) : SystemException(message.value_or(SR::Arg_UnauthorizedAccessException), source)
{ {
HRresult = HResults::HR_COR_E_UNAUTHORIZEDACCESS; HRresult = HResults::HR_COR_E_UNAUTHORIZEDACCESS;
} }
UnauthorizedAccessException(std::string const& message, std::source_location const& source = std::source_location::current()) UnauthorizedAccessException(OptionalString const& message, std::shared_ptr<Exception> const& innerException, std::source_location const& source = std::source_location::current())
: SystemException(!message.empty() ? message : SR::Arg_UnauthorizedAccessException, source) : SystemException(message.value_or(SR::Arg_UnauthorizedAccessException), innerException, source)
{
HRresult = HResults::HR_COR_E_UNAUTHORIZEDACCESS;
}
UnauthorizedAccessException(std::string const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current())
: SystemException(!message.empty() ? message : SR::Arg_UnauthorizedAccessException, innerException, source)
{ {
HRresult = HResults::HR_COR_E_UNAUTHORIZEDACCESS; HRresult = HResults::HR_COR_E_UNAUTHORIZEDACCESS;
} }

View File

@ -0,0 +1,29 @@
#ifndef CSHARP_IO_BINARY_CPP
#define CSHARP_IO_BINARY_CPP
#include "stream.hpp"
#include "exception.hpp"
#include <optional>
namespace csharp {
class BinaryReader {
public:
BinaryReader(std::shared_ptr<Stream> input, bool leaveOpen = false)
: _stream(input), _leaveOpen(leaveOpen)
{
if (input == nullptr)
throw csharp::ArgumentNullException(std::nullopt, "input");
}
virtual void Close() {
if (!_leaveOpen)
_stream->Close();
}
private:
std::shared_ptr<Stream> _stream;
bool _leaveOpen;
};
}
#endif

View File

@ -7,26 +7,20 @@
namespace csharp { namespace csharp {
class IOException : public SystemException { class IOException : public SystemException {
public: public:
IOException(std::source_location const& source = std::source_location::current()) IOException(OptionalString const& message = std::nullopt, std::source_location const& source = std::source_location::current())
: SystemException(SR::Arg_IOException, source) : SystemException(message.value_or(SR::Arg_IOException), source)
{ {
HRresult = HResults::HR_COR_E_IO; HRresult = HResults::HR_COR_E_IO;
} }
IOException(std::string const& message, std::source_location const& source = std::source_location::current()) IOException(OptionalString const& message, size_t hresult, std::source_location const& source = std::source_location::current())
: SystemException(!message.empty() ? message : SR::Arg_IOException, source) : SystemException(message.value_or(SR::Arg_IOException), source)
{
HRresult = HResults::HR_COR_E_IO;
}
IOException(std::string const& message, size_t hresult, std::source_location const& source = std::source_location::current())
: SystemException(!message.empty() ? message : SR::Arg_IOException, source)
{ {
HRresult = hresult; HRresult = hresult;
} }
IOException(std::string const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current()) IOException(OptionalString const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current())
: SystemException(!message.empty() ? message : SR::Arg_IOException, innerException, source) : SystemException(message.value_or(SR::Arg_IOException), innerException, source)
{ {
HRresult = HResults::HR_COR_E_IO; HRresult = HResults::HR_COR_E_IO;
} }
@ -34,20 +28,14 @@ namespace csharp {
class EndOfStreamException : public IOException { class EndOfStreamException : public IOException {
public: public:
EndOfStreamException(std::source_location const& source = std::source_location::current()) EndOfStreamException(OptionalString const& message = std::nullopt, std::source_location const& source = std::source_location::current())
: IOException(SR::Arg_IOException, source) : IOException(message.value_or(SR::Arg_IOException), source)
{
HRresult = HResults::HR_COR_E_ENDOFSTREAM;
}
EndOfStreamException(std::string const& message, std::source_location const& source = std::source_location::current())
: IOException(!message.empty() ? message : SR::Arg_IOException, source)
{ {
HRresult = HResults::HR_COR_E_ENDOFSTREAM; HRresult = HResults::HR_COR_E_ENDOFSTREAM;
} }
EndOfStreamException(std::string const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current()) EndOfStreamException(OptionalString const& message, std::shared_ptr<Exception>& innerException, std::source_location const& source = std::source_location::current())
: IOException(!message.empty() ? message : SR::Arg_IOException, innerException, source) : IOException(message.value_or(SR::Arg_IOException), innerException, source)
{ {
HRresult = HResults::HR_COR_E_ENDOFSTREAM; HRresult = HResults::HR_COR_E_ENDOFSTREAM;
} }