ARTS  2.2.66
workspace_ng.cc
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 
19 // File description
21 
31 #include "workspace_ng.h"
32 #include "wsv_aux.h"
33 #include "auto_workspace.h"
34 
36 
38 
39 map<String, Index> Workspace::WsvMap;
40 
42 
46 #ifndef NDEBUG
47 , context("")
48 #endif
49 {
50 }
51 
52 
54 {
55  for ( Index i=0 ; i<wsv_data.nelem() ; ++i )
56  {
57  WsvMap[wsv_data[i].Name()] = i;
58  }
59 }
60 
61 
63 {
64  Workspace::wsv_data.push_back(wsv);
66  return wsv_data.nelem()-1;
67 }
68 
69 
71 
77 {
78  WsvStruct *wsvs = ws[i].top ();
79 
80  if (wsvs &&& wsvs->wsv)
81  {
82  wsmh.deallocate (wsv_data[i].Group(), wsvs->wsv);
83  wsvs->wsv = NULL;
84  wsvs->auto_allocated = false;
85  wsvs->initialized = false;
86  }
87 }
88 
89 
91 
97 {
98  WsvStruct *wsvs = new WsvStruct;
99 
100  wsvs->auto_allocated = true;
101  if (ws[i].size() && ws[i].top()->wsv)
102  {
103  wsvs->wsv = wsmh.duplicate (wsv_data[i].Group(), ws[i].top()->wsv);
104  wsvs->initialized = true;
105  }
106  else
107  {
108  wsvs->wsv = NULL;
109  wsvs->initialized = false;
110  }
111  ws[i].push (wsvs);
112 }
113 
114 
116 {
117  ws.resize (wsv_data.nelem());
118 }
119 
120 
122 
131 Workspace::Workspace (const Workspace& workspace) : ws(workspace.ws.nelem())
132 {
133 #ifndef NDEBUG
134  context = workspace.context;
135 #endif
136  for (Index i=0; i < workspace.ws.nelem(); i++)
137  {
138  WsvStruct *wsvs = new WsvStruct;
139  wsvs->auto_allocated = false;
140  if (workspace.ws[i].size() && workspace.ws[i].top()->wsv)
141  {
142  wsvs->wsv = workspace.ws[i].top()->wsv;
143  wsvs->initialized = workspace.ws[i].top()->initialized;
144  }
145  else
146  {
147  wsvs->wsv = NULL;
148  wsvs->initialized = false;
149  }
150  ws[i].push (wsvs);
151  }
152 }
153 
154 
156 
160 {
161 #ifndef NDEBUG
162 #pragma omp critical (ws_destruct)
163  if (context != "") cout << "WS destruct: " << context << endl;
164 #endif
165  for (int i = 0; i < ws.nelem (); i++)
166  {
167  WsvStruct *wsvs;
168 
169  while (ws[i].size ())
170  {
171  wsvs = ws[i].top ();
172  if (wsvs->auto_allocated && wsvs->wsv)
173  {
174  wsmh.deallocate (wsv_data[i].Group(), wsvs->wsv);
175  }
176  delete (wsvs);
177  ws[i].pop ();
178  }
179  }
180  ws.empty ();
181 }
182 
183 
185 
192 {
193  WsvStruct *wsvs = ws[i].top ();
194  void *vp = NULL;
195  if (wsvs)
196  {
197  vp = wsvs->wsv;
198  delete wsvs;
199  ws[i].pop ();
200  }
201  return vp;
202 }
203 
204 
206 
212 {
213  WsvStruct *wsvs = ws[i].top ();
214 
215  if (wsvs)
216  {
217  if (wsvs->wsv)
218  wsmh.deallocate (wsv_data[i].Group(), wsvs->wsv);
219 
220  delete wsvs;
221  ws[i].pop ();
222  }
223 }
224 
225 
227 
233 void Workspace::push (Index i, void *wsv)
234 {
235  WsvStruct *wsvs = new WsvStruct;
236  wsvs->auto_allocated = false;
237  wsvs->initialized = true;
238  wsvs->wsv = wsv;
239  ws[i].push (wsvs);
240 }
241 
242 
244 
253 {
254  WsvStruct *wsvs = new WsvStruct;
255  wsvs->auto_allocated = false;
256  wsvs->initialized = false;
257  wsvs->wsv = wsv;
258  ws[i].push (wsvs);
259 }
260 
261 
263 
270 {
271  if (!ws[i].size ())
272  push (i, NULL);
273 
274  if (!ws[i].top ()->wsv)
275  {
276  ws[i].top ()->auto_allocated = true;
277  ws[i].top ()->wsv = wsmh.allocate (wsv_data[i].Group());
278  }
279 
280  ws[i].top ()->initialized = true;
281 
282  return (ws[i].top()->wsv);
283 }
284 
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
static Array< WsvRecord > wsv_data
Definition: workspace_ng.h:64
Index nelem() const
Number of elements.
Definition: array.h:176
void initialize()
void duplicate(Index i)
Duplicate WSV.
Definition: workspace_ng.cc:96
Defines the enum type that acts as a handle for workspace variables groups.
void pop_free(Index i)
Pop the topmost wsv from its stack and free its memory.
void * allocate(Index wsvg)
Getaway function to call the allocation function for the WSV group with the given Index...
This file contains the declaration and partly the implementation of the workspace class...
String context
Only for debugging.
Definition: workspace_ng.h:61
virtual ~Workspace()
Destruct the workspace.
void * operator[](Index i)
Retrieve pointer to the given WSV.
Index nelem()
Definition: workspace_ng.h:96
void * pop(Index i)
Pop the topmost wsv from its stack.
WorkspaceMemoryHandler wsmh
Definition: workspace_ng.cc:35
Workspace()
Construct a new workspace.
Definition: workspace_ng.cc:45
void deallocate(Index wsvg, void *vp)
Getaway function to call the deallocation function for the WSV group with the given Index...
void push(Index i, void *wsv)
Push a new wsv onto its stack.
static map< String, Index > WsvMap
Definition: workspace_ng.h:67
static Index add_wsv(const WsvRecord &wsv)
Definition: workspace_ng.cc:62
void push_uninitialized(Index i, void *wsv)
Push a new wsv onto its stack but mark it as uninitialized.
Workspace class.
Definition: workspace_ng.h:47
static void define_wsv_map()
Definition: workspace_ng.cc:53
Array< stack< WsvStruct * > > ws
Workspace variable container.
Definition: workspace_ng.h:56
void * duplicate(Index wsvg, void *vp)
Getaway function to call the duplication function for the WSV group with the given Index...
This class contains all static information for one workspace variable.
Definition: wsv_aux.h:54
void del(Index i)
Delete WSV.
Definition: workspace_ng.cc:76
Auxiliary header stuff related to workspace variable groups.