| 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 <iterator> |
| 10 |
#include <limits> |
| 11 |
#include <optional> |
| 12 |
#include <stdexcept> |
| 13 |
#include <string> |
| 14 |
#include <type_traits> |
| 15 |
#include <utility> |
| 16 |
#include "fwd-valptridx.h" |
| 17 |
#include "pack.h" |
| 18 |
#include "compiler-poison.h" |
| 19 |
|
| 20 |
#ifdef DXX_CONSTANT_TRUE |
| 21 |
#ifdef DXX_HAVE_ATTRIBUTE_WARNING |
| 22 |
/* This causes many warnings because some conversions are not checked for |
| 23 |
* safety. Eliminating the warnings by changing the call sites to check first |
| 24 |
* would be a useful improvement. |
| 25 |
*/ |
| 26 |
//#define DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT __attribute__((__warning__("call not eliminated"))) |
| 27 |
#endif |
| 28 |
#endif |
| 29 |
|
| 30 |
#define DXX_VALPTRIDX_CHECK(SUCCESS_CONDITION,ERROR,...) \ |
| 31 |
static_cast<void>( \ |
| 32 |
static_cast<bool>(SUCCESS_CONDITION) || (ERROR::report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VA(__VA_ARGS__)), 0) \ |
| 33 |
) \ |
| 34 |
|
| 35 |
#ifndef DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 36 |
#define DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 37 |
#endif |
| 38 |
|
| 39 |
template <typename managed_type> |
| 40 |
class valptridx<managed_type>::array_base_count_type |
| 41 |
{ |
| 42 |
protected: |
| 43 |
/* Use the smallest size that will store all legal values. Currently, |
| 44 |
* no user has a maximum size that needs more than uint16_t, so reject |
| 45 |
* attempts to use that size. This protects against silently getting |
| 46 |
* the wrong result if a future change raises the maximum size above |
| 47 |
* uint16_t. |
| 48 |
*/ |
| 49 |
using count_type = typename std::conditional<(array_size <= UINT8_MAX), uint8_t, typename std::conditional<(array_size <= UINT16_MAX), uint16_t, void>::type>::type; |
| 50 |
union { |
| 51 |
count_type count{}; |
| 52 |
/* |
| 53 |
* Use DXX_VALPTRIDX_FOR_EACH_PPI_TYPE to generate empty union |
| 54 |
* members based on basic_{i,v}val_member_factory |
| 55 |
* specializations. |
| 56 |
*/ |
| 57 |
#define DXX_VALPTRIDX_DEFINE_MEMBER_FACTORIES(MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, IVPREFIX, MCPREFIX) \ |
| 58 |
valptridx<MANAGED_TYPE>::f ## IVPREFIX ## MCPREFIX ## PISUFFIX \ |
| 59 |
IVPREFIX ## MCPREFIX ## PISUFFIX |
| 60 |
DXX_VALPTRIDX_FOR_EACH_PPI_TYPE(DXX_VALPTRIDX_DEFINE_MEMBER_FACTORIES, managed_type,,); |
| 61 |
#undef DXX_VALPTRIDX_DEFINE_MEMBER_FACTORIES |
| 62 |
}; |
| 63 |
#if __GNUC__ >= 13 |
| 64 |
constexpr array_base_count_type() = default; // requires fix for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98423 (>=gcc-13) |
| 65 |
#else |
| 66 |
constexpr array_base_count_type() : |
| 67 |
count{} |
| 68 |
{ |
| 69 |
} |
| 70 |
#endif |
| 71 |
public: |
| 72 |
count_type get_count() const |
| 73 |
{ |
| 74 |
return count; |
| 75 |
} |
| 76 |
void set_count(const count_type c) |
| 77 |
{ |
| 78 |
count = c; |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
template < |
| 83 |
typename INTEGRAL_TYPE, |
| 84 |
std::size_t array_size_value, |
| 85 |
valptridx_detail::untyped_utilities::report_error_style report_const_error_value, |
| 86 |
valptridx_detail::untyped_utilities::report_error_style report_mutable_error_value |
| 87 |
> |
| 88 |
constexpr std::integral_constant<std::size_t, array_size_value> valptridx_detail::specialized_type_parameters<INTEGRAL_TYPE, array_size_value, report_const_error_value, report_mutable_error_value>::array_size; |
| 89 |
|
| 90 |
class valptridx_detail::untyped_utilities::report_error_undefined |
| 91 |
{ |
| 92 |
public: |
| 93 |
dxx_compiler_attribute_cold |
| 94 |
DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 95 |
static void report(...) |
| 96 |
{ |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
class valptridx_detail::untyped_utilities::report_error_trap_terse |
| 101 |
{ |
| 102 |
public: |
| 103 |
/* Accept and discard any arguments, to encourage the compiler to discard |
| 104 |
* as dead any values that exist only as arguments to `report()`. |
| 105 |
*/ |
| 106 |
[[noreturn]] |
| 107 |
dxx_compiler_attribute_cold |
| 108 |
DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 109 |
static void report(...) |
| 110 |
{ |
| 111 |
__builtin_trap(); |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
class valptridx_detail::untyped_utilities::index_mismatch_trap_verbose |
| 116 |
{ |
| 117 |
public: |
| 118 |
[[noreturn]] |
| 119 |
dxx_compiler_attribute_cold |
| 120 |
DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 121 |
static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const void *const array, const unsigned long supplied_index, const void *const expected_pointer, const void *const actual_pointer) |
| 122 |
{ |
| 123 |
/* Load each of the arguments into storage before executing the trap, |
| 124 |
* so that inspection of those locations in the core dump can readily |
| 125 |
* retrieve these values, even if the values would otherwise be unused |
| 126 |
* and optimized out. |
| 127 |
*/ |
| 128 |
__asm__ __volatile__("" :: DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_R_VARS "rm" (array), "rm" (supplied_index), "rm" (expected_pointer), "rm" (actual_pointer)); |
| 129 |
__builtin_trap(); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
class valptridx_detail::untyped_utilities::index_range_trap_verbose |
| 134 |
{ |
| 135 |
public: |
| 136 |
[[noreturn]] |
| 137 |
dxx_compiler_attribute_cold |
| 138 |
DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 139 |
static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const void *const array, const unsigned long supplied_index) |
| 140 |
{ |
| 141 |
__asm__ __volatile__("" :: DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_R_VARS "rm" (array), "rm" (supplied_index)); |
| 142 |
__builtin_trap(); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
class valptridx_detail::untyped_utilities::null_pointer_trap_verbose |
| 147 |
{ |
| 148 |
public: |
| 149 |
[[noreturn]] |
| 150 |
dxx_compiler_attribute_cold |
| 151 |
DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 152 |
static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DEFN_VARS) |
| 153 |
{ |
| 154 |
__asm__ __volatile__("" :: DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_N_VARS); |
| 155 |
__builtin_trap(); |
| 156 |
} |
| 157 |
[[noreturn]] |
| 158 |
dxx_compiler_attribute_cold |
| 159 |
DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 160 |
static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const void *const array) |
| 161 |
{ |
| 162 |
__asm__ __volatile__("" :: DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_R_VARS "rm" (array)); |
| 163 |
__builtin_trap(); |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
template <typename P> |
| 168 |
class valptridx<P>::index_mismatch_exception : |
| 169 |
public std::logic_error |
| 170 |
{ |
| 171 |
using std::logic_error::logic_error; |
| 172 |
public: |
| 173 |
[[noreturn]] |
| 174 |
dxx_compiler_attribute_cold |
| 175 |
DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 176 |
static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const array_managed_type *, index_type, const_pointer_type, const_pointer_type); |
| 177 |
}; |
| 178 |
|
| 179 |
template <typename P> |
| 180 |
class valptridx<P>::index_range_exception : |
| 181 |
public std::out_of_range |
| 182 |
{ |
| 183 |
using std::out_of_range::out_of_range; |
| 184 |
public: |
| 185 |
[[noreturn]] |
| 186 |
dxx_compiler_attribute_cold |
| 187 |
DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 188 |
static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const array_managed_type *, long); |
| 189 |
}; |
| 190 |
|
| 191 |
template <typename P> |
| 192 |
class valptridx<P>::null_pointer_exception : |
| 193 |
public std::logic_error |
| 194 |
{ |
| 195 |
using std::logic_error::logic_error; |
| 196 |
public: |
| 197 |
[[noreturn]] |
| 198 |
dxx_compiler_attribute_cold |
| 199 |
DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 200 |
static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DEFN_VARS); |
| 201 |
[[noreturn]] |
| 202 |
dxx_compiler_attribute_cold |
| 203 |
DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT |
| 204 |
static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const array_managed_type *); |
| 205 |
}; |
| 206 |
|
| 207 |
template <typename managed_type> |
| 208 |
template <typename handle_index_mismatch> |
| 209 |
void valptridx<managed_type>::check_index_match(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_reference_type r, index_type i, const array_managed_type &a dxx_compiler_attribute_unused) |
| 210 |
{ |
| 211 |
const auto pi = &a[i]; |
| 212 |
DXX_VALPTRIDX_CHECK(pi == &r, handle_index_mismatch, &a, i, pi, &r); |
| 213 |
} |
| 214 |
|
| 215 |
template <typename managed_type> |
| 216 |
template <typename handle_index_range_error, template <typename> class Compare> |
| 217 |
typename valptridx<managed_type>::index_type valptridx<managed_type>::check_index_range(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const index_type i, const array_managed_type *const a) |
| 218 |
{ |
| 219 |
std::size_t si; |
| 220 |
if constexpr (std::is_enum<index_type>::value) |
| 221 |
si = static_cast<std::size_t>(i); |
| 222 |
else |
| 223 |
si = i; |
| 224 |
check_index_range_size<handle_index_range_error, Compare>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS si, a); |
| 225 |
return i; |
| 226 |
} |
| 227 |
|
| 228 |
template <typename managed_type> |
| 229 |
template <typename handle_index_range_error, template <typename> class Compare> |
| 230 |
typename valptridx<managed_type>::index_type valptridx<managed_type>::check_index_range_size(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const std::size_t s, const array_managed_type *const a) |
| 231 |
{ |
| 232 |
DXX_VALPTRIDX_CHECK(Compare<std::size_t>()(s, array_size), handle_index_range_error, a, s); |
| 233 |
/* This cast will truncate the value to fit in index_type, which is |
| 234 |
* normally smaller than std::size_t. However, truncation is legal |
| 235 |
* here, because (1) DXX_VALPTRIDX_CHECK would trap on an index that |
| 236 |
* was outside the array size and (2) index_type can represent any |
| 237 |
* valid size in the array. Thus, if DXX_VALPTRIDX_CHECK did not |
| 238 |
* trap[1], the truncation cannot change the value of the index. |
| 239 |
* |
| 240 |
* [1] If valptridx was built without index validation, then no trap |
| 241 |
* would be issued even for invalid data. Validation-disabled builds |
| 242 |
* are permitted to exhibit undefined behavior in cases where the |
| 243 |
* validation-enabled build would have trapped. |
| 244 |
*/ |
| 245 |
return static_cast<index_type>(s); |
| 246 |
} |
| 247 |
|
| 248 |
template <typename managed_type> |
| 249 |
template <typename handle_null_pointer> |
| 250 |
void valptridx<managed_type>::check_null_pointer_conversion(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_pointer_type p) |
| 251 |
{ |
| 252 |
DXX_VALPTRIDX_CHECK(p, handle_null_pointer); |
| 253 |
} |
| 254 |
|
| 255 |
template <typename managed_type> |
| 256 |
template <typename handle_null_pointer> |
| 257 |
void valptridx<managed_type>::check_null_pointer(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_pointer_type p, const array_managed_type &a dxx_compiler_attribute_unused) |
| 258 |
{ |
| 259 |
DXX_VALPTRIDX_CHECK(p, handle_null_pointer, &a); |
| 260 |
} |
| 261 |
|
| 262 |
template <typename managed_type> |
| 263 |
template <typename handle_index_mismatch, typename handle_index_range_error> |
| 264 |
void valptridx<managed_type>::check_implicit_index_range_ref(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const managed_type &r, const array_managed_type &a) |
| 265 |
{ |
| 266 |
check_explicit_index_range_ref<handle_index_mismatch, handle_index_range_error>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS r, static_cast<const_pointer_type>(&r) - static_cast<const_pointer_type>(&a.front()), a); |
| 267 |
} |
| 268 |
|
| 269 |
template <typename managed_type> |
| 270 |
template <typename handle_index_mismatch, typename handle_index_range_error> |
| 271 |
void valptridx<managed_type>::check_explicit_index_range_ref(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_reference_type &r, std::size_t i, const array_managed_type &a) |
| 272 |
{ |
| 273 |
const auto ii = check_index_range_size<handle_index_range_error>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, &a); |
| 274 |
check_index_match<handle_index_mismatch>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS r, ii, a); |
| 275 |
} |
| 276 |
|
| 277 |
template <typename managed_type> |
| 278 |
class valptridx<managed_type>::partial_policy::require_valid |
| 279 |
{ |
| 280 |
public: |
| 281 |
static constexpr bool allow_nullptr{false}; |
| 282 |
[[nodiscard]] |
| 283 |
static constexpr std::false_type check_allowed_invalid_index(index_type) { return {}; } |
| 284 |
template <typename input_index_type> |
| 285 |
[[nodiscard]] |
| 286 |
static constexpr std::optional<index_type> check_nothrow_index(input_index_type entry_index) |
| 287 |
#if !defined(__clang_major__) || (__clang_major__ >= 16) |
| 288 |
/* <clang-16 fails to handle the second clause correctly: |
| 289 |
``` |
| 290 |
common/include/valptridx.h:281:46: note: candidate template ignored: substitution failure [with input_index_type = dcx::wallnum_t]: invalid operands to binary expression ('std::numeric_limits<dcx::wallnum_t>::type' (aka 'dcx::wallnum_t') and 'std::numeric_limits<unsigned short>::type' (aka 'unsigned short')) |
| 291 |
``` |
| 292 |
* A `requires` expression should be short-circuited, so since |
| 293 |
* `std::unsigned_integral<input_index_type>` is not satisfied for |
| 294 |
* `input_index_type` of `dcx::wallnum_t`, the second expression |
| 295 |
* should never have been examined. |
| 296 |
* |
| 297 |
* gcc (all supported versions) and clang-16 get this right. When |
| 298 |
* clang-15 is no longer supported, the preprocessor guard can be |
| 299 |
* removed. |
| 300 |
*/ |
| 301 |
requires( |
| 302 |
std::same_as<index_type, input_index_type> || |
| 303 |
/* Require that the input_index_type can represent all values |
| 304 |
* that the underlying `index_type` can represent. |
| 305 |
*/ |
| 306 |
(std::unsigned_integral<input_index_type> && std::numeric_limits<input_index_type>::max() >= std::numeric_limits<integral_type>::max()) |
| 307 |
) |
| 308 |
#endif |
| 309 |
{ |
| 310 |
return std::less<std::size_t>()(static_cast<std::size_t>(entry_index), array_size) |
| 311 |
/* This static_cast may truncate the type to a smaller size |
| 312 |
* (such as `uint32_t` -> `uint16_t`), but will not change the |
| 313 |
* value, since `entry_index` is less than array_size, and |
| 314 |
* array_size must be representable in `index_type`. |
| 315 |
*/ |
| 316 |
? std::optional<index_type>(static_cast<index_type>(entry_index)) |
| 317 |
: std::nullopt; |
| 318 |
} |
| 319 |
}; |
| 320 |
|
| 321 |
template <typename managed_type> |
| 322 |
class valptridx<managed_type>::partial_policy::allow_invalid |
| 323 |
{ |
| 324 |
public: |
| 325 |
static constexpr bool allow_nullptr{true}; |
| 326 |
[[nodiscard]] |
| 327 |
static constexpr auto check_allowed_invalid_index(const index_type i) |
| 328 |
{ |
| 329 |
return i == index_type{std::numeric_limits<integral_type>::max()}; |
| 330 |
} |
| 331 |
template <typename input_index_type> |
| 332 |
[[nodiscard]] |
| 333 |
static constexpr std::optional<index_type> check_nothrow_index(const input_index_type entry_index) |
| 334 |
{ |
| 335 |
return check_allowed_invalid_index(entry_index) |
| 336 |
? std::optional<index_type>(static_cast<index_type>(entry_index)) |
| 337 |
: require_valid::check_nothrow_index(entry_index); |
| 338 |
} |
| 339 |
}; |
| 340 |
|
| 341 |
template <typename managed_type> |
| 342 |
template <template <typename> class policy> |
| 343 |
class valptridx<managed_type>::partial_policy::apply_cv_policy |
| 344 |
{ |
| 345 |
template <typename T> |
| 346 |
using apply_cv_qualifier = typename policy<T>::type; |
| 347 |
public: |
| 348 |
using array_managed_type = apply_cv_qualifier<valptridx<managed_type>::array_managed_type>; |
| 349 |
using pointer = apply_cv_qualifier<managed_type> *; |
| 350 |
using reference = apply_cv_qualifier<managed_type> &; |
| 351 |
}; |
| 352 |
|
| 353 |
template <typename managed_type> |
| 354 |
class valptridx<managed_type>::vc : |
| 355 |
public partial_policy::require_valid, |
| 356 |
public partial_policy::template apply_cv_policy<std::add_const> |
| 357 |
{ |
| 358 |
}; |
| 359 |
|
| 360 |
template <typename managed_type> |
| 361 |
class valptridx<managed_type>::vm : |
| 362 |
public partial_policy::require_valid, |
| 363 |
public partial_policy::template apply_cv_policy<std::remove_const> |
| 364 |
{ |
| 365 |
}; |
| 366 |
|
| 367 |
template <typename managed_type> |
| 368 |
class valptridx<managed_type>::ic : |
| 369 |
public partial_policy::allow_invalid, |
| 370 |
public partial_policy::template apply_cv_policy<std::add_const> |
| 371 |
{ |
| 372 |
}; |
| 373 |
|
| 374 |
template <typename managed_type> |
| 375 |
class valptridx<managed_type>::im : |
| 376 |
public partial_policy::allow_invalid, |
| 377 |
public partial_policy::template apply_cv_policy<std::remove_const> |
| 378 |
{ |
| 379 |
}; |
| 380 |
|
| 381 |
template <typename managed_type> |
| 382 |
template <typename policy> |
| 383 |
class valptridx<managed_type>::idx : |
| 384 |
public policy |
| 385 |
{ |
| 386 |
using containing_type = valptridx<managed_type>; |
| 387 |
public: |
| 388 |
using policy::allow_nullptr; |
| 389 |
using policy::check_allowed_invalid_index; |
| 390 |
using index_type = typename containing_type::index_type; |
| 391 |
using integral_type = typename containing_type::integral_type; |
| 392 |
using typename policy::array_managed_type; |
| 393 |
idx() = delete; |
| 394 |
idx(const idx &) = default; |
| 395 |
idx(idx &&) = default; |
| 396 |
idx &operator=(const idx &) & = default; |
| 397 |
idx &operator=(idx &&) & = default; |
| 398 |
idx &operator=(const idx &) && = delete; |
| 399 |
idx &operator=(idx &&) && = delete; |
| 400 |
|
| 401 |
index_type get_unchecked_index() const { return m_idx; } |
| 402 |
|
| 403 |
/* If moving from allow_invalid to allow_invalid, no check is |
| 404 |
* needed. |
| 405 |
* If moving from require_valid to anything, no check is needed. |
| 406 |
*/ |
| 407 |
template <typename rpolicy> |
| 408 |
requires( |
| 409 |
allow_nullptr || !idx<rpolicy>::allow_nullptr |
| 410 |
) |
| 411 |
idx(const idx<rpolicy> &rhs) : |
| 412 |
m_idx{rhs.get_unchecked_index()} |
| 413 |
{ |
| 414 |
} |
| 415 |
template <typename rpolicy> |
| 416 |
requires( |
| 417 |
!(allow_nullptr || !idx<rpolicy>::allow_nullptr) |
| 418 |
) |
| 419 |
idx(const idx<rpolicy> &rhs DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS) : |
| 420 |
/* If moving from allow_invalid to require_valid, check range. |
| 421 |
*/ |
| 422 |
m_idx{check_index_range<index_range_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS rhs.get_unchecked_index(), nullptr)} |
| 423 |
{ |
| 424 |
} |
| 425 |
template <typename rpolicy> |
| 426 |
idx(idx<rpolicy> &&rhs) : |
| 427 |
m_idx{rhs.get_unchecked_index()} |
| 428 |
{ |
| 429 |
/* Prevent move from allow_invalid into require_valid. The |
| 430 |
* right hand side must be saved and checked for validity before |
| 431 |
* being used to initialize a require_valid type. |
| 432 |
*/ |
| 433 |
static_assert(allow_nullptr || !idx<rpolicy>::allow_nullptr, "cannot move from allow_invalid to require_valid"); |
| 434 |
} |
| 435 |
idx(index_type i DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS) : |
| 436 |
m_idx{check_allowed_invalid_index(i) ? i : check_index_range<index_range_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, nullptr)} |
| 437 |
{ |
| 438 |
} |
| 439 |
idx(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS index_type i, array_managed_type &a) : |
| 440 |
m_idx{check_allowed_invalid_index(i) ? i : check_index_range<index_range_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, &a)} |
| 441 |
{ |
| 442 |
} |
| 443 |
protected: |
| 444 |
template <index_type v> |
| 445 |
idx(const magic_constant<v> &, allow_none_construction) : |
| 446 |
m_idx{v} |
| 447 |
{ |
| 448 |
/* Prevent using `allow_none_construction` when the type permits |
| 449 |
* use of an invalid index. `allow_none_construction` is intended |
| 450 |
* to override the validity requirement on types that require a |
| 451 |
* valid index, for the limited purpose of constructing a sentinel. |
| 452 |
*/ |
| 453 |
static_assert(!allow_nullptr, "allow_none_construction used where nullptr was already legal"); |
| 454 |
/* Prevent using `allow_none_construction` with a valid index. |
| 455 |
* Valid indexes should not use the `allow_none_construction` |
| 456 |
* override. |
| 457 |
*/ |
| 458 |
static_assert(static_cast<std::size_t>(v) >= array_size, "allow_none_construction used with valid index"); |
| 459 |
} |
| 460 |
idx(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS index_type i, array_managed_type &a, allow_end_construction) : |
| 461 |
m_idx{check_index_range<index_range_error_type<array_managed_type>, std::less_equal>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, &a)} |
| 462 |
{ |
| 463 |
} |
| 464 |
idx(index_type i, assume_nothrow_index) : |
| 465 |
m_idx{i} |
| 466 |
{ |
| 467 |
} |
| 468 |
idx(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS typename policy::pointer p, array_managed_type &a) : |
| 469 |
m_idx{check_index_range_size<index_range_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS p - &a.front(), &a)} |
| 470 |
{ |
| 471 |
} |
| 472 |
public: |
| 473 |
template <index_type v> |
| 474 |
constexpr idx(const magic_constant<v> &) : |
| 475 |
m_idx{v} |
| 476 |
{ |
| 477 |
/* If the policy requires a valid index (indicated by |
| 478 |
* `allow_nullptr == false`), then require that the magic index be |
| 479 |
* valid. |
| 480 |
*/ |
| 481 |
static_assert(allow_nullptr || static_cast<std::size_t>(v) < array_size, "invalid magic index not allowed for this policy"); |
| 482 |
} |
| 483 |
template <typename rpolicy> |
| 484 |
constexpr bool operator==(const idx<rpolicy> &rhs) const |
| 485 |
{ |
| 486 |
return m_idx == rhs.get_unchecked_index(); |
| 487 |
} |
| 488 |
constexpr bool operator==(const index_type &i) const |
| 489 |
{ |
| 490 |
return m_idx == i; |
| 491 |
} |
| 492 |
template <index_type v> |
| 493 |
constexpr bool operator==(const magic_constant<v> &) const |
| 494 |
{ |
| 495 |
static_assert(allow_nullptr || static_cast<std::size_t>(v) < array_size, "invalid magic index not allowed for this policy"); |
| 496 |
return m_idx == v; |
| 497 |
} |
| 498 |
operator index_type() const |
| 499 |
{ |
| 500 |
return m_idx; |
| 501 |
} |
| 502 |
protected: |
| 503 |
index_type m_idx; |
| 504 |
idx &operator++() |
| 505 |
{ |
| 506 |
if constexpr (std::is_enum<index_type>::value) |
| 507 |
m_idx = static_cast<index_type>(1u + static_cast<typename std::underlying_type<index_type>::type>(m_idx)); |
| 508 |
else |
| 509 |
++ m_idx; |
| 510 |
return *this; |
| 511 |
} |
| 512 |
idx &operator--() |
| 513 |
{ |
| 514 |
if constexpr (std::is_enum<index_type>::value) |
| 515 |
m_idx = static_cast<index_type>(static_cast<typename std::underlying_type<index_type>::type>(m_idx) - 1u); |
| 516 |
else |
| 517 |
-- m_idx; |
| 518 |
return *this; |
| 519 |
} |
| 520 |
}; |
| 521 |
|
| 522 |
template <typename managed_type> |
| 523 |
template <typename policy> |
| 524 |
class valptridx<managed_type>::ptr : |
| 525 |
public policy |
| 526 |
{ |
| 527 |
using containing_type = valptridx<managed_type>; |
| 528 |
public: |
| 529 |
using policy::allow_nullptr; |
| 530 |
using policy::check_allowed_invalid_index; |
| 531 |
using index_type = typename containing_type::index_type; |
| 532 |
using const_pointer_type = typename containing_type::const_pointer_type; |
| 533 |
using mutable_pointer_type = typename containing_type::mutable_pointer_type; |
| 534 |
using allow_none_construction = typename containing_type::allow_none_construction; |
| 535 |
using typename policy::array_managed_type; |
| 536 |
using typename policy::pointer; |
| 537 |
using typename policy::reference; |
| 538 |
|
| 539 |
ptr() = delete; |
| 540 |
/* Override template matches to make same-type copy/move trivial */ |
| 541 |
ptr(const ptr &) = default; |
| 542 |
ptr(ptr &&) = default; |
| 543 |
ptr &operator=(const ptr &) & = default; |
| 544 |
ptr &operator=(ptr &&) & = default; |
| 545 |
ptr &operator=(const ptr &) && = delete; |
| 546 |
ptr &operator=(ptr &&) && = delete; |
| 547 |
|
| 548 |
pointer get_unchecked_pointer() const { return m_ptr; } |
| 549 |
pointer get_nonnull_pointer(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DECL_VARS) const |
| 550 |
{ |
| 551 |
/* If !allow_nullptr, assume nullptr was caught at construction. */ |
| 552 |
const auto p{m_ptr}; |
| 553 |
if constexpr (allow_nullptr) |
| 554 |
check_null_pointer_conversion<null_pointer_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS p); |
| 555 |
else |
| 556 |
DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_VOID_VARS(); |
| 557 |
return p; |
| 558 |
} |
| 559 |
reference get_checked_reference(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DECL_VARS) const |
| 560 |
{ |
| 561 |
return *get_nonnull_pointer(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VA()); |
| 562 |
} |
| 563 |
|
| 564 |
ptr(std::nullptr_t) |
| 565 |
requires(policy::allow_nullptr) // This constructor always stores nullptr, so require a policy with `allow_nullptr == true`. |
| 566 |
: |
| 567 |
m_ptr{nullptr} |
| 568 |
{ |
| 569 |
} |
| 570 |
template <index_type v> |
| 571 |
requires( |
| 572 |
static_cast<std::size_t>(v) >= array_size // Require that the index be invalid, since a valid index should compute a non-nullptr here, but with no array, no pointer can be computed. |
| 573 |
) |
| 574 |
ptr(const magic_constant<v> &) : |
| 575 |
ptr{nullptr} |
| 576 |
{ |
| 577 |
} |
| 578 |
template <index_type v> |
| 579 |
requires( |
| 580 |
static_cast<std::size_t>(v) < array_size // valid magic index required when using array |
| 581 |
) |
| 582 |
ptr(const magic_constant<v> &, array_managed_type &a) : |
| 583 |
m_ptr{&a[v]} |
| 584 |
{ |
| 585 |
} |
| 586 |
template <typename rpolicy> |
| 587 |
requires( |
| 588 |
(std::convertible_to<typename ptr<rpolicy>::pointer, pointer>) && |
| 589 |
(policy::allow_nullptr || !ptr<rpolicy>::allow_nullptr) |
| 590 |
) |
| 591 |
ptr(const ptr<rpolicy> &rhs) : |
| 592 |
m_ptr{rhs.get_unchecked_pointer()} |
| 593 |
{ |
| 594 |
} |
| 595 |
template <typename rpolicy> |
| 596 |
requires( |
| 597 |
(std::convertible_to<typename ptr<rpolicy>::pointer, pointer>) && |
| 598 |
!(policy::allow_nullptr || !ptr<rpolicy>::allow_nullptr) |
| 599 |
) |
| 600 |
ptr(const ptr<rpolicy> &rhs DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS) : |
| 601 |
m_ptr{rhs.get_unchecked_pointer()} |
| 602 |
{ |
| 603 |
check_null_pointer_conversion<null_pointer_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS m_ptr); |
| 604 |
} |
| 605 |
template <typename rpolicy> |
| 606 |
requires( |
| 607 |
(std::convertible_to<typename ptr<rpolicy>::pointer, pointer>) && |
| 608 |
(policy::allow_nullptr || !ptr<rpolicy>::allow_nullptr) // cannot move from allow_invalid to require_valid |
| 609 |
) |
| 610 |
ptr(ptr<rpolicy> &&rhs) : |
| 611 |
m_ptr{rhs.get_unchecked_pointer()} |
| 612 |
{ |
| 613 |
/* Prevent move from allow_invalid into require_valid. The |
| 614 |
* right hand side must be saved and checked for validity before |
| 615 |
* being used to initialize a require_valid type. |
| 616 |
*/ |
| 617 |
} |
| 618 |
ptr(index_type i) = delete; |
| 619 |
ptr(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS index_type i, array_managed_type &a) : |
| 620 |
m_ptr{check_allowed_invalid_index(i) ? nullptr : &a[check_index_range<index_range_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, &a)]} |
| 621 |
{ |
| 622 |
} |
| 623 |
ptr(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS index_type i, array_managed_type &a, allow_end_construction) : |
| 624 |
m_ptr{std::next(a.begin(), static_cast<std::size_t>(check_index_range<index_range_error_type<array_managed_type>, std::less_equal>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, &a)))} |
| 625 |
{ |
| 626 |
} |
| 627 |
ptr(index_type i, array_managed_type &a, assume_nothrow_index) : |
| 628 |
m_ptr{&a[i]} |
| 629 |
{ |
| 630 |
} |
| 631 |
ptr(pointer p) = delete; |
| 632 |
ptr(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS pointer p, array_managed_type &a) : |
| 633 |
/* No array consistency check here, since some code incorrectly |
| 634 |
* defines instances of `object` outside the Objects array, then |
| 635 |
* passes pointers to those instances to this function. |
| 636 |
*/ |
| 637 |
m_ptr{p} |
| 638 |
{ |
| 639 |
if constexpr (!allow_nullptr) |
| 640 |
check_null_pointer<null_pointer_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS p, a); |
| 641 |
} |
| 642 |
ptr(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS reference r, array_managed_type &a) : |
| 643 |
m_ptr{(check_implicit_index_range_ref<index_mismatch_error_type<array_managed_type>, index_range_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS r, a), &r)} |
| 644 |
{ |
| 645 |
} |
| 646 |
ptr(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS reference r, index_type i, array_managed_type &a) : |
| 647 |
m_ptr{(check_explicit_index_range_ref<index_mismatch_error_type<array_managed_type>, index_range_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS r, i, a), &r)} |
| 648 |
{ |
| 649 |
} |
| 650 |
|
| 651 |
operator mutable_pointer_type() const { return m_ptr; } // implicit pointer conversion deprecated |
| 652 |
operator const_pointer_type() const { return m_ptr; } // implicit pointer conversion deprecated |
| 653 |
template <typename rpolicy> |
| 654 |
requires(!std::is_same<policy, rpolicy>::value) |
| 655 |
ptr rebind_policy(ptr<rpolicy> &&rhs) const |
| 656 |
{ |
| 657 |
/* This method could be marked as `static`, but is non-static so |
| 658 |
* that callers must possess an instance of the target type. |
| 659 |
* This serves as a basic check against casts that could remove |
| 660 |
* `const` incorrectly. |
| 661 |
*/ |
| 662 |
return ptr(std::move(rhs), typename containing_type::rebind_policy{}); |
| 663 |
} |
| 664 |
pointer operator->() const & |
| 665 |
{ |
| 666 |
return get_nonnull_pointer(); |
| 667 |
} |
| 668 |
operator reference() const & |
| 669 |
{ |
| 670 |
return get_checked_reference(); |
| 671 |
} |
| 672 |
reference operator*() const & |
| 673 |
{ |
| 674 |
return get_checked_reference(); |
| 675 |
} |
| 676 |
explicit operator bool() const & |
| 677 |
{ |
| 678 |
return !(*this == nullptr); |
| 679 |
} |
| 680 |
pointer operator->() const && |
| 681 |
{ |
| 682 |
static_assert(!allow_nullptr, "operator-> not allowed with allow_invalid policy"); |
| 683 |
return operator->(); |
| 684 |
} |
| 685 |
operator reference() const && |
| 686 |
{ |
| 687 |
static_assert(!allow_nullptr, "implicit reference not allowed with allow_invalid policy"); |
| 688 |
return *this; |
| 689 |
} |
| 690 |
reference operator*() const && |
| 691 |
{ |
| 692 |
static_assert(!allow_nullptr, "operator* not allowed with allow_invalid policy"); |
| 693 |
return *this; |
| 694 |
} |
| 695 |
explicit operator bool() const && = delete; |
| 696 |
constexpr bool operator==(std::nullptr_t) const |
| 697 |
{ |
| 698 |
static_assert(allow_nullptr, "nullptr comparison not allowed: value is never null"); |
| 699 |
return m_ptr == nullptr; |
| 700 |
} |
| 701 |
constexpr bool operator==(const_pointer_type p) const |
| 702 |
{ |
| 703 |
return m_ptr == p; |
| 704 |
} |
| 705 |
constexpr bool operator==(mutable_pointer_type p) const |
| 706 |
{ |
| 707 |
return m_ptr == p; |
| 708 |
} |
| 709 |
template <typename rpolicy> |
| 710 |
constexpr bool operator==(const ptr<rpolicy> &rhs) const |
| 711 |
{ |
| 712 |
return *this == rhs.get_unchecked_pointer(); |
| 713 |
} |
| 714 |
long operator-(auto &&) const = delete; |
| 715 |
bool operator<(auto &&) const = delete; |
| 716 |
bool operator>(auto &&) const = delete; |
| 717 |
bool operator<=(auto &&) const = delete; |
| 718 |
bool operator>=(auto &&) const = delete; |
| 719 |
protected: |
| 720 |
pointer m_ptr; |
| 721 |
ptr &operator++() |
| 722 |
{ |
| 723 |
++ m_ptr; |
| 724 |
return *this; |
| 725 |
} |
| 726 |
ptr &operator--() |
| 727 |
{ |
| 728 |
-- m_ptr; |
| 729 |
return *this; |
| 730 |
} |
| 731 |
ptr(allow_none_construction) |
| 732 |
requires( |
| 733 |
!policy::allow_nullptr // allow_none_construction is only needed where nullptr is not already legal |
| 734 |
) |
| 735 |
: |
| 736 |
m_ptr(nullptr) |
| 737 |
{ |
| 738 |
} |
| 739 |
template <typename rpolicy> |
| 740 |
requires( |
| 741 |
policy::allow_nullptr || !ptr<rpolicy>::allow_nullptr // cannot rebind from allow_invalid to require_valid |
| 742 |
) |
| 743 |
ptr(ptr<rpolicy> &&rhs, typename containing_type::rebind_policy) : |
| 744 |
m_ptr{const_cast<managed_type *>(rhs.get_unchecked_pointer())} |
| 745 |
{ |
| 746 |
} |
| 747 |
}; |
| 748 |
|
| 749 |
#if DXX_VALPTRIDX_ENFORCE_STRICT_PI_SEPARATION |
| 750 |
template <typename T> |
| 751 |
struct strong_typedef : T |
| 752 |
{ |
| 753 |
using T::T; |
| 754 |
template <typename O> |
| 755 |
requires(std::constructible_from<T, O &&>) |
| 756 |
/* This constructor is explicit if the requires expression is false. |
| 757 |
* - if std::constructible_from is not satisfied, this constructor is |
| 758 |
* disabled by the above `requires` constraint |
| 759 |
* - if std::constructible_from is satisfied and `explicit(true) T(O |
| 760 |
* &&)` exists, then this `requires` is an invalid expression |
| 761 |
* (because the conversion from `O &&` to `T` is implicit, and thus |
| 762 |
* cannot use the `explicit(true)` constructor), so this constructor |
| 763 |
* is `explicit(not false)` -> `explicit(true)`, matching the `T` |
| 764 |
* constructor |
| 765 |
* - if std::constructible_from is satisfied and `explicit(false) T(O |
| 766 |
* &&)` exists, then this `requires` is a valid expression, so this |
| 767 |
* constructor is `explicit(not true)` -> `explicit(false)` |
| 768 |
*/ |
| 769 |
explicit( |
| 770 |
not requires(void (*f)(T), O &&o) { |
| 771 |
{ (*f)(std::forward<O>(o)) }; |
| 772 |
} |
| 773 |
) |
| 774 |
strong_typedef(O &&o) : |
| 775 |
T{std::forward<O>(o)} |
| 776 |
{ |
| 777 |
} |
| 778 |
strong_typedef() = default; |
| 779 |
strong_typedef(const strong_typedef &) = default; |
| 780 |
strong_typedef(strong_typedef &&) = default; |
| 781 |
strong_typedef &operator=(const strong_typedef &) & = default; |
| 782 |
strong_typedef &operator=(strong_typedef &&) & = default; |
| 783 |
strong_typedef &operator=(const strong_typedef &) && = delete; |
| 784 |
strong_typedef &operator=(strong_typedef &&) && = delete; |
| 785 |
}; |
| 786 |
#endif |
| 787 |
|
| 788 |
template <typename managed_type> |
| 789 |
template <typename policy> |
| 790 |
class valptridx<managed_type>::ptridx : |
| 791 |
public prohibit_void_ptr<ptridx<policy>>, |
| 792 |
public ptr<policy>, |
| 793 |
public idx<policy> |
| 794 |
{ |
| 795 |
using containing_type = valptridx<managed_type>; |
| 796 |
public: |
| 797 |
using vptr_type = ptr<policy>; |
| 798 |
using vidx_type = idx<policy>; |
| 799 |
using typename vidx_type::array_managed_type; |
| 800 |
using typename vidx_type::index_type; |
| 801 |
using typename vidx_type::integral_type; |
| 802 |
using typename vptr_type::pointer; |
| 803 |
using vptr_type::allow_nullptr; |
| 804 |
using vidx_type::operator==; |
| 805 |
using vptr_type::operator==; |
| 806 |
ptridx(const ptridx &) = default; |
| 807 |
ptridx(ptridx &&) = default; |
| 808 |
ptridx &operator=(const ptridx &) & = default; |
| 809 |
ptridx &operator=(ptridx &&) & = default; |
| 810 |
ptridx &operator=(const ptridx &) && = delete; |
| 811 |
ptridx &operator=(ptridx &&) && = delete; |
| 812 |
ptridx(std::nullptr_t) = delete; |
| 813 |
/* Prevent implicit conversion. Require use of the factory function. |
| 814 |
*/ |
| 815 |
ptridx(pointer p) = delete; |
| 816 |
template <typename rpolicy> |
| 817 |
requires( |
| 818 |
allow_nullptr || !ptridx<rpolicy>::allow_nullptr |
| 819 |
) |
| 820 |
ptridx(const ptridx<rpolicy> &rhs) : |
| 821 |
vptr_type{static_cast<const typename ptridx<rpolicy>::vptr_type &>(rhs)}, |
| 822 |
vidx_type{static_cast<const typename ptridx<rpolicy>::vidx_type &>(rhs)} |
| 823 |
{ |
| 824 |
} |
| 825 |
template <typename rpolicy> |
| 826 |
requires( |
| 827 |
!(allow_nullptr || !ptridx<rpolicy>::allow_nullptr) |
| 828 |
) |
| 829 |
ptridx(const ptridx<rpolicy> &rhs DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS) : |
| 830 |
vptr_type{static_cast<const typename ptridx<rpolicy>::vptr_type &>(rhs) DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_PASS_VARS}, |
| 831 |
vidx_type{static_cast<const typename ptridx<rpolicy>::vidx_type &>(rhs) DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_PASS_VARS} |
| 832 |
{ |
| 833 |
} |
| 834 |
template <typename rpolicy> |
| 835 |
ptridx(ptridx<rpolicy> &&rhs) : |
| 836 |
vptr_type{static_cast<typename ptridx<rpolicy>::vptr_type &&>(rhs)}, |
| 837 |
vidx_type{static_cast<typename ptridx<rpolicy>::vidx_type &&>(rhs)} |
| 838 |
{ |
| 839 |
} |
| 840 |
template <index_type v> |
| 841 |
ptridx(const magic_constant<v> &m) : |
| 842 |
vptr_type{m}, |
| 843 |
vidx_type{m} |
| 844 |
{ |
| 845 |
} |
| 846 |
template <index_type v> |
| 847 |
ptridx(const magic_constant<v> &m, array_managed_type &a) : |
| 848 |
vptr_type{m, a}, |
| 849 |
vidx_type{m} |
| 850 |
{ |
| 851 |
} |
| 852 |
template <index_type v> |
| 853 |
ptridx(const magic_constant<v> &m, const allow_none_construction n) : |
| 854 |
vptr_type{n}, |
| 855 |
vidx_type{m, n} |
| 856 |
{ |
| 857 |
} |
| 858 |
ptridx(index_type i) = delete; |
| 859 |
ptridx(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS index_type i, array_managed_type &a) : |
| 860 |
vptr_type{DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, a}, |
| 861 |
vidx_type{DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, a} |
| 862 |
{ |
| 863 |
} |
| 864 |
ptridx(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS index_type i, array_managed_type &a, const allow_end_construction e) : |
| 865 |
vptr_type{DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, a, e}, |
| 866 |
vidx_type{DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, a, e} |
| 867 |
{ |
| 868 |
} |
| 869 |
ptridx(index_type i, array_managed_type &a, const assume_nothrow_index e) : |
| 870 |
vptr_type{i, a, e}, |
| 871 |
vidx_type{i, e} |
| 872 |
{ |
| 873 |
} |
| 874 |
ptridx(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS pointer p, array_managed_type &a) : |
| 875 |
/* Null pointer is never allowed when an index must be computed. |
| 876 |
* Check for null, then use the reference constructor for |
| 877 |
* vptr_type to avoid checking again. |
| 878 |
*/ |
| 879 |
vptr_type{DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS (check_null_pointer<null_pointer_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS p, a), *p), a}, |
| 880 |
vidx_type{DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS p, a} |
| 881 |
{ |
| 882 |
} |
| 883 |
ptridx(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS pointer p, index_type i, array_managed_type &a) : |
| 884 |
vptr_type{DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS (check_null_pointer<null_pointer_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS p, a), *p), i, a}, |
| 885 |
vidx_type{DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, a} |
| 886 |
{ |
| 887 |
} |
| 888 |
template <typename rpolicy> |
| 889 |
requires(!std::is_same<policy, rpolicy>::value) |
| 890 |
ptridx rebind_policy(ptridx<rpolicy> &&rhs) const |
| 891 |
{ |
| 892 |
return ptridx(std::move(rhs), typename containing_type::rebind_policy{}); |
| 893 |
} |
| 894 |
ptridx absolute_sibling(const index_type i DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS) const |
| 895 |
{ |
| 896 |
static_assert(!allow_nullptr, "absolute_sibling not allowed with invalid ptridx"); |
| 897 |
ptridx r(*this); |
| 898 |
r.m_ptr += check_index_range<index_range_error_type<array_managed_type>>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, nullptr) - this->m_idx; |
| 899 |
r.m_idx = i; |
| 900 |
return r; |
| 901 |
} |
| 902 |
template <typename rpolicy> |
| 903 |
constexpr bool operator==(const ptridx<rpolicy> &rhs) const |
| 904 |
{ |
| 905 |
return vptr_type::operator==(static_cast<const typename ptridx<rpolicy>::vptr_type &>(rhs)); |
| 906 |
} |
| 907 |
protected: |
| 908 |
ptridx &operator++() |
| 909 |
{ |
| 910 |
vptr_type::operator++(); |
| 911 |
vidx_type::operator++(); |
| 912 |
return *this; |
| 913 |
} |
| 914 |
ptridx &operator--() |
| 915 |
{ |
| 916 |
vptr_type::operator--(); |
| 917 |
vidx_type::operator--(); |
| 918 |
return *this; |
| 919 |
} |
| 920 |
template <typename rpolicy> |
| 921 |
ptridx(ptridx<rpolicy> &&rhs, const typename containing_type::rebind_policy rebind) : |
| 922 |
vptr_type{static_cast<typename ptridx<rpolicy>::vptr_type &&>(rhs), rebind}, |
| 923 |
vidx_type{static_cast<typename ptridx<rpolicy>::vidx_type &&>(rhs)} |
| 924 |
{ |
| 925 |
/* No static_assert for policy compatibility. Incompatible |
| 926 |
* policy conversions will be trapped by the static_assert in |
| 927 |
* `vptr_type`. |
| 928 |
*/ |
| 929 |
} |
| 930 |
}; |
| 931 |
|
| 932 |
template <typename managed_type> |
| 933 |
template <typename guarded_type> |
| 934 |
class valptridx<managed_type>::guarded |
| 935 |
{ |
| 936 |
static_assert(std::is_trivially_destructible<guarded_type>::value, "non-trivial destructor found for guarded_type"); |
| 937 |
enum state : uint8_t |
| 938 |
{ |
| 939 |
/* empty - the untrusted input was invalid, so no guarded_type |
| 940 |
* exists |
| 941 |
*/ |
| 942 |
empty, |
| 943 |
/* initialized - the untrusted input was valid, so a |
| 944 |
* guarded_type type exists, but the calling code has not yet |
| 945 |
* tested the state of this guarded<P> |
| 946 |
*/ |
| 947 |
initialized, |
| 948 |
/* checked - the untrusted input was valid, and the calling code |
| 949 |
* has called operator bool() |
| 950 |
*/ |
| 951 |
checked, |
| 952 |
}; |
| 953 |
union { |
| 954 |
state m_dummy; |
| 955 |
guarded_type m_value; |
| 956 |
}; |
| 957 |
mutable state m_state; |
| 958 |
public: |
| 959 |
dxx_compiler_attribute_cold |
| 960 |
guarded(std::nullptr_t) : |
| 961 |
m_dummy{}, m_state{empty} |
| 962 |
{ |
| 963 |
} |
| 964 |
guarded(guarded_type &&v) : |
| 965 |
m_value{std::move(v)}, m_state{initialized} |
| 966 |
{ |
| 967 |
} |
| 968 |
[[nodiscard]] |
| 969 |
explicit operator bool() const |
| 970 |
{ |
| 971 |
/* |
| 972 |
* If no contained guarded_type exists, return false. |
| 973 |
* Otherwise, record that the result has been tested and then |
| 974 |
* return true. operator*() uses m_state to enforce that the |
| 975 |
* result is tested. |
| 976 |
*/ |
| 977 |
if (unlikely(m_state == empty)) |
| 978 |
return false; |
| 979 |
m_state = checked; |
| 980 |
return true; |
| 981 |
} |
| 982 |
[[nodiscard]] |
| 983 |
guarded_type operator*() const & |
| 984 |
{ |
| 985 |
/* |
| 986 |
* Correct code will always execute as if this method was just |
| 987 |
* the return statement, with none of the sanity checks. The |
| 988 |
* checks are present to catch misuse of this type, preferably |
| 989 |
* at compile-time, but at least at runtime. |
| 990 |
*/ |
| 991 |
#define DXX_VALPTRIDX_GUARDED_OBJECT_NO "access to guarded object that does not exist" |
| 992 |
#define DXX_VALPTRIDX_GUARDED_OBJECT_MAYBE "access to guarded object that may not exist" |
| 993 |
#ifdef DXX_CONSTANT_TRUE |
| 994 |
{ |
| 995 |
/* |
| 996 |
* If the validity has not been verified by the caller, then |
| 997 |
* fail. Choose an error message and function name |
| 998 |
* based on whether the contained type provably does not |
| 999 |
* exist. It provably does not exist if this call is on a |
| 1000 |
* path where operator bool() returned false. It |
| 1001 |
* conditionally might not exist if this call is on a path |
| 1002 |
* where operator bool() has not been called. |
| 1003 |
*/ |
| 1004 |
if (DXX_CONSTANT_TRUE(m_state == empty)) |
| 1005 |
DXX_ALWAYS_ERROR_FUNCTION(DXX_VALPTRIDX_GUARDED_OBJECT_NO); |
| 1006 |
/* If the contained object might not exist: */ |
| 1007 |
if (DXX_CONSTANT_TRUE(m_state == initialized)) |
| 1008 |
DXX_ALWAYS_ERROR_FUNCTION(DXX_VALPTRIDX_GUARDED_OBJECT_MAYBE); |
| 1009 |
} |
| 1010 |
#endif |
| 1011 |
/* |
| 1012 |
* If the compiler does not offer constant truth analysis |
| 1013 |
* (perhaps because of insufficient optimization), then emit a |
| 1014 |
* runtime check for whether the guarded_type exists. |
| 1015 |
* |
| 1016 |
* This test can throw even if the contained object is valid, if |
| 1017 |
* the caller did not first validate that the contained object |
| 1018 |
* is valid. This restriction is necessary since inputs are |
| 1019 |
* usually valid even when untested, so throwing only on state |
| 1020 |
* `empty` would allow incorrect usage to persist in the code |
| 1021 |
* until someone happened to receive an invalid input from an |
| 1022 |
* untrusted source. |
| 1023 |
*/ |
| 1024 |
if (m_state != checked) |
| 1025 |
throw std::logic_error(m_state == empty ? DXX_VALPTRIDX_GUARDED_OBJECT_NO : DXX_VALPTRIDX_GUARDED_OBJECT_MAYBE); |
| 1026 |
#undef DXX_VALPTRIDX_GUARDED_OBJECT_MAYBE |
| 1027 |
#undef DXX_VALPTRIDX_GUARDED_OBJECT_NO |
| 1028 |
return m_value; |
| 1029 |
} |
| 1030 |
guarded_type operator*() const && = delete; |
| 1031 |
}; |
| 1032 |
|
| 1033 |
template <typename managed_type> |
| 1034 |
class valptridx<managed_type>::array_managed_type : |
| 1035 |
public array_base_count_type, |
| 1036 |
public array_base_storage_type |
| 1037 |
{ |
| 1038 |
public: |
| 1039 |
/* |
| 1040 |
* Expose the union members that act as factory methods. Leave the |
| 1041 |
* `count` union member protected. |
| 1042 |
*/ |
| 1043 |
#define DXX_VALPTRIDX_ACCESS_SUBTYPE_MEMBER_FACTORIES(MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, IVPREFIX, MCPREFIX) \ |
| 1044 |
using array_base_count_type::IVPREFIX ## MCPREFIX ## PISUFFIX |
| 1045 |
DXX_VALPTRIDX_FOR_EACH_PPI_TYPE(DXX_VALPTRIDX_ACCESS_SUBTYPE_MEMBER_FACTORIES,,,); |
| 1046 |
#undef DXX_VALPTRIDX_ACCESS_SUBTYPE_MEMBER_FACTORIES |
| 1047 |
using typename array_base_storage_type::reference; |
| 1048 |
using typename array_base_storage_type::const_reference; |
| 1049 |
reference operator[](const index_type n) |
| 1050 |
{ |
| 1051 |
return array_base_storage_type::operator[](n); |
| 1052 |
} |
| 1053 |
const_reference operator[](const index_type n) const |
| 1054 |
{ |
| 1055 |
return array_base_storage_type::operator[](n); |
| 1056 |
} |
| 1057 |
reference operator[](auto) const = delete; |
| 1058 |
#if DXX_HAVE_POISON_UNDEFINED |
| 1059 |
array_managed_type(); |
| 1060 |
#else |
| 1061 |
array_managed_type() = default; |
| 1062 |
#endif |
| 1063 |
array_managed_type(const array_managed_type &) = delete; |
| 1064 |
array_managed_type &operator=(const array_managed_type &) = delete; |
| 1065 |
}; |
| 1066 |
|
| 1067 |
template <typename managed_type> |
| 1068 |
template <typename Pc, typename Pm> |
| 1069 |
class valptridx<managed_type>::basic_ival_member_factory |
| 1070 |
{ |
| 1071 |
using containing_type = valptridx<managed_type>; |
| 1072 |
protected: |
| 1073 |
/* |
| 1074 |
* These casts are well-defined: |
| 1075 |
* - The reinterpret_cast is defined because |
| 1076 |
* `basic_ival_member_factory` is a base of a member of the |
| 1077 |
* anonymous union in `array_base_count_type`. |
| 1078 |
* - The anonymous union in `array_base_count_type` is the only |
| 1079 |
* member of that type, so its storage must be aligned to the |
| 1080 |
* beginning of the object. |
| 1081 |
* - `basic_ival_member_factory` and its derivatives are not used |
| 1082 |
* anywhere other than `array_base_count_type`, so any call to |
| 1083 |
* these methods must be on an instance used in |
| 1084 |
* `array_base_count_type`. |
| 1085 |
* |
| 1086 |
* - The static_cast is defined because `array_base_count_type` is a |
| 1087 |
* non-virtual base of `array_managed_type`. |
| 1088 |
* - `array_base_count_type` is not used as a base for any class |
| 1089 |
* other than `array_managed_type`, nor used freestanding, so any |
| 1090 |
* instance of `array_base_count_type` must be safe to downcast to |
| 1091 |
* `array_managed_type`. |
| 1092 |
*/ |
| 1093 |
constexpr const array_managed_type &get_array() const |
| 1094 |
{ |
| 1095 |
return static_cast<const array_managed_type &>(reinterpret_cast<const array_base_count_type &>(*this)); |
| 1096 |
} |
| 1097 |
array_managed_type &get_array() |
| 1098 |
{ |
| 1099 |
return static_cast<array_managed_type &>(reinterpret_cast<array_base_count_type &>(*this)); |
| 1100 |
} |
| 1101 |
template <typename P> |
| 1102 |
static guarded<P> check_untrusted_internal(const index_type i, auto &a) |
| 1103 |
{ |
| 1104 |
if (P::check_nothrow_index(i)) |
| 1105 |
return P{i, a, assume_nothrow_index{}}; |
| 1106 |
else |
| 1107 |
return nullptr; |
| 1108 |
} |
| 1109 |
template <typename P, typename index_type, typename array_type> |
| 1110 |
static guarded<P> check_untrusted_internal(index_type &&, array_type &) = delete; |
| 1111 |
template <typename P, typename index_type, typename array_type> |
| 1112 |
requires( |
| 1113 |
std::constructible_from<typename P::index_type, index_type> |
| 1114 |
) |
| 1115 |
[[nodiscard]] |
| 1116 |
/* C++ does not allow `static operator()()` until C++23, so name it |
| 1117 |
* `call_operator` instead. |
| 1118 |
*/ |
| 1119 |
static P call_operator(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const index_type i, array_type &a) |
| 1120 |
{ |
| 1121 |
return P(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, a); |
| 1122 |
} |
| 1123 |
template <typename P, containing_type::index_type v> |
| 1124 |
[[nodiscard]] |
| 1125 |
static P call_operator(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const containing_type::magic_constant<v> &m, auto &a) |
| 1126 |
{ |
| 1127 |
/* |
| 1128 |
* All call_operator definitions must have the macro which |
| 1129 |
* defines filename/lineno, but the magic_constant overload |
| 1130 |
* has no need for them. |
| 1131 |
* |
| 1132 |
* Cast them to void to silence the warning about unused |
| 1133 |
* parameters. |
| 1134 |
*/ |
| 1135 |
DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_VOID_VARS(); |
| 1136 |
return P(m, a); |
| 1137 |
} |
| 1138 |
basic_ival_member_factory() = default; |
| 1139 |
public: |
| 1140 |
basic_ival_member_factory(const basic_ival_member_factory &) = delete; |
| 1141 |
basic_ival_member_factory &operator=(const basic_ival_member_factory &) = delete; |
| 1142 |
void *operator &() const = delete; |
| 1143 |
template <typename T> |
| 1144 |
[[nodiscard]] |
| 1145 |
guarded<Pc> check_untrusted(T &&t) const |
| 1146 |
{ |
| 1147 |
return this->template check_untrusted_internal<Pc>(static_cast<T &&>(t), get_array()); |
| 1148 |
} |
| 1149 |
template <typename T> |
| 1150 |
[[nodiscard]] |
| 1151 |
guarded<Pm> check_untrusted(T &&t) |
| 1152 |
{ |
| 1153 |
return this->template check_untrusted_internal<Pm>(static_cast<T &&>(t), get_array()); |
| 1154 |
} |
| 1155 |
template <typename T> |
| 1156 |
[[nodiscard]] |
| 1157 |
Pc operator()(T &&t DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS) const |
| 1158 |
{ |
| 1159 |
return this->template call_operator<Pc>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS static_cast<T &&>(t), get_array()); |
| 1160 |
} |
| 1161 |
template <typename T> |
| 1162 |
[[nodiscard]] |
| 1163 |
Pm operator()(T &&t DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS) |
| 1164 |
{ |
| 1165 |
return this->template call_operator<Pm>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS static_cast<T &&>(t), get_array()); |
| 1166 |
} |
| 1167 |
}; |
| 1168 |
|
| 1169 |
template <typename managed_type> |
| 1170 |
template <typename T> |
| 1171 |
class valptridx<managed_type>::factory_range_iterator : |
| 1172 |
T |
| 1173 |
{ |
| 1174 |
public: |
| 1175 |
/* This static_assert is eager: it will reject a type T that |
| 1176 |
* would be dangerous if used in the affected algorithms, |
| 1177 |
* regardless of whether the program attempts such a use. This |
| 1178 |
* is acceptable since the modification to fix this assertion |
| 1179 |
* should not break any intended uses of the type. To pass the |
| 1180 |
* assertion, the type T must define: |
| 1181 |
|
| 1182 |
T &operator=(T &&) && = delete; |
| 1183 |
|
| 1184 |
* If normal move assignment is desired, also define: |
| 1185 |
|
| 1186 |
T &operator=(T &&) & = default; |
| 1187 |
|
| 1188 |
*/ |
| 1189 |
static_assert(!std::is_assignable<T &&, T &&>::value, "Accessibility of `T::operator=(T &&) &&` permits generation of incorrect code when passing factory_range_iterator<T> to some algorithms. Explicitly delete `T::operator=(T &&) &&` to inhibit this assertion."); |
| 1190 |
using iterator_category = std::bidirectional_iterator_tag; |
| 1191 |
/* If `T::vidx_type` is a type, then `value_type` is `T`. Otherwise, |
| 1192 |
* `value_type` is `managed_type`. This allows `value_type` to be the |
| 1193 |
* underlying type when `T == ptr`, which produces a flatter type |
| 1194 |
* hierarchy. |
| 1195 |
*/ |
| 1196 |
using value_type = std::conditional_t<(requires { typename T::vidx_type; }), T, managed_type>; |
| 1197 |
using difference_type = std::ptrdiff_t; |
| 1198 |
using typename T::pointer; |
| 1199 |
using typename T::reference; |
| 1200 |
/* A default constructor must be declared to satisfy constraints from |
| 1201 |
* std::ranges algorithms. The declaration must be visible in an unrelated |
| 1202 |
* context, so it is public here. However, it must not be used in normal |
| 1203 |
* operation. Therefore, the default constructor is declared in order to |
| 1204 |
* satisfy the library's concept check, but never implemented, since even |
| 1205 |
* the library's algorithms never default construct an instance of this. |
| 1206 |
* |
| 1207 |
* std::sentinel_for -> |
| 1208 |
* std::semiregular -> |
| 1209 |
* std::default_initializable -> |
| 1210 |
* requires { T{}; } |
| 1211 |
*/ |
| 1212 |
factory_range_iterator(); |
| 1213 |
factory_range_iterator(T i) : |
| 1214 |
T{std::move(i)} |
| 1215 |
{ |
| 1216 |
} |
| 1217 |
T base() const |
| 1218 |
{ |
| 1219 |
return *this; |
| 1220 |
} |
| 1221 |
/* Delegate to the base type `operator*` when possible. If the base type |
| 1222 |
* is a `ptridx`, then define `operator*` to return a copy of the |
| 1223 |
* underlying `ptridx`, so that the caller can capture both the pointer and |
| 1224 |
* the index. If the base type is not a `ptridx`, delegate to the base |
| 1225 |
* type `operator*`. The base version will return a reference to the |
| 1226 |
* underlying `managed_type`, avoiding the use of a temporary. |
| 1227 |
*/ |
| 1228 |
decltype(auto) operator*() const & |
| 1229 |
requires(std::is_same_v<value_type, managed_type>) |
| 1230 |
{ |
| 1231 |
return this->T::operator*(); |
| 1232 |
} |
| 1233 |
value_type operator*() const & |
| 1234 |
requires(!std::is_same_v<value_type, managed_type>) |
| 1235 |
{ |
| 1236 |
return *this; |
| 1237 |
} |
| 1238 |
/* Some STL algorithms require: |
| 1239 |
* |
| 1240 |
* !!std::is_same<decltype(iter), decltype(++iter)>::value |
| 1241 |
* |
| 1242 |
* If this requirement is not met, template argument deduction |
| 1243 |
* fails when the algorithm calls a helper function. |
| 1244 |
* |
| 1245 |
* If not for this requirement, `using T::operator++` would have |
| 1246 |
* been sufficient here. |
| 1247 |
*/ |
| 1248 |
factory_range_iterator &operator++() |
| 1249 |
{ |
| 1250 |
/* Use a static_cast instead of ignoring the return value and |
| 1251 |
* returning `*this`, to encourage the compiler to implement |
| 1252 |
* this as a tail-call since |
| 1253 |
* |
| 1254 |
* `offsetof(factory_range_iterator, T) == 0` |
| 1255 |
*/ |
| 1256 |
return static_cast<factory_range_iterator &>(this->T::operator++()); |
| 1257 |
} |
| 1258 |
/* operator++(int) is currently unused, but is required to satisfy |
| 1259 |
* the concept check on forward iterator. |
| 1260 |
*/ |
| 1261 |
factory_range_iterator operator++(int) |
| 1262 |
{ |
| 1263 |
auto result{*this}; |
| 1264 |
this->T::operator++(); |
| 1265 |
return result; |
| 1266 |
} |
| 1267 |
factory_range_iterator &operator--() |
| 1268 |
{ |
| 1269 |
return static_cast<factory_range_iterator &>(this->T::operator--()); |
| 1270 |
} |
| 1271 |
constexpr bool operator==(const factory_range_iterator &rhs) const = default; |
| 1272 |
constexpr bool operator==(const pointer rhs) const |
| 1273 |
{ |
| 1274 |
return static_cast<const T &>(*this) == rhs; |
| 1275 |
} |
| 1276 |
}; |
| 1277 |
|
| 1278 |
template <typename managed_type> |
| 1279 |
template <typename Pc, typename Pm> |
| 1280 |
class valptridx<managed_type>::basic_vval_member_factory : |
| 1281 |
public basic_ival_member_factory<Pc, Pm> |
| 1282 |
{ |
| 1283 |
protected: |
| 1284 |
using basic_ival_member_factory<Pc, Pm>::get_array; |
| 1285 |
using basic_ival_member_factory<Pc, Pm>::call_operator; |
| 1286 |
template <typename P> |
| 1287 |
using iterator = factory_range_iterator<P>; |
| 1288 |
template <typename P, typename policy, typename A> |
| 1289 |
static P call_operator(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const typename valptridx<managed_type>::template wrapper<valptridx<managed_type>::idx<policy>> i, A &a) |
| 1290 |
{ |
| 1291 |
return P(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, a); |
| 1292 |
} |
| 1293 |
template <typename P> |
| 1294 |
[[nodiscard]] |
| 1295 |
static P call_operator(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const typename P::mutable_pointer_type p, typename P::array_managed_type &a) |
| 1296 |
{ |
| 1297 |
return P(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS p, a); |
| 1298 |
} |
| 1299 |
/* |
| 1300 |
* When P is a const type, require a const array, so that attempts to |
| 1301 |
* provide a mutable array fail. This provides a slightly clearer error |
| 1302 |
* when trying to pass a const pointer to a mutable factory. |
| 1303 |
*/ |
| 1304 |
template <typename P> |
| 1305 |
requires(std::same_as<const array_managed_type, typename P::array_managed_type>) |
| 1306 |
[[nodiscard]] |
| 1307 |
static P call_operator(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const typename P::const_pointer_type p, const array_managed_type &a) |
| 1308 |
{ |
| 1309 |
return P(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS p, a); |
| 1310 |
} |
| 1311 |
template <typename P, typename A> |
| 1312 |
static iterator<P> end_internal(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS A &a) |
| 1313 |
{ |
| 1314 |
return P(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS static_cast<index_type>(a.get_count()), a, allow_end_construction{}); |
| 1315 |
} |
| 1316 |
public: |
| 1317 |
[[nodiscard]] |
| 1318 |
typename array_base_storage_type::size_type count() const |
| 1319 |
{ |
| 1320 |
return get_array().get_count(); |
| 1321 |
} |
| 1322 |
[[nodiscard]] |
| 1323 |
typename array_base_storage_type::size_type size() const |
| 1324 |
{ |
| 1325 |
return get_array().size(); |
| 1326 |
} |
| 1327 |
template <typename T> |
| 1328 |
[[nodiscard]] |
| 1329 |
Pc operator()(T &&t DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS) const |
| 1330 |
{ |
| 1331 |
return this->template call_operator<Pc>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS static_cast<T &&>(t), get_array()); |
| 1332 |
} |
| 1333 |
template <typename T> |
| 1334 |
[[nodiscard]] |
| 1335 |
Pm operator()(T &&t DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_L_DECL_VARS) |
| 1336 |
{ |
| 1337 |
return this->template call_operator<Pm>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS static_cast<T &&>(t), get_array()); |
| 1338 |
} |
| 1339 |
[[nodiscard]] |
| 1340 |
iterator<Pc> begin() const |
| 1341 |
{ |
| 1342 |
return Pc(valptridx<managed_type>::magic_constant<index_type{}>(), get_array()); |
| 1343 |
} |
| 1344 |
[[nodiscard]] |
| 1345 |
iterator<Pm> begin() |
| 1346 |
{ |
| 1347 |
return Pm(valptridx<managed_type>::magic_constant<index_type{}>(), get_array()); |
| 1348 |
} |
| 1349 |
[[nodiscard]] |
| 1350 |
iterator<Pc> end(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DECL_VARS) const |
| 1351 |
{ |
| 1352 |
return this->template end_internal<Pc>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS get_array()); |
| 1353 |
} |
| 1354 |
[[nodiscard]] |
| 1355 |
iterator<Pm> end(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DECL_VARS) |
| 1356 |
{ |
| 1357 |
return this->template end_internal<Pm>(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS get_array()); |
| 1358 |
} |
| 1359 |
}; |
| 1360 |
|
| 1361 |
#define DXX_VALPTRIDX_DEFINE_FACTORY(MANAGED_TYPE, GLOBAL_FACTORY, GLOBAL_ARRAY, MEMBER_FACTORY) \ |
| 1362 |
dxx_compiler_attribute_unused static auto &GLOBAL_FACTORY = GLOBAL_ARRAY.MEMBER_FACTORY |
| 1363 |
|
| 1364 |
#define DXX_VALPTRIDX_DEFINE_GLOBAL_FACTORY(MANAGED_TYPE, DERIVED_TYPE_PREFIX, GLOBAL_ARRAY, PISUFFIX, IVPREFIX, MCPREFIX) \ |
| 1365 |
DXX_VALPTRIDX_DEFINE_FACTORY(MANAGED_TYPE, IVPREFIX ## MCPREFIX ## DERIVED_TYPE_PREFIX ## PISUFFIX, GLOBAL_ARRAY, IVPREFIX ## MCPREFIX ## PISUFFIX) |
| 1366 |
|
| 1367 |
#define DXX_VALPTRIDX_DEFINE_GLOBAL_FACTORIES(MANAGED_TYPE,DERIVED_TYPE_PREFIX,GLOBAL_ARRAY) \ |
| 1368 |
extern MANAGED_TYPE ## _array GLOBAL_ARRAY; \ |
| 1369 |
namespace { namespace { \ |
| 1370 |
DXX_VALPTRIDX_FOR_EACH_PPI_TYPE(DXX_VALPTRIDX_DEFINE_GLOBAL_FACTORY, MANAGED_TYPE, DERIVED_TYPE_PREFIX, GLOBAL_ARRAY); \ |
| 1371 |
} } |
| 1372 |
|