Functions.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. namespace anki {
  8. /// @addtogroup util
  9. /// @{
  10. /// @addtogroup misc
  11. /// @{
  12. /// Pick a random number from min to max
  13. extern I32 randRange(I32 min, I32 max);
  14. /// Pick a random number from min to max
  15. extern U32 randRange(U32 min, U32 max);
  16. /// Pick a random number from min to max
  17. extern F32 randRange(F32 min, F32 max);
  18. /// Pick a random number from min to max
  19. extern F64 randRange(F64 min, F64 max);
  20. extern F32 randFloat(F32 max);
  21. /// Get the size in bytes of a vector
  22. template<typename Vec>
  23. inline PtrSize getVectorSizeInBytes(const Vec& v)
  24. {
  25. return v.size() * sizeof(typename Vec::value_type);
  26. }
  27. /// Trim a string
  28. inline std::string trimString(const std::string& str, const char* what = " ")
  29. {
  30. std::string out = str;
  31. out.erase(0, out.find_first_not_of(what));
  32. out.erase(out.find_last_not_of(what) + 1);
  33. return out;
  34. }
  35. /// Replace substring. Substitute occurances of @a from into @a to inside the
  36. /// @a str string
  37. extern std::string replaceAllString(const std::string& str,
  38. const std::string& from, const std::string& to);
  39. /// Delete a pointer properly
  40. template<typename T>
  41. inline void propperDelete(T*& x)
  42. {
  43. typedef char TypeMustBeComplete[sizeof(T) ? 1 : -1];
  44. (void) sizeof(TypeMustBeComplete);
  45. delete x;
  46. x = nullptr;
  47. }
  48. /// Delete a pointer properly
  49. template<typename T>
  50. inline void propperDeleteArray(T*& x)
  51. {
  52. typedef char TypeMustBeComplete[sizeof(T) ? 1 : -1];
  53. (void) sizeof(TypeMustBeComplete);
  54. delete[] x;
  55. x = nullptr;
  56. }
  57. /// A simple template trick to remove the pointer from one type
  58. ///
  59. /// Example:
  60. /// @code
  61. /// double a = 1234.456;
  62. /// RemovePointer<decltype(&a)>::Type b = a;
  63. /// @endcode
  64. /// The b is of type double
  65. template<typename T>
  66. struct RemovePointer;
  67. template<typename T>
  68. struct RemovePointer<T*>
  69. {
  70. typedef T Type;
  71. };
  72. /// @}
  73. /// @}
  74. } // end namespace anki
  75. #endif