Skip to content

Instantly share code, notes, and snippets.

@ultimateprogramer
Forked from gaspard/gist:1087380
Last active October 27, 2022 10:23
Show Gist options
  • Select an option

  • Save ultimateprogramer/83aeca704a904310025bbe45ba0125dd to your computer and use it in GitHub Desktop.

Select an option

Save ultimateprogramer/83aeca704a904310025bbe45ba0125dd to your computer and use it in GitHub Desktop.

Revisions

  1. ultimateprogramer revised this gist Oct 27, 2022. 6 changed files with 91 additions and 38 deletions.
    38 changes: 0 additions & 38 deletions gistfile1.c++
    Original file line number Diff line number Diff line change
    @@ -1,38 +0,0 @@
    // g++ simple.cpp -shared -o libsimple.dylib
    #include <stdio.h>

    class Simple {
    int id_;
    public:
    Simple(int id);

    ~Simple();

    int id();
    };

    Simple::Simple(int id) : id_(id) {
    printf("[%p:%i] Simple()\n", this, id_);
    }

    Simple::~Simple() {
    printf("[%p:%i] ~Simple()\n", this, id_);
    }

    int Simple::id() {
    return id_;
    }

    extern "C" {
    Simple *Simple_Simple(int id) {
    return new Simple(id);
    }

    void Simple__gc(Simple *this_) {
    delete this_;
    }

    int Simple_id(Simple *this_) {
    return this_->id();
    }
    }
    36 changes: 36 additions & 0 deletions gistfile1.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    // g++ simple.cpp -shared -o libsimple.dylib
    #include <stdio.h>

    class Simple {
    int id_;
    public:
    Simple(int id);
    ~Simple();
    int id();
    };

    Simple::Simple(int id) : id_(id) {
    printf("[%p:%i] Simple()\n", this, id_);
    }

    Simple::~Simple() {
    printf("[%p:%i] ~Simple()\n", this, id_);
    }

    int Simple::id() {
    return id_;
    }

    extern "C" {
    Simple *Simple_Simple(int id) {
    return new Simple(id);
    }

    void Simple__gc(Simple *this_) {
    delete this_;
    }

    int Simple_id(Simple *this_) {
    return this_->id();
    }
    }
    7 changes: 7 additions & 0 deletions gistfile2.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    /* This is C code */
    #include "Fred.h"

    void c_function(Fred* fred)
    {
    cplusplus_callback_function(fred);
    }
    11 changes: 11 additions & 0 deletions gistfile2.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    // This is C++ code
    #include "Fred.h"

    Fred::Fred() : a_(0) {}
    void Fred::wilma(int a) {}

    Fred* cplusplus_callback_function(Fred* fred)
    {
    fred->wilma(123);
    return fred;
    }
    28 changes: 28 additions & 0 deletions gistfile2.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    /* This header can be read by both C and C++ compilers */
    #ifndef FRED_H
    #define FRED_H
    #ifdef __cplusplus
    class Fred {
    public:
    Fred();
    void wilma(int);
    private:
    int a_;
    };
    #else
    typedef struct Fred Fred;
    #endif
    #ifdef __cplusplus
    extern "C" {
    #endif
    #if defined(__STDC__) || defined(__cplusplus)
    extern void c_function(Fred*); /* ANSI C prototypes */
    extern Fred* cplusplus_callback_function(Fred*);
    #else
    extern void c_function(); /* K&R style */
    extern Fred* cplusplus_callback_function();
    #endif
    #ifdef __cplusplus
    }
    #endif
    #endif /*FRED_H*/
    9 changes: 9 additions & 0 deletions gistfile2b.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    // This is C++ code
    #include "Fred.h"

    int main()
    {
    Fred fred;
    c_function(&fred);
    // ...
    }
  2. @gaspard gaspard created this gist Jul 17, 2011.
    38 changes: 38 additions & 0 deletions gistfile1.c++
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    // g++ simple.cpp -shared -o libsimple.dylib
    #include <stdio.h>

    class Simple {
    int id_;
    public:
    Simple(int id);

    ~Simple();

    int id();
    };

    Simple::Simple(int id) : id_(id) {
    printf("[%p:%i] Simple()\n", this, id_);
    }

    Simple::~Simple() {
    printf("[%p:%i] ~Simple()\n", this, id_);
    }

    int Simple::id() {
    return id_;
    }

    extern "C" {
    Simple *Simple_Simple(int id) {
    return new Simple(id);
    }

    void Simple__gc(Simple *this_) {
    delete this_;
    }

    int Simple_id(Simple *this_) {
    return this_->id();
    }
    }