Skip to content

Instantly share code, notes, and snippets.

@adamnemecek
Last active August 29, 2015 14:19
Show Gist options
  • Save adamnemecek/160a3d56c4e663eaa961 to your computer and use it in GitHub Desktop.
Save adamnemecek/160a3d56c4e663eaa961 to your computer and use it in GitHub Desktop.
cocoa.snippets.mm
#import <Foundation/Foundation.h>
void loadFScript() {
[[[NSBundle bundleWithPath:@"/Library/Frameworks/FScript.framework"] classNamed:@"FScriptMenuItem"] performSelector:@selector(insertInMainMenu)];
}
template<typename T>
inline
NSValue* NSValueFromStruct(T *object)
{
return [NSValue value:object withObjCType:@encode(T)];
}
template<typename T>
inline
T NSValueToStruct(NSValue *value)
{
T s = {};
[value getValue:&s];
return s;
}
void f()
{
NSRect r = {};
NSMutableArray *a = [[NSMutableArray alloc] init];
[a addObject:NSValueFromStruct<NSRect>(&r)];
NSLog(@"%@", NSStringFromRect(r));
NSLog(@"%@", a);
NSLog(@"%@", NSStringFromRect(NSValueToStruct<NSRect>([a objectAtIndex:0])));
}
//
template<typename T>
T WXMin(T a, T b);
template<typename T>
T WXMax(T a, T b);
template<>
float WXMin(float a, float b) {
return fminf(a, b);
}
template<>
float WXMax(float a, float b) {
return fmaxf(a, b);
}
template<typename T>
class WXMinMax {
public:
WXMinMax(T _a, T _b): a(_a), b(_b) {};
inline T min() { return WXMin(a, b); }
inline T max() { return WXMax(a, b); }
inline T diff() { return max() - min(); }
private:
T a;
T b;
};
int main(int argc, char *argv[]) {
@autoreleasepool {
f();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment