Last active
May 28, 2016 06:52
-
-
Save callionica/10df35ea677691e2fd84b6a04d51f94f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
template <class T> | |
inline T* as(NSObject* o) { | |
if ([o isKindOfClass: [T class]]) { | |
return (T*)o; | |
} | |
return nil; | |
} | |
template <class T> | |
inline bool is(NSObject* o) { | |
if ([o isKindOfClass: [T class]]) { | |
return true; | |
} | |
return false; | |
} | |
inline NSString* to_NSString(const CGRect& rect) { | |
return NSStringFromCGRect(rect); | |
} | |
inline NSString* to_NSString(const CGPoint& point) { | |
return NSStringFromCGPoint(point); | |
} | |
inline NSString* to_NSString(const std::string& text) { | |
return [[NSString alloc] initWithBytes: text.c_str() length: text.size() encoding: NSUTF8StringEncoding]; | |
} | |
inline NSString* to_NSStringView(const std::string& text) { | |
return [[NSString alloc] initWithBytesNoCopy: const_cast<char*>(text.c_str()) length: text.size() encoding: NSUTF8StringEncoding freeWhenDone: NO]; | |
} | |
inline NSString* to_NSString(const std::shared_ptr<std::string>& text) { | |
if (!text) { | |
return nil; | |
} | |
return to_NSString(*text); | |
} | |
inline std::string to_string(NSData* data) { | |
auto result = std::string(); | |
result.resize(data.length); | |
[data getBytes: const_cast<char*>(result.c_str()) length: result.size()]; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment