Utility.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * Triangles
  3. * Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #pragma once
  18. #include <iostream>
  19. /**
  20. * @class Rect
  21. * @author POSITIVE MENTAL ATTITUDE
  22. * @date 06/09/16
  23. * @file Utility.hpp
  24. * @brief Helper class for box collision.
  25. */
  26. struct Rect
  27. {
  28. float left, right, top, bottom;
  29. };
  30. /**
  31. * @class Echo
  32. * @author POSITIVE MENTAL ATTITUDE
  33. * @date 06/09/16
  34. * @file Utility.hpp
  35. * @brief Helper class for stdout/stderr output.
  36. */
  37. class Echo
  38. {
  39. public:
  40. enum
  41. {
  42. Empty, Info, Load, Debug, Error
  43. };
  44. Echo() = default;
  45. template<typename Type, typename... Args>
  46. static void out(int prefix, Type out, Args... args);
  47. template<typename Type, typename... Args>
  48. static void debug(Type out, Args... args);
  49. template<typename Type, typename... Args>
  50. static void info(Type out, Args... args);
  51. template<typename Type, typename... Args>
  52. static void load(Type out, Args... args);
  53. template<typename Type, typename... Args>
  54. static void error(Type out, Args... args);
  55. static void out(std::string debug);
  56. static void out(float debug);
  57. static void setLogLevel(int loglevel);
  58. private:
  59. static void helper(bool err, std::wstring out);
  60. template <typename T>
  61. static void helper(bool err, T out);
  62. template<typename Type, typename... Args>
  63. static void helper(bool err, const Type out, Args... args);
  64. static bool printType(int order);
  65. static int _loglevel;
  66. };
  67. template <typename Type>
  68. void Echo::helper(bool err, Type out)
  69. {
  70. if(err)
  71. std::cerr << out;
  72. else
  73. std::cout << out;
  74. }
  75. template<typename Type, typename... Args>
  76. void Echo::helper(bool err, const Type out, Args... args)
  77. {
  78. if(err)
  79. std::cerr << out;
  80. else
  81. std::cout << out;
  82. helper(err, args...);
  83. }
  84. template<typename Type, typename... Args>
  85. void Echo::out(int order, Type out, Args... args)
  86. {
  87. if(!printType(order))
  88. return;
  89. if(order == Error)
  90. {
  91. helper(true, out, args...);
  92. std::cerr << std::endl;
  93. }
  94. else
  95. {
  96. helper(false, out, args...);
  97. std::cout << std::endl;
  98. }
  99. }
  100. template<typename Type, typename... Args>
  101. void Echo::debug(Type out, Args... args)
  102. {
  103. if(!printType(Debug))
  104. return;
  105. helper(false, out, args...);
  106. std::cout << std::endl;
  107. }
  108. template<typename Type, typename... Args>
  109. void Echo::info(Type out, Args... args)
  110. {
  111. if(!printType(Info))
  112. return;
  113. helper(false, out, args...);
  114. std::cout << std::endl;
  115. }
  116. template<typename Type, typename... Args>
  117. void Echo::load(Type out, Args... args)
  118. {
  119. if(!printType(Load))
  120. return;
  121. helper(false, out, args...);
  122. std::cout << std::endl;
  123. }
  124. template<typename Type, typename... Args>
  125. void Echo::error(Type out, Args... args)
  126. {
  127. if(!printType(Error))
  128. return;
  129. helper(true, out, args...);
  130. std::cerr << std::endl;
  131. }