Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Objective-C++を使ってMRCで快適に開発する
Search
Yuki Yasoshima
September 15, 2017
Technology
0
22k
Objective-C++を使ってMRCで快適に開発する
iOSDC 2017 9/15
サンプルコード
https://github.com/objective-audio/objc_ptr_sample
Yuki Yasoshima
September 15, 2017
Tweet
Share
More Decks by Yuki Yasoshima
See All by Yuki Yasoshima
Swift Concurrencyとレースコンディション
objectiveaudio
1
490
モーダルの遷移を理解する
objectiveaudio
4
1.7k
オーディオ波形を表示するために知っておくべきこと
objectiveaudio
0
850
AVAudioEngineでリアルタイムレンダリング
objectiveaudio
1
690
リファクタリング・チャレンジ リバーシ編
objectiveaudio
0
130
UIKitは2度ベルを鳴らす
objectiveaudio
0
280
iOSDC2018.pdf
objectiveaudio
1
2k
Other Decks in Technology
See All in Technology
IBC 2024 動画技術関連レポート / IBC 2024 Report
cyberagentdevelopers
PRO
1
110
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
13k
アジャイルチームがらしさを発揮するための目標づくり / Making the goal and enabling the team
kakehashi
3
150
FlutterアプリにおけるSLI/SLOを用いたユーザー体験の可視化と計測基盤構築
ostk0069
0
110
Lambdaと地方とコミュニティ
miu_crescent
2
370
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
540
OTelCol_TailSampling_and_SpanMetrics
gumamon
1
220
オープンソースAIとは何か? --「オープンソースAIの定義 v1.0」詳細解説
shujisado
10
1.2k
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
390
生成AIが変えるデータ分析の全体像
ishikawa_satoru
0
170
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
300
エンジニア人生の拡張性を高める 「探索型キャリア設計」の提案
tenshoku_draft
1
130
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Practical Orchestrator
shlominoach
186
10k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Designing Experiences People Love
moore
138
23k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Become a Pro
speakerdeck
PRO
25
5k
A Modern Web Designer's Workflow
chriscoyier
693
190k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
A Tale of Four Properties
chriscoyier
156
23k
The Language of Interfaces
destraynor
154
24k
Transcript
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ Λͬͯ .3$Ͱշదʹ։ൃ͢Δ :VLJ:BTPTIJNB !ZBTP@TBO
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ͷϝϞϦཧ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF "VUPNBUJD3FGFSFODF$PVOUJOH "3$ ࢀরΧϯλํࣜͷϝϞϦཧΛ ίϯύΠϥ͕ࣗಈͰߦͬͯ͘ΕΔ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF "3$ͷઃఆ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF .BOVBM3FGFSFODF$PVOUJOH .3$ ࢀরΧϯλํࣜͷϝϞϦཧΛ खಈͰߦ͏
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF .3$ͰͷϝϞϦཧ TestClass *value = [[TestClass alloc] init]; // ࢀরΧϯτ
1 [value release]; // ࢀরΧϯτ 0
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF .3$ͰͷϝϞϦཧ NSNumber *value = [[NSNumber alloc] initWithInt:1]; [value retain];
// ࢀরΧϯτ 2 [value release]; // ࢀরΧϯτ 1 [value release]; // ࢀরΧϯτ 0
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF .3$ͰͷϝϞϦཧ @autoreleasepool { NSNumber *value = [[NSNumber alloc] initWithInt:1];
[value autorelease]; // ࢀরΧϯτ = 1 (- 1) ... } // ࢀরΧϯτ = 0
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF "3$ͷ߹ͷΫϥε࣮ @implementation Sample { NSNumber *_value; } - (void)setValue:(NSNumber
*)value { _value = value; } - (NSNumber *)value { return _value; } @end
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF .3$ͷ߹ͷΫϥε࣮ @implementation Sample { NSNumber *_value; } - (void)setValue:(NSNumber
*)value { [value retain]; [_value release]; _value = value; } - (NSNumber *)value { return [[_value retain] autorelease]; } - (void)dealloc { [_value release]; [super dealloc]; } @end
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ֦ுࢠΛNNʹ͢Δ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ࣅͨػೳͷݺͼํͷҧ͍ 0CKFDUJWF$ 4XJGU $ Πϯελϯεมɾϝιου ϝϯόมɾؔ ΠχγϟϥΠβ ίϯετϥΫλ δΣωϦΫε
ςϯϓϨʔτ ϒϩοΫɾΫϩʔδϟ ϥϜμࣜ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ #import <Foundation/Foundation.h> #import <string> #import <iostream> void Sample()
{ NSString *objcText = @"Obj-C Text"; NSLog(@"%@", objcText); std::string cppText = "C++ Text"; std::cout << cppText << std::endl; }
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ @implementation ObjcClass { std::string _text; } - (void)setText:(std::string
const &)text { _text = text; } - (std::string const &)text { return _text; } @end
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ class CppClass { NSString *_text; public: void setText(NSString
*text) { _text = text; } NSString *text() { return _text; } };
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF $ ͷϝϞϦཧ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF $ ΦϒδΣΫτͷΠϯελϯεԽ CppClass *pointer = new CppClass(); delete pointer;
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF $ ΦϒδΣΫτͷΠϯελϯεԽ CppClass value_1; CppClass value_2(2); CppClass value_3{3}; CppClass
value_4 = CppClass(4); auto value_5 = CppClass(5);
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ղ์͞ΕΔλΠϛϯάʢؔʣ void SampleFunc() { CppClass value; } // <-
valueؔͷείʔϓΛൈ͚Δ࣌ʹղ์͞ΕΔ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ղ์͞ΕΔλΠϛϯάʢؔʣ void SampleFunc() { if (true) { CppClass value;
} // <- valueJGͷείʔϓΛൈ͚Δ࣌ʹղ์͞ΕΔ }
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ղ์͞ΕΔλΠϛϯάʢ$ Ϋϥεʣ class CppParentClass { public: CppClass value; ~CppParentClass()
{ } // <- σετϥΫλ͕ݺΕͨޙʹϝϯόมvalue͕ղ์͞ΕΔ };
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ղ์͞ΕΔλΠϛϯάʢ0CK$Ϋϥεʣ @implementation ObjcParent { CppClass value; } - (void)dealloc
{ [super dealloc];// <- deallocͷதͰΠϯελϯεม͕ղ์͞ΕΔ } @end
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ͷΦϒδΣΫτΛ อ࣋͢Δ$ ͷΫϥεΛ࡞Δ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ͷΦϒδΣΫτΛ อ࣋͢Δ$ ͷΫϥεΛ࡞ͬͨ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF PCKD@QUSIQQ // // objc_ptr.hpp // #pragma once #include <memory>
#include <functional> template <typename T> class objc_impl { public: T const value; objc_impl(T value) : value(value) { [value retain]; } ~objc_impl() { [value release]; } }; template <typename T> class weak_objc_ptr; template <typename T> objc_ptr<T> make_objc_ptr(T value) { objc_ptr<T> ptr(value); [value release]; return ptr; } template <typename T> class weak_objc_ptr { std::weak_ptr<objc_impl<T>> _impl; public: weak_objc_ptr() = default; weak_objc_ptr(objc_ptr<T> ptr) : _impl(ptr._impl) { } objc_ptr<T> lock() const { if (auto locked = _impl.lock()) { return objc_ptr<T>(locked); } else { return objc_ptr<T>(); } } }; template <typename T> weak_objc_ptr<T> to_weak(objc_ptr<T> ptr) { return weak_objc_ptr<T>(ptr); } template <typename T> class objc_ptr { friend class weak_objc_ptr<T>; std::shared_ptr<objc_impl<T>> _impl; objc_ptr(std::shared_ptr<objc_impl<T>> holder) : _impl(holder) { } public: objc_ptr() : objc_ptr(nil) { } objc_ptr(T value) : _impl(std::make_shared<objc_impl<T>>(value)) { } objc_ptr(std::function<T(void)> handler) { @autoreleasepool { _impl = std::make_shared<objc_impl<T>>(handler()); } } T value() const { return _impl->value; } T operator*() const { return _impl->value; } explicit operator bool() const { return _impl->value != nil; } };
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF PCKD@QUS5 TIBSFE@QUSPCKD@JNQM5 PCKD@QUSΫϥεͷߏ PCKD@JNQM5 ˞50CKFDUJWF$ͷΫϥεͷܕʢʮ/44USJOH ʯͳͲʣ 5WBMVFʢSFUBJOͯ͠อ࣋ʣ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF PCKD@QUSͷ༻ྫ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF SFUBJOͯ͠อ࣋͢Δ TestClass *testObj = [[[TestClass alloc] init] autorelease]; objc_ptr<TestClass
*> test_ptr(testObj); NSLog(@"value : %@", test_ptr.value()); NSLog(@"value : %@", *test_ptr);
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ࢀরΧϯτͦͷ··อ࣋͢Δ auto test_ptr = make_objc_ptr([[TestClass alloc] init]); NSLog(@“value :
%@", *test_ptr);
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF BVUPSFMFBTFΛ֎ͯ͠อ࣋͢Δ objc_ptr<TestClass *> test_ptr( []() { return [[[TestClass alloc]
init] autorelease]; } ); NSLog(@"value : %@", *test_ptr);
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ऑࢀরͰอ࣋͢Δ auto test_ptr = make_objc_ptr([[TestClass alloc] init]); weak_objc_ptr<TestClass *>
weak_ptr = test_ptr; if (auto locked_ptr = weak_ptr.lock()) { NSLog(@"value : %@", *locked_ptr); }
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ऑࢀরΛฦؔ͢ auto test_ptr = make_objc_ptr([[TestClass alloc] init]); auto weak_ptr
= to_weak(test_ptr); auto handler = [weak_ptr]() { auto locked_ptr = weak_ptr.lock(); NSLog(@"locked value : %@", locked_ptr.value()); }; handler();
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ΠϯελϯεมͰ͏ @implementation Sample { objc_ptr<NSNumber *> _value; } -
(void)setValue:(NSNumber *)value { _value = value; } - (NSNumber *)value { return *_value; } @end
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ͭͷ0CKFDUJWF$ΦϒδΣΫτΛ ෳͷPCKD@QUSʹ࣋ͨͤͳ͍ํ͕ྑ͍ ʢಛʹXFBLΛ͏߹ʣ ༻্ͷҙ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF PCKD@QUSಉ࢜ΛൺֱͰ͖ΔΑ͏ʹ͢ΔɺͳͲ "3$ͱ.3$ΛPCKD@QUSͰٵऩ͢Δ ͞Βʹൃలͤ͞Δ
IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ͓ΘΓ