Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_safe_cast.h
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Adaptive Framework Safe Cast Header
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
9 #ifndef __AFW_SAFE_CAST_H__
10 #define __AFW_SAFE_CAST_H__
11 
12 #include "afw_interface.h"
13 #include <limits.h>
14 
15 
31 
32 /*
33  * NOTE:
34  *
35  * This can be improved later based on the compiler target. For instance,
36  * if two types have the same internal representation, an #ifdef can be
37  * used to just assign the value without doing the value test.
38  */
39 
47 AFW_DEFINE_STATIC_INLINE(afw_size_t)
49 {
50  afw_size_t result = (afw_size_t)off;
51 
52  if (result != off) {
53  AFW_THROW_ERROR_Z(general, "Type safe error", xctx);
54  }
55 
56  return result;
57 }
58 
59 
67 AFW_DEFINE_STATIC_INLINE(afw_size_t)
69 {
70  afw_size_t result = (afw_size_t)integer;
71 
72  if (result != integer) {
73  AFW_THROW_ERROR_Z(general, "Type safe error", xctx);
74  }
75 
76  return result;
77 }
78 
79 
80 
88 AFW_DEFINE_STATIC_INLINE(int)
90 {
91  int result = (int)integer;
92 
93  if (result != integer) {
94  AFW_THROW_ERROR_Z(general, "Type safe error", xctx);
95  }
96 
97  return result;
98 }
99 
100 
101 
109 AFW_DEFINE_STATIC_INLINE(int)
111 {
112  int result = (int)size;
113 
114  if (result != size) {
115  AFW_THROW_ERROR_Z(general, "Type safe error", xctx);
116  }
117 
118  return result;
119 }
120 
121 
122 
130 AFW_DEFINE_STATIC_INLINE(afw_integer_t)
132 {
133  afw_integer_t result = (afw_integer_t)size;
134 
135  if (result != size) {
136  AFW_THROW_ERROR_Z(general, "Type safe error", xctx);
137  }
138 
139  return result;
140 }
141 
142 
143 
151 AFW_DEFINE_STATIC_INLINE(afw_int32_t)
153 {
154  afw_int32_t result = (afw_int32_t)size;
155 
156  if (result != size) {
157  AFW_THROW_ERROR_Z(general, "Type safe error", xctx);
158  }
159 
160  return result;
161 }
162 
163 
164 
172 AFW_DEFINE_STATIC_INLINE(afw_uint32_t)
174 {
175  afw_uint32_t result = (afw_uint32_t)size;
176 
177  if (result != size) {
178  AFW_THROW_ERROR_Z(general, "Type safe error", xctx);
179  }
180 
181  return result;
182 }
183 
184 
185 AFW_END_DECLARES
186 
189 #endif /* __AFW_SAFE_CAST_H__ */
#define AFW_BEGIN_DECLARES
Interfaceafw_interface header.
apr_int32_t afw_int32_t
32-bit signed integer.
Definition: afw_common.h:163
apr_uint32_t afw_uint32_t
32-bit unsigned integer.
Definition: afw_common.h:184
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
apr_int64_t afw_integer_t
typedef for big signed int.
Definition: afw_common.h:321
apr_off_t afw_off_t
off_t.
Definition: afw_common.h:154
#define AFW_THROW_ERROR_Z(code, message_z, xctx)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:283
afw_integer_t afw_safe_cast_size_to_integer(afw_size_t size, afw_xctx_t *xctx)
Safely cast afw_size_t to afw_integer_t.
int afw_safe_cast_size_to_int(afw_size_t size, afw_xctx_t *xctx)
Safely cast afw_size_t to int.
afw_uint32_t afw_safe_cast_size_to_uint32(afw_size_t size, afw_xctx_t *xctx)
Safely cast afw_size_t to afw_uint32_t.
int afw_safe_cast_integer_to_int(afw_integer_t integer, afw_xctx_t *xctx)
Safely cast afw_integer_t to int.
Definition: afw_safe_cast.h:89
afw_size_t afw_safe_cast_integer_to_size(afw_integer_t integer, afw_xctx_t *xctx)
Safely cast afw_integer_t to afw_size_t.
Definition: afw_safe_cast.h:68
afw_size_t afw_safe_cast_off_to_size(afw_off_t off, afw_xctx_t *xctx)
Safely cast afw_off_t to afw_size_t.
Definition: afw_safe_cast.h:48
afw_int32_t afw_safe_cast_size_to_int32(afw_size_t size, afw_xctx_t *xctx)
Safely cast afw_size_t to afw_int32_t.
Interface afw_xctx public struct.