Skip to content

Instantly share code, notes, and snippets.

@k06a
Last active January 15, 2016 08:30
Show Gist options
  • Save k06a/cb6de3ffd85fd706fcc5 to your computer and use it in GitHub Desktop.
Save k06a/cb6de3ffd85fd706fcc5 to your computer and use it in GitHub Desktop.
NSValue C++ object
#import <Foundation/Foundation.h>
namespace cv {
class Mat;
}
@interface NSValue (CVMat)
+ (instancetype)valueWithCVMat:(cv::Mat)mat;
@property (readonly) cv::Mat CVMatValue;
@end
#import <opencv2/opencv.hpp>
#import "NSValue+CVMat.h"
@interface NSValueDeleteOnDealloc : NSValue
@property (strong, nonatomic) NSValue *value;
@end
@implementation NSValueDeleteOnDealloc
- (void)dealloc {
delete self.value.pointerValue;
}
- (BOOL)respondsToSelector:(SEL)aSelector {
return [super respondsToSelector:aSelector]
|| [self.value respondsToSelector:aSelector];
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
return self.value;
}
@end
//
@implementation NSValue (CVMat)
+ (instancetype)valueWithCVMat:(cv::Mat)mat {
NSValueDeleteOnDealloc *me = [[NSValueDeleteOnDealloc alloc] init];
me.value = [NSValue valueWithPointer:(new cv::Mat(mat))];
return me;
}
- (cv::Mat)CVMatValue {
if ([self isKindOfClass:[NSValueDeleteOnDealloc class]]) {
NSValueDeleteOnDealloc *me = self;
return *(cv::Mat *)me.value.pointerValue;
}
return cv::Mat();
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment