Functions.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /// A simple template trick to remove the pointer from one type
  49. ///
  50. /// Example:
  51. /// @code
  52. /// double a = 1234.456;
  53. /// RemovePointer<decltype(&a)>::Type b = a;
  54. /// @endcode
  55. /// The b is of type double
  56. template<typename T>
  57. struct RemovePointer;
  58. template<typename T>
  59. struct RemovePointer<T*>
  60. {
  61. typedef T Type;
  62. };
  63. /// @}
  64. /// @}
  65. } // end namespace anki
  66. #endif