Slide 1

Slide 1 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$Λ࢖ͬͯ .3$Ͱշదʹ։ൃ͢Δ :VLJ:BTPTIJNB !ZBTP@TBO

Slide 2

Slide 2 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ͷϝϞϦ؅ཧ

Slide 3

Slide 3 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF "VUPNBUJD3FGFSFODF$PVOUJOH "3$ ࢀরΧ΢ϯλํࣜͷϝϞϦ؅ཧΛ ίϯύΠϥ͕ࣗಈͰߦͬͯ͘ΕΔ

Slide 4

Slide 4 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF "3$ͷઃఆ

Slide 5

Slide 5 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF .BOVBM3FGFSFODF$PVOUJOH .3$ ࢀরΧ΢ϯλํࣜͷϝϞϦ؅ཧΛ खಈͰߦ͏

Slide 6

Slide 6 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF .3$ͰͷϝϞϦ؅ཧ TestClass *value = [[TestClass alloc] init]; // ࢀরΧ΢ϯτ 1 [value release]; // ࢀরΧ΢ϯτ 0

Slide 7

Slide 7 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF .3$ͰͷϝϞϦ؅ཧ NSNumber *value = [[NSNumber alloc] initWithInt:1]; [value retain]; // ࢀরΧ΢ϯτ 2 [value release]; // ࢀরΧ΢ϯτ 1 [value release]; // ࢀরΧ΢ϯτ 0

Slide 8

Slide 8 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF .3$ͰͷϝϞϦ؅ཧ @autoreleasepool { NSNumber *value = [[NSNumber alloc] initWithInt:1]; [value autorelease]; // ࢀরΧ΢ϯτ = 1 (- 1) ... } // ࢀরΧ΢ϯτ = 0

Slide 9

Slide 9 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF "3$ͷ৔߹ͷΫϥε࣮૷ @implementation Sample { NSNumber *_value; } - (void)setValue:(NSNumber *)value { _value = value; } - (NSNumber *)value { return _value; } @end

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$

Slide 12

Slide 12 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ֦ுࢠΛNNʹ͢Δ

Slide 13

Slide 13 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ࣅͨػೳͷݺͼํͷҧ͍ 0CKFDUJWF$ 4XJGU $ Πϯελϯεม਺ɾϝιου ϝϯόม਺ɾؔ਺ ΠχγϟϥΠβ ίϯετϥΫλ δΣωϦΫε ςϯϓϨʔτ ϒϩοΫɾΫϩʔδϟ ϥϜμࣜ

Slide 14

Slide 14 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ #import #import #import void Sample() { NSString *objcText = @"Obj-C Text"; NSLog(@"%@", objcText); std::string cppText = "C++ Text"; std::cout << cppText << std::endl; }

Slide 15

Slide 15 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ @implementation ObjcClass { std::string _text; } - (void)setText:(std::string const &)text { _text = text; } - (std::string const &)text { return _text; } @end

Slide 16

Slide 16 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ class CppClass { NSString *_text; public: void setText(NSString *text) { _text = text; } NSString *text() { return _text; } };

Slide 17

Slide 17 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF $ͷϝϞϦ؅ཧ

Slide 18

Slide 18 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF $ΦϒδΣΫτͷΠϯελϯεԽ CppClass *pointer = new CppClass(); delete pointer;

Slide 19

Slide 19 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF $ΦϒδΣΫτͷΠϯελϯεԽ CppClass value_1; CppClass value_2(2); CppClass value_3{3}; CppClass value_4 = CppClass(4); auto value_5 = CppClass(5);

Slide 20

Slide 20 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ղ์͞ΕΔλΠϛϯάʢؔ਺ʣ void SampleFunc() { CppClass value; } // <- value͸ؔ਺ͷείʔϓΛൈ͚Δ࣌ʹղ์͞ΕΔ

Slide 21

Slide 21 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ղ์͞ΕΔλΠϛϯάʢؔ਺ʣ void SampleFunc() { if (true) { CppClass value; } // <- value͸JGͷείʔϓΛൈ͚Δ࣌ʹղ์͞ΕΔ }

Slide 22

Slide 22 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ղ์͞ΕΔλΠϛϯάʢ$Ϋϥεʣ class CppParentClass { public: CppClass value; ~CppParentClass() { } // <- σετϥΫλ͕ݺ͹Εͨޙʹϝϯόม਺value͕ղ์͞ΕΔ };

Slide 23

Slide 23 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ղ์͞ΕΔλΠϛϯάʢ0CK$Ϋϥεʣ @implementation ObjcParent { CppClass value; } - (void)dealloc { [super dealloc];// <- deallocͷதͰΠϯελϯεม਺͕ղ์͞ΕΔ } @end

Slide 24

Slide 24 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ͷΦϒδΣΫτΛ อ࣋͢Δ$ͷΫϥεΛ࡞Δ

Slide 25

Slide 25 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF 0CKFDUJWF$ͷΦϒδΣΫτΛ อ࣋͢Δ$ͷΫϥεΛ࡞ͬͨ

Slide 26

Slide 26 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF PCKD@QUSIQQ // // objc_ptr.hpp // #pragma once #include #include template class objc_impl { public: T const value; objc_impl(T value) : value(value) { [value retain]; } ~objc_impl() { [value release]; } }; template class weak_objc_ptr; template objc_ptr make_objc_ptr(T value) { objc_ptr ptr(value); [value release]; return ptr; } template class weak_objc_ptr { std::weak_ptr> _impl; public: weak_objc_ptr() = default; weak_objc_ptr(objc_ptr ptr) : _impl(ptr._impl) { } objc_ptr lock() const { if (auto locked = _impl.lock()) { return objc_ptr(locked); } else { return objc_ptr(); } } }; template weak_objc_ptr to_weak(objc_ptr ptr) { return weak_objc_ptr(ptr); } template class objc_ptr { friend class weak_objc_ptr; std::shared_ptr> _impl; objc_ptr(std::shared_ptr> holder) : _impl(holder) { } public: objc_ptr() : objc_ptr(nil) { } objc_ptr(T value) : _impl(std::make_shared>(value)) { } objc_ptr(std::function handler) { @autoreleasepool { _impl = std::make_shared>(handler()); } } T value() const { return _impl->value; } T operator*() const { return _impl->value; } explicit operator bool() const { return _impl->value != nil; } };

Slide 27

Slide 27 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF PCKD@QUS5 TIBSFE@QUSPCKD@JNQM5 PCKD@QUSΫϥεͷߏ੒ PCKD@JNQM5 ˞5͸0CKFDUJWF$ͷΫϥεͷܕʢʮ/44USJOHʯͳͲʣ 5WBMVFʢSFUBJOͯ͠อ࣋ʣ

Slide 28

Slide 28 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF PCKD@QUSͷ࢖༻ྫ

Slide 29

Slide 29 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF SFUBJOͯ͠อ࣋͢Δ TestClass *testObj = [[[TestClass alloc] init] autorelease]; objc_ptr test_ptr(testObj); NSLog(@"value : %@", test_ptr.value()); NSLog(@"value : %@", *test_ptr);

Slide 30

Slide 30 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ࢀরΧ΢ϯτͦͷ··อ࣋͢Δ auto test_ptr = make_objc_ptr([[TestClass alloc] init]); NSLog(@“value : %@", *test_ptr);

Slide 31

Slide 31 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF BVUPSFMFBTFΛ֎ͯ͠อ࣋͢Δ objc_ptr test_ptr( []() { return [[[TestClass alloc] init] autorelease]; } ); NSLog(@"value : %@", *test_ptr);

Slide 32

Slide 32 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ऑࢀরͰอ࣋͢Δ auto test_ptr = make_objc_ptr([[TestClass alloc] init]); weak_objc_ptr weak_ptr = test_ptr; if (auto locked_ptr = weak_ptr.lock()) { NSLog(@"value : %@", *locked_ptr); }

Slide 33

Slide 33 text

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();

Slide 34

Slide 34 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF Πϯελϯεม਺Ͱ࢖͏ @implementation Sample { objc_ptr _value; } - (void)setValue:(NSNumber *)value { _value = value; } - (NSNumber *)value { return *_value; } @end

Slide 35

Slide 35 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ͭͷ0CKFDUJWF$ΦϒδΣΫτΛ ෳ਺ͷPCKD@QUSʹ࣋ͨͤͳ͍ํ͕ྑ͍ ʢಛʹXFBLΛ࢖͏৔߹ʣ ࢖༻্ͷ஫ҙ

Slide 36

Slide 36 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF PCKD@QUSಉ࢜ΛൺֱͰ͖ΔΑ͏ʹ͢ΔɺͳͲ "3$ͱ.3$ΛPCKD@QUSͰٵऩ͢Δ ͞Βʹൃలͤ͞Δ

Slide 37

Slide 37 text

IUUQTHJUIVCDPNPCKFDUJWFBVEJPPCKD@QUS@TBNQMF ͓ΘΓ