Common.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "Common.h"
  2. /// <summary>
  3. /// 析构函数
  4. /// </summary>
  5. ErrorMsg::~ErrorMsg()
  6. {
  7. if (_errorMsg)
  8. {
  9. delete _errorMsg;
  10. _errorMsg = nullptr;
  11. }
  12. }
  13. /// <summary>
  14. /// 设置错误信息
  15. /// </summary>
  16. /// <param name="code"></param>
  17. /// <param name="errorMsgs"></param>
  18. void ErrorMsg::SetErrorMsg(ErrorCode code, std::vector<const char*> errorMsgs)
  19. {
  20. if (_errorMsg)
  21. {
  22. delete _errorMsg;
  23. _errorMsg = NULL;
  24. }
  25. int count = errorMsgs.size();
  26. std::stringstream ss;
  27. for (int ni = 0; ni < count; ni++)
  28. {
  29. ss << errorMsgs[ni];
  30. }
  31. std::string msgCombined = ss.str();
  32. int len = msgCombined.length();
  33. _errorMsg = new char[msgCombined.length() + 1];
  34. msgCombined.copy(_errorMsg, len, 0);
  35. *(_errorMsg + len) = '\0';
  36. _errorCode = code;
  37. }
  38. /// <summary>
  39. /// 获取错误信息
  40. /// </summary>
  41. /// <param name="errorCode"></param>
  42. /// <param name="errorMsg"></param>
  43. /// <param name="errorMaxLen"></param>
  44. void ErrorMsg::GetErrorMsg(ErrorCode& errorCode, char* errorMsg, const int errorMaxLen)
  45. {
  46. errorCode = _errorCode;
  47. if (!_errorMsg)
  48. {
  49. return;
  50. }
  51. int errorLen = strlen(_errorMsg);
  52. if (errorMaxLen > errorLen)
  53. {
  54. wcscpy_s(errorMsg, errorLen + 1, _errorMsg);
  55. }
  56. else
  57. {
  58. wcscpy_s(errorMsg, errorMaxLen, _errorMsg, errorMaxLen - 1);
  59. }
  60. }
  61. /// <summary>
  62. /// 初始化静态成员变量_errorMsg
  63. /// </summary>
  64. char* ErrorMsg::_errorMsg = nullptr;
  65. /// <summary>
  66. /// 初始化静态成员变量 _errorCode
  67. /// </summary>
  68. ErrorCode ErrorMsg::_errorCode = None;