Skip to content
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

PEP 1 #907

Merged
merged 20 commits into from
Oct 18, 2023
Merged

PEP 1 #907

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Current develop

### Added (new features/APIs/variables/...)
- [[PR 907]](https://github.com/parthenon-hpc-lab/parthenon/pull/907) PEP1: Allow subclassing StateDescriptor
- [[PR 932]](https://github.com/parthenon-hpc-lab/parthenon/pull/932) Add GetOrAddFlag to metadata
- [[PR 931]](https://github.com/parthenon-hpc-lab/parthenon/pull/931) Allow SparsePacks with subsets of blocks
- [[PR 921]](https://github.com/parthenon-hpc-lab/parthenon/pull/921) Add more flexible ways of adding and using MeshData/MeshBlockData objects to DataCollections
Expand Down
35 changes: 34 additions & 1 deletion src/interface/packages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <memory>
#include <string>
#include <vector>

#include "basic_types.hpp"

Expand All @@ -26,15 +27,47 @@ class Packages_t {
Packages_t() = default;
void Add(const std::shared_ptr<StateDescriptor> &package);

std::shared_ptr<StateDescriptor> const &Get(const std::string &name) {
std::shared_ptr<StateDescriptor> const &Get(const std::string &name) const {
return packages_.at(name);
}

// Retrieve a package pointer, cast to a given type T
template <typename T>
T *Get(const std::string &name) const {
return static_cast<T *>(packages_.at(name).get());
}

const Dictionary<std::shared_ptr<StateDescriptor>> &AllPackages() const {
return packages_;
}
Dictionary<std::shared_ptr<StateDescriptor>> &AllPackages() { return packages_; }

// Returns a sub-Dictionary containing just pointers to packages of type T.
// Dictionary is a *new copy*, and members are bare pointers, not shared_ptr.
template <typename T>
const Dictionary<T *> AllPackagesOfType() const {
Dictionary<T *> sub_dict;
for (auto package : packages_) {
if (T *cast_package = dynamic_cast<T *>(package.second.get())) {
sub_dict[package.first] = cast_package;
}
}
return sub_dict;
}

// Returns a list of pointers to packages of type T.
// List contains bare pointers, not shared_ptr objects
template <typename T>
const std::vector<T *> ListPackagesOfType() const {
std::vector<T *> sub_list;
for (auto package : packages_) {
if (T *cast_package = dynamic_cast<T *>(package.second.get())) {
sub_list.append(cast_package);
}
}
return sub_list;
}

private:
Dictionary<std::shared_ptr<StateDescriptor>> packages_;
};
Expand Down
5 changes: 4 additions & 1 deletion src/interface/state_descriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ class StateDescriptor {
}
}

// Virtual destructor for subclassing
virtual ~StateDescriptor() = default;

static std::shared_ptr<StateDescriptor>
CreateResolvedStateDescriptor(Packages_t &packages);

Expand Down Expand Up @@ -429,7 +432,7 @@ class StateDescriptor {

friend std::ostream &operator<<(std::ostream &os, const StateDescriptor &sd);

private:
protected:
void InvertControllerMap();

Params params_;
Expand Down
Loading