assert.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libfbasics - library of some basic tools
  4. * See file License for license information.
  5. */
  6. #ifndef _ASSERT_H_
  7. #define _ASSERT_H_
  8. #include <iostream>
  9. #include <string>
  10. #include <sstream>
  11. #include <core/basics/types.h>
  12. #include <core/basics/Exception.h>
  13. namespace NICE {
  14. //#define assert(t) if (!t) {cout << "Assertion failed." << endl;}
  15. /**
  16. * Deprecated! A simple assertion function.
  17. * @deprecated Use fassert() or fassert2() instead
  18. */
  19. inline void assert2(const bool t, const std::string& message = std::string()) {
  20. if (!t) {
  21. std::cerr << "Assertion failed. " << message << std::endl;
  22. //std::stringstream s;
  23. //s << "Assertion failed: " << message;
  24. //fthrow(Exception, s.str());
  25. }
  26. }
  27. /**
  28. * A simple assertion function. (Behavior might be changed in the future.)
  29. */
  30. inline void __assert(const bool exp,
  31. std::string message = std::string(),
  32. std::string code = std::string(),
  33. const char* file = "",
  34. uint line = 0) {
  35. if (!exp) {
  36. //std::cerr << "Assertion failed. " << message << std::endl;
  37. std::stringstream s;
  38. s << "Assertion failed in file " << file << " at line " << line
  39. << ". Code: '" << code << "'";
  40. if (message.size() > 0) {
  41. s << " Message: '" << message << "'";
  42. }
  43. s << ".";
  44. // std::cerr << s.str() << std::endl;
  45. // fthrow(Exception, s.str());
  46. std::string* buffer = new std::string(s.str());
  47. fthrow(Exception, *buffer);
  48. }
  49. }
  50. #define fassert(exp) NICE::__assert((exp), "", #exp,__FILE__,__LINE__)
  51. #define fassert2(exp, msg) \
  52. { \
  53. std::stringstream s; \
  54. s << msg; \
  55. NICE::__assert((exp), s.str(), #exp,__FILE__,__LINE__); \
  56. }
  57. } // namespace
  58. #endif // _ASSERT_H_