-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Objective-C++ documentations for c/cpp section
- Loading branch information
1 parent
86bea84
commit 6a08c64
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Objective-C++ | ||
|
||
Objective-C++ implements Objective-C's plugins for C++. In this way, it becomes possible to use Objective-C elements with C++ codes. With Jule, you can use Objective-C++ like Objective-C. | ||
|
||
## Using | ||
|
||
You can create your header files with the extension `.h` in the standard way. But we recommended o use `.hpp` extension for C++ headers. These are does not pose any compatibility issues as it is one of the standard extensions supported by Jule. | ||
|
||
For recommended development experience, declarations should be in the header file and implementation in another source code file. In this context, let's say we have Objective-C++ code like this: | ||
|
||
Our `log.hpp`: | ||
```objective-cpp | ||
#ifndef LOG_HPP | ||
#define LOG_HPP | ||
void Log(const char *text); | ||
#endif // LOG_HPP | ||
``` | ||
|
||
Our `log.mm`: | ||
```objective-cpp | ||
#include "log.hpp" | ||
#include <Foundation/Foundation.h> | ||
void Log(const char *text) { | ||
NSString *log = [[NSString alloc] initWithUTF8String:text]; | ||
NSLog(@"%@", log); | ||
} | ||
``` | ||
|
||
We want to use above Objective-C++ code with Jule. The first thing we need to do is pass the `-framework Foundation` argument to the build command using the `jule:pass` top-directive. Otherwise, we will encounter a compilation error. | ||
|
||
Then we link the definitions we want to use by linking the relevant header file and source code. Then we are ready to use it. | ||
|
||
Our `main.jule`: | ||
```jule | ||
//jule:pass -framework Foundation | ||
cpp use "log.hpp" | ||
cpp use "log.mm" | ||
cpp type char: byte | ||
cpp unsafe fn Log(text: *cpp.char) | ||
fn Log(text: str) { | ||
unsafe { cpp.Log((*cpp.char)(&text[0])) } | ||
} | ||
fn main() { | ||
Log("Log from Jule!") | ||
} | ||
``` | ||
|
||
::: tip | ||
The above code also has wrapper in a Jule for linked function. This increases safety and makes it easier to maintain the relevant function. | ||
::: |