ARTS  1.0.222
make_auto_md_cc.cc
Go to the documentation of this file.
1 /* Copyright (C) 2000, 2001 Stefan Buehler <sbuehler@uni-bremen.de>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  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 
18 #include "arts.h"
19 #include "token.h"
20 #include "array.h"
21 #include "file.h"
22 #include "auto_wsv.h"
23 #include "methods.h"
24 #include "wsv_aux.h"
25 
26 /* Adds commas and indentation to parameter lists. */
27 void align(ofstream& ofs, bool& is_first_parameter, const String& indent)
28 {
29  // Add comma and line break, if not first element:
30  if (is_first_parameter)
31  is_first_parameter = false;
32  else
33  {
34  ofs << ",\n";
35  // Make proper indentation:
36  ofs << indent;
37  }
38 }
39 
40 int main()
41 {
42  try
43  {
44  // Make the global data visible:
45  extern Array<MdRecord> md_data;
46  extern const ArrayOfString wsv_group_names;
47  extern const Array<WsvRecord> wsv_data;
48 
49  // Initialize method data.
51 
52  // Initialize the wsv group name array:
54 
55  // Initialize wsv data.
57 
58 
59  const Index n_md = md_data.nelem();
60  const Index n_wsv = wsv_data.nelem();
61 
62  // For safety, check if n_wsv and N_WSV have the same value. If not,
63  // then the file wsv.h is not up to date.
64  if (N_WSV != n_wsv)
65  {
66  cout << "The file wsv.h is not up to date!\n";
67  cout << "(N_WSV = " << N_WSV << ", n_wsv = " << n_wsv << ")\n";
68  cout << "Make wsv.h first. Check if Makefile is correct.\n";
69  return 1;
70  }
71 
72  // Write auto_md.cc:
73  // -----------
74  ofstream ofs;
75  open_output_file(ofs,"auto_md.cc");
76 
77  ofs << "// This file was generated automatically by make_auto_md_cc.cc.\n";
78  ofs << "// DO NOT EDIT !\n";
79  ofs << "// Generated: "
80  << __DATE__ << ", "
81  << __TIME__ << "\n\n";
82 
83  ofs << "#include \"arts.h\"\n"
84  << "#include \"make_array.h\"\n"
85  << "#include \"auto_md.h\"\n"
86  << "#include \"auto_wsv_groups.h\"\n"
87  << "#include \"wsv_aux.h\"\n"
88  << "\n";
89 
90  // Declare wsv_data:
91  ofs << "// The workspace variable pointers:\n"
92  << "extern const Array<WsvP*> wsv_pointers;\n\n"
93 
94  << "// Other wsv data:\n"
95  << "extern const Array<WsvRecord> wsv_data;\n\n";
96 
97 
98  // Write all get-away functions:
99  // -----------------------------
100  for (Index i=0; i<n_md; ++i)
101  {
102  // This is needed to flag the first function parameter, which
103  // needs no line break before being written:
104  bool is_first_parameter = true;
105  // The String indent is needed to achieve the correct
106  // indentation of the functin parameters:
107  String indent = String(md_data[i].Name().nelem()+3,' ');;
108 
109  // There are four lists of parameters that we have to
110  // write.
111  ArrayOfIndex vo=md_data[i].Output(); // Output
112  ArrayOfIndex vi=md_data[i].Input(); // Input
113  ArrayOfIndex vgo=md_data[i].GOutput(); // Generic Output
114  ArrayOfIndex vgi=md_data[i].GInput(); // Generic Input
115  // vo and vi contain handles of workspace variables,
116  // vgo and vgi handles of workspace variable groups.
117 
118  // Check, if some workspace variables are in both the
119  // input and the output list, and erase those from the input
120  // list:
121  for (ArrayOfIndex::const_iterator j=vo.begin(); j<vo.end(); ++j)
122  for (ArrayOfIndex::iterator k=vi.begin(); k<vi.end(); ++k)
123  if ( *j == *k )
124  {
125  // erase_vector_element(vi,k);
126  k = vi.erase(k) - 1;
127  // We need the -1 here, otherwise due to the
128  // following increment we would miss the element
129  // behind the erased one, which is now at the
130  // position of the erased one.
131  }
132 
133  // There used to be a similar block here for the generic
134  // input/output variables. However, this was a mistake. For
135  // example, if a method has a vector as generic input and a
136  // vector as generic output, this does not mean that it is
137  // the same vector!
138 
139  {
140 
141  String ws, mr;
142 
143  // Use parameter name only if it is used inside the function
144  // to avoid warnings
145  if (vo.nelem () || vi.nelem ())
146  {
147  ws = " ws";
148  }
149 
150  // Use parameter name only if it is used inside the function
151  // to avoid warnings
152  if ( vgo.nelem () || vgi.nelem ()
153  || md_data[i].Keywords().nelem())
154  {
155  mr = " mr";
156  }
157 
158  ofs << "void " << md_data[i].Name()
159  << "_g(WorkSpace&" << ws
160  << ", const MRecord&" << mr << ")\n"
161  << "{\n";
162  }
163 
164 
165 
166  // Define generic output pointers
167  for (Index j=0; j<vgo.nelem(); ++j)
168  {
169  ofs << " " << wsv_group_names[md_data[i].GOutput()[j]]
170  << " *GO" << j << " = *wsv_pointers[mr.Output()[" << j
171  << "]];\n";
172  }
173 
174  // Define generic input pointers
175  for (Index j=0; j<vgi.nelem(); ++j)
176  {
177  ofs << " " << wsv_group_names[md_data[i].GInput()[j]]
178  << " *GI" << j << " = *wsv_pointers[mr.Input()[" << j
179  << "]];\n";
180  }
181 
182  ofs << " " << md_data[i].Name() << "(";
183 
184  // Write the Output workspace variables:
185  for (Index j=0; j<vo.nelem(); ++j)
186  {
187  // Add comma and line break, if not first element:
188  align(ofs,is_first_parameter,indent);
189 
190  ofs << "ws." << wsv_data[vo[j]].Name();
191  }
192 
193  // Write the Generic output workspace variables:
194  for (Index j=0; j<vgo.nelem(); ++j)
195  {
196  // Add comma and line break, if not first element:
197  align(ofs,is_first_parameter,indent);
198 
199  ofs << "*GO" << j;
200  }
201 
202  // Write the Generic output workspace variable names:
203  for (Index j=0; j<vgo.nelem(); ++j)
204  {
205  // Add comma and line break, if not first element:
206  align(ofs,is_first_parameter,indent);
207 
208  ofs << "wsv_data[mr.Output()["
209  << j
210  << "]].Name()";
211  }
212 
213  // Write the Input workspace variables:
214  for (Index j=0; j<vi.nelem(); ++j)
215  {
216  // Add comma and line break, if not first element:
217  align(ofs,is_first_parameter,indent);
218 
219  ofs << "ws." << wsv_data[vi[j]].Name();
220  }
221 
222  // Write the Generic input workspace variables:
223  for (Index j=0; j<vgi.nelem(); ++j)
224  {
225  // Add comma and line break, if not first element:
226  align(ofs,is_first_parameter,indent);
227 
228  ofs << "*GI" << j;
229  }
230 
231  // Write the Generic input workspace variable names:
232  for (Index j=0; j<vgi.nelem(); ++j)
233  {
234  // Add comma and line break, if not first element:
235  align(ofs,is_first_parameter,indent);
236 
237  ofs << "wsv_data[mr.Input()["
238  << j
239  << "]].Name()";
240  }
241 
242  // Write the control parameters:
243  {
244  // The mr parameters look all the same (mr[i]), so we just
245  // need to know the number of them:
246  Index n_mr = md_data[i].Keywords().nelem();
247 
248  for (Index j=0; j!=n_mr; ++j)
249  {
250  // Add comma and line break, if not first element:
251  align(ofs,is_first_parameter,indent);
252 
253  ofs << "mr.Values()[" << j << "]";
254  }
255  }
256 
257  ofs << ");\n";
258  ofs << "}\n\n";
259  }
260 
261  // Add getaways, the array that hold pointers to the getaway functions:
262  {
263  String indent = " ";
264  bool is_first_parameter = true;
265 
266  ofs << "// The array holding the pointers to the getaway functions.\n"
267  << "void (*getaways[])(WorkSpace&, const MRecord&)\n"
268  << " = {";
269  for (Index i=0; i<n_md; ++i)
270  {
271  // Add comma and line break, if not first element:
272  align(ofs,is_first_parameter,indent);
273 
274  ofs << md_data[i].Name() << "_g";
275  }
276  ofs << "};\n\n";
277  }
278 
279  }
280  catch (exception x)
281  {
282  cout << "Something went wrong. Message text:\n";
283  cout << x.what() << '\n';
284  return 1;
285  }
286 
287  return 0;
288 }
main
int main()
Definition: make_auto_md_cc.cc:40
define_wsv_data
void define_wsv_data()
Define the lookup data for the workspace variables.
Definition: workspace.cc:44
wsv_data
const Array< WsvRecord > wsv_data
Definition: workspace.cc:42
align
void align(ofstream &ofs, bool &is_first_parameter, const String &indent)
Definition: make_auto_md_cc.cc:27
array.h
This file contains the definition of Array.
Array
This can be used to make arrays out of anything.
Definition: array.h:48
define_md_data
void define_md_data()
Define the lookup data for the workspace methods.
Definition: methods.cc:42
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:61
wsv_group_names
ArrayOfString wsv_group_names
Definition: groups.cc:36
define_wsv_group_names
void define_wsv_group_names()
Define the array of workspace variable group names.
Definition: groups.cc:51
open_output_file
void open_output_file(ofstream &file, const String &name)
Open a file for writing.
Definition: file.cc:91
md_data
Array< MdRecord > md_data
The lookup information for the workspace methods.
Definition: globals_2.cc:53
N_WSV
#define N_WSV
Definition: auto_wsv.h:19
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: arts.h:153
String
my_basic_string< char > String
The String type for ARTS.
Definition: mystring.h:281
file.h
This file contains basic functions to handle ASCII and binary (HDF) data files.
token.h
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:115
methods.h
Declaration of the class MdRecord.
wsv_aux.h
Auxiliary header stuff related to workspace variable groups.
auto_wsv.h
Declares the enum type that acts as a handle for workspace variables. Also declares the workspace its...
arts.h
The global header file for ARTS.