Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qcore_foundation.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2014 Samuel Gaist <samuel.gaist@edeltech.ch>
3// Copyright (C) 2014 Petroules Corporation.
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5
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>
12
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>
17#endif
18
19#import <CoreFoundation/CoreFoundation.h>
20#import <Foundation/Foundation.h>
21
22#if defined(QT_PLATFORM_UIKIT)
23#import <CoreGraphics/CoreGraphics.h>
24#endif
25
27
36QByteArray QByteArray::fromCFData(CFDataRef data)
37{
38 if (!data)
39 return QByteArray();
40
41 return QByteArray(reinterpret_cast<const char *>(CFDataGetBytePtr(data)), CFDataGetLength(data));
42}
43
57QByteArray QByteArray::fromRawCFData(CFDataRef data)
58{
59 if (!data)
60 return QByteArray();
61
62 return QByteArray::fromRawData(reinterpret_cast<const char *>(CFDataGetBytePtr(data)), CFDataGetLength(data));
63}
64
75CFDataRef QByteArray::toCFData() const
76{
77 return CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(data()), length());
78}
79
93CFDataRef QByteArray::toRawCFData() const
94{
95 return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(data()),
96 length(), kCFAllocatorNull);
97}
98
107QByteArray QByteArray::fromNSData(const NSData *data)
108{
109 if (!data)
110 return QByteArray();
111 return QByteArray(reinterpret_cast<const char *>([data bytes]), [data length]);
112}
113
127QByteArray QByteArray::fromRawNSData(const NSData *data)
128{
129 if (!data)
130 return QByteArray();
131 return QByteArray::fromRawData(reinterpret_cast<const char *>([data bytes]), [data length]);
132}
133
144NSData *QByteArray::toNSData() const
145{
146 return [NSData dataWithBytes:constData() length:size()];
147}
148
162NSData *QByteArray::toRawNSData() const
163{
164 // const_cast is fine here because NSData is immutable thus will never modify bytes we're giving it
165 return [NSData dataWithBytesNoCopy:const_cast<char *>(constData()) length:size() freeWhenDone:NO];
166}
167
168// ----------------------------------------------------------------------------
169
178QString QString::fromCFString(CFStringRef string)
179{
180 if (!string)
181 return QString();
182 CFIndex length = CFStringGetLength(string);
183
184 // Fast path: CFStringGetCharactersPtr does not copy but may
185 // return null for any and no reason.
186 const UniChar *chars = CFStringGetCharactersPtr(string);
187 if (chars)
188 return QString(reinterpret_cast<const QChar *>(chars), length);
189
191 CFStringGetCharacters(string, CFRangeMake(0, length), reinterpret_cast<UniChar *>(ret.data()));
192 return ret;
193}
194
205CFStringRef QString::toCFString() const
206{
207 return QStringView{*this}.toCFString();
208}
209
220CFStringRef QStringView::toCFString() const
221{
222 return CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(data()), size());
223}
224
233QString QString::fromNSString(const NSString *string)
234{
235 if (!string)
236 return QString();
238 qstring.resize([string length]);
239 [string getCharacters: reinterpret_cast<unichar*>(qstring.data()) range: NSMakeRange(0, [string length])];
240 return qstring;
241}
242
253NSString *QString::toNSString() const
254{
255 return QStringView{*this}.toNSString();
256}
257
268NSString *QStringView::toNSString() const
269{
270 return [NSString stringWithCharacters:reinterpret_cast<const UniChar*>(data()) length:size()];
271}
272
273// ----------------------------------------------------------------------------
274
283QUuid QUuid::fromCFUUID(CFUUIDRef uuid)
284{
285 if (!uuid)
286 return QUuid();
287 const CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuid);
288 return QUuid::fromRfc4122(QByteArrayView(reinterpret_cast<const char *>(&bytes), sizeof(bytes)));
289}
290
301CFUUIDRef QUuid::toCFUUID() const
302{
303 const QByteArray bytes = toRfc4122();
304 return CFUUIDCreateFromUUIDBytes(0, *reinterpret_cast<const CFUUIDBytes *>(bytes.constData()));
305}
306
315QUuid QUuid::fromNSUUID(const NSUUID *uuid)
316{
317 if (!uuid)
318 return QUuid();
319 uuid_t bytes;
320 [uuid getUUIDBytes:bytes];
321 return QUuid::fromRfc4122(QByteArrayView(reinterpret_cast<const char *>(bytes), sizeof(bytes)));
322}
323
334NSUUID *QUuid::toNSUUID() const
335{
336 const QByteArray bytes = toRfc4122();
337 return [[[NSUUID alloc] initWithUUIDBytes:*reinterpret_cast<const uuid_t *>(bytes.constData())] autorelease];
338}
339
340// ----------------------------------------------------------------------------
341
342
349QUrl QUrl::fromCFURL(CFURLRef url)
350{
351 if (!url)
352 return QUrl();
353 return QUrl(QString::fromCFString(CFURLGetString(url)));
354}
355
364CFURLRef QUrl::toCFURL() const
365{
366 CFURLRef url = 0;
367 CFStringRef str = toString(FullyEncoded).toCFString();
368 if (str) {
369 url = CFURLCreateWithString(0, str, 0);
370 CFRelease(str);
371 }
372 return url;
373}
374
381QUrl QUrl::fromNSURL(const NSURL *url)
382{
383 if (!url)
384 return QUrl();
385 return QUrl(QString::fromNSString([url absoluteString]));
386}
387
396NSURL *QUrl::toNSURL() const
397{
398 return [NSURL URLWithString:toString(FullyEncoded).toNSString()];
399}
400
401// ----------------------------------------------------------------------------
402
403
412QDateTime QDateTime::fromCFDate(CFDateRef date)
413{
414 if (!date)
415 return QDateTime();
416 CFAbsoluteTime sSinceEpoch = kCFAbsoluteTimeIntervalSince1970 + CFDateGetAbsoluteTime(date);
417 return QDateTime::fromMSecsSinceEpoch(qRound64(sSinceEpoch * 1000));
418}
419
430CFDateRef QDateTime::toCFDate() const
431{
432 return CFDateCreate(kCFAllocatorDefault, (static_cast<CFAbsoluteTime>(toMSecsSinceEpoch())
433 / 1000) - kCFAbsoluteTimeIntervalSince1970);
434}
435
444QDateTime QDateTime::fromNSDate(const NSDate *date)
445{
446 if (!date)
447 return QDateTime();
448 return QDateTime::fromMSecsSinceEpoch(qRound64([date timeIntervalSince1970] * 1000));
449}
450
461NSDate *QDateTime::toNSDate() const
462{
463 return [NSDate
464 dateWithTimeIntervalSince1970:static_cast<NSTimeInterval>(toMSecsSinceEpoch()) / 1000];
465}
466
467// ----------------------------------------------------------------------------
468
469#if QT_CONFIG(timezone)
478QTimeZone QTimeZone::fromCFTimeZone(CFTimeZoneRef timeZone)
479{
480 if (!timeZone)
481 return QTimeZone();
482 return QTimeZone(QString::fromCFString(CFTimeZoneGetName(timeZone)).toLatin1());
483}
484
495CFTimeZoneRef QTimeZone::toCFTimeZone() const
496{
497#ifndef QT_NO_DYNAMIC_CAST
498 Q_ASSERT(dynamic_cast<const QMacTimeZonePrivate *>(d.d));
499#endif
500 const QMacTimeZonePrivate *p = static_cast<const QMacTimeZonePrivate *>(d.d);
501 return reinterpret_cast<CFTimeZoneRef>([p->nsTimeZone() copy]);
502}
503
512QTimeZone QTimeZone::fromNSTimeZone(const NSTimeZone *timeZone)
513{
514 if (!timeZone)
515 return QTimeZone();
516 return QTimeZone(QString::fromNSString(timeZone.name).toLatin1());
517}
518
529NSTimeZone *QTimeZone::toNSTimeZone() const
530{
531 return [static_cast<NSTimeZone *>(toCFTimeZone()) autorelease];
532}
533#endif
534
535// ----------------------------------------------------------------------------
536
545CGRect QRect::toCGRect() const noexcept
546{
547 return CGRectMake(x(), y(), width(), height());
548}
549
558CGRect QRectF::toCGRect() const noexcept
559{
560 return CGRectMake(x(), y(), width(), height());
561}
562
571QRectF QRectF::fromCGRect(CGRect rect) noexcept
572{
573 return QRectF(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
574}
575
576// ----------------------------------------------------------------------------
577
586CGPoint QPoint::toCGPoint() const noexcept
587{
588 return CGPointMake(x(), y());
589}
590
599CGPoint QPointF::toCGPoint() const noexcept
600{
601 return CGPointMake(x(), y());
602}
603
612QPointF QPointF::fromCGPoint(CGPoint point) noexcept
613{
614 return QPointF(point.x, point.y);
615}
616
617// ----------------------------------------------------------------------------
618
627CGSize QSize::toCGSize() const noexcept
628{
629 return CGSizeMake(width(), height());
630}
631
640CGSize QSizeF::toCGSize() const noexcept
641{
642 return CGSizeMake(width(), height());
643}
644
653QSizeF QSizeF::fromCGSize(CGSize size) noexcept
654{
655 return QSizeF(size.width, size.height);
656}
657
\inmodule QtCore
Definition qbytearray.h:57
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
Definition qbytearray.h:611
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
qsizetype length() const noexcept
Same as size().
Definition qbytearray.h:499
friend class QString
Definition qbytearray.h:588
constexpr QByteArray() noexcept
Constructs an empty byte array.
Definition qbytearray.h:597
static QByteArray fromRawData(const char *data, qsizetype size)
Constructs a QByteArray that uses the first size bytes of the data array.
Definition qbytearray.h:409
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qdatetime.h:283
static QDateTime fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone)
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore
Definition qsize.h:208
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qtimezone.h:26
\inmodule QtCore
Definition qurl.h:94
\inmodule QtCore
Definition quuid.h:31
static QUuid fromRfc4122(QByteArrayView) noexcept
Creates a QUuid object from the binary representation of the UUID, as specified by RFC 4122 section 4...
Definition quuid.cpp:595
QString str
[2]
QDate date
[1]
rect
[4]
Combined button and popup list for selecting options.
constexpr Initialization Uninitialized
_string< true > qstring
Definition language.h:111
static jboolean copy(JNIEnv *, jobject)
qint64 qRound64(qfloat16 d) noexcept
Definition qfloat16.h:330
return ret
GLint GLint GLint GLint GLint x
[0]
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint GLenum GLsizei length
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLsizei range
GLint GLsizei width
GLint y
GLfloat GLfloat p
[1]
GLsizei const GLchar *const * string
[0]
Definition qopenglext.h:694
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QUrl url("example.com")
[constructor-url-reference]
char * toString(const MyType &t)
[31]