![]() |
exceptions4c
version 2.8
An exception handling framework for C
|
exceptions4c automatic resource handling header file More...
Defines | |
Convenience macros for acquiring and disposing resources | |
These macros let you acquire and dispose different kinds of resources according to the dispose pattern. | |
#define | e4c_using_memory(_buffer_, _bytes_) |
Introduces a block of code with automatic acquisition and disposal of a memory buffer. | |
#define | e4c_using_file(_file_, _path_, _mode_) |
Introduces a block of code with automatic acquisition and disposal of a file stream. | |
#define | e4c_using_mutex(_mutex_) |
Introduces a block of code with automatic acquisition and disposal of a mutex. | |
Variables | |
Resource handling exceptions | |
const e4c_exception_type | MemoryAllocationException |
This exception is thrown when malloc returns a null pointer. | |
const e4c_exception_type | FileException |
This exception is thrown when a file error occurs. | |
const e4c_exception_type | FileOpenException |
This exception is thrown when a file cannot be opened. | |
const e4c_exception_type | FileCloseException |
This exception is thrown when a file cannot be closed. | |
const e4c_exception_type | MutexException |
This exception is thrown when a mutex error occurs. | |
const e4c_exception_type | MutexLockException |
This exception is thrown when a mutex cannot be locked. | |
const e4c_exception_type | MutexUnlockException |
This exception is thrown when a mutex cannot be unlocked. |
exceptions4c automatic resource handling header file
This header file needs to be included in order to be able to use any of the automatic resource handling macros:
This is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this software. If not, see <http://www.gnu.org/licenses/>.
#define e4c_using_file | ( | _file_, | |
_path_, | |||
_mode_ | |||
) |
E4C_WITH(_file_, e4c_dispose_file){ \
_file_ = e4c_acquire_file(_path_, _mode_); \
if(_file_ == NULL){ \
E4C_THROW(FileOpenException, "Could not open file: " #_path_); \
} \
}E4C_USE
Introduces a block of code with automatic acquisition and disposal of a file stream.
_file_ | The file to be acquired, used and then disposed |
_path_ | The path of the file to be opened |
_mode_ | The access mode for the file |
This macro lets you acquire and dispose (open and close) files according to the dispose pattern:
FILE * file; e4c_using_file(file, "log.txt", "a"){ // implicit: file = fopen("log.txt", "a"); fputs("hello, world!\n", file); // implicit: fclose(file); }
FileOpenException | If fopen returns NULL |
FileCloseException | If fclose does not return zero |
#define e4c_using_memory | ( | _buffer_, | |
_bytes_ | |||
) |
E4C_WITH(_buffer_, e4c_dispose_memory){ \
_buffer_ = e4c_acquire_memory(_bytes_); \
if(_buffer_ == NULL){ \
E4C_THROW(MemoryAllocationException, \
"Could not allocate memory for: " #_buffer_); \
} \
}E4C_USE
Introduces a block of code with automatic acquisition and disposal of a memory buffer.
_buffer_ | The buffer to be acquired, used and then disposed |
_bytes_ | The amount of memory to be allocated (in bytes) |
This macro lets you acquire and dispose memory buffers according to the dispose pattern:
void * buffer; e4c_using_memory(buffer, 1024){ // implicit: buffer = malloc(1024); memset(buffer, 0, 1024); // implicit: free(buffer); }
MemoryAllocationException | If malloc returns NULL |
#define e4c_using_mutex | ( | _mutex_ | ) |
E4C_WITH(_mutex_, e4c_dispose_mutex){ \
int result = e4c_acquire_mutex(_mutex_); \
if(result != 0){ \
E4C_THROW(MutexLockException, "Could not lock mutex: " #_mutex_); \
} \
}E4C_USE
Introduces a block of code with automatic acquisition and disposal of a mutex.
_mutex_ | The mutex to be locked, used and then unlocked |
This macro lets you lock and unlock mutexes according to the dispose pattern:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int counter = 0; e4c_using_mutex(&mutex){ // implicit: pthread_mutex_lock(&mutex); counter++; // implicit: pthread_mutex_unlock(&mutex); }
MutexLockException | If pthread_mutex_lock does not return zero |
MutexUnlockException | If pthread_mutex_unlock does not return zero |
This exception is thrown when a file cannot be closed.
FileCloseException
is thrown by e4c_using_file
when fclose
does not return zero
for whatever reason.
This exception is thrown when a file error occurs.
FileException
is the general type of exceptions produced by failed file operations.
error_number
of the exception; it captures the value of errno
at the time the exception was thrown (i. e. right after fopen
or fclose
).This exception is thrown when a file cannot be opened.
FileOpenException
is thrown by e4c_using_file
when fopen
returns NULL
for whatever reason.
This exception is thrown when malloc returns a null pointer.
MemoryAllocationException
is thrown by e4c_using_memory
when malloc
returns NULL
for whatever reason.
This exception is thrown when a mutex error occurs.
MutexException
is the general type of exceptions produced by failed mutex operations.
This exception is thrown when a mutex cannot be locked.
MutexLockException
is thrown by e4c_using_mutex
when fopen
returns NULL
for whatever reason.
This exception is thrown when a mutex cannot be unlocked.
MutexUnlockException
is thrown by e4c_using_mutex
when fclose
does not return zero
for whatever reason.