Functions.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /// @file
  2. /// Contains misc functions
  3. #ifndef ANKI_UTIL_FUNCTIONS_H
  4. #define ANKI_UTIL_FUNCTIONS_H
  5. #include "anki/util/StdTypes.h"
  6. #include <string>
  7. #include <cmath>
  8. namespace anki {
  9. /// @addtogroup util
  10. /// @{
  11. /// @addtogroup misc
  12. /// @{
  13. /// Pick a random number from min to max
  14. extern I32 randRange(I32 min, I32 max);
  15. /// Pick a random number from min to max
  16. extern U32 randRange(U32 min, U32 max);
  17. /// Pick a random number from min to max
  18. extern F32 randRange(F32 min, F32 max);
  19. /// Pick a random number from min to max
  20. extern F64 randRange(F64 min, F64 max);
  21. extern F32 randFloat(F32 max);
  22. /// Get align number
  23. /// @param alignment The bytes of alignment
  24. /// @param value The value to align
  25. template<typename Type>
  26. Type getAlignedRoundUp(PtrSize alignment, Type value)
  27. {
  28. PtrSize v = (PtrSize)value;
  29. v = (v + alignment - 1) & ~(alignment - 1);
  30. return (Type)v;
  31. }
  32. /// Align number
  33. /// @param alignment The bytes of alignment
  34. /// @param value The value to align
  35. template<typename Type>
  36. void alignRoundUp(PtrSize alignment, Type& value)
  37. {
  38. value = getAlignedRoundUp(alignment, value);
  39. }
  40. /// Check if a number is aligned
  41. template<typename Type>
  42. bool isAligned(PtrSize alignment, Type value)
  43. {
  44. return ((PtrSize)value % alignment) == 0;
  45. }
  46. /// Get the size in bytes of a vector
  47. template<typename Vec>
  48. inline PtrSize getVectorSizeInBytes(const Vec& v)
  49. {
  50. return v.size() * sizeof(typename Vec::value_type);
  51. }
  52. /// Trim a string
  53. inline std::string trimString(const std::string& str, const char* what = " ")
  54. {
  55. std::string out = str;
  56. out.erase(0, out.find_first_not_of(what));
  57. out.erase(out.find_last_not_of(what) + 1);
  58. return out;
  59. }
  60. /// Check if a number os a power of 2
  61. inline Bool isPowerOfTwo(U64 x)
  62. {
  63. while(((x % 2) == 0) && x > 1)
  64. {
  65. x /= 2;
  66. }
  67. return (x == 1);
  68. }
  69. /// Get the next power of two number. For example if x is 130 this will return
  70. /// 256
  71. template<typename Int>
  72. inline Int nextPowerOfTwo(Int x)
  73. {
  74. return pow(2, ceil(log(x) / log(2)));
  75. }
  76. /// Replace substring. Substitute occurances of @a from into @a to inside the
  77. /// @a str string
  78. extern std::string replaceAllString(const std::string& str,
  79. const std::string& from, const std::string& to);
  80. /// Delete a pointer properly
  81. template<typename T>
  82. inline void propperDelete(T*& x)
  83. {
  84. typedef char TypeMustBeComplete[sizeof(T) ? 1 : -1];
  85. (void) sizeof(TypeMustBeComplete);
  86. delete x;
  87. x = nullptr;
  88. }
  89. /// Delete a pointer properly
  90. template<typename T>
  91. inline void propperDeleteArray(T*& x)
  92. {
  93. typedef char TypeMustBeComplete[sizeof(T) ? 1 : -1];
  94. (void) sizeof(TypeMustBeComplete);
  95. delete[] x;
  96. x = nullptr;
  97. }
  98. /// A simple template trick to remove the pointer from one type
  99. ///
  100. /// Example:
  101. /// @code
  102. /// double a = 1234.456;
  103. /// RemovePointer<decltype(&a)>::Type b = a;
  104. /// @endcode
  105. /// The b is of type double
  106. template<typename T>
  107. struct RemovePointer;
  108. template<typename T>
  109. struct RemovePointer<T*>
  110. {
  111. typedef T Type;
  112. };
  113. /// @}
  114. /// @}
  115. } // end namespace anki
  116. #endif