Utility.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. static void out(std::string debug);
  48. static void out(float debug);
  49. static void setLogLevel(int loglevel);
  50. private:
  51. static void helper(bool err, std::wstring out);
  52. template <typename T>
  53. static void helper(bool err, T out);
  54. template<typename Type, typename... Args>
  55. static void helper(bool err, const Type out, Args... args);
  56. static bool printType(int order);
  57. static int _loglevel;
  58. };
  59. template <typename Type>
  60. void Echo::helper(bool err, Type out)
  61. {
  62. if(err)
  63. std::cerr << out;
  64. else
  65. std::cout << out;
  66. }
  67. template<typename Type, typename... Args>
  68. void Echo::helper(bool err, const Type out, Args... args)
  69. {
  70. if(err)
  71. std::cerr << out;
  72. else
  73. std::cout << out;
  74. helper(err, args...);
  75. }
  76. template<typename Type, typename... Args>
  77. void Echo::out(int order, Type out, Args... args)
  78. {
  79. if(!printType(order))
  80. return;
  81. if(order == Error)
  82. {
  83. helper(true, out, args...);
  84. std::cerr << std::endl;
  85. }
  86. else
  87. {
  88. helper(false, out, args...);
  89. std::cout << std::endl;
  90. }
  91. }