6#include <QtCore/qstring.h>
7#include <QtCore/qurl.h>
8#include <QtCore/qdatetime.h>
9#include <QtCore/quuid.h>
10#include <QtCore/qbytearray.h>
11#include <QtCore/qrect.h>
13#if QT_CONFIG(timezone)
14#include <QtCore/qtimezone.h>
15#include <QtCore/private/qtimezoneprivate_p.h>
16#include <QtCore/private/qcore_mac_p.h>
19#import <CoreFoundation/CoreFoundation.h>
20#import <Foundation/Foundation.h>
22#if defined(QT_PLATFORM_UIKIT)
23#import <CoreGraphics/CoreGraphics.h>
29
30
31
32
33
34
35
36
37
38QByteArray QByteArray::fromCFData(CFDataRef data)
43 return QByteArray(
reinterpret_cast<
const char *>(CFDataGetBytePtr(data)), CFDataGetLength(data));
47
48
49
50
51
52
53
54
55
56
57
58
59QByteArray QByteArray::fromRawCFData(CFDataRef data)
64 return QByteArray::fromRawData(
reinterpret_cast<
const char *>(CFDataGetBytePtr(data)), CFDataGetLength(data));
68
69
70
71
72
73
74
75
76
77CFDataRef QByteArray::toCFData()
const
79 return CFDataCreate(kCFAllocatorDefault,
reinterpret_cast<
const UInt8 *>(data()), length());
83
84
85
86
87
88
89
90
91
92
93
94
95CFDataRef QByteArray::toRawCFData()
const
97 return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault,
reinterpret_cast<
const UInt8 *>(data()),
98 length(), kCFAllocatorNull);
102
103
104
105
106
107
108
109QByteArray QByteArray::fromNSData(
const NSData *data)
113 return QByteArray(
reinterpret_cast<
const char *>([data bytes]), [data length]);
117
118
119
120
121
122
123
124
125
126
127
128
129QByteArray QByteArray::fromRawNSData(
const NSData *data)
133 return QByteArray::fromRawData(
reinterpret_cast<
const char *>([data bytes]), [data length]);
137
138
139
140
141
142
143
144
145
146NSData *QByteArray::toNSData()
const
148 return [NSData dataWithBytes:constData() length:size()];
152
153
154
155
156
157
158
159
160
161
162
163
164NSData *QByteArray::toRawNSData()
const
167 return [NSData dataWithBytesNoCopy:
const_cast<
char *>(constData()) length:size() freeWhenDone:NO];
173
174
175
176
177
178
179
180QString QString::fromCFString(CFStringRef string)
184 CFIndex length = CFStringGetLength(string);
188 const UniChar *chars = CFStringGetCharactersPtr(string);
190 return QString(
reinterpret_cast<
const QChar *>(chars), length);
192 QString ret(length, Qt::Uninitialized);
193 CFStringGetCharacters(string, CFRangeMake(0, length),
reinterpret_cast<UniChar *>(ret.data()));
198
199
200
201
202
203
204
205
206
207CFStringRef QString::toCFString()
const
209 return QStringView{*
this}.toCFString();
213
214
215
216
217
218
219
220
221
222CFStringRef QStringView::toCFString()
const
224 return CFStringCreateWithCharacters(0,
reinterpret_cast<
const UniChar *>(data()), size());
228
229
230
231
232
233
234
235QString QString::fromNSString(
const NSString *string)
240 qstring.resize([string length]);
241 [string getCharacters:
reinterpret_cast<unichar*>(qstring.data()) range: NSMakeRange(0, [string length])];
246
247
248
249
250
251
252
253
254
255NSString *QString::toNSString()
const
257 return QStringView{*
this}.toNSString();
261
262
263
264
265
266
267
268
269
270NSString *QStringView::toNSString()
const
272 return [NSString stringWithCharacters:
reinterpret_cast<
const UniChar*>(data()) length:size()];
278
279
280
281
282
283
284
285QUuid QUuid::fromCFUUID(CFUUIDRef uuid)
289 const CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuid);
290 return QUuid::fromRfc4122(QByteArrayView(
reinterpret_cast<
const char *>(&bytes),
sizeof(bytes)));
294
295
296
297
298
299
300
301
302
303CFUUIDRef QUuid::toCFUUID()
const
305 const auto bytes = toBytes();
306 return CFUUIDCreateFromUUIDBytes(0, *
reinterpret_cast<
const CFUUIDBytes *>(&bytes));
310
311
312
313
314
315
316
317QUuid QUuid::fromNSUUID(
const NSUUID *uuid)
322 [uuid getUUIDBytes:bytes];
323 return QUuid::fromRfc4122(QByteArrayView(
reinterpret_cast<
const char *>(bytes),
sizeof(bytes)));
327
328
329
330
331
332
333
334
335
336NSUUID *QUuid::toNSUUID()
const
338 const auto bytes = toBytes();
339 static_assert(
sizeof bytes ==
sizeof(uuid_t));
341 memcpy(&u, &bytes,
sizeof(uuid_t));
342 return [[[NSUUID alloc] initWithUUIDBytes:u] autorelease];
349
350
351
352
353
354QUrl QUrl::fromCFURL(CFURLRef url)
358 return QUrl(QString::fromCFString(CFURLGetString(url)));
362
363
364
365
366
367
368
369CFURLRef QUrl::toCFURL()
const
372 CFStringRef str = toString(FullyEncoded).toCFString();
374 url = CFURLCreateWithString(0, str, 0);
381
382
383
384
385
386QUrl QUrl::fromNSURL(
const NSURL *url)
390 return QUrl(QString::fromNSString([url absoluteString]));
394
395
396
397
398
399
400
401NSURL *QUrl::toNSURL()
const
403 return [NSURL URLWithString:toString(FullyEncoded).toNSString()];
410
411
412
413
414
415
416
417QDateTime QDateTime::fromCFDate(CFDateRef date)
421 CFAbsoluteTime sSinceEpoch = kCFAbsoluteTimeIntervalSince1970 + CFDateGetAbsoluteTime(date);
422 return QDateTime::fromMSecsSinceEpoch(qRound64(sSinceEpoch * 1000));
426
427
428
429
430
431
432
433
434
435CFDateRef QDateTime::toCFDate()
const
437 return CFDateCreate(kCFAllocatorDefault, (
static_cast<CFAbsoluteTime>(toMSecsSinceEpoch())
438 / 1000) - kCFAbsoluteTimeIntervalSince1970);
442
443
444
445
446
447
448
449QDateTime QDateTime::fromNSDate(
const NSDate *date)
453 return QDateTime::fromMSecsSinceEpoch(qRound64([date timeIntervalSince1970] * 1000));
457
458
459
460
461
462
463
464
465
466NSDate *QDateTime::toNSDate()
const
469 dateWithTimeIntervalSince1970:
static_cast<NSTimeInterval>(toMSecsSinceEpoch()) / 1000];
474#if QT_CONFIG(timezone)
476
477
478
479
480
481
482
483QTimeZone QTimeZone::fromCFTimeZone(CFTimeZoneRef timeZone)
487 return QTimeZone(QString::fromCFString(CFTimeZoneGetName(timeZone)).toLatin1());
491
492
493
494
495
496
497
498
499
500CFTimeZoneRef QTimeZone::toCFTimeZone()
const
502#ifndef QT_NO_DYNAMIC_CAST
503 Q_ASSERT(
dynamic_cast<
const QMacTimeZonePrivate *>(d.d));
505 const QMacTimeZonePrivate *p =
static_cast<
const QMacTimeZonePrivate *>(d.d);
506 return reinterpret_cast<CFTimeZoneRef>([p->nsTimeZone() copy]);
510
511
512
513
514
515
516
517QTimeZone QTimeZone::fromNSTimeZone(
const NSTimeZone *timeZone)
521 return QTimeZone(QString::fromNSString(timeZone.name).toLatin1());
525
526
527
528
529
530
531
532
533
534NSTimeZone *QTimeZone::toNSTimeZone()
const
536 return [
static_cast<NSTimeZone *>(toCFTimeZone()) autorelease];
543
544
545
546
547
548
549
550CGRect QRect::toCGRect()
const noexcept
552 return CGRectMake(x(), y(), width(), height());
556
557
558
559
560
561
562
563CGRect QRectF::toCGRect()
const noexcept
565 return CGRectMake(x(), y(), width(), height());
569
570
571
572
573
574
575
576QRectF QRectF::fromCGRect(CGRect rect)
noexcept
578 return QRectF(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
584
585
586
587
588
589
590
591CGPoint QPoint::toCGPoint()
const noexcept
593 return CGPointMake(x(), y());
597
598
599
600
601
602
603
604CGPoint QPointF::toCGPoint()
const noexcept
606 return CGPointMake(x(), y());
610
611
612
613
614
615
616
617QPointF QPointF::fromCGPoint(CGPoint point)
noexcept
619 return QPointF(point.x, point.y);
625
626
627
628
629
630
631
632CGSize QSize::toCGSize()
const noexcept
634 return CGSizeMake(width(), height());
638
639
640
641
642
643
644
645CGSize QSizeF::toCGSize()
const noexcept
647 return CGSizeMake(width(), height());
651
652
653
654
655
656
657
658QSizeF QSizeF::fromCGSize(CGSize size)
noexcept
660 return QSizeF(size.width, size.height);