Overview¶
What is djinni?¶
Djinni is a tool for generating cross-language type declarations and interface bindings to connect C++ with either Java, Objective-C, Python or C#.
Djinni can be used to interface cross-platform C++ library code with platform-specific code on any popular operating system. Djinni was announced by Dropbox at CppCon 2014:
Djinni ❤️ Community!
Dropbox ended maintenance of Djinni in March 2020. Since June 2020 this fork gives Djinni a second life by maintaining it as a community-driven project in the cross-language-cpp organization!
How the magic works¶
To call from one programming language to another, usually a lot of gluecode is required. Djinni automatically generates this gluecode from an interface definition language (IDL).
All you need to do is implement the interfaces generated by djinni in one language and use it from the other language. Djinni takes care of the heavy lifting: Converting data-types, managing pointers and managing memory.
Example: Call C++ Code from Java or Objective-C
This Djinni Interface defines a class that is implemented in C++ and can be called from Java or Objective-C:
HelloWorld = interface +c {
static create(): HelloWorld;
fromCpp(): string
}
#include "djinni/generated/HelloWorld.hpp"
class HelloWorldImpl : public HelloWorld {
public:
static std::shared_ptr<HelloWorld> create() {
return std::make_shared<HelloWorldImpl>();
}
std::string fromCpp() {
return "Hello From C++!";
}
}
HelloWorld helloWorld = HelloWorld.create();
System.out.println(helloWorld.fromCpp());
// Hello from C++!
HelloWorld *helloWorld = [HelloWorld create];
NSLog([helloWorld fromCpp]);
// Hello from C++!
Example: Call Java/Objective-C Code from C++
This Djinni interface can be implemented in either Java or Objective-C, and can be called from C++:
HelloWorld = interface +j +o {
fromLanguage(): string;
}
public class HelloWorldJava extends HelloWorld {
@Override
String fromLanguage() {
return "from Java";
}
}
@interface HelloWorldObjC : NSObject <HelloWorld>
- (NSString)fromLanguage;
@end
@implementation HelloWorldObjC
- (NSString)fromLanguage {
return "from Objective-C";
}
@end
std::cout << helloWorldInstance->fromLanguage() << std::endl;
Main Features¶
- Generates parallel C++, Java, Objective-C, Python and C++/CLI type definitions from a single interface description file.
- Supports the intersection of the languages' primitive types, and user-defined enums, flags, records, and interfaces.
- Generates interface code allowing bidirectional calls between C++ and ...
- ... Java (with JNI)
- ... Objective-C (with Objective-C++)
- ... Python (with CFFI)
- ... C# (with C++/CLI)
- Can autogenerate comparator functions (equality, ordering) on data types.
Components of Djinni¶
Djinni consists of two core components:
- Generator: Command-line tool that takes an IDL file as input and generates the gluecode.
- Support lib: Library that is required for the gluecode to work.