objective c - Use of Cocoa in C++ like in Delphi -
there pattern in delphi work cocoa classes. blog pattern here:
using os x apis directly delphi
i wonder, how works , whether can done xcode in c++ too.
as example take function "internalgetmacospath" system.ioutils.pas. put excerpts of involved code @ end of post. line of code interested in is:
nsfile := tnsfilemanager.wrap(tnsfilemanager.occlass.defaultmanager); in essence following equivalent in c++:
nsfilemanagerclass* pfmanagerclass; nsfilemanager* pfmanager; // getocclass void* cls = objc_getclass("nsfilemanager"); pfmanagerclass = import(cls); // wrap pfmanager = import(pfmanagerclass->defaultmanager()); i know whether dummy function "import" can defined use of objective-c runtime functions. guess delphi has use objective-c runtime functions in end too. else have informations vtables?
code snippets:
nsfilemanager = interface(nsobject) ['{9736df94-95e9-4111-8c94-82d5eff52a81}'] function urlfordirectory(directory: nssearchpathdirectory; indomain: nssearchpathdomainmask; appropriateforurl: nsurl; create: boolean; error: ppointer): nsurl; cdecl; ... nsfilemanagerclass = interface(nsobjectclass) ['{e666adeb-9dbe-4bf0-bee0-5912cf9f5ab4}'] {class} function defaultmanager: pointer; cdecl; end; tocimport = class(trawvirtualclass, ilocalobject) tocgenericimport<c: iobjectivecclass; t: iobjectivecinstance> = class(tocimport) tnsfilemanager = class(tocgenericimport<nsfilemanagerclass, nsfilemanager>) end; class function tocgenericimport<c,t>.getocclass: c; var classimport: tocimport; clsid: pointer; itypeinfo: ptypeinfo; ctypeinfo: ptypeinfo; begin itypeinfo := typeinfo(t); ctypeinfo := typeinfo(c); clsid := objc_getclass(marshaledastring(shortstrtoutf8string(@itypeinfo^.name))); fclassvtable := tocvtable.create(ctypeinfo, false); classimport := tocimport.create(clsid, nil, fclassvtable); classimport.queryinterface(gettypedata(ctypeinfo)^.guid, focclass); result := focclass; end; class function tocgenericimport<c,t>.wrap(p: pointer): t; var objid: pointer; obj: tocimport; itypeinfo: ptypeinfo; begin itypeinfo := typeinfo(t); obj := tocimport.create(p, nil, getinstancevtable); obj.queryinterface(gettypedata(itypeinfo)^.guid, result); end; class function tpath.internalgetmacospath(const searchedpath: nssearchpathdirectory; const searchmask: nssearchpathdomainmask): string; var nsfile: nsfilemanager; url: nsurl; begin nsfile := tnsfilemanager.wrap(tnsfilemanager.occlass.defaultmanager); url := nsfile.urlfordirectory(searchedpath, searchmask, nil, true, nil);
Comments
Post a Comment