| 1 |
/** |
|---|
| 2 |
XXX |
|---|
| 3 |
|
|---|
| 4 |
Authors: Daniel Keep |
|---|
| 5 |
Copyright: 2006, Daniel Keep |
|---|
| 6 |
License: BSD v2 (http://opensource.org/licenses/bsd-license.php). |
|---|
| 7 |
**/ |
|---|
| 8 |
/** |
|---|
| 9 |
Copyright © 2006 Daniel Keep |
|---|
| 10 |
All rights reserved. |
|---|
| 11 |
|
|---|
| 12 |
Redistribution and use in source and binary forms, with or without |
|---|
| 13 |
modification, are permitted provided that the following conditions are |
|---|
| 14 |
met: |
|---|
| 15 |
|
|---|
| 16 |
* Redistributions of source code must retain the above copyright |
|---|
| 17 |
notice, this list of conditions and the following disclaimer. |
|---|
| 18 |
|
|---|
| 19 |
* Redistributions in binary form must reproduce the above copyright |
|---|
| 20 |
notice, this list of conditions and the following disclaimer in the |
|---|
| 21 |
documentation and/or other materials provided with the distribution. |
|---|
| 22 |
|
|---|
| 23 |
* Neither the name of this software, nor the names of its contributors |
|---|
| 24 |
may be used to endorse or promote products derived from this software |
|---|
| 25 |
without specific prior written permission. |
|---|
| 26 |
|
|---|
| 27 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 28 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|---|
| 29 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 30 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|---|
| 31 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|---|
| 32 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|---|
| 33 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|---|
| 34 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|---|
| 35 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|---|
| 36 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 37 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 38 |
**/ |
|---|
| 39 |
module cairooo.callbacks; |
|---|
| 40 |
|
|---|
| 41 |
private |
|---|
| 42 |
{ |
|---|
| 43 |
import cairo.cairo; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
alias void delegate(ubyte[]) ReadDelegate; |
|---|
| 47 |
alias void delegate(ubyte[]) WriteDelegate; |
|---|
| 48 |
|
|---|
| 49 |
package |
|---|
| 50 |
{ |
|---|
| 51 |
cairo_status_t |
|---|
| 52 |
readFunctionWrapper(void* closure, ubyte* data, uint length) |
|---|
| 53 |
{ |
|---|
| 54 |
// Ok, the closure is actually a pointer to a ReadDelegate object (at |
|---|
| 55 |
// least, we *really* hope that it is :P). What we need to do is wrap |
|---|
| 56 |
// the raw pointer into a dynamic array, and catch any thrown |
|---|
| 57 |
// exceptions. |
|---|
| 58 |
ubyte[] dataArray = data[0..length]; |
|---|
| 59 |
ReadDelegate* readDelegate = cast(ReadDelegate*) closure; |
|---|
| 60 |
|
|---|
| 61 |
try |
|---|
| 62 |
{ |
|---|
| 63 |
(*readDelegate)(dataArray); |
|---|
| 64 |
return cairo_status_t.CAIRO_STATUS_SUCCESS; |
|---|
| 65 |
} |
|---|
| 66 |
catch( Exception e ) |
|---|
| 67 |
{ |
|---|
| 68 |
// Ok, we caught an exception: just return the generic read |
|---|
| 69 |
// failure flag. |
|---|
| 70 |
return cairo_status_t.CAIRO_STATUS_READ_ERROR; |
|---|
| 71 |
} |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
cairo_status_t |
|---|
| 75 |
writeFunctionWrapper(void* closure, ubyte* data, uint length) |
|---|
| 76 |
{ |
|---|
| 77 |
// Again, the closure is actually a pointer to a WriteDelegate. We |
|---|
| 78 |
// just need to convert the raw pointer to a bounds-checked array, and |
|---|
| 79 |
// return the correct failure code when we cop an exception. |
|---|
| 80 |
ubyte[] dataArray = data[0..length]; |
|---|
| 81 |
WriteDelegate* writeDelegate = cast(WriteDelegate*) closure; |
|---|
| 82 |
|
|---|
| 83 |
try |
|---|
| 84 |
{ |
|---|
| 85 |
(*writeDelegate)(dataArray); |
|---|
| 86 |
return cairo_status_t.CAIRO_STATUS_SUCCESS; |
|---|
| 87 |
} |
|---|
| 88 |
catch( Exception e ) |
|---|
| 89 |
{ |
|---|
| 90 |
// Got an exception: return write failure |
|---|
| 91 |
return cairo_status_t.CAIRO_STATUS_WRITE_ERROR; |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|