15 #ifndef SHAKA_EMBEDDED_UTIL_OBJC_UTILS_H_ 16 #define SHAKA_EMBEDDED_UTIL_OBJC_UTILS_H_ 18 #if !defined(__OBJC__) || !defined(__cplusplus) 19 # error "Can only be included from Objective-C++" 22 #include <Foundation/Foundation.h> 25 #include <type_traits> 26 #include <unordered_map> 44 static NSString *
ToObjc(
const std::string &value) {
45 return [[NSString alloc] initWithBytes:value.c_str()
47 encoding:NSUTF8StringEncoding];
51 return [value UTF8String];
58 return value ? YES : NO;
72 return value.
has_value() ? [[NSNumber alloc] initWithDouble:value.
value()]
88 static NSMutableArray<dest_type> *
ToObjc(
const std::vector<T>& value) {
89 NSMutableArray<dest_type>* ret =
90 [[NSMutableArray<dest_type> alloc] initWithCapacity:value.size()];
91 for (
size_t i = 0; i < value.size(); i++)
96 static std::vector<T>
FromObjc(NSArray<dest_type> *value) {
98 ret.reserve([value count]);
99 for (
size_t i = 0; i < [value count]; i++)
105 template <
typename T>
109 static NSMutableDictionary<NSString *, dest_type> *
ToObjc(
110 const std::unordered_map<std::string, T> &map) {
111 std::vector<id> keys;
112 keys.reserve(map.size());
113 std::vector<id> values;
114 values.reserve(map.size());
116 for (
const auto& pair : map) {
121 return [NSMutableDictionary dictionaryWithObjects:values.data()
126 static std::unordered_map<std::string, T>
FromObjc(
127 NSDictionary<NSString *, dest_type> *value) {
128 std::unordered_map<std::string, T> ret;
129 ret.reserve([value count]);
130 for (NSString *key in value) {
139 template <
typename... Args>
142 dispatch_async(dispatch_get_main_queue(), ^{
143 NSObject *client = weak_client;
144 if (client && [client respondsToSelector:selector]) {
145 IMP imp = [client methodForSelector:selector];
146 auto func =
reinterpret_cast<void (*)(
id,
SEL, Args...)
>(imp);
147 func(client, selector, args...);
154 template <
typename Ret>
156 template <
typename Func>
167 template <
typename Func>
178 template <
typename This,
typename Ret,
typename Func>
182 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
187 dispatch_async(dispatch_get_main_queue(), ^{
201 #endif // SHAKA_EMBEDDED_UTIL_OBJC_UTILS_H_ static void Invoke(const AsyncResults< void > &future, Func block)
static NSNumber * ToObjc(optional< double > value)
const Error & error() const
static double ToObjc(double value)
static NSString * ToObjc(const std::string &value)
static std::unordered_map< std::string, T > FromObjc(NSDictionary< NSString *, dest_type > *value)
static NSString * ToObjc(optional< std::string > value)
static NSMutableArray< dest_type > * ToObjc(const std::vector< T > &value)
const T & value() const &
static BOOL ToObjc(bool value)
void DispatchObjcEvent(__weak id weak_client, SEL selector, Args... args)
const U & results() const
static std::string FromObjc(NSString *value)
void CallBlockForFuture(This that, AsyncResults< Ret > future, Func block)
decltype(ObjcConverter< T >::ToObjc(std::declval< T >())) dest_type
decltype(ObjcConverter< T >::ToObjc(std::declval< T >())) dest_type
static NSMutableDictionary< NSString *, dest_type > * ToObjc(const std::unordered_map< std::string, T > &map)
static std::vector< T > FromObjc(NSArray< dest_type > *value)
static void Invoke(const AsyncResults< Ret > &future, Func block)