ABLATE Source Documentation  0.12.34
accessor.hpp
1 #ifndef ABLATELIBRARY_PARTICLEACCESSOR_HPP
2 #define ABLATELIBRARY_PARTICLEACCESSOR_HPP
3 
4 #include <petsc.h>
5 #include <functional>
6 #include <map>
7 #include <vector>
8 #include "particles/field.hpp"
9 #include "pointData.hpp"
10 #include "utilities/petscUtilities.hpp"
11 
12 namespace ablate::particles::accessors {
16 template <class DataType>
17 class Accessor {
18  private:
22  std::map<std::string, Data<DataType>> dataCache;
23 
27  const bool cachePointData;
28 
32  std::vector<std::function<void()>> destructors{};
33 
34  protected:
40  virtual Data<DataType> CreateData(const std::string& fieldName) = 0;
41 
42  public:
43  explicit Accessor(bool cachePointData) : cachePointData(cachePointData) {}
44 
45  virtual ~Accessor() {
46  for (auto& function : destructors) {
47  function();
48  }
49  };
50 
56  Data<DataType> operator[](const std::string& fieldName) { return GetData(fieldName); }
57 
63  inline Data<DataType> GetData(const std::string& fieldName) {
64  if (cachePointData) {
65  if (!dataCache.count(fieldName)) {
66  dataCache[fieldName] = CreateData(fieldName);
67  }
68  return dataCache.at(fieldName);
69  } else {
70  return CreateData(fieldName);
71  }
72  }
73 
78  inline void RegisterCleanupFunction(const std::function<void()>& function) { destructors.push_back(function); }
79 
83  Accessor(const Accessor&) = delete;
84 };
85 } // namespace ablate::particles::accessors
86 #endif // ABLATELIBRARY_PARTICLEACCESSOR_HPP
Definition: accessor.hpp:17
Data< DataType > GetData(const std::string &fieldName)
Definition: accessor.hpp:63
virtual Data< DataType > CreateData(const std::string &fieldName)=0
void RegisterCleanupFunction(const std::function< void()> &function)
Definition: accessor.hpp:78
Data< DataType > operator[](const std::string &fieldName)
Definition: accessor.hpp:56
Accessor(const Accessor &)=delete
Definition: pointData.hpp:14