-
Notifications
You must be signed in to change notification settings - Fork 57
Background delegates
Dimitrios Amaxilatis edited this page Jul 22, 2013
·
1 revision
Delegates are a concept from C# for storing member function pointers (and ordinary function pointers, and static member function pointers, ...), but thereby avoiding virtual functions. More info is available at:
- Delegates in C#: http://www.codeproject.com/KB/cs/delegates.aspx
- Very short overview covering different approaches in C++: http://www.rsdn.ru/Forum/Message.aspx?mid=1712875&only=1
- A general explanation: http://aigamedev.com/programming-tips/fast-delegates-game-logic
- Sergey Ryazanov: http://www.codeproject.com/KB/cpp/ImpossiblyFastCppDelegate.aspx
- myelin: http://www.myelin.co.nz/notes/callbacks/cpp-delegates.html
- Yet Another Generalized Functors Implementation in C++: http://www.codeproject.com/KB/cpp/genfunctors.aspx
- Don Clugston: http://www.codeproject.com/KB/cpp/FastDelegate.aspx
- JaeWook Choi, Delegates with Boost: http://www.codeproject.com/KB/cpp/fastdelegate2.aspx
- Callbacks in C++ using template functors, "origin" of delegates, from 1994: http://www.tutok.sk/fastgl/callback.html
Example:
static void scw_notify_receivers( struct netpacket_handler_args* args )
{
receivers[3]( 0x1a, 0x2b, 0 );
}
Assembler output:
00002bf6 <_ZN7wiselib20scw_notify_receiversEP22netpacket_handler_args>:
2bf6: 0c 43 clr r12 ;
2bf8: 7d 40 2b 00 mov.b #43, r13 ;#0x002b
2bfc: 3e 40 1a 00 mov #26, r14 ;#0x001a
2c00: 1f 42 80 11 mov &0x1180,r15 ;0x1180
2c04: 92 12 82 11 call &0x1182 ;
2c08: 30 41 ret
Assembler output:
00002c22 <_ZN7wiselib20scw_notify_receiversEP22netpacket_handler_args>:
2c22: 0b 12 push r11 ;
2c24: 0a 12 push r10 ;
2c26: 1d 42 88 11 mov &0x1188,r13 ;0x1188
2c2a: 1e 42 8a 11 mov &0x118a,r14 ;0x118a
2c2e: 1a 42 86 11 mov &0x1186,r10 ;0x1186
2c32: 0a 5e add r14, r10 ;
2c34: 0f 4d mov r13, r15 ;
2c36: 1f f3 and #1, r15 ;r3 As==01
2c38: 4f 93 cmp.b #0, r15 ;r3 As==00
2c3a: 05 24 jz $+12 ;abs 0x2c46
2c3c: 0f 4d mov r13, r15 ;
2c3e: 1f c3 bic #1, r15 ;r3 As==01
2c40: 2f 5a add @r10, r15 ;
2c42: 2b 4f mov @r15, r11 ;
2c44: 01 3c jmp $+4 ;abs 0x2c48
2c46: 0b 4d mov r13, r11 ;
2c48: 0c 43 clr r12 ;
2c4a: 7d 40 2b 00 mov.b #43, r13 ;#0x002b
2c4e: 3e 40 1a 00 mov #26, r14 ;#0x001a
2c52: 0f 4a mov r10, r15 ;
2c54: 8b 12 call r11 ;
2c56: 3a 41 pop r10 ;
2c58: 3b 41 pop r11 ;
2c5a: 30 41 ret
Example:
virtual void execute( void *data )
{
del_( data );
//del_ = isense_timer_delegate_t();
}
Assembler output:
04001ee4 <__ZNK9delegate1IvPvEclES0_>:
4001ee4: 9c 21 ff fc b.addi r1,r1,-0x4
4001ee8: d4 01 48 00 b.sw 0x0(r1),r9
4001eec: 84 e3 00 04 b.lwz r7,0x4(r3)
4001ef0: 48 00 38 00 l.jalr r7
4001ef4: 84 63 00 00 b.lwz r3,0x0(r3)
4001ef8: 85 21 00 00 b.lwz r9,0x0(r1)
4001efc: 44 00 48 00 l.jr r9
4001f00: 9c 21 00 04 b.addi r1,r1,0x4
04001f04 <__ZN7wiselib74_GLOBAL__N_.._.._external_interface_isense_isense_os.cpp_00000000_6B6A63C719iSenseTimerCallback7executeEPv>:
4001f04: 03 ff ff f8 l.j 4001ee4 <__ZNK9delegate1IvPvEclES0_>
4001f08: 9c 63 00 04 b.addi r3,r3,0x4
Assembler output:
04001f44 <__ZNK12fastdelegate13FastDelegate1IPvvEclES1_>:
4001f44: 9c 21 ff e4 b.addi r1,r1,-0x1c
4001f48: d4 01 48 18 b.sw 0x18(r1),r9
4001f4c: d4 01 c0 14 b.sw 0x14(r1),r24
4001f50: d4 01 d0 10 b.sw 0x10(r1),r26
4001f54: ab 44 00 00 b.ori r26,r4,0x0
4001f58: 07 ff ff f6 l.jal 4001f30 <__ZNK12fastdelegate6detail10ClosurePtrIMNS0_12GenericClassEFvPvEPFvS3_ES7_E16GetClosureMemPtrEv>
4001f5c: 87 03 00 00 b.lwz r24,0x0(r3)
4001f60: d4 01 58 00 b.sw 0x0(r1),r11
4001f64: d4 01 60 04 b.sw 0x4(r1),r12
4001f68: d4 01 58 08 b.sw 0x8(r1),r11
4001f6c: a4 eb 00 01 b.andi r7,r11,0x1
4001f70: bc 07 00 00 b.sfeqi r7,0x0
4001f74: 10 00 00 06 b.bf 4001f8c <__ZNK12fastdelegate13FastDelegate1IPvvEclES1_+0x48>
4001f78: d4 01 60 0c b.sw 0xc(r1),r12
4001f7c: e0 f8 60 00 b.add r7,r24,r12
4001f80: 84 e7 00 00 b.lwz r7,0x0(r7)
4001f84: e0 e7 58 00 b.add r7,r7,r11
4001f88: 85 67 ff ff b.lwz r11,-0x1(r7)
4001f8c: e0 78 60 00 b.add r3,r24,r12
4001f90: 48 00 58 00 l.jalr r11
4001f94: a8 9a 00 00 b.ori r4,r26,0x0
4001f98: 87 41 00 10 b.lwz r26,0x10(r1)
4001f9c: 87 01 00 14 b.lwz r24,0x14(r1)
4001fa0: 85 21 00 18 b.lwz r9,0x18(r1)
4001fa4: 44 00 48 00 l.jr r9
4001fa8: 9c 21 00 1c b.addi r1,r1,0x1c
04001fac <__ZN7wiselib74_GLOBAL__N_.._.._external_interface_isense_isense_os.cpp_00000000_1DE8CCC319iSenseTimerCallback7executeEPv>:
4001fac: 03 ff ff e6 l.j 4001f44 <__ZNK12fastdelegate13FastDelegate1IPvvEclES1_>
4001fb0: 9c 63 00 04 b.addi r3,r3,0x4