From daa49494d417ecc4c9b17423bcb9bb9a11f51c26 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Sun, 9 Apr 2017 14:19:53 +0200 Subject: [PATCH] Add OFString (QString) --- ObjQt.pro | 4 +++- common/OFString+QString.h | 9 +++++++++ common/OFString+QString.mm | 30 ++++++++++++++++++++++++++++++ common/helpers.h | 6 ++++-- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 common/OFString+QString.h create mode 100644 common/OFString+QString.mm diff --git a/ObjQt.pro b/ObjQt.pro index 7907648..fa7a4e6 100644 --- a/ObjQt.pro +++ b/ObjQt.pro @@ -10,6 +10,7 @@ INCLUDEPATH += common \ QtWidgets HEADERS += common/helpers.h \ + common/OFString+QString.h \ common/QtOwnershipManaging.h \ QtCore/QtChildEvent.h \ QtCore/QtCoreApplication.h \ @@ -21,7 +22,8 @@ HEADERS += common/helpers.h \ QtWidgets/QtApplication.h \ QtWidgets/QtWidget.h -SOURCES += QtCore/QtChildEvent.mm \ +SOURCES += common/OFString+QString.mm \ + QtCore/QtChildEvent.mm \ QtCore/QtCoreApplication.mm \ QtCore/QtEvent.mm \ QtCore/QtObject.mm \ diff --git a/common/OFString+QString.h b/common/OFString+QString.h new file mode 100644 index 0000000..ac4fdb4 --- /dev/null +++ b/common/OFString+QString.h @@ -0,0 +1,9 @@ +#import + +#include + +@interface OFString (QString) ++ (instancetype)stringWithQString: (const QString&)qString; +- initWithQString: (const QString&)qString; +- (QString)qString; +@end diff --git a/common/OFString+QString.mm b/common/OFString+QString.mm new file mode 100644 index 0000000..7d34eb6 --- /dev/null +++ b/common/OFString+QString.mm @@ -0,0 +1,30 @@ +#import "OFString+QString.h" + +@implementation OFString (QString) ++ stringWithQString: (const QString&)qString +{ + return [[[self alloc] initWithQString: qString] autorelease]; +} + +- initWithQString: (const QString&)qString +{ + static_assert(sizeof(QChar) == sizeof(of_char16_t), + "QChar and of_char16_t have a different size!"); + + return [self initWithUTF16String: (of_char16_t*)qString.data() + length: qString.length()]; +} + +- (QString)qString +{ + static_assert(sizeof(of_char16_t) == sizeof(QChar), + "of_char16_t and QChar have a different size!"); + + void *pool = objc_autoreleasePoolPush(); + QString ret = QString((QChar*)[self UTF16String]); + + objc_autoreleasePoolPop(pool); + + return ret; +} +@end diff --git a/common/helpers.h b/common/helpers.h index cf95511..fe43dbb 100644 --- a/common/helpers.h +++ b/common/helpers.h @@ -4,16 +4,18 @@ #include #include +#import "OFString+QString.h" + static OF_INLINE OFString* toOF(const QString &qString) { - return [OFString stringWithUTF8String: qString.toUtf8()]; + return [OFString stringWithQString: qString]; } static OF_INLINE QString toQt(OFString *string) { - return QString::fromUtf8([string UTF8String]); + return [string qString]; } static OF_INLINE of_point_t