Interacting with C++ classes from Swift -
i have significant library of classes written in c++. i'm trying make use of them through type of bridge within swift rather rewrite them swift code. primary motivation c++ code represents core library used on multiple platforms. effectively, i'm creating swift based ui allow core functionality work under os x.
there other questions asking, "how call c++ function swift." not question. bridge c++ function, following works fine:
define bridging header through "c"
#ifndef imagereader_hpp #define imagereader_hpp #ifdef __cplusplus extern "c" { #endif const char *hexdump(char *filename); const char *imagetype(char *filename); #ifdef __cplusplus } #endif #endif /* imagereader_hpp */
swift code can call functions directly
let type = string.fromcstring(imagetype(filename)) let dump = string.fromcstring(hexdump(filename))
my question more specific. how can instantiate , manipulate c++ class within swift? can't seem find published on this.
i've worked out manageable answer. how clean you'd entirely based upon how work you're willing do.
first, take c++ class , create c "wrapper" functions interface it. example, if have c++ class:
class mbr { std::string filename; public: mbr (std::string filename); const char *hexdump(); const char *imagetype(); const char *bootcode(); const char *partitions(); private: bool readfile(unsigned char *buffer, const unsigned int length); };
we implement these c++ functions:
#include "mbr.hpp" using namespace std; const void * initialize(char *filename) { mbr *mbr = new mbr(filename); return (void *)mbr; } const char *hexdump(const void *object) { mbr *mbr; static char retval[2048]; mbr = (mbr *)object; strcpy(retval, mbr -> hexdump()); return retval; } const char *imagetype(const void *object) { mbr *mbr; static char retval[256]; mbr = (mbr *)object; strcpy(retval, mbr -> imagetype()); return retval; }
the bridge header contains:
#ifndef imagereader_hpp #define imagereader_hpp #ifdef __cplusplus extern "c" { #endif const void *initialize(char *filename); const char *hexdump(const void *object); const char *imagetype(const void *object); #ifdef __cplusplus } #endif #endif /* imagereader_hpp */
from swift, can instantiate object , interact so:
let cppobject = unsafemutablepointer<void>(initialize(filename)) let type = string.fromcstring(imagetype(cppobject)) let dump = string.fromcstring(hexdump(cppobject)) self.imagetypelabel.stringvalue = type! self.dumpdisplay.stringvalue = dump!
so, can see, solution (which rather simple) create wrappers instantiate object , return pointer object. can passed wrapper functions can treat object conforming class , call member functions.
making cleaner
while fantastic start , proves feasible use existing c++ classes trivial bridge, can cleaner.
cleaning mean remove unsafemutablepointer<void>
middle of our swift code , encapsulate swift class. essentially, use same c/c++ wrapper functions interface them swift class. swift class maintains object reference , passes method , attribute reference calls through bridge c++ object!
having done this, of bridging code encapsulated in swift class. though still using c bridge, using c++ objects transparently without having resort recoding them in objective-c or objective-c++.
Comments
Post a Comment