Skip to content

Commit

Permalink
add Objective-C++ documentations for c/cpp section
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 10, 2023
1 parent 86bea84 commit 6a08c64
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export default defineConfig({
],
},
{ text: 'Objective-C', link: '/cpp/objective-c' },
{ text: 'Objective-C++', link: '/cpp/objective-cpp' },
],
},
{
Expand Down
58 changes: 58 additions & 0 deletions src/cpp/objective-cpp.md
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.
:::

0 comments on commit 6a08c64

Please sign in to comment.