//////////////////////////////////////////////////////////////////////////////// // // KCPP Assert Header File // #ifndef KCPP_ASSERT_HPP #define KCPP_ASSERT_HPP #include <string> #include <kcpp_error.hpp> namespace kcpp { /** * アサーションに失敗した際に、throw される Error です。 */ class AssertError : public Error { public: /** * 最後に発生したエラーメッセージを持つ AssertError を構築します。 * エラーメッセージを取得できない場合、空文字列がメッセージに設定されます。 */ AssertError() noexcept; /** * AssertError をコピーして生成します。 * * @param t コピー元 */ AssertError(const AssertError& t) noexcept; /** * 指定されたメッセージを持つ AssertError を構築します。 * * @param msg メッセージ */ AssertError(const std::string& msg) noexcept; /** * 指定されたメッセージおよび、エラー発生情報を持つ AssertError を構築します。 * * @param msg メッセージ * @param file エラー発生ファイル名 * @param func エラー発生関数名 * @param line エラー発生行番号 */ AssertError(const std::string& msg, const char* file, const char* func, int line) noexcept; /** * AssertErro を破棄します。 */ virtual ~AssertError() noexcept; /** * エラー発生ファイル名を返します。 * * @return エラー発生ファイル名 */ const std::string& getFile() const noexcept; /** * エラー発生関数名を返します。 * * @return エラー発生関数名 */ const std::string& getFunc() const noexcept; /** * エラー発生行番号を返します。 * * @return エラー発生行番号 */ int getLine() const noexcept; private: std::string errorFile; //< エラー発生ファイル名 std::string errorFunc; //< エラー発生関数名 int errorLine; //< エラー発生行番号 }; namespace Assert { /** * 指定された値が期待する値かを判定し、期待しない値の場合、AssertError を throw します。 * * @param expected 期待する値 * @param actual 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertEquals (bool expected, bool actual, const char* file, const char* func, int line); /** * 指定された値が期待する値かを判定し、期待しない値の場合、AssertError を throw します。 * * @param expected 期待する値 * @param actual 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertEquals (char expected, char actual, const char* file, const char* func, int line); /** * 指定された値が期待する値かを判定し、期待しない値の場合、AssertError を throw します。 * * @param expected 期待する値 * @param actual 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertEquals (int expected, int actual, const char* file, const char* func, int line); /** * 指定された値が期待する値かを判定し、期待しない値の場合、AssertError を throw します。 * * @param expected 期待する値 * @param actual 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertEquals (long expected, long actual, const char* file, const char* func, int line); /** * 指定された値が期待する値かを判定し、期待しない値の場合、AssertError を throw します。 * * @param expected 期待する値 * @param actual 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertEquals (double expected, double actual, const char* file, const char* func, int line); /** * 指定された値が期待する値かを判定し、期待しない値の場合、AssertError を throw します。 * * @param expected 期待する値 * @param actual 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertEquals (const std::string& expected, const std::string& actual, const char* file, const char* func, int line); /** * 指定された値が期待する値かを判定し、期待しない値の場合、AssertError を throw します。 * * @param expected 期待する値 * @param actual 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertEquals (const char* expected, const std::string& actual, const char* file, const char* func, int line); /** * 指定された値が期待する値かを判定し、期待しない値の場合、AssertError を throw します。 * * @param expected 期待する値 * @param actual 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertEquals (const std::string& expected, const char* actual, const char* file, const char* func, int line); /** * 指定された値が期待する値かを判定し、期待しない値の場合、AssertError を throw します。 * * @param expected 期待する値 * @param actual 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertEquals (const char* expected, const char* actual, const char* file, const char* func, int line); /** * 指定された値が true か否かを判定し、true でない場合、AssertError を throw します。 * * @param condition 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertTrue (bool condition , const char* file, const char* func, int line); /** * 指定された値が false か否かを判定し、false でない場合、AssertError を throw します。 * * @param condition 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertFalse (bool condition , const char* file, const char* func, int line); /** * 指定された値が nullptr か否かを判定し、nullptr でない場合、AssertError を throw します。 * * @param condition 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertNull (void* obj , const char* file, const char* func, int line); /** * 指定された値が nullptr か否かを判定し、nullptr の場合、AssertError を throw します。 * * @param condition 比較する値 * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertNotNull(void* obj , const char* file, const char* func, int line); /** * 常に AssertError を throw します。 * * @param file ファイル名 * @param func 関数名 * @param line 行番号 */ void assertFail( const char* file, const char* func, int line); /** * 指定された actual が、expected と同一でない場合、AssertError を throw します。 * * @param expected 期待する値 * @param actual 比較する値 */ #define assertEquals(expected, actual) assertEquals(expected, actual, __FILE__, __func__, __LINE__) /** * 指定された condition が、true でない場合、AssertError を throw します。 * * @param condition 比較する値 */ #define assertTrue(condition) assertTrue(condition, __FILE__, __func__, __LINE__) /** * 指定された condition が、false でない場合、AssertError を throw します。 * * @param condition 比較する値 */ #define assertFalse(condition) assertFalse(condition, __FILE__, __func__, __LINE__) /** * 指定された obj が、nullptr でない場合、AssertError を throw します。 * * @param obj 比較する値 */ #define assertNull(obj) assertNull(obj, __FILE__, __func__, __LINE__) /** * 指定された obj が、nullptr の場合、AssertError を throw します。 * * @param obj 比較する値 */ #define assertNotNull(obj) assertNotNull(obj, __FILE__, __func__, __LINE__) /** * 常に、AssertError を throw します。 */ #define assertFail() assertFail(__FILE__, __func__, __LINE__) } } #endif // KCPP_ASSERT_HPP