1 | #include "objc_api.h" |
2 | |
3 | #ifdef TI_PLATFORM_OSX |
4 | |
5 | namespace taichi { |
6 | namespace mac { |
7 | |
8 | nsobj_unique_ptr<TI_NSString> wrap_string_as_ns_string(const std::string &str) { |
9 | constexpr int kNSUTF8StringEncoding = 4; |
10 | id ns_string = clscall("NSString" , "alloc" ); |
11 | auto *ptr = cast_call<TI_NSString *>( |
12 | ns_string, |
13 | "initWithBytesNoCopy:length:encoding:freeWhenDone:" , str.data(), |
14 | str.size(), kNSUTF8StringEncoding, false); |
15 | return wrap_as_nsobj_unique_ptr(ptr); |
16 | } |
17 | |
18 | std::string to_string(TI_NSString *ns) { |
19 | return cast_call<const char *>(ns, "UTF8String" ); |
20 | } |
21 | |
22 | int ns_array_count(TI_NSArray *na) { |
23 | return cast_call<int>(na, "count" ); |
24 | } |
25 | |
26 | TI_NSAutoreleasePool *create_autorelease_pool() { |
27 | return cast_call<TI_NSAutoreleasePool *>( |
28 | clscall("NSAutoreleasePool" , "alloc" ), "init" ); |
29 | } |
30 | |
31 | void drain_autorelease_pool(TI_NSAutoreleasePool *pool) { |
32 | // "drain" is same as "release", so we don't need to release |pool| itself. |
33 | // https://developer.apple.com/documentation/foundation/nsautoreleasepool |
34 | call(pool, "drain" ); |
35 | } |
36 | |
37 | ScopedAutoreleasePool::ScopedAutoreleasePool() { |
38 | pool_ = create_autorelease_pool(); |
39 | } |
40 | |
41 | ScopedAutoreleasePool::~ScopedAutoreleasePool() { |
42 | drain_autorelease_pool(pool_); |
43 | } |
44 | |
45 | } // namespace mac |
46 | } // namespace taichi |
47 | |
48 | #endif // TI_PLATFORM_OSX |
49 | |