print_variable.hxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #ifndef PRINT_VARIABLE_HXX
  2. #define PRINT_VARIABLE_HXX
  3. #include "libbinom/include/variables/key_value.hxx"
  4. #include "libbinom/include/binom.hxx"
  5. #include "tester.hxx"
  6. namespace binom::utils {
  7. class {
  8. using Variable = binom::Variable;
  9. void printKey(const KeyValue value) {
  10. switch (value.getType()) {
  11. case binom::VarKeyType::null: std::cout << "[null] null"; break;
  12. case binom::VarKeyType::boolean: std::cout << "[bool] " << std::boolalpha << bool(value.toNumber()) << std::noboolalpha; break;
  13. case binom::VarKeyType::ui8: std::cout << "[ui8] " << ui16(value.toNumber()); break;
  14. case binom::VarKeyType::si8: std::cout << "[si8] " << i16(value.toNumber()); break;
  15. case binom::VarKeyType::ui16: std::cout << "[ui16] " << ui16(value.toNumber()); break;
  16. case binom::VarKeyType::si16: std::cout << "[si16] " << i16(value.toNumber()); break;
  17. case binom::VarKeyType::ui32: std::cout << "[ui32] " << ui32(value.toNumber()); break;
  18. case binom::VarKeyType::si32: std::cout << "[si32] " << i32(value.toNumber()); break;
  19. case binom::VarKeyType::f32: std::cout << "[f32] " << f32(value.toNumber()); break;
  20. case binom::VarKeyType::ui64: std::cout << "[ui64] " << ui64(value.toNumber()); break;
  21. case binom::VarKeyType::si64: std::cout << "[si64] " << i64(value.toNumber()); break;
  22. case binom::VarKeyType::f64: std::cout << "[f64] " << f64(value.toNumber()); break;
  23. case binom::VarKeyType::bit_array:
  24. std::cout << "[bit_array] " << std::boolalpha;
  25. for(auto value : value.toBitArray()) std::cout << bool(value) << ' ';
  26. std::cout << std::noboolalpha;
  27. break;
  28. case binom::VarKeyType::ui8_array:
  29. std::cout << "[ui8_array] ";
  30. if(value.toBufferArray().isPrintable()) std::cout << "[string] " << std::string(value.toBufferArray());
  31. else for(auto value : value.toBufferArray()) std::cout << ui16(value) << ' ';
  32. break;
  33. case binom::VarKeyType::si8_array:
  34. std::cout << "[si8_array] ";
  35. if(value.toBufferArray().isPrintable()) std::cout << "[string] " << std::string(value.toBufferArray());
  36. else for(auto value : value.toBufferArray()) std::cout << i16(value) << ' ';
  37. break;
  38. case binom::VarKeyType::ui16_array:
  39. std::cout << "[ui16_array] ";
  40. if(value.toBufferArray().isPrintable()) std::wcout << "[wstring] " << std::wstring(value.toBufferArray());
  41. else for(auto value : value.toBufferArray()) std::cout << ui16(value) << ' ';
  42. break;
  43. case binom::VarKeyType::si16_array:
  44. std::cout << "[si16_array] ";
  45. if(value.toBufferArray().isPrintable()) std::wcout << "[wstring] " << std::wstring(value.toBufferArray());
  46. else for(auto value : value.toBufferArray()) std::cout << i16(value) << ' ';
  47. break;
  48. case binom::VarKeyType::ui32_array:
  49. std::cout << "[ui32_array] ";
  50. if(value.toBufferArray().isPrintable()) std::wcout << "[wstring] " << std::wstring(value.toBufferArray());
  51. else for(auto value : value.toBufferArray()) std::cout << ui32(value) << ' ';
  52. break;
  53. case binom::VarKeyType::si32_array:
  54. std::cout << "[si32_array] ";
  55. if(value.toBufferArray().isPrintable()) std::wcout << "[wstring] " << std::wstring(value.toBufferArray());
  56. else for(auto value : value.toBufferArray()) std::cout << i32(value) << ' ';
  57. break;
  58. case binom::VarKeyType::f32_array:
  59. std::cout << "[f32_array] ";
  60. if(value.toBufferArray().isPrintable()) std::wcout << "[wstring] " << std::wstring(value.toBufferArray());
  61. else for(auto value : value.toBufferArray()) std::cout << f32(value) << ' ';
  62. break;
  63. case binom::VarKeyType::ui64_array:
  64. std::cout << "[ui64_array] ";
  65. for(auto value : value.toBufferArray()) std::cout << ui64(value) << ' ';
  66. break;
  67. case binom::VarKeyType::si64_array:
  68. std::cout << "[si64_array] ";
  69. for(auto value : value.toBufferArray()) std::cout << i64(value) << ' ';
  70. break;
  71. case binom::VarKeyType::f64_array:
  72. std::cout << "[f64_array] ";
  73. for(auto value : value.toBufferArray()) std::cout << f64(value) << ' ';
  74. break;
  75. default: std::cout << "<unexpected type>"; break;
  76. }
  77. }
  78. void print(const Variable& variable, size_t shift = 0, const char* msg = "") {
  79. switch (variable.getType()) {
  80. case binom::VarType::null: std::cout << std::string(shift, '|') << msg << "[null]\n\r"; break;
  81. case binom::VarType::boolean: std::cout << std::string(shift, '|') << msg << "[bool] " << std::boolalpha << bool(variable.toNumber()) << std::noboolalpha << std::endl; break;
  82. case binom::VarType::ui8: std::cout << std::string(shift, '|') << msg << "[ui8] " << ui16(variable.toNumber()) << std::endl; break;
  83. case binom::VarType::si8: std::cout << std::string(shift, '|') << msg << "[si8] " << i16(variable.toNumber()) << std::endl; break;
  84. case binom::VarType::ui16: std::cout << std::string(shift, '|') << msg << "[ui16] " << ui16(variable.toNumber()) << std::endl; break;
  85. case binom::VarType::si16: std::cout << std::string(shift, '|') << msg << "[si16] " << i16(variable.toNumber()) << std::endl; break;
  86. case binom::VarType::ui32: std::cout << std::string(shift, '|') << msg << "[ui32] " << ui32(variable.toNumber()) << std::endl; break;
  87. case binom::VarType::si32: std::cout << std::string(shift, '|') << msg << "[si32] " << i32(variable.toNumber()) << std::endl; break;
  88. case binom::VarType::f32: std::cout << std::string(shift, '|') << msg << "[f32] " << f32(variable.toNumber()) << std::endl; break;
  89. case binom::VarType::ui64: std::cout << std::string(shift, '|') << msg << "[ui64] " << ui64(variable.toNumber()) << std::endl; break;
  90. case binom::VarType::si64: std::cout << std::string(shift, '|') << msg << "[si64] " << i64(variable.toNumber()) << std::endl; break;
  91. case binom::VarType::f64: std::cout << std::string(shift, '|') << msg << "[f64] " << f64(variable.toNumber()) << std::endl; break;
  92. case binom::VarType::bit_array:
  93. std::cout << std::string(shift, '|') << msg << "[bit_array] " << std::boolalpha;
  94. for(auto value : variable.toBitArray()) std::cout << bool(value) << ' ';
  95. std::cout << std::noboolalpha << std::endl;
  96. break;
  97. case binom::VarType::ui8_array:
  98. std::cout << std::string(shift, '|') << msg << "[ui8_array] ";
  99. if(variable.toBufferArray().isPrintable()) std::cout << "[string] " << std::string(variable.toBufferArray());
  100. else for(auto value : variable.toBufferArray()) std::cout << ui16(value) << ' ';
  101. std::cout << std::endl;
  102. break;
  103. case binom::VarType::si8_array:
  104. std::cout << std::string(shift, '|') << msg << "[si8_array] ";
  105. if(variable.toBufferArray().isPrintable()) std::cout << "[string] " << std::string(variable.toBufferArray());
  106. else for(auto value : variable.toBufferArray()) std::cout << i16(value) << ' ';
  107. std::cout << std::endl;
  108. break;
  109. case binom::VarType::ui16_array:
  110. std::cout << std::string(shift, '|') << msg << "[ui16_array] ";
  111. if(variable.toBufferArray().isPrintable()) std::wcout << "[wstring] " << std::wstring(variable.toBufferArray());
  112. else for(auto value : variable.toBufferArray()) std::cout << ui16(value) << ' ';
  113. std::cout << std::endl;
  114. break;
  115. case binom::VarType::si16_array:
  116. std::cout << std::string(shift, '|') << msg << "[si16_array] ";
  117. if(variable.toBufferArray().isPrintable()) std::wcout << "[wstring] " << std::wstring(variable.toBufferArray());
  118. else for(auto value : variable.toBufferArray()) std::cout << i16(value) << ' ';
  119. std::cout << std::endl;
  120. break;
  121. case binom::VarType::ui32_array:
  122. std::cout << std::string(shift, '|') << msg << "[ui32_array] ";
  123. if(variable.toBufferArray().isPrintable()) std::wcout << "[wstring] " << std::wstring(variable.toBufferArray());
  124. else for(auto value : variable.toBufferArray()) std::cout << ui32(value) << ' ';
  125. std::cout << std::endl;
  126. break;
  127. case binom::VarType::si32_array:
  128. std::cout << std::string(shift, '|') << msg << "[si32_array] ";
  129. if(variable.toBufferArray().isPrintable()) std::wcout << "[wstring] " << std::wstring(variable.toBufferArray());
  130. else for(auto value : variable.toBufferArray()) std::cout << i32(value) << ' ';
  131. std::cout << std::endl;
  132. break;
  133. case binom::VarType::f32_array:
  134. std::cout << std::string(shift, '|') << msg << "[f32_array] ";
  135. if(variable.toBufferArray().isPrintable()) std::wcout << "[wstring] " << std::wstring(variable.toBufferArray());
  136. else for(auto value : variable.toBufferArray()) std::cout << f32(value) << ' ';
  137. std::cout << std::endl;
  138. break;
  139. case binom::VarType::ui64_array:
  140. std::cout << std::string(shift, '|') << msg << "[ui64_array] ";
  141. for(auto value : variable.toBufferArray()) std::cout << ui64(value) << ' ';
  142. std::cout << std::endl;
  143. break;
  144. case binom::VarType::si64_array:
  145. std::cout << std::string(shift, '|') << msg << "[si64_array] ";
  146. for(auto value : variable.toBufferArray()) std::cout << i64(value) << ' ';
  147. std::cout << std::endl;
  148. break;
  149. case binom::VarType::f64_array:
  150. std::cout << std::string(shift, '|') << msg << "[f64_array] ";
  151. for(auto value : variable.toBufferArray()) std::cout << f64(value) << ' ';
  152. std::cout << std::endl;
  153. break;
  154. case binom::VarType::array:
  155. std::cout << std::string(shift, '|') << msg << "[array]\n\r";
  156. if(shift >= 100) {
  157. std::cout << std::string(shift, '|') << "Error: The maximum stack size for the printVariable function has been reached!\n\r";
  158. return;
  159. }
  160. for(const auto& var : variable.toArray()) print(var, shift + 1);
  161. break;
  162. case binom::VarType::list:
  163. std::cout << std::string(shift, '|') << msg << "[list]\n\r";
  164. if(shift >= 100) {
  165. std::cout << std::string(shift, '|') << "Error: The maximum stack size for the printVariable function has been reached!\n\r";
  166. return;
  167. }
  168. for(const auto& var : variable.toList())
  169. print(var, shift + 1);
  170. break;
  171. case binom::VarType::map:
  172. std::cout << std::string(shift, '|') << msg << "[map]\n\r";
  173. if(shift >= 100) {
  174. std::cout << std::string(shift, '|') << "Error: The maximum stack size for the printVariable function has been reached!\n\r";
  175. return;
  176. }
  177. for(const auto& named_var : variable.toMap()) {
  178. std::cout << std::string(shift, '|') << "KEY - "; printKey(named_var.getKey()); std::cout << ":" << std::endl;
  179. print(named_var.getValue(), shift + 1, "VALUE - ");
  180. }
  181. break;
  182. case binom::VarType::multimap:
  183. std::cout << std::string(shift, '|') << msg << "[multimap]\n\r";
  184. if(shift >= 100) {
  185. std::cout << std::string(shift, '|') << "Error: The maximum stack size for the printVariable function has been reached!\n\r";
  186. return;
  187. }
  188. for(const auto& named_var : variable.toMultiMap()) {
  189. std::cout << std::string(shift, '|') << "KEY - "; printKey(named_var.getKey()); std::cout << ":" << std::endl;
  190. print(named_var.getValue(), shift + 1, "VALUE - ");
  191. }
  192. break;
  193. default:
  194. case binom::VarType::invalid_type: std::cout << std::string(shift, '|') << "<unexpected type>\n\r"; break;
  195. }
  196. }
  197. public:
  198. void operator()(const Variable& var) {
  199. std::cout << std::string(log_depth, '|') INFO << "\x1B[94m[i] printVariable(const Variable&) \x1B[34m:\n\r";
  200. GRP_PUSH
  201. print(var, log_depth);
  202. GRP_POP
  203. std::cout << "\033[0m"; std::cout.flush();
  204. }
  205. void operator()(Variable&& var) {
  206. std::cout << std::string(log_depth, '|') INFO << "\x1B[94m[i] printVariable(Variable&&) \x1B[34m:\n\r";
  207. GRP_PUSH
  208. print(var, log_depth);
  209. GRP_POP
  210. std::cout << "\033[0m"; std::cout.flush();
  211. }
  212. void operator()(const KeyValue& var) {
  213. std::cout << std::string(log_depth, '|') INFO << "\x1B[94m[i] printVariable(const KeyValue&) \x1B[34m:\n\r";
  214. GRP_PUSH
  215. print(var, log_depth);
  216. GRP_POP
  217. std::cout << "\033[0m"; std::cout.flush();
  218. }
  219. void operator()(KeyValue&& var) {
  220. std::cout << std::string(log_depth, '|') INFO << "\x1B[94m[i] printVariable(KeyValue&&) \x1B[34m:\n\r";
  221. GRP_PUSH
  222. print(var, log_depth);
  223. GRP_POP
  224. std::cout << "\033[0m"; std::cout.flush();
  225. }
  226. } printVariable;
  227. }
  228. #endif // PRINT_VARIABLE_HXX