00001 #include <odindata/step.h>
00002
00003
00004 template <class T>
00005 T* Step<T>::clone() const {
00006 T* result=allocate();
00007 result->init();
00008 result->args.copy_ldr_vals(args);
00009 return result;
00010 }
00011
00012
00013 template <class T>
00014 void Step<T>::set_args(const STD_string& argstr) {
00015 Log<OdinData> odinlog(c_label(),"set_args");
00016
00017 unsigned int nargs=args.numof_pars();
00018 if(!nargs) return;
00019
00020 svector toks(tokens(argstr,',','(',')'));
00021 for(unsigned int i=0; i<toks.size(); i++) {
00022 ODINLOG(odinlog,normalDebug) << "toks[" << i << "]=" << toks[i] << STD_endl;
00023 if(i<nargs) {
00024 args[i].parsevalstring(toks[i]);
00025 } else {
00026 ODINLOG(odinlog,warningLog) << "More arguments provided than parameters in step - argument: " << toks[i] << STD_endl;
00027 }
00028 }
00029 }
00030
00031
00032 template <class T>
00033 STD_string Step<T>::args_description() const {
00034 int nargs=args.numof_pars();
00035 STD_string result;
00036 for(int i=0; i<nargs; i++) {
00037 result+=args[i].get_description();
00038
00039 STD_string unit=args[i].get_unit();
00040 if(unit!="") result+=" ["+unit+"]";
00041
00042 svector alt=args[i].get_alternatives();
00043 if(alt.size()) result+=" ("+tokenstring(alt,0)+")";
00044 if(i<(nargs-1)) result+=",";
00045 }
00046 return result;
00047 }
00048
00049
00050 template <class T>
00051 void Step<T>::append_opts(JcampDxBlock& parblock) {
00052 parblock.merge(args);
00053 }
00054
00055
00056 template <class T>
00057 void Step<T>::append_arg(JcampDxClass& arg, const STD_string& arglabel) {
00058 arg.set_label(label()+"_"+arglabel);
00059 args.append(arg);
00060 }
00061
00062
00063
00065
00066
00067 template <class T>
00068 StepFactory<T>::StepFactory(JcampDxBlock* parblock) {
00069 STD_list<T*> steplist;
00070 T::create_templates(steplist);
00071
00072 for(typename STD_list<T*>::const_iterator it=steplist.begin(); it!=steplist.end(); ++it) {
00073 T* p=(*it);
00074 p->init();
00075 if(parblock) p->append_opts(*parblock);
00076 templates[p->label()]=p;
00077 }
00078 }
00079
00080
00081 template <class T>
00082 StepFactory<T>::~StepFactory() {
00083 for(typename StepMap::iterator it=templates.begin(); it!=templates.end(); ++it) delete it->second;
00084 for(typename STD_list<T*>::iterator it=garbage.begin(); it!=garbage.end(); ++it) delete (*it);
00085 }
00086
00087
00088 template <class T>
00089 T* StepFactory<T>::create(const STD_string& label) const {
00090 Log<OdinData> odinlog("StepFactory","create");
00091 T* result=0;
00092 typename StepMap::const_iterator it=templates.find(label);
00093 if(it!=templates.end()) {
00094 result=it->second->clone();
00095 } else {
00096 ODINLOG(odinlog,errorLog) << "Step with label >" << label << "< not found" << STD_endl;
00097 return 0;
00098 }
00099 garbage.push_back(result);
00100 return result;
00101 }
00102
00103
00104
00105 struct StepDoc {
00106 STD_string label;
00107 STD_string description;
00108 STD_string in;
00109 STD_string out;
00110 STD_string options;
00111 };
00112
00113
00114 template <class T>
00115 STD_string StepFactory<T>::manual() const {
00116 STD_string result;
00117
00118 STD_map<STD_string, STD_list<StepDoc> > strmap;
00119
00120 for(typename StepMap::const_iterator it=templates.begin(); it!=templates.end(); ++it) {
00121
00122 const char* classtype=typeid(*(it->second)).name();
00123 while( (*classtype) >='0' && (*classtype) <='9' ) classtype++;
00124
00125 STD_string classname(classtype);
00126 classname=rmblock(classname,"IL","");
00127
00128 StepDoc doc;
00129 doc.label=it->first;
00130 doc.description=it->second->description();
00131 T::interface_description(it->second, doc.in, doc.out);
00132
00133 JcampDxBlock parblock;
00134 it->second->append_opts(parblock);
00135 int nargs=parblock.numof_pars();
00136 for(int i=0; i<nargs; i++) {
00137 doc.options+=" Option "+itos(i+1)+": "+parblock[i].get_description();
00138 STD_string cmdlineopt=parblock[i].get_cmdline_option();
00139 if(cmdlineopt!="") doc.options+=", command-line option: -"+cmdlineopt;
00140 doc.options+="\n";
00141 }
00142 strmap[classname].push_back(doc);
00143 }
00144
00145
00146 result+="/**\n";
00147 result+=" * @addtogroup "+T::manual_group()+"\n";
00148 result+=" * @{\n";
00149 result+=" */\n\n";
00150
00151 for(STD_map<STD_string, STD_list<StepDoc> >::const_iterator it=strmap.begin(); it!=strmap.end(); ++it) {
00152
00153 result+="/**\n";
00154 result+=" * \\class "+it->first+"\n";
00155
00156 STD_string labels;
00157 STD_string descr;
00158 for(STD_list<StepDoc>::const_iterator docit=it->second.begin(); docit!=it->second.end(); ++docit) {
00159 if(labels!="") labels+=", ";
00160 labels+="\\b "+docit->label;
00161 descr=docit->description;
00162 }
00163 result+=" * \\brief "+labels+": "+descr+"\n\n";
00164
00165 for(STD_list<StepDoc>::const_iterator docit=it->second.begin(); docit!=it->second.end(); ++docit) {
00166 result+=" * Functor label: \\b "+docit->label+"\n\n";
00167 if(docit->in!="" || docit->out!="") {
00168 result+=" * Interface of \\b "+docit->label+":\n";
00169 result+=" * - input: "+docit->in+"\n";
00170 result+=" * - output: "+docit->out+"\n";
00171 }
00172 result+=" *\n";
00173 result+=" *\n";
00174 if(docit->options!="") {
00175 result+=" * Options of \\b "+docit->label+":\n";
00176 result+=" * \\verbatim \n";
00177 result+=docit->options;
00178 result+=" \\endverbatim \n\n";
00179 }
00180 }
00181 result+=" */\n\n";
00182 }
00183
00184 result+="/** @}\n";
00185 result+=" */\n";
00186
00187 return result;
00188 }
00189
00190
00191 template <class T>
00192 STD_string StepFactory<T>::get_cmdline_usage(const STD_string& lineprefix)const{
00193 STD_string ret;
00194 for(typename StepMap::const_iterator it=templates.begin(); it!=templates.end(); ++it) {
00195 const T* st=it->second;
00196 ret += lineprefix+"-"+st->label();
00197 STD_string argsdescr=st->args_description();
00198 if(argsdescr!="") ret += " <"+argsdescr+">";
00199 ret += " : "+st->description()+"\n";
00200 }
00201 return ret;
00202 }