ODIN
step_code.h
1 #include <odindata/step.h>
2 
3 #include <typeinfo> // for typeid
4 
5 template <class T>
6 T* Step<T>::clone() const {
7  T* result=allocate();
8  result->init();
9  result->args.copy_ldr_vals(args);
10  return result;
11 }
12 
13 
14 template <class T>
15 void Step<T>::set_args(const STD_string& argstr) {
16  Log<OdinData> odinlog(c_label(),"set_args");
17 
18  unsigned int nargs=args.numof_pars();
19  if(!nargs) return; // Just a flag
20 
21  svector toks(tokens(argstr,',','(',')'));
22  for(unsigned int i=0; i<toks.size(); i++) {
23  STD_string oneargstr=replaceStr(toks[i], "\"", ""); // remove parenthesis at this point
24  ODINLOG(odinlog,normalDebug) << "oneargstr[" << i << "]=" << oneargstr << STD_endl;
25  if(i<nargs) {
26  args[i].parsevalstring(oneargstr);
27  } else {
28  ODINLOG(odinlog,warningLog) << "More arguments provided than parameters in step - argument: " << toks[i] << STD_endl;
29  }
30  }
31 }
32 
33 
34 template <class T>
35 STD_string Step<T>::args_description() const {
36  int nargs=args.numof_pars();
37  STD_string result;
38  for(int i=0; i<nargs; i++) {
39  result+=args[i].get_description();
40 
41  STD_string unit=args[i].get_unit();
42  if(unit!="") result+=" ["+unit+"]";
43 
44  STD_string defval=args[i].printvalstring();
45  if(defval!="" && defval!=ODIN_NONE_STR) result+=" (default="+defval+")";
46 
47  svector alt=args[i].get_alternatives();
48  if(alt.size()) result+=" ("+tokenstring(alt,0)+")";
49  if(i<(nargs-1)) result+=",";
50  }
51  return result;
52 }
53 
54 
55 template <class T>
56 STD_string Step<T>::args_values() const {
57  int nargs=args.numof_pars();
58  STD_string result;
59  for(int i=0; i<nargs; i++) {
60  result+=args[i].printvalstring();
61  STD_string unit=args[i].get_unit();
62  if(unit!="") result+="["+unit+"]";
63  if(i<(nargs-1)) result+=",";
64  }
65  return result;
66 }
67 
68 
69 template <class T>
70 void Step<T>::append_opts(LDRblock& parblock) {
71  parblock.merge(args);
72 }
73 
74 
75 template <class T>
76 void Step<T>::append_arg(LDRbase& arg, const STD_string& arglabel) {
77  arg.set_label(label()+"_"+arglabel);
78  args.append(arg);
79 }
80 
81 
82 
84 
85 
86 template <class T>
88  STD_list<T*> steplist;
89  T::create_templates(steplist);
90 
91  for(typename STD_list<T*>::const_iterator it=steplist.begin(); it!=steplist.end(); ++it) {
92  T* p=(*it);
93  p->init();
94  if(parblock) p->append_opts(*parblock);
95  templates[p->label()]=p;
96  }
97 }
98 
99 
100 template <class T>
102  for(typename StepMap::iterator it=templates.begin(); it!=templates.end(); ++it) delete it->second;
103  for(typename STD_list<T*>::iterator it=garbage.begin(); it!=garbage.end(); ++it) delete (*it);
104 }
105 
106 
107 template <class T>
108 T* StepFactory<T>::create(const STD_string& label) const {
109  Log<OdinData> odinlog("StepFactory","create");
110  T* result=0;
111  typename StepMap::const_iterator it=templates.find(label);
112  if(it!=templates.end()) {
113  result=it->second->clone();
114  } else {
115  ODINLOG(odinlog,errorLog) << "Step with label >" << label << "< not found" << STD_endl;
116  return 0;
117  }
118  garbage.push_back(result);
119  return result;
120 }
121 
122 
123 // Helper class for managing step entries in the manual
124 struct StepDoc {
125  STD_string label;
126  STD_string description;
127  STD_string in;
128  STD_string out;
129  STD_string options;
130 };
131 
132 
133 template <class T>
134 STD_string StepFactory<T>::manual() const {
135  STD_string result;
136 
137  STD_map<STD_string, STD_list<StepDoc> > strmap;
138 
139  for(typename StepMap::const_iterator it=templates.begin(); it!=templates.end(); ++it) {
140 
141  const char* classtype=typeid(*(it->second)).name();
142  while( (*classtype) >='0' && (*classtype) <='9' ) classtype++; // strip off leading numbers (GCC)
143 
144  STD_string classname(classtype);
145  classname=rmblock(classname,"IL",""); // strip off template parameters (GCC)
146 
147  StepDoc doc;
148  doc.label=it->first;
149  doc.description=it->second->description();
150  T::interface_description(it->second, doc.in, doc.out);
151 
152  LDRblock parblock;
153  it->second->append_opts(parblock);
154  int nargs=parblock.numof_pars();
155  for(int i=0; i<nargs; i++) {
156  doc.options+=" Option "+itos(i+1)+": "+parblock[i].get_description();
157  STD_string cmdlineopt=parblock[i].get_cmdline_option();
158  if(cmdlineopt!="") doc.options+=", command-line option: -"+cmdlineopt;
159  doc.options+="\n";
160  }
161  strmap[classname].push_back(doc);
162  }
163 
164 
165  result+="/**\n";
166  result+=" * @addtogroup "+T::manual_group()+"\n";
167  result+=" * @{\n";
168  result+=" */\n\n";
169 
170  for(STD_map<STD_string, STD_list<StepDoc> >::const_iterator it=strmap.begin(); it!=strmap.end(); ++it) {
171 
172  result+="/**\n";
173  result+=" * \\class "+it->first+"\n";
174 
175  STD_string labels;
176  STD_string descr; // assume the same for all
177  for(STD_list<StepDoc>::const_iterator docit=it->second.begin(); docit!=it->second.end(); ++docit) {
178  if(labels!="") labels+=", ";
179  labels+="\\b "+docit->label;
180  descr=docit->description;
181  }
182  result+=" * \\brief "+labels+": "+descr+"\n\n";
183 
184  for(STD_list<StepDoc>::const_iterator docit=it->second.begin(); docit!=it->second.end(); ++docit) {
185  result+=" * Functor label: \\b "+docit->label+"\n\n";
186  if(docit->in!="" || docit->out!="") {
187  result+=" * Interface of \\b "+docit->label+":\n";
188  result+=" * - input: "+docit->in+"\n";
189  result+=" * - output: "+docit->out+"\n";
190  }
191  result+=" *\n";
192  result+=" *\n";
193  if(docit->options!="") {
194  result+=" * Options of \\b "+docit->label+":\n";
195  result+=" * \\verbatim \n";
196  result+=docit->options;
197  result+=" \\endverbatim \n\n";
198  }
199  }
200  result+=" */\n\n";
201  }
202 
203  result+="/** @}\n";
204  result+=" */\n";
205 
206  return result;
207 }
208 
209 
210 template <class T>
211 STD_string StepFactory<T>::get_cmdline_usage(const STD_string& lineprefix)const{
212  STD_string ret;
213  for(typename StepMap::const_iterator it=templates.begin(); it!=templates.end(); ++it) {
214  const T* st=it->second;
215  ret += lineprefix+"-"+st->label();
216  STD_string argsdescr=st->args_description();
217  if(argsdescr!="") ret += " <"+argsdescr+">";
218  ret += " : "+st->description()+"\n";
219  }
220  return ret;
221 }
const STD_string & get_description() const
Definition: ldrbase.h:376
LDRblock & merge(LDRblock &block, bool onlyUserPars=true)
unsigned int numof_pars() const
Labeled & set_label(const STD_string &label)
Definition: tjlabel.h:43
Definition: tjlog.h:218
STD_string manual() const
Definition: step_code.h:134
T * create(const STD_string &label) const
Definition: step_code.h:108
STD_string get_cmdline_usage(const STD_string &lineprefix) const
Definition: step_code.h:211
StepFactory(LDRblock *parblock=0)
Definition: step_code.h:87
void append_arg(LDRbase &arg, const STD_string &arglabel)
Definition: step_code.h:76
void set_args(const STD_string &argstr)
Definition: step_code.h:15
void append_opts(LDRblock &parblock)
Definition: step_code.h:70
STD_string args_description() const
Definition: step_code.h:35
STD_string args_values() const
Definition: step_code.h:56
T * clone() const
Definition: step_code.h:6
STD_string replaceStr(const STD_string &s, const STD_string &searchstring, const STD_string &replacement, whichOccurences mode=allOccurences)
STD_string rmblock(const STD_string &s, const STD_string &blockbegin, const STD_string &blockend, bool rmbegin=true, bool rmend=true, bool rmall=true, bool hierachical=false)
STD_string tokenstring(const svector &tokens, unsigned int linewidth=_DEFAULT_LINEWIDTH_)
svector tokens(const STD_string &tokenstring, char custom_separator=0, char escape_begin='"', char escape_end='"')
STD_string itos(int i, unsigned int maxabs=0)