aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objc/utils
diff options
context:
space:
mode:
authorGravatar DINH Viet Hoa <dinh.viet.hoa@gmail.com>2013-03-22 18:00:46 -0700
committerGravatar DINH Viet Hoa <dinh.viet.hoa@gmail.com>2013-03-22 18:00:46 -0700
commit37265d9a5b2da31f522cc4f3d26ccc6bb5199b10 (patch)
tree12e5516c5a6a36f679a1d99c99f71953e34635a5 /src/objc/utils
parent7b4eafc8149b151b63a111bbcc6f3a7f322c552d (diff)
Implemented Objective-C wrapper for core/abstract
Diffstat (limited to 'src/objc/utils')
-rw-r--r--src/objc/utils/NSArray+MCO.h2
-rw-r--r--src/objc/utils/NSArray+MCO.mm9
-rw-r--r--src/objc/utils/NSDictionary+MCO.h2
-rw-r--r--src/objc/utils/NSDictionary+MCO.mm14
-rw-r--r--src/objc/utils/NSObject+MCO.h11
-rw-r--r--src/objc/utils/NSObject+MCO.mm73
-rw-r--r--src/objc/utils/NSValue+MCO.h25
-rw-r--r--src/objc/utils/NSValue+MCO.mm100
8 files changed, 233 insertions, 3 deletions
diff --git a/src/objc/utils/NSArray+MCO.h b/src/objc/utils/NSArray+MCO.h
index 8d95acbc..769da85d 100644
--- a/src/objc/utils/NSArray+MCO.h
+++ b/src/objc/utils/NSArray+MCO.h
@@ -17,6 +17,8 @@ namespace mailcore {
@interface NSArray (MCO)
#ifdef __cplusplus
++ (NSArray *) mco_arrayWithMCArray:(mailcore::Array *)array;
+
- (mailcore::Array *) mco_mcArray;
#endif
diff --git a/src/objc/utils/NSArray+MCO.mm b/src/objc/utils/NSArray+MCO.mm
index 1c4ef3e6..4b9eaab7 100644
--- a/src/objc/utils/NSArray+MCO.mm
+++ b/src/objc/utils/NSArray+MCO.mm
@@ -14,6 +14,15 @@
@implementation NSArray (MCO)
++ (NSArray *) mco_arrayWithMCArray:(mailcore::Array *)array
+{
+ NSMutableArray * result = [NSMutableArray array];
+ for(unsigned int i = 0 ; i < array->count() ; i ++) {
+ [result addObject:[NSObject mco_objectWithMCObject:array->objectAtIndex(i)]];
+ }
+ return result;
+}
+
- (mailcore::Array *) mco_mcArray
{
mailcore::Array * result = mailcore::Array::array();
diff --git a/src/objc/utils/NSDictionary+MCO.h b/src/objc/utils/NSDictionary+MCO.h
index 71812a46..8e78a872 100644
--- a/src/objc/utils/NSDictionary+MCO.h
+++ b/src/objc/utils/NSDictionary+MCO.h
@@ -17,6 +17,8 @@ namespace mailcore {
@interface NSDictionary (MCO)
#ifdef __cplusplus
++ (NSDictionary *) mco_dictionaryWithMCHashMap:(mailcore::HashMap *)hashmap;
+
- (mailcore::HashMap *) mco_mcHashMap;
#endif
diff --git a/src/objc/utils/NSDictionary+MCO.mm b/src/objc/utils/NSDictionary+MCO.mm
index a751f85b..8ba2f7a5 100644
--- a/src/objc/utils/NSDictionary+MCO.mm
+++ b/src/objc/utils/NSDictionary+MCO.mm
@@ -14,6 +14,20 @@
@implementation NSDictionary (MCO)
++ (NSDictionary *) mco_dictionaryWithMCHashMap:(mailcore::HashMap *)hashmap
+{
+ NSMutableDictionary * result = [NSMutableDictionary dictionary];
+ mailcore::Array * keys = hashmap->allKeys();
+ for(unsigned int i = 0 ; i < keys->count() ; i ++) {
+ mailcore::Object * mcKey = keys->objectAtIndex(i);
+ mailcore::Object * mcValue = hashmap->objectForKey(mcKey);
+ id key = [NSObject mco_objectWithMCObject:mcKey];
+ id value = [NSObject mco_objectWithMCObject:mcValue];
+ [result setObject:value forKey:key];
+ }
+ return result;
+}
+
- (mailcore::HashMap *) mco_mcHashMap
{
mailcore::HashMap * result = mailcore::HashMap::hashMap();
diff --git a/src/objc/utils/NSObject+MCO.h b/src/objc/utils/NSObject+MCO.h
index 349bd27a..6cc24884 100644
--- a/src/objc/utils/NSObject+MCO.h
+++ b/src/objc/utils/NSObject+MCO.h
@@ -9,6 +9,10 @@
#import <Foundation/Foundation.h>
#ifdef __cplusplus
+#include <typeinfo>
+#endif
+
+#ifdef __cplusplus
namespace mailcore {
class Object;
}
@@ -17,7 +21,14 @@ namespace mailcore {
@interface NSObject (MCO)
#ifdef __cplusplus
++ (NSObject *) mco_objectWithMCObject:(mailcore::Object *)object;
+
- (mailcore::Object *) mco_mcObject;
#endif
@end
+
+#ifdef __cplusplus
+extern void MCORegisterClass(Class aClass, const std::type_info * info);
+#endif
+
diff --git a/src/objc/utils/NSObject+MCO.mm b/src/objc/utils/NSObject+MCO.mm
index e25db319..52a1bc43 100644
--- a/src/objc/utils/NSObject+MCO.mm
+++ b/src/objc/utils/NSObject+MCO.mm
@@ -8,19 +8,84 @@
#import "NSObject+MCO.h"
+#include <typeinfo>
+
#import "NSData+MCO.h"
#import "NSString+MCO.h"
#import "NSDictionary+MCO.h"
#import "NSArray+MCO.h"
+#import "NSValue+MCO.h"
#include "MCBaseTypes.h"
#include "MCUtils.h"
+static chash * classHash = NULL;
+
+static void init(void)
+{
+ static dispatch_once_t onceToken = 0;
+ dispatch_once(&onceToken, ^{
+ classHash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
+ });
+}
+
+void MCORegisterClass(Class aClass, const std::type_info * info)
+{
+ chashdatum key;
+ chashdatum value;
+ key.data = &info;
+ key.len = sizeof(info);
+ value.data = aClass;
+ value.len = 0;
+ chash_set(classHash, &key, &value, NULL);
+}
+
+static Class classWithTypeInfo(const std::type_info * info)
+{
+ int r;
+ chashdatum key;
+ chashdatum value;
+ key.data = &info;
+ key.len = sizeof(info);
+ r = chash_get(classHash, &key, &value);
+ if (r < 0)
+ return nil;
+
+ return (Class) value.data;
+}
+
@implementation NSObject (MCO)
++ (NSObject *) mco_objectWithMCObject:(mailcore::Object *)object
+{
+ if (&typeid(object) == &typeid(mailcore::Value *)) {
+ return [NSValue mco_valueWithMCValue:(mailcore::Value *) object];
+ }
+ else if (&typeid(object) == &typeid(mailcore::Data *)) {
+ return [NSData mco_dataWithMCData:(mailcore::Data *) object];
+ }
+ else if (&typeid(object) == &typeid(mailcore::String *)) {
+ return [NSString mco_stringWithMCString:(mailcore::String *) object];
+ }
+ else if (&typeid(object) == &typeid(mailcore::HashMap *)) {
+ return [NSDictionary mco_dictionaryWithMCHashMap:(mailcore::HashMap *) object];
+ }
+ else if (&typeid(object) == &typeid(mailcore::Array *)) {
+ return [NSArray mco_arrayWithMCArray:(mailcore::Array *) object];
+ }
+ else {
+ Class aClass = classWithTypeInfo(&typeid(* object));
+ MCAssert(aClass != nil);
+ return (NSObject *) [aClass mco_objectWithMCObject:object];
+ }
+}
+
- (mailcore::Object *) mco_mcObject
{
- if ([self isKindOfClass:[NSData class]]) {
+ if ([self isKindOfClass:[NSValue class]]) {
+ return [(NSValue *) self mco_mcValue];
+ }
+ else if ([self isKindOfClass:[NSData class]]) {
return [(NSData *) self mco_mcData];
}
else if ([self isKindOfClass:[NSString class]]) {
@@ -33,8 +98,10 @@
return [(NSDictionary *) self mco_mcHashMap];
}
else {
- MCAssert(0);
- return nil;
+ if (![self respondsToSelector:@selector(mco_mcObject)]) {
+ MCAssert(0);
+ }
+ return [self mco_mcObject];
}
}
diff --git a/src/objc/utils/NSValue+MCO.h b/src/objc/utils/NSValue+MCO.h
new file mode 100644
index 00000000..bee3c742
--- /dev/null
+++ b/src/objc/utils/NSValue+MCO.h
@@ -0,0 +1,25 @@
+//
+// NSNumber+MCO.h
+// mailcore2
+//
+// Created by DINH Viêt Hoà on 3/21/13.
+// Copyright (c) 2013 MailCore. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#ifdef __cplusplus
+namespace mailcore {
+ class Value;
+}
+#endif
+
+@interface NSValue (MCO)
+
+#ifdef __cplusplus
++ (NSValue *) mco_valueWithMCValue:(mailcore::Value *)value;
+
+- (mailcore::Value *) mco_mcValue;
+#endif
+
+@end
diff --git a/src/objc/utils/NSValue+MCO.mm b/src/objc/utils/NSValue+MCO.mm
new file mode 100644
index 00000000..81bcebd7
--- /dev/null
+++ b/src/objc/utils/NSValue+MCO.mm
@@ -0,0 +1,100 @@
+//
+// NSNumber+MCO.m
+// mailcore2
+//
+// Created by DINH Viêt Hoà on 3/21/13.
+// Copyright (c) 2013 MailCore. All rights reserved.
+//
+
+#import "NSValue+MCO.h"
+
+#include "MCAssert.h"
+#include "MCValue.h"
+#include "MCValuePrivate.h"
+
+@implementation NSValue (MCO)
+
++ (NSNumber *) mco_valueWithMCValue:(mailcore::Value *)value
+{
+ switch (value->type()) {
+ case VALUE_TYPE_BOOL_VALUE:
+ return [NSNumber numberWithBool:value->boolValue()];
+ case VALUE_TYPE_CHAR_VALUE:
+ return [NSNumber numberWithChar:value->charValue()];
+ case VALUE_TYPE_UNSIGNED_CHAR_VALUE:
+ return [NSNumber numberWithUnsignedChar:value->unsignedCharValue()];
+ case VALUE_TYPE_SHORT_VALUE:
+ return [NSNumber numberWithShort:value->shortValue()];
+ case VALUE_TYPE_UNSIGNED_SHORT_VALUE:
+ return [NSNumber numberWithUnsignedChar:value->unsignedShortValue()];
+ case VALUE_TYPE_INT_VALUE:
+ return [NSNumber numberWithInt:value->intValue()];
+ case VALUE_TYPE_UNSIGNED_INT_VALUE:
+ return [NSNumber numberWithUnsignedInt:value->unsignedIntValue()];
+ case VALUE_TYPE_LONG_VALUE:
+ return [NSNumber numberWithLong:value->longValue()];
+ case VALUE_TYPE_UNSIGNED_LONG_VALUE:
+ return [NSNumber numberWithUnsignedLong:value->unsignedLongValue()];
+ case VALUE_TYPE_LONG_LONG_VALUE:
+ return [NSNumber numberWithLongLong:value->longLongValue()];
+ case VALUE_TYPE_UNSIGNED_LONG_LONG_VALUE:
+ return [NSNumber numberWithUnsignedLongLong:value->unsignedLongLongValue()];
+ case VALUE_TYPE_FLOAT_VALUE:
+ return [NSNumber numberWithFloat:value->floatValue()];
+ case VALUE_TYPE_DOUBLE_VALUE:
+ return [NSNumber numberWithDouble:value->doubleValue()];
+ case VALUE_TYPE_POINTER_VALUE:
+ MCAssert(0);
+ return nil;
+ case VALUE_TYPE_DATA_VALUE:
+ MCAssert(0);
+ return nil;
+ default:
+ MCAssert(0);
+ return nil;
+ }
+}
+
+- (mailcore::Value *) mco_mcValue;
+{
+ NSNumber * nb = (NSNumber *) self;
+ if (strcmp([self objCType], @encode(BOOL)) == 0) {
+ return mailcore::Value::valueWithBoolValue([nb boolValue]);
+ }
+ else if (strcmp([self objCType], @encode(char)) == 0) {
+ return mailcore::Value::valueWithCharValue([nb charValue]);
+ }
+ else if (strcmp([self objCType], @encode(unsigned char)) == 0) {
+ return mailcore::Value::valueWithUnsignedCharValue([nb unsignedCharValue]);
+ }
+ else if (strcmp([self objCType], @encode(int)) == 0) {
+ return mailcore::Value::valueWithIntValue([nb intValue]);
+ }
+ else if (strcmp([self objCType], @encode(unsigned int)) == 0) {
+ return mailcore::Value::valueWithUnsignedIntValue([nb unsignedIntValue]);
+ }
+ else if (strcmp([self objCType], @encode(long)) == 0) {
+ return mailcore::Value::valueWithLongValue([nb longValue]);
+ }
+ else if (strcmp([self objCType], @encode(unsigned long)) == 0) {
+ return mailcore::Value::valueWithUnsignedLongValue([nb unsignedLongValue]);
+ }
+ else if (strcmp([self objCType], @encode(long long)) == 0) {
+ return mailcore::Value::valueWithLongLongValue([nb longLongValue]);
+ }
+ else if (strcmp([self objCType], @encode(unsigned long long)) == 0) {
+ return mailcore::Value::valueWithUnsignedLongLongValue([nb unsignedLongLongValue]);
+ }
+ else if (strcmp([self objCType], @encode(float)) == 0) {
+ return mailcore::Value::valueWithFloatValue([nb floatValue]);
+ }
+ else if (strcmp([self objCType], @encode(double)) == 0) {
+ return mailcore::Value::valueWithDoubleValue([nb doubleValue]);
+ }
+ else {
+ MCAssert(0);
+ return NULL;
+ }
+}
+
+@end