-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
98 lines (73 loc) · 1.95 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// util.h
// AiffPlayer
//
// Created by koji on 11/01/06.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#ifndef __UTIL_H__
#define __UTIL_H__
#import <Cocoa/Cocoa.h>
#import "MacRuby/MacRuby.h"
#include <string>
#include <typeinfo>
//demangle function
//http://d.hatena.ne.jp/hidemon/20080731/1217488497
#include <string>
std::string demangle(const char * name);
//dump C struct with MacRuby
//using ruby to meta
template <typename T>
void dump_struct(const T &t){
const std::type_info &type = typeid(t);
std::string demangled_type_name = demangle(type.name());
NSValue *v = [NSValue valueWithPointer:&t];
NSString *typeName = [NSString stringWithCString:demangled_type_name.c_str() encoding:kCFStringEncodingUTF8 ];
id ruby_util = [[MacRuby sharedRuntime] evaluateString:@"RUtil"];
[ruby_util performRubySelector:@selector(dump_struct_withName:) withArguments:v,typeName,NULL];
}
//convert enum four cc to human readable
NSString *EnumToFOURCC(UInt32 val);
// macro IF_FAILED(result,message){...}
/*
usage:
OSStatus result = SomeAPIReturnsOSStatus(....);
IF_FAILED(result, "uups failed to SomeAPIReturnsOSStatus")){
return ;
}
//in above code . block only exeuted if result != noErr.
*/
#define FAILED_BLOCK(result,message) if ( FailChecker checker = FailChecker(result,message) ){} else
#define IF_FAILED(result,message) if ( FailChecker checker = FailChecker(result,message) ){} else
class FailChecker{
public:
FailChecker(OSStatus result, const char *msg){
_result = result;
if (_result != noErr){
NSLog(@"%s. err=%d", msg, _result);
}
}
operator bool (){
return (_result == noErr);
}
private:
OSStatus _result;
};
//benchmark
class Timer{
public:
Timer(){
}
void start(){
_startTime = CFAbsoluteTimeGetCurrent();
}
void stop(){
_endTime = CFAbsoluteTimeGetCurrent();
}
CFAbsoluteTime result(){
return _endTime - _startTime;
}
private:
CFAbsoluteTime _startTime, _endTime;
};
#endif //__UTIL_H__