Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
model_collection.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <type_traits>
8#include <variant>
9#include <vector>
10
11#include "../io/ParameterMap.h"
12#include "../utils/error_handling.h" // always_false
13#include "galaxy/disk_galaxy.h"
14
185
186namespace model_detail
187{
188
189// this is just a placeholder model type until we have 2 or more models
191 explicit DummyModel(ParameterMap& pmap) {}
192};
193
194// a type-safe union that can represent all model types
195// -> to add a new kind of model, append it to the list of template arguments
196// -> todo: consolidate DiskGalaxy and ClusteredDiskGalaxy into a single class
197using model_variant = std::variant<DummyModel, ClusteredDiskGalaxy>;
198
199// define logic to check if a type T is an allowed type of a std::variant
200template <typename T, typename variant>
202
203template <typename T, typename... Types>
204struct isVariantAlternativeType<T, std::variant<Types...>> : public std::disjunction<std::is_same<T, Types>...> {
205};
206
207// evaluates to whether a type T is a known variant type
208template <class T>
209constexpr bool is_model_type = isVariantAlternativeType<T, model_variant>::value;
210
211} // namespace model_detail
212
215{
217 std::vector<model_detail::model_variant> vec_;
218
219 public:
221 ModelCollection() = default;
222
224 explicit ModelCollection(ParameterMap& pmap);
225
226 ModelCollection(ModelCollection&&) = default;
227 ModelCollection& operator=(ModelCollection&&) = default;
228
229 // we forbid copy construction and copy assignment to retain flexibility (in case
230 // we convert vec_ to hold std::unique_ptr instances)
231 ModelCollection(const ModelCollection&) = delete;
232 ModelCollection& operator=(const ModelCollection&) = delete;
233
238 template <typename T>
239 const T* try_get() const
240 {
241 static_assert(model_detail::is_model_type<T>, "T isn't a known model type");
242 for (const model_detail::model_variant& v : vec_) {
243 const T* ptr = std::get_if<T>(&v);
244 if (ptr != nullptr) return ptr;
245 }
246
247 return nullptr;
248 }
249};
250
Holds a collection of models.
Definition model_collection.h:215
ModelCollection()=default
default constructor
const T * try_get() const
Try to retrieve a model of the specified type.
Definition model_collection.h:239
A class that provides map-like access to parameter files.
Definition ParameterMap.h:74
Definition model_collection.h:190
Definition model_collection.h:201