| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
#pragma once |
| 8 |
|
| 9 |
#include <functional> |
| 10 |
#include <type_traits> |
| 11 |
#include <cstddef> |
| 12 |
#include "dxxsconf.h" |
| 13 |
#include "cpp-valptridx.h" |
| 14 |
#include "d_array.h" |
| 15 |
|
| 16 |
#if DXX_HAVE_CXX_BUILTIN_FILE_LINE |
| 17 |
#define DXX_VALPTRIDX_ENABLE_REPORT_FILENAME |
| 18 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DECL_VARS const char *filename = __builtin_FILE(), const unsigned lineno = __builtin_LINE() |
| 19 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS , DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DECL_VARS |
| 20 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DEFN_VARS const char *const filename, const unsigned lineno |
| 21 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DEFN_VARS, |
| 22 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_PASS_VARS_ filename, lineno |
| 23 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_VOID_VARS() static_cast<void>(filename), static_cast<void>(lineno) |
| 24 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_PASS_VARS , DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_PASS_VARS_ |
| 25 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_PASS_VARS_, |
| 26 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VA(...) DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_PASS_VARS_, ## __VA_ARGS__ |
| 27 |
#define DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_N_VARS "rm" (filename), "rm" (lineno) |
| 28 |
#define DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_R_VARS DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_N_VARS, |
| 29 |
#else |
| 30 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DECL_VARS |
| 31 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS |
| 32 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DEFN_VARS |
| 33 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS |
| 34 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_VOID_VARS() static_cast<void>(0) |
| 35 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_PASS_VARS |
| 36 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS |
| 37 |
#define DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VA(...) __VA_ARGS__ |
| 38 |
#define DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_N_VARS |
| 39 |
#define DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_R_VARS |
| 40 |
#endif |
| 41 |
|
| 42 |
template <typename managed_type> |
| 43 |
class valptridx : |
| 44 |
protected valptridx_specialized_types<managed_type>::type |
| 45 |
{ |
| 46 |
using specialized_types = typename valptridx_specialized_types<managed_type>::type; |
| 47 |
using specialized_types::array_size; |
| 48 |
class partial_policy |
| 49 |
{ |
| 50 |
public: |
| 51 |
class require_valid; |
| 52 |
class allow_invalid; |
| 53 |
template <template <typename> class policy> |
| 54 |
class apply_cv_policy; |
| 55 |
}; |
| 56 |
class vc; /* require_valid + const_policy */ |
| 57 |
class ic; /* allow_invalid + const_policy */ |
| 58 |
class vm; /* require_valid + mutable_policy */ |
| 59 |
class im; /* allow_invalid + mutable_policy */ |
| 60 |
template <typename> |
| 61 |
class guarded; |
| 62 |
class array_base_count_type; |
| 63 |
struct array_base_storage_integral |
| 64 |
{ |
| 65 |
using type = std::array<managed_type, array_size>; |
| 66 |
}; |
| 67 |
/* Note: index_type must be an `enum` type, but |
| 68 |
* `requires(std::is_enum<index_type>::value)` cannot be checked |
| 69 |
* here, because the name `array_base_storage_enum<index_type>` may be |
| 70 |
* formed for non-enum types, but the internal type `type` will not be |
| 71 |
* used in those cases. |
| 72 |
*/ |
| 73 |
template <typename index_type> |
| 74 |
struct array_base_storage_enum |
| 75 |
{ |
| 76 |
using type = dcx::enumerated_array<managed_type, array_size, index_type>; |
| 77 |
}; |
| 78 |
protected: |
| 79 |
using const_pointer_type = const managed_type *; |
| 80 |
using const_reference_type = const managed_type &; |
| 81 |
using mutable_pointer_type = managed_type *; |
| 82 |
using typename specialized_types::integral_type; |
| 83 |
using typename specialized_types::index_type; |
| 84 |
static_assert(std::is_enum_v<index_type> || std::unsigned_integral<index_type>); |
| 85 |
using array_base_storage_type = typename std::conditional_t<std::is_integral<index_type>::value, array_base_storage_integral, array_base_storage_enum<index_type>>::type; |
| 86 |
|
| 87 |
public: |
| 88 |
class array_managed_type; |
| 89 |
using typename specialized_types::report_error_uses_exception; |
| 90 |
/* ptridx<policy> publicly inherits from idx<policy> and |
| 91 |
* ptr<policy>, but should not be implicitly sliced to one of the |
| 92 |
* base types. To prevent slicing, define |
| 93 |
* DXX_VALPTRIDX_ENFORCE_STRICT_PI_SEPARATION to a non-zero value. |
| 94 |
* When enabled, slicing prevention makes *ptr and *idx derive from |
| 95 |
* ptr<policy> / idx<policy>. When disabled, *ptr and *idx are |
| 96 |
* typedef aliases for ptr<policy> / idx<policy>. |
| 97 |
*/ |
| 98 |
template <typename policy> |
| 99 |
class idx; |
| 100 |
template <typename policy> |
| 101 |
class ptr; |
| 102 |
template <typename policy> |
| 103 |
class ptridx; |
| 104 |
protected: |
| 105 |
template <typename Pc, typename Pm> |
| 106 |
class basic_ival_member_factory; |
| 107 |
template <typename Pc, typename Pm> |
| 108 |
class basic_vval_member_factory; |
| 109 |
template <typename T> |
| 110 |
class factory_range_iterator; |
| 111 |
using typename specialized_types::allow_end_construction; |
| 112 |
using typename specialized_types::assume_nothrow_index; |
| 113 |
|
| 114 |
template <typename handle_index_mismatch> |
| 115 |
static inline void check_index_match(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_reference_type, index_type, const array_managed_type &); |
| 116 |
template < |
| 117 |
typename handle_index_range_error, |
| 118 |
template <typename> class Compare = std::less |
| 119 |
> |
| 120 |
static inline index_type check_index_range(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS index_type, const array_managed_type *); |
| 121 |
template < |
| 122 |
typename handle_index_range_error, |
| 123 |
template <typename> class Compare = std::less |
| 124 |
> |
| 125 |
static inline index_type check_index_range_size(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS std::size_t, const array_managed_type *); |
| 126 |
template <typename handle_index_mismatch, typename handle_index_range_error> |
| 127 |
static inline void check_explicit_index_range_ref(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_reference_type, std::size_t, const array_managed_type &); |
| 128 |
/* Given a reference `r` to an element that should be in the array `a`, |
| 129 |
* compute the array index `i` that should refer to `r`. Check that `i` is |
| 130 |
* within the bounds of the array, and that computing `&a[i]` yields `&r`. |
| 131 |
* The second test catches an error of the form: |
| 132 |
* |
| 133 |
* T a[3]; |
| 134 |
* T *b = reinterpret_cast<char *>(&a[1]) + 1; |
| 135 |
* check_implicit_index_range_ref(*b, a); |
| 136 |
* |
| 137 |
* Use of the cast would allow the pointer to become misaligned and point |
| 138 |
* into the middle of an instance. Such a pointer would never be correct, |
| 139 |
* so detect and trap it here. |
| 140 |
*/ |
| 141 |
template <typename handle_index_mismatch, typename handle_index_range_error> |
| 142 |
static inline void check_implicit_index_range_ref(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_reference_type r, const array_managed_type &a); |
| 143 |
template <typename handle_null_pointer> |
| 144 |
static inline void check_null_pointer_conversion(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_pointer_type); |
| 145 |
template <typename handle_null_pointer> |
| 146 |
static inline void check_null_pointer(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_pointer_type, const array_managed_type &); |
| 147 |
|
| 148 |
#define DXX_VALPTRIDX_FOR_EACH_VC_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, IVPREFIX) \ |
| 149 |
VERB(MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, IVPREFIX, m); \ |
| 150 |
VERB(MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, IVPREFIX, c) |
| 151 |
|
| 152 |
#define DXX_VALPTRIDX_FOR_EACH_IV_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX) \ |
| 153 |
DXX_VALPTRIDX_FOR_EACH_VC_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, i); \ |
| 154 |
DXX_VALPTRIDX_FOR_EACH_VC_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, v) |
| 155 |
|
| 156 |
#define DXX_VALPTRIDX_FOR_EACH_IDX_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT) \ |
| 157 |
DXX_VALPTRIDX_FOR_EACH_IV_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, idx) |
| 158 |
|
| 159 |
#define DXX_VALPTRIDX_FOR_EACH_PTR_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT) \ |
| 160 |
DXX_VALPTRIDX_FOR_EACH_IV_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, ptr) |
| 161 |
|
| 162 |
#define DXX_VALPTRIDX_FOR_EACH_PTRIDX_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT) \ |
| 163 |
DXX_VALPTRIDX_FOR_EACH_IV_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, ptridx) |
| 164 |
|
| 165 |
#define DXX_VALPTRIDX_FOR_EACH_PPI_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT) \ |
| 166 |
DXX_VALPTRIDX_FOR_EACH_PTR_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT); \ |
| 167 |
DXX_VALPTRIDX_FOR_EACH_PTRIDX_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT) |
| 168 |
|
| 169 |
#define DXX_VALPTRIDX_FOR_EACH_IPPI_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT) \ |
| 170 |
DXX_VALPTRIDX_FOR_EACH_IDX_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT); \ |
| 171 |
DXX_VALPTRIDX_FOR_EACH_PPI_TYPE(VERB, MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT) |
| 172 |
|
| 173 |
class index_mismatch_exception; |
| 174 |
class index_range_exception; |
| 175 |
class null_pointer_exception; |
| 176 |
public: |
| 177 |
/* This is a special placeholder that allows segiter to bypass the |
| 178 |
* normal rules. The calling code is responsible for providing the |
| 179 |
* safety that is bypassed. Use this bypass only if you understand |
| 180 |
* exactly what you are skipping and why your use is safe despite |
| 181 |
* the lack of checking. |
| 182 |
*/ |
| 183 |
using typename specialized_types::allow_none_construction; |
| 184 |
using typename specialized_types::rebind_policy; |
| 185 |
typedef ptridx<ic> icptridx; |
| 186 |
typedef ptridx<im> imptridx; |
| 187 |
typedef ptridx<vc> vcptridx; |
| 188 |
typedef ptridx<vm> vmptridx; |
| 189 |
|
| 190 |
#define DXX_VALPTRIDX_DECLARE_PI_TYPE(MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, IVPREFIX, MCPREFIX) \ |
| 191 |
using IVPREFIX ## MCPREFIX ## PISUFFIX = typename specialized_types::template wrapper< PISUFFIX < IVPREFIX ## MCPREFIX > > |
| 192 |
DXX_VALPTRIDX_FOR_EACH_IDX_TYPE(DXX_VALPTRIDX_DECLARE_PI_TYPE,,,); |
| 193 |
DXX_VALPTRIDX_FOR_EACH_PTR_TYPE(DXX_VALPTRIDX_DECLARE_PI_TYPE,,,); |
| 194 |
#undef DXX_VALPTRIDX_DEFINE_PI_TYPE |
| 195 |
|
| 196 |
#define DXX_VALPTRIDX_MC_qualifier_m |
| 197 |
#define DXX_VALPTRIDX_MC_qualifier_c const |
| 198 |
#define DXX_VALPTRIDX_DECLARE_MEMBER_FACTORIES(MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, IVPREFIX, MCPREFIX) \ |
| 199 |
using f ## IVPREFIX ## MCPREFIX ## PISUFFIX = \ |
| 200 |
DXX_VALPTRIDX_MC_qualifier_ ## MCPREFIX \ |
| 201 |
basic_ ## IVPREFIX ## val_member_factory< \ |
| 202 |
IVPREFIX ## c ## PISUFFIX, \ |
| 203 |
IVPREFIX ## m ## PISUFFIX \ |
| 204 |
> |
| 205 |
DXX_VALPTRIDX_FOR_EACH_PPI_TYPE(DXX_VALPTRIDX_DECLARE_MEMBER_FACTORIES,,,); |
| 206 |
#undef DXX_VALPTRIDX_DECLARE_MEMBER_FACTORIES |
| 207 |
#undef DXX_VALPTRIDX_MC_qualifier_c |
| 208 |
#undef DXX_VALPTRIDX_MC_qualifier_m |
| 209 |
|
| 210 |
/* Commit a DRY violation here so that tools can easily find these |
| 211 |
* types. Strict DRY compliance would mean using a macro to |
| 212 |
* generate the `using` line, since the typedef, the dispatcher type |
| 213 |
* it references, and an argument to that type are all dependent on |
| 214 |
* the same string. |
| 215 |
*/ |
| 216 |
template <typename T> |
| 217 |
using index_mismatch_error_type = typename specialized_types::template dispatch_index_mismatch_error<T, index_mismatch_exception>; |
| 218 |
template <typename T> |
| 219 |
using index_range_error_type = typename specialized_types::template dispatch_index_range_error<T, index_range_exception>; |
| 220 |
template <typename T> |
| 221 |
using null_pointer_error_type = typename specialized_types::template dispatch_null_pointer_error<T, null_pointer_exception>; |
| 222 |
|
| 223 |
template <index_type constant> |
| 224 |
class magic_constant |
| 225 |
{ |
| 226 |
public: |
| 227 |
constexpr operator index_type() const { return constant; } |
| 228 |
}; |
| 229 |
}; |
| 230 |
|
| 231 |
#define DXX_VALPTRIDX_DEFINE_FACTORY_TYPEDEF(MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, IVPREFIX, MCPREFIX) \ |
| 232 |
using f ## IVPREFIX ## MCPREFIX ## DERIVED_TYPE_PREFIX ## PISUFFIX = valptridx<MANAGED_TYPE>::f ## IVPREFIX ## MCPREFIX ## PISUFFIX |
| 233 |
|
| 234 |
#define DXX_VALPTRIDX_DEFINE_SUBTYPE_TYPEDEF(MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, IVPREFIX, MCPREFIX) \ |
| 235 |
using IVPREFIX ## MCPREFIX ## DERIVED_TYPE_PREFIX ## PISUFFIX ## _t = valptridx<MANAGED_TYPE>::IVPREFIX ## MCPREFIX ## PISUFFIX |
| 236 |
|
| 237 |
#define DXX_VALPTRIDX_DEFINE_SUBTYPE_TYPEDEFS(MANAGED_TYPE, DERIVED_TYPE_PREFIX) \ |
| 238 |
using MANAGED_TYPE ## _array = valptridx<MANAGED_TYPE>::array_managed_type; \ |
| 239 |
DXX_VALPTRIDX_FOR_EACH_PPI_TYPE(DXX_VALPTRIDX_DEFINE_FACTORY_TYPEDEF, MANAGED_TYPE, DERIVED_TYPE_PREFIX,); \ |
| 240 |
DXX_VALPTRIDX_FOR_EACH_IPPI_TYPE(DXX_VALPTRIDX_DEFINE_SUBTYPE_TYPEDEF, MANAGED_TYPE, DERIVED_TYPE_PREFIX,) |