65int main(
int argc,
char *argv[]) {
76 for(; pos<argc; pos++) {
77 std::string option(argv[pos]);
79 if((option==
"-?") || (option==
"--help")) {
84 if(option==
"-loader") {
94 if(option==
"-wrapper") {
104 if(option==
"-merge") {
109 if(option.c_str()[0]==
'-') {
110 usage(
"unknown option "+ option);
118 if(merge && (loader || swig || wrapper)) {
119 usage(
"mismatching options: either merge or code generation");
121 if(!merge && !(loader || swig || wrapper)) {
122 usage(
"mismatching options: either merge or code generation");
125 usage(
"mismatching options: flat is only applicable to swig");
127 if(merge && (argc-pos <2)) {
128 usage(
"mismatching agruments: to few files to merge");
130 if(!merge && (argc-pos !=2)) {
131 usage(
"mismatching agruments: need one source and one destination for code generation");
137 for(; pos< argc-1; pos++) {
142 if(std::string(argv[argc-1]) !=
"-") {
154 std::ofstream rtiheader;
155 std::ofstream rticode;
156 std::ofstream swigheader;
157 std::ofstream wrpheader;
158 std::ofstream wrpcode;
160 rtiheader.open((std::string(argv[pos])+
".h").c_str(), std::ios::out);
161 rticode.open((std::string(argv[pos])+
".cpp").c_str(), std::ios::out);
164 swigheader.open((std::string(argv[pos])+
".i").c_str(), std::ios::out);
167 wrpheader.open((std::string(argv[pos])+
".h").c_str(), std::ios::out);
168 wrpcode.open((std::string(argv[pos])+
".cpp").c_str(), std::ios::out);
173 rtiheader <<
"/* rti2code: autogenerated libFAUDES rti registration: ";
175 rticode <<
"/* rti2code: autogenerated libFAUDES rti registration: ";
179 swigheader <<
"/* rti2code: autogenerated libFAUDES swig bindings declarations: ";
183 wrpheader <<
"/* rti2code: autogenerated convenioence wrapper: ";
185 wrpcode <<
"/* rti2code: autogenerated convenioence wrapper: ";
191 rticode <<
"namespace faudes {" << std::endl;
192 rticode <<
"/* Register faudes types */" << std::endl;
201 std::string ctype=tit->second->CType();
202 std::string ftype=tit->second->Name();
204 if(ctype==
"")
continue;
206 size_t pos=ctype.find(
"faudes::");
207 if(pos!=std::string::npos)
208 ctype=ctype.substr(std::string(
"faudes::").length());
215 std::cout <<
"rti2code: generating auto-registration code for \"" << ftype <<
"\"" << std::endl;
217 std::string rtiname = std::string(
"gRti") +
ToStringInteger(tcnt) +
"Register" + ftype;
218 rticode <<
"AutoRegisterType<" << ctype <<
"> " << rtiname <<
"(\"" << ftype <<
"\");";
219 rticode << std::endl;
221 if(tit->second->ElementTag()!=
"") {
222 rtiname = std::string(
"gRti") +
ToStringInteger(tcnt) +
"ElementTag" + ftype;
223 rticode <<
"AutoRegisterElementTag<" << ctype <<
"> " << rtiname <<
"(\"" << ftype <<
224 "\", \"" << tit->second->ElementTag() <<
"\");";
225 rticode << std::endl;
228 if(tit->second->ElementType()!=
"") {
229 rtiname = std::string(
"gRti") +
ToStringInteger(tcnt) +
"ElementType" + ftype;
230 rticode <<
"AutoRegisterElementType<" << ctype <<
"> " << rtiname <<
"(\"" << ftype <<
231 "\", \"" << tit->second->ElementType() <<
"\");";
232 rticode << std::endl;
239 rticode <<
"} // namespace" << std::endl;
244 rtiheader <<
"namespace faudes {" << std::endl;
245 rtiheader <<
"void LoadRegisteredTypes(void);" << std::endl;
246 rtiheader <<
"} // namespace" << std::endl;
251 rticode <<
"namespace faudes {" << std::endl;
252 rticode <<
"/* Register faudes types */" << std::endl;
253 rticode <<
"void LoadRegisteredTypes(void) {" << std::endl;
261 std::string ctype=tit->second->CType();
263 if(ctype==
"")
continue;
265 size_t pos=ctype.find(
"faudes::");
266 if(pos!=std::string::npos)
267 ctype=ctype.substr(std::string(
"faudes::").length());
269 if(tit->second->AutoRegistered())
continue;
271 std::cout <<
"rti2code: generating registration code for \"" << tit->second->Name() <<
"\"" << std::endl;
273 rticode <<
" TypeRegistry::G()->Insert<" << ctype <<
">(\"" << tit->second->Name() <<
"\");";
274 rticode << std::endl;
280 rticode <<
"}" << std::endl;
281 rticode <<
"} // namespace" << std::endl;
287 rtiheader <<
"namespace faudes {" << std::endl;
288 rtiheader <<
"void LoadRegisteredFunctions(void);" << std::endl;
289 rtiheader <<
"} // namespace" << std::endl;
294 rticode <<
"namespace faudes {" << std::endl;
295 rticode <<
"/* Register faudes functions */" << std::endl;
296 rticode <<
"void LoadRegisteredFunctions(void) {" << std::endl;
301 rtiheader <<
"namespace faudes {" << std::endl;
306 wrpcode <<
"namespace faudes {" << std::endl;
307 wrpheader <<
"namespace faudes {" << std::endl;
317 std::string ctype=fdef->
CType();
318 std::string fname = fdef->
Name();
320 if(ctype==
"")
continue;
322 size_t pos=ctype.find(
"faudes::");
323 if(pos!=std::string::npos)
324 ctype=ctype.substr(std::string(
"faudes::").length());
327 std::cout <<
"rti2cocde: function registration: " << fname <<
": no signatures" << std::endl;
333 std::vector< std::vector<std::string> > fparams;
334 std::vector< std::vector<std::string> > ctypes;
335 std::vector< std::vector<Parameter::ParamAttr> > fattrib;
336 std::vector< std::vector<bool> > cretval;
340 std::vector<std::string> fparamsi;
341 std::vector<std::string> ctypesi;
342 std::vector<Parameter::ParamAttr> fattribi;
343 std::vector<bool> cretvali;
345 for(
int j=0; j<sigi.
Size(); j++) {
347 std::string fparam=sigi.
At(j).
Name();
348 std::string ftype=sigi.
At(j).
Type();
352 if(fcret) retcount++;
364 if(retcount>1)
break;
366 size_t pos=ctype.find(
"faudes::");
367 if(pos!=std::string::npos)
368 ctype=ctype.substr(std::string(
"faudes::").length());
370 fparamsi.push_back(fparam);
371 ctypesi.push_back(ctype);
372 fattribi.push_back(fattr);
373 cretvali.push_back(fcret);
376 if((
int) ctypesi.size()!=sigi.
Size()) {
377 std::cout <<
"rti2code: function registration: " << fname <<
": cannot interpret signature "
378 << sigi.
Name() << std::endl;
382 fparams.push_back(fparamsi);
383 ctypes.push_back(ctypesi);
384 fattrib.push_back(fattribi);
385 cretval.push_back(cretvali);
388 std::cout <<
"rti2code: generating rti wrapper for \"" << fdef->
Name() <<
"\"" <<
389 " #" << ctypes.size() <<
" variants" << std::endl;
390 std::string rtiname = std::string(
"Rti") +
ToStringInteger(fcnt) + ctype;
394 rticode <<
" FunctionRegistry::G()->Insert<" << rtiname <<
">(\"" << fname <<
"\");" << std::endl;
398 rtiheader <<
"/* Function class for C++ function " << ctype <<
"*/" << std::endl;
399 rtiheader <<
"class " << rtiname <<
" : public Function { " << std::endl;
400 rtiheader <<
"public:" << std::endl;
401 rtiheader <<
" using Function::operator=;" << std::endl;
402 rtiheader << rtiname <<
"(const FunctionDefinition* fdef) : Function(fdef) {};" << std::endl;
403 rtiheader <<
"virtual Function* New(void) const { return new " << rtiname <<
"(pFuncDef); };" << std::endl;
404 rtiheader <<
"protected:" << std::endl;
408 for(
unsigned int i=0; i<ctypes.size(); i++)
409 for(
unsigned int j=0; j<ctypes.at(i).size(); j++)
410 rtiheader << ctypes.at(i).at(j) <<
"* " <<
"mP_" << i <<
"_" << j <<
";" << std::endl;
414 rtiheader <<
"virtual bool DoTypeCheck(int n) {" << std::endl;
415 rtiheader <<
" bool res=false;" << std::endl;
416 rtiheader <<
" switch(mVariantIndex) { "<< std::endl;
417 for(
unsigned int i=0; i<ctypes.size(); i++) {
418 rtiheader <<
" case " << i <<
": { // variant " << fdef->
Variant(i).
Name() << std::endl;
419 rtiheader <<
" switch(n) { "<< std::endl;
420 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
421 rtiheader <<
" case " << j <<
": ";
422 rtiheader <<
" res=DoTypeCast<" << ctypes.at(i).at(j) <<
">(" << j <<
", mP_" << i <<
"_" << j <<
"); ";
423 rtiheader <<
"break; "<< std::endl;
425 rtiheader <<
" default: break; " << std::endl;
426 rtiheader <<
" } "<< std::endl;
427 rtiheader <<
" break; "<< std::endl;
428 rtiheader <<
" } "<< std::endl;
430 rtiheader <<
" default: break; " << std::endl;
431 rtiheader <<
" } "<< std::endl;
432 rtiheader <<
" return res;" << std::endl;
433 rtiheader <<
"};" << std::endl;
437 rtiheader <<
"virtual void DoExecute(void) {" << std::endl;
441 rtiheader <<
" switch(mVariantIndex) { "<< std::endl;
442 for(
unsigned int i=0; i<ctypes.size(); i++) {
443 rtiheader <<
" case " << i <<
": { // variant " << fdef->
Variant(i).
Name() << std::endl;
446 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
447 if(!cretval.at(i).at(j))
continue;
449 if(ctypes.at(i).at(j) ==
"Integer") {
450 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference()) = ";
453 if(ctypes.at(i).at(j) ==
"Boolean") {
454 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference()) = ";
457 if(ctypes.at(i).at(j) ==
"String") {
458 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference()) = ";
461 rtiheader <<
"*mP_" << i <<
"_" << j <<
" = ";
464 rtiheader << ctype <<
"(";
467 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
468 if(cretval.at(i).at(j))
continue;
469 if((parpos++)!=0) rtiheader <<
" ,";
471 if(ctypes.at(i).at(j) ==
"Integer") {
472 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference())";
475 if(ctypes.at(i).at(j) ==
"Boolean") {
476 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference())";
479 if(ctypes.at(i).at(j) ==
"String") {
480 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference())";
483 rtiheader <<
"*mP_" << i <<
"_" << j;
485 rtiheader <<
"); break; };" << std::endl;
488 rtiheader <<
" default: break; " << std::endl;
489 rtiheader <<
" }; "<< std::endl;
491 rtiheader <<
"}; "<< std::endl;
493 rtiheader <<
"};" << std::endl;
498 swigheader <<
"/* faudes-function \"" << fname <<
"\" */" << std::endl;
503 std::string plugin=fdef->
PlugIn();
504 swigheader <<
"#if " <<
"( SwigModule == \"Swig" << plugin <<
"\")";
506 swigheader << std::endl;
512 swigheader <<
"%rename(" << fname <<
") " << ctype <<
";" << std::endl;
516 std::vector< std::string > lfsigs;
517 std::vector< std::string > lfdecs;
518 std::vector< std::string > lfdefs;
519 std::vector< std::string > lrtypes;
520 std::vector< std::string > lhelp;
523 for(
unsigned int i=0; i<ctypes.size(); i++) {
525 std::string lrtype=
"void";
527 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
528 if(!cretval.at(i).at(j))
continue;
530 if(ctypes.at(i).at(j) ==
"Integer") {
534 if(ctypes.at(i).at(j) ==
"Boolean") {
538 if(ctypes.at(i).at(j) ==
"String") {
539 lrtype=
"std::string";
542 lrtype = ctypes.at(i).at(j);
546 lrtypes.push_back(lrtype);
548 std::string lfdec = ctype +
"(";
549 std::string lfsig = ctype +
"-";
551 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
553 if(cretval.at(i).at(j))
continue;
559 lfsig += ctypes.at(i).at(j);
564 if(ctypes.at(i).at(j) ==
"Integer") {
565 lfdec +=
"long int&";
568 if(ctypes.at(i).at(j) ==
"Boolean") {
572 if(ctypes.at(i).at(j) ==
"String") {
573 lfdec +=
"std::string&";
576 lfdec += ctypes.at(i).at(j) +
"&";
580 if(ctypes.at(i).at(j) ==
"Boolean" || ctypes.at(i).at(j) ==
"String"
581 || ctypes.at(i).at(j) ==
"Integer") {
589 lfdec +=
" " + fparams.at(i).at(j);
594 lfdecs.push_back(lfdec);
595 lfsigs.push_back(lfsig);
597 std::string luasig =
" " + fname +
"(";
598 bool leftcomma =
false;
599 bool rightcomma =
false;
600 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
602 if(ctypes.at(i).at(j) ==
"Boolean" || ctypes.at(i).at(j) ==
"String"
603 || ctypes.at(i).at(j) ==
"Integer")
605 if(leftcomma) luasig =
"," + luasig;
607 luasig=ctypes.at(i).at(j) + luasig;
612 if(rightcomma) luasig +=
", ";
614 luasig += sigi.
At(j).
Str();
620 std::string topic= fdef->
PlugIn();
622 if(topic==
"CoreFaudes") {
626 if(topic.length()>0) topic.at(0)=toupper(topic.at(0));
627 if(key1.length()>0) key1.at(0)=toupper(key1.at(0));
628 lhelp.push_back(
"SwigHelpEntry(\"" + topic +
"\", \"" + key1 +
"\", \"" +
634 lfdefs.push_back(
"");
639 for(
unsigned int i=0; i<ctypes.size(); i++) {
640 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
647 for(
unsigned int i=0; i<ctypes.size() && xwrp; i++) {
649 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
651 if(cretval.at(i).at(j)) {
664 if(ctypes.at(i).at(j) ==
"Boolean" || ctypes.at(i).at(j) ==
"String"
665 || ctypes.at(i).at(j) ==
"Integer") {
675 std::string lrtype = ctypes.at(i).at(fret);
676 lrtypes.push_back(lrtype);
678 std::string lfdec = ctype +
"(";
679 std::string lfsig = ctype +
"-";
682 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
684 if(j==fret)
continue;
690 lfsig += ctypes.at(i).at(j);
695 if(ctypes.at(i).at(j) ==
"Integer") {
696 lfdec +=
"long int&";
699 if(ctypes.at(i).at(j) ==
"Boolean") {
703 if(ctypes.at(i).at(j) ==
"String") {
704 lfdec +=
"std::string&";
707 lfdec += ctypes.at(i).at(j) +
"&";
710 lfdec +=
" " + fparams.at(i).at(j);
715 lfdecs.push_back(lfdec);
716 lfsigs.push_back(lfsig);
725 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
732 lfdef += fparams.at(i).at(j);
734 lfdef +=
"); return res; }";
736 lfdefs.push_back(lfdef);
744 for(
unsigned int i=1; i<lfsigs.size();i++) {
747 if(lfsigs.at(i)==lfsigs.at(j))
break;
749 std::cout <<
"rti2code: ambiguous signatures: ";
750 std::cout << lrtypes.at(i) <<
"+" << lfsigs.at(i) <<
" vs. ";
751 std::cout << lrtypes.at(j) <<
"+" << lfsigs.at(j) << std::endl;
753 if((lrtypes.at(j)==
"void") && (lrtypes.at(i)!=
"void"))
754 {lfdecs[j]=
"";
continue;}
755 if((lrtypes.at(i)==
"void") && (lrtypes.at(j)!=
"void"))
756 {lfdecs[i]=
"";
continue;}
764 for(
unsigned int i=0; i<lfdecs.size(); i++) {
765 if(lfdecs.at(i)==
"")
continue;
766 swigheader <<
"%feature(\"autodoc\",\"1\");" << std::endl;
767 swigheader << lrtypes.at(i) <<
" " << lfdecs.at(i) <<
";" << std::endl;
769 swigheader << lhelp.at(i) <<
";" << std::endl;
773 swigheader <<
"#endif " << std::endl;
775 swigheader << std::endl;
776 std::cout <<
"rti2code: generating swig interface for function \"" << fdef->
Name() <<
"\"" <<
777 " #" << lcount <<
" variants" << std::endl;
783 for(
unsigned int i=0; i<lfdecs.size(); i++) {
784 if(lfdecs.at(i)==
"")
continue;
785 if(lfdefs.at(i)==
"")
continue;
786 wrpheader <<
"extern FAUDES_API " << lrtypes.at(i) <<
" " << lfdecs.at(i) <<
";" << std::endl;
787 wrpcode << lrtypes.at(i) <<
" " << lfdecs.at(i) <<
" " << lfdefs.at(i) <<
";" << std::endl;
790 std::cout <<
"rti2code: generating convenience wrappers for function \"" << fdef->
Name() <<
"\"" <<
791 " #" << lcount <<
" variants" << std::endl;
798 rtiheader <<
"} // namespace" << std::endl;
803 rticode <<
"}" << std::endl;
804 rticode <<
"} // namespace" << std::endl;
809 wrpheader <<
"} // namespace" << std::endl;
810 wrpcode <<
"} // namespace" << std::endl;