| 1 |
/* |
|---|
| 2 |
Copyright 2006, 2007 Kirk McDonald |
|---|
| 3 |
|
|---|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
|---|
| 5 |
this software and associated documentation files (the "Software"), to deal in |
|---|
| 6 |
the Software without restriction, including without limitation the rights to |
|---|
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
|---|
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
|---|
| 9 |
so, subject to the following conditions: |
|---|
| 10 |
|
|---|
| 11 |
The above copyright notice and this permission notice shall be included in all |
|---|
| 12 |
copies or substantial portions of the Software. |
|---|
| 13 |
|
|---|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|---|
| 20 |
SOFTWARE. |
|---|
| 21 |
*/ |
|---|
| 22 |
|
|---|
| 23 |
/** |
|---|
| 24 |
* This module used to contain some more or less dirty hacks for converting |
|---|
| 25 |
* between function and delegate types. As of DMD 0.174, the language has |
|---|
| 26 |
* built-in support for hacking apart delegates like this. Hooray! |
|---|
| 27 |
*/ |
|---|
| 28 |
module pyd.dg_convert; |
|---|
| 29 |
|
|---|
| 30 |
import pyd.lib_abstract : |
|---|
| 31 |
ParameterTypeTuple, |
|---|
| 32 |
ReturnType |
|---|
| 33 |
; |
|---|
| 34 |
//import std.traits; |
|---|
| 35 |
|
|---|
| 36 |
template fn_to_dgT(Fn) { |
|---|
| 37 |
alias ParameterTypeTuple!(Fn) T; |
|---|
| 38 |
alias ReturnType!(Fn) Ret; |
|---|
| 39 |
|
|---|
| 40 |
alias Ret delegate(T) type; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
/** |
|---|
| 44 |
* This template converts a function type into an equivalent delegate type. |
|---|
| 45 |
*/ |
|---|
| 46 |
template fn_to_dg(Fn) { |
|---|
| 47 |
alias fn_to_dgT!(Fn).type fn_to_dg; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
// Breaking out the old hack again for GDC support... |
|---|
| 51 |
struct dg_struct { |
|---|
| 52 |
void* ptr; |
|---|
| 53 |
void* funcptr; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
union dg_hack(T) { |
|---|
| 57 |
dg_struct fake_dg; |
|---|
| 58 |
T real_dg; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
/** |
|---|
| 62 |
* This template function converts a pointer to a member function into a |
|---|
| 63 |
* delegate. |
|---|
| 64 |
*/ |
|---|
| 65 |
fn_to_dg!(Fn) dg_wrapper(T, Fn) (T t, Fn fn) { |
|---|
| 66 |
//fn_to_dg!(Fn) dg; |
|---|
| 67 |
//dg.ptr = t; |
|---|
| 68 |
//dg.funcptr = fn; |
|---|
| 69 |
|
|---|
| 70 |
//return dg; |
|---|
| 71 |
|
|---|
| 72 |
dg_hack!(fn_to_dg!(Fn)) dg; |
|---|
| 73 |
dg.fake_dg.ptr = cast(void*)t; |
|---|
| 74 |
dg.fake_dg.funcptr = fn; |
|---|
| 75 |
return dg.real_dg; |
|---|
| 76 |
} |
|---|