Last active
January 15, 2016 08:30
-
-
Save k06a/cb6de3ffd85fd706fcc5 to your computer and use it in GitHub Desktop.
NSValue C++ object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
namespace cv { | |
class Mat; | |
} | |
@interface NSValue (CVMat) | |
+ (instancetype)valueWithCVMat:(cv::Mat)mat; | |
@property (readonly) cv::Mat CVMatValue; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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