StdTypes.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef ANKI_UTIL_STD_TYPES_H
  6. #define ANKI_UTIL_STD_TYPES_H
  7. #include "anki/Config.h"
  8. #include <cstdint>
  9. #include <cstddef>
  10. #include <limits>
  11. namespace anki {
  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. /// Error codes
  56. enum class ErrorCode
  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. /// @}
  125. } // end namespace anki
  126. #endif