ARTS 2.5.10 (git: 2f1c442c)
workspace_ng.h
Go to the documentation of this file.
1/* Copyright (C) 2004-2012 Oliver Lemke <olemke@core-dump.info>
2
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
16 USA. */
17
25#ifndef WORKSPACE_NG_INCLUDED
26#define WORKSPACE_NG_INCLUDED
27
28#include <map>
29#include <memory>
30#include <stack>
31#include <vector>
32
33#include "array.h"
34#include "arts_omp.h"
35#include "wsv_aux.h"
36
38 std::shared_ptr<void> wsv;
40};
41
42using WorkspaceVariable = stack<WorkspaceVariableStruct, std::vector<WorkspaceVariableStruct>>;
43
46 ~WorkspaceBorrowGuard() noexcept { wsv.pop(); };
47};
48
53class Workspace final : public std::enable_shared_from_this<Workspace> {
58 Workspace();
59
67 Workspace(const Workspace &workspace);
68
70
71 public:
74
76 std::shared_ptr<wsv_data_type> wsv_data_ptr;
77
78 using WsvMap_type = map<String, Index>;
79 std::shared_ptr<WsvMap_type> WsvMap_ptr;
80
82
84 [[nodiscard]] static std::shared_ptr<Workspace> create();
85
87 [[nodiscard]] std::shared_ptr<Workspace> shallowcopy() const;
88
90 Workspace(Workspace&&) noexcept = default;
91
98 void set_empty(Index i);
99
107 void duplicate(Index i);
108
114 [[nodiscard]] bool is_initialized(Index i) const;
115
117 [[nodiscard]] Index depth(Index i) const;
118
125 void pop(Index i);
126
127 void emplace(Index);
128
136 template <typename T>
138 using U = std::remove_cv_t<T>;
139 std::shared_ptr<U> wsv_ptr(const_cast<U *>(&wsv), [](U *) {});
141 wsvs.initialized = true;
142 wsvs.wsv = wsv_ptr;
143 ws[i].push(wsvs);
144 return {ws[i]};
145 }
146
154 template <typename T>
156 using U = std::remove_cv_t<T>;
157 std::shared_ptr<U> wsv_ptr(const_cast<U *>(&wsv), [](U *) {});
159 wsvs.initialized = false;
160 wsvs.wsv = wsv_ptr;
161 ws[i].push(wsvs);
162 return {ws[i]};
163 }
164
172 template <typename T>
173 void push_move(Index i, std::shared_ptr<T> &&wsv_ptr) {
175 wsvs.initialized = true;
176 wsvs.wsv = std::forward<std::shared_ptr<T>>(wsv_ptr);
177 ws[i].push(std::move(wsvs));
178 }
179
181 [[nodiscard]] Index nelem() const { return ws.nelem(); }
182
184 Index add_wsv(const WsvRecord &wsv);
185
187 std::shared_ptr<void> operator[](Index i);
188
190 template <class T>
191 T* get(const char *name) {
192 if (const Index pos = WsvMap_ptr -> at(name); is_initialized(pos)) {
193 return static_cast<T*>(ws[pos].top().wsv.get());
194 }
195 return nullptr;
196 }
197
199 void swap(Workspace &other) noexcept;
200
209 template <typename OutputStream>
210 void PrintWsvName(OutputStream &outstream, Index i) const {
211 outstream << (*wsv_data_ptr)[i].Name() << "(" << i << ") ";
212 }
213
215 std::shared_ptr<Workspace> deepcopy();
216
217 wsv_data_type wsvs(const ArrayOfIndex&) const;
219};
220
221template <typename T>
222concept CopyConstructor = requires(const T& a) {{ T{a} };};
223
224template <typename T>
225concept ShallowCopyConstructor = requires(const T& a) {a.shallowcopy();};
226
227template <typename T>
229
230
231template <typename T>
232std::shared_ptr<T> get_shallow_copy(const T& x) {
233 if constexpr (ShallowCopyConstructor<T>) return x.shallowcopy();
234 else { T mout{x}; return std::make_shared<T>(std::move(mout)); }
235}
236
237template <CanCopy T>
241 std::shared_ptr<T> copy;
242
243 public:
245 : orig(ws),
247 copy(nullptr) {}
248
249 OmpParallelCopyGuard(T &ws, bool do_copy_manually)
250 : orig(ws), do_copy(do_copy_manually), copy(nullptr) {}
251
253 : orig(cp.orig),
254 do_copy(cp.do_copy),
255 copy(do_copy ? get_shallow_copy(orig) : nullptr) {}
256
257 operator T &() { return copy ? *copy : orig; }
258
259 operator const T &() const { return copy ? *copy : orig; }
260};
261
263
264#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:46
bool arts_omp_in_parallel()
Wrapper for omp_in_parallel.
Definition: arts_omp.cc:62
Header file for helper functions for OpenMP.
This can be used to make arrays out of anything.
Definition: array.h:48
Index nelem() const ARTS_NOEXCEPT
Definition: array.h:92
OmpParallelCopyGuard(T &ws, bool do_copy_manually)
Definition: workspace_ng.h:249
std::shared_ptr< T > copy
Definition: workspace_ng.h:241
OmpParallelCopyGuard(const OmpParallelCopyGuard &cp)
Definition: workspace_ng.h:252
Workspace class.
Definition: workspace_ng.h:53
std::shared_ptr< wsv_data_type > wsv_data_ptr
Definition: workspace_ng.h:76
void swap(Workspace &other) noexcept
Swap with another workspace.
Index add_wsv(const WsvRecord &wsv)
Add a new variable to this workspace.
Definition: workspace_ng.cc:42
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:155
void PrintWsvName(OutputStream &outstream, Index i) const
Print WSV name to output stream.
Definition: workspace_ng.h:210
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.
void claim_agenda_ownership()
Definition: workspace_ng.cc:92
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:191
bool is_initialized(Index i) const
Checks existence of the given WSV.
Workspace(Workspace &&) noexcept=default
Allow move construction of this object in public.
map< String, Index > WsvMap_type
Definition: workspace_ng.h:78
wsv_data_type wsvs(const ArrayOfIndex &) const
Workspace * original_workspace
Definition: workspace_ng.h:81
void duplicate(Index i)
Duplicate WSV.
Definition: workspace_ng.cc:56
Index nelem() const
Get the number of workspace variables.
Definition: workspace_ng.h:181
Array< WorkspaceVariable > ws
Workspace variable container.
Definition: workspace_ng.h:73
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:79
Array< WsvRecord > wsv_data_type
Definition: workspace_ng.h:75
Index depth(Index i) const
Return scoping level of the given WSV.
void set_empty(Index i)
Delete WSV.
Definition: workspace_ng.cc:49
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:137
void push_move(Index i, std::shared_ptr< T > &&wsv_ptr)
Move a WSV onto its stack.
Definition: workspace_ng.h:173
This class contains all static information for one workspace variable.
Definition: wsv_aux.h:58
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
~WorkspaceBorrowGuard() noexcept
Definition: workspace_ng.h:46
WorkspaceVariable & wsv
Definition: workspace_ng.h:45
std::shared_ptr< void > wsv
Definition: workspace_ng.h:38
#define a
std::shared_ptr< T > get_shallow_copy(const T &x)
Definition: workspace_ng.h:232
stack< WorkspaceVariableStruct, std::vector< WorkspaceVariableStruct > > WorkspaceVariable
Definition: workspace_ng.h:42
Auxiliary header stuff related to workspace variable groups.