SpComm.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifndef _SP_COMM_H__
  2. #define _SP_COMM_H__
  3. #define _CRTDBG_MAP_ALLOC
  4. #include <stdlib.h>
  5. #ifdef RVC_OS_WIN
  6. #include <crtdbg.h>
  7. #endif //RVC_OS_WIN
  8. #include <cstring>
  9. #include <iostream>
  10. #include <sstream>
  11. #include "SpBase.h"
  12. namespace SP
  13. {
  14. //depecrated, to use defined at Module/include/CommEntityUtil.hpp
  15. struct MachineFormat
  16. {
  17. enum Site
  18. {
  19. CMB_UNKNOWN,
  20. CMB_LIB, /** 行内大堂*/
  21. CMB_FLB, /** 离行机器*/
  22. };
  23. #define SITE_ENUM_TYPE(MACRO) \
  24. MACRO(LIB)\
  25. MACRO(FLB)
  26. #define ENUM_MAP_CONVERT(elem) \
  27. if (stricmp(lpcszSiteName, "CMB."#elem) == 0) return CMB_##elem;
  28. /*!
  29. * convert cmb site name to enum type.
  30. */
  31. static Site Str2Site(LPCSTR lpcszSiteName)
  32. {
  33. if (lpcszSiteName == NULL || strlen(lpcszSiteName) == 0)
  34. return CMB_UNKNOWN;
  35. SITE_ENUM_TYPE(ENUM_MAP_CONVERT)
  36. return CMB_UNKNOWN;
  37. }
  38. #undef ENUM_MAP_CONVERT
  39. #define ENUM_MAP_CONVERT(elem) case CMB_##elem: return "CMB."#elem;
  40. static LPCSTR Site2Str(Site site) {
  41. switch (site) {
  42. SITE_ENUM_TYPE(ENUM_MAP_CONVERT)
  43. default:
  44. break;
  45. }
  46. return "Unkown";
  47. }
  48. enum What
  49. {
  50. RVC_UNKNOWN,
  51. RVC_Stand2S, /** 落地式大机*/
  52. RVC_Stand1SPlus /** 单屏大机*/
  53. };
  54. #define MACHINE_ENUM_TYPE(MACRO) \
  55. MACRO(Stand2S)\
  56. MACRO(Stand1SPlus)
  57. #undef ENUM_MAP_CONVERT
  58. #define ENUM_MAP_CONVERT(elem) \
  59. if (stricmp(lpcszTypeName, "RVC."#elem) == 0) return RVC_##elem;
  60. /*!
  61. * convert cmb site name to enum type.
  62. */
  63. static What Str2Type(LPCSTR lpcszTypeName)
  64. {
  65. if (lpcszTypeName == NULL || strlen(lpcszTypeName) == 0)
  66. return RVC_UNKNOWN;
  67. MACHINE_ENUM_TYPE(ENUM_MAP_CONVERT)
  68. return RVC_UNKNOWN;
  69. }
  70. #undef ENUM_MAP_CONVERT
  71. #define ENUM_MAP_CONVERT(elem) case RVC_##elem: return "RVC."#elem;
  72. static LPCSTR Type2Str(What what) {
  73. switch (what) {
  74. MACHINE_ENUM_TYPE(ENUM_MAP_CONVERT)
  75. default:
  76. break;
  77. }
  78. return "Unkown";
  79. }
  80. };
  81. /** Leak Detector*/
  82. namespace Perf
  83. {
  84. struct LeakDetector
  85. {
  86. LeakDetector()
  87. {
  88. #if defined(RVC_OS_WIN) && defined(WITH_DEBUG)
  89. int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
  90. flag |= _CRTDBG_LEAK_CHECK_DF;
  91. flag |= _CRTDBG_ALLOC_MEM_DF;
  92. _CrtSetDbgFlag(flag);
  93. #endif //defined(RVC_OS_WIN) && defined(WITH_DEBUG)
  94. }
  95. ~LeakDetector()
  96. {
  97. }
  98. };
  99. struct LineLeakDetector
  100. {
  101. LineLeakDetector() {
  102. #if defined(RVC_OS_WIN) && defined(WITH_DEBUG)
  103. _CrtMemCheckpoint(&s1);
  104. #endif //defined(RVC_OS_WIN) && defined(WITH_DEBUG)
  105. }
  106. ~LineLeakDetector()
  107. {
  108. #if defined(RVC_OS_WIN) && defined(WITH_DEBUG)
  109. _CrtMemCheckpoint(&s2);
  110. if (_CrtMemDifference(&s3, &s1, &s2))
  111. _CrtMemDumpStatistics(&s3);
  112. //_CrtDumpMemoryLeaks();
  113. #endif //defined(RVC_OS_WIN) && defined(WITH_DEBUG)
  114. }
  115. #if defined(RVC_OS_WIN) && defined(WITH_DEBUG)
  116. _CrtMemState s1, s2, s3;
  117. #endif //defined(RVC_OS_WIN) && defined(WITH_DEBUG)
  118. };
  119. }
  120. namespace Detail {
  121. struct SourceLineInfo {
  122. SourceLineInfo() :file(""), line(0){/*empty*/}
  123. SourceLineInfo(char const* file, std::size_t line) :file(file), line(line){/*empty*/}
  124. #if defined(_MSC_VER)
  125. #else
  126. SourceLineInfo(SourceLineInfo const& rhs) = default;
  127. SourceLineInfo(SourceLineInfo && rhs) = default;
  128. SourceLineInfo& operator = (SourceLineInfo const& rhs) = default;
  129. SourceLineInfo& operator =(SourceLineInfo && rhs) = default;
  130. #endif //_MSC_VER
  131. bool operator == (SourceLineInfo const& rhs) const {
  132. return line < rhs.line || (line == rhs.line && (std::strcmp(file, rhs.file) < 0));
  133. }
  134. bool operator < (SourceLineInfo const& rhs) const {
  135. return line == rhs.line && (file == rhs.file || std::strcmp(file, rhs.file) == 0);
  136. }
  137. const std::string ToString() const {
  138. std::ostringstream oss;
  139. oss << "file: {" << _GetFileName(file) << "} ,line: {" << line << "}";
  140. return oss.str();
  141. }
  142. char const* file;
  143. std::size_t line;
  144. };
  145. inline std::ostream& operator << (std::ostream& os, SourceLineInfo const& info)
  146. {
  147. os << info.file << '(' << info.line << ')';
  148. return os;
  149. }
  150. }
  151. }
  152. #define SP_INTERNAL_LINEINFO ::SP::Detail::SourceLineInfo( __FILE__, static_cast<std::size_t>( __LINE__ ) )
  153. #ifdef RVC_OS_WIN
  154. #define SP_SLEEP(ms) Sleep(ms)
  155. #else
  156. #include <unistd.h>
  157. #define SP_SLEEP(ms) usleep((ms) * 1000)
  158. #endif
  159. #ifndef ASSERT
  160. #include <assert.h>
  161. #define ASSERT assert
  162. #endif
  163. #endif // _SP_COMM_H__