| 1 | package SDL2::error 0.01 { | ||
| 2 | use strictures 2; | ||
| 3 | use experimental 'signatures'; | ||
| 4 | use SDL2::Utils; | ||
| 5 | # | ||
| 6 | enum SDL_errorcode => [ | ||
| 7 | qw[ | ||
| 8 | SDL_ENOMEM | ||
| 9 | SDL_EFREAD | ||
| 10 | SDL_EFWRITE | ||
| 11 | SDL_EFSEEK | ||
| 12 | SDL_UNSUPPORTED | ||
| 13 | SDL_LASTERROR | ||
| 14 | ] | ||
| 15 | ]; | ||
| 16 | attach error => { | ||
| 17 | SDL_SetError => [ | ||
| 18 | ['string'] => 'int' => | ||
| 19 | sub ( $inner, $fmt, @params ) { $inner->( sprintf( $fmt, @params ) ); } | ||
| 20 | ], | ||
| 21 | SDL_GetError => [ [] => 'string' ], | ||
| 22 | SDL_GetErrorMsg => [ | ||
| 23 | [ 'string', 'int' ] => 'string' => sub ( $inner, $errstr, $maxlen = length $errstr ) { | ||
| 24 | $_[1] = ' ' x $maxlen if !defined $_[1] || length $errstr != $maxlen; | ||
| 25 | $inner->( $_[1], $maxlen ); | ||
| 26 | } | ||
| 27 | ], | ||
| 28 | SDL_ClearError => [ [] => 'void' ], | ||
| 29 | SDL_Error => [ ['SDL_errorcode'], 'int' ] | ||
| 30 | }; | ||
| 31 | |||
| 32 | =encoding utf-8 | ||
| 33 | |||
| 34 | =head1 NAME | ||
| 35 | |||
| 36 | SDL2::error - Simple Error Message Routines for SDL | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | use SDL2 qw[:error]; | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | Functions in this import tag provide simple error message routines for SDL. | ||
| 45 | C<SDL_GetError( )>|/C<SDL_GetError( )> >> can be called for almost all SDL | ||
| 46 | functions to determine what problems are occurring. Check the wiki page of each | ||
| 47 | specific SDL function to see whether L<< C<SDL_GetError( )>|/C<SDL_GetError( )> | ||
| 48 | >> is meaningful for them or not. These functions may be imported with the | ||
| 49 | C<:error> tag. | ||
| 50 | |||
| 51 | =head1 Functions | ||
| 52 | |||
| 53 | The SDL error messages are in English. | ||
| 54 | |||
| 55 | =head2 C<SDL_SetError( ... )> | ||
| 56 | |||
| 57 | Set the SDL error message for the current thread. | ||
| 58 | |||
| 59 | Calling this function will replace any previous error message that was set. | ||
| 60 | |||
| 61 | This function always returns C<-1>, since SDL frequently uses C<-1> to signify | ||
| 62 | an failing result, leading to this idiom: | ||
| 63 | |||
| 64 | if ($error_code) { | ||
| 65 | return SDL_SetError( 'This operation has failed: %d', $error_code ); | ||
| 66 | } | ||
| 67 | |||
| 68 | Expected parameters: | ||
| 69 | |||
| 70 | =over | ||
| 71 | |||
| 72 | =item C<fmt> | ||
| 73 | |||
| 74 | a C<printf( )>-style message format string | ||
| 75 | |||
| 76 | =item C<@params> | ||
| 77 | |||
| 78 | additional parameters matching % tokens in the C<fmt> string, if any | ||
| 79 | |||
| 80 | =back | ||
| 81 | |||
| 82 | =head2 C<SDL_GetError( )> | ||
| 83 | |||
| 84 | Retrieve a message about the last error that occurred on the current thread. | ||
| 85 | |||
| 86 | warn SDL_GetError( ); | ||
| 87 | |||
| 88 | It is possible for multiple errors to occur before calling C<SDL_GetError( )>. | ||
| 89 | Only the last error is returned. | ||
| 90 | |||
| 91 | The message is only applicable when an SDL function has signaled an error. You | ||
| 92 | must check the return values of SDL function calls to determine when to | ||
| 93 | appropriately call C<SDL_GetError( )>. You should B<not> use the results of | ||
| 94 | C<SDL_GetError( )> to decide if an error has occurred! Sometimes SDL will set | ||
| 95 | an error string even when reporting success. | ||
| 96 | |||
| 97 | SDL will B<not> clear the error string for successful API calls. You B<must> | ||
| 98 | check return values for failure cases before you can assume the error string | ||
| 99 | applies. | ||
| 100 | |||
| 101 | Error strings are set per-thread, so an error set in a different thread will | ||
| 102 | not interfere with the current thread's operation. | ||
| 103 | |||
| 104 | The returned string is internally allocated and must not be freed by the | ||
| 105 | application. | ||
| 106 | |||
| 107 | Returns a message with information about the specific error that occurred, or | ||
| 108 | an empty string if there hasn't been an error message set since the last call | ||
| 109 | to L<< C<SDL_ClearError( )>|/C<SDL_ClearError( )> >>. The message is only | ||
| 110 | applicable when an SDL function has signaled an error. You must check the | ||
| 111 | return values of SDL function calls to determine when to appropriately call | ||
| 112 | C<SDL_GetError( )>. | ||
| 113 | |||
| 114 | =head2 C<SDL_GetErrorMsg( ... )> | ||
| 115 | |||
| 116 | Get the last error message that was set for the current thread. | ||
| 117 | |||
| 118 | my $x; | ||
| 119 | warn SDL_GetErrorMsg($x, 300); | ||
| 120 | |||
| 121 | This allows the caller to copy the error string into a provided buffer, but | ||
| 122 | otherwise operates exactly the same as L<< C<SDL_GetError( )>|/C<SDL_GetError( | ||
| 123 | )> >>. | ||
| 124 | |||
| 125 | =over | ||
| 126 | |||
| 127 | =item C<errstr> | ||
| 128 | |||
| 129 | A buffer to fill with the last error message that was set for the current | ||
| 130 | thread | ||
| 131 | |||
| 132 | =item C<maxlen> | ||
| 133 | |||
| 134 | The size of the buffer pointed to by the errstr parameter | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | Returns the pointer passed in as the C<errstr> parameter. | ||
| 139 | |||
| 140 | =head2 C<SDL_ClearError( )> | ||
| 141 | |||
| 142 | Clear any previous error message for this thread. | ||
| 143 | |||
| 144 | SDL_ClearError( ); | ||
| 145 | |||
| 146 | =head2 C<SDL_Error( ... )> | ||
| 147 | |||
| 148 | Set the current error to a member of the L<< | ||
| 149 | C<<SDL_errorcode>|SDL2::Enum/C<:errorcode> >> enum. | ||
| 150 | |||
| 151 | SDL_Error( SDL_EFWRITE ); | ||
| 152 | |||
| 153 | Unconditionally returns C<-1>. | ||
| 154 | |||
| 155 | =head1 Enumerations | ||
| 156 | |||
| 157 | These are defined for your use! | ||
| 158 | |||
| 159 | =head2 C<SDL_errorcode> | ||
| 160 | |||
| 161 | These values may be imported with the C<:errorcode> tag. | ||
| 162 | |||
| 163 | =over | ||
| 164 | |||
| 165 | =item C<SDL_ENOMEM> - Out of memory | ||
| 166 | |||
| 167 | =item C<SDL_EFREAD> - Error reading file | ||
| 168 | |||
| 169 | =item C<SDL_EFWRITE> - Error writing file | ||
| 170 | |||
| 171 | =item C<SDL_EFSEEK> - Error seeking in file | ||
| 172 | |||
| 173 | =item C<SDL_UNSUPPORTED> | ||
| 174 | |||
| 175 | =item C<SDL_LASTERROR> | ||
| 176 | |||
| 177 | =back | ||
| 178 | |||
| 179 | =head1 LICENSE | ||
| 180 | |||
| 181 | Copyright (C) Sanko Robinson. | ||
| 182 | |||
| 183 | This library is free software; you can redistribute it and/or modify it under | ||
| 184 | the terms found in the Artistic License 2. Other copyrights, terms, and | ||
| 185 | conditions may apply to data transmitted through this module. | ||
| 186 | |||
| 187 | =head1 AUTHOR | ||
| 188 | |||
| 189 | Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||
| 190 | |||
| 191 | =begin stopwords | ||
| 192 | |||
| 193 | errstr | ||
| 194 | |||
| 195 | =end stopwords | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | }; | ||
| 200 | 1; |