|
199 | 199 | #include <cstddef>
|
200 | 200 | #include <utility>
|
201 | 201 | #include <cstdio>
|
| 202 | + #include <span> |
202 | 203 |
|
203 | 204 | #if defined(CPP2_USE_SOURCE_LOCATION)
|
204 | 205 | #include <source_location>
|
@@ -494,6 +495,130 @@ class out {
|
494 | 495 | }(PARAM1)
|
495 | 496 | //--------------------------------------------------------------------
|
496 | 497 |
|
| 498 | +//----------------------------------------------------------------------- |
| 499 | +// |
| 500 | +// cpp2::safety_check() ensures that cpp1 pointers are also covered by safetychecks |
| 501 | +// |
| 502 | +//----------------------------------------------------------------------- |
| 503 | +// |
| 504 | +template <typename... Ts> |
| 505 | +inline constexpr auto program_violates_lifetime_safety_guarantee = sizeof...(Ts) < 0; |
| 506 | + |
| 507 | +template <typename T> |
| 508 | + requires std::is_pointer_v<T> |
| 509 | +class safetychecked_pointer { |
| 510 | + T ptr; |
| 511 | +public: |
| 512 | + |
| 513 | + constexpr safetychecked_pointer(T ptr) : ptr{ptr} {} |
| 514 | + |
| 515 | + constexpr operator T&() noexcept { return ptr; } |
| 516 | + |
| 517 | + template <typename... Ts> void operator+ () const {static_assert(program_violates_lifetime_safety_guarantee<Ts...>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 518 | + template <typename... Ts> void operator- () const {static_assert(program_violates_lifetime_safety_guarantee<Ts...>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 519 | + template <typename X> void operator+ (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 520 | + template <typename X> void operator- (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 521 | + template <typename X> void operator* (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 522 | + template <typename X> void operator/ (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 523 | + template <typename X> void operator% (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 524 | + template <typename X> void operator^ (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 525 | + template <typename X> void operator& (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 526 | + template <typename X> void operator| (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 527 | + |
| 528 | + template <typename... Ts> void operator++ (Ts...) const {static_assert(program_violates_lifetime_safety_guarantee<Ts...>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 529 | + template <typename... Ts> void operator-- (Ts...) const {static_assert(program_violates_lifetime_safety_guarantee<Ts...>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 530 | + template <typename... Ts> void operator[] (Ts...) const {static_assert(program_violates_lifetime_safety_guarantee<Ts...>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 531 | + template <typename X> void operator+= (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 532 | + template <typename X> void operator-= (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 533 | + template <typename X> void operator*= (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 534 | + template <typename X> void operator/= (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 535 | + |
| 536 | + template <typename... Ts> void operator~ () const {static_assert(program_violates_lifetime_safety_guarantee<Ts...>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 537 | + template <typename X > void operator%= (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 538 | + template <typename X > void operator^= (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 539 | + template <typename X > void operator&= (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 540 | + template <typename X > void operator|= (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 541 | + template <typename X > void operator<<=(X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 542 | + template <typename X > void operator>>=(X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 543 | + template <typename X > void operator<< (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 544 | + template <typename X > void operator>> (X) const {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 545 | + |
| 546 | + template <typename X > friend void operator+ (X, const safetychecked_pointer&) {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 547 | + template <typename X > friend void operator- (X, const safetychecked_pointer&) {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 548 | + template <typename X > friend void operator* (X, const safetychecked_pointer&) {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 549 | + template <typename X > friend void operator/ (X, const safetychecked_pointer&) {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer arithmetic is illegal - use std::span or gsl::span instead");} |
| 550 | + template <typename X > friend void operator% (X, const safetychecked_pointer&) {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 551 | + template <typename X > friend void operator^ (X, const safetychecked_pointer&) {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 552 | + template <typename X > friend void operator& (X, const safetychecked_pointer&) {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 553 | + template <typename X > friend void operator| (X, const safetychecked_pointer&) {static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first");} |
| 554 | + |
| 555 | + |
| 556 | + template <typename X> |
| 557 | + requires (std::is_same_v<T,X> || std::is_base_of_v<T, X>) |
| 558 | + constexpr safetychecked_pointer& operator=(X lhs) noexcept { |
| 559 | + ptr = lhs; |
| 560 | + return *this; |
| 561 | + } |
| 562 | + |
| 563 | + template <typename X> |
| 564 | + requires std::is_same_v<std::nullptr_t,X> |
| 565 | + constexpr void operator=(X lhs) noexcept { static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer assignment from null is illegal"); } |
| 566 | + |
| 567 | + template <typename X> |
| 568 | + requires std::is_integral_v<X> |
| 569 | + constexpr void operator=(X lhs) noexcept { static_assert(program_violates_lifetime_safety_guarantee<X>, "pointer assignment from integer is illegal"); } |
| 570 | + |
| 571 | + bool operator!() const { return !ptr; } |
| 572 | + |
| 573 | + constexpr safetychecked_pointer<T*> operator&() noexcept { return &ptr; } |
| 574 | + |
| 575 | + constexpr auto operator*() noexcept { |
| 576 | + if constexpr (std::is_pointer_v<CPP2_TYPEOF(*ptr)>) { |
| 577 | + return safetychecked_pointer<CPP2_TYPEOF(*ptr)>(*ptr); |
| 578 | + } else { |
| 579 | + return *ptr; |
| 580 | + } |
| 581 | + } |
| 582 | + |
| 583 | + constexpr T operator->() const noexcept { return ptr; } |
| 584 | +}; |
| 585 | + |
| 586 | +template <typename X> |
| 587 | + requires ( !std::is_pointer_v<std::remove_cvref_t<X>> |
| 588 | + && std::is_copy_constructible_v<X> ) |
| 589 | +inline constexpr decltype(auto) safety_check(X const& x) { |
| 590 | + return x; |
| 591 | +} |
| 592 | + |
| 593 | +template <typename X> |
| 594 | + requires std::is_rvalue_reference_v<X> |
| 595 | +inline constexpr decltype(auto) safety_check(X&& x) { |
| 596 | + return x; |
| 597 | +} |
| 598 | + |
| 599 | +template <typename X> |
| 600 | + requires (std::is_pointer_v<std::remove_cvref_t<X>> && !std::is_bounded_array_v<X>) |
| 601 | +inline constexpr auto safety_check(X const& x) { |
| 602 | + return safetychecked_pointer(x); |
| 603 | +} |
| 604 | + |
| 605 | +template <typename X> |
| 606 | + requires (!std::is_pointer_v<std::remove_cvref_t<X>> && !std::is_function_v<X> && !std::is_bounded_array_v<X>) |
| 607 | +inline constexpr auto& safety_check(X& x) { |
| 608 | + return x; |
| 609 | +} |
| 610 | + |
| 611 | +template <typename X> |
| 612 | + requires (!std::is_copy_constructible_v<X>) |
| 613 | +inline constexpr auto safety_check(X&& x) { |
| 614 | + return std::forward<X>(x); |
| 615 | +} |
| 616 | + |
| 617 | +template <typename X> |
| 618 | + requires std::is_bounded_array_v<X> |
| 619 | +inline constexpr auto safety_check(X const& x) { |
| 620 | + return std::span(x); |
| 621 | +} |
497 | 622 |
|
498 | 623 | //-----------------------------------------------------------------------
|
499 | 624 | //
|
|
0 commit comments