Common.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * TI Voxel Lib component.
  3. *
  4. * Copyright (c) 2014 Texas Instruments Inc.
  5. */
  6. #ifndef VOXEL_COMMON_H
  7. #define VOXEL_COMMON_H
  8. #include <vector>
  9. #include <stdint.h>
  10. #include <string>
  11. #include <iostream>
  12. #include <fstream>
  13. #include <sstream>
  14. #include <unordered_map>
  15. #include <functional>
  16. #include <list>
  17. #include <set>
  18. #include <atomic>
  19. #include <cctype>
  20. #include <algorithm>
  21. #include <complex>
  22. #include <thread>
  23. #include <Ptr.h>
  24. #include <mutex>
  25. #include <condition_variable>
  26. #include "VoxelConfig.h"
  27. #include "VoxelExports.h"
  28. #ifdef _WIN32
  29. typedef uint32_t uint;
  30. #endif
  31. namespace Voxel
  32. {
  33. /**
  34. * \defgroup Util Common and Utility classes
  35. * @{
  36. */
  37. #ifndef SWIG
  38. template <typename T>
  39. using Vector = std::vector<T>;
  40. template <typename T>
  41. using List = std::list<T>;
  42. template <typename T>
  43. using Set = std::set<T>;
  44. template <typename K, typename V>
  45. using Map = std::unordered_map<K, V>;
  46. template <typename T>
  47. using Function = std::function<T>;
  48. template <typename T>
  49. using Atomic = std::atomic<T>;
  50. template <typename T>
  51. using Lock = std::unique_lock<T>;
  52. template <typename T>
  53. using shared_ptr = Ptr<T>;
  54. #else
  55. #define Vector std::vector
  56. #define List std::list
  57. #define Set std::set
  58. #define Map std::unordered_map
  59. #define Function std::function
  60. #define Atomic std::atomic
  61. #define Lock std::unique_lock
  62. #endif
  63. typedef std::string String;
  64. typedef int IndexType;
  65. typedef std::size_t SizeType;
  66. typedef uint8_t ByteType;
  67. typedef uint64_t TimeStampType;
  68. typedef uint32_t GeneratorIDType;
  69. typedef uint64_t FileOffsetType;
  70. typedef std::complex<float> Complex;
  71. typedef std::complex<double> ComplexDouble;
  72. typedef std::thread Thread;
  73. typedef Ptr<Thread> ThreadPtr;
  74. typedef std::mutex Mutex;
  75. typedef std::condition_variable ConditionVariable;
  76. typedef std::ifstream InputFileStream;
  77. typedef std::ofstream OutputFileStream;
  78. typedef std::istream InputStream;
  79. typedef std::ostream OutputStream;
  80. typedef std::ostringstream OutputStringStream;
  81. typedef std::istringstream InputStringStream;
  82. struct Version { uint8_t major, minor; };
  83. /// String functions
  84. String VOXEL_EXPORT getHex(uint16_t value);
  85. void VOXEL_EXPORT split(const String &str, const char delimiter, Vector<String> &split);
  86. void VOXEL_EXPORT breakLines(const String &str, std::ostream &out, const uint maxPerLine, const String &newlinePrefix);
  87. /// Array handling template
  88. template<typename T, int sz>
  89. int arraySize(T(&)[sz])
  90. {
  91. return sz;
  92. }
  93. // Allocate in bytes and irrespective of size required by 'T'. This is useful if 'T' is has data structures which could have extendable elements.
  94. // E.g.: Windows USB device handling structures in usbioctl.h
  95. template <typename T>
  96. Ptr<T> byteAlloc(unsigned long sizeInBytes)
  97. {
  98. return Ptr<T>((T *)new uint8_t[sizeInBytes], [](T *d) { delete[]((uint8_t *)d); });
  99. }
  100. uint VOXEL_EXPORT gcd(uint n, uint m);
  101. // This returns nearest 'n = 2^m' such that 2^(m - 1) < value < 2^m
  102. unsigned int VOXEL_EXPORT nearestPowerOf2(unsigned int value, unsigned int &index);
  103. #define FLOAT_EPSILON 1E-5f
  104. inline bool floatEquals(float lhs, float rhs)
  105. {
  106. if((lhs - rhs > FLOAT_EPSILON) || (rhs - lhs > FLOAT_EPSILON))
  107. return false;
  108. else
  109. return true;
  110. }
  111. /**
  112. * @brief Left Trim
  113. *
  114. * Trims whitespace from the left end of the provided String
  115. *
  116. * @param[out] s The String to trim
  117. *
  118. * @return The modified String&
  119. */
  120. inline String& ltrim(String& s) {
  121. s.erase(s.begin(), std::find_if(s.begin(), s.end(),
  122. std::ptr_fun<int, int>(std::isgraph)));
  123. return s;
  124. }
  125. /**
  126. * @brief Right Trim
  127. *
  128. * Trims whitespace from the right end of the provided String
  129. *
  130. * @param[out] s The String to trim
  131. *
  132. * @return The modified String&
  133. */
  134. inline String& rtrim(String& s) {
  135. s.erase(std::find_if(s.rbegin(), s.rend(),
  136. std::ptr_fun<int, int>(std::isgraph)).base(), s.end());
  137. return s;
  138. }
  139. /**
  140. * @brief Trim
  141. *
  142. * Trims whitespace from both ends of the provided String
  143. *
  144. * @param[out] s The String to trim
  145. *
  146. * @return The modified String&
  147. */
  148. inline String& trim(String& s) {
  149. return ltrim(rtrim(s));
  150. }
  151. /// Filesystem functions -- returns the number of files read or else -1 for error. Only those files whose name matches "matchString" partially (sub-string) are returned "files"
  152. int VOXEL_EXPORT getFiles(const String &dir, const String &matchString, Vector<String> &files);
  153. bool VOXEL_EXPORT isFilePresent(const String &filename);
  154. bool VOXEL_EXPORT isDirectory(const String &filename);
  155. bool VOXEL_EXPORT makeDirectory(const String &filename);
  156. String VOXEL_EXPORT dirname(const String &filename);
  157. String VOXEL_EXPORT basename(const String &filename);
  158. String VOXEL_EXPORT substitute(const String &s, const Vector<String> &keys, const Vector<String> &values);
  159. /**
  160. * @}
  161. */
  162. }
  163. #endif //VOXEL_COMMON_H