StdTypes.h 3.9 KB

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