-
Notifications
You must be signed in to change notification settings - Fork 26
Base class for submodules: base_step.
#154
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1671aa9
Base class for submodules: base_step.
djmallum 249d567
Correcting `base_step` constness, concepts, and constructors
djmallum 8bb2582
Added documentation to `base_step`
djmallum 0b4d269
Added `#pragma once` to `base_step.hpp`
djmallum 220be97
Added GNU License Header and author name
djmallum 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
Some comments aren't visible on the classic Files Changed page.
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,72 @@ | ||
| // | ||
| // Canadian Hydrological Model - The Canadian Hydrological Model (CHM) is a novel | ||
| // modular unstructured mesh based approach for hydrological modelling | ||
| // Copyright (C) 2018 Christopher Marsh | ||
| // | ||
| // This file is part of Canadian Hydrological Model. | ||
| // | ||
| // Canadian Hydrological Model is free software: you can redistribute it and/or | ||
| // modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Canadian Hydrological Model is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Canadian Hydrological Model. If not, see | ||
| // <http://www.gnu.org/licenses/>. | ||
| // | ||
|
|
||
| // | ||
| // Created by Donovan Allum 2025 | ||
| // | ||
|
|
||
| #pragma once | ||
| #include <concepts> | ||
djmallum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @brief Base class for submodules using the Curiously Recurring Template Pattern (CRTP). | ||
| * | ||
| * This class enforces that derived classes implement an `execute_impl(Data&) const` method, | ||
| * and provides a public `execute(Data&) const` method that forwards to the derived implementation. | ||
| * | ||
| * The CRTP pattern allows compile-time polymorphism, enabling static dispatch and | ||
| * avoiding the overhead of virtual functions. This is useful for performance-critical | ||
| * code or when templates are preferred over inheritance. | ||
| * | ||
| * Usage example: | ||
| * @code | ||
| * struct MyStep : public base_step<MyStep, MyData> { | ||
| * void execute_impl(MyData& d) { | ||
| * // Step-specific logic here | ||
| * } | ||
| * }; | ||
| * MyStep step; | ||
| * MyData data; | ||
| * step.execute(data); // Calls MyStep::execute_impl(data) | ||
| * @endcode | ||
| */ | ||
|
|
||
| template<typename T, typename Data> | ||
| concept GuaranteeImplementExecute = requires(const T& t, Data& d) { | ||
| { t.execute_impl(d) } -> std::same_as<void>; | ||
| }; | ||
|
|
||
| template<class Derived, class Data> | ||
| class base_step { | ||
| protected: | ||
| base_step() {}; | ||
| public: | ||
| void execute(Data& d) const { | ||
|
|
||
| static_assert(GuaranteeImplementExecute<Derived,Data>, | ||
| "Derived class must implement: void execute_impl(Data&) const"); | ||
|
|
||
| static_cast<const Derived*>(this)->execute_impl(d); | ||
| } | ||
| ~base_step() {}; | ||
| }; | ||
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.