00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "arts.h"
00019 #include "array.h"
00020 #include "file.h"
00021 #include "methods.h"
00022 #include "workspace_ng.h"
00023 #include "agenda_record.h"
00024
00025
00026 void align(ofstream& ofs, bool& is_first_parameter, const String& indent)
00027 {
00028
00029 if (is_first_parameter)
00030 is_first_parameter = false;
00031 else
00032 {
00033 ofs << ",\n";
00034
00035 ofs << indent;
00036 }
00037 }
00038
00039 int main()
00040 {
00041 try
00042 {
00043
00044 extern Array<MdRecord> md_data;
00045 extern const ArrayOfString wsv_group_names;
00046 const Array<WsvRecord>& wsv_data = Workspace::wsv_data;
00047
00048
00049 define_wsv_group_names();
00050
00051
00052 Workspace::define_wsv_data();
00053
00054
00055 Workspace::define_wsv_map();
00056
00057
00058 define_md_data_raw();
00059
00060
00061 expand_md_data_raw_to_md_data();
00062
00063
00064 const Index n_md = md_data.nelem();
00065
00066
00067
00068 ofstream ofs;
00069 open_output_file(ofs,"auto_md.cc");
00070
00071 ofs << "// This file was generated automatically by make_auto_md_cc.cc.\n";
00072 ofs << "// DO NOT EDIT !\n";
00073 ofs << "// Generated: "
00074 << __DATE__ << ", "
00075 << __TIME__ << "\n\n";
00076
00077 ofs << "#include \"arts.h\"\n"
00078 << "#include \"make_array.h\"\n"
00079 << "#include \"auto_md.h\"\n"
00080 << "#include \"wsv_aux.h\"\n"
00081 << "#include \"mc_interp.h\"\n"
00082 << "#include \"m_append.h\"\n"
00083 << "#include \"m_delete.h\"\n"
00084 << "#include \"m_copy.h\"\n"
00085 << "#include \"m_extract.h\"\n"
00086 << "#include \"m_general.h\"\n"
00087 << "#include \"m_ignore.h\"\n"
00088 << "#include \"m_nc.h\"\n"
00089 << "#include \"m_select.h\"\n"
00090 << "#include \"m_xml.h\"\n"
00091 << "#include \"m_basic_types.h\"\n"
00092 << "#include \"agenda_record.h\"\n"
00093 << "#include \"workspace_ng.h\"\n"
00094 << "\n";
00095
00096
00097
00098
00099
00100 for (Index i=0; i<n_md; ++i)
00101 {
00102 const MdRecord& mdd = md_data[i];
00103
00104
00105
00106 bool is_first_parameter = true;
00107
00108
00109 String indent = String(mdd.Name().nelem()+3,' ');;
00110
00111
00112 bool pass_workspace = false;
00113
00114
00115
00116 ArrayOfIndex vo=mdd.Out();
00117 const ArrayOfIndex& vi = mdd.InOnly();
00118 ArrayOfIndex vgo=mdd.GOutType();
00119 ArrayOfIndex vgi=mdd.GInType();
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 {
00130
00131 String ws, mr;
00132
00133
00134
00135 ws = " ws";
00136 if (!mdd.AgendaMethod() && !mdd.PassWorkspace() && !vo.nelem () && !vi.nelem () && !vgo.nelem () && !vgi.nelem ())
00137 {
00138 ws = "";
00139 }
00140
00141
00142
00143 for (Index j = 0; !pass_workspace && j < mdd.In().nelem(); j++)
00144 {
00145 if (wsv_data[mdd.In()[j]].Group() == get_wsv_group_id ("Agenda"))
00146 {
00147 pass_workspace = true;
00148 }
00149 }
00150
00151
00152
00153 for (Index j = 0; !pass_workspace && j < mdd.GInType().nelem(); j++)
00154 {
00155 if (mdd.GInType()[j] == get_wsv_group_id ("Agenda"))
00156 {
00157 pass_workspace = true;
00158 }
00159 }
00160
00161
00162
00163 if ( vo.nelem () || vi.nelem () || vgo.nelem () || vgi.nelem ()
00164 || mdd.AgendaMethod())
00165 {
00166 mr = " mr";
00167 }
00168
00169 if ( mdd.Supergeneric() )
00170 {
00171 ofs << "void " << mdd.Name()
00172 << "_sg_" << mdd.ActualGroups()
00173 << "_g(Workspace&" << ws
00174 << ", const MRecord&" << mr << ")\n"
00175 << "{\n";
00176 }
00177 else
00178 {
00179 ofs << "void " << mdd.Name()
00180 << "_g(Workspace&" << ws
00181 << ", const MRecord&" << mr << ")\n"
00182 << "{\n";
00183 }
00184 }
00185
00186 ofs << " " << mdd.Name() << "(";
00187
00188 if (pass_workspace || mdd.PassWorkspace() || mdd.AgendaMethod())
00189 {
00190 ofs << "ws";
00191 is_first_parameter = false;
00192 }
00193
00194
00195 for (Index j=0; j<vo.nelem(); ++j)
00196 {
00197
00198
00199
00200
00201 assert( wsv_data[vo[j]].Group() < wsv_group_names.nelem() );
00202
00203
00204 align(ofs,is_first_parameter,indent);
00205
00206 ofs << "*(("
00207 << wsv_group_names[wsv_data[vo[j]].Group()]
00208 << " *)ws[mr.Out()[" << j
00209 << "]])";
00210 }
00211
00212
00213 for (Index j=0; j<vgo.nelem(); ++j)
00214 {
00215
00216
00217
00218
00219 assert( vgo[j] < wsv_group_names.nelem() );
00220
00221
00222 align(ofs,is_first_parameter,indent);
00223
00224 ofs << "*((" << wsv_group_names[vgo[j]]
00225 << " *)ws[mr.Out()[" << j+vo.nelem()
00226 << "]])";
00227 }
00228
00229
00230 if (mdd.PassWsvNames())
00231 {
00232 for (Index j=0; j<vgo.nelem(); ++j)
00233 {
00234
00235 align(ofs,is_first_parameter,indent);
00236
00237 ofs << "Workspace::wsv_data[mr.Out()["
00238 << j+vo.nelem()
00239 << "]].Name()";
00240 }
00241 }
00242
00243
00244 for (Index j=0; j<vi.nelem(); ++j)
00245 {
00246
00247 align(ofs,is_first_parameter,indent);
00248
00249 if (wsv_data[vi[j]].Group() == get_wsv_group_id("Agenda"))
00250 {
00251 ofs << "*(("
00252 << wsv_group_names[wsv_data[vi[j]].Group()]
00253 << " *)ws[mr.In()[" << j
00254 << "]])";
00255 }
00256 else
00257 {
00258 ofs << "*(("
00259 << wsv_group_names[wsv_data[vi[j]].Group()]
00260 << " *)ws[mr.In()[" << j
00261 << "]])";
00262 }
00263 }
00264
00265
00266 {
00267 if (mdd.SetMethod())
00268 {
00269
00270 align(ofs,is_first_parameter,indent);
00271
00272 ofs << "mr.SetValue()";
00273 }
00274 else
00275 {
00276
00277 for (Index j=0; j<vgi.nelem(); ++j)
00278 {
00279
00280
00281
00282
00283 assert( vgi[j] < wsv_group_names.nelem() );
00284
00285
00286 align(ofs,is_first_parameter,indent);
00287
00288 ofs << "*((" << wsv_group_names[vgi[j]]
00289 << " *)ws[mr.In()[" << j+vi.nelem()
00290 << "]])";
00291 }
00292
00293
00294 if (mdd.PassWsvNames())
00295 {
00296 for (Index j=0; j<vgi.nelem(); ++j)
00297 {
00298
00299 align(ofs,is_first_parameter,indent);
00300
00301 ofs << "Workspace::wsv_data[mr.In()["
00302 << j+vi.nelem()
00303 << "]].Name()";
00304 }
00305 }
00306 }
00307 }
00308
00309
00310 if ( mdd.AgendaMethod() )
00311 {
00312 align(ofs,is_first_parameter,indent);
00313 ofs << "mr.Tasks()";
00314 }
00315
00316 ofs << ");\n";
00317 ofs << "}\n\n";
00318 }
00319
00320
00321 {
00322 String indent = " ";
00323 bool is_first_parameter = true;
00324
00325 ofs << "// The array holding the pointers to the getaway functions.\n"
00326 << "void (*getaways[])(Workspace&, const MRecord&)\n"
00327 << " = {";
00328 for (Index i=0; i<n_md; ++i)
00329 {
00330 const MdRecord& mdd = md_data[i];
00331
00332
00333 align(ofs,is_first_parameter,indent);
00334
00335 if ( mdd.Supergeneric() )
00336 {
00337 ofs << mdd.Name()
00338 << "_sg_" << mdd.ActualGroups()
00339 << "_g";
00340 }
00341 else
00342 {
00343 ofs << mdd.Name() << "_g";
00344 }
00345 }
00346 ofs << "};\n\n";
00347 }
00348
00349
00350
00351
00352 Workspace::define_wsv_map ();
00353 define_agenda_data ();
00354
00355 extern const Array<AgRecord> agenda_data;
00356 for (Index i = 0; i < agenda_data.nelem (); i++)
00357 {
00358 const AgRecord& agr = agenda_data[i];
00359 const ArrayOfIndex& ago = agr.Out();
00360 const ArrayOfIndex& agi = agr.In();
00361 ostringstream ain_push_os, ain_pop_os;
00362 ostringstream aout_push_os, aout_pop_os;
00363
00364 write_agenda_wrapper_header (ofs, agr);
00365
00366 ofs << "\n";
00367 ofs << "{\n";
00368
00369 if (ago.nelem () || agi.nelem ())
00370 {
00371 ofs << " extern map<String, Index> AgendaMap;\n"
00372 << " extern const Array<AgRecord> agenda_data;\n"
00373 << "\n"
00374 << " const AgRecord& agr =\n"
00375 << " agenda_data[AgendaMap.find (input_agenda.name ())->second];\n"
00376 << "\n";
00377 }
00378 if (ago.nelem ())
00379 {
00380 for (Index j = 0; j < ago.nelem (); j++)
00381 {
00382
00383 ArrayOfIndex::const_iterator it = agi.begin ();
00384 while (it != agi.end () && *it != ago[j]) it++;
00385 if (it == agi.end ())
00386 {
00387 aout_push_os << " ws.push_uninitialized (aout[" << j << "], "
00388 << "(void *)&" << wsv_data[ago[j]].Name () << ");\n";
00389 }
00390 else
00391 {
00392 aout_push_os << " ws.push (aout[" << j << "], "
00393 << "(void *)&" << wsv_data[ago[j]].Name () << ");\n";
00394 }
00395 aout_pop_os << " ws.pop (aout[" << j << "]);\n";
00396 }
00397 }
00398 if (agi.nelem ())
00399 {
00400 for (Index j = 0; j < agi.nelem (); j++)
00401 {
00402
00403 ArrayOfIndex::const_iterator it = ago.begin ();
00404 while (it != ago.end () && *it != agi[j]) it++;
00405 if (it == ago.end ())
00406 {
00407 ain_push_os << " ws.push (ain[" << j << "], "
00408 << "(void *)&" << wsv_data[agi[j]].Name () << ");\n";
00409 ain_pop_os << " ws.pop (ain[" << j << "]);\n";
00410 }
00411 }
00412 }
00413
00414 if (aout_push_os.str().length())
00415 {
00416 ofs << " const ArrayOfIndex& aout = agr.Out();\n";
00417 ofs << aout_push_os.str () << "\n";
00418 }
00419 if (ain_push_os.str().length())
00420 {
00421 ofs << " const ArrayOfIndex& ain = agr.In();\n";
00422 ofs << ain_push_os.str () << "\n";
00423 }
00424
00425 ofs << " const ArrayOfIndex& outputs_to_push = input_agenda.get_output2push();\n"
00426 << " const ArrayOfIndex& outputs_to_dup = input_agenda.get_output2dup();\n"
00427 << "\n"
00428 << " for (ArrayOfIndex::const_iterator it = outputs_to_push.begin ();\n"
00429 << " it != outputs_to_push.end (); it++)\n"
00430 << " { ws.push (*it, NULL); }\n"
00431 << "\n"
00432 << " for (ArrayOfIndex::const_iterator it = outputs_to_dup.begin ();\n"
00433 << " it != outputs_to_dup.end (); it++)\n"
00434 << " { ws.duplicate (*it); }\n"
00435 << "\n";
00436
00437 ofs << " String agenda_error_msg;\n"
00438 << " bool agenda_failed = false;\n\n"
00439 << " try {\n"
00440 << " input_agenda.execute (ws);\n"
00441 << " } catch (runtime_error e) {\n"
00442 << " ostringstream os;\n"
00443 << " os << \"Run-time error in agenda: \"\n"
00444 << " << input_agenda.name() << \'\\n\' << e.what();\n"
00445 << " agenda_failed = true;\n"
00446 << " agenda_error_msg = os.str();\n"
00447 << " }\n";
00448
00449 ofs << " for (ArrayOfIndex::const_iterator it = outputs_to_push.begin ();\n"
00450 << " it != outputs_to_push.end (); it++)\n"
00451 << " { ws.pop_free (*it); }\n"
00452 << "\n"
00453 << " for (ArrayOfIndex::const_iterator it = outputs_to_dup.begin ();\n"
00454 << " it != outputs_to_dup.end (); it++)\n"
00455 << " { ws.pop_free (*it); }\n\n";
00456
00457 if (aout_pop_os.str().length())
00458 {
00459 ofs << aout_pop_os.str () << "\n";
00460 }
00461 if (ain_pop_os.str().length())
00462 {
00463 ofs << ain_pop_os.str () << "\n";
00464 }
00465
00466 ofs << " if (agenda_failed) throw runtime_error (agenda_error_msg);\n\n";
00467
00468 ofs << "}\n\n";
00469 }
00470 }
00471 catch (runtime_error x)
00472 {
00473 cout << "Something went wrong. Message text:\n";
00474 cout << x.what() << '\n';
00475 return 1;
00476 }
00477
00478 return 0;
00479 }