| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113 |
- // Filename: interfaceMakerPythonNative.cxx
- ////////////////////////////////////////////////////////////////////
- //
- // PANDA 3D SOFTWARE
- // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
- //
- // All use of this software is subject to the terms of the Panda 3d
- // Software license. You should have received a copy of this license
- // along with this source code; you will also find a current copy of
- // the license at http://etc.cmu.edu/panda3d/docs/license/ .
- //
- // To contact the maintainers of this program write to
- // [email protected] .
- //
- ////////////////////////////////////////////////////////////////////
- #include "interfaceMakerPythonNative.h"
- #include "interrogateBuilder.h"
- #include "interrogate.h"
- #include "functionRemap.h"
- #include "parameterRemapUnchanged.h"
- #include "typeManager.h"
- #include "interrogateDatabase.h"
- #include "interrogateType.h"
- #include "interrogateFunction.h"
- #include "cppFunctionType.h"
- #include "cppPointerType.h"
- #include "cppTypeDeclaration.h"
- #include "cppStructType.h"
- #include "vector"
- #include "cppParameterList.h"
- #include "algorithm"
- #include <set>
- #include <map>
- extern bool inside_python_native;
- extern InterrogateType dummy_type;
- extern std::string EXPORT_IMPORT_PREFEX;
- #define CLASS_PREFEX "Dtool_"
- #define INSTANCE_PREFEX "Dtool_"
- #define BASE_INSTANCE_NAME "Dtool_PyInstDef"
- /////////////////////////////////////////////////////////
- // Name Remaper...
- // Snagged from ffi py code....
- /////////////////////////////////////////////////////////
- struct RenameSet
- {
- char * _from;
- char * _to;
- int function_type;
- };
- struct FlagSet
- {
- char * _to;
- int function_type;
- };
- ///////////////////////////////////////////////////////////////////////////////////////
- RenameSet methodRenameDictionary[] = {
- { "operator==" , "eq", 0 },
- { "operator!=" , "ne", 0 },
- { "operator<<" , "__lshift__", 0 },
- { "operator>>" , "__rshift__", 0 },
- { "operator<" , "lessThan", 0 },
- { "operator>" , "greaterThan", 0 },
- { "operator<=" , "lessThanOrEqual", 0 },
- { "operator>=" , "greaterThanOrEqual", 0 },
- { "operator=" , "assign", 0 },
- { "operator()" , "__call__", 0 },
- { "operator[]" , "__getitem__", 0 },
- { "operator++" , "increment", 0 },
- { "operator--" , "decrement", 0 },
- { "operator^" , "__xor__", 0 },
- { "operator%" , "__mod__", 0 },
- { "operator!" , "logicalNot", 0 },
- { "operator~" , "bitwiseNot", 0 },
- { "operator&" , "__and__", 0 },
- { "operator&&" , "logicalAnd", 0 },
- { "operator|" , "__or__", 0 },
- { "operator||" , "logicalOr", 0 },
- { "operator+" , "__add__", 0 },
- { "operator-" , "__sub__", 0 },
- { "operator*" , "__mul__", 0 },
- { "operator/" , "__div__", 0 },
- { "operator+=" , "__iadd__", 1 },
- { "operator-=" , "__isub__", 1 },
- { "operator*=" , "__imul__", 1 },
- { "operator/=" , "__idiv__", 1 },
- { "operator," , "concatenate", 0 },
- { "operator|=" , "__ior__", 1 },
- { "operator&=" , "__iand__", 1 },
- { "operator^=" , "__ixor__", 1 },
- { "operator~=" , "bitwiseNotEqual", 0 },
- { "operator->" , "dereference", 0 },
- { "operator<<=" , "__ilshift__", 1 },
- { "operator>>=" , "__irshift__", 1 },
- { "print" , "Cprint", 0 },
- { "CInterval.setT" , "_priv__cSetT", 0 },
- { NULL, NULL, -1 }
- };
- char * InPlaceSet[] = {
- "__iadd__",
- "__isub__",
- "__imul__",
- "__idiv__",
- "__ior__",
- "__iand__",
- "__ixor__",
- "__ilshift__",
- "__irshift__",
- NULL,
- };
- ///////////////////////////////////////////////////////////////////////////////////////
- RenameSet classRenameDictionary[] = {
- { "Loader" , "PandaLoader",0 },
- { "String" , "CString",0 },
- { "LMatrix4f" , "Mat4",0 },
- { "LMatrix3f" , "Mat3",0 },
- { "LVecBase4f" , "VBase4",0 },
- { "LVector4f" , "Vec4",0 },
- { "LPoint4f" , "Point4",0 },
- { "LVecBase3f" , "VBase3",0 },
- { "LVector3f" , "Vec3",0 },
- { "LPoint3f" , "Point3",0 },
- { "LVecBase2f" , "VBase2",0 },
- { "LVector2f" , "Vec2",0 },
- { "LPoint2f" , "Point2",0 },
- { "LQuaternionf" , "Quat",0 },
- { "LMatrix4d" , "Mat4D",0 },
- { "LMatrix3d" , "Mat3D",0 },
- { "LVecBase4d" , "VBase4D",0 },
- { "LVector4d" , "Vec4D",0 },
- { "LPoint4d" , "Point4D",0 },
- { "LVecBase3d" , "VBase3D",0 },
- { "LVector3d" , "Vec3D",0 },
- { "LPoint3d" , "Point3D",0 },
- { "LVecBase2d" , "VBase2D",0 },
- { "LVector2d" , "Vec2D",0 },
- { "LPoint2d" , "Point2D",0 },
- { "LQuaterniond" , "QuatD",0 },
- { "Plane" , "PlaneBase",0 },
- { "Planef" , "Plane",0 },
- { "Planed" , "PlaneD",0 },
- { "Frustum" , "FrustumBase",0 },
- { "Frustumf" , "Frustum",0 },
- { "Frustumd" , "FrustumD",0 },
- { NULL,NULL,-1 }
- };
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- char * pythonKeywords[] = {
- "and",
- "del",
- "for",
- "is",
- "raise",
- "assert",
- "elif",
- "from",
- "lambda",
- "return",
- "break",
- "else",
- "global",
- "not",
- "try",
- "class",
- "except",
- "if",
- "or",
- "while",
- "continue",
- "exec",
- "import",
- "pass",
- "def",
- "finally",
- "in",
- "print",
- NULL};
- ///////////////////////////////////////////////////////////////////////////////////////
- std::string checkKeyword(std::string & cppName)
- {
- for(int x = 0; pythonKeywords[x] != NULL; x++)
- {
- if(cppName == pythonKeywords[x])
- {
- return std::string("_")+cppName;
- }
- }
- return cppName;
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- std::string classNameFromCppName(const std::string &cppName)
- {
- //# initialize to empty string
- std::string className = "";
- //# These are the characters we want to strip out of the name
- const std::string badChars("!@#$%^&*()<>,.-=+~{}? ");
- int nextCap = 0;
- int firstChar = 1;
- for(std::string::const_iterator chr = cppName.begin(); chr != cppName.end(); chr++)
- {
- if (badChars.find(*chr) != std::string::npos)
- {
- }
- else if (*chr == '_')
- {
- nextCap = 1;
- }
- else if (nextCap || firstChar)
- {
- className += toupper(*chr);
- nextCap = 0;
- firstChar = 0;
- }
- else
- {
- className += * chr;
- }
- }
- for(int x = 0; classRenameDictionary[x]._from != NULL; x++)
- {
- if(className == classRenameDictionary[x]._from)
- className = classRenameDictionary[x]._to;
- }
- if (className.empty())
- {
- std::string text = "** ERROR ** Renaming class: " + cppName + " to empty string";
- printf("%s",text.c_str());
- }
- //# FFIConstants.notify.debug('Renaming class: ' + cppName + ' to: ' + className)
- //# Note we do not have to check for keywords because class name are capitalized
- return className;
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- std::string nonClassNameFromCppName(const std::string &cppName_in)
- {
- std::string className = classNameFromCppName(cppName_in);
- //# Make the first character lowercase
- std::string newName;
- int pass = 0;
- for(std::string::const_iterator chr = className.begin(); chr != className.end(); chr++)
- {
- if(pass == 0)
- newName += toupper(*chr);
- else
- newName += tolower(*chr);
- pass++;
- }
- //# Mangle names that happen to be python keywords so they are not anymore
- //newName = checkKeyword(newName)
- return newName;
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- std::string methodNameFromCppName(std::string cppName, const std::string &className)
- {
- std::string methodName;
- std::string badChars(" ");
- int nextCap = 0;
- for(std::string::const_iterator chr = cppName.begin(); chr != cppName.end(); chr++)
- {
- if (badChars.find(*chr) != std::string::npos)
- {
- }
- else if (*chr == '_')
- {
- nextCap = 1;
- }
- else if (nextCap)
- {
- methodName += toupper(*chr);
- nextCap = 0;
- }
- else
- {
- methodName += *chr;
- }
- }
- std::string LookUpName = methodName;
- for(int x = 0; classRenameDictionary[x]._from != NULL; x++)
- {
- if(methodName == methodRenameDictionary[x]._from || (cppName == methodRenameDictionary[x]._from && methodRenameDictionary[x].function_type != 0) )
- {
- methodName = methodRenameDictionary[x]._to;
- }
- }
- if(className.size() > 0)
- {
- LookUpName = className + '.' + methodName;
- for(int x = 0; classRenameDictionary[x]._from != NULL; x++)
- {
- if(methodName == methodRenameDictionary[x]._from)
- methodName = methodRenameDictionary[x]._to;
- }
- }
-
- // # Mangle names that happen to be python keywords so they are not anymore
- methodName = checkKeyword(methodName);
- return methodName;
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- std::string make_safe_name(const std::string & name)
- {
- return InterrogateBuilder::clean_identifier(name);
- /*
- static const char safe_chars2[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
- std::string result = name;
- size_t pos = result.find_first_not_of(safe_chars2);
- while (pos != std::string::npos)
- {
- result[pos] = '_';
- pos = result.find_first_not_of(safe_chars2);
- }
- return result;
- */
- }
- bool isInplaceFunction(const std::string &cppName)
- {
- std::string wname = methodNameFromCppName(cppName,"");
- for(int x = 0; InPlaceSet[x] != NULL; x++)
- if(InPlaceSet[x] == wname)
- return true;
- return false;
- }
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::GetValideChildClasses( std::map< std::string ,CastDetails > &answer, CPPStructType * inclass, const std::string &up_cast_seed, bool downcastposible)
- {
- if(inclass == NULL)
- return;
- CPPStructType::Derivation::const_iterator bi;
- for (bi = inclass->_derivation.begin();
- bi != inclass->_derivation.end();
- ++bi)
- {
- const CPPStructType::Base &base = (*bi);
- // if (base._vis <= V_public)
- // downcastposible = false;
- CPPStructType *base_type = TypeManager::resolve_type(base._base)->as_struct_type();
- if(base_type != NULL)
- {
- std::string scoped_name = base_type->get_fully_scoped_name();
- if(answer.find(scoped_name) == answer.end())
- {
- answer[scoped_name]._can_downcast = downcastposible;
- answer[scoped_name]._to_class_name = scoped_name;
- answer[scoped_name]._structType = base_type;
- if(base._is_virtual)
- answer[scoped_name]._can_downcast = false;
- std::string local_up_cast("( ");
- local_up_cast += scoped_name + " *)"+ up_cast_seed +"";
- answer[scoped_name]._up_cast_string = local_up_cast;
- answer[scoped_name]._is_legal_py_class = isCppTypeLegal(base_type);
- }
- else
- {
- answer[scoped_name]._can_downcast = false;
- }
- GetValideChildClasses(answer,base_type, answer[scoped_name]._up_cast_string,answer[scoped_name]._can_downcast);
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Function : WriteReturnInstance
- //
- ///////////////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::WriteReturnInstance(ostream &out, int indent_level, std::string &return_expr, std::string &ows_memory_flag, const std::string &class_name, CPPType *ctype, bool inplace)
- {
- if(inplace == true)
- {
- indent(out, indent_level)<<"Py_INCREF(self);\n";
- indent(out, indent_level)<<"return self;\n";
- }
- else
- {
- indent(out, indent_level)<<"if("<< return_expr<< " == NULL)\n";
- indent(out, indent_level)<<"{\n";
- indent(out, indent_level)<<" Py_INCREF(Py_None);\n";
- indent(out, indent_level)<<" return Py_None;\n";
- indent(out, indent_level)<<"}\n";
- if(IsPandaTypedObject(ctype->as_struct_type()))
- {
- std::string typestr = "((TypedObject *)" + return_expr + ")->get_type_index()";
- indent(out, indent_level)<<"return DTool_CreatePyInstanceTyped((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(class_name) << ","<< ows_memory_flag<<","<<typestr<<");\n";
- }
- else
- {
- // indent(out, indent_level)<< "if(" << return_expr <<"!= NULL)\n";
- indent(out, indent_level)
- <<"return DTool_CreatePyInstance((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(class_name) << ","<<ows_memory_flag<<");\n";
- }
- }
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::Constructor
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- InterfaceMakerPythonNative::
- InterfaceMakerPythonNative(InterrogateModuleDef *def) :
- InterfaceMakerPython(def)
- {
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::Destructor
- // Access: Public, Virtual
- // Description:
- ////////////////////////////////////////////////////////////////////
- InterfaceMakerPythonNative::
- ~InterfaceMakerPythonNative() {
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::write_prototypes
- // Access: Public, Virtual
- // Description: Generates the list of function prototypes
- // corresponding to the functions that will be output in
- // write_functions().
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_prototypes(ostream &out_code,ostream *out_h)
- {
- inside_python_native = true;
- Functions::iterator fi;
- if(out_h != NULL)
- *out_h << "#include \"py_panda.h\"\n\n";
- out_code << "//********************************************************************\n";
- out_code << "//*** prototypes for .. Glabal\n";
- out_code << "//********************************************************************\n";
- /*
- for (fi = _functions.begin(); fi != _functions.end(); ++fi)
- {
- Function *func = (*fi);
- if(!func->_itype.is_global() && isFunctionLegal(func))
- write_prototype_for(out_code, func);
- }
- */
- Objects::iterator oi;
- for (oi = _objects.begin(); oi != _objects.end(); ++oi)
- {
- Object *object = (*oi).second;
- if(object->_itype.is_class() ||object->_itype.is_struct())
- {
- if(isCppTypeLegal(object->_itype._cpptype))
- {
- if(isExportThisRun(object->_itype._cpptype))
- {
- write_prototypes_class(out_code,out_h,object);
- }
- else
- //write_prototypes_class_external(out_code,object);
- _external_imports.insert(make_safe_name(object->_itype.get_scoped_name()));
- }
- }
- }
- out_code << "//********************************************************************\n";
- out_code << "//*** prototypes for .. Extrernal Objects \n";
- out_code << "//********************************************************************\n";
- for(std::set< std::string >::iterator ii = _external_imports.begin(); ii != _external_imports.end(); ii++)
- out_code << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" <<*ii <<";\n";
- inside_python_native = false;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function : write_prototypes_class_external
- //
- // Description : Output enough enformation to a declartion of a externally
- // generated dtool type object
- /////////////////////////////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_prototypes_class_external(ostream &out, Object * obj)
- {
- std::string class_name = make_safe_name(obj->_itype.get_scoped_name());
- std::string c_class_name = obj->_itype.get_true_name();
- out << "//********************************************************************\n";
- out << "//*** prototypes for external.. " << class_name <<"\n";
- out << "//********************************************************************\n";
- out << "typedef "<< c_class_name <<" "<< class_name <<"_localtype;\n";
- out << "Define_Module_Class_Forward("<< _def->module_name << ", "<< class_name << "," << class_name <<"_localtype,"<< classNameFromCppName(c_class_name) << ");\n";
- }
- ///////////////////////////////////////// ////////////////////////////////////////////////////
- // Function : write_prototypes_class
- //
- /////////////////////////////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_prototypes_class(ostream &out_code,ostream *out_h, Object * obj)
- {
- std::string ClassName = make_safe_name(obj->_itype.get_scoped_name());
- Functions::iterator fi;
-
- out_code << "//********************************************************************\n";
- out_code << "//*** prototypes for .. " << ClassName <<"\n";
- out_code << "//********************************************************************\n";
- /*
- for (fi = obj->_methods.begin(); fi != obj->_methods.end(); ++fi)
- {
- Function *func = (*fi);
- write_prototype_for(out_code, func);
- }
- */
- /*
- for (fi = obj->_constructors.begin(); fi != obj->_constructors.end(); ++fi)
- {
- Function *func = (*fi);
- std::string fname = "int Dtool_Init_"+ClassName+"(PyObject *self, PyObject *args, PyObject *kwds)";
- write_prototype_for_name(out_code, func,fname);
- }
- */
- write_ClasseDeclarations(out_code,out_h,obj);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::write_functions
- // Access: Public, Virtual
- // Description: Generates the list of functions that are appropriate
- // for this interface. This function is called *before*
- // write_prototypes(), above.
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_functions(ostream &out)
- {
- inside_python_native = true;
- out << "//********************************************************************\n";
- out << "//*** Functions for .. Global \n" ;
- out << "//********************************************************************\n";
- Functions::iterator fi;
- for (fi = _functions.begin(); fi != _functions.end(); ++fi)
- {
- Function *func = (*fi);
- if(!func->_itype.is_global() && isFunctionLegal(func))
- write_function_for_top(out, func,"");
- }
- Objects::iterator oi;
- for (oi = _objects.begin(); oi != _objects.end(); ++oi)
- {
- Object *object = (*oi).second;
- if(object->_itype.is_class() ||object->_itype.is_struct())
- {
- if(isCppTypeLegal(object->_itype._cpptype))
- if(isExportThisRun(object->_itype._cpptype))
- write_ClasseDetails(out,object);
- }
- }
- // Objects::iterator oi;
- for (oi = _objects.begin(); oi != _objects.end(); ++oi)
- {
- Object *object = (*oi).second;
- if(!object->_itype.get_outer_class())
- {
- if(object->_itype.is_class() ||object->_itype.is_struct())
- if(isCppTypeLegal(object->_itype._cpptype))
- if(isExportThisRun(object->_itype._cpptype))
- write_module_class(out,object);
- }
- }
- inside_python_native = true;
- }
- ////////////////////////////////////////////////////////////
- // Function : write_ClasseDetails
- ////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_ClasseDetails(ostream &out, Object * obj)
- {
- Functions::iterator fi;
- //std::string cClassName = obj->_itype.get_scoped_name();
- std::string ClassName = make_safe_name(obj->_itype.get_scoped_name());
- std::string cClassName = obj->_itype.get_true_name();
- out << "//********************************************************************\n";
- out << "//*** Functions for .. "<< cClassName <<" \n" ;
- out << "//********************************************************************\n";
- for (fi = obj->_methods.begin(); fi != obj->_methods.end(); ++fi)
- {
- Function *func = (*fi);
- if( (func))
- {
- ostringstream GetThis;
- GetThis << " "<<cClassName << " * local_this = NULL;\n";
- GetThis << " DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<< ClassName<<",(void **)&local_this);\n";
- GetThis << " if(local_this == NULL) {\n";
- GetThis << " // This might happen if Python called a reverse operator like __rsub__().\n";
- GetThis << " Py_INCREF(Py_NotImplemented);\n";
- GetThis << " return Py_NotImplemented;\n";
- GetThis << " }\n";
- write_function_for_top(out, func,GetThis.str());
- }
- }
- // bool AnyLeganConstructors;
- if(obj->_constructors.size() == 0)
- {
- std::string fname = "int Dtool_Init_"+ClassName+"(PyObject *self, PyObject *args, PyObject *kwds)";
- out << fname << "\n";
- out << "{\n";
- out << " PyErr_SetString(PyExc_TypeError, \"Error Can Not Init Constant Class (" << cClassName << ")\");\n";
- out << " return -1;\n" ;
- out << "}\n";
- }
- else
- {
- for (fi = obj->_constructors.begin(); fi != obj->_constructors.end(); ++fi)
- {
- Function *func = (*fi);
- std::string fname = "int Dtool_Init_"+ClassName+"(PyObject *self, PyObject *args, PyObject *kwds) ";
- write_function_for_name(out, func,fname,"",ClassName);
- }
- }
- std::map< string ,CastDetails > details;
- std::map< string ,CastDetails >::iterator di;
- builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(obj->_itype._cpptype)),false);
- GetValideChildClasses(details,obj->_itype._cpptype->as_struct_type());
- for(di = details.begin(); di != details.end(); di++)
- {
- //InterrogateType ptype =idb->get_type(di->first);
- if(di->second._is_legal_py_class && !isExportThisRun(di->second._structType))
- _external_imports.insert(make_safe_name(di->second._to_class_name));
- //out << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(di->second._to_class_name) <<";\n";
- }
-
- { // the Cast Converter
- out << "inline void * Dtool_UpcastInterface_"<< ClassName << "(PyObject *self, Dtool_PyTypedObject *requested_type)\n";
- out << "{\n";
- out << " Dtool_PyTypedObject *SelfType = ((Dtool_PyInstDef *)self)->_My_Type;\n";
- out << " if(SelfType != &Dtool_" << ClassName <<")\n";
- out << " {\n";
- out << " printf(\""<<ClassName<<" ** Bad Source Type-- Requesting Conversion from %s to %s\\n\",((Dtool_PyInstDef *)self)->_My_Type->_name,requested_type->_name);fflush(NULL);\n";;
- out << " return NULL;\n";
- out << " }\n";
- out << " \n";
- out << " "<<cClassName<<" * local_this = (" << cClassName<<" *)((Dtool_PyInstDef *)self)->_ptr_to_object;\n";
- out << " if(requested_type == &Dtool_"<<ClassName<<")\n";
- out << " return local_this;\n";
- for(di = details.begin(); di != details.end(); di++)
- {
- if(di->second._is_legal_py_class)
- {
- out << " if(requested_type == &Dtool_"<<make_safe_name(di->second._to_class_name)<<")\n";
- out << " return "<< di->second._up_cast_string << " local_this;\n";
- }
- }
- out << " return NULL;\n";
- out << "}\n";
- out << "inline void * Dtool_DowncastInterface_"<< ClassName << "(void *from_this, Dtool_PyTypedObject *from_type)\n";
- out << "{\n";
- out << " if(from_this == NULL || from_type == NULL)\n";
- out << " return NULL;\n";
- out << " if(from_type == &Dtool_" << ClassName<<")\n";
- out << " return from_this;\n";
- for(di = details.begin(); di != details.end(); di++)
- {
- if(di->second._can_downcast && di->second._is_legal_py_class)
- {
- out << " if(from_type == &Dtool_"<<make_safe_name(di->second._to_class_name)<<")\n";
- out << " {\n";
- out << " "<< di->second._to_class_name << "* other_this = ("<< di->second._to_class_name << "*)from_this;\n" ;
- out << " return ("<< cClassName << "*)other_this;\n";
- out << " }\n";
- }
- }
- out << " return (void *) NULL;\n";
- out << "}\n";
- }
- }
- ////////////////////////////////////////////////////////////
- /// Function : write_ClasseDeclarations
- //
- //
- ////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_ClasseDeclarations(ostream &out, ostream *out_h,Object * obj )
- {
- const InterrogateType &itype = obj->_itype;
- std::string class_name = make_safe_name(obj->_itype.get_scoped_name());
- std::string c_class_name = itype.get_true_name();
- std::string class_struct_name = std::string(CLASS_PREFEX) +class_name;
- out << "typedef "<< c_class_name <<" "<< class_name <<"_localtype;\n";
- if(obj->_itype.has_destructor() ||
- obj->_itype.destructor_is_inherited())
- {
- if(TypeManager::is_reference_count(obj->_itype._cpptype))
- {
- out << "Define_Module_ClassRef("<< _def->module_name<<"," << class_name << "," << class_name <<"_localtype,"<< classNameFromCppName(c_class_name) <<");\n";
- }
- else
- {
- out << "Define_Module_Class("<<_def->module_name << "," << class_name << "," <<class_name <<"_localtype,"<< classNameFromCppName(c_class_name) <<");\n";
- }
- }
- else
- {
- if(TypeManager::is_reference_count(obj->_itype._cpptype))
- {
- out << "Define_Module_ClassRef_Private("<<_def->module_name << "," << class_name << "," << class_name <<"_localtype,"<< classNameFromCppName(c_class_name) <<");\n";
- }
- else
- {
- out << "Define_Module_Class_Private("<<_def->module_name<< "," << class_name << "," << class_name <<"_localtype,"<< classNameFromCppName(c_class_name) << ");\n";
- }
- }
- if(out_h != NULL)
- *out_h << "extern \"C\" " << EXPORT_IMPORT_PREFEX << " struct Dtool_PyTypedObject Dtool_" << class_name <<";\n";
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::write_module
- // Access: Public, Virtual
- // Description: Generates whatever additional code is required to
- // support a module file.
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_sub_module(ostream &out, Object *obj)
- {
- //Object * obj = _objects[_embeded_index] ;
- std::string ClassName = make_safe_name(obj->_itype.get_scoped_name());
- out << "//********************************************************************\n";
- out << "//*** Module Init Updcall .." << obj->_itype.get_scoped_name() << "\n";
- out << "//********************************************************************\n";
- out << " Dtool_PyModuleClassInit_"<< ClassName <<"(module);\n";
- }
- /////////////////////////////////////////////////////////////////////////////
- // Function : write_module_support
- /////////////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_module_support(ostream &out,ostream *out_h,InterrogateModuleDef *moduledefdef)
- {
- out << "//********************************************************************\n";
- out << "//*** Module Object Linker .. \n";
- out << "//********************************************************************\n";
- out << "static void BuildInstants(PyObject * module)\n";
- out << "{\n";
- Objects::iterator oi;
- for (oi = _objects.begin(); oi != _objects.end(); ++oi)
- {
- Object *object = (*oi).second;
- if(!object->_itype.get_outer_class())
- {
- if(object->_itype.is_enum())
- {
- int enum_count = object->_itype.number_of_enum_values();
- if(enum_count > 0)
- {
- out << "//********************************************************************\n";
- out << "//*** Module Enums .." << object->_itype.get_scoped_name() << "\n";
- out << "//********************************************************************\n";
- }
- for(int xx = 0; xx< enum_count; xx++)
- out << " PyModule_AddIntConstant(module,\"" << classNameFromCppName(object->_itype.get_enum_value_name(xx)) <<"\","<< object->_itype.get_enum_value(xx) << ");\n";
- }
- }
- }
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- int num_manifests = idb->get_num_global_manifests();
- for (int mi = 0; mi < num_manifests; mi++)
- {
- ManifestIndex manifest_index = idb->get_global_manifest(mi);
- const InterrogateManifest &iman = idb->get_manifest(manifest_index);
- if (iman.has_getter())
- {
- FunctionIndex func_index = iman.get_getter();
- record_function(dummy_type, func_index);
- }
- if(iman.has_int_value())
- out << " PyModule_AddIntConstant(module,\"" << classNameFromCppName(iman.get_name()) <<"\","<< iman.get_int_value() << ");\n";
- else
- out << " PyModule_AddStringConstant(module,\"" << classNameFromCppName(iman.get_name()) <<"\",\""<< iman.get_definition().c_str() << "\");\n";
- }
- for (oi = _objects.begin(); oi != _objects.end(); ++oi)
- {
- Object *object = (*oi).second;
- if(!object->_itype.get_outer_class())
- {
- if(object->_itype.is_class() ||object->_itype.is_struct())
- if(isCppTypeLegal(object->_itype._cpptype))
- if(isExportThisRun(object->_itype._cpptype))
- write_sub_module(out,object);
- }
- }
- out << "//********************************************************************\n";
- out << "//*** Module Init Updcall .. Externally Defined Class\n";
- out << "//********************************************************************\n";
- // for(std::set< std::string >::iterator ii = _external_imports.begin(); ii != _external_imports.end(); ii++)
- // out << "Dtool_" <<*ii <<"._Dtool_ClassInit(NULL);\n";
- out << "}\n";
- bool force_base_functions = true;
- out << "static PyMethodDef python_simple_funcs[] = {\n";
- Functions::iterator fi;
- for (fi = _functions.begin(); fi != _functions.end(); ++fi)
- {
- Function *func = (*fi);
- if(!func->_itype.is_global() && isFunctionLegal(func))
- {
- {
- out << " { \"" << methodNameFromCppName( func->_ifunc.get_name(),"") << "\", (PyCFunction) &"
- << func->_name << ", METH_VARARGS| METH_KEYWORDS ," << func->_name << "_comment},\n";
- }
- }
- }
- if(force_base_functions)
- {
- out << " //Support Function For Dtool_types ... for know in each module ??\n";
- out << " {\"Dtool_BarrowThisRefrence\", &Dtool_BarrowThisRefrence,METH_VARARGS,\"Used to barrow 'this' poiner ( to, from)\\n Assumes no ownership\"}, \n";
- out << " {\"Dtool_AddToDictionary\", &Dtool_AddToDictionary,METH_VARARGS,\"Used to Items Into a types (tp_dict)\"}, \n";
- }
- out << " { NULL, NULL ,0,NULL}\n" << "};\n\n";
- out << "struct LibrayDef " << moduledefdef->library_name <<"_moddef = {python_simple_funcs,BuildInstants};\n";
- if(out_h != NULL)
- *out_h << "extern struct LibrayDef " << moduledefdef->library_name <<"_moddef;\n";
- }
- /////////////////////////////////////////////////////////////////////////////
- ///// Function : write_module
- /////////////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_module(ostream &out,ostream *out_h, InterrogateModuleDef *moduledefdef)
- {
- InterfaceMakerPython::write_module(out,out_h, moduledefdef);
- Objects::iterator oi;
- out << "//********************************************************************\n";
- out << "//*** Py Init Code For .. GlobalScope\n" ;
- out << "//********************************************************************\n";
- out << "#ifdef _WIN32\n"
- << "extern \"C\" __declspec(dllexport) void init" << moduledefdef->module_name << "();\n"
- << "#else\n"
- << "extern \"C\" void init" << moduledefdef->module_name << "();\n"
- << "#endif\n\n";
- out << "void init" << moduledefdef->module_name << "() {\n";
- out << " LibrayDef * refs[] = {&" << moduledefdef->library_name << "_moddef,NULL};\n";
- out << " Dtool_PyModuleInitHelper(refs,\"" << moduledefdef->module_name << "\");\n";
- out << "}\n\n";
- }
- ////////////////////////////////////////////////////////////////////
- /// Function : GetSlotedFunctinDef
- //
- // This function is used to define special behavior for class functions..
- // main use is to encode the slot pointer logic and function call
- // conventions for the slaot interface..
- ////////////////////////////////////////////////////////////////////
- bool GetSlotedFunctinDef(const std::string &thimputstring, std::string &answer_location, int &wraper_type)
- {
- wraper_type = -1;
- if(thimputstring.size() > 4 && thimputstring[0] == '_' && thimputstring[1] == '_')
- {
- if(thimputstring == "__add__")
- {
- answer_location = "tp_as_number->nb_add";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__sub__")
- {
- answer_location = "tp_as_number->nb_subtract";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__mul__")
- {
- answer_location = "tp_as_number->nb_multiply";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__div__")
- {
- answer_location = "tp_as_number->nb_divide";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__mod__")
- {
- answer_location = "tp_as_number->nb_remainder";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__lshift__")
- {
- answer_location = "tp_as_number->nb_lshift";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__rshift__")
- {
- answer_location = "tp_as_number->nb_rshift";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__xor__")
- {
- answer_location = "tp_as_number->nb_xor";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__and__")
- {
- answer_location = "tp_as_number->nb_and";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__or__")
- {
- answer_location = "tp_as_number->nb_or";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__iadd__")
- {
- answer_location = "tp_as_number->nb_inplace_add";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__isub__")
- {
- answer_location = "tp_as_number->nb_inplace_subtract";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__imul__")
- {
- answer_location = "tp_as_number->nb_inplace_multiply";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__idiv__")
- {
- answer_location = "tp_as_number->nb_inplace_divide";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__imod__")
- {
- answer_location = ".tp_as_number->nb_inplace_remainder";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__ilshift__")
- {
- answer_location = "tp_as_number->nb_inplace_lshift";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__irshift__")
- {
- answer_location = "tp_as_number->nb_inplace_rshift";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__iand__")
- {
- answer_location = "tp_as_number->nb_inplace_and";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__ixor__")
- {
- answer_location = "tp_as_number->nb_inplace_xor";
- wraper_type = 3;
- return true;
- }
- if(thimputstring == "__int__")
- {
- answer_location = "tp_as_number->nb_int";
- wraper_type = 2;
- return true;
- }
- // if(thimputstring == "__coerce__")
- // {
- // answer_location = "tp_as_number->nb_coerce";
- // return true;
- // }
- // mapping methods
- if(thimputstring == "__getitem__")
- {
- answer_location = "tp_as_mapping->mp_subscript";
- wraper_type = 3;
- return true;
- }
- //Direct methods
- if(thimputstring == "__call__")
- {
- answer_location = "tp_call";
- //wraper_type = 1;
- return true;
- }
- }
- return false;
- };
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function :write_module_class
- /////////////////////////////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::
- write_module_class(ostream &out, Object *obj) {
- bool has_local_hash = false;
- bool has_local_repr = false;
- bool has_local_str = false;
- {
- int num_nested = obj->_itype.number_of_nested_types();
- for (int ni = 0; ni < num_nested; ni++)
- {
- TypeIndex nested_index = obj->_itype.get_nested_type(ni);
- Object * nested_obj = _objects[nested_index];
- if(nested_obj->_itype.is_class() ||nested_obj->_itype.is_struct())
- {
- write_module_class(out,nested_obj);
- }
- }
- }
- bool is_runtime_typed = IsPandaTypedObject(obj->_itype._cpptype->as_struct_type());
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- std::string ClassName = make_safe_name(obj->_itype.get_scoped_name());
- std::string cClassName = obj->_itype.get_true_name();
- std::string export_calss_name = classNameFromCppName(obj->_itype.get_name());
- Functions::iterator fi;
- out << "//********************************************************************\n";
- out << "//*** Py Init Code For .. "<< ClassName <<" | " << export_calss_name <<"\n" ;
- out << "//********************************************************************\n";
- out << "PyMethodDef Dtool_Methods_"<< ClassName << "[]= {\n";
- std::map<int , Function * > static_functions;
- std::map<Function *, std::string > normal_Operator_functions;
- std::map<Function *, std::pair< std::string , int> > wraped_Operator_functions;
- // function Table
- int x;
- for (x = 0, fi = obj->_methods.begin(); fi != obj->_methods.end(); ++fi,x++)
- {
- Function *func = (*fi);
- std::string temp0;
- int temp1;
- if(!GetSlotedFunctinDef( methodNameFromCppName( func->_ifunc.get_name(),export_calss_name),temp0,temp1))
- {
- out << " { \"" << methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) << "\",(PyCFunction ) &"
- << func->_name << ", METH_VARARGS| METH_KEYWORDS ," << func->_name << "_comment},\n";
- if(!isFunctionWithThis(func))
- static_functions[x] = func;
- }
- else
- {
- if(temp1 > 0)
- {
- wraped_Operator_functions[func] = std::pair< std::string, int>(temp0,temp1);
- out << " { \"" << methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) << "\",(PyCFunction ) &"
- << func->_name << ", METH_VARARGS| METH_KEYWORDS ," << func->_name << "_comment},\n";
- if(!isFunctionWithThis(func))
- static_functions[x] = func;
- }
- else
- {
- normal_Operator_functions[func] = temp0;
- out << " { \"" << methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) << "\",(PyCFunction ) &"
- << func->_name << ", METH_VARARGS| METH_KEYWORDS ," << func->_name << "_comment},\n";
- if(!isFunctionWithThis(func))
- static_functions[x] = func;
- }
- }
- }
- out << " { NULL, NULL }\n"
- << "};\n\n";
- int num_derivations = obj->_itype.number_of_derivations();
- int di;
- for (di = 0; di < num_derivations; di++)
- {
- TypeIndex d_type_Index = obj->_itype.get_derivation(di);
- if(!interrogate_type_is_unpublished(d_type_Index))
- {
- const InterrogateType &d_itype = idb->get_type(d_type_Index);
- if(isCppTypeLegal(d_itype._cpptype))
- {
- if(!isExportThisRun(d_itype._cpptype))
- {
- _external_imports.insert(make_safe_name(d_itype.get_scoped_name().c_str()));
- //out << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(d_itype.get_scoped_name().c_str()) <<";\n";
- }
- }
- }
- }
- std::vector< std::string > bases;
- for (di = 0; di < num_derivations; di++)
- {
- TypeIndex d_type_Index = obj->_itype.get_derivation(di);
- if(!interrogate_type_is_unpublished(d_type_Index))
- {
- const InterrogateType &d_itype = idb->get_type(d_type_Index);
- if(isCppTypeLegal(d_itype._cpptype))
- {
- bases.push_back(make_safe_name(d_itype.get_scoped_name().c_str()));
- }
- }
- }
- if(bases.empty())
- bases.push_back("DTOOL_SUPPER_BASE");
- {
- std::map<Function *, std::pair< std::string , int> >::iterator rfi; // wraped_Operator_functions;
- for(rfi = wraped_Operator_functions.begin(); rfi != wraped_Operator_functions.end(); rfi++)
- {
- if(rfi->second.second == 1)
- {
- Function *func = rfi->first;
- out << "//////////////////\n";
- out << "// Required TO Convert the calling Conventions.. \n";
- out << "// " <<ClassName<< " ..." << rfi->second.first <<" = "<< methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) <<"\n";
- out << "//////////////////\n";
- out << "static PyObject * " << func->_name << methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) << "( PyObject * self, PyObject * args, PyObject *dict)\n";
- out << "{\n";
- out << " return "<< func->_name <<"(self,args);\n";
- out << "}\n\n";
- }
- else if(rfi->second.second == 2)
- {
- Function *func = rfi->first;
- out << "//////////////////\n";
- out << "// Required TO Convert the calling Conventions.. \n";
- out << "// " <<ClassName<< " ..." << rfi->second.first <<" = "<< methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) <<"\n";
- out << "//////////////////\n";
- out << "static PyObject * " << func->_name << methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) << "( PyObject * self)\n";
- out << "{\n";
- out << " return "<< func->_name <<"(self,Py_None,Py_None);\n";
- out << "}\n\n";
- }
- if(rfi->second.second == 3)
- {
- Function *func = rfi->first;
- out << "//////////////////\n";
- out << "// Required TO Convert the calling Conventions.. \n";
- out << "// " <<ClassName<< " ..." << rfi->second.first <<" = "<< methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) <<"\n";
- out << "//////////////////\n";
- out << "static PyObject * " << func->_name << methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) << "( PyObject * self, PyObject * args)\n";
- out << "{\n";
- out << " return "<< func->_name <<"(self,args,Py_None);\n";
- out << "}\n\n";
- }
- }
- if(HasAGetKeyFunction(obj->_itype))
- {
- out << "//////////////////\n";
- out << "// A LocalHash(getKey) Function for this type";
- out << "// " <<ClassName << "\n";
- out << "//////////////////\n";
- out << "static long DTool_HashKey_"<<ClassName << "(PyObject * self)\n";
- out << "{\n";
- out << " "<<cClassName << " * local_this = NULL;\n";
- out << " DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<< ClassName<<",(void **)&local_this);\n";
- out << " if(local_this == NULL)\n";
- out << " {\n";
- out << " return -1;\n";
- out << " };\n";
- out << " return local_this->get_key();\n";
- out << "}\n\n";
- has_local_hash = true;
- }
- else
- {
- if(bases.size() == 0)
- {
- out << "//////////////////\n";
- out << "// A LocalHash(This Pointer) Function for this type";
- out << "// " <<ClassName << "\n";
- out << "//////////////////\n";
- out << "static long DTool_HashKey_"<<ClassName << "(PyObject * self)\n";
- out << "{\n";
- out << " "<<cClassName << " * local_this = NULL;\n";
- out << " DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<< ClassName<<",(void **)&local_this);\n";
- out << " if(local_this == NULL)\n";
- out << " {\n";
- out << " return -1;\n";
- out << " };\n";
- out << " return (long)local_this;\n";
- out << "}\n\n";
- has_local_hash = true;
- }
- }
- int need_repr = NeedsAReprFunction(obj->_itype);
- if(need_repr > 0)
- {
- out << "//////////////////\n";
- out << "// A __repr__ Function\n";
- out << "// " <<ClassName << "\n";
- out << "//////////////////\n";
- out << "static PyObject * Dtool_Repr_"<<ClassName << "(PyObject * self)\n";
- out << "{\n";
- out << " "<<cClassName << " * local_this = NULL;\n";
- out << " DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<< ClassName<<",(void **)&local_this);\n";
- out << " if(local_this != NULL)\n";
- out << " {\n";
- out << " ostringstream os;\n";
- if (need_repr == 2) {
- out << " local_this->output(os);\n";
- } else {
- out << " local_this->python_repr(os, \""
- << classNameFromCppName(ClassName) << "\");\n";
- }
- out << " std::string ss = os.str();\n";
- out << " return PyString_FromStringAndSize(ss.data(),ss.length());\n";
- out << " };\n";
- out << " return Py_BuildValue(\"\");\n";
- out << "}\n";
- has_local_repr = true;
- }
- int need_str = NeedsAStrFunction(obj->_itype);
- if(need_str > 0)
- {
- out << "//////////////////\n";
- out << "// A __str__ Function\n";
- out << "// " <<ClassName << "\n";
- out << "//////////////////\n";
- out << "static PyObject * Dtool_Str_"<<ClassName << "(PyObject * self)\n";
- out << "{\n";
- out << " "<<cClassName << " * local_this = NULL;\n";
- out << " DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<< ClassName<<",(void **)&local_this);\n";
- out << " if(local_this != NULL)\n";
- out << " {\n";
- out << " ostringstream os;\n";
- if(need_str == 2)
- out << " local_this->write(os,0);\n";
- else
- out << " local_this->write(os);\n";
- out << " std::string ss = os.str();\n";
- out << " return PyString_FromStringAndSize(ss.data(),ss.length());\n";
- out << " };\n";
- out << " return Py_BuildValue(\"\");\n";
- out << "}\n";
- has_local_str = true;
- }
- }
- out << "void Dtool_PyModuleClassInit_" << ClassName << "(PyObject *module)\n";
- out << "{\n";
- out << " static bool initdone = false;\n";
- out << " if(!initdone)\n";
- out << " {\n";
- out << " initdone = true;\n";
- // out << " memset(Dtool_"<< ClassName << ".As_PyTypeObject().tp_as_number,0,sizeof(PyNumberMethods));\n";
- // out << " memset(Dtool_"<< ClassName << ".As_PyTypeObject().tp_as_mapping,0,sizeof(PyMappingMethods));\n";
- // out << " static Dtool_PyTypedObject *InheritsFrom[] = {";
- // add doc string
- if (obj->_itype.has_comment()) {
- out << "#ifndef NDEBUG\n";
- out << " // Class documentation string\n";
- out << " Dtool_" << ClassName
- << ".As_PyTypeObject().tp_doc =\n";
- output_quoted(out, 10, obj->_itype.get_comment());
- out << ";\n"
- << "#endif\n";
- }
- // add bases///
- if(bases.size() > 0)
- {
- out << " // Dependent Objects \n";
- std::string format1= "";
- std::string format2= "";
- for(std::vector< std::string >::iterator bi = bases.begin(); bi != bases.end(); bi++)
- {
- format1 += "O";
- format2 += ",&Dtool_" + *bi + ".As_PyTypeObject()";
- out << " Dtool_"<< make_safe_name(*bi) << "._Dtool_ClassInit(NULL);\n";
- }
- out << " Dtool_"<<ClassName<<".As_PyTypeObject().tp_bases = Py_BuildValue(\"(" << format1 << ")\""<< format2 << ");\n";
- }
- // get dictionary
- out << " Dtool_" << ClassName << ".As_PyTypeObject().tp_dict = PyDict_New();\n";
- out << " PyDict_SetItemString(Dtool_"<<ClassName <<".As_PyTypeObject().tp_dict,\"DtoolClassDict\",Dtool_"<<ClassName <<".As_PyTypeObject().tp_dict);\n";
-
- // the standard call functions
- std::map<Function *, std::string >::iterator ofi;
- for(ofi = normal_Operator_functions.begin(); ofi != normal_Operator_functions.end(); ofi++)
- {
- Function *func = ofi->first;
- out << " // " << ofi->second <<" = "<< methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) <<"\n";
- out << " Dtool_" << ClassName <<".As_PyTypeObject()." << ofi->second <<" = &" << func->_name <<";\n";
- }
- // wraped functions...
- {
- std::map<Function *, std::pair< std::string , int> >::iterator rfi; // wraped_Operator_functions;
- for(rfi = wraped_Operator_functions.begin(); rfi != wraped_Operator_functions.end(); rfi++)
- {
- Function *func = rfi->first;
- out << " // " << rfi->second.first <<" = "<< methodNameFromCppName( func->_ifunc.get_name(),export_calss_name) <<"\n";
- out << " Dtool_" << ClassName <<".As_PyTypeObject()." << rfi->second.first <<" = &" << func->_name << methodNameFromCppName( func->_ifunc.get_name(),export_calss_name)<<";\n";
- }
- }
- // compare and hash work together in PY inherit behavior hmm grrr
- // __hash__
- if(has_local_hash == true)
- {
- out << " // __hash__\n";
- out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_hash = &DTool_HashKey_"<<ClassName <<";\n";
- out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_compare = &DTOOL_PyObject_Compare;\n";
-
- }
- if(has_local_repr == true)
- {
- out << " // __repr__\n";
- out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_repr = & Dtool_Repr_"<<ClassName <<";\n";
- }
- if(has_local_str == true)
- {
- out << " // __str__\n";
- out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_str = & Dtool_Str_"<<ClassName <<";\n";
- }
- else if(has_local_repr == true)
- {
- out << " // __str__ Repr Proxy\n";
- out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_str = & Dtool_Repr_"<<ClassName <<";\n";
- }
- int num_nested = obj->_itype.number_of_nested_types();
- for (int ni = 0; ni < num_nested; ni++)
- {
- TypeIndex nested_index = obj->_itype.get_nested_type(ni);
- Object * nested_obj = _objects[nested_index];
- if(nested_obj->_itype.is_class() ||nested_obj->_itype.is_struct())
- {
- std::string ClassName1 = make_safe_name(nested_obj->_itype.get_scoped_name());
- std::string ClassName2 = make_safe_name(nested_obj->_itype.get_name());
- out << " // Nested Object "<< ClassName1 << ";\n";
- out << " Dtool_" << ClassName1 << "._Dtool_ClassInit(NULL);\n";
- out << " PyDict_SetItemString(Dtool_" << ClassName << ".As_PyTypeObject().tp_dict,\"" << classNameFromCppName(ClassName2) <<"\",(PyObject *)&Dtool_" << ClassName1 << ".As_PyTypeObject());\n";
- }
- else
- {
- if(nested_obj->_itype.is_enum())
- {
- out << " // Enum "<< nested_obj->_itype.get_scoped_name() << ";\n";
- int enum_count = nested_obj->_itype.number_of_enum_values();
- for(int xx = 0; xx< enum_count; xx++)
- out << " PyDict_SetItemString(Dtool_" << ClassName << ".As_PyTypeObject().tp_dict,\"" << classNameFromCppName(nested_obj->_itype.get_enum_value_name(xx)) <<"\",PyInt_FromLong("<< nested_obj->_itype.get_enum_value(xx) << "));\n";
- }
- }
- }
- out << " if(PyType_Ready(&Dtool_"<< ClassName << ".As_PyTypeObject()) < 0)\n";
- out << " {\n";
- out << " PyErr_SetString(PyExc_TypeError, \"PyType_Ready("<< ClassName << ")\");\n";
- out << " printf(\" Error In PyType_Ready" << ClassName << "\");\n";
- out << " return;\n";
- out << " }\n";
- out << " Py_INCREF(&Dtool_"<< ClassName << ".As_PyTypeObject());\n";
- out << " PyDict_SetItemString(Dtool_"<<ClassName <<".As_PyTypeObject().tp_dict,\""<<export_calss_name<< "\",&Dtool_"<<ClassName <<".As_PyObject());\n";
- // static function into dictionary with bogus self..
- //
- std::map<int , Function * >::iterator sfi;
- for(sfi= static_functions.begin(); sfi != static_functions.end(); sfi++)
- {
- out << " // Static Method " << methodNameFromCppName( sfi->second->_ifunc.get_name(),export_calss_name) << "\n";
- out << " PyDict_SetItemString(Dtool_" << ClassName << ".As_PyTypeObject().tp_dict,\"" ;
- out << methodNameFromCppName( sfi->second->_ifunc.get_name(),export_calss_name) ;
- out << "\",PyCFunction_New(&Dtool_Methods_"<< ClassName <<"[" << sfi->first << "],&Dtool_"<< ClassName<< ".As_PyObject()));\n";
- }
- if(is_runtime_typed)
- out << " RegisterRuntimeClass(&Dtool_"<<ClassName<<","<< cClassName <<"::get_class_type().get_index());\n";
- else
- out << " RegisterRuntimeClass(&Dtool_"<<ClassName<<",-1);\n";
- out << " }\n";
- out << " if(module != NULL)\n";
- out << " {\n";
- out << " Py_INCREF(&Dtool_"<< ClassName << ".As_PyTypeObject());\n";
- out << " PyModule_AddObject(module, \""<<export_calss_name<<"\",(PyObject *)&Dtool_"<< ClassName << ".As_PyTypeObject());\n";
- out << " }\n";
- out << "}\n";
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::synthesize_this_parameter
- // Access: Public, Virtual
- // Description: This method should be overridden and redefined to
- // return true for interfaces that require the implicit
- // "this" parameter, if present, to be passed as the
- // first parameter to any wrapper functions.
- ////////////////////////////////////////////////////////////////////
- bool InterfaceMakerPythonNative::
- synthesize_this_parameter() {
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::get_wrapper_prefix
- // Access: Protected, Virtual
- // Description: Returns the prefix string used to generate wrapper
- // function names.
- ////////////////////////////////////////////////////////////////////
- string InterfaceMakerPythonNative::
- get_wrapper_prefix() {
- return "Dtool_";
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::get_unique_prefix
- // Access: Protected, Virtual
- // Description: Returns the prefix string used to generate unique
- // symbolic names, which are not necessarily C-callable
- // function names.
- ////////////////////////////////////////////////////////////////////
- string InterfaceMakerPythonNative::get_unique_prefix()
- {
- return "Dtool_";
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::record_function_wrapper
- // Access: Protected, Virtual
- // Description: Associates the function wrapper with its function in
- // the appropriate structures in the database.
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::record_function_wrapper(InterrogateFunction &ifunc, FunctionWrapperIndex wrapper_index)
- {
- ifunc._python_wrappers.push_back(wrapper_index);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::write_prototype_for
- // Access: Private
- // Description: Writes the prototype for the indicated function.
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_prototype_for(ostream &out, InterfaceMaker::Function *func)
- {
- std::string fname = "PyObject *"+func->_name+"(PyObject *self, PyObject *args)";
- write_prototype_for_name(out,func,fname);
- }
- ////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_prototype_for_name(ostream &out, InterfaceMaker::Function *func, const std::string &function_namename)
- {
- Function::Remaps::const_iterator ri;
- // for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri) {
- // FunctionRemap *remap = (*ri);
- if (!output_function_names) {
- // If we're not saving the function names, don't export it from
- // the library.
- out << "static ";
- } else {
- out << "extern \"C\" ";
- }
- out << function_namename <<";\n";
- // }
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::write_function_for
- // Access: Private
- // Description: Writes the definition for a function that will call
- // the indicated C++ function or method.
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_function_for_top(ostream &out, InterfaceMaker::Function *func, const std::string &PreProcess)
- {
- std::string fname = "static PyObject *"+func->_name+"(PyObject *self, PyObject *args,PyObject *kwds)";
- write_function_for_name(out,func,fname,PreProcess,"");
- }
- ////////////////////////////////////////////////////////////////////
- /// Function : write_function_for_name
- //
- // Wrap a complete name override function for Py.....
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::
- write_function_for_name(ostream &out1, InterfaceMaker::Function *func,
- const std::string &function_name,
- const std::string &PreProcess,
- const std::string &ClassName) {
- ostringstream forward_decl;
- ostringstream out;
- std::map<int , std::set<FunctionRemap *> > MapSets;
- std::map<int , std::set<FunctionRemap *> >::iterator mii;
- std::set<FunctionRemap *>::iterator sii;
- Function::Remaps::const_iterator ri;
- out1 << "/******************************************************************\n" << " * Python type method wrapper for\n";
- for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri) {
- FunctionRemap *remap = (*ri);
- if (isRemapLegal(*remap)) {
- int parameter_size = remap->_parameters.size();
- if(remap->_has_this && remap->_type != FunctionRemap::T_constructor)
- parameter_size --;
-
- MapSets[parameter_size].insert(remap);
- out1 << " * ";
- remap->write_orig_prototype(out1, 0);
- out1 << "\n";
- } else {
- out1 << " * Rejected Remap [";
- remap->write_orig_prototype(out1, 0);
- out1 << "]\n";
- }
- }
- out1 << " *******************************************************************/\n";
- out << function_name << " {\n";
- if (isFunctionWithThis(func)) {
- out << PreProcess;
- }
- bool is_inplace = isInplaceFunction(func->_ifunc.get_name());
- if (MapSets.empty()) {
- return;
- }
- std::string FunctionComment = func->_ifunc._comment;
- std::string FunctionComment1;
- if (FunctionComment.size() > 2) {
- FunctionComment += "\n";
- }
- bool constructor = false;
- if (MapSets.size() > 1) {
- string expected_params;
-
- indent(out,4) << "int parameter_count = 1;\n";
- indent(out,4) << "if(PyTuple_Check(args))\n";
- indent(out,4) << "{\n";
- indent(out,4) << " parameter_count = PyTuple_Size(args);\n" ;
- indent(out,4) << " if(kwds != NULL && PyDict_Check(kwds))\n";
- indent(out,4) << " parameter_count += PyDict_Size(kwds);\n" ;
- indent(out,4) << "}\n";
-
- indent(out,4) << "switch(parameter_count)\n";
- indent(out,4) << "{\n";
- for (mii = MapSets.begin(); mii != MapSets.end(); mii ++) {
- indent(out,4) << "case(" << mii->first << "):\n";
- indent(out,8) << "{\n";
-
- write_function_forset(out,func,mii->second,expected_params,8,forward_decl,ClassName + function_name, is_inplace);
- if ((*mii->second.begin())->_type == FunctionRemap::T_constructor) {
- constructor = true;
- }
-
-
- indent(out,8)<< "}\n";
- indent(out,8)<< "break;\n";
- }
-
- indent(out,4)<< "default:\n";
- indent(out,8)<< "{\n";
- indent(out,12)
- << "PyErr_Format(PyExc_TypeError, \""
- << func->_remaps[0]->_cppfunc->get_local_name(&parser)
- << "() takes ";
- // We add one to the parameter count for "self", following the
- // Python convention.
- int add_self = func->_has_this ? 1 : 0;
- size_t mic;
- for (mic = 0, mii = MapSets.begin();
- mii != MapSets.end();
- ++mii, ++mic) {
- if (mic == MapSets.size() - 1) {
- if (mic == 1) {
- out << " or ";
- } else {
- out << ", or ";
- }
- } else if (mic != 0) {
- out << ", ";
- }
- out << mii->first + add_self;
- }
- out << " arguments (%d given)\", parameter_count + " << add_self << ");\n";
-
- if (constructor)
- indent(out,12) << "return -1;\n";
- else
- indent(out,12) << "return (PyObject *) NULL; \n";
-
- indent(out,8)<< "}\n";
- indent(out,8)<< "break;\n";
- indent(out,4)<< "}\n";
-
- out << " if(!PyErr_Occurred()) // let error pass on \n";
- out << " PyErr_SetString(PyExc_TypeError, \n";
- out << " \"Arguments must match one of:\\n\"\n";
- output_quoted(out, 10, expected_params);
- out << ");\n";
- if (constructor)
- indent(out,4) << "return -1;\n";
- else
- indent(out,4) << "return (PyObject *) NULL; \n";
-
- if(!expected_params.empty() && FunctionComment1.empty())
- FunctionComment1 += "C++ Interface:\n";
-
- FunctionComment1 += expected_params;
- } else {
- string expected_params = "";
- for (mii = MapSets.begin(); mii != MapSets.end(); mii ++) {
- write_function_forset(out,func,mii->second,expected_params,4,forward_decl,ClassName + function_name,is_inplace);
- if((*mii->second.begin())->_type == FunctionRemap::T_constructor) {
- constructor = true;
- }
- }
- out << " if(!PyErr_Occurred())\n";
- out << " PyErr_SetString(PyExc_TypeError,\n";
- out << " \"Must Match :\\n\"\n";
- output_quoted(out, 10, expected_params);
- out << ");\n";
- if (constructor)
- indent(out,4) << "return -1;\n";
- else
- indent(out,4) << "return (PyObject *) NULL; \n";
-
- if (!expected_params.empty() && FunctionComment1.empty()) {
- FunctionComment1 += "C++ Interface:\n";
- }
-
- FunctionComment1 += expected_params;
- }
- out << "}\n\n";
- if (!FunctionComment1.empty()) {
- FunctionComment = FunctionComment1 + "\n" + FunctionComment;
- }
- if (!constructor) {
- // Write out the function doc string. We only do this if it is
- // not a constructor, since we don't have a place to put the
- // constructor doc string.
- out << "#ifndef NDEBUG\n";
- out << "static char * " << func->_name << "_comment =\n";
- output_quoted(out, 4, FunctionComment);
- out << ";\n";
- out << "#else\n";
- out << "static char * " << func->_name << "_comment = NULL;\n";
- out << "#endif\n";
- }
- out << "\n";
- out1 << forward_decl.str();
- out1 << out.str();
- }
- ////////////////////////////////////////////////////////
- // Function : GetParnetDepth
- //
- // Support Function used to Sort the name based overrides.. For know must be complex to simple
- ////////////////////////////////////////////////////////
- int GetParnetDepth(CPPType *type)
- {
- int answer = 0;
- // printf(" %s\n",type->get_local_name().c_str());
- if (TypeManager::is_basic_string_char(type)) {
- } else if (TypeManager::is_basic_string_wchar(type)) {
- } else if (TypeManager::is_bool(type)) {
- } else if (TypeManager::is_unsigned_longlong(type)) {
- } else if (TypeManager::is_longlong(type)) {
- } else if (TypeManager::is_integer(type)) {
- } else if (TypeManager::is_float(type)) {
- } else if (TypeManager::is_char_pointer(type)) {
- } else if (TypeManager::is_pointer_to_PyObject(type)) {
- } else if (TypeManager::is_pointer(type) ||TypeManager::is_reference(type) || TypeManager::is_struct(type) )
- {
- answer ++;
- int deepest = 0;
- TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(type)),false);
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- const InterrogateType &itype = idb->get_type(type_index);
- if(itype.is_class() ||itype.is_struct())
- {
- int num_derivations = itype.number_of_derivations();
- for (int di = 0; di < num_derivations; di++)
- {
- TypeIndex d_type_Index = itype.get_derivation(di);
- const InterrogateType &d_itype = idb->get_type(d_type_Index);
- int this_one = GetParnetDepth(d_itype._cpptype);
- if(this_one > deepest)
- deepest = this_one;
- }
- }
- answer += deepest;
- // printf(" Class Name %s %d\n",itype.get_name().c_str(),answer);
- }
- // printf(" Class Name %s %d\n",itype.get_name().c_str(),answer);
- return answer;
- }
- ////////////////////////////////////////////////////////
- // The Core sort function for remap calling orders..
- //////////////////////////////////////////////////////////
- int RemapCompareLesss(FunctionRemap * in1 , FunctionRemap * in2)
- {
- if(in1->_parameters.size() != in2->_parameters.size())
- return (in1->_parameters.size() > in2->_parameters.size());
-
- int pcount = in1->_parameters.size();
- for(int x = 0; x< pcount; x++)
- {
- CPPType *orig_type1 = in1->_parameters[x]._remap->get_orig_type();
- CPPType *orig_type2 = in2->_parameters[x]._remap->get_orig_type();
- int pd1 = GetParnetDepth(orig_type1);
- int pd2 = GetParnetDepth(orig_type2);
- if(pd1 != pd2)
- return pd1> pd2;
- }
- // ok maybe something to do with return strength..
- return false;
- }
- //////////////////////////////////////////////////////////
- // Convience for the sort behavior..
- ///////////////////////////////////////////////////////////
- std::vector< FunctionRemap * > SortFunctionSet(std::set< FunctionRemap *> &remaps)
- {
- std::vector< FunctionRemap * > out;
- for(std::set< FunctionRemap *>::iterator ii = remaps.begin(); ii!= remaps.end(); ii++)
- out.push_back(*ii);
- std::sort(out.begin(), out.end(), RemapCompareLesss);
- return out;
- }
- ///////////////////////////////////////////////////////////
- // Function : write_function_forset
- //
- // A set is defined as all rempas that have the same number of paramaters..
- ///////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_function_forset(ostream &out, InterfaceMaker::Function *func,
- std::set< FunctionRemap *> &remapsin, string &expected_params, int indent_level,ostream &forward_decl, const std::string &functionname, bool is_inplace)
- {
- if(remapsin.size() > 1)
- {
- // printf("---------------------------- Start Sort ----- %s , %s\n",func->_name.c_str(),functionname.c_str());
- std::vector<FunctionRemap *> remaps = SortFunctionSet(remapsin);
- std::vector<FunctionRemap *>::iterator sii;
- for(sii = remaps.begin(); sii != remaps.end(); sii ++)
- {
- FunctionRemap *remap = (*sii);
- if(isRemapLegal(*remap))
- {
- indent(out,indent_level)<< "{ // -2 " ;
- remap->write_orig_prototype(out, 0); out << "\n" ;
- write_function_instance(out, func, remap,expected_params,indent_level+4,false,forward_decl, func->_name, is_inplace);
- indent(out,indent_level+4)<< "PyErr_Clear(); \n";
- indent(out,indent_level)<< "}\n\n";
- }
- }
- }
- else
- {
- std::set<FunctionRemap *>::iterator sii;
- for(sii = remapsin.begin(); sii != remapsin.end(); sii ++)
- {
- FunctionRemap *remap = (*sii);
- if(isRemapLegal(*remap))
- {
- indent(out,indent_level)<< "// 1-" ;remap->write_orig_prototype(out, 0); out << "\n" ;
- // indent(out,indent_level)<< "do{\n";
- write_function_instance(out, func, remap,expected_params,indent_level+4,true,forward_decl, func->_name, is_inplace);
- // indent(out,indent_level)<< "}while(false);\n";
- }
- }
- }
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::write_function_instance
- // Access: Private
- // Description: Writes out the particular function that handles a
- // single instance of an overloaded function.
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::write_function_instance(ostream &out, InterfaceMaker::Function *func1,
- FunctionRemap *remap, string &expected_params, int indent_level, bool errors_fatal, ostream &ForwardDeclrs, const std::string &functionnamestr, bool is_inplace)
- {
- string format_specifiers;
- std::string keyword_list;
- string parameter_list;
- string container;
- vector_string pexprs;
- string extra_convert;
- string extra_param_check;
- string extra_cleanup;
- string pname_for_pyobject;
- bool is_constructor = false;
- if (remap->_type == FunctionRemap::T_constructor) {
- is_constructor = true;
- }
- // Make one pass through the parameter list. We will output a
- // one-line temporary variable definition for each parameter, while
- // simultaneously building the ParseTuple() function call and also
- // the parameter expression list for call_function().
- expected_params += remap->_cppfunc->get_simple_name();
- expected_params += "(";
- int pn;
- for (pn = 0; pn < (int)remap->_parameters.size(); pn++) {
- if(pn > 0) {
- expected_params += ", ";
- }
- CPPType *orig_type = remap->_parameters[pn]._remap->get_orig_type();
- CPPType *type = remap->_parameters[pn]._remap->get_new_type();
- string param_name = remap->get_parameter_name(pn);
-
- // This is the string to convert our local variable to the
- // appropriate C++ type. Normally this is just a cast.
- string pexpr_string =
- "(" + type->get_local_name(&parser) + ")" + param_name;
- if (!remap->_has_this || pn != 0) {
- keyword_list += "\""+remap->_parameters[pn]._name + "\", ";
- }
- if (remap->_parameters[pn]._remap->new_type_is_atomic_string()) {
- if (TypeManager::is_char_pointer(orig_type)) {
- indent(out,indent_level)<< "char *" << param_name;
- format_specifiers += "s";
- parameter_list += ", &" + param_name;
-
- } else if (TypeManager::is_wstring(orig_type)) {
- indent(out,indent_level) << "Py_UNICODE *" << param_name
- << "_str; int " << param_name << "_len";
- format_specifiers += "u#";
- parameter_list += ", &" + param_name
- + "_str, &" + param_name + "_len";
- pexpr_string = "basic_string<wchar_t>((wchar_t *)" +
- param_name + "_str, " +
- param_name + "_len)";
- } else {
- indent(out,indent_level) << "char *" << param_name
- << "_str; int " << param_name << "_len";
- format_specifiers += "s#";
- parameter_list += ", &" + param_name
- + "_str, &" + param_name + "_len";
- pexpr_string = "basic_string<char>(" +
- param_name + "_str, " +
- param_name + "_len)";
- }
- expected_params += "string";
-
- } else if (TypeManager::is_bool(type)) {
- indent(out,indent_level) << "PyObject *" << param_name;
- format_specifiers += "O";
- parameter_list += ", &" + param_name;
- pexpr_string = "(PyObject_IsTrue(" + param_name + ")!=0)";
- expected_params += "bool";
- pname_for_pyobject += param_name;
- } else if (TypeManager::is_unsigned_longlong(type)) {
- indent(out,indent_level) << "PyObject *" << param_name;
- format_specifiers += "O";
- parameter_list += ", &" + param_name;
- extra_convert += " PyObject *" + param_name + "_long = PyNumber_Long(" + param_name + ");";
- extra_param_check += "|| (" + param_name + "_long == NULL)";
- pexpr_string = "PyLong_AsUnsignedLongLong(" + param_name + "_long)";
- extra_cleanup += " Py_XDECREF(" + param_name + "_long);";
- expected_params += "unsigned long long";
- pname_for_pyobject += param_name;
- } else if (TypeManager::is_longlong(type)) {
- indent(out,indent_level) << "PyObject *" << param_name;
- format_specifiers += "O";
- parameter_list += ", &" + param_name;
- extra_convert += " PyObject *" + param_name + "_long = PyNumber_Long(" + param_name + ");";
- extra_param_check += "|| (" + param_name + "_long == NULL)";
- pexpr_string = "PyLong_AsLongLong(" + param_name + "_long)";
- extra_cleanup += " Py_XDECREF(" + param_name + "_long);";
- expected_params += "long long";
- pname_for_pyobject += param_name;
- } else if (TypeManager::is_unsigned_integer(type)) {
- indent(out,indent_level) << "PyObject *" << param_name;
- format_specifiers += "O";
- parameter_list += ", &" + param_name;
- extra_convert += " PyObject *" + param_name + "_uint = PyNumber_Long(" + param_name + ");";
- extra_param_check += "|| (" + param_name + "_uint == NULL)";
- pexpr_string = "PyLong_AsUnsignedLong(" + param_name + "_uint)";
- extra_cleanup += " Py_XDECREF(" + param_name + "_uint);";
- expected_params += "unsigned int";
- pname_for_pyobject += param_name;
- } else if (TypeManager::is_integer(type)) {
- indent(out,indent_level) << "int " << param_name;
- format_specifiers += "i";
- parameter_list += ", &" + param_name;
- expected_params += "int";
- } else if (TypeManager::is_float(type)) {
- indent(out,indent_level) << "double " << param_name;
- format_specifiers += "d";
- parameter_list += ", &" + param_name;
- expected_params += "float ";
- } else if (TypeManager::is_char_pointer(type)) {
- indent(out,indent_level) << "char *" << param_name;
- format_specifiers += "s";
- parameter_list += ", &" + param_name;
- expected_params += "string";
- } else if (TypeManager::is_pointer_to_PyObject(type)) {
- indent(out,indent_level) << "PyObject *" << param_name;
- format_specifiers += "O";
- parameter_list += ", &" + param_name;
- pexpr_string = param_name;
- pname_for_pyobject += param_name;
- expected_params += "PyObject";
- } else if (TypeManager::is_pointer(type)) {
- expected_params += type->get_preferred_name();
- if (!remap->_has_this || pn != 0) {
- indent(out, indent_level)
- << "PyObject *" << param_name;
- format_specifiers += "O";
- parameter_list += ", &" + param_name;
- pname_for_pyobject += param_name;
- TypeIndex p_type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(type)),false);
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- const InterrogateType &p_itype = idb->get_type(p_type_index);
- //make_safe_name(itype.get_scoped_name())
- extra_convert += p_itype.get_scoped_name()+" *" + param_name + "_this = ("+p_itype.get_scoped_name()+" *)";
- // need to a forward scope for this class..
- if(!isExportThisRun(p_itype._cpptype)) {
- _external_imports.insert(make_safe_name(p_itype.get_scoped_name()));
- //ForwardDeclrs << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(p_itype.get_scoped_name()) << ";\n";
- }
- ostringstream str;
- str << "DTOOL_Call_GetPointerThisClass(" << param_name
- << ", &Dtool_" << make_safe_name(p_itype.get_scoped_name())
- << ", " << pn << ", \""
- << remap->_cppfunc->get_local_name(&parser)
- << "\");\n";
- extra_convert += str.str();
- extra_param_check += "|| (" + param_name + "_this == NULL)";
- pexpr_string = param_name + "_this";
- }
- } else {
- // Ignore a parameter.
- indent(out,indent_level) << "PyObject *" << param_name;
- format_specifiers += "O";
- parameter_list += ", &" + param_name;
- expected_params += "any";
- pname_for_pyobject += param_name;
- }
- if (remap->_parameters[pn]._has_name) {
- expected_params += " " + remap->_parameters[pn]._name;
- }
- if (remap->_has_this && pn == 0) {
- container = "local_this";
- } else {
- out << ";\n";
- }
- pexprs.push_back(pexpr_string);
- }
- expected_params += ")\n";
-
- /*if (!format_specifiers.empty()) */ {
- std::string format_specifiers1 = format_specifiers + ":" + remap->_cppfunc->get_local_name(&parser);
- indent(out,indent_level)
- << "static char * key_word_list[] = {" << keyword_list << "NULL};\n";
- if (remap->_parameters.size() == 1 ||
- (remap->_has_this && remap->_parameters.size() == 2)) {
- indent(out,indent_level)
- << "// Special Case to Make operator work \n";
- indent(out,indent_level)
- << "if(PyTuple_Check(args) || (kwds != NULL && PyDict_Check(kwds)))\n";
- indent(out,indent_level)
- << " (PyArg_ParseTupleAndKeywords(args,kwds, \""
- << format_specifiers1 << "\", key_word_list" << parameter_list
- << "));\n";
- indent(out,indent_level)
- << "else\n";
- indent(out,indent_level)
- << " (PyArg_Parse(args, \"" << format_specifiers1 << "\""
- << parameter_list << "));\n";
- indent(out,indent_level)
- << "if(!PyErr_Occurred())\n";
- } else {
- indent(out,indent_level)
- << "if (PyArg_ParseTupleAndKeywords(args,kwds, \""
- << format_specifiers1 << "\", key_word_list"
- << parameter_list << "))\n";
- }
- }
- indent(out,indent_level) << "{\n";
- if (!extra_convert.empty()) {
- indent(out,indent_level+4)
- << extra_convert << "\n";
- }
- int extra_indent_level = indent_level+4;
- if (!extra_param_check.empty()) {
- indent(out,extra_indent_level)
- << "if (!(" << extra_param_check.substr(3) << "))\n";
- indent(out,extra_indent_level)
- <<"{\n";
- extra_indent_level+=4;
- }
-
- if (!remap->_void_return &&
- remap->_return_type->new_type_is_atomic_string()) {
- // Treat strings as a special case. We don't want to format the
- // return expression.
- string tt;
- string return_expr = remap->call_function(out, extra_indent_level, false, container, pexprs);
- CPPType *type = remap->_return_type->get_orig_type();
- indent(out,extra_indent_level);
- type->output_instance(out, "return_value", &parser);
- // type->output_instance(tt, "return_value", &parser);
- out << " = " << return_expr << ";\n";
-
- if (track_interpreter) {
- indent(out,extra_indent_level) << "in_interpreter = 1;\n";
- }
- if (!extra_cleanup.empty()) {
- indent(out,extra_indent_level) << extra_cleanup << "\n";
- }
- return_expr = manage_return_value(out, 4, remap, "return_value");
- do_assert_init(out, extra_indent_level,is_constructor);
- pack_return_value(out, extra_indent_level, remap, return_expr,ForwardDeclrs,is_inplace);
- } else {
- string return_expr = remap->call_function(out, extra_indent_level, true, container, pexprs);
- if (return_expr.empty()) {
- if (track_interpreter) {
- indent(out,extra_indent_level) << "in_interpreter = 1;\n";
- }
- if (!extra_cleanup.empty()) {
- indent(out,extra_indent_level) << extra_cleanup << "\n";
- }
- do_assert_init(out, extra_indent_level,is_constructor);
- indent(out,extra_indent_level) << "return Py_BuildValue(\"\");\n";
- } else {
- CPPType *type = remap->_return_type->get_temporary_type();
- if(!is_inplace) {
- indent(out,extra_indent_level);
- type->output_instance(out, "return_value", &parser);
- out << " = " << return_expr << ";\n";
- }
- if (track_interpreter) {
- indent(out,extra_indent_level) << "in_interpreter = 1;\n";
- }
- if (!extra_cleanup.empty()) {
- indent(out,extra_indent_level) << extra_cleanup << "\n";
- }
-
- return_expr = manage_return_value(out, extra_indent_level, remap, "return_value");
- do_assert_init(out, extra_indent_level,is_constructor);
- pack_return_value(out, extra_indent_level, remap, remap->_return_type->temporary_to_return(return_expr),ForwardDeclrs,is_inplace);
- }
- }
- if (!extra_param_check.empty()) {
- extra_indent_level-=4;
- indent(out,extra_indent_level)<< "}\n";
- }
-
- indent(out,indent_level) << "}\n";
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::pack_return_value
- // Access: Private
- // Description: Outputs a command to pack the indicated expression,
- // of the return_type type, as a Python return value.
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::pack_return_value(ostream &out, int indent_level,
- FunctionRemap *remap, string return_expr, ostream &ForwardDeclrs, bool is_inplace)
- {
- CPPType *orig_type = remap->_return_type->get_orig_type();
- CPPType *type = remap->_return_type->get_new_type();
- if (remap->_return_type->new_type_is_atomic_string()) {
- if (TypeManager::is_char_pointer(orig_type)) {
- indent(out, indent_level)
- << "return PyString_FromString(" << return_expr << ");\n";
- } else if (TypeManager::is_wstring(orig_type)) {
- indent(out, indent_level)
- << "return PyUnicode_FromWideChar("
- << return_expr << ".data(), (int)" << return_expr << ".length());\n";
- } else {
- indent(out, indent_level)
- << "return PyString_FromStringAndSize("
- << return_expr << ".data(), (int)" << return_expr << ".length());\n";
- }
- } else if (TypeManager::is_unsigned_longlong(type)) {
- indent(out, indent_level)
- << "return PyLong_FromUnsignedLongLong(" << return_expr << ");\n";
- } else if (TypeManager::is_longlong(type)) {
- indent(out, indent_level)
- << "return PyLong_FromLongLong(" << return_expr << ");\n";
- } else if(TypeManager::is_unsigned_integer(type)){
- indent(out, indent_level)
- << "return PyLong_FromUnsignedLong(" << return_expr << ");\n";
- }else if (TypeManager::is_integer(type)) {
- indent(out, indent_level)
- << "return PyInt_FromLong(" << return_expr << ");\n";
- } else if (TypeManager::is_float(type)) {
- indent(out, indent_level)
- << "return PyFloat_FromDouble(" << return_expr << ");\n";
- } else if (TypeManager::is_char_pointer(type)) {
- indent(out, indent_level)
- << "return PyString_FromString(" << return_expr << ");\n";
- }
- else if (TypeManager::is_pointer_to_PyObject(type))
- {
- indent(out, indent_level)
- << "return "<< return_expr << ";\n";
-
- }
- else if (TypeManager::is_pointer(type))
- {
- if (TypeManager::is_struct(orig_type) || TypeManager::is_ref_to_anything(orig_type))
- {
- if( TypeManager::is_ref_to_anything(orig_type))
- {
- TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(type)),false);
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- const InterrogateType &itype = idb->get_type(type_index);
- std::string ows_memory_flag("true");
- if(remap->_return_value_needs_management)
- ows_memory_flag = "true";
- else
- ows_memory_flag = "false";
- if(!isExportThisRun(itype._cpptype))
- {
- _external_imports.insert(make_safe_name(itype.get_scoped_name()));
- //ForwardDeclrs << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
- }
- WriteReturnInstance(out,indent_level,return_expr,ows_memory_flag,itype.get_scoped_name(),itype._cpptype,is_inplace);
- // indent(out, indent_level)
- // <<"return DTool_CreatePyInstance((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(itype.get_scoped_name()) << ","<<ows_memory_flag<<");\n";
- }
- else
- {
- if (remap->_type == FunctionRemap::T_constructor )
- {
- // should only reach this in the INIT function a a Class .. IE the PY exists before the CPP object
- // this is were we type to returned a class/struct.. ie CPP TYpe
- TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(orig_type)),false);
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- const InterrogateType &itype = idb->get_type(type_index);
- indent(out, indent_level)
- <<"return DTool_PyInit_Finalize(self, " << return_expr <<",&"<<CLASS_PREFEX << make_safe_name(itype.get_scoped_name()) << ",true);\n";
- }
- else
- {
- std::string ows_memory_flag("true");
- if(remap->_return_value_needs_management)
- ows_memory_flag = "true";
- else
- ows_memory_flag = "false";
- if(remap->_manage_reference_count)
- {
- TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(type)),false);
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- const InterrogateType &itype = idb->get_type(type_index);
- if(!isExportThisRun(itype._cpptype))
- {
- _external_imports.insert(make_safe_name(itype.get_scoped_name()));
- //ForwardDeclrs << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
- }
- // ForwardDeclrs << "extern \"C\" struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
- WriteReturnInstance(out,indent_level,return_expr,ows_memory_flag,itype.get_scoped_name(),itype._cpptype,is_inplace);
- //indent(out, indent_level)
- // <<"return DTool_CreatePyInstance((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(itype.get_scoped_name()) << ","<<ows_memory_flag<<");\n";
- }
- else
- {
- TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(orig_type)),false);
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- const InterrogateType &itype = idb->get_type(type_index);
- if(!isExportThisRun(itype._cpptype))
- {
- _external_imports.insert(make_safe_name(itype.get_scoped_name()));
- //ForwardDeclrs << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
- }
- // ForwardDeclrs << "extern \"C\" struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
- //indent(out, indent_level)
- // <<"return DTool_CreatePyInstance((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(itype.get_scoped_name()) << ","<<ows_memory_flag<<");\n";
- WriteReturnInstance(out,indent_level,return_expr,ows_memory_flag,itype.get_scoped_name(),itype._cpptype,is_inplace);
- }
- }
- }
- }
- else if( TypeManager::is_struct(orig_type->as_pointer_type()->_pointing_at) )
- {
- TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(orig_type)),false);
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- const InterrogateType &itype = idb->get_type(type_index);
- std::string ows_memory_flag("true");
- if(remap->_return_value_needs_management)
- ows_memory_flag ="true";
- else
- ows_memory_flag = "false";
- if(!isExportThisRun(itype._cpptype))
- {
- _external_imports.insert(make_safe_name(itype.get_scoped_name()));
- //ForwardDeclrs << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
- }
- // ForwardDeclrs << "extern \"C\" struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
- WriteReturnInstance(out,indent_level,return_expr,ows_memory_flag,itype.get_scoped_name(),itype._cpptype,is_inplace);
- //indent(out, indent_level)
- // << "return DTool_CreatePyInstance((void *)" << return_expr <<","<<CLASS_PREFEX << make_safe_name(itype.get_scoped_name()) <<","<< ows_memory_flag<< ");\n";
- }
- else
- {
- indent(out, indent_level)<<" Shouln Never Reach This InterfaceMakerPythonNative::pack_return_value";
- //<< "return PyInt_FromLong((int)" << return_expr << ");\n";
- }
- } else {
- // Return None.
- indent(out, indent_level)
- << "return Py_BuildValue(\"\");\n";
- }
- }
- ////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::record_object
- // Access: Protected
- // Description: Records the indicated type, which may be a struct
- // type, along with all of its associated methods, if
- // any.
- ////////////////////////////////////////////////////////////////////
- InterfaceMaker::Object *InterfaceMakerPythonNative::record_object(TypeIndex type_index)
- {
- if (type_index == 0) {
- return (Object *)NULL;
- }
- Objects::iterator oi = _objects.find(type_index);
- if (oi != _objects.end()) {
- return (*oi).second;
- }
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- const InterrogateType &itype = idb->get_type(type_index);
- if(!isCppTypeLegal(itype._cpptype))
- {
- return (Object *)NULL;
- }
- Object *object = new Object(itype);
- bool inserted = _objects.insert(Objects::value_type(type_index, object)).second;
- assert(inserted);
- Function *function;
- int num_constructors = itype.number_of_constructors();
- for (int ci = 0; ci < num_constructors; ci++)
- {
- function = record_function(itype, itype.get_constructor(ci));
- if(isFunctionLegal(function))
- object->_constructors.push_back(function);
- }
-
- int num_methods = itype.number_of_methods();
- int mi;
- for (mi = 0; mi < num_methods; mi++)
- {
- function = record_function(itype, itype.get_method(mi));
- if(isFunctionLegal(function))
- object->_methods.push_back(function);
- }
-
- int num_casts = itype.number_of_casts();
- for (mi = 0; mi < num_casts; mi++)
- {
- function = record_function(itype, itype.get_cast(mi));
- if(isFunctionLegal(function))
- object->_methods.push_back(function);
- }
-
- int num_derivations = itype.number_of_derivations();
- for (int di = 0; di < num_derivations; di++)
- {
- TypeIndex d_type_Index = itype.get_derivation(di);
- idb->get_type(d_type_Index);
- if(!interrogate_type_is_unpublished(d_type_Index))
- {
- if (itype.derivation_has_upcast(di))
- {
- function = record_function(itype, itype.derivation_get_upcast(di));
- if(isFunctionLegal(function))
- object->_methods.push_back(function);
- }
- if (itype.derivation_has_downcast(di))
- {
- // Downcasts are methods of the base class, not the child class.
- TypeIndex base_type_index = itype.get_derivation(di);
- const InterrogateType &base_type = idb->get_type(base_type_index);
- function = record_function(base_type, itype.derivation_get_downcast(di));
- if(isFunctionLegal(function))
- {
- Object * pobject = record_object(base_type_index);
- if(pobject != NULL)
- pobject->_methods.push_back(function);
- }
- }
- }
- }
- int num_elements = itype.number_of_elements();
- for (int ei = 0; ei < num_elements; ei++)
- {
- ElementIndex element_index = itype.get_element(ei);
- const InterrogateElement &ielement = idb->get_element(element_index);
- if (ielement.has_getter())
- {
- FunctionIndex func_index = ielement.get_getter();
- record_function(itype, func_index);
- }
- if (ielement.has_setter())
- {
- FunctionIndex func_index = ielement.get_setter();
- record_function(itype, func_index);
- }
- }
- int num_nested = itype.number_of_nested_types();
- for (int ni = 0; ni < num_nested; ni++)
- {
- TypeIndex nested_index = itype.get_nested_type(ni);
- record_object(nested_index);
- }
- return object;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMaker::generate_wrappers
- // Access: Public, Virtual
- // Description: Walks through the set of functions in the database
- // and generates wrappers for each function, storing
- // these in the database. No actual code should be
- // output yet; this just updates the database with the
- // wrapper information.
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::generate_wrappers()
- {
- inside_python_native = true;
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- // We use a while loop rather than a simple for loop, because we
- // might increase the number of types recursively during the
- // traversal.
- int ti = 0;
- while (ti < idb->get_num_all_types())
- {
- TypeIndex type_index = idb->get_all_type(ti);
- record_object(type_index);
- ++ti;
- }
- int num_global_elements = idb->get_num_global_elements();
- for (int gi = 0; gi < num_global_elements; ++gi) {
- printf(" Global Type = %d",gi);
- TypeIndex type_index = idb->get_global_element(gi);
- record_object(type_index);
- }
- int num_functions = idb->get_num_global_functions();
- for (int fi = 0; fi < num_functions; fi++)
- {
- FunctionIndex func_index = idb->get_global_function(fi);
- record_function(dummy_type, func_index);
- }
- int num_manifests = idb->get_num_global_manifests();
- for (int mi = 0; mi < num_manifests; mi++)
- {
- ManifestIndex manifest_index = idb->get_global_manifest(mi);
- const InterrogateManifest &iman = idb->get_manifest(manifest_index);
- if (iman.has_getter())
- {
- FunctionIndex func_index = iman.get_getter();
- record_function(dummy_type, func_index);
- }
- }
- int num_elements = idb->get_num_global_elements();
- for (int ei = 0; ei < num_elements; ei++)
- {
- printf(" Element %d\n",ei);
- ElementIndex element_index = idb->get_global_element(ei);
- const InterrogateElement &ielement = idb->get_element(element_index);
- if (ielement.has_getter())
- {
- FunctionIndex func_index = ielement.get_getter();
- record_function(dummy_type, func_index);
- }
- if (ielement.has_setter())
- {
- FunctionIndex func_index = ielement.get_setter();
- record_function(dummy_type, func_index);
- }
- }
- inside_python_native = false;
- }
- //////////////////////////////////////////////
- // Function :isCppTypeLegal
- //
- // is the cpp object supported by by the dtool_py interface..
- //////////////////////////////////////////////
- bool InterfaceMakerPythonNative::isCppTypeLegal(CPPType *in_ctype)
- {
- if(in_ctype == NULL)
- return false;
- if(builder.in_ignoretype(in_ctype->get_local_name(&parser)))
- {
- return false;
- }
- //bool answer = false;
- CPPType *type = TypeManager::unwrap(TypeManager::resolve_type(in_ctype));
- type = TypeManager::unwrap(type);
- //CPPType *type = ctype;
- if(TypeManager::is_basic_string_char(type))
- {
- return true;
- }
- else if(TypeManager::is_basic_string_wchar(type))
- {
- return true;
- }
- else if(TypeManager::is_simple(type))
- {
- return true;
- }
- else if(builder.in_forcetype(in_ctype->get_local_name(&parser)))
- {
- return true;
- }
- else if(TypeManager::IsExported(type) == true)
- {
- return true;
- }
- else if(TypeManager::is_pointer_to_PyObject(in_ctype) == true)
- {
- return true;
- }
- //if(answer == false)
- // printf(" -------------------- Bad Type ?? %s \n",type->get_local_name().c_str());
- return false;
- }
- //////////////////////////////////////////////
- // Function :isExportThisRun
- //
- //////////////////////////////////////////////
- bool InterfaceMakerPythonNative::isExportThisRun(CPPType *ctype)
- {
- CPPType *type = TypeManager::unwrap(ctype);
- if(TypeManager::IsLocal(type))
- return true;
- if(builder.in_forcetype(type->get_local_name(&parser)))
- return true;
- return false;
- }
- //////////////////////////////////////////////
- // Function : isExportThisRun
- /////////////////////////////////////////////
- bool InterfaceMakerPythonNative::isExportThisRun(Function *func)
- {
- if(func == NULL || !isFunctionLegal(func))
- return false;
- Function::Remaps::const_iterator ri;
- for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri)
- {
- FunctionRemap *remap = (*ri);
- return isExportThisRun(remap->_cpptype);
- }
- return false;
- }
- //////////////////////////////////////////////
- // Function : isRemapLegal
- //////////////////////////////////////////////
- bool InterfaceMakerPythonNative::isRemapLegal( FunctionRemap &remap)
- {
- // return must be legal and managable..
- if(!isCppTypeLegal(remap._return_type->get_orig_type()))
- {
- // printf(" isRemapLegal Return Is Bad %s\n",remap._return_type->get_orig_type()->get_fully_scoped_name().c_str());
- return false;
- }
- // ouch .. bad things will happen here .. do not even try..
- if(remap._ForcedVoidReturn)
- return false;
- // all params must be legal
- for (int pn = 0; pn < (int)remap._parameters.size(); pn++)
- {
- CPPType *orig_type = remap._parameters[pn]._remap->get_orig_type();
- if(!isCppTypeLegal(orig_type))
- return false;
- }
- // ok all looks ok.
- return true;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function : isFunctionLegal
- ////////////////////////////////////////////////////////////////////////
- bool InterfaceMakerPythonNative::isFunctionLegal( Function *func)
- {
- Function::Remaps::const_iterator ri;
- for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri)
- {
- FunctionRemap *remap = (*ri);
- if(isRemapLegal(*remap))
- {
- // printf(" Function Is Marked Legal %s\n",func->_name.c_str());
- return true;
- }
- }
- // printf(" Function Is Marked Illegal %s\n",func->_name.c_str());
- return false;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function : isFunctionWithThis
- //
- // If any rempas have a this .. this function has a this..( of self) to python..
- ////////////////////////////////////////////////////////////////////////
- bool InterfaceMakerPythonNative::isFunctionWithThis( Function *func)
- {
- Function::Remaps::const_iterator ri;
- for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri)
- {
- FunctionRemap *remap = (*ri);
- if(remap->_has_this)
- return true;
- }
- return false;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPython::test_assert
- // Access: Protected
- // Description: Outputs code to check to see if an assertion has
- // failed while the C++ code was executing, and report
- // this failure back to Python.
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::do_assert_init(ostream &out, int indent_level, bool constructor) const {
- if (watch_asserts) {
- out << "#ifndef NDEBUG\n";
- indent(out, indent_level)
- << "Notify *notify = Notify::ptr();\n";
- indent(out, indent_level)
- << "if (notify->has_assert_failed()) {\n";
- indent(out, indent_level + 2)
- << "PyErr_SetString(PyExc_AssertionError, notify->get_assert_error_message().c_str());\n";
- indent(out, indent_level + 2)
- << "notify->clear_assert_failed();\n";
- if(constructor)
- indent(out, indent_level + 2) << "return -1;\n";
- else
- indent(out, indent_level + 2) << "return (PyObject *)NULL;\n";
- indent(out, indent_level)
- << "}\n";
- indent(out, indent_level)
- << "if (PyErr_Occurred()) {\n";
- if(constructor)
- indent(out, indent_level + 2) << "return -1;\n";
- else
- indent(out, indent_level + 2) << "return (PyObject *)NULL;\n";
- indent(out, indent_level)
- << "}\n";
- out << "#endif\n";
- }
- }
- ////////////////////////////////////////////////////////
- // Function : IsRunTimeTyped
- ///////////////////////////////////////////////////////
- bool InterfaceMakerPythonNative::IsRunTimeTyped(const InterrogateType &itype)
- {
- TypeIndex ptype_id = itype.get_outer_class();
- if(ptype_id > 0)
- {
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- InterrogateType ptype = idb->get_type(ptype_id);
- return IsRunTimeTyped(ptype);
- }
- if(itype.get_name() == "TypedObject")
- return true;
- return false;
- };
- //////////////////////////////////////////////////////////
- // Function : DoesInheritFromIsClass
- //
- // Helper function to check cpp class inharatience..
- ///////////////////////////////////////////////////////////
- bool InterfaceMakerPythonNative::DoesInheritFromIsClass( const CPPStructType * inclass, const std::string &name)
- {
- if(inclass == NULL)
- return false;
- CPPStructType::Derivation::const_iterator bi;
- for (bi = inclass->_derivation.begin();
- bi != inclass->_derivation.end();
- ++bi)
- {
- const CPPStructType::Base &base = (*bi);
- CPPStructType *base_type = TypeManager::resolve_type(base._base)->as_struct_type();
- if(base_type != NULL)
- {
- std::string scoped_name = base_type->get_fully_scoped_name();
- if(scoped_name == name)
- return true;
- if(DoesInheritFromIsClass(base_type,name) == true)
- return true;
- }
- }
- return false;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- // Function : HasAGetKeyFunction
- //
- // does the class have a supportable GetKey for hash usage..
- //////////////////////////////////////////////////////////////////////////////////////////
- bool InterfaceMakerPythonNative::HasAGetKeyFunction(const InterrogateType &itype_class)
- {
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- int num_methods = itype_class.number_of_methods();
- int mi;
- for (mi = 0; mi < num_methods; mi++)
- {
- FunctionIndex func_index = itype_class.get_method(mi);
- const InterrogateFunction &ifunc = idb->get_function(func_index);
- if(ifunc.get_name() == "get_key")
- {
- if (ifunc._instances != (InterrogateFunction::Instances *)NULL)
- {
- InterrogateFunction::Instances::const_iterator ii;
- for (ii = ifunc._instances->begin();ii != ifunc._instances->end();++ii)
- {
- CPPInstance *cppinst = (*ii).second;
- CPPFunctionType *cppfunc = cppinst->_type->as_function_type();
- if(cppfunc != NULL)
- {
- if(cppfunc->_parameters != NULL && cppfunc->_return_type != NULL && TypeManager::is_integer(cppfunc->_return_type))
- {
- if(cppfunc->_parameters->_parameters.size() == 0)
- {
- return true;
- }
- }
- }
- }
- }
- }
- }
- return false;
- };
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::NeedsAStrFunction
- // Access: Private
- // Description: Returns -1 if the class does not define write() (and
- // therefore cannot support a __str__ function).
- //
- // Returns 1 if the class defines write(ostream).
- //
- // Returns 2 if the class defines write(ostream, int).
- ////////////////////////////////////////////////////////////////////
- int InterfaceMakerPythonNative::
- NeedsAStrFunction(const InterrogateType &itype_class) {
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- int num_methods = itype_class.number_of_methods();
- int mi;
- for (mi = 0; mi < num_methods; ++mi) {
- FunctionIndex func_index = itype_class.get_method(mi);
- const InterrogateFunction &ifunc = idb->get_function(func_index);
- if (ifunc.get_name() == "write") {
- if (ifunc._instances != (InterrogateFunction::Instances *)NULL) {
- InterrogateFunction::Instances::const_iterator ii;
- for (ii = ifunc._instances->begin();
- ii != ifunc._instances->end();
- ++ii) {
- CPPInstance *cppinst = (*ii).second;
- CPPFunctionType *cppfunc = cppinst->_type->as_function_type();
-
- if (cppfunc != NULL) {
- if (cppfunc->_parameters != NULL &&
- cppfunc->_return_type != NULL &&
- TypeManager::is_void(cppfunc->_return_type)) {
- if (cppfunc->_parameters->_parameters.size() == 1) {
- CPPInstance *inst1 = cppfunc->_parameters->_parameters[0];
- if (TypeManager::is_pointer_to_ostream(inst1->_type)) {
- // write(ostream)
- return 1;
- }
- }
- if (cppfunc->_parameters->_parameters.size() == 2) {
- CPPInstance *inst1 = cppfunc->_parameters->_parameters[0];
- if (TypeManager::is_pointer_to_ostream(inst1->_type)) {
- inst1 = cppfunc->_parameters->_parameters[1];
- if (inst1->_initializer != NULL) {
- // write(ostream, int = 0)
- return 1;
- }
-
- if (TypeManager::is_integer(inst1->_type)) {
- // write(ostream, int)
- return 2;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- return -1;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::NeedsAReprFunction
- // Access: Private
- // Description: Returns -1 if the class does not define output() or
- // python_repr() (and therefore cannot support a
- // __repr__ function).
- //
- // Returns 1 if the class defines python_repr(ostream, string).
- //
- // Returns 2 if the class defines output(ostream).
- ////////////////////////////////////////////////////////////////////
- int InterfaceMakerPythonNative::
- NeedsAReprFunction(const InterrogateType &itype_class) {
- InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
- int num_methods = itype_class.number_of_methods();
- int mi;
- for (mi = 0; mi < num_methods; ++mi) {
- FunctionIndex func_index = itype_class.get_method(mi);
- const InterrogateFunction &ifunc = idb->get_function(func_index);
- if (ifunc.get_name() == "python_repr") {
- if (ifunc._instances != (InterrogateFunction::Instances *)NULL) {
- InterrogateFunction::Instances::const_iterator ii;
- for (ii = ifunc._instances->begin();
- ii != ifunc._instances->end();
- ++ii) {
- CPPInstance *cppinst = (*ii).second;
- CPPFunctionType *cppfunc = cppinst->_type->as_function_type();
-
- if (cppfunc != NULL) {
- if (cppfunc->_parameters != NULL &&
- cppfunc->_return_type != NULL &&
- TypeManager::is_void(cppfunc->_return_type)) {
- if (cppfunc->_parameters->_parameters.size() == 2) {
- CPPInstance *inst1 = cppfunc->_parameters->_parameters[0];
- if (TypeManager::is_pointer_to_ostream(inst1->_type)) {
- inst1 = cppfunc->_parameters->_parameters[1];
- if (TypeManager::is_string(inst1->_type) ||
- TypeManager::is_char_pointer(inst1->_type)) {
- // python_repr(ostream, string)
- return 1;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- for (mi = 0; mi < num_methods; ++mi) {
- FunctionIndex func_index = itype_class.get_method(mi);
- const InterrogateFunction &ifunc = idb->get_function(func_index);
- if (ifunc.get_name() == "output") {
- if (ifunc._instances != (InterrogateFunction::Instances *)NULL) {
- InterrogateFunction::Instances::const_iterator ii;
- for (ii = ifunc._instances->begin();
- ii != ifunc._instances->end();
- ++ii) {
- CPPInstance *cppinst = (*ii).second;
- CPPFunctionType *cppfunc = cppinst->_type->as_function_type();
-
- if (cppfunc != NULL) {
- if (cppfunc->_parameters != NULL &&
- cppfunc->_return_type != NULL &&
- TypeManager::is_void(cppfunc->_return_type)) {
- if (cppfunc->_parameters->_parameters.size() == 1) {
- CPPInstance *inst1 = cppfunc->_parameters->_parameters[0];
- if (TypeManager::is_pointer_to_ostream(inst1->_type)) {
- // output(ostream)
- return 2;
- }
- }
- if (cppfunc->_parameters->_parameters.size() >= 2) {
- CPPInstance *inst1 = cppfunc->_parameters->_parameters[0];
- if (TypeManager::is_pointer_to_ostream(inst1->_type)) {
- inst1 = cppfunc->_parameters->_parameters[1];
- if (inst1->_initializer != NULL) {
- // output(ostream, foo = bar, ...)
- return 2;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- return -1;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: InterfaceMakerPythonNative::output_quoted
- // Access: Private
- // Description: Outputs the indicated string as a single quoted,
- // multi-line string to the generated C++ source code.
- // The output point is left on the last line of the
- // string, following the trailing quotation mark.
- ////////////////////////////////////////////////////////////////////
- void InterfaceMakerPythonNative::
- output_quoted(ostream &out, int indent_level, const std::string &str) {
- indent(out, indent_level)
- << '"';
- std::string::const_iterator si;
- for (si = str.begin(); si != str.end(); ++si) {
- switch (*si) {
- case '"':
- case '\\':
- out << '\\' << *si;
- break;
- case '\n':
- out << "\\n\"\n";
- indent(out, indent_level)
- << '"';
- break;
- default:
- if (!isprint(*si)) {
- out << "\\" << oct << setw(3) << setfill('0') << (unsigned int)(*si)
- << dec;
- } else {
- out << *si;
- }
- }
- }
- out << '"';
- }
|