Repository navigation
Wide pointer narrowing tutorial using inheritance #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /* Copyright (c) 2025 The XYZ Protocol Authors. All Rights Reserved. | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| this software and associated documentation files (the "Software"), to deal in | ||
| the Software without restriction, including without limitation the rights to | ||
| use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| the Software, and to permit persons to whom the Software is furnished to do so, | ||
| subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| ==============================================================================*/ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| // A minimal example of how wide pointers can perform zero-cost narrowing | ||
| // conversions when vtables are composed with inheritance. | ||
| // | ||
| // For non-tutorial code, constructors would need constraining with concepts to | ||
| // prevent construction of wide pointers that perform multiple vtable lookups. | ||
|
|
||
| namespace xyz::tutorials::wide_pointer_narrowing { | ||
|
|
||
| // The tests count vtable calls. | ||
| // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) | ||
| thread_local std::size_t vtable_calls = 0; | ||
|
|
||
| struct FooVtable { | ||
| int (*fn_foo)(void*); | ||
| }; | ||
|
|
||
| struct BarVtable { | ||
| int (*fn_bar)(void*); | ||
| }; | ||
|
|
||
| struct FooPtr { | ||
| void* ptr_; | ||
| const FooVtable* vtable_; | ||
|
|
||
| template <typename T> | ||
| explicit FooPtr(T* t) : ptr_(t) { | ||
| static constexpr FooVtable vtable{ | ||
| +[](void* self) { | ||
| ++vtable_calls; | ||
| return static_cast<T*>(self)->foo(); | ||
| }, | ||
| }; | ||
| vtable_ = &vtable; | ||
| } | ||
|
|
||
| FooPtr(void* ptr, const FooVtable* vtable) : ptr_(ptr), vtable_(vtable) {} | ||
|
|
||
| int foo() const { return vtable_->fn_foo(ptr_); } | ||
| }; | ||
|
|
||
| struct BarPtr { | ||
| void* ptr_; | ||
| const BarVtable* vtable_; | ||
|
|
||
| template <typename T> | ||
| explicit BarPtr(T* t) : ptr_(t) { | ||
| static constexpr BarVtable vtable{ | ||
| +[](void* self) { | ||
| ++vtable_calls; | ||
| return static_cast<T*>(self)->bar(); | ||
| }, | ||
| }; | ||
| vtable_ = &vtable; | ||
| } | ||
|
|
||
| BarPtr(void* ptr, const BarVtable* vtable) : ptr_(ptr), vtable_(vtable) {} | ||
|
|
||
| int bar() const { return vtable_->fn_bar(ptr_); } | ||
| }; | ||
|
|
||
| // Defining the wide vtable in terms of Foo and Bar vtables allows | ||
| // narrowing conversions without performing nested vtable calls. | ||
| struct WideVtable : FooVtable, BarVtable {}; | ||
|
|
||
| struct WidePtr { | ||
| void* ptr_; | ||
| const WideVtable* vtable_; | ||
|
|
||
| template <typename T> | ||
| explicit WidePtr(T* t) : ptr_(t) { | ||
| static constexpr WideVtable vtable{ | ||
| {+[] /* foo */ (void* self) { | ||
| ++vtable_calls; | ||
| return static_cast<T*>(self)->foo(); | ||
| }}, | ||
| {+[] /* bar */ (void* self) { | ||
| ++vtable_calls; | ||
| return static_cast<T*>(self)->bar(); | ||
| }}, | ||
| }; | ||
| vtable_ = &vtable; | ||
| } | ||
|
|
||
| explicit operator FooPtr() const { return {ptr_, vtable_}; } | ||
|
|
||
| explicit operator BarPtr() const { return {ptr_, vtable_}; } | ||
|
|
||
| int foo() const { return vtable_->fn_foo(ptr_); } | ||
|
|
||
| int bar() const { return vtable_->fn_bar(ptr_); } | ||
| }; | ||
|
|
||
| struct FooBar { | ||
| int foo() { return 0; } | ||
|
|
||
| int bar() { return 1; } | ||
| }; | ||
|
|
||
| TEST(TutorialsWidePointerNarrowing, WidePointerCheck) { | ||
| vtable_calls = 0; | ||
|
|
||
| FooBar fb; | ||
| WidePtr wptr(&fb); | ||
|
|
||
| EXPECT_EQ(wptr.foo(), 0); | ||
| EXPECT_EQ(wptr.bar(), 1); | ||
| EXPECT_EQ(vtable_calls, 2); | ||
| } | ||
|
|
||
| TEST(TutorialsWidePointerNarrowing, FooPointerCheck) { | ||
| vtable_calls = 0; | ||
|
|
||
| FooBar fb; | ||
| FooPtr foo_ptr(&fb); | ||
| EXPECT_EQ(foo_ptr.foo(), 0); | ||
| EXPECT_EQ(vtable_calls, 1); | ||
| } | ||
|
|
||
| TEST(TutorialsWidePointerNarrowing, BarPointerCheck) { | ||
| vtable_calls = 0; | ||
|
|
||
| FooBar fb; | ||
|
|
||
| BarPtr bar_ptr(&fb); | ||
| EXPECT_EQ(bar_ptr.bar(), 1); | ||
| EXPECT_EQ(vtable_calls, 1); | ||
| } | ||
|
|
||
| TEST(TutorialsWidePointerNarrowing, FooPointerNarrowingCheck) { | ||
| vtable_calls = 0; | ||
|
|
||
| FooBar fb; | ||
| WidePtr wptr(&fb); | ||
|
|
||
| FooPtr foo_ptr(wptr); | ||
| EXPECT_EQ(foo_ptr.foo(), 0); | ||
| EXPECT_EQ(vtable_calls, 1); | ||
|
jbcoe marked this conversation as resolved.
Resolved
|
||
|
|
||
| EXPECT_EQ(foo_ptr.vtable_, static_cast<const FooVtable*>(wptr.vtable_)); | ||
| } | ||
|
|
||
| TEST(TutorialsWidePointerNarrowing, BarPointerNarrowingCheck) { | ||
| vtable_calls = 0; | ||
|
|
||
| FooBar fb; | ||
| WidePtr wptr(&fb); | ||
|
|
||
| BarPtr bar_ptr(wptr); | ||
| EXPECT_EQ(bar_ptr.bar(), 1); | ||
| EXPECT_EQ(vtable_calls, 1); | ||
|
|
||
| EXPECT_EQ(bar_ptr.vtable_, static_cast<const BarVtable*>(wptr.vtable_)); | ||
| } | ||
|
|
||
| } // namespace xyz::tutorials::wide_pointer_narrowing | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.