ARTS 2.5.11 (git: 6827797f)
workspace_ng.h
Go to the documentation of this file.
1
8#ifndef WORKSPACE_NG_INCLUDED
9#define WORKSPACE_NG_INCLUDED
10
11#include <unordered_map>
12#include <memory>
13#include <stack>
14#include <vector>
15
16#include "array.h"
17#include "arts_omp.h"
18#include "wsv_aux.h"
19
21 std::shared_ptr<void> wsv;
23};
24
25using WorkspaceVariable = stack<WorkspaceVariableStruct, std::vector<WorkspaceVariableStruct>>;
26
29 ~WorkspaceBorrowGuard() noexcept { wsv.pop(); };
30};
31
36class Workspace final : public std::enable_shared_from_this<Workspace> {
41 Workspace();
42
50 Workspace(const Workspace &workspace);
51
53
54 public:
57
59 std::shared_ptr<wsv_data_type> wsv_data_ptr;
60
61 using WsvMap_type = unordered_map<String, Index>;
62 std::shared_ptr<WsvMap_type> WsvMap_ptr;
63
65
67 [[nodiscard]] static std::shared_ptr<Workspace> create();
68
70 [[nodiscard]] std::shared_ptr<Workspace> shallowcopy() const;
71
73 Workspace(Workspace&&) noexcept = default;
74
81 void set_empty(Index i);
82
90 void duplicate(Index i);
91
97 [[nodiscard]] bool is_initialized(Index i) const;
98
100 [[nodiscard]] Index depth(Index i) const;
101
108 void pop(Index i);
109
110 void emplace(Index);
111
119 template <typename T>
120 WorkspaceBorrowGuard borrow(Index i, T &wsv) {
121 using U = std::remove_cv_t<T>;
122 std::shared_ptr<U> wsv_ptr(const_cast<U *>(&wsv), [](U *) {});
124 wsvs.initialized = true;
125 wsvs.wsv = wsv_ptr;
126 ws[i].push(wsvs);
127 return {ws[i]};
128 }
129
137 template <typename T>
139 using U = std::remove_cv_t<T>;
140 std::shared_ptr<U> wsv_ptr(const_cast<U *>(&wsv), [](U *) {});
142 wsvs.initialized = false;
143 wsvs.wsv = wsv_ptr;
144 ws[i].push(wsvs);
145 return {ws[i]};
146 }
147
155 template <typename T>
156 void push_move(Index i, std::shared_ptr<T> &&wsv_ptr) {
158 wsvs.initialized = true;
159 wsvs.wsv = std::forward<std::shared_ptr<T>>(wsv_ptr);
160 ws[i].push(std::move(wsvs));
161 }
162
164 [[nodiscard]] Index nelem() const { return ws.nelem(); }
165
167 Index add_wsv(const WsvRecord &wsv);
168
170 std::shared_ptr<void> operator[](Index i);
171
173 template <class T>
174 T* get(const char *name) {
175 if (const Index pos = WsvMap_ptr -> at(name); is_initialized(pos)) {
176 return static_cast<T*>(ws[pos].top().wsv.get());
177 }
178 return nullptr;
179 }
180
182 void swap(Workspace &other) noexcept;
183
192 template <typename OutputStream>
193 void PrintWsvName(OutputStream &outstream, Index i) const {
194 outstream << (*wsv_data_ptr)[i].Name() << "(" << i << ") ";
195 }
196
198 std::shared_ptr<Workspace> deepcopy();
199
200 wsv_data_type wsvs(const ArrayOfIndex&) const;
202};
203
204template <typename T>
205concept CopyConstructor = requires(const T& a) {{ T{a} };};
206
207template <typename T>
208concept ShallowCopyConstructor = requires(const T& a) {a.shallowcopy();};
209
210template <typename T>
212
213
214template <typename T>
215std::shared_ptr<T> get_shallow_copy(const T& x) {
216 if constexpr (ShallowCopyConstructor<T>) return x.shallowcopy();
217 else { T mout{x}; return std::make_shared<T>(std::move(mout)); }
218}
219
220template <CanCopy T>
224 std::shared_ptr<T> copy;
225
226 public:
228 : orig(ws),
230 copy(nullptr) {}
231
232 OmpParallelCopyGuard(T &ws, bool do_copy_manually)
233 : orig(ws), do_copy(do_copy_manually), copy(nullptr) {}
234
236 : orig(cp.orig),
237 do_copy(cp.do_copy),
238 copy(do_copy ? get_shallow_copy(orig) : nullptr) {}
239
240 operator T &() { return copy ? *copy : orig; }
241
242 operator const T &() const { return copy ? *copy : orig; }
243};
244
246
247#endif /* WORKSPACE_NG_INCLUDED */
This file contains the definition of Array.
int arts_omp_get_max_threads()
Wrapper for omp_get_max_threads.
Definition: arts_omp.cc:29
bool arts_omp_in_parallel()
Wrapper for omp_in_parallel.
Definition: arts_omp.cc:45
Header file for helper functions for OpenMP.
This can be used to make arrays out of anything.
Definition: array.h:31
Index nelem() const ARTS_NOEXCEPT
Definition: array.h:75
OmpParallelCopyGuard(T &ws, bool do_copy_manually)
Definition: workspace_ng.h:232
std::shared_ptr< T > copy
Definition: workspace_ng.h:224
OmpParallelCopyGuard(const OmpParallelCopyGuard &cp)
Definition: workspace_ng.h:235
Workspace class.
Definition: workspace_ng.h:36
std::shared_ptr< wsv_data_type > wsv_data_ptr
Definition: workspace_ng.h:59
void swap(Workspace &other) noexcept
Swap with another workspace.
Definition: workspace_ng.cc:95
Index add_wsv(const WsvRecord &wsv)
Add a new variable to this workspace.
Definition: workspace_ng.cc:25
static std::shared_ptr< Workspace > create()
Creates a new Workspace, it has to be created as a shared pointer.
void emplace(Index)
WorkspaceBorrowGuard borrow_uninitialized(Index i, T &wsv)
Put a new WSV onto its stack.
Definition: workspace_ng.h:138
void PrintWsvName(OutputStream &outstream, Index i) const
Print WSV name to output stream.
Definition: workspace_ng.h:193
std::shared_ptr< Workspace > shallowcopy() const
Shallow copy of a Workspace, it has to be created as a shared pointer.
void pop(Index i)
Remove the topmost WSV from its stack.
Definition: workspace_ng.cc:92
void claim_agenda_ownership()
Definition: workspace_ng.cc:75
T * get(const char *name)
Retrieve a value ptr if it exist (FIXME: C++20 allows const char* as template argument)
Definition: workspace_ng.h:174
unordered_map< String, Index > WsvMap_type
Definition: workspace_ng.h:61
bool is_initialized(Index i) const
Checks existence of the given WSV.
Workspace(Workspace &&) noexcept=default
Allow move construction of this object in public.
wsv_data_type wsvs(const ArrayOfIndex &) const
Workspace * original_workspace
Definition: workspace_ng.h:64
void duplicate(Index i)
Duplicate WSV.
Definition: workspace_ng.cc:39
Index nelem() const
Get the number of workspace variables.
Definition: workspace_ng.h:164
Array< WorkspaceVariable > ws
Workspace variable container.
Definition: workspace_ng.h:56
Workspace()
Construct a new workspace.
std::shared_ptr< Workspace > deepcopy()
Gets a full copy that owns all the data (only gets the top of the stack)
std::shared_ptr< WsvMap_type > WsvMap_ptr
Definition: workspace_ng.h:62
Array< WsvRecord > wsv_data_type
Definition: workspace_ng.h:58
Index depth(Index i) const
Return scoping level of the given WSV.
void set_empty(Index i)
Delete WSV.
Definition: workspace_ng.cc:32
std::shared_ptr< void > operator[](Index i)
Retrieve a pointer to the given WSV.
WorkspaceBorrowGuard borrow(Index i, T &wsv)
Push a new WSV onto its stack.
Definition: workspace_ng.h:120
void push_move(Index i, std::shared_ptr< T > &&wsv_ptr)
Move a WSV onto its stack.
Definition: workspace_ng.h:156
This class contains all static information for one workspace variable.
Definition: wsv_aux.h:41
~WorkspaceBorrowGuard() noexcept
Definition: workspace_ng.h:29
WorkspaceVariable & wsv
Definition: workspace_ng.h:28
std::shared_ptr< void > wsv
Definition: workspace_ng.h:21
#define a
std::shared_ptr< T > get_shallow_copy(const T &x)
Definition: workspace_ng.h:215
stack< WorkspaceVariableStruct, std::vector< WorkspaceVariableStruct > > WorkspaceVariable
Definition: workspace_ng.h:25
Auxiliary header stuff related to workspace variable groups.