Block-Structured AMR Software Framework
AMReX_FabFactory.H
Go to the documentation of this file.
1 
2 #ifndef BL_FABFACTORY_H_
3 #define BL_FABFACTORY_H_
4 #include <AMReX_Config.H>
5 
6 #include <AMReX_Box.H>
7 #include <AMReX_Print.H>
8 #include <AMReX_MakeType.H>
9 #include <AMReX_Vector.H>
10 #include <AMReX_Arena.H>
11 #include <AMReX_TypeTraits.H>
12 
13 namespace amrex
14 {
15 
16 class FArrayBox;
17 
18 enum class FabType : int {
19  covered = -1,
20  regular = 0,
21  singlevalued = 1,
22  multivalued = 2,
23  undefined = 100
24 };
25 
26 struct FabInfo
27 {
28  bool alloc = true;
29  bool shared = false;
30  Arena* arena = nullptr;
31 
32  FabInfo& SetAlloc (bool a) noexcept {
33  alloc = a;
34  return *this;
35  }
36 
37  FabInfo& SetShared (bool s) noexcept {
38  shared = s;
39  return *this;
40  }
41 
42  FabInfo& SetArena (Arena* ar) noexcept {
43  arena = ar;
44  return *this;
45  }
46 };
47 
48 template <class FAB>
50 {
51 public:
52  FabFactory () noexcept = default;
53  FabFactory (FabFactory const&) noexcept = default;
54  FabFactory (FabFactory &&) noexcept = default;
55  FabFactory& operator= (FabFactory const&) noexcept = default;
56  FabFactory& operator= (FabFactory &&) noexcept = default;
57  virtual ~FabFactory () noexcept = default;
59  virtual FAB* create (const Box& box, int ncomps, const FabInfo& info, int box_index) const = 0;
61  virtual FAB* create_alias (FAB const& /*rhs*/, int /*scomp*/, int /*ncomp*/) const { return nullptr; }
62  virtual void destroy (FAB* fab) const = 0;
63  AMREX_NODISCARD virtual FabFactory<FAB>* clone () const = 0;
64  AMREX_NODISCARD virtual Long nBytes (const Box& box, int ncomps, int /*box_index*/) const {
65  if constexpr (IsBaseFab_v<FAB>) {
66  return box.numPts() * ncomps * Long(sizeof(typename FAB::value_type));
67  } else {
68  return -1;
69  }
70  }
71 };
72 
73 template <class FAB>
75  : public FabFactory<FAB>
76 {
77 public:
79  FAB* create (const Box& box, int ncomps, const FabInfo& info, int /*box_index*/) const override
80  {
81  return new FAB(box, ncomps, info.alloc, info.shared, info.arena);
82  }
83 
85  FAB* create_alias (FAB const& rhs, int scomp, int ncomp) const override
86  {
87  return new FAB(rhs, amrex::make_alias, scomp, ncomp);
88  }
89 
90  void destroy (FAB* fab) const override
91  {
92  delete fab;
93  }
94 
96  DefaultFabFactory<FAB>* clone () const override {
97  return new DefaultFabFactory<FAB>();
98  }
99 };
100 
101 }
102 
103 #endif
#define AMREX_NODISCARD
Definition: AMReX_Extension.H:251
A virtual base class for objects that manage their own dynamic memory allocation.
Definition: AMReX_Arena.H:100
AMREX_GPU_HOST_DEVICE Long numPts() const noexcept
Returns the number of points contained in the BoxND.
Definition: AMReX_Box.H:346
Definition: AMReX_FabFactory.H:76
void destroy(FAB *fab) const override
Definition: AMReX_FabFactory.H:90
AMREX_NODISCARD DefaultFabFactory< FAB > * clone() const override
Definition: AMReX_FabFactory.H:96
AMREX_NODISCARD FAB * create_alias(FAB const &rhs, int scomp, int ncomp) const override
Definition: AMReX_FabFactory.H:85
AMREX_NODISCARD FAB * create(const Box &box, int ncomps, const FabInfo &info, int) const override
Definition: AMReX_FabFactory.H:79
Definition: AMReX_FabFactory.H:50
virtual AMREX_NODISCARD FAB * create_alias(FAB const &, int, int) const
Definition: AMReX_FabFactory.H:61
virtual AMREX_NODISCARD FAB * create(const Box &box, int ncomps, const FabInfo &info, int box_index) const =0
virtual AMREX_NODISCARD FabFactory< FAB > * clone() const =0
virtual AMREX_NODISCARD Long nBytes(const Box &box, int ncomps, int) const
Definition: AMReX_FabFactory.H:64
virtual void destroy(FAB *fab) const =0
FabFactory() noexcept=default
@ FAB
Definition: AMReX_AmrvisConstants.H:86
Definition: AMReX_Amr.cpp:49
@ make_alias
Definition: AMReX_MakeType.H:7
FabType
Definition: AMReX_FabFactory.H:18
const int[]
Definition: AMReX_BLProfiler.cpp:1664
Definition: AMReX_FabFactory.H:27
FabInfo & SetArena(Arena *ar) noexcept
Definition: AMReX_FabFactory.H:42
bool alloc
Definition: AMReX_FabFactory.H:28
FabInfo & SetShared(bool s) noexcept
Definition: AMReX_FabFactory.H:37
bool shared
Definition: AMReX_FabFactory.H:29
FabInfo & SetAlloc(bool a) noexcept
Definition: AMReX_FabFactory.H:32
Arena * arena
Definition: AMReX_FabFactory.H:30