funall.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. #include "data.h"
  3. #include <iomanip>
  4. #include <openssl/sha.h>
  5. template<class T>
  6. inline rettype::type getmapvalue(std::string strvalue, std::map<std::string, std::string>& tmapvalue, T& tValue)
  7. {
  8. if (strvalue.empty())
  9. {
  10. return rettype::except;
  11. }
  12. std::map<std::string, std::string>::iterator ifindvalue;
  13. ifindvalue = tmapvalue.find(strvalue);
  14. if (ifindvalue == tmapvalue.end())
  15. {
  16. return rettype::except;
  17. }
  18. try
  19. {
  20. using F = std::decay_t<decltype(tValue)>;
  21. if constexpr (std::is_same_v<F, std::int32_t>)
  22. {
  23. tValue = std::stoi(ifindvalue->second);
  24. }
  25. else if constexpr (std::is_same_v<F, std::int64_t>)
  26. {
  27. tValue = std::stoll(ifindvalue->second);
  28. }
  29. if constexpr (std::is_same_v<F, std::uint32_t>)
  30. {
  31. tValue = std::stoul(ifindvalue->second);
  32. }
  33. else if constexpr (std::is_same_v<F, std::uint64_t>)
  34. {
  35. tValue = std::stoull(ifindvalue->second);
  36. }
  37. else if constexpr (std::is_same_v<F, std::string>)
  38. {
  39. tValue = ifindvalue->second;
  40. }
  41. else if constexpr (std::is_same_v<F, std::double_t>)
  42. {
  43. tValue = std::stod(ifindvalue->second);
  44. }
  45. return rettype::exist;
  46. }
  47. catch (std::exception)
  48. {
  49. return rettype::except;
  50. }
  51. return rettype::absent;
  52. }
  53. template <typename T>
  54. inline rettype::type getvaluedata(std::map<std::string, std::string> tmapdata, T& tdata)
  55. {
  56. rettype::type rettype;
  57. boost::pfr::for_each_field(tdata, [&rettype, &tdata, &tmapdata](auto& member, size_t index) {
  58. rettype = getmapvalue(tdata.getfieldvalue(index), tmapdata, member);
  59. if (rettype != rettype::exist)
  60. {
  61. rettype = rettype::absent;
  62. }
  63. });
  64. tdata.getdata();
  65. return rettype;
  66. }
  67. template <class T>
  68. inline void Random_Shuffle(T a, T b)
  69. {
  70. std::random_device rd;
  71. std::mt19937 g(rd());
  72. std::shuffle(a, b, g);
  73. }
  74. inline std::double_t getdouble_t(__int64 itemp)
  75. {
  76. std::double_t dscore = itemp;
  77. dscore = itemp;
  78. dscore /= 10000;
  79. return dscore;
  80. }
  81. //±£ÁôÁ½Î»
  82. inline std::double_t getdouble_t_two(__int64 itemp)
  83. {
  84. itemp /= 100;
  85. itemp *= 100;
  86. std::double_t dscore = itemp;
  87. dscore = itemp;
  88. dscore /= 10000;
  89. return dscore;
  90. }
  91. inline std::string sha256_to_base16(const std::string& input) {
  92. unsigned char hash[SHA256_DIGEST_LENGTH];
  93. SHA256_CTX sha256_ctx;
  94. SHA256_Init(&sha256_ctx);
  95. SHA256_Update(&sha256_ctx, input.c_str(), input.length());
  96. SHA256_Final(hash, &sha256_ctx);
  97. std::stringstream oss;
  98. for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
  99. oss << std::setw(2) << std::setfill('0') << std::hex << (int)hash[i];
  100. }
  101. return oss.str();
  102. }