ARTS 2.5.4 (git: 31ce4f0e)
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> {
54 public:
57
58 std::shared_ptr<Array<WsvRecord>> wsv_data_ptr;
59
60 std::shared_ptr<map<String, Index>> WsvMap_ptr;
61
63
68 Workspace();
69
77 Workspace(const Workspace &workspace);
78
85 void set_empty(Index i);
86
94 void duplicate(Index i);
95
101 [[nodiscard]] bool is_initialized(Index i) const;
102
104 [[nodiscard]] Index depth(Index i) const;
105
112 void pop(Index i);
113
114 void emplace(Index);
115
123 template <typename T>
125 using U = std::remove_cv_t<T>;
126 std::shared_ptr<U> wsv_ptr(const_cast<U *>(&wsv), [](U *) {});
128 wsvs.initialized = true;
129 wsvs.wsv = wsv_ptr;
130 ws[i].push(wsvs);
131 return {ws[i]};
132 }
133
141 template <typename T>
143 using U = std::remove_cv_t<T>;
144 std::shared_ptr<U> wsv_ptr(const_cast<U *>(&wsv), [](U *) {});
146 wsvs.initialized = false;
147 wsvs.wsv = wsv_ptr;
148 ws[i].push(wsvs);
149 return {ws[i]};
150 }
151
159 template <typename T>
160 void push_move(Index i, std::shared_ptr<T> &&wsv_ptr) {
162 wsvs.initialized = true;
163 wsvs.wsv = std::forward<std::shared_ptr<T>>(wsv_ptr);
164 ws[i].push(wsvs);
165 }
166
168 [[nodiscard]] Index nelem() const { return ws.nelem(); }
169
171 Index add_wsv(const WsvRecord &wsv);
172
174 std::shared_ptr<void> operator[](Index i);
175
177 template <class T>
178 T* get(const char *name) {
179 if (const Index pos = WsvMap_ptr -> at(name); is_initialized(pos)) {
180 return static_cast<T*>(ws[pos].top().wsv.get());
181 }
182 return nullptr;
183 }
184
186 void swap(Workspace &other) noexcept;
187
196 template <typename OutputStream>
197 void PrintWsvName(OutputStream &outstream, Index i) const {
198 outstream << (*wsv_data_ptr)[i].Name() << "(" << i << ") ";
199 }
200
201 std::shared_ptr<Workspace> shared_ptr() {return shared_from_this();}
202};
203
204template <class T>
208 std::shared_ptr<T> copy;
209
210 public:
212 : orig(ws),
214 copy(nullptr) {}
215
216 OmpParallelCopyGuard(T &ws, bool do_copy_manually)
217 : orig(ws), do_copy(do_copy_manually), copy(nullptr) {}
218
220 : orig(cp.orig),
221 do_copy(cp.do_copy),
222 copy(do_copy ? new T{orig} : nullptr) {}
223
224 operator T &() { return copy ? *copy : orig; }
225
226 operator const T &() const { return copy ? *copy : orig; }
227};
228
230
231#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.
Index nelem() const ARTS_NOEXCEPT
Definition: array.h:92
OmpParallelCopyGuard(T &ws, bool do_copy_manually)
Definition: workspace_ng.h:216
std::shared_ptr< T > copy
Definition: workspace_ng.h:208
OmpParallelCopyGuard(const OmpParallelCopyGuard &cp)
Definition: workspace_ng.h:219
Workspace class.
Definition: workspace_ng.h:53
void swap(Workspace &other) noexcept
Swap with another workspace.
Definition: workspace_ng.cc:93
Index add_wsv(const WsvRecord &wsv)
Add a new variable to this workspace.
Definition: workspace_ng.cc:41
void emplace(Index)
WorkspaceBorrowGuard borrow_uninitialized(Index i, T &wsv)
Put a new WSV onto its stack.
Definition: workspace_ng.h:142
void PrintWsvName(OutputStream &outstream, Index i) const
Print WSV name to output stream.
Definition: workspace_ng.h:197
void pop(Index i)
Remove the topmost WSV from its stack.
Definition: workspace_ng.cc:90
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:178
bool is_initialized(Index i) const
Checks existence of the given WSV.
Definition: workspace_ng.cc:97
std::shared_ptr< map< String, Index > > WsvMap_ptr
Definition: workspace_ng.h:60
Workspace * original_workspace
Definition: workspace_ng.h:62
void duplicate(Index i)
Duplicate WSV.
Definition: workspace_ng.cc:55
Index nelem() const
Get the number of workspace variables.
Definition: workspace_ng.h:168
Array< WorkspaceVariable > ws
Workspace variable container.
Definition: workspace_ng.h:56
Workspace()
Construct a new workspace.
Index depth(Index i) const
Return scoping level of the given WSV.
std::shared_ptr< Array< WsvRecord > > wsv_data_ptr
Definition: workspace_ng.h:58
void set_empty(Index i)
Delete WSV.
Definition: workspace_ng.cc:48
std::shared_ptr< Workspace > shared_ptr()
Definition: workspace_ng.h:201
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:124
void push_move(Index i, std::shared_ptr< T > &&wsv_ptr)
Move a WSV onto its stack.
Definition: workspace_ng.h:160
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
stack< WorkspaceVariableStruct, std::vector< WorkspaceVariableStruct > > WorkspaceVariable
Definition: workspace_ng.h:42
Auxiliary header stuff related to workspace variable groups.