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());
210 if(tit->second->AutoRegistered())
continue;
212 std::cout <<
"rti2code: generating auto-registration code for \"" << ftype <<
"\"" << std::endl;
214 std::string rtiname = std::string(
"gRti") +
ToStringInteger(tcnt) +
"Register" + ftype;
215 rticode <<
"AutoRegisterType<" << ctype <<
"> " << rtiname <<
"(\"" << ftype <<
"\");";
216 rticode << std::endl;
218 if(tit->second->ElementTag()!=
"") {
219 rtiname = std::string(
"gRti") +
ToStringInteger(tcnt) +
"ElementTag" + ftype;
220 rticode <<
"AutoRegisterElementTag<" << ctype <<
"> " << rtiname <<
"(\"" << ftype <<
221 "\", \"" << tit->second->ElementTag() <<
"\");";
222 rticode << std::endl;
225 if(tit->second->ElementType()!=
"") {
226 rtiname = std::string(
"gRti") +
ToStringInteger(tcnt) +
"ElementType" + ftype;
227 rticode <<
"AutoRegisterElementType<" << ctype <<
"> " << rtiname <<
"(\"" << ftype <<
228 "\", \"" << tit->second->ElementType() <<
"\");";
229 rticode << std::endl;
236 rticode <<
"} // namespace" << std::endl;
241 rtiheader <<
"namespace faudes {" << std::endl;
242 rtiheader <<
"void LoadRegisteredTypes(void);" << std::endl;
243 rtiheader <<
"} // namespace" << std::endl;
248 rticode <<
"namespace faudes {" << std::endl;
249 rticode <<
"/* Register faudes types */" << std::endl;
250 rticode <<
"void LoadRegisteredTypes(void) {" << std::endl;
258 std::string ctype=tit->second->CType();
260 if(ctype==
"")
continue;
262 size_t pos=ctype.find(
"faudes::");
263 if(pos!=std::string::npos)
264 ctype=ctype.substr(std::string(
"faudes::").length());
266 if(tit->second->AutoRegistered())
continue;
268 std::cout <<
"rti2code: generating registration code for \"" << tit->second->Name() <<
"\"" << std::endl;
270 rticode <<
" TypeRegistry::G()->Insert<" << ctype <<
">(\"" << tit->second->Name() <<
"\");";
271 rticode << std::endl;
277 rticode <<
"}" << std::endl;
278 rticode <<
"} // namespace" << std::endl;
284 rtiheader <<
"namespace faudes {" << std::endl;
285 rtiheader <<
"void LoadRegisteredFunctions(void);" << std::endl;
286 rtiheader <<
"} // namespace" << std::endl;
291 rticode <<
"namespace faudes {" << std::endl;
292 rticode <<
"/* Register faudes functions */" << std::endl;
293 rticode <<
"void LoadRegisteredFunctions(void) {" << std::endl;
298 rtiheader <<
"namespace faudes {" << std::endl;
303 wrpcode <<
"namespace faudes {" << std::endl;
304 wrpheader <<
"namespace faudes {" << std::endl;
314 std::string ctype=fdef->
CType();
315 std::string fname = fdef->
Name();
317 if(ctype==
"")
continue;
319 size_t pos=ctype.find(
"faudes::");
320 if(pos!=std::string::npos)
321 ctype=ctype.substr(std::string(
"faudes::").length());
324 std::cout <<
"rti2cocde: function registration: " << fname <<
": no signatures" << std::endl;
330 std::vector< std::vector<std::string> > fparams;
331 std::vector< std::vector<std::string> > ctypes;
332 std::vector< std::vector<Parameter::ParamAttr> > fattrib;
333 std::vector< std::vector<bool> > cretval;
337 std::vector<std::string> fparamsi;
338 std::vector<std::string> ctypesi;
339 std::vector<Parameter::ParamAttr> fattribi;
340 std::vector<bool> cretvali;
342 for(
int j=0; j<sigi.
Size(); j++) {
344 std::string fparam=sigi.
At(j).
Name();
345 std::string ftype=sigi.
At(j).
Type();
349 if(fcret) retcount++;
361 if(retcount>1)
break;
363 size_t pos=ctype.find(
"faudes::");
364 if(pos!=std::string::npos)
365 ctype=ctype.substr(std::string(
"faudes::").length());
367 fparamsi.push_back(fparam);
368 ctypesi.push_back(ctype);
369 fattribi.push_back(fattr);
370 cretvali.push_back(fcret);
373 if((
int) ctypesi.size()!=sigi.
Size()) {
374 std::cout <<
"rti2code: function registration: " << fname <<
": cannot interpret signature "
375 << sigi.
Name() << std::endl;
379 fparams.push_back(fparamsi);
380 ctypes.push_back(ctypesi);
381 fattrib.push_back(fattribi);
382 cretval.push_back(cretvali);
385 std::cout <<
"rti2code: generating rti wrapper for \"" << fdef->
Name() <<
"\"" <<
386 " #" << ctypes.size() <<
" variants" << std::endl;
387 std::string rtiname = std::string(
"Rti") +
ToStringInteger(fcnt) + ctype;
391 rticode <<
" FunctionRegistry::G()->Insert<" << rtiname <<
">(\"" << fname <<
"\");" << std::endl;
395 rtiheader <<
"/* Function class for C++ function " << ctype <<
"*/" << std::endl;
396 rtiheader <<
"class " << rtiname <<
" : public Function { " << std::endl;
397 rtiheader <<
"public:" << std::endl;
398 rtiheader <<
" using Function::operator=;" << std::endl;
399 rtiheader << rtiname <<
"(const FunctionDefinition* fdef) : Function(fdef) {};" << std::endl;
400 rtiheader <<
"virtual Function* New(void) const { return new " << rtiname <<
"(pFuncDef); };" << std::endl;
401 rtiheader <<
"protected:" << std::endl;
405 for(
unsigned int i=0; i<ctypes.size(); i++)
406 for(
unsigned int j=0; j<ctypes.at(i).size(); j++)
407 rtiheader << ctypes.at(i).at(j) <<
"* " <<
"mP_" << i <<
"_" << j <<
";" << std::endl;
411 rtiheader <<
"virtual bool DoTypeCheck(int n) {" << std::endl;
412 rtiheader <<
" bool res=false;" << std::endl;
413 rtiheader <<
" switch(mVariantIndex) { "<< std::endl;
414 for(
unsigned int i=0; i<ctypes.size(); i++) {
415 rtiheader <<
" case " << i <<
": { // variant " << fdef->
Variant(i).
Name() << std::endl;
416 rtiheader <<
" switch(n) { "<< std::endl;
417 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
418 rtiheader <<
" case " << j <<
": ";
419 rtiheader <<
" res=DoTypeCast<" << ctypes.at(i).at(j) <<
">(" << j <<
", mP_" << i <<
"_" << j <<
"); ";
420 rtiheader <<
"break; "<< std::endl;
422 rtiheader <<
" default: break; " << std::endl;
423 rtiheader <<
" } "<< std::endl;
424 rtiheader <<
" break; "<< std::endl;
425 rtiheader <<
" } "<< std::endl;
427 rtiheader <<
" default: break; " << std::endl;
428 rtiheader <<
" } "<< std::endl;
429 rtiheader <<
" return res;" << std::endl;
430 rtiheader <<
"};" << std::endl;
434 rtiheader <<
"virtual void DoExecute(void) {" << std::endl;
438 rtiheader <<
" switch(mVariantIndex) { "<< std::endl;
439 for(
unsigned int i=0; i<ctypes.size(); i++) {
440 rtiheader <<
" case " << i <<
": { // variant " << fdef->
Variant(i).
Name() << std::endl;
443 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
444 if(!cretval.at(i).at(j))
continue;
446 if(ctypes.at(i).at(j) ==
"Integer") {
447 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference()) = ";
450 if(ctypes.at(i).at(j) ==
"Boolean") {
451 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference()) = ";
454 if(ctypes.at(i).at(j) ==
"String") {
455 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference()) = ";
458 rtiheader <<
"*mP_" << i <<
"_" << j <<
" = ";
461 rtiheader << ctype <<
"(";
464 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
465 if(cretval.at(i).at(j))
continue;
466 if((parpos++)!=0) rtiheader <<
" ,";
468 if(ctypes.at(i).at(j) ==
"Integer") {
469 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference())";
472 if(ctypes.at(i).at(j) ==
"Boolean") {
473 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference())";
476 if(ctypes.at(i).at(j) ==
"String") {
477 rtiheader <<
"*(mP_" << i <<
"_" << j <<
"->CReference())";
480 rtiheader <<
"*mP_" << i <<
"_" << j;
482 rtiheader <<
"); break; };" << std::endl;
485 rtiheader <<
" default: break; " << std::endl;
486 rtiheader <<
" }; "<< std::endl;
488 rtiheader <<
"}; "<< std::endl;
490 rtiheader <<
"};" << std::endl;
495 swigheader <<
"/* faudes-function \"" << fname <<
"\" */" << std::endl;
500 std::string plugin=fdef->
PlugIn();
501 swigheader <<
"#if " <<
"( SwigModule == \"Swig" << plugin <<
"\")";
503 swigheader << std::endl;
509 swigheader <<
"%rename(" << fname <<
") " << ctype <<
";" << std::endl;
513 std::vector< std::string > lfsigs;
514 std::vector< std::string > lfdecs;
515 std::vector< std::string > lfdefs;
516 std::vector< std::string > lrtypes;
517 std::vector< std::string > lhelp;
520 for(
unsigned int i=0; i<ctypes.size(); i++) {
522 std::string lrtype=
"void";
524 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
525 if(!cretval.at(i).at(j))
continue;
527 if(ctypes.at(i).at(j) ==
"Integer") {
531 if(ctypes.at(i).at(j) ==
"Boolean") {
535 if(ctypes.at(i).at(j) ==
"String") {
536 lrtype=
"std::string";
539 lrtype = ctypes.at(i).at(j);
543 lrtypes.push_back(lrtype);
545 std::string lfdec = ctype +
"(";
546 std::string lfsig = ctype +
"-";
548 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
550 if(cretval.at(i).at(j))
continue;
556 lfsig += ctypes.at(i).at(j);
561 if(ctypes.at(i).at(j) ==
"Integer") {
562 lfdec +=
"long int&";
565 if(ctypes.at(i).at(j) ==
"Boolean") {
569 if(ctypes.at(i).at(j) ==
"String") {
570 lfdec +=
"std::string&";
573 lfdec += ctypes.at(i).at(j) +
"&";
577 if(ctypes.at(i).at(j) ==
"Boolean" || ctypes.at(i).at(j) ==
"String"
578 || ctypes.at(i).at(j) ==
"Integer") {
586 lfdec +=
" " + fparams.at(i).at(j);
591 lfdecs.push_back(lfdec);
592 lfsigs.push_back(lfsig);
594 std::string luasig =
" " + fname +
"(";
595 bool leftcomma =
false;
596 bool rightcomma =
false;
597 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
599 if(ctypes.at(i).at(j) ==
"Boolean" || ctypes.at(i).at(j) ==
"String"
600 || ctypes.at(i).at(j) ==
"Integer")
602 if(leftcomma) luasig =
"," + luasig;
604 luasig=ctypes.at(i).at(j) + luasig;
609 if(rightcomma) luasig +=
", ";
611 luasig += sigi.
At(j).
Str();
617 std::string topic= fdef->
PlugIn();
619 if(topic==
"CoreFaudes") {
623 if(topic.length()>0) topic.at(0)=toupper(topic.at(0));
624 if(key1.length()>0) key1.at(0)=toupper(key1.at(0));
625 lhelp.push_back(
"SwigHelpEntry(\"" + topic +
"\", \"" + key1 +
"\", \"" +
631 lfdefs.push_back(
"");
636 for(
unsigned int i=0; i<ctypes.size(); i++) {
637 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
644 for(
unsigned int i=0; i<ctypes.size() && xwrp; i++) {
646 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
648 if(cretval.at(i).at(j)) {
661 if(ctypes.at(i).at(j) ==
"Boolean" || ctypes.at(i).at(j) ==
"String"
662 || ctypes.at(i).at(j) ==
"Integer") {
672 std::string lrtype = ctypes.at(i).at(fret);
673 lrtypes.push_back(lrtype);
675 std::string lfdec = ctype +
"(";
676 std::string lfsig = ctype +
"-";
679 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
681 if(j==fret)
continue;
687 lfsig += ctypes.at(i).at(j);
692 if(ctypes.at(i).at(j) ==
"Integer") {
693 lfdec +=
"long int&";
696 if(ctypes.at(i).at(j) ==
"Boolean") {
700 if(ctypes.at(i).at(j) ==
"String") {
701 lfdec +=
"std::string&";
704 lfdec += ctypes.at(i).at(j) +
"&";
707 lfdec +=
" " + fparams.at(i).at(j);
712 lfdecs.push_back(lfdec);
713 lfsigs.push_back(lfsig);
722 for(
unsigned int j=0; j<ctypes.at(i).size(); j++) {
729 lfdef += fparams.at(i).at(j);
731 lfdef +=
"); return res; }";
733 lfdefs.push_back(lfdef);
741 for(
unsigned int i=1; i<lfsigs.size();i++) {
744 if(lfsigs.at(i)==lfsigs.at(j))
break;
746 std::cout <<
"rti2code: ambiguous signatures: ";
747 std::cout << lrtypes.at(i) <<
"+" << lfsigs.at(i) <<
" vs. ";
748 std::cout << lrtypes.at(j) <<
"+" << lfsigs.at(j) << std::endl;
750 if((lrtypes.at(j)==
"void") && (lrtypes.at(i)!=
"void"))
751 {lfdecs[j]=
"";
continue;}
752 if((lrtypes.at(i)==
"void") && (lrtypes.at(j)!=
"void"))
753 {lfdecs[i]=
"";
continue;}
761 for(
unsigned int i=0; i<lfdecs.size(); i++) {
762 if(lfdecs.at(i)==
"")
continue;
763 swigheader <<
"%feature(\"autodoc\",\"1\");" << std::endl;
764 swigheader << lrtypes.at(i) <<
" " << lfdecs.at(i) <<
";" << std::endl;
766 swigheader << lhelp.at(i) <<
";" << std::endl;
770 swigheader <<
"#endif " << std::endl;
772 swigheader << std::endl;
773 std::cout <<
"rti2code: generating swig interface for function \"" << fdef->
Name() <<
"\"" <<
774 " #" << lcount <<
" variants" << std::endl;
780 for(
unsigned int i=0; i<lfdecs.size(); i++) {
781 if(lfdecs.at(i)==
"")
continue;
782 if(lfdefs.at(i)==
"")
continue;
783 wrpheader <<
"extern FAUDES_API " << lrtypes.at(i) <<
" " << lfdecs.at(i) <<
";" << std::endl;
784 wrpcode << lrtypes.at(i) <<
" " << lfdecs.at(i) <<
" " << lfdefs.at(i) <<
";" << std::endl;
787 std::cout <<
"rti2code: generating convenience wrappers for function \"" << fdef->
Name() <<
"\"" <<
788 " #" << lcount <<
" variants" << std::endl;
795 rtiheader <<
"} // namespace" << std::endl;
800 rticode <<
"}" << std::endl;
801 rticode <<
"} // namespace" << std::endl;
806 wrpheader <<
"} // namespace" << std::endl;
807 wrpcode <<
"} // namespace" << std::endl;