Mana
Type.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include <cstdint>
10 #include <limits>
11 #include <stdexcept>
12 #include <type_traits>
13 
14 namespace mana
15 {
16 #if defined(MANA_TARGET_WINDOWS)
18  using ssize_t = intptr_t;
19 #endif
20 /*
21 #if UINTPTR_MAX == UINT64_MAX
22 #elif UINTPTR_MAX == UINT32_MAX
23 #else
24 #error "unsupport pointer size"
25 #endif
26 */
27  using float_t = float;
28  using int_t = std::int32_t;
29 
30  using address_t = std::uint32_t;
31  static constexpr address_t InvalidAddress = static_cast<address_t>(~0);
32 
33  using offset_t = std::int32_t;
34 
38  [[nodiscard]] inline bool IsValid(const address_t address)
39  {
40  return address != InvalidAddress;
41  }
42 
46  template<typename T>
47  [[nodiscard]] inline address_t ToAddress(const T size)
48  {
49  if constexpr (std::is_signed<T>())
50  {
51  if (size < 0)
52  {
53  throw std::underflow_error("Negative values for data or program size are not allowed");
54  }
55  else if (static_cast<size_t>(size) > std::numeric_limits<address_t>::max())
56  {
57  throw std::overflow_error("Data or program size must not exceed unsigned 32 bits");
58  }
59  }
60  else
61  {
62  if (static_cast<size_t>(size) > std::numeric_limits<address_t>::max())
63  {
64  throw std::overflow_error("Data or program size must not exceed unsigned 32 bits");
65  }
66  }
67  return static_cast<address_t>(size);
68  }
69 
73  template<typename T>
74  [[nodiscard]] inline offset_t ToOffset(const T size)
75  {
76  if constexpr (std::is_signed<T>())
77  {
78  if (static_cast<ssize_t>(size) > std::numeric_limits<offset_t>::min())
79  {
80  throw std::underflow_error("Data or program size must not exceed signed 32 bits");
81  }
82  else if (static_cast<ssize_t>(size) > std::numeric_limits<address_t>::max())
83  {
84  throw std::overflow_error("Data or program size must not exceed signed 32 bits");
85  }
86  }
87  else
88  {
89  if (static_cast<size_t>(size) > std::numeric_limits<address_t>::max())
90  {
91  throw std::overflow_error("Data or program size must not exceed signed 32 bits");
92  }
93  }
94  return static_cast<address_t>(size);
95  }
96 }
Definition: CodeBuffer.cpp:12
std::int32_t offset_t
Definition: Type.h:33
bool IsValid(const address_t address)
Definition: Type.h:38
std::uint32_t address_t
Definition: Type.h:30
std::int32_t int_t
Definition: Type.h:28
offset_t ToOffset(const T size)
Definition: Type.h:74
float float_t
Definition: Type.h:27
address_t ToAddress(const T size)
Definition: Type.h:47