error.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #pragma once
  37. enum test_return_t {
  38. TEST_SUCCESS,
  39. TEST_FAILURE,
  40. TEST_SKIPPED
  41. };
  42. static inline bool test_success(test_return_t rc)
  43. {
  44. return (rc == TEST_SUCCESS);
  45. }
  46. static inline bool test_failed(test_return_t rc)
  47. {
  48. return (rc != TEST_SUCCESS);
  49. }
  50. namespace libtest {
  51. class Error
  52. {
  53. public:
  54. Error():
  55. _is_error(false),
  56. _file(NULL),
  57. _line(-1),
  58. _func(NULL)
  59. {
  60. }
  61. Error(const Error& error_):
  62. _is_error(false),
  63. _file(NULL),
  64. _line(0),
  65. _func(NULL)
  66. {
  67. if (error_.is_error())
  68. {
  69. _is_error= true;
  70. _file= error_.file();
  71. _line= error_.line();
  72. _func= error_.func();
  73. _message= error_.error();
  74. }
  75. }
  76. Error(const char* file_, int line_, const char* func_, const std::string& message_):
  77. _is_error(true),
  78. _file(file_),
  79. _line(line_),
  80. _func(func_),
  81. _message(message_)
  82. {
  83. }
  84. Error& operator=(const Error&) = delete;
  85. bool is_error() const
  86. {
  87. return _is_error;
  88. }
  89. const char* func() const
  90. {
  91. return _func;
  92. }
  93. const char* file() const
  94. {
  95. return _file;
  96. }
  97. int line() const
  98. {
  99. return _line;
  100. }
  101. const std::string& error() const
  102. {
  103. return _message;
  104. }
  105. private:
  106. bool _is_error;
  107. const char* _file;
  108. int _line;
  109. const char* _func;
  110. std::string _message;
  111. };
  112. } // namespace libtest