Guest User

Untitled

a guest
Apr 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. struct badType;
  2.  
  3. template <typename TType = badType>
  4. struct type_decl {
  5. static constexpr const char * format_string() {
  6. return (std::is_integral_v<TType>
  7. ? "%d"
  8. : (std::is_floating_point_v<TType>
  9. ? "%f"
  10. : (std::is_pointer_v<TType>
  11. ? "%X"
  12. : "")
  13. )
  14. );
  15. }
  16. };
  17.  
  18. template <>
  19. struct type_decl<const char *> {
  20. static constexpr const char * format_string() {
  21. return "%s";
  22. }
  23. };
  24.  
  25. template <typename TType>
  26. constexpr const char * get_fmt_string(TType value) {
  27. return type_decl<TType>::format_string();
  28. };
  29.  
  30. template <typename ...TArgs>
  31. void brain_fuck(TArgs ...args) {
  32. using unpacker_t = int[];
  33.  
  34. string_buf<4096> buffer;
  35.  
  36. int num_args = 0;
  37.  
  38. (void)unpacker_t
  39. {
  40. (
  41. args,
  42. buffer.append(" arg[%d] : ", num_args++),
  43. buffer.append("%s : ", typeid(args).name()),
  44. buffer.append("%X : '", sizeof(args)),
  45. buffer.append(get_fmt_string(args), args),
  46. buffer.append("'\n"),
  47. 0)...,
  48. };
  49.  
  50. debugf("**** %d args ****\n", num_args);
  51. debugf(buffer);
  52. }
  53.  
  54. brain_fuck("oh", "my", "god", 2.0f, 1, true);
  55.  
  56. /*
  57. Output:
  58.  
  59. **** 6 args ****
  60. arg[0] : char const * : 4 : 'oh'
  61. arg[1] : char const * : 4 : 'my'
  62. arg[2] : char const * : 4 : 'god'
  63. arg[3] : float : 4 : '2.000000'
  64. arg[4] : int : 4 : '1'
  65. arg[5] : bool : 1 : '1'
  66. */
Add Comment
Please, Sign In to add comment