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
510
モーダルの遷移を理解する
objectiveaudio
4
1.8k
オーディオ波形を表示するために知っておくべきこと
objectiveaudio
0
860
AVAudioEngineでリアルタイムレンダリング
objectiveaudio
1
710
リファクタリング・チャレンジ リバーシ編
objectiveaudio
0
130
UIKitは2度ベルを鳴らす
objectiveaudio
0
280
iOSDC2018.pdf
objectiveaudio
1
2k
Other Decks in Technology
See All in Technology
宇宙ベンチャーにおける最近の情シス取り組みについて
axelmizu
0
110
Wvlet: A New Flow-Style Query Language For Functional Data Modeling and Interactive Data Analysis - Trino Summit 2024
xerial
1
110
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
830
20241220_S3 tablesの使い方を検証してみた
handy
3
170
どちらを使う?GitHub or Azure DevOps Ver. 24H2
kkamegawa
0
600
小学3年生夏休みの自由研究「夏休みに Copilot で遊んでみた」
taichinakamura
0
140
CustomCopを使ってMongoidのコーディングルールを整えてみた
jinoketani
0
220
MLOps の現場から
asei
6
630
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
190
アップデート紹介:AWS Data Transfer Terminal
stknohg
PRO
0
170
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
140
LINEスキマニにおけるフロントエンド開発
lycorptech_jp
PRO
0
330
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
GraphQLとの向き合い方2022年版
quramy
44
13k
Designing for Performance
lara
604
68k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Faster Mobile Websites
deanohume
305
30k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
GitHub's CSS Performance
jonrohan
1030
460k
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 ͓ΘΓ