Advertisement
RobertDeMilo

Используем макросы и улучшаем фреймворк

Oct 27th, 2023
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. template <typename T>
  9. void AssertImpl(const T& t, const string& expr, const string& file_name, const string& func_name,int line, const string& hint = "")
  10. {
  11.     if (t == false)
  12.     {
  13.         if (!hint.empty())
  14.         {
  15.             cout << file_name << "(" << line << ")" << ": " << func_name << ": " << "ASSERT(" << expr << ")" << " failed. Hint: " << hint << endl;
  16.             abort();
  17.         }
  18.         else
  19.         {
  20.             cout << file_name << "(" << line << ")" << ": " << func_name << ": " << "ASSERT(" << expr << ")" << " failed." << endl;
  21.             abort();
  22.         }
  23.     }
  24. }
  25.  
  26. #define ASSERT(expr) AssertImpl( (expr), (#expr), __FILE__, __FUNCTION__, __LINE__ )
  27.  
  28. #define ASSERT_HINT(expr, hint) AssertImpl( (expr), (#expr), __FILE__, __FUNCTION__, __LINE__, (hint) )
  29.  
  30. int main()
  31. {
  32.  
  33.     string hello = "hello"s;
  34.     ASSERT(!hello.empty());
  35.  
  36.     ASSERT_HINT(2 + 2 == 5, "This will fail"s);
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement