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