Преглед изворни кода

Merge branch 'abstract-exception-class' of public/ls-standard-library into development

patrick-christopher.mattulat пре 1 година
родитељ
комит
31fc90c7fa

+ 1 - 0
README.md

@@ -40,6 +40,7 @@ A __Date__ class comes with this submodule, which you can use to represent a dat
 
 - made test constructors public and reduced test setup overhead
 - added missing __nodiscard__ attributes to test package
+- made __Exception__ base class abstract, which prevents it from being instantiated
 
 #### Fixes ####
 

+ 4 - 2
include/ls-std/core/exception/Exception.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-22
-* Changed:         2023-02-22
+* Changed:         2023-03-28
 *
 * */
 
@@ -24,12 +24,14 @@ namespace ls::std::core
       ~Exception() noexcept override;
 
       [[nodiscard]] ::std::string getName() const;
-      [[nodiscard]] const char *what() const noexcept override;
+      [[nodiscard]] const char *what() const noexcept override = 0;
 
     protected:
 
       ::std::string message{};
       ::std::string name{};
+
+      [[nodiscard]] const char *_getIdentifiedMessage(const ::std::string &_defaultMessage) const;
   };
 }
 

+ 2 - 13
source/ls-std/core/exception/EventNotHandledException.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2023-02-04
- * Changed:         2023-02-23
+ * Changed:         2023-03-28
  *
  * */
 
@@ -27,16 +27,5 @@ EventNotHandledException::~EventNotHandledException() noexcept = default;
 
 const char *EventNotHandledException::what() const noexcept
 {
-  string concatenatedMessage = this->name + " thrown - ";
-
-  if (this->message.empty())
-  {
-    concatenatedMessage = concatenatedMessage + "event was not handled - nothing happened!";
-  }
-  else
-  {
-    concatenatedMessage = concatenatedMessage + this->message;
-  }
-
-  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+  return this->_getIdentifiedMessage("event was not handled - nothing happened!");
 }

+ 2 - 13
source/ls-std/core/exception/EventNotSubscribedException.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2023-02-04
- * Changed:         2023-02-23
+ * Changed:         2023-03-28
  *
  * */
 
@@ -27,16 +27,5 @@ EventNotSubscribedException::~EventNotSubscribedException() noexcept = default;
 
 const char *EventNotSubscribedException::what() const noexcept
 {
-  string concatenatedMessage = this->name + " thrown - ";
-
-  if (this->message.empty())
-  {
-    concatenatedMessage = concatenatedMessage + "event was not subscribed!";
-  }
-  else
-  {
-    concatenatedMessage = concatenatedMessage + this->message;
-  }
-
-  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+  return this->_getIdentifiedMessage("event was not subscribed!");
 }

+ 15 - 3
source/ls-std/core/exception/Exception.cpp

@@ -3,11 +3,12 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-22
-* Changed:         2023-02-23
+* Changed:         2023-03-28
 *
 * */
 
 #include <ls-std/core/exception/Exception.hpp>
+#include <ls-std/core/exception/ExceptionMessage.hpp>
 
 using ls::std::core::Exception;
 using std::move;
@@ -23,7 +24,18 @@ string Exception::getName() const
   return this->name;
 }
 
-const char *Exception::what() const noexcept
+const char *Exception::_getIdentifiedMessage(const string &_defaultMessage) const
 {
-  return "base exception class in use - method not implemented!";
+  string concatenatedMessage = this->name + " thrown - ";
+
+  if (this->message.empty())
+  {
+    concatenatedMessage = concatenatedMessage + _defaultMessage;
+  }
+  else
+  {
+    concatenatedMessage = concatenatedMessage + this->message;
+  }
+
+  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
 }

+ 2 - 13
source/ls-std/core/exception/FileNotFoundException.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2023-02-04
- * Changed:         2023-02-23
+ * Changed:         2023-03-28
  *
  * */
 
@@ -27,16 +27,5 @@ FileNotFoundException::~FileNotFoundException() noexcept = default;
 
 const char *FileNotFoundException::what() const noexcept
 {
-  string concatenatedMessage = this->name + " thrown - ";
-
-  if (this->message.empty())
-  {
-    concatenatedMessage = concatenatedMessage + "file not found!";
-  }
-  else
-  {
-    concatenatedMessage = concatenatedMessage + this->message;
-  }
-
-  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+  return this->_getIdentifiedMessage("file not found!");
 }

+ 2 - 13
source/ls-std/core/exception/FileOperationException.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2023-02-04
- * Changed:         2023-02-23
+ * Changed:         2023-03-28
  *
  * */
 
@@ -27,16 +27,5 @@ FileOperationException::~FileOperationException() noexcept = default;
 
 const char *FileOperationException::what() const noexcept
 {
-  string concatenatedMessage = this->name + " thrown - ";
-
-  if (this->message.empty())
-  {
-    concatenatedMessage = concatenatedMessage + "file operation failed!";
-  }
-  else
-  {
-    concatenatedMessage = concatenatedMessage + this->message;
-  }
-
-  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+  return this->_getIdentifiedMessage("file operation failed!");
 }

+ 2 - 13
source/ls-std/core/exception/IllegalArgumentException.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2023-02-04
- * Changed:         2023-02-23
+ * Changed:         2023-03-28
  *
  * */
 
@@ -27,16 +27,5 @@ IllegalArgumentException::~IllegalArgumentException() noexcept = default;
 
 const char *IllegalArgumentException::what() const noexcept
 {
-  string concatenatedMessage = this->name + " thrown - ";
-
-  if (this->message.empty())
-  {
-    concatenatedMessage = concatenatedMessage + "passed argument is not valid!";
-  }
-  else
-  {
-    concatenatedMessage = concatenatedMessage + this->message;
-  }
-
-  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+  return this->_getIdentifiedMessage("passed argument is not valid!");
 }

+ 2 - 13
source/ls-std/core/exception/IllegalArithmeticOperationException.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2023-02-04
- * Changed:         2023-02-23
+ * Changed:         2023-03-28
  *
  * */
 
@@ -27,16 +27,5 @@ IllegalArithmeticOperationException::~IllegalArithmeticOperationException() noex
 
 const char *IllegalArithmeticOperationException::what() const noexcept
 {
-  string concatenatedMessage = this->name + " thrown - ";
-
-  if (this->message.empty())
-  {
-    concatenatedMessage = concatenatedMessage + "arithmetic operation is not allowed!";
-  }
-  else
-  {
-    concatenatedMessage = concatenatedMessage + this->message;
-  }
-
-  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+  return this->_getIdentifiedMessage("arithmetic operation is not allowed!");
 }

+ 2 - 13
source/ls-std/core/exception/IncompleteJsonException.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2023-02-04
- * Changed:         2023-02-23
+ * Changed:         2023-03-28
  *
  * */
 
@@ -27,16 +27,5 @@ IncompleteJsonException::~IncompleteJsonException() noexcept = default;
 
 const char *IncompleteJsonException::what() const noexcept
 {
-  string concatenatedMessage = this->name + " thrown - ";
-
-  if (this->message.empty())
-  {
-    concatenatedMessage = concatenatedMessage + "this JSON string is incomplete.";
-  }
-  else
-  {
-    concatenatedMessage = concatenatedMessage + this->message;
-  }
-
-  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+  return this->_getIdentifiedMessage("this JSON string is incomplete.");
 }

+ 2 - 13
source/ls-std/core/exception/IndexOutOfBoundsException.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-10
-* Changed:         2023-02-23
+* Changed:         2023-03-28
 *
 * */
 
@@ -27,16 +27,5 @@ IndexOutOfBoundsException::~IndexOutOfBoundsException() noexcept = default;
 
 const char *IndexOutOfBoundsException::what() const noexcept
 {
-  string concatenatedMessage = this->name + " thrown - ";
-
-  if (this->message.empty())
-  {
-    concatenatedMessage = concatenatedMessage + "provided index is out of bounds!";
-  }
-  else
-  {
-    concatenatedMessage = concatenatedMessage + this->message;
-  }
-
-  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+  return this->_getIdentifiedMessage("provided index is out of bounds!");
 }

+ 2 - 13
source/ls-std/core/exception/NotImplementedException.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-03-27
-* Changed:         2023-03-27
+* Changed:         2023-03-28
 *
 * */
 
@@ -29,16 +29,5 @@ NotImplementedException::~NotImplementedException() noexcept = default;
 
 const char *NotImplementedException::what() const noexcept
 {
-  string concatenatedMessage = this->name + " thrown - ";
-
-  if (this->message.empty())
-  {
-    concatenatedMessage = concatenatedMessage + "method is not implemented and should not be used!";
-  }
-  else
-  {
-    concatenatedMessage = concatenatedMessage + this->message;
-  }
-
-  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+  return this->_getIdentifiedMessage("method is not implemented and should not be used!");
 }

+ 2 - 13
source/ls-std/core/exception/NullPointerException.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2023-02-04
- * Changed:         2023-02-23
+ * Changed:         2023-03-28
  *
  * */
 
@@ -27,16 +27,5 @@ NullPointerException::~NullPointerException() noexcept = default;
 
 const char *NullPointerException::what() const noexcept
 {
-  string concatenatedMessage = this->name + " thrown - ";
-
-  if (this->message.empty())
-  {
-    concatenatedMessage = concatenatedMessage + "reference is null!";
-  }
-  else
-  {
-    concatenatedMessage = concatenatedMessage + this->message;
-  }
-
-  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+  return this->_getIdentifiedMessage("reference is null!");
 }