StdTypes.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/Config.h>
  7. #include <cstdint>
  8. #include <cstddef>
  9. #include <limits>
  10. namespace anki
  11. {
  12. /// @addtogroup util_other
  13. /// @{
  14. using I8 = int8_t; ///< Integer 8bit
  15. const I8 MAX_I8 = std::numeric_limits<I8>::max();
  16. const I8 MIN_I8 = std::numeric_limits<I8>::min();
  17. using I16 = int16_t; ///< Integer 16bit
  18. const I16 MAX_I16 = std::numeric_limits<I16>::max();
  19. const I16 MIN_I16 = std::numeric_limits<I16>::min();
  20. using I32 = int32_t; ///< Integer 32bit
  21. const I32 MAX_I32 = std::numeric_limits<I32>::max();
  22. const I32 MIN_I32 = std::numeric_limits<I32>::min();
  23. using I64 = int64_t; ///< Integer 64bit
  24. const I64 MAX_I64 = std::numeric_limits<I64>::max();
  25. const I64 MIN_I64 = std::numeric_limits<I64>::min();
  26. using I = int_fast32_t; ///< Fast signed integer at least 32bit
  27. const I MAX_I = std::numeric_limits<I>::max();
  28. const I MIN_I = std::numeric_limits<I>::min();
  29. using U8 = uint8_t; ///< Unsigned integer 8bit
  30. const U8 MAX_U8 = std::numeric_limits<U8>::max();
  31. const U8 MIN_U8 = std::numeric_limits<U8>::min();
  32. using U16 = uint16_t; ///< Unsigned integer 16bit
  33. const U16 MAX_U16 = std::numeric_limits<U16>::max();
  34. const U16 MIN_U16 = std::numeric_limits<U16>::min();
  35. using U32 = uint32_t; ///< Unsigned integer 32bit
  36. const U32 MAX_U32 = std::numeric_limits<U32>::max();
  37. const U32 MIN_U32 = std::numeric_limits<U32>::min();
  38. using U64 = uint64_t; ///< Unsigned integer 64bit
  39. const U64 MAX_U64 = std::numeric_limits<U64>::max();
  40. const U64 MIN_U64 = std::numeric_limits<U64>::min();
  41. using U = uint_fast32_t; ///< Fast unsigned integer at least 32bit
  42. const U MAX_U = std::numeric_limits<U>::max();
  43. const U MIN_U = std::numeric_limits<U>::min();
  44. using PtrSize = size_t; ///< Like size_t
  45. const PtrSize MAX_PTR_SIZE = std::numeric_limits<PtrSize>::max();
  46. const PtrSize MIN_PTR_SIZE = std::numeric_limits<PtrSize>::min();
  47. using F32 = float; ///< Floating point 32bit
  48. const F32 MAX_F32 = std::numeric_limits<F32>::max();
  49. const F32 MIN_F32 = -std::numeric_limits<F32>::max();
  50. using F64 = double; ///< Floating point 64bit
  51. const F64 MAX_F64 = std::numeric_limits<F64>::max();
  52. const F64 MIN_F64 = -std::numeric_limits<F64>::max();
  53. using Bool = bool; ///< Fast boolean type
  54. using Bool8 = U8; ///< Small 8bit boolean type
  55. using Bool32 = U32; ///< A 32bit boolean
  56. /// Error codes
  57. enum class ErrorCode : I32
  58. {
  59. NONE,
  60. OUT_OF_MEMORY,
  61. FUNCTION_FAILED, ///< External operation failed
  62. USER_DATA,
  63. // File errors
  64. FILE_NOT_FOUND,
  65. FILE_ACCESS, ///< Read/write access error
  66. UNKNOWN
  67. };
  68. /// Representation of error and a wrapper on top of error codes.
  69. class Error
  70. {
  71. public:
  72. /// Construct using an error code.
  73. Error(ErrorCode code)
  74. : m_code(code)
  75. {
  76. }
  77. /// Copy.
  78. Error(const Error& b)
  79. : m_code(b.m_code)
  80. {
  81. }
  82. /// Copy.
  83. Error& operator=(const Error& b)
  84. {
  85. m_code = b.m_code;
  86. return *this;
  87. }
  88. /// Compare.
  89. Bool operator==(const Error& b) const
  90. {
  91. return m_code == b.m_code;
  92. }
  93. /// Compare.
  94. Bool operator==(ErrorCode code) const
  95. {
  96. return m_code == code;
  97. }
  98. /// Compare.
  99. Bool operator!=(const Error& b) const
  100. {
  101. return m_code != b.m_code;
  102. }
  103. /// Compare.
  104. Bool operator!=(ErrorCode code) const
  105. {
  106. return m_code != code;
  107. }
  108. /// Check if it is an error.
  109. operator Bool() const
  110. {
  111. return m_code != ErrorCode::NONE;
  112. }
  113. /// @privatesection
  114. /// @{
  115. ErrorCode _getCode() const
  116. {
  117. return m_code;
  118. }
  119. I32 _getCodeInt() const
  120. {
  121. return static_cast<I32>(m_code);
  122. }
  123. /// @}
  124. private:
  125. ErrorCode m_code = ErrorCode::NONE;
  126. };
  127. /// Macro to check if a method/function returned an error.
  128. #define ANKI_CHECK(x_) \
  129. do \
  130. { \
  131. Error error = x_; \
  132. if(ANKI_UNLIKELY(error)) \
  133. { \
  134. return error; \
  135. } \
  136. } while(0)
  137. /// Macro the check if a memory allocation is OOM.
  138. #define ANKI_CHECK_OOM(x_) \
  139. do \
  140. { \
  141. if(ANKI_UNLIKELY(x_ == nullptr)) \
  142. { \
  143. return ErrorCode::OUT_OF_MEMORY; \
  144. } \
  145. } while(0)
  146. /// Macro to nuliffy a pointer on debug builds.
  147. #if ANKI_DEBUG == 1
  148. #define ANKI_DBG_NULLIFY_PTR = nullptr
  149. #else
  150. #define ANKI_DBG_NULLIFY_PTR
  151. #endif
  152. /// @}
  153. } // end namespace anki