TEXT   14

module gen

Guest on 20th July 2022 06:50:47 AM

  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.   string title, TITLE;
  9.   cout << "What would you like your module to be named?\n";
  10.   cin >> title;
  11.  
  12.   TITLE = title;
  13.  
  14.   for(int i=0; i < title.length(); i++)
  15.   {
  16.       TITLE[i] = toupper(title[i]);
  17.   }
  18.  
  19.   ofstream cc_file, h_file, prm_file;
  20.   cc_file.open((title + ".cc").c_str());
  21.  
  22.   cc_file << "// implementation of " << title << " class" << "\n\n";
  23.   cc_file << "#include \"" << title << ".h\" \n\n";
  24.   cc_file << "/* ++++ from here to a line with ---- are needed for each file. */ \n";
  25.   cc_file << "// MI_KEY is a tag for this file, and can be anything unique\n"
  26.           << "#define MI_KEY " << title << "\n\n#include \"mi.h\"\n\n";
  27.   cc_file << "MI_FUNC()\n{\n  MI_DECL(1);\n\n  MI_MODULE("<< title <<");"
  28.           << "// add for each function defined in this file\nMI_EXIT();\n}\n/* ----- */\n\n";
  29.  
  30.   cc_file << title << "::" << title << '\n' << "(const string& name_arg, const module* parent_arg)\n"
  31.           << "  : primitive(name_arg, parent_arg,\n           \""
  32.           << title << "\" /* name of primitive */,\n"
  33.           << "             2 /* number of inputs */,\n"
  34.           << "        2 /* number of outputs */ ),\n"
  35.           << "    default_r(1.0e6), default_m1(1.4), default_m2(1.4),\n"
  36.           << "    default_R(10*3.0857e22), default_psi(0.0),\n"
  37.           << "    h_plus(0.0), h_cross(0.0),\n   param(0.0), val(0.0)\n{";
  38.  
  39.   cc_file << "  // define inputs, one for each input\n"
  40.           << "  setup_input\n" << "    ( 0 /* serial index, starting with 0 */,\n"
  41.           << "      data_ref( &default_in0 /* default storage if none connected */,\n"
  42.           << "          data_ref::Type_Real /* data type defined in data_ref.h */),\n"
  43.           << "      (const void**)(&in0) /* pointer to the input */ );\n"
  44.           << "  set_input_name\n    ( 0 /* serial index */,\n      \"in0\" /* name of this input */);\n";
  45.  
  46.   cc_file << "  setup_input\n" << "    ( 1 /* serial index, starting with 0 */,\n"
  47.           << "      data_ref( &default_in1 /* default storage if none connected */,\n"
  48.           << "          data_ref::Type_Real /* data type defined in data_ref.h */),\n"
  49.           << "      (const void**)(&in1) /* pointer to the input */ );\n"
  50.           << "  set_input_name\n    ( 1 /* serial index */,\n      \"in1\" /* name of this input */);\n";
  51.  
  52.   cc_file << "  /* define outputs, one for each output */\n  setup_output\n"
  53.           << "    ( 0 /* serial output index, starting with 0 */,\n"
  54.           << "      data_ref( &out0 /* output variable */, \n"
  55.           << "          data_ref::Type_Real /* data type */));\n"
  56.           << "  set_output_name\n    ( 0 /* serial index */,\n      \"out0\" /* name of this input */);\n";
  57.  
  58.   cc_file << "  setup_output\n    ( 1 /* serial output index, starting with 0 */, \n"
  59.           << "      data_ref( &out1 /* output variable */,\n"
  60.           << "          data_ref::Type_Real /* data type */));\n"
  61.           << "  set_output_name\n"
  62.           << "    ( 1 /* serial index */, \n      \"out1\" /* name of this input */);\n";
  63.  
  64.   cc_file << "\n  // defining user settings, one for each setting\n  add_auxiliary\n"
  65.           << "    ( data_ref(&param /* storage of the setting value */, \n"
  66.           << "         data_ref::Type_Integer /* data type defined in data_ref.h */), \n"
  67.           << "      \"paramName\" /* name of setting */);\n}\n\n"
  68.           << title <<"::~" << title << "( void )\n{}\n\n"
  69.           << "// implement this just as it is\n"
  70.           << "module*" << title << "::new_type\n"
  71.           << "(const string& name_arg, const module* parent_arg) const\n"
  72.           << "{ return new " << title << "(name_arg, parent_arg); }\n\n";
  73.  
  74.   cc_file << "bool " << title << "::set_time_step(adlib_real time_step_arg)\n{\n"
  75.           << "  if ( time_step_arg < 1e-10 )\n    {\n"
  76.           << "      mess(WARNING) << \"time step too short : time step = \"\n"
  77.           << "              << time_step_arg << endl;\n      return false;\n    }\n"
  78.           << "  else\n    {\n      return true;\n    }\n}\n\n";
  79.  
  80.   cc_file << "// this is called when a setting is changed\n"
  81.           << "void " << title << "::sub_sub_load( void )\n"
  82.           << "{\n  if ( param < 0 )\n    {\n"
  83.           << "      mess(ERROR) << \"param should be positive or 0\" << endl;\n"
  84.           << "      exit(1);\n    }\n  else if ( param == 0)\n    {\n"
  85.           << "      mess(WARNING) << \"param = 0 is fragile\" << endl;\n      val = 1;\n"
  86.           << "    }\n  else\n    {\n      val = 1/param;\n    }\n}\n\n";
  87.  
  88.   cc_file << "// this is the core part\nvoid " << title << "::action( void )\n"
  89.           << "{  // calculate outputs using inputs and local variables\n"
  90.           << "    out0 = *in0 + param * (*in1);\n"
  91.           << "    out1 = val * (*in0) + (*in1);\n}\n";
  92.  
  93.   cc_file.close();
  94.  
  95.   h_file.open((title + ".h").c_str());
  96.   h_file << "/*\n * This creates a module based on the primitive class.\n *\n"
  97.          << " * This module has two real inputs, two real outputs and one setting.\n */\n\n"
  98.          << "#ifndef __" << TITLE << "_H__\n"
  99.          << "#define __" << TITLE << "_H__\n\n"
  100.          << "// this class is based on primitive class\n#include \"primitive.h\"\n\n"
  101.          << "/*******************************##*******************************\\ \n"
  102.          << "*\n* " << title << "*\n"
  103.          << "\\*******************************##*******************************/\n\n";
  104.  
  105.   h_file << "class " << title << " : public primitive\n{\n"
  106.          << "                   /*******************##*******************\\ \n"
  107.          << "                   ****           P U B L I C            ****\n"
  108.          << "                   \\*******************##*******************/\n"
  109.          << "  public:\n  // define constructor just this way\n"
  110.          << "  " << title
  111.          << "\n  (const string& name_arg = \"\", const module* parent_arg = NULL);\n"
  112.          << "  ~" << title << "(void);\n"
  113.          << "\n\n  // this is used to create an object in the event queue\n"
  114.          << "  module* new_type(const string& name_arg, const module* parent_arg) const;\n\n"
  115.          << "  // Sets the simulation time step for this module.\n"
  116.          << "  bool set_time_step(adlib_real time_step_arg);\n"
  117.          << "\n  // this is called when settings are changed"
  118.          << "  void sub_sub_load( void );\n"
  119.          << "void sub_sub_load( void );\n\n"
  120.          << "  // This is called repeatedly at each time step. "
  121.          << "This needs to update the output\n  void action( void );"
  122.          << "\n\n                       /*******************##*******************\\ \n"
  123.          << "                   ****         P R O T E C T E D        ****\n"
  124.          << "                   \\*******************##*******************/\n"
  125.          << " protected:\n  // inputs\n  adlib_real *in0, *in1;\n"
  126.          << "  // storage of default inputs, if nothing is connected to input ports,\n"
  127.          << "  // input ptrs point to these\n"
  128.          << "  adlib_real default_in0, default_in1;\n\n"
  129.          << "  // outputs\n"
  130.          << "   adlib_real out0, out1;\n\n  // settings\n  adlib_real param;\n\n"
  131.          << "  // private variable\n  adlib_real val;\n\n};\n\n";
  132.   h_file << "// this is needed for each header file\n"
  133.          << "// MI_STATIC_INIT needs to be included for each function defined in this file\n"
  134.          << "#if defined(engine_static)\n#define " << TITLE << "_INIT(a) \\\n"
  135.          << "  MI_STATIC_INIT(a, " << title << "());\n"
  136.          << "#else\n#define " << TITLE <<"_INIT(a)\n#endif\n"
  137.          << "#endif  // __" << TITLE << "_H__\n";
  138.   h_file.close();
  139.  
  140.   prm_file.open((title + ".prm").c_str());
  141.  
  142.   prm_file << "% This is the " << title << " module.\n"
  143.            << "%*Port input in0\n%*{\n"
  144.            << "%*  dataType = real\n%*  DefaultValue = 1\n"
  145.            << "%*}\n%*Port input in1\n%*{\n"
  146.            << "%*  dataType = real\n%*}\n%*Port output out0\n"
  147.            << "%*{\n%*  dataType = real\n%*}\n"
  148.            << "%*Port output out1\n%*{\n%*  dataType = real\n"
  149.            << "%*}\n%*GUI_Settings\n%*{\n%*  ScreenSize 150x150\n"
  150.            << "%*  Group 'Whatever'\n%*}\n";
  151.  
  152.   prm_file.close();
  153.  
  154.   cout << "Don't forget to add the appropriate lines to\n"
  155.        << "module_base_register.cc and Makefile.am\n";
  156.  
  157.   return 1;
  158. }

Raw Paste


Login or Register to edit or fork this paste. It's free.