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
460
モーダルの遷移を理解する
objectiveaudio
4
1.7k
オーディオ波形を表示するために知っておくべきこと
objectiveaudio
0
840
AVAudioEngineでリアルタイムレンダリング
objectiveaudio
1
680
リファクタリング・チャレンジ リバーシ編
objectiveaudio
0
130
UIKitは2度ベルを鳴らす
objectiveaudio
0
270
iOSDC2018.pdf
objectiveaudio
1
2k
Other Decks in Technology
See All in Technology
Commitment vs Harrisonism - Keynote for Scrum Niseko 2024
miholovesq
6
1.1k
Forget efficiency – Become more productive without the stress
ufried
0
150
ガバメントクラウド先行事業中間報告を読み解く
sugiim
1
1.4k
[AWS JAPAN 生成AIハッカソン] Dialog の紹介
yoshimi0227
0
150
一休.comレストランにおけるRustの活用
kymmt90
3
590
30万人が利用するチャットをFirebase Realtime DatabaseからActionCableへ移行する方法
ryosk7
5
350
バクラクにおける可観測性向上の取り組み
yuu26
3
420
スプリントゴールにチームの状態も設定する背景とその効果 / Team state in sprint goals why and impact
kakehashi
2
100
Product Engineer Night #6プロダクトエンジニアを育む仕組み・施策
hacomono
PRO
1
470
20241031_AWS_生成AIハッカソン_GenMuck
tsumita
0
110
Aurora_BlueGreenDeploymentsやってみた
tsukasa_ishimaru
1
130
プロダクトエンジニアが活躍する環境を作りたくて 事業責任者になった話 ~プロダクトエンジニアの行き着く先~
gimupop
1
480
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
167
49k
Making Projects Easy
brettharned
115
5.9k
What's new in Ruby 2.0
geeforr
342
31k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Facilitating Awesome Meetings
lara
49
6k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
Unsuck your backbone
ammeep
668
57k
Statistics for Hackers
jakevdp
796
220k
Building an army of robots
kneath
302
42k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
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 ͓ΘΓ