Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

swift functions from cpp

// main.swift

private func printGreeting(modifier: UnsafePointer<CChar>) {
    print("Hello (String(cString: modifier))World!")
}

var callbacks = SomeCLibCallbacks(
    printGreeting: { (modifier) in
        printGreeting(modifier: modifier)
    }
)
SomeCLibSetup(&callbacks)

SomeCLibTest()

// SomeCLib.h

#ifndef SomeCLib_h
#define SomeCLib_h

struct SomeCLibCallbacks {
    void (* _Nonnull printGreeting)(const char * _Nonnull modifier);
};
typedef struct SomeCLibCallbacks SomeCLibCallbacks;

extern void SomeCLibSetup(const SomeCLibCallbacks * _Nonnull callbacks);

extern void SomeCLibTest(void);

#endif

// SomeCLib.c

#include "SomeCLib.h"

static SomeCLibCallbacks sCallbacks;

extern void SomeCLibSetup(const SomeCLibCallbacks * callbacks) {
    sCallbacks = *callbacks;
}

extern void SomeCLibTest(void) {
    sCallbacks.printGreeting("Cruel ");
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #swift #functions #cpp
ADD COMMENT
Topic
Name
6+2 =