st_utest.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* SPDX-License-Identifier: MIT */
  2. /* Copyright (c) 2013-2024 The SRS Authors */
  3. #ifndef ST_UTEST_PUBLIC_HPP
  4. #define ST_UTEST_PUBLIC_HPP
  5. // Before define the private/protected, we must include some system header files.
  6. // Or it may fail with:
  7. // redeclared with different access struct __xfer_bufptrs
  8. // @see https://stackoverflow.com/questions/47839718/sstream-redeclared-with-public-access-compiler-error
  9. #include <gtest/gtest.h>
  10. #include <st.h>
  11. #include <string>
  12. #define VOID
  13. // Close the fd automatically.
  14. #define StFdCleanup(fd, stfd) impl__StFdCleanup _ST_free_##fd(&fd, &stfd)
  15. #define StStfdCleanup(stfd) impl__StFdCleanup _ST_free_##stfd(NULL, &stfd)
  16. class impl__StFdCleanup {
  17. int* fd_;
  18. st_netfd_t* stfd_;
  19. public:
  20. impl__StFdCleanup(int* fd, st_netfd_t* stfd) : fd_(fd), stfd_(stfd) {
  21. }
  22. virtual ~impl__StFdCleanup() {
  23. if (stfd_ && *stfd_) {
  24. st_netfd_close(*stfd_);
  25. } else if (fd_ && *fd_ > 0) {
  26. ::close(*fd_);
  27. }
  28. }
  29. };
  30. // For coroutine function to return with error object.
  31. struct ErrorObject {
  32. int r0_;
  33. int errno_;
  34. std::string message_;
  35. ErrorObject(int r0, std::string message) : r0_(r0), errno_(errno), message_(message) {
  36. }
  37. };
  38. extern std::ostream& operator<<(std::ostream& out, const ErrorObject* err);
  39. #define ST_ASSERT_ERROR(error, r0, message) if (error) return new ErrorObject(r0, message)
  40. #define ST_COROUTINE_JOIN(trd, r0) ErrorObject* r0 = NULL; if (trd) st_thread_join(trd, (void**)&r0); SrsUniquePtr<ErrorObject> r0_uptr(r0)
  41. #define ST_EXPECT_SUCCESS(r0) EXPECT_TRUE(!r0) << r0
  42. #define ST_EXPECT_FAILED(r0) EXPECT_TRUE(r0) << r0
  43. #include <stdlib.h>
  44. // To free the instance in the current scope, for instance, MyClass* ptr,
  45. // which is a ptr and this class will:
  46. // 1. free the ptr.
  47. // 2. set ptr to NULL.
  48. //
  49. // Usage:
  50. // MyClass* po = new MyClass();
  51. // // ...... use po
  52. // SrsAutoFree(MyClass, po);
  53. //
  54. // Usage for array:
  55. // MyClass** pa = new MyClass*[size];
  56. // // ....... use pa
  57. // SrsAutoFreeA(MyClass*, pa);
  58. //
  59. // @remark the MyClass can be basic type, for instance, SrsAutoFreeA(char, pstr),
  60. // where the char* pstr = new char[size].
  61. // To delete object.
  62. #define SrsAutoFree(className, instance) \
  63. impl_SrsAutoFree<className> _auto_free_##instance(&instance, false, false, NULL)
  64. // To delete array.
  65. #define SrsAutoFreeA(className, instance) \
  66. impl_SrsAutoFree<className> _auto_free_array_##instance(&instance, true, false, NULL)
  67. // Use free instead of delete.
  68. #define SrsAutoFreeF(className, instance) \
  69. impl_SrsAutoFree<className> _auto_free_##instance(&instance, false, true, NULL)
  70. // Use hook instead of delete.
  71. #define SrsAutoFreeH(className, instance, hook) \
  72. impl_SrsAutoFree<className> _auto_free_##instance(&instance, false, false, hook)
  73. // The template implementation.
  74. template<class T>
  75. class impl_SrsAutoFree
  76. {
  77. private:
  78. T** ptr;
  79. bool is_array;
  80. bool _use_free;
  81. void (*_hook)(T*);
  82. public:
  83. // If use_free, use free(void*) to release the p.
  84. // If specified hook, use hook(p) to release it.
  85. // Use delete to release p, or delete[] if p is an array.
  86. impl_SrsAutoFree(T** p, bool array, bool use_free, void (*hook)(T*)) {
  87. ptr = p;
  88. is_array = array;
  89. _use_free = use_free;
  90. _hook = hook;
  91. }
  92. virtual ~impl_SrsAutoFree() {
  93. if (ptr == NULL || *ptr == NULL) {
  94. return;
  95. }
  96. if (_use_free) {
  97. free(*ptr);
  98. } else if (_hook) {
  99. _hook(*ptr);
  100. } else {
  101. if (is_array) {
  102. delete[] *ptr;
  103. } else {
  104. delete *ptr;
  105. }
  106. }
  107. *ptr = NULL;
  108. }
  109. };
  110. // The time unit in ms, for example 100 * SRS_UTIME_MILLISECONDS means 100ms.
  111. #define SRS_UTIME_MILLISECONDS 1000
  112. #endif