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