Last active
October 17, 2019 07:39
-
-
Save gcjyzdd/7facddc327baeb3ca89c13bcc0182cda to your computer and use it in GitHub Desktop.
Generate C plugin from C++. Copied from Saman Barghi's [site](http://samanbarghi.com/blog/2016/12/06/generate-c-interface-from-c-source-code-using-clang-libtooling/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Rectangle.h" | |
| #include "cwrapper.h" | |
| #ifdef __cplusplus | |
| extern "C"{ | |
| #endif | |
| WRectangle* Rectangle_create(int l, int w){ | |
| //calling the constructor and casting the pointer to WRectangle | |
| return reinterpret_cast<WRectangle*>( new Rectangle(l,w) ); | |
| } | |
| void Rectangle_destroy(WRectangle* self){ | |
| //calling the destructor | |
| delete reinterpret_cast<Rectangle*>(self); | |
| } | |
| int Rectangle_area(WRectangle* self){ | |
| return reinterpret_cast<Rectangle*>(self)->area(); | |
| } | |
| int Rectangle_perimeter(WRectangle* self){ | |
| return reinterpret_cast<Rectangle*>(self)->perimeter(); | |
| } | |
| #ifdef __cplusplus | |
| } | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifdef __cplusplus | |
| extern "C"{ | |
| #endif | |
| /* Wrapper struct to hold a pointer to | |
| Rectangle object in C */ | |
| struct WRectangle; | |
| typedef struct WRectangle WRectangle; | |
| /* Wrapper for the constructor */ | |
| WRectangle* Rectangle_create(int l, int w); | |
| /* Wrapper for destructor */ | |
| void Rectangle_destroy(WRectangle* self); | |
| /* Wrapper for method area */ | |
| int Rectangle_area(WRectangle* self); | |
| /* Wrapper for method perimeter */ | |
| int Rectangle_perimeter(WRectangle* self); | |
| #ifdef __cplusplus | |
| } | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Rectangle{ | |
| private: | |
| int length; | |
| int width; | |
| public: | |
| Rectangle(int l, int w): length(l), width(w){}; | |
| int area(){ return length*width;}; | |
| perimeter() { return 2*(lendth+width);}; | |
| ~Rectangle(){}; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment