diff --git a/CHANGELOG.md b/CHANGELOG.md index d55ce40ac5b3..49d929c46ab5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/interface/packages.hpp b/src/interface/packages.hpp index 285a815bf9ea..1ef6db59f58f 100644 --- a/src/interface/packages.hpp +++ b/src/interface/packages.hpp @@ -16,6 +16,7 @@ #include #include +#include #include "basic_types.hpp" @@ -26,15 +27,47 @@ class Packages_t { Packages_t() = default; void Add(const std::shared_ptr &package); - std::shared_ptr const &Get(const std::string &name) { + std::shared_ptr const &Get(const std::string &name) const { return packages_.at(name); } + // Retrieve a package pointer, cast to a given type T + template + T *Get(const std::string &name) const { + return static_cast(packages_.at(name).get()); + } + const Dictionary> &AllPackages() const { return packages_; } Dictionary> &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 + const Dictionary AllPackagesOfType() const { + Dictionary sub_dict; + for (auto package : packages_) { + if (T *cast_package = dynamic_cast(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 + const std::vector ListPackagesOfType() const { + std::vector sub_list; + for (auto package : packages_) { + if (T *cast_package = dynamic_cast(package.second.get())) { + sub_list.append(cast_package); + } + } + return sub_list; + } + private: Dictionary> packages_; }; diff --git a/src/interface/state_descriptor.hpp b/src/interface/state_descriptor.hpp index ff21e628147c..5bb37ea83b60 100644 --- a/src/interface/state_descriptor.hpp +++ b/src/interface/state_descriptor.hpp @@ -108,6 +108,9 @@ class StateDescriptor { } } + // Virtual destructor for subclassing + virtual ~StateDescriptor() = default; + static std::shared_ptr CreateResolvedStateDescriptor(Packages_t &packages); @@ -429,7 +432,7 @@ class StateDescriptor { friend std::ostream &operator<<(std::ostream &os, const StateDescriptor &sd); - private: + protected: void InvertControllerMap(); Params params_;