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