IOSAD-184 Designated constructor

IOSAD-184
Dirk Zimmermann 2 years ago
parent 9f5588c5f3
commit 06eca6d6f0

@ -11,9 +11,9 @@
#import "message.h"
/// Implements a basic method to enable automated reference counting (ARC)
/// for pointers that are not aware of it, e.g. allocatey by malloc.
/// for pointers that are not aware of it, e.g. allocated by malloc.
///
/// When this object goes out of scope (is freed), it calls free() on the managed pointer.
/// When this object goes out of scope (is released), it calls `free()` or a configurable function on the managed pointer.
@interface PEPAutoPointer : NSObject
/// The function that will be used to free the managed pointer, `free` by default.
@ -22,6 +22,16 @@
/// Specialized version that will auto-release/free the given message struct when it goes out of scope.
+ (instancetype)autoPointerWithMessage:(message *)message;
/// Construct an object containing a pointer, and invoke the freeing function when the object,
/// and therefore the pointer, goes out of scope.
/// @param pointer The pointer to free when going out of scope.
/// @param freeFn The function to use for freeing the pointer.
- (instancetype)initWithPointer:(void *)pointer freeFn:(void (*)(void *))freeFn;
/// Constructs an object containing nil as a pointer, and using `free()` as the method to free it.
/// In order to be useful, the pointer needs to be filled after that. See `voidPointerPointer` or `charPointerPointer`.
- (instancetype)init;
/// Specialized version that will auto-release/free the given message struct when it goes out of scope.
- (instancetype)initWithMessage:(message *)message;

@ -21,25 +21,24 @@
return [[self alloc] initWithMessage:message];
}
- (instancetype)init
- (instancetype)initWithPointer:(void *)pointer freeFn:(void (*)(void *))freeFn
{
self = [super init];
if (self) {
// By default, use free() for releasing the internal pointer
self.freeFn = free;
_thePointer = pointer;
_freeFn = freeFn;
}
return self;
}
- (instancetype)initWithMessage:(message *)message
{
self = [self init];
if (self) {
_thePointer = message;
// For freeing a message, free_message() is needed
_freeFn = (void (*)(void *)) free_message;
}
return self;
return [self initWithPointer:message freeFn:(void (*)(void *)) free_message];
}
- (instancetype)init
{
return [self initWithPointer:nil freeFn:free];
}
- (void **)voidPointerPointer

Loading…
Cancel
Save