Skip to content

uliwitness/cpptoswift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Hack for generating a Swift class around a C++ class. It does this by wrapping the C++ class in C and then generating a Swift class that calls this

Usage

Put the following in your prefix header or at the start of every exported C++ header:

#ifndef CP2SINIT
#define CP2SINIT
#define CP2SMETHOD
#define CP2SCLASS
#endif

(This will hide cpp2swift's markup from the C++ compiler)

Then mark up every class, constructor and method that you want to export from your class to Swift with CP2SCLASS, CP2SINIT resp. CP2SMETHOD. E.g. a simple example:

#include <stdio.h>

#ifndef CP2SINIT
#define CP2SINIT
#define CP2SMETHOD
#define CP2SCLASS
#endif


class cppclass CP2SCLASS
{
public:
	cppclass() CP2SINIT : ivar(1234)	{}
	
	void	method( int theNum ) CP2SMETHOD;
	void	method2() CP2SMETHOD { printf("yay!"); }
	
protected:
	int	ivar;
};

Then run the cpptoswift tool with your C++ header as the first argument and the path and base name (without filename suffix) you want for the output files as the second argument, E.g.:

cpptoswift mysource.hpp generated/mysource

It will generate 3 files:

generated/mysource.cpp	-	The glue code that bridges your C++ class to C.
generated/mysource.h	-	A header that you should include from your
							Swift app's bridging header.
generated/mysource.swift-	A Swift class that calls the functions declared
							and defined by the other two files to create and
							destruct the C++ object.

Note that this software is a work in progress and might not fully work, especially as C++ code becomes more complex.

It is recommended that you put all the generated code in a framework, which can then have its own bridging header, leaving your actual project that uses the Swift wrapper free from C++ and C, and mostly ignorant of the C++ code's internal file structure.

License

Copyright 2015 by Uli Kusterer.

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.

3. This notice may not be removed or altered from any source
distribution.

The source code files generated by this software based on your files are exempt from this license and are subject to whatever original license the input files were under.

About

Hack that'll hopefully one day auto-generate a Swift wrapper around C++ source code.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages