summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/Qt6CoreMacros.cmake89
-rw-r--r--src/corelib/animation/qabstractanimation.cpp8
-rw-r--r--src/corelib/animation/qanimationgroup.cpp8
-rw-r--r--src/corelib/animation/qpropertyanimation.cpp2
-rw-r--r--src/corelib/animation/qsequentialanimationgroup.cpp6
-rw-r--r--src/corelib/animation/qvariantanimation.cpp6
-rw-r--r--src/corelib/codecs/qgb18030codec.cpp1
-rw-r--r--src/corelib/codecs/qicucodec.cpp16
-rw-r--r--src/corelib/codecs/qisciicodec.cpp2
-rw-r--r--src/corelib/codecs/qsimplecodec.cpp66
-rw-r--r--src/corelib/configure.json12
-rw-r--r--src/corelib/corelib.pro3
-rw-r--r--src/corelib/doc/qtcore.qdocconf2
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp1
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp16
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp15
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp16
-rw-r--r--src/corelib/doc/src/dontdocument.qdoc4
-rw-r--r--src/corelib/doc/src/qtcore-index.qdoc2
-rw-r--r--src/corelib/doc/src/resource-system.qdoc4
-rw-r--r--src/corelib/global/qcompilerdetection.h98
-rw-r--r--src/corelib/global/qconfig-bootstrapped.h7
-rw-r--r--src/corelib/global/qendian.cpp7
-rw-r--r--src/corelib/global/qendian.h4
-rw-r--r--src/corelib/global/qflags.h7
-rw-r--r--src/corelib/global/qfloat16.cpp16
-rw-r--r--src/corelib/global/qfloat16.h14
-rw-r--r--src/corelib/global/qglobal.cpp66
-rw-r--r--src/corelib/global/qglobal.h14
-rw-r--r--src/corelib/global/qglobal_p.h4
-rw-r--r--src/corelib/global/qlibraryinfo.cpp52
-rw-r--r--src/corelib/global/qlogging.cpp10
-rw-r--r--src/corelib/global/qnamespace.h201
-rw-r--r--src/corelib/global/qnamespace.qdoc22
-rw-r--r--src/corelib/global/qnumeric_p.h3
-rw-r--r--src/corelib/global/qoperatingsystemversion.cpp2
-rw-r--r--src/corelib/global/qrandom.cpp2
-rw-r--r--src/corelib/io/io.pri1
-rw-r--r--src/corelib/io/qabstractfileengine.cpp2
-rw-r--r--src/corelib/io/qdir.cpp5
-rw-r--r--src/corelib/io/qdiriterator.cpp2
-rw-r--r--src/corelib/io/qfile.cpp4
-rw-r--r--src/corelib/io/qfile.h10
-rw-r--r--src/corelib/io/qfiledevice.cpp2
-rw-r--r--src/corelib/io/qfileinfo.cpp21
-rw-r--r--src/corelib/io/qfileinfo.h1
-rw-r--r--src/corelib/io/qfilesystemengine_unix.cpp6
-rw-r--r--src/corelib/io/qfilesystemmetadata_p.h21
-rw-r--r--src/corelib/io/qfilesystemwatcher.cpp2
-rw-r--r--src/corelib/io/qfilesystemwatcher_inotify.cpp2
-rw-r--r--src/corelib/io/qfilesystemwatcher_win.cpp1
-rw-r--r--src/corelib/io/qfsfileengine_win.cpp4
-rw-r--r--src/corelib/io/qiodevice.cpp2
-rw-r--r--src/corelib/io/qloggingcategory.cpp2
-rw-r--r--src/corelib/io/qnoncontiguousbytedevice.cpp20
-rw-r--r--src/corelib/io/qprocess.cpp32
-rw-r--r--src/corelib/io/qprocess.h3
-rw-r--r--src/corelib/io/qprocess_unix.cpp37
-rw-r--r--src/corelib/io/qprocess_win.cpp15
-rw-r--r--src/corelib/io/qresource.cpp230
-rw-r--r--src/corelib/io/qresource.h5
-rw-r--r--src/corelib/io/qsavefile.cpp2
-rw-r--r--src/corelib/io/qsettings.cpp27
-rw-r--r--src/corelib/io/qsettings_p.h13
-rw-r--r--src/corelib/io/qsettings_wasm.cpp259
-rw-r--r--src/corelib/io/qstandardpaths.cpp68
-rw-r--r--src/corelib/io/qstorageinfo_unix.cpp2
-rw-r--r--src/corelib/io/qtemporaryfile.cpp6
-rw-r--r--src/corelib/io/qurl.cpp2
-rw-r--r--src/corelib/itemmodels/qabstractitemmodel.cpp52
-rw-r--r--src/corelib/itemmodels/qabstractproxymodel.cpp2
-rw-r--r--src/corelib/itemmodels/qconcatenatetablesproxymodel.cpp2
-rw-r--r--src/corelib/itemmodels/qidentityproxymodel.cpp24
-rw-r--r--src/corelib/itemmodels/qidentityproxymodel.h2
-rw-r--r--src/corelib/itemmodels/qitemselectionmodel.cpp19
-rw-r--r--src/corelib/itemmodels/qsortfilterproxymodel.cpp6
-rw-r--r--src/corelib/itemmodels/qsortfilterproxymodel.h22
-rw-r--r--src/corelib/itemmodels/qstringlistmodel.cpp28
-rw-r--r--src/corelib/kernel/qbasictimer.cpp10
-rw-r--r--src/corelib/kernel/qcoreapplication.cpp157
-rw-r--r--src/corelib/kernel/qcoreapplication_p.h10
-rw-r--r--src/corelib/kernel/qcoreapplication_win.cpp2
-rw-r--r--src/corelib/kernel/qcoreevent.cpp2
-rw-r--r--src/corelib/kernel/qdeadlinetimer.h4
-rw-r--r--src/corelib/kernel/qeventdispatcher_glib.cpp40
-rw-r--r--src/corelib/kernel/qeventdispatcher_unix.cpp6
-rw-r--r--src/corelib/kernel/qeventdispatcher_unix_p.h2
-rw-r--r--src/corelib/kernel/qeventdispatcher_win.cpp38
-rw-r--r--src/corelib/kernel/qeventloop.cpp43
-rw-r--r--src/corelib/kernel/qmetaobject.cpp116
-rw-r--r--src/corelib/kernel/qmetaobjectbuilder.cpp54
-rw-r--r--src/corelib/kernel/qmetatype.cpp54
-rw-r--r--src/corelib/kernel/qmetatype.h26
-rw-r--r--src/corelib/kernel/qmimedata.cpp2
-rw-r--r--src/corelib/kernel/qobject.cpp253
-rw-r--r--src/corelib/kernel/qobject.h10
-rw-r--r--src/corelib/kernel/qobject_p.h8
-rw-r--r--src/corelib/kernel/qobjectcleanuphandler.cpp2
-rw-r--r--src/corelib/kernel/qobjectdefs.h188
-rw-r--r--src/corelib/kernel/qsharedmemory.cpp2
-rw-r--r--src/corelib/kernel/qsharedmemory_systemv.cpp6
-rw-r--r--src/corelib/kernel/qsharedmemory_unix.cpp2
-rw-r--r--src/corelib/kernel/qsocketnotifier.cpp15
-rw-r--r--src/corelib/kernel/qtestsupport_core.cpp2
-rw-r--r--src/corelib/kernel/qtimer.cpp14
-rw-r--r--src/corelib/kernel/qtimer.h6
-rw-r--r--src/corelib/kernel/qtimerinfo_unix.cpp20
-rw-r--r--src/corelib/kernel/qtmetamacros.h234
-rw-r--r--src/corelib/kernel/qtranslator.cpp38
-rw-r--r--src/corelib/kernel/qvariant.cpp166
-rw-r--r--src/corelib/kernel/qvariant.h38
-rw-r--r--src/corelib/kernel/qvariant_p.h5
-rw-r--r--src/corelib/kernel/qwineventnotifier.cpp6
-rw-r--r--src/corelib/mimetypes/mime/generate.bat73
-rw-r--r--src/corelib/mimetypes/mime/generate.pl113
-rw-r--r--src/corelib/mimetypes/mime/hexdump.ps143
-rw-r--r--src/corelib/mimetypes/mimetypes.pri29
-rw-r--r--src/corelib/mimetypes/qmimedatabase.cpp13
-rw-r--r--src/corelib/mimetypes/qmimeprovider.cpp99
-rw-r--r--src/corelib/mimetypes/qmimeprovider_p.h11
-rw-r--r--src/corelib/plugin/qfactoryloader.cpp8
-rw-r--r--src/corelib/plugin/qlibrary.cpp34
-rw-r--r--src/corelib/plugin/qlibrary_p.h2
-rw-r--r--src/corelib/plugin/qlibrary_unix.cpp2
-rw-r--r--src/corelib/plugin/qpluginloader.cpp9
-rw-r--r--src/corelib/serialization/qbinaryjsonvalue.cpp18
-rw-r--r--src/corelib/serialization/qbinaryjsonvalue_p.h4
-rw-r--r--src/corelib/serialization/qcborarray.h2
-rw-r--r--src/corelib/serialization/qcborcommon.cpp328
-rw-r--r--src/corelib/serialization/qcborcommon.h2
-rw-r--r--src/corelib/serialization/qcborcommon_p.h84
-rw-r--r--src/corelib/serialization/qcbormap.h2
-rw-r--r--src/corelib/serialization/qcborstream.h226
-rw-r--r--src/corelib/serialization/qcborstreamreader.cpp (renamed from src/corelib/serialization/qcborstream.cpp)1142
-rw-r--r--src/corelib/serialization/qcborstreamreader.h210
-rw-r--r--src/corelib/serialization/qcborstreamwriter.cpp868
-rw-r--r--src/corelib/serialization/qcborstreamwriter.h130
-rw-r--r--src/corelib/serialization/qcborvalue.cpp195
-rw-r--r--src/corelib/serialization/qcborvalue.h18
-rw-r--r--src/corelib/serialization/qcborvalue_p.h12
-rw-r--r--src/corelib/serialization/qdatastream.h20
-rw-r--r--src/corelib/serialization/qjsonarray.cpp12
-rw-r--r--src/corelib/serialization/qjsoncbor.cpp8
-rw-r--r--src/corelib/serialization/qjsondocument.cpp7
-rw-r--r--src/corelib/serialization/qjsondocument.h10
-rw-r--r--src/corelib/serialization/qjsonobject.cpp4
-rw-r--r--src/corelib/serialization/qjsonobject.h2
-rw-r--r--src/corelib/serialization/qjsonparser.cpp7
-rw-r--r--src/corelib/serialization/qjsonvalue.cpp12
-rw-r--r--src/corelib/serialization/qjsonwriter.cpp3
-rw-r--r--src/corelib/serialization/qtextstream.cpp52
-rw-r--r--src/corelib/serialization/qtextstream.h42
-rw-r--r--src/corelib/serialization/serialization.pri17
-rw-r--r--src/corelib/statemachine/qabstractstate.cpp6
-rw-r--r--src/corelib/statemachine/qabstracttransition.cpp4
-rw-r--r--src/corelib/statemachine/qeventtransition.cpp2
-rw-r--r--src/corelib/statemachine/qhistorystate.cpp2
-rw-r--r--src/corelib/statemachine/qsignaltransition.cpp2
-rw-r--r--src/corelib/statemachine/qstate.cpp20
-rw-r--r--src/corelib/statemachine/qstatemachine.cpp102
-rw-r--r--src/corelib/text/qbytearray.cpp791
-rw-r--r--src/corelib/text/qbytearray.h290
-rw-r--r--src/corelib/text/qbytearray_p.h2
-rw-r--r--src/corelib/text/qbytearraylist.h3
-rw-r--r--src/corelib/text/qchar.cpp258
-rw-r--r--src/corelib/text/qchar.h3
-rw-r--r--src/corelib/text/qlocale.cpp128
-rw-r--r--src/corelib/text/qlocale.h6
-rw-r--r--src/corelib/text/qlocale.qdoc10
-rw-r--r--src/corelib/text/qlocale_mac.mm14
-rw-r--r--src/corelib/text/qlocale_p.h3
-rw-r--r--src/corelib/text/qlocale_tools.cpp2
-rw-r--r--src/corelib/text/qlocale_unix.cpp4
-rw-r--r--src/corelib/text/qlocale_win.cpp97
-rw-r--r--src/corelib/text/qregexp.cpp8
-rw-r--r--src/corelib/text/qregularexpression.cpp169
-rw-r--r--src/corelib/text/qregularexpression.h20
-rw-r--r--src/corelib/text/qstring.cpp554
-rw-r--r--src/corelib/text/qstring.h267
-rw-r--r--src/corelib/text/qstringalgorithms.h1
-rw-r--r--src/corelib/text/qstringbuilder.cpp4
-rw-r--r--src/corelib/text/qstringbuilder.h10
-rw-r--r--src/corelib/text/qstringliteral.h44
-rw-r--r--src/corelib/text/qstringview.cpp15
-rw-r--r--src/corelib/text/qstringview.h2
-rw-r--r--src/corelib/text/qtextboundaryfinder.cpp16
-rw-r--r--src/corelib/text/qunicodetables_p.h8
-rw-r--r--src/corelib/thread/qatomic.cpp204
-rw-r--r--src/corelib/thread/qatomic.h6
-rw-r--r--src/corelib/thread/qbasicatomic.h6
-rw-r--r--src/corelib/thread/qexception.cpp2
-rw-r--r--src/corelib/thread/qfutex_p.h4
-rw-r--r--src/corelib/thread/qfutureinterface.cpp2
-rw-r--r--src/corelib/thread/qmutex.cpp6
-rw-r--r--src/corelib/thread/qmutex.h10
-rw-r--r--src/corelib/thread/qmutex_linux.cpp4
-rw-r--r--src/corelib/thread/qorderedmutexlocker_p.h2
-rw-r--r--src/corelib/thread/qreadwritelock.cpp10
-rw-r--r--src/corelib/thread/qresultstore.cpp2
-rw-r--r--src/corelib/thread/qthread.cpp36
-rw-r--r--src/corelib/thread/qthread.h8
-rw-r--r--src/corelib/thread/qthread_unix.cpp28
-rw-r--r--src/corelib/thread/qthread_win.cpp6
-rw-r--r--src/corelib/thread/qthreadpool.cpp2
-rw-r--r--src/corelib/thread/qthreadstorage.cpp12
-rw-r--r--src/corelib/thread/qwaitcondition.h34
-rw-r--r--src/corelib/thread/qwaitcondition.qdoc32
-rw-r--r--src/corelib/thread/qwaitcondition_unix.cpp2
-rw-r--r--src/corelib/time/qcalendar.cpp6
-rw-r--r--src/corelib/time/qdatetime.cpp415
-rw-r--r--src/corelib/time/qdatetimeparser.cpp49
-rw-r--r--src/corelib/time/qdatetimeparser_p.h2
-rw-r--r--src/corelib/time/qtimezone.cpp4
-rw-r--r--src/corelib/time/qtimezoneprivate.cpp19
-rw-r--r--src/corelib/time/qtimezoneprivate_android.cpp52
-rw-r--r--src/corelib/time/qtimezoneprivate_icu.cpp12
-rw-r--r--src/corelib/time/qtimezoneprivate_mac.mm47
-rw-r--r--src/corelib/time/qtimezoneprivate_tz.cpp2
-rw-r--r--src/corelib/tools/qalgorithms.h4
-rw-r--r--src/corelib/tools/qarraydata.cpp95
-rw-r--r--src/corelib/tools/qarraydata.h224
-rw-r--r--src/corelib/tools/qarraydataops.h413
-rw-r--r--src/corelib/tools/qarraydatapointer.h154
-rw-r--r--src/corelib/tools/qbitarray.cpp32
-rw-r--r--src/corelib/tools/qbitarray.h4
-rw-r--r--src/corelib/tools/qeasingcurve.cpp20
-rw-r--r--src/corelib/tools/qflatmap_p.h982
-rw-r--r--src/corelib/tools/qhash.cpp114
-rw-r--r--src/corelib/tools/qhash.h479
-rw-r--r--src/corelib/tools/qline.cpp2
-rw-r--r--src/corelib/tools/qmap.cpp107
-rw-r--r--src/corelib/tools/qmap.h375
-rw-r--r--src/corelib/tools/qpoint.h1
-rw-r--r--src/corelib/tools/qrect.h1
-rw-r--r--src/corelib/tools/qscopedvaluerollback.h6
-rw-r--r--src/corelib/tools/qscopeguard.h4
-rw-r--r--src/corelib/tools/qscopeguard.qdoc8
-rw-r--r--src/corelib/tools/qsharedpointer.cpp2
-rw-r--r--src/corelib/tools/qsimd.cpp2
-rw-r--r--src/corelib/tools/qsimd_p.h2
-rw-r--r--src/corelib/tools/qvector.h1135
-rw-r--r--src/corelib/tools/tools.pri2
242 files changed, 9113 insertions, 6176 deletions
diff --git a/src/corelib/Qt6CoreMacros.cmake b/src/corelib/Qt6CoreMacros.cmake
index 3da011ecb1..da0b4641ae 100644
--- a/src/corelib/Qt6CoreMacros.cmake
+++ b/src/corelib/Qt6CoreMacros.cmake
@@ -350,58 +350,53 @@ function(qt6_add_big_resources outfiles )
set(${outfiles} ${${outfiles}} PARENT_SCOPE)
endfunction()
-set(_Qt5_COMPONENT_PATH "${CMAKE_CURRENT_LIST_DIR}/..")
+set(_Qt6_COMPONENT_PATH "${CMAKE_CURRENT_LIST_DIR}/..")
-if (NOT CMAKE_VERSION VERSION_LESS 2.8.9)
- macro(qt6_use_modules _target _link_type)
- if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
- if(CMAKE_WARN_DEPRECATED)
- set(messageType WARNING)
- endif()
- if(CMAKE_ERROR_DEPRECATED)
- set(messageType FATAL_ERROR)
- endif()
- if(messageType)
- message(${messageType} "The qt6_use_modules macro is obsolete. Use target_link_libraries with IMPORTED targets instead.")
- endif()
- endif()
+macro(qt6_use_modules _target _link_type)
+ if(CMAKE_WARN_DEPRECATED)
+ set(messageType WARNING)
+ endif()
+ if(CMAKE_ERROR_DEPRECATED)
+ set(messageType FATAL_ERROR)
+ endif()
+ if(messageType)
+ message(${messageType} "The qt6_use_modules macro is obsolete. Use target_link_libraries with IMPORTED targets instead.")
+ endif()
- if (NOT TARGET ${_target})
- message(FATAL_ERROR "The first argument to qt6_use_modules must be an existing target.")
- endif()
- if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE" )
- set(_qt6_modules ${ARGN})
- set(_qt6_link_type ${_link_type})
- else()
- set(_qt6_modules ${_link_type} ${ARGN})
- endif()
+ if (NOT TARGET ${_target})
+ message(FATAL_ERROR "The first argument to qt6_use_modules must be an existing target.")
+ endif()
+ if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE" )
+ set(_qt6_modules ${ARGN})
+ set(_qt6_link_type ${_link_type})
+ else()
+ set(_qt6_modules ${_link_type} ${ARGN})
+ endif()
- if ("${_qt6_modules}" STREQUAL "")
- message(FATAL_ERROR "qt6_use_modules requires at least one Qt module to use.")
- endif()
+ if ("${_qt6_modules}" STREQUAL "")
+ message(FATAL_ERROR "qt6_use_modules requires at least one Qt module to use.")
+ endif()
- foreach(_module ${_qt6_modules})
- if (NOT Qt5${_module}_FOUND)
- find_package(Qt5${_module} PATHS "${_Qt5_COMPONENT_PATH}" NO_DEFAULT_PATH)
- if (NOT Qt5${_module}_FOUND)
- message(FATAL_ERROR "Cannot use \"${_module}\" module which has not yet been found.")
- endif()
- endif()
- target_link_libraries(${_target} ${_qt6_link_type} ${Qt5${_module}_LIBRARIES})
- set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${Qt5${_module}_INCLUDE_DIRS})
- set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${Qt5${_module}_COMPILE_DEFINITIONS})
- set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS_RELEASE QT_NO_DEBUG)
- set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS_RELWITHDEBINFO QT_NO_DEBUG)
- set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS_MINSIZEREL QT_NO_DEBUG)
- if (Qt5_POSITION_INDEPENDENT_CODE
- AND (CMAKE_VERSION VERSION_LESS 2.8.12
- AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
- OR CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)))
- set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ${Qt5_POSITION_INDEPENDENT_CODE})
+ foreach(_module ${_qt6_modules})
+ if (NOT Qt6${_module}_FOUND)
+ find_package(Qt6${_module} PATHS "${_Qt6_COMPONENT_PATH}" NO_DEFAULT_PATH)
+ if (NOT Qt6${_module}_FOUND)
+ message(FATAL_ERROR "Cannot use \"${_module}\" module which has not yet been found.")
endif()
- endforeach()
- endmacro()
-endif()
+ endif()
+ target_link_libraries(${_target} ${_qt6_link_type} ${Qt6${_module}_LIBRARIES})
+ set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${Qt6${_module}_INCLUDE_DIRS})
+ set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${Qt6${_module}_COMPILE_DEFINITIONS})
+ set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS_RELEASE QT_NO_DEBUG)
+ set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS_RELWITHDEBINFO QT_NO_DEBUG)
+ set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS_MINSIZEREL QT_NO_DEBUG)
+ if (Qt6_POSITION_INDEPENDENT_CODE
+ AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
+ OR CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0))
+ set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ${Qt6_POSITION_INDEPENDENT_CODE})
+ endif()
+ endforeach()
+endmacro()
function(add_qt_gui_executable target)
if(ANDROID)
diff --git a/src/corelib/animation/qabstractanimation.cpp b/src/corelib/animation/qabstractanimation.cpp
index 46b01449d4..b7136dc055 100644
--- a/src/corelib/animation/qabstractanimation.cpp
+++ b/src/corelib/animation/qabstractanimation.cpp
@@ -220,7 +220,7 @@ QUnifiedTimer::QUnifiedTimer() :
QObject(), defaultDriver(this), lastTick(0), timingInterval(DEFAULT_TIMER_INTERVAL),
currentAnimationIdx(0), insideTick(false), insideRestart(false), consistentTiming(false), slowMode(false),
startTimersPending(false), stopTimerPending(false),
- slowdownFactor(5.0f), profilerCallback(0),
+ slowdownFactor(5.0f), profilerCallback(nullptr),
driverStartTime(0), temporalDrift(0)
{
time.invalidate();
@@ -922,7 +922,7 @@ qint64 QAnimationDriver::elapsed() const
The default animation driver just spins the timer...
*/
QDefaultAnimationDriver::QDefaultAnimationDriver(QUnifiedTimer *timer)
- : QAnimationDriver(0), m_unified_timer(timer)
+ : QAnimationDriver(nullptr), m_unified_timer(timer)
{
connect(this, SIGNAL(started()), this, SLOT(startTimer()));
connect(this, SIGNAL(stopped()), this, SLOT(stopTimer()));
@@ -1035,7 +1035,7 @@ void QAbstractAnimationPrivate::setState(QAbstractAnimation::State newState)
\sa QVariantAnimation, QAnimationGroup
*/
QAbstractAnimation::QAbstractAnimation(QObject *parent)
- : QObject(*new QAbstractAnimationPrivate, 0)
+ : QObject(*new QAbstractAnimationPrivate, nullptr)
{
// Allow auto-add on reparent
setParent(parent);
@@ -1045,7 +1045,7 @@ QAbstractAnimation::QAbstractAnimation(QObject *parent)
\internal
*/
QAbstractAnimation::QAbstractAnimation(QAbstractAnimationPrivate &dd, QObject *parent)
- : QObject(dd, 0)
+ : QObject(dd, nullptr)
{
// Allow auto-add on reparent
setParent(parent);
diff --git a/src/corelib/animation/qanimationgroup.cpp b/src/corelib/animation/qanimationgroup.cpp
index 69e2cfc9bc..729f55d68f 100644
--- a/src/corelib/animation/qanimationgroup.cpp
+++ b/src/corelib/animation/qanimationgroup.cpp
@@ -133,7 +133,7 @@ QAbstractAnimation *QAnimationGroup::animationAt(int index) const
if (index < 0 || index >= d->animations.size()) {
qWarning("QAnimationGroup::animationAt: index is out of bounds");
- return 0;
+ return nullptr;
}
return d->animations.at(index);
@@ -243,14 +243,14 @@ QAbstractAnimation *QAnimationGroup::takeAnimation(int index)
Q_D(QAnimationGroup);
if (index < 0 || index >= d->animations.size()) {
qWarning("QAnimationGroup::takeAnimation: no animation at index %d", index);
- return 0;
+ return nullptr;
}
QAbstractAnimation *animation = d->animations.at(index);
- QAbstractAnimationPrivate::get(animation)->group = 0;
+ QAbstractAnimationPrivate::get(animation)->group = nullptr;
// ### removing from list before doing setParent to avoid inifinite recursion
// in ChildRemoved event
d->animations.removeAt(index);
- animation->setParent(0);
+ animation->setParent(nullptr);
d->animationRemoved(index, animation);
return animation;
}
diff --git a/src/corelib/animation/qpropertyanimation.cpp b/src/corelib/animation/qpropertyanimation.cpp
index 2a3572d441..c71a77e073 100644
--- a/src/corelib/animation/qpropertyanimation.cpp
+++ b/src/corelib/animation/qpropertyanimation.cpp
@@ -259,7 +259,7 @@ void QPropertyAnimation::updateState(QAbstractAnimation::State newState,
QVariantAnimation::updateState(newState, oldState);
- QPropertyAnimation *animToStop = 0;
+ QPropertyAnimation *animToStop = nullptr;
{
static QBasicMutex mutex;
auto locker = qt_unique_lock(mutex);
diff --git a/src/corelib/animation/qsequentialanimationgroup.cpp b/src/corelib/animation/qsequentialanimationgroup.cpp
index 66e346a2fe..98ac04a14f 100644
--- a/src/corelib/animation/qsequentialanimationgroup.cpp
+++ b/src/corelib/animation/qsequentialanimationgroup.cpp
@@ -282,7 +282,7 @@ QPauseAnimation *QSequentialAnimationGroup::insertPause(int index, int msecs)
if (index < 0 || index > d->animations.size()) {
qWarning("QSequentialAnimationGroup::insertPause: index is out of bounds");
- return 0;
+ return nullptr;
}
QPauseAnimation *pause = new QPauseAnimation(msecs);
@@ -430,7 +430,7 @@ void QSequentialAnimationGroupPrivate::setCurrentAnimation(int index, bool inter
if (index == -1) {
Q_ASSERT(animations.isEmpty());
currentAnimationIndex = -1;
- currentAnimation = 0;
+ currentAnimation = nullptr;
return;
}
@@ -503,7 +503,7 @@ void QSequentialAnimationGroupPrivate::_q_uncontrolledAnimationFinished()
*/
void QSequentialAnimationGroupPrivate::animationInsertedAt(int index)
{
- if (currentAnimation == 0)
+ if (currentAnimation == nullptr)
setCurrentAnimation(0); // initialize the current animation
if (currentAnimationIndex == index
diff --git a/src/corelib/animation/qvariantanimation.cpp b/src/corelib/animation/qvariantanimation.cpp
index 216c015732..98b02f0202 100644
--- a/src/corelib/animation/qvariantanimation.cpp
+++ b/src/corelib/animation/qvariantanimation.cpp
@@ -209,7 +209,7 @@ void QVariantAnimationPrivate::updateInterpolator()
if (type == currentInterval.end.second.userType())
interpolator = getInterpolator(type);
else
- interpolator = 0;
+ interpolator = nullptr;
//we make sure that the interpolator is always set to something
if (!interpolator)
@@ -445,7 +445,7 @@ QVariantAnimation::Interpolator QVariantAnimationPrivate::getInterpolator(int in
{
QInterpolatorVector *interpolators = registeredInterpolators();
const auto locker = qt_scoped_lock(registeredInterpolatorsMutex);
- QVariantAnimation::Interpolator ret = 0;
+ QVariantAnimation::Interpolator ret = nullptr;
if (interpolationType < interpolators->count()) {
ret = interpolators->at(interpolationType);
if (ret) return ret;
@@ -479,7 +479,7 @@ QVariantAnimation::Interpolator QVariantAnimationPrivate::getInterpolator(int in
case QMetaType::QRectF:
return castToInterpolator(_q_interpolateVariant<QRectF>);
default:
- return 0; //this type is not handled
+ return nullptr; //this type is not handled
}
}
diff --git a/src/corelib/codecs/qgb18030codec.cpp b/src/corelib/codecs/qgb18030codec.cpp
index 4206eacb6f..ca15be1cea 100644
--- a/src/corelib/codecs/qgb18030codec.cpp
+++ b/src/corelib/codecs/qgb18030codec.cpp
@@ -107,7 +107,6 @@ QByteArray QGb18030Codec::convertFromUnicode(const QChar *uc, int len, Converter
if (high >= 0) {
if (uc[i].isLowSurrogate()) {
// valid surrogate pair
- ++i;
uint u = QChar::surrogateToUcs4(high, uc[i].unicode());
len = qt_UnicodeToGb18030(u, buf);
if (len >= 2) {
diff --git a/src/corelib/codecs/qicucodec.cpp b/src/corelib/codecs/qicucodec.cpp
index 5a778c2638..f9092277b2 100644
--- a/src/corelib/codecs/qicucodec.cpp
+++ b/src/corelib/codecs/qicucodec.cpp
@@ -381,7 +381,7 @@ static QTextCodec *loadQtCodec(const char *name)
return QIsciiCodec::create(name);
#endif
- return 0;
+ return nullptr;
}
/// \threadsafe
@@ -438,7 +438,7 @@ QTextCodec *QIcuCodec::defaultCodecUnlocked()
{
QCoreGlobalData *globalData = QCoreGlobalData::instance();
if (!globalData)
- return 0;
+ return nullptr;
QTextCodec *c = globalData->codecForLocale.loadAcquire();
if (c)
return c;
@@ -523,13 +523,13 @@ QTextCodec *QIcuCodec::codecForNameUnlocked(const char *name)
return c;
if (qt_only)
- return 0;
+ return nullptr;
// check whether there is really a converter for the name available.
UConverter *conv = ucnv_open(standardName, &error);
if (!conv) {
qDebug("codecForName: ucnv_open failed %s %s", standardName, u_errorName(error));
- return 0;
+ return nullptr;
}
//qDebug() << "QIcuCodec: Standard name for " << name << "is" << standardName;
ucnv_close(conv);
@@ -552,7 +552,7 @@ QTextCodec *QIcuCodec::codecForMibUnlocked(int mib)
if (mib == 2107)
return codecForNameUnlocked("TSCII");
- return 0;
+ return nullptr;
}
@@ -567,7 +567,7 @@ QIcuCodec::~QIcuCodec()
UConverter *QIcuCodec::getConverter(QTextCodec::ConverterState *state) const
{
- UConverter *conv = 0;
+ UConverter *conv = nullptr;
if (state) {
if (!state->d) {
// first time
@@ -609,7 +609,7 @@ QString QIcuCodec::convertToUnicode(const char *chars, int length, QTextCodec::C
ucnv_toUnicode(conv,
&uc, ucEnd,
&chars, end,
- 0, false, &error);
+ nullptr, false, &error);
if (!U_SUCCESS(error) && error != U_BUFFER_OVERFLOW_ERROR) {
qDebug("convertToUnicode failed: %s", u_errorName(error));
break;
@@ -646,7 +646,7 @@ QByteArray QIcuCodec::convertFromUnicode(const QChar *unicode, int length, QText
ucnv_fromUnicode(conv,
&ch, chEnd,
&uc, end,
- 0, false, &error);
+ nullptr, false, &error);
if (!U_SUCCESS(error))
qDebug("convertFromUnicode failed: %s", u_errorName(error));
convertedChars = ch - string.data();
diff --git a/src/corelib/codecs/qisciicodec.cpp b/src/corelib/codecs/qisciicodec.cpp
index d9a86d77c7..9689818559 100644
--- a/src/corelib/codecs/qisciicodec.cpp
+++ b/src/corelib/codecs/qisciicodec.cpp
@@ -74,7 +74,7 @@ QTextCodec *QIsciiCodec::create(const char *name)
if (qTextCodecNameMatch(name, codecs[i].name))
return new QIsciiCodec(i);
}
- return 0;
+ return nullptr;
}
QIsciiCodec::~QIsciiCodec()
diff --git a/src/corelib/codecs/qsimplecodec.cpp b/src/corelib/codecs/qsimplecodec.cpp
index 16a9b8a7c3..4e82620003 100644
--- a/src/corelib/codecs/qsimplecodec.cpp
+++ b/src/corelib/codecs/qsimplecodec.cpp
@@ -51,7 +51,7 @@ static const struct {
quint16 values[128];
} unicodevalues[QSimpleTextCodec::numSimpleCodecs] = {
// from RFC 1489, ftp://ftp.isi.edu/in-notes/rfc1489.txt
- { "KOI8-R", { "csKOI8R", 0 }, 2084,
+ { "KOI8-R", { "csKOI8R", nullptr }, 2084,
{ 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524,
0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2219/**/, 0x221A, 0x2248,
@@ -72,7 +72,7 @@ static const struct {
// it should be 0x2022 (BULLET).
// from RFC 2319, ftp://ftp.isi.edu/in-notes/rfc2319.txt
- { "KOI8-U", { "KOI8-RU", 0 }, 2088,
+ { "KOI8-U", { "KOI8-RU", nullptr }, 2088,
{ 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524,
0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2219, 0x221A, 0x2248,
@@ -97,7 +97,7 @@ static const struct {
// $ for A in 8 9 A B C D E F ; do for B in 0 1 2 3 4 5 6 7 8 9 A B C D E F ; do echo 0x${A}${B} 0xFFFD ; done ; done > /tmp/digits ; for a in 8859-* ; do (awk '/^0x[89ABCDEF]/{ print $1, $2 }' < $a ; cat /tmp/digits) | sort | uniq -w4 | cut -c6- | paste '-d ' - - - - - - - - | sed -e 's/ /, /g' -e 's/$/,/' -e '$ s/,$/} },/' -e '1 s/^/{ /' > ~/tmp/$a ; done
// then I inserted the files manually.
- { "ISO-8859-2", {"latin2", "iso-ir-101", "csISOLatin2", 0 }, 5,
+ { "ISO-8859-2", {"latin2", "iso-ir-101", "csISOLatin2", nullptr }, 5,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -114,7 +114,7 @@ static const struct {
0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7,
0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9} },
- { "ISO-8859-3", { "latin3", "iso-ir-109", "csISOLatin3", 0 }, 6,
+ { "ISO-8859-3", { "latin3", "iso-ir-109", "csISOLatin3", nullptr }, 6,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -131,7 +131,7 @@ static const struct {
0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
0xFFFD, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x0121, 0x00F6, 0x00F7,
0x011D, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x016D, 0x015D, 0x02D9} },
- { "ISO-8859-4", { "latin4", "iso-ir-110", "csISOLatin4", 0 }, 7,
+ { "ISO-8859-4", { "latin4", "iso-ir-110", "csISOLatin4", nullptr }, 7,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -148,7 +148,7 @@ static const struct {
0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x012B,
0x0111, 0x0146, 0x014D, 0x0137, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x0169, 0x016B, 0x02D9} },
- { "ISO-8859-5", { "cyrillic", "iso-ir-144", "csISOLatinCyrillic", 0 }, 8,
+ { "ISO-8859-5", { "cyrillic", "iso-ir-144", "csISOLatinCyrillic", nullptr }, 8,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -165,7 +165,7 @@ static const struct {
0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457,
0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x00A7, 0x045E, 0x045F} },
- { "ISO-8859-6", { "ISO-8859-6-I", "ECMA-114", "ASMO-708", "arabic", "iso-ir-127", "csISOLatinArabic", 0 }, 82,
+ { "ISO-8859-6", { "ISO-8859-6-I", "ECMA-114", "ASMO-708", "arabic", "iso-ir-127", "csISOLatinArabic", nullptr }, 82,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -182,7 +182,7 @@ static const struct {
0x0648, 0x0649, 0x064A, 0x064B, 0x064C, 0x064D, 0x064E, 0x064F,
0x0650, 0x0651, 0x0652, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD} },
- { "ISO-8859-7", { "ECMA-118", "greek", "iso-ir-126", "csISOLatinGreek", 0 }, 10,
+ { "ISO-8859-7", { "ECMA-118", "greek", "iso-ir-126", "csISOLatinGreek", nullptr }, 10,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -199,7 +199,7 @@ static const struct {
0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,
0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, 0xFFFD} },
- { "ISO-8859-8", { "ISO 8859-8-I", "iso-ir-138", "hebrew", "csISOLatinHebrew", 0 }, 85,
+ { "ISO-8859-8", { "ISO 8859-8-I", "iso-ir-138", "hebrew", "csISOLatinHebrew", nullptr }, 85,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -216,7 +216,7 @@ static const struct {
0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,
0x05E8, 0x05E9, 0x05EA, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD} },
- { "ISO-8859-9", { "iso-ir-148", "latin5", "csISOLatin5", 0 }, 12,
+ { "ISO-8859-9", { "iso-ir-148", "latin5", "csISOLatin5", nullptr }, 12,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -233,7 +233,7 @@ static const struct {
0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131, 0x015F, 0x00FF} },
- { "ISO-8859-10", { "iso-ir-157", "latin6", "ISO-8859-10:1992", "csISOLatin6", 0 }, 13,
+ { "ISO-8859-10", { "iso-ir-157", "latin6", "ISO-8859-10:1992", "csISOLatin6", nullptr }, 13,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -250,7 +250,7 @@ static const struct {
0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x00EF,
0x00F0, 0x0146, 0x014D, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x0169,
0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x0138} },
- { "ISO-8859-13", { 0 }, 109,
+ { "ISO-8859-13", { nullptr }, 109,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -267,7 +267,7 @@ static const struct {
0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C,
0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7,
0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x2019} },
- { "ISO-8859-14", { "iso-ir-199", "latin8", "iso-celtic", 0 }, 110,
+ { "ISO-8859-14", { "iso-ir-199", "latin8", "iso-celtic", nullptr }, 110,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -284,7 +284,7 @@ static const struct {
0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
0x0175, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x1E6B,
0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x0177, 0x00FF} },
- { "ISO-8859-16", { "iso-ir-226", "latin10", 0 }, 112,
+ { "ISO-8859-16", { "iso-ir-226", "latin10", nullptr }, 112,
{ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
@@ -305,7 +305,7 @@ static const struct {
// next bits generated again from tables on the Unicode 3.0 CD.
// $ for a in CP* ; do (awk '/^0x[89ABCDEF]/{ print $1, $2 }' < $a) | sort | sed -e 's/#UNDEF.*$/0xFFFD/' | cut -c6- | paste '-d ' - - - - - - - - | sed -e 's/ /, /g' -e 's/$/,/' -e '$ s/,$/} },/' -e '1 s/^/{ /' > ~/tmp/$a ; done
- { "IBM850", { "CP850", "csPC850Multilingual", 0 }, 2009,
+ { "IBM850", { "CP850", "csPC850Multilingual", nullptr }, 2009,
{ 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,
0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,
@@ -322,7 +322,7 @@ static const struct {
0x00DE, 0x00DA, 0x00DB, 0x00D9, 0x00FD, 0x00DD, 0x00AF, 0x00B4,
0x00AD, 0x00B1, 0x2017, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8,
0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0} },
- { "IBM874", { "CP874", 0 }, -874, //### what is the mib?
+ { "IBM874", { "CP874", nullptr }, -874, //### what is the mib?
{ 0x20AC, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2026, 0xFFFD, 0xFFFD,
0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -339,7 +339,7 @@ static const struct {
0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F,
0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57,
0x0E58, 0x0E59, 0x0E5A, 0x0E5B, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD} },
- { "IBM866", { "CP866", "csIBM866", 0 }, 2086,
+ { "IBM866", { "CP866", "csIBM866", nullptr }, 2086,
{ 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
@@ -357,7 +357,7 @@ static const struct {
0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040E, 0x045E,
0x00B0, 0x2219, 0x00B7, 0x221A, 0x2116, 0x00A4, 0x25A0, 0x00A0} },
- { "windows-1250", { "CP1250", 0 }, 2250,
+ { "windows-1250", { "CP1250", nullptr }, 2250,
{ 0x20AC, 0xFFFD, 0x201A, 0xFFFD, 0x201E, 0x2026, 0x2020, 0x2021,
0xFFFD, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -374,7 +374,7 @@ static const struct {
0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7,
0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9} },
- { "windows-1251", { "CP1251", 0 }, 2251,
+ { "windows-1251", { "CP1251", nullptr }, 2251,
{ 0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021,
0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F,
0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -391,7 +391,7 @@ static const struct {
0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F} },
- { "windows-1252", { "CP1252", 0 }, 2252,
+ { "windows-1252", { "CP1252", nullptr }, 2252,
{ 0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0xFFFD, 0x017D, 0xFFFD,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -408,7 +408,7 @@ static const struct {
0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF} },
- { "windows-1253", {"CP1253", 0 }, 2253,
+ { "windows-1253", {"CP1253", nullptr }, 2253,
{ 0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0xFFFD, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -425,7 +425,7 @@ static const struct {
0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,
0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, 0xFFFD} },
- { "windows-1254", { "CP1254", 0 }, 2254,
+ { "windows-1254", { "CP1254", nullptr }, 2254,
{ 0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -442,7 +442,7 @@ static const struct {
0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131, 0x015F, 0x00FF} },
- { "windows-1255", { "CP1255", 0 }, 2255,
+ { "windows-1255", { "CP1255", nullptr }, 2255,
{ 0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -459,7 +459,7 @@ static const struct {
0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,
0x05E8, 0x05E9, 0x05EA, 0xFFFD, 0xFFFD, 0x200E, 0x200F, 0xFFFD} },
- { "windows-1256", { "CP1256", 0 }, 2256,
+ { "windows-1256", { "CP1256", nullptr }, 2256,
{ 0x20AC, 0x067E, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688,
0x06AF, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -476,7 +476,7 @@ static const struct {
0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0649, 0x064A, 0x00EE, 0x00EF,
0x064B, 0x064C, 0x064D, 0x064E, 0x00F4, 0x064F, 0x0650, 0x00F7,
0x0651, 0x00F9, 0x0652, 0x00FB, 0x00FC, 0x200E, 0x200F, 0x06D2} },
- { "windows-1257", { "CP1257", 0 }, 2257,
+ { "windows-1257", { "CP1257", nullptr }, 2257,
{ 0x20AC, 0xFFFD, 0x201A, 0xFFFD, 0x201E, 0x2026, 0x2020, 0x2021,
0xFFFD, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0x00A8, 0x02C7, 0x00B8,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -493,7 +493,7 @@ static const struct {
0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C,
0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7,
0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x02D9} },
- { "windows-1258", { "CP1258", 0 }, 2258,
+ { "windows-1258", { "CP1258", nullptr }, 2258,
{ 0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0xFFFD, 0x2039, 0x0152, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -511,7 +511,7 @@ static const struct {
0x0111, 0x00F1, 0x0323, 0x00F3, 0x00F4, 0x01A1, 0x00F6, 0x00F7,
0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x01B0, 0x20AB, 0x00FF} },
- { "macintosh", { "Apple Roman", "MacRoman", 0 }, 2027,
+ { "macintosh", { "Apple Roman", "MacRoman", nullptr }, 2027,
{ 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1,
0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8,
0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3,
@@ -532,7 +532,7 @@ static const struct {
// This one is based on the charmap file
// /usr/share/i18n/charmaps/SAMI-WS2.gz, which is manually adapted
// to this format by Boerre Gaup <boerre@subdimension.com>
- { "WINSAMI2", { "WS2", 0 }, -165,
+ { "WINSAMI2", { "WS2", nullptr }, -165,
{ 0x20AC, 0xFFFD, 0x010C, 0x0192, 0x010D, 0x01B7, 0x0292, 0x01EE,
0x01EF, 0x0110, 0x0160, 0x2039, 0x0152, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -555,7 +555,7 @@ static const struct {
// to iso8859-11, so we name it 8859-11 here, but recognise the name tis620 too.
// $ for A in 8 9 A B C D E F ; do for B in 0 1 2 3 4 5 6 7 8 9 A B C D E F ; do echo x${A}${B} 0xFFFD ; done ; done > /tmp/digits ; (cut -c25- < TIS-620 ; cat /tmp/digits) | awk '/^x[89ABCDEF]/{ print $1, $2 }' | sed -e 's/<U/0x/' -e 's/>//' | sort | uniq -w4 | cut -c5- | paste '-d ' - - - - - - - - | sed -e 's/ /, /g' -e 's/$/,/' -e '$ s/,$/} },/' -e '1 s/^/{ /' > ~/tmp/tis-620
- { "TIS-620", { "ISO 8859-11", 0 }, 2259, // Thai character set mib enum taken from tis620 (which is byte by byte equivalent)
+ { "TIS-620", { "ISO 8859-11", nullptr }, 2259, // Thai character set mib enum taken from tis620 (which is byte by byte equivalent)
{ 0x20AC, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2026, 0xFFFD, 0xFFFD,
0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
@@ -582,7 +582,7 @@ static const struct {
Alias: r8
Alias: csHPRoman8
*/
- { "hp-roman8", { "roman8", "csHPRoman8", 0 }, 2004,
+ { "hp-roman8", { "roman8", "csHPRoman8", nullptr }, 2004,
{ 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
@@ -603,7 +603,7 @@ static const struct {
// if you add more chacater sets at the end, change LAST_MIB above
};
-QSimpleTextCodec::QSimpleTextCodec(int i) : forwardIndex(i), reverseMap(0)
+QSimpleTextCodec::QSimpleTextCodec(int i) : forwardIndex(i), reverseMap(nullptr)
{
}
@@ -640,7 +640,7 @@ static QByteArray *buildReverseMap(int forwardIndex)
QString QSimpleTextCodec::convertToUnicode(const char* chars, int len, ConverterState *) const
{
- if (len <= 0 || chars == 0)
+ if (len <= 0 || chars == nullptr)
return QString();
const unsigned char * c = (const unsigned char *)chars;
@@ -665,7 +665,7 @@ QByteArray QSimpleTextCodec::convertFromUnicode(const QChar *in, int length, Con
QByteArray *rmap = reverseMap.loadAcquire();
if (!rmap){
rmap = buildReverseMap(this->forwardIndex);
- if (!reverseMap.testAndSetRelease(0, rmap)) {
+ if (!reverseMap.testAndSetRelease(nullptr, rmap)) {
delete rmap;
rmap = reverseMap.loadAcquire();
}
diff --git a/src/corelib/configure.json b/src/corelib/configure.json
index 33bf3d759c..b607c300dd 100644
--- a/src/corelib/configure.json
+++ b/src/corelib/configure.json
@@ -1090,14 +1090,20 @@
"label": "Windows System Libraries",
"condition": "config.win32 && libs.advapi32 && libs.gdi32 && libs.kernel32 && libs.netapi32 && libs.ole32 && libs.shell32 && libs.uuid && libs.user32 && libs.winmm && libs.ws2_32"
},
- "cborstream": {
- "label": "CBOR stream I/O",
- "purpose": "Provides support for reading and writing the CBOR binary format.
+ "cborstreamreader": {
+ "label": "CBOR stream reading",
+ "purpose": "Provides support for reading the CBOR binary format.
Note that this is required for plugin loading. Qt GUI needs QPA plugins for basic operation.",
"section": "Utilities",
"output": [ "publicFeature" ]
},
+ "cborstreamwriter": {
+ "label": "CBOR stream writing",
+ "purpose": "Provides support for writing the CBOR binary format.",
+ "section": "Utilities",
+ "output": [ "publicFeature" ]
+ },
"binaryjson": {
"label": "Binary JSON (deprecated)",
"purpose": "Provides support for the deprecated binary JSON format.",
diff --git a/src/corelib/corelib.pro b/src/corelib/corelib.pro
index eca888385b..3430154d13 100644
--- a/src/corelib/corelib.pro
+++ b/src/corelib/corelib.pro
@@ -1,6 +1,6 @@
TARGET = QtCore
QT =
-CONFIG += exceptions metatypes install_metatypes
+CONFIG += exceptions
MODULE = core # not corelib, as per project file
MODULE_CONFIG = moc resources
@@ -17,6 +17,7 @@ DEFINES += QT_NO_USING_NAMESPACE QT_NO_FOREACH
msvc:equals(QT_ARCH, i386): QMAKE_LFLAGS += /BASE:0x67000000
CONFIG += simd optimize_full
+CONFIG += metatypes install_metatypes
QMAKE_DOCS = $$PWD/doc/qtcore.qdocconf
diff --git a/src/corelib/doc/qtcore.qdocconf b/src/corelib/doc/qtcore.qdocconf
index 15b1925e51..2b9adabc3a 100644
--- a/src/corelib/doc/qtcore.qdocconf
+++ b/src/corelib/doc/qtcore.qdocconf
@@ -26,7 +26,7 @@ qhp.QtCore.subprojects.classes.sortPages = true
tagfile = ../../../doc/qtcore/qtcore.tags
-depends += activeqt qtdbus qtgui qtwidgets qtnetwork qtdoc qtmacextras qtquick qtlinguist qtdesigner qtconcurrent qtxml qmake qtwinextras qtqml
+depends += activeqt qtdbus qtgui qtwidgets qtnetwork qtdoc qtmacextras qtquick qtlinguist qtdesigner qtconcurrent qtxml qmake qtwinextras qtqml qtcmake
headerdirs += ..
diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp
index eb75a29ca2..a540b88247 100644
--- a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp
@@ -50,6 +50,7 @@
//! [0]
label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
+label->setAlignment({ });
//! [0]
diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp
index 1966f8195a..43bcc22720 100644
--- a/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp
@@ -71,7 +71,7 @@ QTimer *timer = qobject_cast<QTimer *>(obj);
// timer == (QObject *)obj
QAbstractButton *button = qobject_cast<QAbstractButton *>(obj);
-// button == 0
+// button == nullptr
//! [3]
@@ -144,7 +144,7 @@ class MyObject : public QObject
Q_OBJECT
public:
- MyObject(QObject *parent = 0);
+ MyObject(QObject *parent = nullptr);
protected:
void timerEvent(QTimerEvent *event) override;
@@ -322,7 +322,7 @@ QObject::connect: Cannot queue arguments of type 'MyType'
//! [26]
-disconnect(myObject, 0, 0, 0);
+disconnect(myObject, nullptr, nullptr, nullptr);
//! [26]
@@ -332,7 +332,7 @@ myObject->disconnect();
//! [28]
-disconnect(myObject, SIGNAL(mySignal()), 0, 0);
+disconnect(myObject, SIGNAL(mySignal()), nullptr, nullptr);
//! [28]
@@ -342,7 +342,7 @@ myObject->disconnect(SIGNAL(mySignal()));
//! [30]
-disconnect(myObject, 0, myReceiver, 0);
+disconnect(myObject, nullptr, myReceiver, nullptr);
//! [30]
@@ -391,7 +391,7 @@ class MyClass : public QObject
Q_OBJECT
public:
- MyClass(QObject *parent = 0);
+ MyClass(QObject *parent = nullptr);
~MyClass();
enum Priority { High, Low, VeryHigh, VeryLow };
@@ -467,7 +467,7 @@ QObject::connect(socket, &QTcpSocket::connected, [=] () {
//! [46]
//! [47]
-disconnect(myObject, &MyObject::mySignal(), 0, 0);
+disconnect(myObject, &MyObject::mySignal(), nullptr, nullptr);
//! [47]
//! [48]
@@ -505,7 +505,7 @@ class MyClass : public QWidget
Q_OBJECT
public:
- MyClass(QWidget *parent = 0);
+ MyClass(QWidget *parent = nullptr);
~MyClass();
bool event(QEvent* ev) override
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp
index b1dc392140..9c07a2e92c 100644
--- a/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp
@@ -422,7 +422,20 @@ text.data(); // returns "Qt is great!"
QByteArray::fromBase64("PHA+SGVsbG8/PC9wPg==", QByteArray::Base64Encoding); // returns "<p>Hello?</p>"
QByteArray::fromBase64("PHA-SGVsbG8_PC9wPg==", QByteArray::Base64UrlEncoding); // returns "<p>Hello?</p>"
-//! [44]
+//! [44bis]
+
+//! [44ter]
+void process(const QByteArray &);
+
+if (auto result = QByteArray::fromBase64Encoding(encodedData))
+ process(*result);
+//! [44ter]
+
+//! [44quater]
+auto result = QByteArray::fromBase64Encoding(encodedData);
+if (result.decodingStatus == QByteArray::Base64DecodingStatus::Ok)
+ process(result.decoded);
+//! [44quater]
//! [45]
QByteArray text = QByteArray::fromHex("517420697320677265617421");
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp
index 9813cc98d5..a140175956 100644
--- a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp
@@ -119,22 +119,6 @@ hash.insert("plenty", 2000);
//! [9]
-//! [10]
-QList<int> values = hash.values("plenty");
-for (int i = 0; i < values.size(); ++i)
- cout << values.at(i) << Qt::endl;
-//! [10]
-
-
-//! [11]
-QHash<QString, int>::iterator i = hash.find("plenty");
-while (i != hash.end() && i.key() == "plenty") {
- cout << i.value() << Qt::endl;
- ++i;
-}
-//! [11]
-
-
//! [12]
QHash<QString, int> hash;
...
diff --git a/src/corelib/doc/src/dontdocument.qdoc b/src/corelib/doc/src/dontdocument.qdoc
index 19ca7db299..5fe05997cc 100644
--- a/src/corelib/doc/src/dontdocument.qdoc
+++ b/src/corelib/doc/src/dontdocument.qdoc
@@ -29,7 +29,7 @@
\dontdocument (QMacAutoReleasePool QIncompatibleFlag QGenericAtomicOps QAtomicTraits
QAtomicOps QBasicAtomicInteger QBasicAtomicPointer QBasicMutex QInternal
QArgument QReturnArgument QArrayData QTypedArrayData QStaticByteArrayData
- QByteRef QStaticStringData QListSpecialMethods QListData QScopedPointerDeleter
+ QStaticStringData QListSpecialMethods QListData QScopedPointerDeleter
QScopedPointerArrayDeleter QScopedPointerPodDeleter QScopedPointerObjectDeleteLater
QMetaTypeId2 QObjectData QObjectUserData QMapNodeBase QMapNode QMapDataBase
QMapData QHashData QHashNode QArrayDataPointer QTextStreamManipulator
@@ -37,5 +37,5 @@
QCborValueRef qfloat16 QDeferredDeleteEvent QSpecialInteger QLittleEndianStorageType
QBigEndianStorageType QFactoryInterface QFutureWatcherBase QJsonValuePtr
QJsonValueRefPtr QLinkedListNode QAbstractConcatenable QStringBuilderCommon
- QTextCodec::ConverterState QThreadStorageData)
+ QTextCodec::ConverterState QThreadStorageData QTextStreamManipulator)
*/
diff --git a/src/corelib/doc/src/qtcore-index.qdoc b/src/corelib/doc/src/qtcore-index.qdoc
index 40a6584af0..5838d13914 100644
--- a/src/corelib/doc/src/qtcore-index.qdoc
+++ b/src/corelib/doc/src/qtcore-index.qdoc
@@ -56,7 +56,7 @@
\include module-use.qdocinc using qt module
\quotefile overview/using-qt-core.cmake
- See also the \l[QtDoc]{Building with CMake} overview.
+ See also the \l{Build with CMake} overview.
\section2 Building with qmake
diff --git a/src/corelib/doc/src/resource-system.qdoc b/src/corelib/doc/src/resource-system.qdoc
index 69ec5e556b..f9ef317799 100644
--- a/src/corelib/doc/src/resource-system.qdoc
+++ b/src/corelib/doc/src/resource-system.qdoc
@@ -189,13 +189,13 @@
XML file to indicate a file should be most compressed, regardless of
which algorithms \c rcc supports.
- \li \c{zstd}: use the \l{Zstandard}{https://zstd.net} library to compress
+ \li \c{zstd}: use the \l{https://zstd.net}{Zstandard} library to compress
contents. Valid compression levels range from 1 to 19, 1 is least
compression (least CPU time) and 19 is the most compression (most CPU
time). The default level is 14. A special value of 0 tells the \c{zstd}
library to choose an implementation-defined default.
- \li \c{zlib}: use the \l{zlib}{https://zlib.net} library to compress
+ \li \c{zlib}: use the \l{https://zlib.net}{zlib} library to compress
contents. Valid compression levels range from 1 to 9, with 1the least
compression (least CPU time) and 9 the most compression (most CPU time).
The special value 0 means "no compression" and should not be used. The
diff --git a/src/corelib/global/qcompilerdetection.h b/src/corelib/global/qcompilerdetection.h
index ad3d2be69b..99a30e941b 100644
--- a/src/corelib/global/qcompilerdetection.h
+++ b/src/corelib/global/qcompilerdetection.h
@@ -358,7 +358,6 @@
# define Q_COMPILER_ATTRIBUTES
# define Q_COMPILER_AUTO_FUNCTION
# define Q_COMPILER_CLASS_ENUM
-# define Q_COMPILER_CONSTEXPR
# define Q_COMPILER_DECLTYPE
# define Q_COMPILER_DEFAULT_MEMBERS
# define Q_COMPILER_DELETE_MEMBERS
@@ -494,6 +493,39 @@
#endif
/*
+ * SG10's SD-6 feature detection and some useful extensions from Clang and GCC
+ * https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
+ * http://clang.llvm.org/docs/LanguageExtensions.html#feature-checking-macros
+ * Not using wrapper macros, per http://eel.is/c++draft/cpp.cond#7.sentence-2
+ */
+#ifndef __has_builtin
+# define __has_builtin(x) 0
+#endif
+#ifndef __has_feature
+# define __has_feature(x) 0
+#endif
+#ifndef __has_attribute
+# define __has_attribute(x) 0
+#endif
+#ifndef __has_cpp_attribute
+# define __has_cpp_attribute(x) 0
+#endif
+#ifndef __has_include
+# define __has_include(x) 0
+#endif
+#ifndef __has_include_next
+# define __has_include_next(x) 0
+#endif
+
+// Kept around until all submodules have transitioned
+#define QT_HAS_BUILTIN(x) __has_builtin(x)
+#define QT_HAS_FEATURE(x) __has_feature(x)
+#define QT_HAS_ATTRIBUTE(x) __has_attribute(x)
+#define QT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
+#define QT_HAS_INCLUDE(x) __has_include(x)
+#define QT_HAS_INCLUDE_NEXT(x) __has_include_next(x)
+
+/*
* C++11 support
*
* Paper Macro SD-6 macro
@@ -783,13 +815,14 @@
# endif
# endif
-# if defined(__has_warning)
-# if __has_warning("-Wunused-private-field")
-# define Q_DECL_UNUSED_MEMBER Q_DECL_UNUSED
-# endif
-# endif
+#endif // Q_CC_CLANG && !Q_CC_INTEL && !Q_CC_MSVC
-#endif // Q_CC_CLANG
+#if defined(Q_CC_CLANG) && !defined(Q_CC_INTEL)
+# ifndef Q_DECL_UNUSED
+# define Q_DECL_UNUSED __attribute__((__unused__))
+# endif
+# define Q_DECL_UNUSED_MEMBER Q_DECL_UNUSED
+#endif
#if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && !defined(Q_CC_CLANG)
# define Q_COMPILER_RESTRICTED_VLA
@@ -837,7 +870,6 @@
# define Q_COMPILER_DEFAULT_MEMBERS
# define Q_COMPILER_DELETE_MEMBERS
/* C++11 features supported in GCC 4.6: */
-# define Q_COMPILER_CONSTEXPR
# define Q_COMPILER_NULLPTR
# define Q_COMPILER_UNRESTRICTED_UNIONS
# define Q_COMPILER_RANGE_FOR
@@ -868,7 +900,11 @@
# define Q_COMPILER_REF_QUALIFIERS
# endif
# endif
- /* C++11 features are complete as of GCC 4.8.1 */
+# if Q_CC_GNU >= 500
+ /* GCC 4.6 introduces constexpr, but it's bugged (at least) in the whole
+ * 4.x series, see e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57694 */
+# define Q_COMPILER_CONSTEXPR
+# endif
# endif
# if __cplusplus > 201103L
# if Q_CC_GNU >= 409
@@ -1020,37 +1056,6 @@
#endif
/*
- * SG10's SD-6 feature detection and some useful extensions from Clang and GCC
- * https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
- * http://clang.llvm.org/docs/LanguageExtensions.html#feature-checking-macros
- */
-#ifdef __has_builtin
-# define QT_HAS_BUILTIN(x) __has_builtin(x)
-#else
-# define QT_HAS_BUILTIN(x) 0
-#endif
-#ifdef __has_attribute
-# define QT_HAS_ATTRIBUTE(x) __has_attribute(x)
-#else
-# define QT_HAS_ATTRIBUTE(x) 0
-#endif
-#ifdef __has_cpp_attribute
-# define QT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
-#else
-# define QT_HAS_CPP_ATTRIBUTE(x) 0
-#endif
-#ifdef __has_include
-# define QT_HAS_INCLUDE(x) __has_include(x)
-#else
-# define QT_HAS_INCLUDE(x) 0
-#endif
-#ifdef __has_include_next
-# define QT_HAS_INCLUDE_NEXT(x) __has_include_next(x)
-#else
-# define QT_HAS_INCLUDE_NEXT(x) 0
-#endif
-
-/*
* C++11 keywords and expressions
*/
#ifdef Q_COMPILER_NULLPTR
@@ -1124,7 +1129,7 @@
# define Q_DECL_ALIGN(n) alignas(n)
#endif
-#if QT_HAS_CPP_ATTRIBUTE(nodiscard) && !defined(Q_CC_CLANG) // P0188R1
+#if __has_cpp_attribute(nodiscard) && !defined(Q_CC_CLANG) // P0188R1
// Can't use [[nodiscard]] with Clang, see https://bugs.llvm.org/show_bug.cgi?id=33518
# undef Q_REQUIRED_RESULT
# define Q_REQUIRED_RESULT [[nodiscard]]
@@ -1226,11 +1231,6 @@
#ifndef QT_MAKE_CHECKED_ARRAY_ITERATOR
# define QT_MAKE_CHECKED_ARRAY_ITERATOR(x, N) (x)
#endif
-#ifdef __has_feature
-# define QT_HAS_FEATURE(x) __has_feature(x)
-#else
-# define QT_HAS_FEATURE(x) 0
-#endif
/*
* Warning/diagnostic handling
@@ -1321,11 +1321,11 @@
} while (false)
#if defined(__cplusplus)
-#if QT_HAS_CPP_ATTRIBUTE(clang::fallthrough)
+#if __has_cpp_attribute(clang::fallthrough)
# define Q_FALLTHROUGH() [[clang::fallthrough]]
-#elif QT_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
+#elif __has_cpp_attribute(gnu::fallthrough)
# define Q_FALLTHROUGH() [[gnu::fallthrough]]
-#elif QT_HAS_CPP_ATTRIBUTE(fallthrough)
+#elif __has_cpp_attribute(fallthrough)
# define Q_FALLTHROUGH() [[fallthrough]]
#endif
#endif
diff --git a/src/corelib/global/qconfig-bootstrapped.h b/src/corelib/global/qconfig-bootstrapped.h
index c58dc56c42..4a8a4598a0 100644
--- a/src/corelib/global/qconfig-bootstrapped.h
+++ b/src/corelib/global/qconfig-bootstrapped.h
@@ -75,15 +75,16 @@
# define QT_FEATURE_alloca_malloc_h -1
#endif
#define QT_FEATURE_binaryjson -1
-#define QT_FEATURE_cborstream -1
+#define QT_FEATURE_cborstreamreader -1
+#define QT_FEATURE_cborstreamwriter 1
#define QT_CRYPTOGRAPHICHASH_ONLY_SHA1
-#define QT_FEATURE_cxx11_random (QT_HAS_INCLUDE(<random>) ? 1 : -1)
+#define QT_FEATURE_cxx11_random (__has_include(<random>) ? 1 : -1)
#define QT_NO_DATASTREAM
#define QT_FEATURE_datestring 1
#define QT_FEATURE_datetimeparser -1
#define QT_FEATURE_easingcurve -1
#define QT_FEATURE_etw -1
-#define QT_FEATURE_getauxval (QT_HAS_INCLUDE(<sys/auxv.h>) ? 1 : -1)
+#define QT_FEATURE_getauxval (__has_include(<sys/auxv.h>) ? 1 : -1)
#define QT_FEATURE_getentropy -1
#define QT_NO_GEOM_VARIANT
#define QT_FEATURE_hijricalendar -1
diff --git a/src/corelib/global/qendian.cpp b/src/corelib/global/qendian.cpp
index c56a7ffbf7..c69572cccf 100644
--- a/src/corelib/global/qendian.cpp
+++ b/src/corelib/global/qendian.cpp
@@ -137,7 +137,7 @@ QT_BEGIN_NAMESPACE
\sa qToLittleEndian()
*/
/*!
- \fn template <typename T> T qFromLittleEndian(const void *src)
+ \fn template <typename T> inline T qFromLittleEndian(const void *src)
\since 4.3
\relates <QtEndian>
@@ -159,7 +159,7 @@ QT_BEGIN_NAMESPACE
\sa qToLittleEndian()
*/
/*!
- \fn template <typename T> T qFromLittleEndian(T src)
+ \fn template <typename T> inline T qFromLittleEndian(T src)
\since 4.3
\relates <QtEndian>
\overload
@@ -171,7 +171,7 @@ QT_BEGIN_NAMESPACE
unmodified.
*/
/*!
- \fn template <typename T> T qFromLittleEndian(const void *src, qsizetype count, void *dest)
+ \fn template <typename T> inline T qFromLittleEndian(const void *src, qsizetype count, void *dest)
\since 5.12
\relates <QtEndian>
@@ -192,7 +192,6 @@ QT_BEGIN_NAMESPACE
an in-place swap (if necessary). If they are not the same, the memory
regions must not overlap.
- \sa qFromLittleEndian()
\sa qToBigEndian()
\sa qToLittleEndian()
*/
diff --git a/src/corelib/global/qendian.h b/src/corelib/global/qendian.h
index 5cd9d3160b..257efbbdbe 100644
--- a/src/corelib/global/qendian.h
+++ b/src/corelib/global/qendian.h
@@ -66,7 +66,7 @@ template <typename T> Q_ALWAYS_INLINE void qToUnaligned(const T src, void *dest)
// Using sizeof(T) inside memcpy function produces internal compiler error with
// MSVC2008/ARM in tst_endian -> use extra indirection to resolve size of T.
const size_t size = sizeof(T);
-#if QT_HAS_BUILTIN(__builtin_memcpy)
+#if __has_builtin(__builtin_memcpy)
__builtin_memcpy
#else
memcpy
@@ -78,7 +78,7 @@ template <typename T> Q_ALWAYS_INLINE T qFromUnaligned(const void *src)
{
T dest;
const size_t size = sizeof(T);
-#if QT_HAS_BUILTIN(__builtin_memcpy)
+#if __has_builtin(__builtin_memcpy)
__builtin_memcpy
#else
memcpy
diff --git a/src/corelib/global/qflags.h b/src/corelib/global/qflags.h
index bd3c219968..4f46de5eaa 100644
--- a/src/corelib/global/qflags.h
+++ b/src/corelib/global/qflags.h
@@ -93,8 +93,10 @@ class QFlags
"long long will overflow.");
Q_STATIC_ASSERT_X((std::is_enum<Enum>::value), "QFlags is only usable on enumeration types.");
+#if QT_DEPRECATED_SINCE(5,15)
struct Private;
typedef int (Private::*Zero);
+#endif
template <typename E> friend QDataStream &operator>>(QDataStream &, QFlags<E> &);
template <typename E> friend QDataStream &operator<<(QDataStream &, QFlags<E>);
public:
@@ -115,8 +117,11 @@ public:
Q_DECL_CONSTEXPR inline QFlags(const QFlags &other);
Q_DECL_CONSTEXPR inline QFlags &operator=(const QFlags &other);
#endif
+ Q_DECL_CONSTEXPR inline QFlags() noexcept : i(0) {}
Q_DECL_CONSTEXPR inline QFlags(Enum flags) noexcept : i(Int(flags)) {}
- Q_DECL_CONSTEXPR inline QFlags(Zero = nullptr) noexcept : i(0) {}
+#if QT_DEPRECATED_SINCE(5,15)
+ QT_DEPRECATED_X("Use default constructor instead") Q_DECL_CONSTEXPR inline QFlags(Zero) noexcept : i(0) {}
+#endif
Q_DECL_CONSTEXPR inline QFlags(QFlag flag) noexcept : i(flag) {}
Q_DECL_CONSTEXPR inline QFlags(std::initializer_list<Enum> flags) noexcept
diff --git a/src/corelib/global/qfloat16.cpp b/src/corelib/global/qfloat16.cpp
index 6c21b7de5a..51766c9760 100644
--- a/src/corelib/global/qfloat16.cpp
+++ b/src/corelib/global/qfloat16.cpp
@@ -109,7 +109,7 @@ QT_BEGIN_NAMESPACE
/*!
\internal
\since 5.14
- bool qfloat16::isInf() const noexcept
+ \fn bool qfloat16::isInf() const noexcept
Tests whether this \c qfloat16 value is an infinity.
@@ -119,7 +119,7 @@ QT_BEGIN_NAMESPACE
/*!
\internal
\since 5.14
- bool qfloat16::isNaN() const noexcept
+ \fn bool qfloat16::isNaN() const noexcept
Tests whether this \c qfloat16 value is "not a number".
@@ -128,7 +128,7 @@ QT_BEGIN_NAMESPACE
/*!
\since 5.14
- bool qfloat16::isNormal() const noexcept
+ \fn bool qfloat16::isNormal() const noexcept
Tests whether this \c qfloat16 value is finite and in normal form.
@@ -138,7 +138,7 @@ QT_BEGIN_NAMESPACE
/*!
\internal
\since 5.14
- bool qfloat16::isFinite() const noexcept
+ \fn bool qfloat16::isFinite() const noexcept
Tests whether this \c qfloat16 value is finite.
@@ -146,6 +146,14 @@ QT_BEGIN_NAMESPACE
*/
/*!
+ \since 5.15
+ \fn qfloat16::copySign(qfloat16 sign) const noexcept
+
+ Returns a qfloat16 with the sign of \a sign but the rest of its value taken
+ from this qfloat16. Serves as qfloat16's equivalent of std::copysign().
+*/
+
+/*!
\internal
\since 5.14
Implements qFpClassify() for qfloat16.
diff --git a/src/corelib/global/qfloat16.h b/src/corelib/global/qfloat16.h
index 9a4f1800a4..f35eefd922 100644
--- a/src/corelib/global/qfloat16.h
+++ b/src/corelib/global/qfloat16.h
@@ -84,6 +84,9 @@ public:
bool isNaN() const noexcept { return ((b16 >> 8) & 0x7e) == 0x7e; }
bool isFinite() const noexcept { return ((b16 >> 8) & 0x7c) != 0x7c; }
Q_CORE_EXPORT int fpClassify() const noexcept;
+ // Can't specialize std::copysign() for qfloat16
+ qfloat16 copySign(qfloat16 sign) const noexcept
+ { return qfloat16(Wrap((sign.b16 & 0x8000) | (b16 & 0x7fff))); }
// Support for std::numeric_limits<qfloat16>
static constexpr qfloat16 _limit_epsilon() noexcept { return qfloat16(Wrap(0x1400)); }
static constexpr qfloat16 _limit_min() noexcept { return qfloat16(Wrap(0x400)); }
@@ -92,7 +95,9 @@ public:
static constexpr qfloat16 _limit_lowest() noexcept { return qfloat16(Wrap(0xfbff)); }
static constexpr qfloat16 _limit_infinity() noexcept { return qfloat16(Wrap(0x7c00)); }
static constexpr qfloat16 _limit_quiet_NaN() noexcept { return qfloat16(Wrap(0x7e00)); }
- // Signalling NaN is 0x7f00
+#if QT_CONFIG(signaling_nan)
+ static constexpr qfloat16 _limit_signaling_NaN() noexcept { return qfloat16(Wrap(0x7f00)); }
+#endif
inline constexpr bool isNormal() const noexcept
{ return (b16 & 0x7fff) == 0 || ((b16 & 0x7c00) && (b16 & 0x7c00) != 0x7c00); }
private:
@@ -239,6 +244,7 @@ QF16_MAKE_ARITH_OP_INT(/)
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
QT_WARNING_DISABLE_GCC("-Wfloat-equal")
+QT_WARNING_DISABLE_INTEL(1572)
inline bool operator>(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) > static_cast<float>(b); }
inline bool operator<(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) < static_cast<float>(b); }
@@ -327,6 +333,12 @@ public:
{ return QT_PREPEND_NAMESPACE(qfloat16)::_limit_infinity(); }
static constexpr QT_PREPEND_NAMESPACE(qfloat16) quiet_NaN()
{ return QT_PREPEND_NAMESPACE(qfloat16)::_limit_quiet_NaN(); }
+#if QT_CONFIG(signaling_nan)
+ static constexpr QT_PREPEND_NAMESPACE(qfloat16) signaling_NaN()
+ { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_signaling_NaN(); }
+#else
+ static constexpr bool has_signaling_NaN = false;
+#endif
};
template<> class numeric_limits<const QT_PREPEND_NAMESPACE(qfloat16)>
diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp
index 799d953c8d..ea91dee471 100644
--- a/src/corelib/global/qglobal.cpp
+++ b/src/corelib/global/qglobal.cpp
@@ -92,7 +92,7 @@
# include <sys/systeminfo.h>
#endif
-#if defined(Q_OS_DARWIN) && QT_HAS_INCLUDE(<IOKit/IOKitLib.h>)
+#if defined(Q_OS_DARWIN) && __has_include(<IOKit/IOKitLib.h>)
# include <IOKit/IOKitLib.h>
# include <private/qcore_mac_p.h>
#endif
@@ -139,10 +139,12 @@ Q_CORE_EXPORT void *qMemSet(void *dest, int c, size_t n);
// in. The idea here is to error or warn if otherwise implicit Qt
// assumptions are not fulfilled on new hardware or compilers
// (if this list becomes too long, consider factoring into a separate file)
-Q_STATIC_ASSERT_X(sizeof(int) == 4, "Qt assumes that int is 32 bits");
Q_STATIC_ASSERT_X(UCHAR_MAX == 255, "Qt assumes that char is 8 bits");
+Q_STATIC_ASSERT_X(sizeof(int) == 4, "Qt assumes that int is 32 bits");
Q_STATIC_ASSERT_X(QT_POINTER_SIZE == sizeof(void *), "QT_POINTER_SIZE defined incorrectly");
Q_STATIC_ASSERT_X(sizeof(float) == 4, "Qt assumes that float is 32 bits");
+Q_STATIC_ASSERT_X(sizeof(char16_t) == 2, "Qt assumes that char16_t is 16 bits");
+Q_STATIC_ASSERT_X(sizeof(char32_t) == 4, "Qt assumes that char32_t is 32 bits");
// While we'd like to check for __STDC_IEC_559__, as per ISO/IEC 9899:2011
// Annex F (C11, normative for C++11), there are a few corner cases regarding
@@ -250,7 +252,7 @@ Q_STATIC_ASSERT((std::is_same<qsizetype, qptrdiff>::value));
Qt::Alignment type is simply a typedef for
QFlags<Qt::AlignmentFlag>. QLabel::setAlignment() takes a
Qt::Alignment parameter, which means that any combination of
- Qt::AlignmentFlag values, or 0, is legal:
+ Qt::AlignmentFlag values, or \c{{ }}, is legal:
\snippet code/src_corelib_global_qglobal.cpp 0
@@ -318,10 +320,20 @@ Q_STATIC_ASSERT((std::is_same<qsizetype, qptrdiff>::value));
*/
/*!
+ \fn template <typename Enum> QFlags<Enum>::QFlags()
+ \since 5.15
+
+ Constructs a QFlags object with no flags set.
+*/
+
+/*!
\fn template <typename Enum> QFlags<Enum>::QFlags(Zero)
+ \deprecated
Constructs a QFlags object with no flags set. The parameter must be a
literal 0 value.
+
+ Deprecated, use default constructor instead.
*/
/*!
@@ -3057,7 +3069,7 @@ enum {
*/
QByteArray QSysInfo::machineUniqueId()
{
-#if defined(Q_OS_DARWIN) && QT_HAS_INCLUDE(<IOKit/IOKitLib.h>)
+#if defined(Q_OS_DARWIN) && __has_include(<IOKit/IOKitLib.h>)
char uuid[UuidStringLen + 1];
io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
QCFString stringRef = (CFStringRef)IORegistryEntryCreateCFProperty(service, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
@@ -4151,36 +4163,6 @@ bool qunsetenv(const char *varName)
*/
/*!
- \macro QABS(n)
- \relates <QtGlobal>
- \obsolete
-
- Use qAbs(\a n) instead.
-
- \sa QMIN(), QMAX()
-*/
-
-/*!
- \macro QMIN(x, y)
- \relates <QtGlobal>
- \obsolete
-
- Use qMin(\a x, \a y) instead.
-
- \sa QMAX(), QABS()
-*/
-
-/*!
- \macro QMAX(x, y)
- \relates <QtGlobal>
- \obsolete
-
- Use qMax(\a x, \a y) instead.
-
- \sa QMIN(), QABS()
-*/
-
-/*!
\macro const char *qPrintable(const QString &str)
\relates <QtGlobal>
@@ -4816,9 +4798,11 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
/*!
\macro qMove(x)
\relates <QtGlobal>
+ \obsolete
- It expands to "std::move" if your compiler supports that C++11 function, or to nothing
- otherwise.
+ Use \c std::move instead.
+
+ It expands to "std::move".
qMove takes an rvalue reference to its parameter \a x, and converts it to an xvalue.
*/
@@ -4919,6 +4903,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
/*!
\macro Q_DECL_OVERRIDE
\since 5.0
+ \obsolete
\relates <QtGlobal>
This macro can be used to declare an overriding virtual
@@ -4926,8 +4911,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
an error if the overriding virtual function does not in fact
override anything.
- It expands to "override" if your compiler supports that C++11
- contextual keyword, or to nothing otherwise.
+ It expands to "override".
The macro goes at the end of the function, usually after the
\c{const}, if any:
@@ -4939,6 +4923,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
/*!
\macro Q_DECL_FINAL
\since 5.0
+ \obsolete
\relates <QtGlobal>
This macro can be used to declare an overriding virtual or a class
@@ -4946,10 +4931,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
no longer override this virtual function, or inherit from this
class, respectively.
- It expands to "final" if your compiler supports that C++11
- contextual keyword, or something non-standard if your compiler
- supports something close enough to the C++11 semantics, or to
- nothing otherwise.
+ It expands to "final".
The macro goes at the end of the function, usually after the
\c{const}, if any:
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h
index 4dc63cae5e..c1fcc12cfb 100644
--- a/src/corelib/global/qglobal.h
+++ b/src/corelib/global/qglobal.h
@@ -717,7 +717,7 @@ inline void qt_noop(void) {}
#if !defined(QT_NO_EXCEPTIONS)
# if !defined(Q_MOC_RUN)
-# if (defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_FEATURE(cxx_exceptions)) || \
+# if (defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !__has_feature(cxx_exceptions)) || \
(defined(Q_CC_GNU) && !defined(__EXCEPTIONS))
# define QT_NO_EXCEPTIONS
# endif
@@ -892,6 +892,10 @@ QT_WARNING_POP
# define Q_DUMMY_COMPARISON_OPERATOR(C)
#endif
+QT_WARNING_PUSH
+// warning: noexcept-expression evaluates to 'false' because of a call to 'void swap(..., ...)'
+QT_WARNING_DISABLE_GCC("-Wnoexcept")
+
namespace QtPrivate
{
namespace SwapExceptionTester { // insulate users from the "using std::swap" below
@@ -911,6 +915,8 @@ inline void qSwap(T &value1, T &value2)
swap(value1, value2);
}
+QT_WARNING_POP
+
#if QT_DEPRECATED_SINCE(5, 0)
Q_CORE_EXPORT QT_DEPRECATED void *qMalloc(size_t size) Q_ALLOC_SIZE(1);
Q_CORE_EXPORT QT_DEPRECATED void qFree(void *ptr);
@@ -1214,8 +1220,10 @@ inline int qIntCast(float f) { return int(f); }
/*
Reentrant versions of basic rand() functions for random number generation
*/
-Q_CORE_EXPORT void qsrand(uint seed);
-Q_CORE_EXPORT int qrand();
+#if QT_DEPRECATED_SINCE(5, 15)
+Q_CORE_EXPORT QT_DEPRECATED_VERSION_X_5_15("use QRandomGenerator instead") void qsrand(uint seed);
+Q_CORE_EXPORT QT_DEPRECATED_VERSION_X_5_15("use QRandomGenerator instead") int qrand();
+#endif
#define QT_MODULE(x)
diff --git a/src/corelib/global/qglobal_p.h b/src/corelib/global/qglobal_p.h
index 58bc8b7bf3..5ab84fa8be 100644
--- a/src/corelib/global/qglobal_p.h
+++ b/src/corelib/global/qglobal_p.h
@@ -74,7 +74,7 @@ Q_CORE_EXPORT time_t qMkTime(struct tm *when);
QT_END_NAMESPACE
-#if !QT_HAS_BUILTIN(__builtin_available)
+#if !__has_builtin(__builtin_available)
#include <initializer_list>
#include <QtCore/qoperatingsystemversion.h>
#include <QtCore/qversionnumber.h>
@@ -142,7 +142,7 @@ QT_END_NAMESPACE
QT_BUILTIN_AVAILABLE1, \
QT_BUILTIN_AVAILABLE0, )
#define __builtin_available(...) QT_BUILTIN_AVAILABLE_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
-#endif // !QT_HAS_BUILTIN(__builtin_available)
+#endif // !__has_builtin(__builtin_available)
#endif // defined(__cplusplus)
#endif // QGLOBAL_P_H
diff --git a/src/corelib/global/qlibraryinfo.cpp b/src/corelib/global/qlibraryinfo.cpp
index da122f75e5..f8accab548 100644
--- a/src/corelib/global/qlibraryinfo.cpp
+++ b/src/corelib/global/qlibraryinfo.cpp
@@ -124,12 +124,12 @@ public:
QLibrarySettings *ls = qt_library_settings();
if (ls) {
#ifndef QT_BUILD_QMAKE
- if (ls->reloadOnQAppAvailable && QCoreApplication::instance() != 0)
+ if (ls->reloadOnQAppAvailable && QCoreApplication::instance() != nullptr)
ls->load();
#endif
return ls->settings.data();
} else {
- return 0;
+ return nullptr;
}
}
};
@@ -146,7 +146,7 @@ void QLibrarySettings::load()
// If we get any settings here, those won't change when the application shows up.
settings.reset(QLibraryInfoPrivate::findConfiguration());
#ifndef QT_BUILD_QMAKE
- reloadOnQAppAvailable = (settings.data() == 0 && QCoreApplication::instance() == 0);
+ reloadOnQAppAvailable = (settings.data() == nullptr && QCoreApplication::instance() == nullptr);
bool haveDevicePaths;
bool haveEffectivePaths;
bool havePaths;
@@ -169,7 +169,7 @@ void QLibrarySettings::load()
|| children.contains(QLatin1String("Paths"));
#ifndef QT_BUILD_QMAKE
if (!havePaths)
- settings.reset(0);
+ settings.reset(nullptr);
#else
} else {
haveDevicePaths = false;
@@ -212,7 +212,7 @@ QSettings *QLibraryInfoPrivate::findConfiguration()
return new QSettings(qtconfig, QSettings::IniFormat);
}
#endif
- return 0; //no luck
+ return nullptr; //no luck
}
#endif // settings
@@ -561,9 +561,39 @@ static QString getRelocatablePrefix()
HMODULE hModule = getWindowsModuleHandle();
const int kBufferSize = 4096;
wchar_t buffer[kBufferSize];
- const int pathSize = GetModuleFileName(hModule, buffer, kBufferSize);
- if (pathSize > 0)
- prefixPath = prefixFromQtCoreLibraryHelper(QString::fromWCharArray(buffer, pathSize));
+ DWORD pathSize = GetModuleFileName(hModule, buffer, kBufferSize);
+ const QString qtCoreFilePath = QString::fromWCharArray(buffer, int(pathSize));
+ const QString qtCoreDirPath = QFileInfo(qtCoreFilePath).absolutePath();
+ pathSize = GetModuleFileName(NULL, buffer, kBufferSize);
+ const QString exeDirPath = QFileInfo(QString::fromWCharArray(buffer, int(pathSize))).absolutePath();
+ if (QFileInfo(exeDirPath) == QFileInfo(qtCoreDirPath)) {
+ // QtCore DLL is next to the executable. This is either a windeployqt'ed executable or an
+ // executable within the QT_HOST_BIN directory. We're detecting the latter case by checking
+ // whether there's an import library corresponding to our QtCore DLL in PREFIX/lib.
+ const QString libdir = QString::fromLatin1(
+ qt_configure_strs + qt_configure_str_offsets[QLibraryInfo::LibrariesPath - 1]);
+ const QLatin1Char slash('/');
+#if defined(Q_CC_MINGW)
+ const QString implibPrefix = QStringLiteral("lib");
+ const QString implibSuffix = QStringLiteral(".a");
+#else
+ const QString implibPrefix;
+ const QString implibSuffix = QStringLiteral(".lib");
+#endif
+ const QString qtCoreImpLibFileName = implibPrefix
+ + QFileInfo(qtCoreFilePath).completeBaseName() + implibSuffix;
+ const QString qtCoreImpLibPath = qtCoreDirPath
+ + slash + QLatin1String(QT_CONFIGURE_LIBLOCATION_TO_PREFIX_PATH)
+ + slash + libdir
+ + slash + qtCoreImpLibFileName;
+ if (!QFileInfo::exists(qtCoreImpLibPath)) {
+ // We did not find a corresponding import library and conclude that this is a
+ // windeployqt'ed executable.
+ return exeDirPath;
+ }
+ }
+ if (!qtCoreFilePath.isEmpty())
+ prefixPath = prefixFromQtCoreLibraryHelper(qtCoreFilePath);
#else
#error "The chosen platform / config does not support querying for a dynamic prefix."
#endif
@@ -751,7 +781,7 @@ QLibraryInfo::rawLocation(LibraryLocation loc, PathGroup group)
// will binary-patch the Qt installation paths -- in such scenarios, Qt
// will be built with a dummy path, thus the compile-time result of
// strlen is meaningless.
- const char * volatile path = 0;
+ const char * volatile path = nullptr;
if (loc == PrefixPath) {
path = getPrefix(
#ifdef QT_BUILD_QMAKE
@@ -892,10 +922,10 @@ void qt_core_boilerplate()
"\n"
"Installation prefix: %s\n"
"Library path: %s\n"
- "Include path: %s\n",
+ "Plugin path: %s\n",
qt_configure_prefix_path_str + 12,
qt_configure_strs + qt_configure_str_offsets[QT_PREPEND_NAMESPACE(QLibraryInfo)::LibrariesPath - 1],
- qt_configure_strs + qt_configure_str_offsets[QT_PREPEND_NAMESPACE(QLibraryInfo)::HeadersPath - 1]);
+ qt_configure_strs + qt_configure_str_offsets[QT_PREPEND_NAMESPACE(QLibraryInfo)::PluginsPath - 1]);
QT_PREPEND_NAMESPACE(qDumpCPUFeatures)();
diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp
index 3a9ff1651b..ca4d8dbd11 100644
--- a/src/corelib/global/qlogging.cpp
+++ b/src/corelib/global/qlogging.cpp
@@ -70,7 +70,7 @@
#if QT_CONFIG(slog2)
#include <sys/slog2.h>
#endif
-#if QT_HAS_INCLUDE(<paths.h>)
+#if __has_include(<paths.h>)
#include <paths.h>
#endif
@@ -106,7 +106,7 @@
# if __UCLIBC_HAS_BACKTRACE__
# define QLOGGING_HAVE_BACKTRACE
# endif
-# elif (defined(__GLIBC__) && defined(__GLIBCXX__)) || (QT_HAS_INCLUDE(<cxxabi.h>) && QT_HAS_INCLUDE(<execinfo.h>))
+# elif (defined(__GLIBC__) && defined(__GLIBCXX__)) || (__has_include(<cxxabi.h>) && __has_include(<execinfo.h>))
# define QLOGGING_HAVE_BACKTRACE
# endif
#endif
@@ -116,7 +116,7 @@ extern char *__progname;
#endif
#ifndef QT_BOOTSTRAPPED
-#if defined(Q_OS_LINUX) && (defined(__GLIBC__) || QT_HAS_INCLUDE(<sys/syscall.h>))
+#if defined(Q_OS_LINUX) && (defined(__GLIBC__) || __has_include(<sys/syscall.h>))
# include <sys/syscall.h>
# if defined(Q_OS_ANDROID) && !defined(SYS_gettid)
@@ -1276,7 +1276,7 @@ void QMessagePattern::setPattern(const QString &pattern)
#if defined(QLOGGING_HAVE_BACKTRACE) && !defined(QT_BOOTSTRAPPED)
// make sure the function has "Message" in the name so the function is removed
-#if ((defined(Q_CC_GNU) && defined(QT_COMPILER_SUPPORTS_SIMD_ALWAYS)) || QT_HAS_ATTRIBUTE(optimize)) \
+#if ((defined(Q_CC_GNU) && defined(QT_COMPILER_SUPPORTS_SIMD_ALWAYS)) || __has_attribute(optimize)) \
&& !defined(Q_CC_INTEL) && !defined(Q_CC_CLANG)
// force skipping the frame pointer, to save the backtrace() function some work
__attribute__((optimize("omit-frame-pointer")))
@@ -1315,7 +1315,7 @@ static QStringList backtraceFramesForLogMessage(int frameCount)
if (function.startsWith(QLatin1String("_Z"))) {
QScopedPointer<char, QScopedPointerPodDeleter> demangled(
- abi::__cxa_demangle(function.toUtf8(), 0, 0, 0));
+ abi::__cxa_demangle(function.toUtf8(), nullptr, nullptr, nullptr));
if (demangled)
function = QString::fromUtf8(qCleanupFuncinfo(demangled.data()));
}
diff --git a/src/corelib/global/qnamespace.h b/src/corelib/global/qnamespace.h
index 047ed8e7b3..b9e981b4a6 100644
--- a/src/corelib/global/qnamespace.h
+++ b/src/corelib/global/qnamespace.h
@@ -41,6 +41,7 @@
#define QNAMESPACE_H
#include <QtCore/qglobal.h>
+#include <QtCore/qtmetamacros.h>
#if defined(__OBJC__) && !defined(__cplusplus)
# warning "File built in Objective-C mode (.m), but using Qt requires Objective-C++ (.mm)"
@@ -48,29 +49,10 @@
QT_BEGIN_NAMESPACE
-#if !defined(Q_QDOC) && !defined(Q_MOC_RUN)
struct QMetaObject;
-const QMetaObject *qt_getQtMetaObject() noexcept; // defined in qobject.h (which can't be included here)
-#define QT_Q_ENUM(ENUM) \
- inline const QMetaObject *qt_getEnumMetaObject(ENUM) noexcept { return qt_getQtMetaObject(); } \
- inline Q_DECL_CONSTEXPR const char *qt_getEnumName(ENUM) noexcept { return #ENUM; }
-#define QT_Q_FLAG(ENUM) QT_Q_ENUM(ENUM)
-#else
-#define QT_Q_ENUM Q_ENUM
-#define QT_Q_FLAG Q_FLAG
-#endif
-
-#ifndef Q_MOC_RUN
-namespace
-#else
-class Q_CORE_EXPORT
-#endif
-Qt {
-#if defined(Q_MOC_RUN)
- Q_OBJECT
-public:
-#endif
+namespace Qt {
+ Q_NAMESPACE_EXPORT(Q_CORE_EXPORT)
enum GlobalColor {
color0,
@@ -523,6 +505,7 @@ public:
AA_DontUseNativeMenuBar = 6,
AA_MacDontSwapCtrlAndMeta = 7,
AA_Use96Dpi = 8,
+ AA_MSWindowsDisableVirtualKeyboard = 9,
#if QT_DEPRECATED_SINCE(5, 14)
AA_X11InitThreads Q_DECL_ENUMERATOR_DEPRECATED = 10,
#endif
@@ -1767,102 +1750,96 @@ public:
};
#ifndef Q_QDOC
- // NOTE: Generally, do not add QT_Q_ENUM if a corresponding Q_Q_FLAG exists.
- QT_Q_ENUM(ScrollBarPolicy)
- QT_Q_ENUM(FocusPolicy)
- QT_Q_ENUM(ContextMenuPolicy)
- QT_Q_ENUM(ArrowType)
- QT_Q_ENUM(ToolButtonStyle)
- QT_Q_ENUM(PenStyle)
- QT_Q_ENUM(PenCapStyle)
- QT_Q_ENUM(PenJoinStyle)
- QT_Q_ENUM(BrushStyle)
- QT_Q_ENUM(FillRule)
- QT_Q_ENUM(MaskMode)
- QT_Q_ENUM(BGMode)
- QT_Q_ENUM(ClipOperation)
- QT_Q_ENUM(SizeMode)
- QT_Q_ENUM(Axis)
- QT_Q_ENUM(Corner)
- QT_Q_ENUM(Edge)
- QT_Q_ENUM(LayoutDirection)
- QT_Q_ENUM(SizeHint)
- QT_Q_ENUM(Orientation)
- QT_Q_ENUM(DropAction)
- QT_Q_FLAG(Alignment)
- QT_Q_ENUM(TextFlag)
- QT_Q_FLAG(Orientations)
- QT_Q_FLAG(SplitBehavior)
- QT_Q_FLAG(DropActions)
- QT_Q_FLAG(Edges)
- QT_Q_FLAG(DockWidgetAreas)
- QT_Q_FLAG(ToolBarAreas)
- QT_Q_ENUM(DockWidgetArea)
- QT_Q_ENUM(ToolBarArea)
- QT_Q_ENUM(TextFormat)
- QT_Q_ENUM(TextElideMode)
- QT_Q_ENUM(DateFormat)
- QT_Q_ENUM(TimeSpec)
- QT_Q_ENUM(DayOfWeek)
- QT_Q_ENUM(CursorShape)
- QT_Q_ENUM(GlobalColor)
- QT_Q_ENUM(AspectRatioMode)
- QT_Q_ENUM(TransformationMode)
- QT_Q_FLAG(ImageConversionFlags)
- QT_Q_ENUM(Key)
- QT_Q_ENUM(ShortcutContext)
- QT_Q_ENUM(TextInteractionFlag)
- QT_Q_FLAG(TextInteractionFlags)
- QT_Q_ENUM(ItemSelectionMode)
- QT_Q_ENUM(ItemSelectionOperation)
- QT_Q_FLAG(ItemFlags)
- QT_Q_ENUM(CheckState)
- QT_Q_ENUM(ItemDataRole)
- QT_Q_ENUM(SortOrder)
- QT_Q_ENUM(CaseSensitivity)
- QT_Q_FLAG(MatchFlags)
- QT_Q_FLAG(KeyboardModifiers)
- QT_Q_FLAG(MouseButtons)
- QT_Q_ENUM(WindowType)
- QT_Q_ENUM(WindowState)
- QT_Q_ENUM(WindowModality)
- QT_Q_ENUM(WidgetAttribute)
- QT_Q_ENUM(ApplicationAttribute)
- QT_Q_FLAG(WindowFlags)
- QT_Q_FLAG(WindowStates)
- QT_Q_ENUM(FocusReason)
- QT_Q_ENUM(InputMethodHint)
- QT_Q_ENUM(InputMethodQuery)
- QT_Q_FLAG(InputMethodHints)
- QT_Q_ENUM(EnterKeyType)
- QT_Q_FLAG(InputMethodQueries)
- QT_Q_FLAG(TouchPointStates)
- QT_Q_ENUM(ScreenOrientation)
- QT_Q_FLAG(ScreenOrientations)
- QT_Q_ENUM(ConnectionType)
- QT_Q_ENUM(ApplicationState)
+ // NOTE: Generally, do not add Q_ENUM_NS if a corresponding Q_FLAG_NS exists.
+ Q_ENUM_NS(ScrollBarPolicy)
+ Q_ENUM_NS(FocusPolicy)
+ Q_ENUM_NS(ContextMenuPolicy)
+ Q_ENUM_NS(ArrowType)
+ Q_ENUM_NS(ToolButtonStyle)
+ Q_ENUM_NS(PenStyle)
+ Q_ENUM_NS(PenCapStyle)
+ Q_ENUM_NS(PenJoinStyle)
+ Q_ENUM_NS(BrushStyle)
+ Q_ENUM_NS(FillRule)
+ Q_ENUM_NS(MaskMode)
+ Q_ENUM_NS(BGMode)
+ Q_ENUM_NS(ClipOperation)
+ Q_ENUM_NS(SizeMode)
+ Q_ENUM_NS(Axis)
+ Q_ENUM_NS(Corner)
+ Q_ENUM_NS(Edge)
+ Q_ENUM_NS(LayoutDirection)
+ Q_ENUM_NS(SizeHint)
+ Q_ENUM_NS(Orientation)
+ Q_ENUM_NS(DropAction)
+ Q_FLAG_NS(Alignment)
+ Q_ENUM_NS(TextFlag)
+ Q_FLAG_NS(Orientations)
+ Q_FLAG_NS(SplitBehavior)
+ Q_FLAG_NS(DropActions)
+ Q_FLAG_NS(Edges)
+ Q_FLAG_NS(DockWidgetAreas)
+ Q_FLAG_NS(ToolBarAreas)
+ Q_ENUM_NS(DockWidgetArea)
+ Q_ENUM_NS(ToolBarArea)
+ Q_ENUM_NS(TextFormat)
+ Q_ENUM_NS(TextElideMode)
+ Q_ENUM_NS(DateFormat)
+ Q_ENUM_NS(TimeSpec)
+ Q_ENUM_NS(DayOfWeek)
+ Q_ENUM_NS(CursorShape)
+ Q_ENUM_NS(GlobalColor)
+ Q_ENUM_NS(AspectRatioMode)
+ Q_ENUM_NS(TransformationMode)
+ Q_FLAG_NS(ImageConversionFlags)
+ Q_ENUM_NS(Key)
+ Q_ENUM_NS(ShortcutContext)
+ Q_ENUM_NS(TextInteractionFlag)
+ Q_FLAG_NS(TextInteractionFlags)
+ Q_ENUM_NS(ItemSelectionMode)
+ Q_ENUM_NS(ItemSelectionOperation)
+ Q_FLAG_NS(ItemFlags)
+ Q_ENUM_NS(CheckState)
+ Q_ENUM_NS(ItemDataRole)
+ Q_ENUM_NS(SortOrder)
+ Q_ENUM_NS(CaseSensitivity)
+ Q_FLAG_NS(MatchFlags)
+ Q_FLAG_NS(KeyboardModifiers)
+ Q_FLAG_NS(MouseButtons)
+ Q_ENUM_NS(WindowType)
+ Q_ENUM_NS(WindowState)
+ Q_ENUM_NS(WindowModality)
+ Q_ENUM_NS(WidgetAttribute)
+ Q_ENUM_NS(ApplicationAttribute)
+ Q_FLAG_NS(WindowFlags)
+ Q_FLAG_NS(WindowStates)
+ Q_ENUM_NS(FocusReason)
+ Q_ENUM_NS(InputMethodHint)
+ Q_ENUM_NS(InputMethodQuery)
+ Q_FLAG_NS(InputMethodHints)
+ Q_ENUM_NS(EnterKeyType)
+ Q_FLAG_NS(InputMethodQueries)
+ Q_FLAG_NS(TouchPointStates)
+ Q_ENUM_NS(ScreenOrientation)
+ Q_FLAG_NS(ScreenOrientations)
+ Q_ENUM_NS(ConnectionType)
+ Q_ENUM_NS(ApplicationState)
#ifndef QT_NO_GESTURES
- QT_Q_ENUM(GestureState)
- QT_Q_ENUM(GestureType)
- QT_Q_ENUM(NativeGestureType)
+ Q_ENUM_NS(GestureState)
+ Q_ENUM_NS(GestureType)
+ Q_ENUM_NS(NativeGestureType)
#endif
- QT_Q_ENUM(CursorMoveStyle)
- QT_Q_ENUM(TimerType)
- QT_Q_ENUM(ScrollPhase)
- QT_Q_ENUM(MouseEventSource)
- QT_Q_FLAG(MouseEventFlag)
- QT_Q_ENUM(ChecksumType)
- QT_Q_ENUM(HighDpiScaleFactorRoundingPolicy)
- QT_Q_ENUM(TabFocusBehavior)
+ Q_ENUM_NS(CursorMoveStyle)
+ Q_ENUM_NS(TimerType)
+ Q_ENUM_NS(ScrollPhase)
+ Q_ENUM_NS(MouseEventSource)
+ Q_FLAG_NS(MouseEventFlag)
+ Q_ENUM_NS(ChecksumType)
+ Q_ENUM_NS(HighDpiScaleFactorRoundingPolicy)
+ Q_ENUM_NS(TabFocusBehavior)
#endif // Q_DOC
}
-#ifdef Q_MOC_RUN
- ;
-#endif
-
-#undef QT_Q_ENUM
-#undef QT_Q_FLAG
typedef bool (*qInternalCallback)(void **);
diff --git a/src/corelib/global/qnamespace.qdoc b/src/corelib/global/qnamespace.qdoc
index 9896cf6e02..6149281904 100644
--- a/src/corelib/global/qnamespace.qdoc
+++ b/src/corelib/global/qnamespace.qdoc
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
@@ -293,6 +293,12 @@
This attribute must be set before QGuiApplication is constructed.
This value was added in 5.13
+ \value AA_MSWindowsDisableVirtualKeyboard When this attribute is set,
+ Windows' native on-screen virtual keyboard will not be shown
+ automatically when a text input widget gains focus on a system
+ without a physical keyboard.
+ This value was added in 5.15
+
The following values are deprecated or obsolete:
\value AA_ImmediateWidgetCreation This attribute is no longer fully
@@ -724,9 +730,17 @@
\value LocalDate \e{This enum value is deprecated.} Use Qt::SystemLocaleShortDate
instead (or Qt::SystemLocaleLongDate if you want long dates).
- \value RFC2822Date \l{RFC 2822}, \l{RFC 850} and \l{RFC 1036} format: either
- \c{[ddd,] dd MMM yyyy hh:mm[:ss] +/-TZ} or \c{ddd MMM dd yyyy hh:mm[:ss] +/-TZ}
- for combined dates and times.
+ \value RFC2822Date \l{RFC 2822}, \l{RFC 850} and \l{RFC 1036} format:
+ either \c{[ddd,] dd MMM yyyy [hh:mm[:ss]][ ±tzoff]}
+ or \c{ddd MMM dd[ hh:mm:ss] yyyy[ ±tzoff]} are recognized for combined dates
+ and times, where \c{tzoff} is a timezone offset in \c{hhmm} format. For
+ dates and times separately, the same formats are matched and the unwanted
+ parts are ignored. In particular, note that a time is not recognized without
+ an accompanying date. When converting dates to string form,
+ format \c{dd MMM yyyy} is used, for times the format is \c{hh:mm:ss}. For
+ combined date and time, these are combined
+ as \c{dd MMM yyyy hh:mm:ss ±tzoff} (omitting the optional leading day of the
+ week from the first format recognized).
\note For \c ISODate formats, each \c Y, \c M and \c D represents a single digit
of the year, month and day used to specify the date. Each \c H, \c M and \c S
diff --git a/src/corelib/global/qnumeric_p.h b/src/corelib/global/qnumeric_p.h
index c006296b3d..7a03e32699 100644
--- a/src/corelib/global/qnumeric_p.h
+++ b/src/corelib/global/qnumeric_p.h
@@ -253,7 +253,8 @@ QT_WARNING_POP
// size_t. Implementations for 8- and 16-bit types will work but may not be as
// efficient. Implementations for 64-bit may be missing on 32-bit platforms.
-#if (defined(Q_CC_GNU) && (Q_CC_GNU >= 500) || (defined(Q_CC_INTEL) && !defined(Q_OS_WIN))) || QT_HAS_BUILTIN(__builtin_add_overflow)
+#if ((defined(Q_CC_INTEL) ? (Q_CC_INTEL >= 1800 && !defined(Q_OS_WIN)) : defined(Q_CC_GNU)) \
+ && Q_CC_GNU >= 500) || __has_builtin(__builtin_add_overflow)
// GCC 5, ICC 18, and Clang 3.8 have builtins to detect overflows
template <typename T> inline
diff --git a/src/corelib/global/qoperatingsystemversion.cpp b/src/corelib/global/qoperatingsystemversion.cpp
index 33793ca168..261f5c8795 100644
--- a/src/corelib/global/qoperatingsystemversion.cpp
+++ b/src/corelib/global/qoperatingsystemversion.cpp
@@ -299,6 +299,8 @@ int QOperatingSystemVersion::compare(const QOperatingSystemVersion &v1,
Returns the current OS type without constructing a QOperatingSystemVersion instance.
+ \since 5.10
+
\sa current()
*/
diff --git a/src/corelib/global/qrandom.cpp b/src/corelib/global/qrandom.cpp
index 563e5eb7ed..3dc024e301 100644
--- a/src/corelib/global/qrandom.cpp
+++ b/src/corelib/global/qrandom.cpp
@@ -1295,7 +1295,7 @@ void qsrand(uint seed)
\note This function is deprecated. In new applications, use
QRandomGenerator instead.
- \sa qrand(), QRandomGenerator
+ \sa qsrand(), QRandomGenerator
*/
int qrand()
{
diff --git a/src/corelib/io/io.pri b/src/corelib/io/io.pri
index fe81689932..c4c6f41387 100644
--- a/src/corelib/io/io.pri
+++ b/src/corelib/io/io.pri
@@ -136,6 +136,7 @@ qtConfig(settings) {
} else: darwin:!nacl {
SOURCES += io/qsettings_mac.cpp
}
+ wasm : SOURCES += io/qsettings_wasm.cpp
}
win32 {
diff --git a/src/corelib/io/qabstractfileengine.cpp b/src/corelib/io/qabstractfileengine.cpp
index 8a1679c5af..070139b608 100644
--- a/src/corelib/io/qabstractfileengine.cpp
+++ b/src/corelib/io/qabstractfileengine.cpp
@@ -658,7 +658,7 @@ QStringList QAbstractFileEngine::entryList(QDir::Filters filters, const QStringL
QAbstractFileEngine::FileFlags QAbstractFileEngine::fileFlags(FileFlags type) const
{
Q_UNUSED(type);
- return nullptr;
+ return {};
}
/*!
diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp
index fae935fc24..526702d151 100644
--- a/src/corelib/io/qdir.cpp
+++ b/src/corelib/io/qdir.cpp
@@ -2372,8 +2372,9 @@ static QString qt_cleanPath(const QString &path, bool *ok)
}
/*!
- Returns \a path with directory separators normalized (converted to "/") and
- redundant ones removed, and "."s and ".."s resolved (as far as possible).
+ Returns \a path with directory separators normalized (that is, platform-native
+ separators converted to "/") and redundant ones removed, and "."s and ".."s
+ resolved (as far as possible).
Symbolic links are kept. This function does not return the
canonical path, but rather the simplest version of the input.
diff --git a/src/corelib/io/qdiriterator.cpp b/src/corelib/io/qdiriterator.cpp
index ce436b06e3..1cf6b1be08 100644
--- a/src/corelib/io/qdiriterator.cpp
+++ b/src/corelib/io/qdiriterator.cpp
@@ -181,7 +181,7 @@ QDirIteratorPrivate::QDirIteratorPrivate(const QFileSystemEntry &entry, const QS
#elif QT_CONFIG(regularexpression)
nameRegExps.reserve(nameFilters.size());
for (const auto &filter : nameFilters) {
- QString re = QRegularExpression::anchoredPattern(QRegularExpression::wildcardToRegularExpression(filter));
+ QString re = QRegularExpression::wildcardToRegularExpression(filter);
nameRegExps.append(
QRegularExpression(re, (filters & QDir::CaseSensitive) ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption));
}
diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp
index 95f03ef816..5320ae2986 100644
--- a/src/corelib/io/qfile.cpp
+++ b/src/corelib/io/qfile.cpp
@@ -251,7 +251,7 @@ QFile::QFile(QFilePrivate &dd)
Constructs a QFile object.
*/
QFile::QFile()
- : QFileDevice(*new QFilePrivate, 0)
+ : QFileDevice(*new QFilePrivate, nullptr)
{
}
/*!
@@ -265,7 +265,7 @@ QFile::QFile(QObject *parent)
Constructs a new file object to represent the file with the given \a name.
*/
QFile::QFile(const QString &name)
- : QFileDevice(*new QFilePrivate, 0)
+ : QFileDevice(*new QFilePrivate, nullptr)
{
Q_D(QFile);
d->fileName = name;
diff --git a/src/corelib/io/qfile.h b/src/corelib/io/qfile.h
index cf1465ec70..2099b2852f 100644
--- a/src/corelib/io/qfile.h
+++ b/src/corelib/io/qfile.h
@@ -84,6 +84,10 @@ public:
// note: duplicated in qglobal.cpp (qEnvironmentVariable)
return QString::fromUtf8(localFileName).normalized(QString::NormalizationForm_C);
}
+ static inline QString decodeName(const char *localFileName)
+ {
+ return QString::fromUtf8(localFileName).normalized(QString::NormalizationForm_C);
+ }
#else
static inline QByteArray encodeName(const QString &fileName)
{
@@ -93,9 +97,11 @@ public:
{
return QString::fromLocal8Bit(localFileName);
}
+ static inline QString decodeName(const char *localFileName)
+ {
+ return QString::fromLocal8Bit(localFileName);
+ }
#endif
- inline static QString decodeName(const char *localFileName)
- { return decodeName(QByteArray(localFileName)); }
#if QT_DEPRECATED_SINCE(5,0)
typedef QByteArray (*EncoderFn)(const QString &fileName);
diff --git a/src/corelib/io/qfiledevice.cpp b/src/corelib/io/qfiledevice.cpp
index ee619d99cc..b0aba3193c 100644
--- a/src/corelib/io/qfiledevice.cpp
+++ b/src/corelib/io/qfiledevice.cpp
@@ -202,7 +202,7 @@ QFileDevice::QFileDevice(QFileDevicePrivate &dd)
\internal
*/
QFileDevice::QFileDevice()
- : QIODevice(*new QFileDevicePrivate, 0)
+ : QIODevice(*new QFileDevicePrivate, nullptr)
{
}
/*!
diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp
index 89834de29f..3fe1aec41f 100644
--- a/src/corelib/io/qfileinfo.cpp
+++ b/src/corelib/io/qfileinfo.cpp
@@ -134,7 +134,7 @@ uint QFileInfoPrivate::getFileFlags(QAbstractFileEngine::FileFlags request) cons
// extra syscall. Bundle detecton on Mac can be slow, expecially on network
// paths, so we separate out that as well.
- QAbstractFileEngine::FileFlags req = nullptr;
+ QAbstractFileEngine::FileFlags req;
uint cachedFlags = 0;
if (request & (QAbstractFileEngine::FlagsMask | QAbstractFileEngine::TypesMask)) {
@@ -1145,6 +1145,25 @@ bool QFileInfo::isShortcut() const
[d]() { return d->getFileFlags(QAbstractFileEngine::LinkType); });
}
+
+/*!
+ Returns \c true if the object points to a junction;
+ otherwise returns \c false.
+
+ Junctions only exist on Windows' NTFS file system, and are typically
+ created by the \c{mklink} command. They can be thought of as symlinks for
+ directories, and can only be created for absolute paths on the local
+ volume.
+*/
+bool QFileInfo::isJunction() const
+{
+ Q_D(const QFileInfo);
+ return d->checkAttribute<bool>(
+ QFileSystemMetaData::LegacyLinkType,
+ [d]() { return d->metaData.isJunction(); },
+ [d]() { return d->getFileFlags(QAbstractFileEngine::LinkType); });
+}
+
/*!
Returns \c true if the object points to a directory or to a symbolic
link to a directory, and that directory is the root directory; otherwise
diff --git a/src/corelib/io/qfileinfo.h b/src/corelib/io/qfileinfo.h
index 3ac028085a..7c7ff56ae4 100644
--- a/src/corelib/io/qfileinfo.h
+++ b/src/corelib/io/qfileinfo.h
@@ -113,6 +113,7 @@ public:
bool isSymLink() const;
bool isSymbolicLink() const;
bool isShortcut() const;
+ bool isJunction() const;
bool isRoot() const;
bool isBundle() const;
diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp
index c3abec8989..3bbebc7fe9 100644
--- a/src/corelib/io/qfilesystemengine_unix.cpp
+++ b/src/corelib/io/qfilesystemengine_unix.cpp
@@ -55,7 +55,7 @@
#include <stdio.h>
#include <errno.h>
-#if QT_HAS_INCLUDE(<paths.h>)
+#if __has_include(<paths.h>)
# include <paths.h>
#endif
#ifndef _PATH_TMP // from <paths.h>
@@ -1076,14 +1076,14 @@ bool QFileSystemEngine::cloneFile(int srcfd, int dstfd, const QFileSystemMetaDat
// sendfile(2) is limited in the kernel to 2G - 4k
const size_t SendfileSize = 0x7ffff000;
- ssize_t n = ::sendfile(dstfd, srcfd, NULL, SendfileSize);
+ ssize_t n = ::sendfile(dstfd, srcfd, nullptr, SendfileSize);
if (n == -1) {
// if we got an error here, give up and try at an upper layer
return false;
}
while (n) {
- n = ::sendfile(dstfd, srcfd, NULL, SendfileSize);
+ n = ::sendfile(dstfd, srcfd, nullptr, SendfileSize);
if (n == -1) {
// uh oh, this is probably a real error (like ENOSPC), but we have
// no way to notify QFile of partial success, so just erase any work
diff --git a/src/corelib/io/qfilesystemmetadata_p.h b/src/corelib/io/qfilesystemmetadata_p.h
index 81f4b3ba13..3154658e5c 100644
--- a/src/corelib/io/qfilesystemmetadata_p.h
+++ b/src/corelib/io/qfilesystemmetadata_p.h
@@ -76,8 +76,7 @@ class Q_AUTOTEST_EXPORT QFileSystemMetaData
{
public:
QFileSystemMetaData()
- : knownFlagsMask(nullptr),
- size_(-1)
+ : size_(-1)
{
}
@@ -111,8 +110,10 @@ public:
AliasType = 0x0,
#endif
#if defined(Q_OS_WIN)
+ JunctionType = 0x04000000,
WinLnkType = 0x08000000, // Note: Uses the same position for AliasType on Mac
#else
+ JunctionType = 0x0,
WinLnkType = 0x0,
#endif
SequentialType = 0x00800000, // Note: overlaps with QAbstractFileEngine::RootFlag
@@ -184,7 +185,7 @@ public:
void clear()
{
- knownFlagsMask = nullptr;
+ knownFlagsMask = {};
}
void clearFlags(MetaDataFlags flags = AllMetaDataFlags)
@@ -205,8 +206,10 @@ public:
bool wasDeleted() const { return (entryFlags & WasDeletedAttribute); }
#if defined(Q_OS_WIN)
bool isLnkFile() const { return (entryFlags & WinLnkType); }
+ bool isJunction() const { return (entryFlags & JunctionType); }
#else
bool isLnkFile() const { return false; }
+ bool isJunction() const { return false; }
#endif
qint64 size() const { return size_; }
@@ -356,9 +359,15 @@ inline void QFileSystemMetaData::fillFromFindData(WIN32_FIND_DATA &findData, boo
if (setLinkType) {
knownFlagsMask |= LinkType;
entryFlags &= ~LinkType;
- if ((fileAttribute_ & FILE_ATTRIBUTE_REPARSE_POINT)
- && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
- entryFlags |= LinkType;
+ if (fileAttribute_ & FILE_ATTRIBUTE_REPARSE_POINT) {
+ if (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
+ entryFlags |= LinkType;
+#if defined(IO_REPARSE_TAG_MOUNT_POINT)
+ } else if ((fileAttribute_ & FILE_ATTRIBUTE_DIRECTORY)
+ && (findData.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT)) {
+ entryFlags |= JunctionType;
+#endif
+ }
}
}
}
diff --git a/src/corelib/io/qfilesystemwatcher.cpp b/src/corelib/io/qfilesystemwatcher.cpp
index 54460aff77..86c8963cb6 100644
--- a/src/corelib/io/qfilesystemwatcher.cpp
+++ b/src/corelib/io/qfilesystemwatcher.cpp
@@ -88,7 +88,7 @@ QFileSystemWatcherEngine *QFileSystemWatcherPrivate::createNativeEngine(QObject
}
QFileSystemWatcherPrivate::QFileSystemWatcherPrivate()
- : native(0), poller(0)
+ : native(nullptr), poller(nullptr)
{
}
diff --git a/src/corelib/io/qfilesystemwatcher_inotify.cpp b/src/corelib/io/qfilesystemwatcher_inotify.cpp
index ca1f6cc359..888af998a5 100644
--- a/src/corelib/io/qfilesystemwatcher_inotify.cpp
+++ b/src/corelib/io/qfilesystemwatcher_inotify.cpp
@@ -242,7 +242,7 @@ QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create(QObject
if (fd == -1) {
fd = inotify_init();
if (fd == -1)
- return 0;
+ return nullptr;
}
return new QInotifyFileSystemWatcherEngine(fd, parent);
}
diff --git a/src/corelib/io/qfilesystemwatcher_win.cpp b/src/corelib/io/qfilesystemwatcher_win.cpp
index 1d42dbfc70..f955e3b53a 100644
--- a/src/corelib/io/qfilesystemwatcher_win.cpp
+++ b/src/corelib/io/qfilesystemwatcher_win.cpp
@@ -403,6 +403,7 @@ QStringList QWindowsFileSystemWatcherEngine::addPaths(const QStringList &paths,
const QString absolutePath = isDir ? fileInfo.absoluteFilePath() : fileInfo.absolutePath();
const uint flags = isDir
? (FILE_NOTIFY_CHANGE_DIR_NAME
+ | FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_FILE_NAME)
: (FILE_NOTIFY_CHANGE_DIR_NAME
| FILE_NOTIFY_CHANGE_FILE_NAME
diff --git a/src/corelib/io/qfsfileengine_win.cpp b/src/corelib/io/qfsfileengine_win.cpp
index 5b868cc447..dd4882a2bc 100644
--- a/src/corelib/io/qfsfileengine_win.cpp
+++ b/src/corelib/io/qfsfileengine_win.cpp
@@ -591,14 +591,14 @@ QAbstractFileEngine::FileFlags QFSFileEngine::fileFlags(QAbstractFileEngine::Fil
if (type & Refresh)
d->metaData.clear();
- QAbstractFileEngine::FileFlags ret = 0;
+ QAbstractFileEngine::FileFlags ret;
if (type & FlagsMask)
ret |= LocalDiskFlag;
bool exists;
{
- QFileSystemMetaData::MetaDataFlags queryFlags = 0;
+ QFileSystemMetaData::MetaDataFlags queryFlags;
queryFlags |= QFileSystemMetaData::MetaDataFlags(uint(type))
& QFileSystemMetaData::Permissions;
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp
index e26508e631..b89cab5e3c 100644
--- a/src/corelib/io/qiodevice.cpp
+++ b/src/corelib/io/qiodevice.cpp
@@ -461,7 +461,7 @@ QIODevice::QIODevice(QIODevicePrivate &dd)
*/
QIODevice::QIODevice()
- : QObject(*new QIODevicePrivate, 0)
+ : QObject(*new QIODevicePrivate, nullptr)
{
#if defined QIODEVICE_DEBUG
QFile *file = qobject_cast<QFile *>(this);
diff --git a/src/corelib/io/qloggingcategory.cpp b/src/corelib/io/qloggingcategory.cpp
index 4d4784cdf7..5de8be116c 100644
--- a/src/corelib/io/qloggingcategory.cpp
+++ b/src/corelib/io/qloggingcategory.cpp
@@ -179,7 +179,7 @@ static void setBoolLane(QBasicAtomicInt *atomic, bool enable, int shift)
The \c QtProject/qtlogging.ini file is looked up in all directories returned
by QStandardPaths::GenericConfigLocation.
- Set the \c QT_LOGGING_DEBUG environment variable to find out where you logging
+ Set the \c QT_LOGGING_DEBUG environment variable to find out where your logging
rules are loaded from.
\section2 Installing a Custom Filter
diff --git a/src/corelib/io/qnoncontiguousbytedevice.cpp b/src/corelib/io/qnoncontiguousbytedevice.cpp
index d1806aa12b..df0197e8eb 100644
--- a/src/corelib/io/qnoncontiguousbytedevice.cpp
+++ b/src/corelib/io/qnoncontiguousbytedevice.cpp
@@ -127,7 +127,7 @@ QT_BEGIN_NAMESPACE
\internal
*/
-QNonContiguousByteDevice::QNonContiguousByteDevice() : QObject((QObject*)0)
+QNonContiguousByteDevice::QNonContiguousByteDevice() : QObject((QObject*)nullptr)
{
}
@@ -188,7 +188,7 @@ const char* QNonContiguousByteDeviceByteArrayImpl::readPointer(qint64 maximumLen
{
if (atEnd()) {
len = -1;
- return 0;
+ return nullptr;
}
if (maximumLength != -1)
@@ -241,7 +241,7 @@ const char* QNonContiguousByteDeviceRingBufferImpl::readPointer(qint64 maximumLe
{
if (atEnd()) {
len = -1;
- return 0;
+ return nullptr;
}
const char *returnValue = ringBuffer->readPointerAtPosition(currentPosition, len);
@@ -282,7 +282,7 @@ qint64 QNonContiguousByteDeviceRingBufferImpl::size() const
QNonContiguousByteDeviceIoDeviceImpl::QNonContiguousByteDeviceIoDeviceImpl(QIODevice *d)
: QNonContiguousByteDevice(),
- currentReadBuffer(0), currentReadBufferSize(16*1024),
+ currentReadBuffer(nullptr), currentReadBufferSize(16*1024),
currentReadBufferAmount(0), currentReadBufferPosition(0), totalAdvancements(0),
eof(false)
{
@@ -301,10 +301,10 @@ const char* QNonContiguousByteDeviceIoDeviceImpl::readPointer(qint64 maximumLeng
{
if (eof == true) {
len = -1;
- return 0;
+ return nullptr;
}
- if (currentReadBuffer == 0)
+ if (currentReadBuffer == nullptr)
currentReadBuffer = new QByteArray(currentReadBufferSize, '\0'); // lazy alloc
if (maximumLength == -1)
@@ -323,7 +323,7 @@ const char* QNonContiguousByteDeviceIoDeviceImpl::readPointer(qint64 maximumLeng
// size was unknown before, emit a readProgress with the final size
if (size() == -1)
emit readProgress(totalAdvancements, totalAdvancements);
- return 0;
+ return nullptr;
}
currentReadBufferAmount = haveRead;
@@ -349,7 +349,7 @@ bool QNonContiguousByteDeviceIoDeviceImpl::advanceReadPointer(qint64 amount)
if (currentReadBufferPosition > currentReadBufferAmount) {
qint64 i = currentReadBufferPosition - currentReadBufferAmount;
while (i > 0) {
- if (device->getChar(0) == false) {
+ if (device->getChar(nullptr) == false) {
emit readProgress(totalAdvancements - i, size());
return false; // ### FIXME handle eof
}
@@ -377,7 +377,7 @@ bool QNonContiguousByteDeviceIoDeviceImpl::reset()
totalAdvancements = 0; //reset the progress counter
if (currentReadBuffer) {
delete currentReadBuffer;
- currentReadBuffer = 0;
+ currentReadBuffer = nullptr;
}
currentReadBufferAmount = 0;
currentReadBufferPosition = 0;
@@ -405,7 +405,7 @@ qint64 QNonContiguousByteDeviceIoDeviceImpl::pos() const
return device->pos();
}
-QByteDeviceWrappingIoDevice::QByteDeviceWrappingIoDevice(QNonContiguousByteDevice *bd) : QIODevice((QObject*)0)
+QByteDeviceWrappingIoDevice::QByteDeviceWrappingIoDevice(QNonContiguousByteDevice *bd) : QIODevice((QObject*)nullptr)
{
byteDevice = bd;
connect(bd, SIGNAL(readyRead()), SIGNAL(readyRead()));
diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp
index 35ca2542f7..aedcae2cdc 100644
--- a/src/corelib/io/qprocess.cpp
+++ b/src/corelib/io/qprocess.cpp
@@ -100,7 +100,7 @@ QT_END_NAMESPACE
#include <private/qcore_unix_p.h>
#endif
-#if QT_HAS_INCLUDE(<paths.h>)
+#if __has_include(<paths.h>)
#include <paths.h>
#endif
@@ -216,7 +216,7 @@ void QProcessEnvironmentPrivate::insert(const QProcessEnvironmentPrivate &other)
environment variables to be removed.
*/
QProcessEnvironment::QProcessEnvironment()
- : d(0)
+ : d(nullptr)
{
}
@@ -436,18 +436,18 @@ void QProcessPrivate::Channel::clear()
case PipeSource:
Q_ASSERT(process);
process->stdinChannel.type = Normal;
- process->stdinChannel.process = 0;
+ process->stdinChannel.process = nullptr;
break;
case PipeSink:
Q_ASSERT(process);
process->stdoutChannel.type = Normal;
- process->stdoutChannel.process = 0;
+ process->stdoutChannel.process = nullptr;
break;
}
type = Normal;
file.clear();
- process = 0;
+ process = nullptr;
}
/*!
@@ -869,8 +869,8 @@ QProcessPrivate::QProcessPrivate()
sequenceNumber = 0;
exitCode = 0;
exitStatus = QProcess::NormalExit;
- startupSocketNotifier = 0;
- deathNotifier = 0;
+ startupSocketNotifier = nullptr;
+ deathNotifier = nullptr;
childStartedPipe[0] = INVALID_Q_PIPE;
childStartedPipe[1] = INVALID_Q_PIPE;
forkfd = -1;
@@ -924,23 +924,23 @@ void QProcessPrivate::cleanup()
if (stdoutChannel.notifier) {
delete stdoutChannel.notifier;
- stdoutChannel.notifier = 0;
+ stdoutChannel.notifier = nullptr;
}
if (stderrChannel.notifier) {
delete stderrChannel.notifier;
- stderrChannel.notifier = 0;
+ stderrChannel.notifier = nullptr;
}
if (stdinChannel.notifier) {
delete stdinChannel.notifier;
- stdinChannel.notifier = 0;
+ stdinChannel.notifier = nullptr;
}
if (startupSocketNotifier) {
delete startupSocketNotifier;
- startupSocketNotifier = 0;
+ startupSocketNotifier = nullptr;
}
if (deathNotifier) {
delete deathNotifier;
- deathNotifier = 0;
+ deathNotifier = nullptr;
}
closeChannel(&stdoutChannel);
closeChannel(&stderrChannel);
@@ -1004,7 +1004,7 @@ QT_WARNING_POP
/*!
\internal
- Returns true if we emitted readyRead().
+ Returns \c true if we emitted readyRead().
*/
bool QProcessPrivate::tryReadFromChannel(Channel *channel)
{
@@ -1229,7 +1229,7 @@ void QProcessPrivate::closeWriteChannel()
#endif
if (stdinChannel.notifier) {
delete stdinChannel.notifier;
- stdinChannel.notifier = 0;
+ stdinChannel.notifier = nullptr;
}
#ifdef Q_OS_WIN
// ### Find a better fix, feeding the process little by little
@@ -2187,6 +2187,8 @@ bool QProcess::startDetached(qint64 *pid)
This method is an alias for start(), and exists only to fully implement
the interface defined by QIODevice.
+ Returns \c true if the program has been started.
+
\sa start(), setProgram(), setArguments()
*/
bool QProcess::open(OpenMode mode)
@@ -2615,7 +2617,7 @@ QT_END_INCLUDE_NAMESPACE
QStringList QProcess::systemEnvironment()
{
QStringList tmp;
- char *entry = 0;
+ char *entry = nullptr;
int count = 0;
while ((entry = environ[count++]))
tmp << QString::fromLocal8Bit(entry);
diff --git a/src/corelib/io/qprocess.h b/src/corelib/io/qprocess.h
index 9fda5fba11..585508adf1 100644
--- a/src/corelib/io/qprocess.h
+++ b/src/corelib/io/qprocess.h
@@ -278,7 +278,8 @@ Q_SIGNALS:
void finished(int exitCode); // ### Qt 6: merge the two signals with a default value
#endif
void finished(int exitCode, QProcess::ExitStatus exitStatus);
-#if QT_DEPRECATED_SINCE(5,6)
+#if QT_DEPRECATED_SINCE(5, 6)
+ QT_DEPRECATED_X("Use QProcess::errorOccurred(QProcess::ProcessError) instead")
void error(QProcess::ProcessError error);
#endif
void errorOccurred(QProcess::ProcessError error);
diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp
index 0c80daa024..2186f23ab6 100644
--- a/src/corelib/io/qprocess_unix.cpp
+++ b/src/corelib/io/qprocess_unix.cpp
@@ -246,7 +246,7 @@ bool QProcessPrivate::openChannel(Channel &channel)
return false;
// create the socket notifiers
- if (threadData->hasEventDispatcher()) {
+ if (threadData.loadRelaxed()->hasEventDispatcher()) {
if (&channel == &stdinChannel) {
channel.notifier = new QSocketNotifier(channel.pipe[1],
QSocketNotifier::Write, q);
@@ -338,11 +338,11 @@ static char **_q_dupEnvironment(const QProcessEnvironmentPrivate::Map &environme
{
*envc = 0;
if (environment.isEmpty())
- return 0;
+ return nullptr;
char **envp = new char *[environment.count() + 2];
- envp[environment.count()] = 0;
- envp[environment.count() + 1] = 0;
+ envp[environment.count()] = nullptr;
+ envp[environment.count() + 1] = nullptr;
auto it = environment.constBegin();
const auto end = environment.constEnd();
@@ -377,7 +377,7 @@ void QProcessPrivate::startProcess()
return;
}
- if (threadData->hasEventDispatcher()) {
+ if (threadData.loadRelaxed()->hasEventDispatcher()) {
startupSocketNotifier = new QSocketNotifier(childStartedPipe[0],
QSocketNotifier::Read, q);
QObject::connect(startupSocketNotifier, SIGNAL(activated(int)),
@@ -390,7 +390,7 @@ void QProcessPrivate::startProcess()
// Create argument list with right number of elements, and set the final
// one to 0.
char **argv = new char *[arguments.count() + 2];
- argv[arguments.count() + 1] = 0;
+ argv[arguments.count() + 1] = nullptr;
// Encode the program name.
QByteArray encodedProgramName = QFile::encodeName(program);
@@ -437,13 +437,13 @@ void QProcessPrivate::startProcess()
// Duplicate the environment.
int envc = 0;
- char **envp = 0;
+ char **envp = nullptr;
if (environment.d.constData()) {
envp = _q_dupEnvironment(environment.d.constData()->vars, &envc);
}
// Encode the working directory if it's non-empty, otherwise just pass 0.
- const char *workingDirPtr = 0;
+ const char *workingDirPtr = nullptr;
QByteArray encodedWorkingDirectory;
if (!workingDirectory.isEmpty()) {
encodedWorkingDirectory = QFile::encodeName(workingDirectory);
@@ -451,8 +451,13 @@ void QProcessPrivate::startProcess()
}
// Start the process manager, and fork off the child process.
+ // ### Qt6: revisit whether the change in behavior due to not using fork()
+ // is acceptable for derived classes.
+ int ffdflags = FFD_CLOEXEC;
+ if (typeid(*q) != typeid(QProcess))
+ ffdflags |= FFD_USE_FORK;
pid_t childPid;
- forkfd = ::forkfd(FFD_CLOEXEC, &childPid);
+ forkfd = ::forkfd(ffdflags , &childPid);
int lastForkErrno = errno;
if (forkfd != FFD_CHILD_PROCESS) {
// Parent process.
@@ -517,7 +522,7 @@ void QProcessPrivate::startProcess()
if (stderrChannel.pipe[0] != -1)
::fcntl(stderrChannel.pipe[0], F_SETFL, ::fcntl(stderrChannel.pipe[0], F_GETFL) | O_NONBLOCK);
- if (threadData->eventDispatcher.loadAcquire()) {
+ if (threadData.loadRelaxed()->eventDispatcher.loadAcquire()) {
deathNotifier = new QSocketNotifier(forkfd, QSocketNotifier::Read, q);
QObject::connect(deathNotifier, SIGNAL(activated(int)),
q, SLOT(_q_processDied()));
@@ -596,7 +601,7 @@ bool QProcessPrivate::processStarted(QString *errorMessage)
if (startupSocketNotifier) {
startupSocketNotifier->setEnabled(false);
startupSocketNotifier->deleteLater();
- startupSocketNotifier = 0;
+ startupSocketNotifier = nullptr;
}
qt_safe_close(childStartedPipe[0]);
childStartedPipe[0] = -1;
@@ -889,7 +894,7 @@ bool QProcessPrivate::waitForDeadChild()
crashed = info.code != CLD_EXITED;
delete deathNotifier;
- deathNotifier = 0;
+ deathNotifier = nullptr;
EINTR_LOOP(ret, forkfd_close(forkfd));
forkfd = -1; // Child is dead, don't try to kill it anymore
@@ -935,7 +940,7 @@ bool QProcessPrivate::startDetached(qint64 *pid)
struct sigaction noaction;
memset(&noaction, 0, sizeof(noaction));
noaction.sa_handler = SIG_IGN;
- ::sigaction(SIGPIPE, &noaction, 0);
+ ::sigaction(SIGPIPE, &noaction, nullptr);
::setsid();
@@ -964,7 +969,7 @@ bool QProcessPrivate::startDetached(qint64 *pid)
char **argv = new char *[arguments.size() + 2];
for (int i = 0; i < arguments.size(); ++i)
argv[i + 1] = ::strdup(QFile::encodeName(arguments.at(i)).constData());
- argv[arguments.size() + 1] = 0;
+ argv[arguments.size() + 1] = nullptr;
// Duplicate the environment.
int envc = 0;
@@ -991,7 +996,7 @@ bool QProcessPrivate::startDetached(qint64 *pid)
struct sigaction noaction;
memset(&noaction, 0, sizeof(noaction));
noaction.sa_handler = SIG_IGN;
- ::sigaction(SIGPIPE, &noaction, 0);
+ ::sigaction(SIGPIPE, &noaction, nullptr);
// '\1' means execv failed
char c = '\1';
@@ -1002,7 +1007,7 @@ bool QProcessPrivate::startDetached(qint64 *pid)
struct sigaction noaction;
memset(&noaction, 0, sizeof(noaction));
noaction.sa_handler = SIG_IGN;
- ::sigaction(SIGPIPE, &noaction, 0);
+ ::sigaction(SIGPIPE, &noaction, nullptr);
// '\2' means internal error
char c = '\2';
diff --git a/src/corelib/io/qprocess_win.cpp b/src/corelib/io/qprocess_win.cpp
index 3ba86063e3..1527cf93ed 100644
--- a/src/corelib/io/qprocess_win.cpp
+++ b/src/corelib/io/qprocess_win.cpp
@@ -49,7 +49,6 @@
#include <qelapsedtimer.h>
#include <qfileinfo.h>
#include <qrandom.h>
-#include <qregexp.h>
#include <qwineventnotifier.h>
#include <private/qsystemlibrary_p.h>
#include <private/qthread_p.h>
@@ -398,7 +397,17 @@ static QString qt_create_commandline(const QString &program, const QStringList &
for (int i=0; i<arguments.size(); ++i) {
QString tmp = arguments.at(i);
// Quotes are escaped and their preceding backslashes are doubled.
- tmp.replace(QRegExp(QLatin1String("(\\\\*)\"")), QLatin1String("\\1\\1\\\""));
+ int index = tmp.indexOf(QLatin1Char('"'));
+ while (index >= 0) {
+ // Escape quote
+ tmp.insert(index++, QLatin1Char('\\'));
+ // Double preceding backslashes (ignoring the one we just inserted)
+ for (int i = index - 2 ; i >= 0 && tmp.at(i) == QLatin1Char('\\') ; --i) {
+ tmp.insert(i, QLatin1Char('\\'));
+ index++;
+ }
+ index = tmp.indexOf(QLatin1Char('"'), index + 1);
+ }
if (tmp.isEmpty() || tmp.contains(QLatin1Char(' ')) || tmp.contains(QLatin1Char('\t'))) {
// The argument must not end with a \ since this would be interpreted
// as escaping the quote -- rather put the \ behind the quote: e.g.
@@ -590,7 +599,7 @@ void QProcessPrivate::startProcess()
if (!pid)
return;
- if (threadData->hasEventDispatcher()) {
+ if (threadData.loadRelaxed()->hasEventDispatcher()) {
processFinishedNotifier = new QWinEventNotifier(pid->hProcess, q);
QObject::connect(processFinishedNotifier, SIGNAL(activated(HANDLE)), q, SLOT(_q_processDied()));
processFinishedNotifier->setEnabled(true);
diff --git a/src/corelib/io/qresource.cpp b/src/corelib/io/qresource.cpp
index 86d361b06a..e636712e57 100644
--- a/src/corelib/io/qresource.cpp
+++ b/src/corelib/io/qresource.cpp
@@ -1,7 +1,7 @@
/****************************************************************************
**
-** Copyright (C) 2018 The Qt Company Ltd.
-** Copyright (C) 2019 Intel Corporation.
+** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2020 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -60,14 +60,14 @@
#include "private/qtools_p.h"
#include "private/qsystemerror_p.h"
+#ifndef QT_NO_COMPRESS
+# include <zconf.h>
+# include <zlib.h>
+#endif
#if QT_CONFIG(zstd)
# include <zstd.h>
#endif
-#ifdef Q_OS_UNIX
-# include "private/qcore_unix_p.h"
-#endif
-
#if defined(Q_OS_UNIX) && !defined(Q_OS_NACL) && !defined(Q_OS_INTEGRITY)
# define QT_USE_MMAP
# include <sys/mman.h>
@@ -280,9 +280,9 @@ static inline QStringList *resourceSearchPaths()
RCC tool used to compress the payload.
\value NoCompression Contents are not compressed
- \value ZlibCompression Contents are compressed using \l{zlib}{https://zlib.net} and can
+ \value ZlibCompression Contents are compressed using \l{https://zlib.net}{zlib} and can
be decompressed using the qUncompress() function.
- \value ZstdCompression Contents are compressed using \l{zstd}{https://zstd.net}. To
+ \value ZstdCompression Contents are compressed using \l{https://zstd.net}{zstd}. To
decompress, use the \c{ZSTD_decompress} function from the zstd
library.
@@ -296,6 +296,8 @@ public:
void ensureInitialized() const;
void ensureChildren() const;
+ qint64 uncompressedSize() const Q_DECL_PURE_FUNCTION;
+ qsizetype decompress(char *buffer, qsizetype bufferSize) const;
bool load(const QString &file);
void clear();
@@ -440,6 +442,78 @@ QResourcePrivate::ensureChildren() const
}
}
+qint64 QResourcePrivate::uncompressedSize() const
+{
+ switch (compressionAlgo) {
+ case QResource::NoCompression:
+ return size;
+
+ case QResource::ZlibCompression:
+#ifndef QT_NO_COMPRESS
+ if (size_t(size) >= sizeof(quint32))
+ return qFromBigEndian<quint32>(data);
+#else
+ Q_ASSERT(!"QResource: Qt built without support for Zlib compression");
+ Q_UNREACHABLE();
+#endif
+ break;
+
+ case QResource::ZstdCompression: {
+#if QT_CONFIG(zstd)
+ size_t n = ZSTD_getFrameContentSize(data, size);
+ return ZSTD_isError(n) ? -1 : qint64(n);
+#else
+ // This should not happen because we've refused to load such resource
+ Q_ASSERT(!"QResource: Qt built without support for Zstd compression");
+ Q_UNREACHABLE();
+#endif
+ }
+
+ }
+ return -1;
+}
+
+qsizetype QResourcePrivate::decompress(char *buffer, qsizetype bufferSize) const
+{
+ Q_ASSERT(data);
+
+ switch (compressionAlgo) {
+ case QResource::NoCompression:
+ Q_UNREACHABLE();
+ break;
+
+ case QResource::ZlibCompression: {
+#ifndef QT_NO_COMPRESS
+ uLong len = uLong(bufferSize);
+ int res = ::uncompress(reinterpret_cast<Bytef *>(buffer), &len,
+ data + sizeof(quint32), uLong(size - sizeof(quint32)));
+ if (res != Z_OK) {
+ qWarning("QResource: error decompressing zlib content (%d)", res);
+ return -1;
+ }
+ return len;
+#else
+ Q_UNREACHABLE();
+#endif
+ }
+
+ case QResource::ZstdCompression: {
+#if QT_CONFIG(zstd)
+ size_t usize = ZSTD_decompress(buffer, bufferSize, data, size);
+ if (ZSTD_isError(usize)) {
+ qWarning("QResource: error decompressing zstd content: %s", ZSTD_getErrorName(usize));
+ return -1;
+ }
+ return usize;
+#else
+ Q_UNREACHABLE();
+#endif
+ }
+ }
+
+ return -1;
+}
+
/*!
Constructs a QResource pointing to \a file. \a locale is used to
load a specific localization of a resource data.
@@ -600,9 +674,12 @@ QResource::Compression QResource::compressionAlgorithm() const
}
/*!
- Returns the size of the data backing the resource.
+ Returns the size of the stored data backing the resource.
- \sa data(), isFile()
+ If the resource is compressed, this function returns the size of the
+ compressed data. See uncompressedSize() for the uncompressed size.
+
+ \sa data(), uncompressedSize(), isFile()
*/
qint64 QResource::size() const
@@ -613,12 +690,29 @@ qint64 QResource::size() const
}
/*!
- Returns direct access to a read only segment of data that this resource
- represents. If the resource is compressed the data returned is compressed
- and the appropriate library functions must be used to access the data. If
- the resource is a directory \nullptr is returned.
+ \since 5.15
+
+ Returns the size of the data in this resource. If the data was not
+ compressed, this function returns the same as size(). If it was, then this
+ function extracts the size of the original uncompressed data from the
+ stored stream.
- \sa size(), compressionAlgorithm(), isFile()
+ \sa size(), uncompressedData(), isFile()
+*/
+qint64 QResource::uncompressedSize() const
+{
+ Q_D(const QResource);
+ d->ensureInitialized();
+ return d->uncompressedSize();
+}
+
+/*!
+ Returns direct access to a segment of read-only data, that this resource
+ represents. If the resource is compressed, the data returned is also
+ compressed. The caller must then decompress the data or use
+ uncompressedData(). If the resource is a directory, \c nullptr is returned.
+
+ \sa uncompressedData(), size(), isFile()
*/
const uchar *QResource::data() const
@@ -629,6 +723,44 @@ const uchar *QResource::data() const
}
/*!
+ \since 5.15
+
+ Returns the resource data, decompressing it first, if the data was stored
+ compressed. If the resource is a directory or an error occurs while
+ decompressing, a null QByteArray is returned.
+
+ \note If the data was compressed, this function will decompress every time
+ it is called. The result is not cached between calls.
+
+ \sa uncompressedData(), size(), isCompressed(), isFile()
+*/
+
+QByteArray QResource::uncompressedData() const
+{
+ Q_D(const QResource);
+ qint64 n = uncompressedSize();
+ if (n < 0)
+ return QByteArray();
+ if (n > std::numeric_limits<QByteArray::size_type>::max()) {
+ qWarning("QResource: compressed content does not fit into a QByteArray; use QFile instead");
+ return QByteArray();
+ }
+ if (d->compressionAlgo == NoCompression)
+ return QByteArray::fromRawData(reinterpret_cast<const char *>(d->data), n);
+
+ // decompress
+ QByteArray result(n, Qt::Uninitialized);
+ n = d->decompress(result.data(), n);
+ if (n < 0)
+ result.clear();
+ else
+ result.truncate(n);
+ return result;
+}
+
+/*!
+ \since 5.8
+
Returns the date and time when the file was last modified before
packaging into a resource.
*/
@@ -1443,13 +1575,7 @@ bool QResourceFileEngine::link(const QString &)
qint64 QResourceFileEngine::size() const
{
Q_D(const QResourceFileEngine);
- if (!d->resource.isValid())
- return 0;
- if (d->resource.compressionAlgorithm() != QResource::NoCompression) {
- d->uncompress();
- return d->uncompressed.size();
- }
- return d->resource.size();
+ return d->resource.isValid() ? d->resource.uncompressedSize() : 0;
}
qint64 QResourceFileEngine::pos() const
@@ -1486,7 +1612,7 @@ bool QResourceFileEngine::isSequential() const
QAbstractFileEngine::FileFlags QResourceFileEngine::fileFlags(QAbstractFileEngine::FileFlags type) const
{
Q_D(const QResourceFileEngine);
- QAbstractFileEngine::FileFlags ret = 0;
+ QAbstractFileEngine::FileFlags ret;
if(!d->resource.isValid())
return ret;
@@ -1578,7 +1704,7 @@ QAbstractFileEngine::Iterator *QResourceFileEngine::beginEntryList(QDir::Filters
*/
QAbstractFileEngine::Iterator *QResourceFileEngine::endEntryList()
{
- return 0;
+ return nullptr;
}
bool QResourceFileEngine::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output)
@@ -1588,7 +1714,7 @@ bool QResourceFileEngine::extension(Extension extension, const ExtensionOption *
const MapExtensionOption *options = (const MapExtensionOption*)(option);
MapExtensionReturn *returnValue = static_cast<MapExtensionReturn*>(output);
returnValue->address = d->map(options->offset, options->size, options->flags);
- return (returnValue->address != 0);
+ return (returnValue->address != nullptr);
}
if (extension == UnMapExtension) {
const UnMapExtensionOption *options = (const UnMapExtensionOption*)option;
@@ -1607,22 +1733,21 @@ uchar *QResourceFileEnginePrivate::map(qint64 offset, qint64 size, QFile::Memory
Q_Q(QResourceFileEngine);
Q_UNUSED(flags);
- qint64 max = resource.size();
- if (resource.compressionAlgorithm() != QResource::NoCompression) {
- uncompress();
- max = uncompressed.size();
- }
-
+ qint64 max = resource.uncompressedSize();
qint64 end;
if (offset < 0 || size <= 0 || !resource.isValid() ||
add_overflow(offset, size, &end) || end > max) {
q->setError(QFile::UnspecifiedError, QString());
- return 0;
+ return nullptr;
}
const uchar *address = resource.data();
- if (resource.compressionAlgorithm() != QResource::NoCompression)
+ if (resource.compressionAlgorithm() != QResource::NoCompression) {
+ uncompress();
+ if (uncompressed.isNull())
+ return nullptr;
address = reinterpret_cast<const uchar *>(uncompressed.constData());
+ }
return const_cast<uchar *>(address) + offset;
}
@@ -1635,41 +1760,10 @@ bool QResourceFileEnginePrivate::unmap(uchar *ptr)
void QResourceFileEnginePrivate::uncompress() const
{
- if (uncompressed.isEmpty() && resource.size()) {
- quint64 size;
- switch (resource.compressionAlgorithm()) {
- case QResource::NoCompression:
- return; // nothing to do
-
- case QResource::ZlibCompression:
-#ifndef QT_NO_COMPRESS
- uncompressed = qUncompress(resource.data(), resource.size());
-#else
- Q_ASSERT(!"QResourceFileEngine::open: Qt built without support for Zlib compression");
-#endif
- break;
-
- case QResource::ZstdCompression:
-#if QT_CONFIG(zstd)
- size = ZSTD_getFrameContentSize(resource.data(), resource.size());
- if (!ZSTD_isError(size)) {
- if (size >= MaxAllocSize) {
- qWarning("QResourceFileEngine::open: content bigger than memory (size %lld)", size);
- } else {
- uncompressed = QByteArray(size, Qt::Uninitialized);
- size = ZSTD_decompress(const_cast<char *>(uncompressed.data()), size,
- resource.data(), resource.size());
- }
- }
- if (ZSTD_isError(size))
- qWarning("QResourceFileEngine::open: error decoding: %s", ZSTD_getErrorName(size));
-#else
- Q_UNUSED(size);
- Q_ASSERT(!"QResourceFileEngine::open: Qt built without support for Zstd compression");
-#endif
- break;
- }
- }
+ if (resource.compressionAlgorithm() == QResource::NoCompression
+ || !uncompressed.isEmpty() || resource.size() == 0)
+ return; // nothing to do
+ uncompressed = resource.uncompressedData();
}
#endif // !defined(QT_BOOTSTRAPPED)
diff --git a/src/corelib/io/qresource.h b/src/corelib/io/qresource.h
index 5ee8d5d266..52b0d74d29 100644
--- a/src/corelib/io/qresource.h
+++ b/src/corelib/io/qresource.h
@@ -1,6 +1,7 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2019 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -75,6 +76,8 @@ public:
Compression compressionAlgorithm() const;
qint64 size() const;
const uchar *data() const;
+ qint64 uncompressedSize() const;
+ QByteArray uncompressedData() const;
QDateTime lastModified() const;
#if QT_DEPRECATED_SINCE(5, 13)
diff --git a/src/corelib/io/qsavefile.cpp b/src/corelib/io/qsavefile.cpp
index 0a884a7df9..067ccda3df 100644
--- a/src/corelib/io/qsavefile.cpp
+++ b/src/corelib/io/qsavefile.cpp
@@ -116,7 +116,7 @@ QSaveFile::QSaveFile(const QString &name)
Constructs a new file object to represent the file with the given \a name.
*/
QSaveFile::QSaveFile(const QString &name)
- : QFileDevice(*new QSaveFilePrivate, 0)
+ : QFileDevice(*new QSaveFilePrivate, nullptr)
{
Q_D(QSaveFile);
d->fileName = name;
diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp
index e485310e86..779b6f72de 100644
--- a/src/corelib/io/qsettings.cpp
+++ b/src/corelib/io/qsettings.cpp
@@ -76,10 +76,6 @@
# include <ioLib.h>
#endif
-#ifdef Q_OS_WASM
-#include <emscripten.h>
-#endif
-
#include <algorithm>
#include <stdlib.h>
@@ -210,7 +206,7 @@ QConfFile *QConfFile::fromName(const QString &fileName, bool _userPerms)
ConfFileHash *usedHash = usedHashFunc();
ConfFileCache *unusedCache = unusedCacheFunc();
- QConfFile *confFile = 0;
+ QConfFile *confFile = nullptr;
const auto locker = qt_scoped_lock(settingsGlobalMutex);
if (!(confFile = usedHash->value(absPath))) {
@@ -234,7 +230,7 @@ void QConfFile::clearCache()
// QSettingsPrivate
QSettingsPrivate::QSettingsPrivate(QSettings::Format format)
- : format(format), scope(QSettings::UserScope /* nothing better to put */), iniCodec(0), fallbacks(true),
+ : format(format), scope(QSettings::UserScope /* nothing better to put */), iniCodec(nullptr), fallbacks(true),
pendingChanges(false), status(QSettings::NoError)
{
}
@@ -242,7 +238,7 @@ QSettingsPrivate::QSettingsPrivate(QSettings::Format format)
QSettingsPrivate::QSettingsPrivate(QSettings::Format format, QSettings::Scope scope,
const QString &organization, const QString &application)
: format(format), scope(scope), organizationName(organization), applicationName(application),
- iniCodec(0), fallbacks(true), pendingChanges(false), status(QSettings::NoError)
+ iniCodec(nullptr), fallbacks(true), pendingChanges(false), status(QSettings::NoError)
{
}
@@ -295,7 +291,7 @@ after_loop:
// see also qsettings_win.cpp, qsettings_winrt.cpp and qsettings_mac.cpp
-#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC)
+#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC) && !defined(Q_OS_WASM)
QSettingsPrivate *QSettingsPrivate::create(QSettings::Format format, QSettings::Scope scope,
const QString &organization, const QString &application)
{
@@ -931,8 +927,8 @@ QStringList QSettingsPrivate::splitArgs(const QString &s, int idx)
void QConfFileSettingsPrivate::initFormat()
{
extension = (format == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini");
- readFunc = 0;
- writeFunc = 0;
+ readFunc = nullptr;
+ writeFunc = nullptr;
#if defined(Q_OS_MAC)
caseSensitivity = (format == QSettings::NativeFormat) ? Qt::CaseSensitive : IniCaseSensitivity;
#else
@@ -1188,7 +1184,9 @@ QConfFileSettingsPrivate::QConfFileSettingsPrivate(QSettings::Format format,
confFiles.append(QConfFile::fromName(systemPath.path + orgFile, false));
}
+#ifndef Q_OS_WASM // wasm needs to delay access until after file sync
initAccess();
+#endif
}
QConfFileSettingsPrivate::QConfFileSettingsPrivate(const QString &fileName,
@@ -1551,13 +1549,6 @@ void QConfFileSettingsPrivate::syncConfFile(QConfFile *confFile)
perms |= QFile::ReadGroup | QFile::ReadOther;
QFile(confFile->name).setPermissions(perms);
}
-#ifdef Q_OS_WASM
- EM_ASM(
- // Sync sandbox filesystem to persistent database filesystem. See QTBUG-70002
- FS.syncfs(false, function(err) {
- });
- );
-#endif
} else {
setStatus(QSettings::AccessError);
}
@@ -3350,7 +3341,7 @@ bool QSettings::contains(const QString &key) const
{
Q_D(const QSettings);
QString k = d->actualKey(key);
- return d->get(k, 0);
+ return d->get(k, nullptr);
}
/*!
diff --git a/src/corelib/io/qsettings_p.h b/src/corelib/io/qsettings_p.h
index d18c96a06c..c30f099a72 100644
--- a/src/corelib/io/qsettings_p.h
+++ b/src/corelib/io/qsettings_p.h
@@ -57,6 +57,10 @@
#include "QtCore/qiodevice.h"
#include "QtCore/qstack.h"
#include "QtCore/qstringlist.h"
+
+#include <QtCore/qvariant.h>
+#include "qsettings.h"
+
#ifndef QT_NO_QOBJECT
#include "private/qobject_p.h"
#endif
@@ -253,6 +257,10 @@ protected:
mutable QSettings::Status status;
};
+#ifdef Q_OS_WASM
+class QWasmSettingsPrivate;
+#endif
+
class QConfFileSettingsPrivate : public QSettingsPrivate
{
public:
@@ -281,7 +289,7 @@ public:
private:
void initFormat();
- void initAccess();
+ virtual void initAccess();
void syncConfFile(QConfFile *confFile);
bool writeIniFile(QIODevice &device, const ParsedSettingsMap &map);
#ifdef Q_OS_MAC
@@ -297,6 +305,9 @@ private:
QString extension;
Qt::CaseSensitivity caseSensitivity;
int nextPosition;
+#ifdef Q_OS_WASM
+ friend class QWasmSettingsPrivate;
+#endif
};
QT_END_NAMESPACE
diff --git a/src/corelib/io/qsettings_wasm.cpp b/src/corelib/io/qsettings_wasm.cpp
new file mode 100644
index 0000000000..8d8f4b505c
--- /dev/null
+++ b/src/corelib/io/qsettings_wasm.cpp
@@ -0,0 +1,259 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qsettings.h"
+#ifndef QT_NO_SETTINGS
+
+#include "qsettings_p.h"
+#ifndef QT_NO_QOBJECT
+#include "qcoreapplication.h"
+#include <QFile>
+#endif // QT_NO_QOBJECT
+#include <QDebug>
+
+#include <QFileInfo>
+#include <QDir>
+#include <emscripten.h>
+
+QT_BEGIN_NAMESPACE
+
+static bool isReadReady = false;
+
+class QWasmSettingsPrivate : public QConfFileSettingsPrivate
+{
+public:
+ QWasmSettingsPrivate(QSettings::Scope scope, const QString &organization,
+ const QString &application);
+ ~QWasmSettingsPrivate();
+
+ bool get(const QString &key, QVariant *value) const override;
+ QStringList children(const QString &prefix, ChildSpec spec) const override;
+ void clear() override;
+ void sync() override;
+ void flush() override;
+ bool isWritable() const override;
+
+ void syncToLocal(const char *data, int size);
+ void loadLocal(const QByteArray &filename);
+ void setReady();
+ void initAccess() override;
+
+private:
+ QString databaseName;
+ QString id;
+};
+
+static void QWasmSettingsPrivate_onLoad(void *userData, void *dataPtr, int size)
+{
+ QWasmSettingsPrivate *wasm = reinterpret_cast<QWasmSettingsPrivate *>(userData);
+
+ QFile file(wasm->fileName());
+ QFileInfo fileInfo(wasm->fileName());
+ QDir dir(fileInfo.path());
+ if (!dir.exists())
+ dir.mkpath(fileInfo.path());
+
+ if (file.open(QFile::WriteOnly)) {
+ file.write(reinterpret_cast<char *>(dataPtr), size);
+ file.close();
+ wasm->setReady();
+ }
+}
+
+static void QWasmSettingsPrivate_onError(void *userData)
+{
+ QWasmSettingsPrivate *wasm = reinterpret_cast<QWasmSettingsPrivate *>(userData);
+ if (wasm)
+ wasm->setStatus(QSettings::AccessError);
+}
+
+static void QWasmSettingsPrivate_onStore(void *userData)
+{
+ QWasmSettingsPrivate *wasm = reinterpret_cast<QWasmSettingsPrivate *>(userData);
+ if (wasm)
+ wasm->setStatus(QSettings::NoError);
+}
+
+static void QWasmSettingsPrivate_onCheck(void *userData, int exists)
+{
+ QWasmSettingsPrivate *wasm = reinterpret_cast<QWasmSettingsPrivate *>(userData);
+ if (wasm) {
+ if (exists)
+ wasm->loadLocal(wasm->fileName().toLocal8Bit());
+ else
+ wasm->setReady();
+ }
+}
+
+QSettingsPrivate *QSettingsPrivate::create(QSettings::Format format,
+ QSettings::Scope scope,
+ const QString &organization,
+ const QString &application)
+{
+ Q_UNUSED(format)
+ if (organization == QLatin1String("Qt"))
+ {
+ QString organizationDomain = QCoreApplication::organizationDomain();
+ QString applicationName = QCoreApplication::applicationName();
+
+ QSettingsPrivate *newSettings;
+ newSettings = new QWasmSettingsPrivate(scope, organizationDomain, applicationName);
+
+ newSettings->beginGroupOrArray(QSettingsGroup(normalizedKey(organization)));
+ if (!application.isEmpty())
+ newSettings->beginGroupOrArray(QSettingsGroup(normalizedKey(application)));
+
+ return newSettings;
+ }
+ return new QWasmSettingsPrivate(scope, organization, application);
+}
+
+QWasmSettingsPrivate::QWasmSettingsPrivate(QSettings::Scope scope, const QString &organization,
+ const QString &application)
+ : QConfFileSettingsPrivate(QSettings::NativeFormat, scope, organization, application)
+{
+ setStatus(QSettings::AccessError); // access error until sandbox gets loaded
+ databaseName = organization;
+ id = application;
+
+ emscripten_idb_async_exists("/home/web_user",
+ fileName().toLocal8Bit(),
+ reinterpret_cast<void*>(this),
+ QWasmSettingsPrivate_onCheck,
+ QWasmSettingsPrivate_onError);
+}
+
+QWasmSettingsPrivate::~QWasmSettingsPrivate()
+{
+}
+
+ void QWasmSettingsPrivate::initAccess()
+{
+ if (isReadReady)
+ QConfFileSettingsPrivate::initAccess();
+}
+
+bool QWasmSettingsPrivate::get(const QString &key, QVariant *value) const
+{
+ if (isReadReady)
+ return QConfFileSettingsPrivate::get(key, value);
+
+ return false;
+}
+
+QStringList QWasmSettingsPrivate::children(const QString &prefix, ChildSpec spec) const
+{
+ return QConfFileSettingsPrivate::children(prefix, spec);
+}
+
+void QWasmSettingsPrivate::clear()
+{
+ QConfFileSettingsPrivate::clear();
+ emscripten_idb_async_delete("/home/web_user",
+ fileName().toLocal8Bit(),
+ reinterpret_cast<void*>(this),
+ QWasmSettingsPrivate_onStore,
+ QWasmSettingsPrivate_onError);
+}
+
+void QWasmSettingsPrivate::sync()
+{
+ QConfFileSettingsPrivate::sync();
+
+ QFile file(fileName());
+ if (file.open(QFile::ReadOnly)) {
+ QByteArray dataPointer = file.readAll();
+
+ emscripten_idb_async_store("/home/web_user",
+ fileName().toLocal8Bit(),
+ reinterpret_cast<void *>(dataPointer.data()),
+ dataPointer.length(),
+ reinterpret_cast<void*>(this),
+ QWasmSettingsPrivate_onStore,
+ QWasmSettingsPrivate_onError);
+ }
+}
+
+void QWasmSettingsPrivate::flush()
+{
+ sync();
+}
+
+bool QWasmSettingsPrivate::isWritable() const
+{
+ return isReadReady && QConfFileSettingsPrivate::isWritable();
+}
+
+void QWasmSettingsPrivate::syncToLocal(const char *data, int size)
+{
+ QFile file(fileName());
+
+ if (file.open(QFile::WriteOnly)) {
+ file.write(data, size + 1);
+ QByteArray data = file.readAll();
+
+ emscripten_idb_async_store("/home/web_user",
+ fileName().toLocal8Bit(),
+ reinterpret_cast<void *>(data.data()),
+ data.length(),
+ reinterpret_cast<void*>(this),
+ QWasmSettingsPrivate_onStore,
+ QWasmSettingsPrivate_onError);
+ setReady();
+ }
+}
+
+void QWasmSettingsPrivate::loadLocal(const QByteArray &filename)
+{
+ emscripten_idb_async_load("/home/web_user",
+ filename.data(),
+ reinterpret_cast<void*>(this),
+ QWasmSettingsPrivate_onLoad,
+ QWasmSettingsPrivate_onError);
+}
+
+void QWasmSettingsPrivate::setReady()
+{
+ isReadReady = true;
+ setStatus(QSettings::NoError);
+ QConfFileSettingsPrivate::initAccess();
+}
+
+QT_END_NAMESPACE
+#endif // QT_NO_SETTINGS
diff --git a/src/corelib/io/qstandardpaths.cpp b/src/corelib/io/qstandardpaths.cpp
index 7874b854e4..a5128a868b 100644
--- a/src/corelib/io/qstandardpaths.cpp
+++ b/src/corelib/io/qstandardpaths.cpp
@@ -48,7 +48,7 @@
#include <qcoreapplication.h>
#endif
-#if QT_HAS_INCLUDE(<paths.h>)
+#if __has_include(<paths.h>)
#include <paths.h>
#endif
@@ -359,22 +359,14 @@ QT_BEGIN_NAMESPACE
/*!
\fn QString QStandardPaths::writableLocation(StandardLocation type)
- Returns the directory where files of \a type should be written to, or an empty string
- if the location cannot be determined.
-
- \note The storage location returned can be a directory that does not exist; i.e., it
- may need to be created by the system or the user.
+ \include standardpath/functiondocs.qdocinc writableLocation
*/
/*!
\fn QStringList QStandardPaths::standardLocations(StandardLocation type)
- Returns all the directories where files of \a type belong.
-
- The list of directories is sorted from high to low priority, starting with
- writableLocation() if it can be determined. This list is empty if no locations
- for \a type are defined.
+ \include standardpath/functiondocs.qdocinc standardLocations
\sa writableLocation()
*/
@@ -398,11 +390,7 @@ static bool existsAsSpecified(const QString &path, QStandardPaths::LocateOptions
}
/*!
- Tries to find a file or directory called \a fileName in the standard locations
- for \a type.
-
- The full path to the first file or directory (depending on \a options) found is returned.
- If no such file or directory can be found, an empty string is returned.
+ \include standardpath/functiondocs.qdocinc locate
*/
QString QStandardPaths::locate(StandardLocation type, const QString &fileName, LocateOptions options)
{
@@ -416,12 +404,7 @@ QString QStandardPaths::locate(StandardLocation type, const QString &fileName, L
}
/*!
- Tries to find all files or directories called \a fileName in the standard locations
- for \a type.
-
- The \a options flag allows to specify whether to look for files or directories.
-
- Returns the list of all the files that were found.
+ \include standardpath/functiondocs.qdocinc locateAll
*/
QStringList QStandardPaths::locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
{
@@ -494,23 +477,7 @@ static inline QString
#endif // Q_OS_WIN
/*!
- Finds the executable named \a executableName in the paths specified by \a paths,
- or the system paths if \a paths is empty.
-
- On most operating systems the system path is determined by the PATH environment variable.
-
- The directories where to search for the executable can be set in the \a paths argument.
- To search in both your own paths and the system paths, call findExecutable twice, once with
- \a paths set and once with \a paths empty.
-
- Symlinks are not resolved, in order to preserve behavior for the case of executables
- whose behavior depends on the name they are invoked with.
-
- \note On Windows, the usual executable extensions (from the PATHEXT environment variable)
- are automatically appended, so that for instance findExecutable("foo") will find foo.exe
- or foo.bat if present.
-
- Returns the absolute file path to the executable, or an empty string if not found.
+ \include standardpath/functiondocs.qdocinc findExecutable
*/
QString QStandardPaths::findExecutable(const QString &executableName, const QStringList &paths)
{
@@ -568,10 +535,7 @@ QString QStandardPaths::findExecutable(const QString &executableName, const QStr
}
/*!
- \fn QString QStandardPaths::displayName(StandardLocation type)
-
- Returns a localized display name for the given location \a type or
- an empty QString if no relevant location can be found.
+ \include standardpath/functiondocs.qdocinc displayName
*/
#if !defined(Q_OS_MAC) && !defined(QT_BOOTSTRAPPED)
@@ -628,23 +592,7 @@ QString QStandardPaths::displayName(StandardLocation type)
/*!
\fn void QStandardPaths::setTestModeEnabled(bool testMode)
- If \a testMode is true, this enables a special "test mode" in
- QStandardPaths, which changes writable locations
- to point to test directories, in order to prevent auto tests from reading from
- or writing to the current user's configuration.
-
- This affects the locations into which test programs might write files:
- GenericDataLocation, DataLocation, ConfigLocation, GenericConfigLocation,
- AppConfigLocation, GenericCacheLocation, CacheLocation.
- Other locations are not affected.
-
- On Unix, \c XDG_DATA_HOME is set to \e ~/.qttest/share, \c XDG_CONFIG_HOME is
- set to \e ~/.qttest/config, and \c XDG_CACHE_HOME is set to \e ~/.qttest/cache.
-
- On \macos, data goes to \e ~/.qttest/Application Support, cache goes to
- \e ~/.qttest/Cache, and config goes to \e ~/.qttest/Preferences.
-
- On Windows, everything goes to a "qttest" directory under Application Data.
+ \include standardpath/functiondocs.qdocinc setTestModeEnabled
*/
static bool qsp_testMode = false;
diff --git a/src/corelib/io/qstorageinfo_unix.cpp b/src/corelib/io/qstorageinfo_unix.cpp
index 1e72241e68..37b8a60c37 100644
--- a/src/corelib/io/qstorageinfo_unix.cpp
+++ b/src/corelib/io/qstorageinfo_unix.cpp
@@ -108,7 +108,7 @@
# endif // QT_LARGEFILE_SUPPORT
#endif // Q_OS_BSD4
-#if QT_HAS_INCLUDE(<paths.h>)
+#if __has_include(<paths.h>)
# include <paths.h>
#endif
#ifndef _PATH_MOUNTED
diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
index acd31f4d84..55d13dad70 100644
--- a/src/corelib/io/qtemporaryfile.cpp
+++ b/src/corelib/io/qtemporaryfile.cpp
@@ -631,6 +631,12 @@ QString QTemporaryFilePrivate::defaultTemplateName()
case sensitive. If the template is not present in the filename,
QTemporaryFile appends the generated part to the filename given.
+ \note On Linux, QTemporaryFile will attempt to create unnamed temporary
+ files. If that succeeds, open() will return true but exists() will be
+ false. If you call fileName() or any function that calls it,
+ QTemporaryFile will give the file a name, so most applications will
+ not see a difference.
+
\sa QDir::tempPath(), QFile
*/
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp
index a7650390f9..0a5f9256eb 100644
--- a/src/corelib/io/qurl.cpp
+++ b/src/corelib/io/qurl.cpp
@@ -823,7 +823,7 @@ recodeFromUser(const QString &input, const ushort *actions, int from, int to)
QString output;
const QChar *begin = input.constData() + from;
const QChar *end = input.constData() + to;
- if (qt_urlRecode(output, begin, end, nullptr, actions))
+ if (qt_urlRecode(output, begin, end, {}, actions))
return output;
return input.mid(from, to - from);
diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp
index fc69e362c7..3bcd81bcdf 100644
--- a/src/corelib/itemmodels/qabstractitemmodel.cpp
+++ b/src/corelib/itemmodels/qabstractitemmodel.cpp
@@ -61,9 +61,9 @@ Q_LOGGING_CATEGORY(lcCheckIndex, "qt.core.qabstractitemmodel.checkindex")
QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex &index)
{
Q_ASSERT(index.isValid()); // we will _never_ insert an invalid index in the list
- QPersistentModelIndexData *d = 0;
+ QPersistentModelIndexData *d = nullptr;
QAbstractItemModel *model = const_cast<QAbstractItemModel *>(index.model());
- QHash<QModelIndex, QPersistentModelIndexData *> &indexes = model->d_func()->persistent.indexes;
+ QMultiHash<QModelIndex, QPersistentModelIndexData *> &indexes = model->d_func()->persistent.indexes;
const auto it = indexes.constFind(index);
if (it != indexes.cend()) {
d = (*it);
@@ -136,7 +136,7 @@ void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data)
*/
QPersistentModelIndex::QPersistentModelIndex()
- : d(0)
+ : d(nullptr)
{
}
@@ -158,7 +158,7 @@ QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other)
*/
QPersistentModelIndex::QPersistentModelIndex(const QModelIndex &index)
- : d(0)
+ : d(nullptr)
{
if (index.isValid()) {
d = QPersistentModelIndexData::create(index);
@@ -176,7 +176,7 @@ QPersistentModelIndex::~QPersistentModelIndex()
{
if (d && !d->ref.deref()) {
QPersistentModelIndexData::destroy(d);
- d = 0;
+ d = nullptr;
}
}
@@ -257,7 +257,7 @@ QPersistentModelIndex &QPersistentModelIndex::operator=(const QModelIndex &other
d = QPersistentModelIndexData::create(other);
if (d) d->ref.ref();
} else {
- d = 0;
+ d = nullptr;
}
return *this;
}
@@ -344,7 +344,7 @@ void *QPersistentModelIndex::internalPointer() const
{
if (d)
return d->index.internalPointer();
- return 0;
+ return nullptr;
}
/*!
@@ -432,7 +432,7 @@ Qt::ItemFlags QPersistentModelIndex::flags() const
{
if (d)
return d->index.flags();
- return 0;
+ return { };
}
/*!
@@ -442,7 +442,7 @@ const QAbstractItemModel *QPersistentModelIndex::model() const
{
if (d)
return d->index.model();
- return 0;
+ return nullptr;
}
/*!
@@ -484,7 +484,7 @@ QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx)
class QEmptyItemModel : public QAbstractItemModel
{
public:
- explicit QEmptyItemModel(QObject *parent = 0) : QAbstractItemModel(parent) {}
+ explicit QEmptyItemModel(QObject *parent = nullptr) : QAbstractItemModel(parent) {}
QModelIndex index(int, int, const QModelIndex &) const override { return QModelIndex(); }
QModelIndex parent(const QModelIndex &) const override { return QModelIndex(); }
int rowCount(const QModelIndex &) const override { return 0; }
@@ -662,8 +662,7 @@ void QAbstractItemModelPrivate::rowsAboutToBeInserted(const QModelIndex &parent,
Q_UNUSED(last);
QVector<QPersistentModelIndexData *> persistent_moved;
if (first < q->rowCount(parent)) {
- for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
- it != persistent.indexes.constEnd(); ++it) {
+ for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
const QModelIndex &index = data->index;
if (index.row() >= first && index.isValid() && index.parent() == parent) {
@@ -699,14 +698,13 @@ void QAbstractItemModelPrivate::itemsAboutToBeMoved(const QModelIndex &srcParent
QVector<QPersistentModelIndexData *> persistent_moved_in_source;
QVector<QPersistentModelIndexData *> persistent_moved_in_destination;
- QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it;
- const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator begin = persistent.indexes.constBegin();
- const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator end = persistent.indexes.constEnd();
+ const auto begin = persistent.indexes.constBegin();
+ const auto end = persistent.indexes.constEnd();
const bool sameParent = (srcParent == destinationParent);
const bool movingUp = (srcFirst > destinationChild);
- for ( it = begin; it != end; ++it) {
+ for (auto it = begin; it != end; ++it) {
QPersistentModelIndexData *data = *it;
const QModelIndex &index = data->index;
const QModelIndex &parent = index.parent();
@@ -811,8 +809,7 @@ void QAbstractItemModelPrivate::rowsAboutToBeRemoved(const QModelIndex &parent,
QVector<QPersistentModelIndexData *> persistent_invalidated;
// find the persistent indexes that are affected by the change, either by being in the removed subtree
// or by being on the same level and below the removed rows
- for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
- it != persistent.indexes.constEnd(); ++it) {
+ for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
bool level_changed = false;
QModelIndex current = data->index;
@@ -867,8 +864,7 @@ void QAbstractItemModelPrivate::columnsAboutToBeInserted(const QModelIndex &pare
Q_UNUSED(last);
QVector<QPersistentModelIndexData *> persistent_moved;
if (first < q->columnCount(parent)) {
- for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
- it != persistent.indexes.constEnd(); ++it) {
+ for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
const QModelIndex &index = data->index;
if (index.column() >= first && index.isValid() && index.parent() == parent)
@@ -904,8 +900,7 @@ void QAbstractItemModelPrivate::columnsAboutToBeRemoved(const QModelIndex &paren
QVector<QPersistentModelIndexData *> persistent_invalidated;
// find the persistent indexes that are affected by the change, either by being in the removed subtree
// or by being on the same level and to the right of the removed columns
- for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
- it != persistent.indexes.constEnd(); ++it) {
+ for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
bool level_changed = false;
QModelIndex current = data->index;
@@ -1950,10 +1945,10 @@ QStringList QAbstractItemModel::mimeTypes() const
QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
{
if (indexes.count() <= 0)
- return 0;
+ return nullptr;
QStringList types = mimeTypes();
if (types.isEmpty())
- return 0;
+ return nullptr;
QMimeData *data = new QMimeData();
QString format = types.at(0);
QByteArray encoded;
@@ -2296,7 +2291,7 @@ Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
{
Q_D(const QAbstractItemModel);
if (!d->indexValid(index))
- return 0;
+ return { };
return Qt::ItemIsSelectable|Qt::ItemIsEnabled;
}
@@ -3376,8 +3371,7 @@ QModelIndexList QAbstractItemModel::persistentIndexList() const
Q_D(const QAbstractItemModel);
QModelIndexList result;
result.reserve(d->persistent.indexes.count());
- for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = d->persistent.indexes.constBegin();
- it != d->persistent.indexes.constEnd(); ++it) {
+ for (auto it = d->persistent.indexes.constBegin(); it != d->persistent.indexes.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
result.append(data->index);
}
@@ -3995,8 +3989,8 @@ bool QAbstractListModel::dropMimeData(const QMimeData *data, Qt::DropAction acti
*/
void QAbstractItemModelPrivate::Persistent::insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data)
{
- QHash<QModelIndex,QPersistentModelIndexData *>::iterator newIt = indexes.insert(key, data);
- QHash<QModelIndex,QPersistentModelIndexData *>::iterator it = newIt;
+ auto newIt = indexes.insert(key, data);
+ auto it = newIt;
++it;
while (it != indexes.end() && it.key() == key) {
qSwap(*newIt,*it);
diff --git a/src/corelib/itemmodels/qabstractproxymodel.cpp b/src/corelib/itemmodels/qabstractproxymodel.cpp
index c863406afd..87559cd6b2 100644
--- a/src/corelib/itemmodels/qabstractproxymodel.cpp
+++ b/src/corelib/itemmodels/qabstractproxymodel.cpp
@@ -159,7 +159,7 @@ QAbstractItemModel *QAbstractProxyModel::sourceModel() const
{
Q_D(const QAbstractProxyModel);
if (d->model == QAbstractItemModelPrivate::staticEmptyModel())
- return 0;
+ return nullptr;
return d->model;
}
diff --git a/src/corelib/itemmodels/qconcatenatetablesproxymodel.cpp b/src/corelib/itemmodels/qconcatenatetablesproxymodel.cpp
index 0319d215a1..3afa132483 100644
--- a/src/corelib/itemmodels/qconcatenatetablesproxymodel.cpp
+++ b/src/corelib/itemmodels/qconcatenatetablesproxymodel.cpp
@@ -497,7 +497,7 @@ void QConcatenateTablesProxyModel::removeSourceModel(QAbstractItemModel *sourceM
{
Q_D(QConcatenateTablesProxyModel);
Q_ASSERT(d->m_models.contains(sourceModel));
- disconnect(sourceModel, 0, this, 0);
+ disconnect(sourceModel, nullptr, this, nullptr);
const int rowsRemoved = sourceModel->rowCount();
const int rowsPrior = d->computeRowsPrior(sourceModel); // location of removed section
diff --git a/src/corelib/itemmodels/qidentityproxymodel.cpp b/src/corelib/itemmodels/qidentityproxymodel.cpp
index 39992eccd3..f5684c6eda 100644
--- a/src/corelib/itemmodels/qidentityproxymodel.cpp
+++ b/src/corelib/itemmodels/qidentityproxymodel.cpp
@@ -313,6 +313,30 @@ bool QIdentityProxyModel::removeRows(int row, int count, const QModelIndex& pare
/*!
\reimp
+ \since 5.15
+ */
+bool QIdentityProxyModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
+{
+ Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == this : true);
+ Q_ASSERT(destinationParent.isValid() ? destinationParent.model() == this : true);
+ Q_D(QIdentityProxyModel);
+ return d->model->moveRows(mapToSource(sourceParent), sourceRow, count, mapToSource(destinationParent), destinationChild);
+}
+
+/*!
+ \reimp
+ \since 5.15
+ */
+bool QIdentityProxyModel::moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild)
+{
+ Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == this : true);
+ Q_ASSERT(destinationParent.isValid() ? destinationParent.model() == this : true);
+ Q_D(QIdentityProxyModel);
+ return d->model->moveColumns(mapToSource(sourceParent), sourceColumn, count, mapToSource(destinationParent), destinationChild);
+}
+
+/*!
+ \reimp
*/
int QIdentityProxyModel::rowCount(const QModelIndex& parent) const
{
diff --git a/src/corelib/itemmodels/qidentityproxymodel.h b/src/corelib/itemmodels/qidentityproxymodel.h
index 89ac89cdba..4c14e6176a 100644
--- a/src/corelib/itemmodels/qidentityproxymodel.h
+++ b/src/corelib/itemmodels/qidentityproxymodel.h
@@ -77,6 +77,8 @@ public:
bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
bool removeColumns(int column, int count, const QModelIndex& parent = QModelIndex()) override;
bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
+ bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
+ bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild) override;
protected:
QIdentityProxyModel(QIdentityProxyModelPrivate &dd, QObject* parent);
diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp
index c93a4d15b9..e4ac5da299 100644
--- a/src/corelib/itemmodels/qitemselectionmodel.cpp
+++ b/src/corelib/itemmodels/qitemselectionmodel.cpp
@@ -656,7 +656,7 @@ void QItemSelectionModelPrivate::initModel(QAbstractItemModel *m)
SLOT(_q_layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)) },
{ SIGNAL(modelReset()),
SLOT(reset()) },
- { 0, 0 }
+ { nullptr, nullptr }
};
if (model == m)
@@ -1627,10 +1627,9 @@ bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &par
QItemSelection sel = d->ranges;
sel.merge(d->currentSelection, d->currentCommand);
- for (int i = 0; i < sel.count(); ++i) {
- QItemSelectionRange range = sel.at(i);
+ for (const QItemSelectionRange &range : qAsConst(sel)) {
if (range.parent() != parent)
- return false;
+ return false;
int top = range.top();
int bottom = range.bottom();
int left = range.left();
@@ -1661,11 +1660,13 @@ bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelInde
QItemSelection sel = d->ranges;
sel.merge(d->currentSelection, d->currentCommand);
- for (int i = 0; i < sel.count(); ++i) {
- int left = sel.at(i).left();
- int right = sel.at(i).right();
- int top = sel.at(i).top();
- int bottom = sel.at(i).bottom();
+ for (const QItemSelectionRange &range : qAsConst(sel)) {
+ if (range.parent() != parent)
+ return false;
+ int top = range.top();
+ int bottom = range.bottom();
+ int left = range.left();
+ int right = range.right();
if (left <= column && right >= column) {
for (int j = top; j <= bottom; j++) {
const Qt::ItemFlags flags = d->model->index(j, column, parent).flags();
diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp
index 978102035e..21303549ab 100644
--- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp
+++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp
@@ -2682,6 +2682,7 @@ void QSortFilterProxyModel::setFilterCaseSensitivity(Qt::CaseSensitivity cs)
d->filter_about_to_be_changed();
d->filter_data.setCaseSensitivity(cs);
d->filter_changed();
+ emit filterCaseSensitivityChanged(cs);
}
/*!
@@ -2707,6 +2708,7 @@ void QSortFilterProxyModel::setSortCaseSensitivity(Qt::CaseSensitivity cs)
d->sort_casesensitivity = cs;
d->sort();
+ emit sortCaseSensitivityChanged(cs);
}
/*!
@@ -2732,6 +2734,7 @@ void QSortFilterProxyModel::setSortLocaleAware(bool on)
d->sort_localeaware = on;
d->sort();
+ emit sortLocaleAwareChanged(on);
}
/*!
@@ -2856,6 +2859,7 @@ void QSortFilterProxyModel::setSortRole(int role)
return;
d->sort_role = role;
d->sort();
+ emit sortRoleChanged(role);
}
/*!
@@ -2881,6 +2885,7 @@ void QSortFilterProxyModel::setFilterRole(int role)
d->filter_about_to_be_changed();
d->filter_role = role;
d->filter_changed();
+ emit filterRoleChanged(role);
}
/*!
@@ -2907,6 +2912,7 @@ void QSortFilterProxyModel::setRecursiveFilteringEnabled(bool recursive)
d->filter_about_to_be_changed();
d->filter_recursive = recursive;
d->filter_changed();
+ emit recursiveFilteringEnabledChanged(recursive);
}
#if QT_DEPRECATED_SINCE(5, 11)
diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.h b/src/corelib/itemmodels/qsortfilterproxymodel.h
index 303226668f..91253dd601 100644
--- a/src/corelib/itemmodels/qsortfilterproxymodel.h
+++ b/src/corelib/itemmodels/qsortfilterproxymodel.h
@@ -68,12 +68,12 @@ class Q_CORE_EXPORT QSortFilterProxyModel : public QAbstractProxyModel
#endif
Q_PROPERTY(int filterKeyColumn READ filterKeyColumn WRITE setFilterKeyColumn)
Q_PROPERTY(bool dynamicSortFilter READ dynamicSortFilter WRITE setDynamicSortFilter)
- Q_PROPERTY(Qt::CaseSensitivity filterCaseSensitivity READ filterCaseSensitivity WRITE setFilterCaseSensitivity)
- Q_PROPERTY(Qt::CaseSensitivity sortCaseSensitivity READ sortCaseSensitivity WRITE setSortCaseSensitivity)
- Q_PROPERTY(bool isSortLocaleAware READ isSortLocaleAware WRITE setSortLocaleAware)
- Q_PROPERTY(int sortRole READ sortRole WRITE setSortRole)
- Q_PROPERTY(int filterRole READ filterRole WRITE setFilterRole)
- Q_PROPERTY(bool recursiveFilteringEnabled READ isRecursiveFilteringEnabled WRITE setRecursiveFilteringEnabled)
+ Q_PROPERTY(Qt::CaseSensitivity filterCaseSensitivity READ filterCaseSensitivity WRITE setFilterCaseSensitivity NOTIFY filterCaseSensitivityChanged)
+ Q_PROPERTY(Qt::CaseSensitivity sortCaseSensitivity READ sortCaseSensitivity WRITE setSortCaseSensitivity NOTIFY sortCaseSensitivityChanged)
+ Q_PROPERTY(bool isSortLocaleAware READ isSortLocaleAware WRITE setSortLocaleAware NOTIFY sortLocaleAwareChanged)
+ Q_PROPERTY(int sortRole READ sortRole WRITE setSortRole NOTIFY sortRoleChanged)
+ Q_PROPERTY(int filterRole READ filterRole WRITE setFilterRole NOTIFY filterRoleChanged)
+ Q_PROPERTY(bool recursiveFilteringEnabled READ isRecursiveFilteringEnabled WRITE setRecursiveFilteringEnabled NOTIFY recursiveFilteringEnabledChanged)
public:
explicit QSortFilterProxyModel(QObject *parent = nullptr);
@@ -185,6 +185,16 @@ public:
QStringList mimeTypes() const override;
Qt::DropActions supportedDropActions() const override;
+
+Q_SIGNALS:
+ void dynamicSortFilterChanged(bool dynamicSortFilter);
+ void filterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity);
+ void sortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity);
+ void sortLocaleAwareChanged(bool sortLocaleAware);
+ void sortRoleChanged(int sortRole);
+ void filterRoleChanged(int filterRole);
+ void recursiveFilteringEnabledChanged(bool recursiveFilteringEnabled);
+
private:
Q_DECLARE_PRIVATE(QSortFilterProxyModel)
Q_DISABLE_COPY(QSortFilterProxyModel)
diff --git a/src/corelib/itemmodels/qstringlistmodel.cpp b/src/corelib/itemmodels/qstringlistmodel.cpp
index a248cdcd38..a950783ed8 100644
--- a/src/corelib/itemmodels/qstringlistmodel.cpp
+++ b/src/corelib/itemmodels/qstringlistmodel.cpp
@@ -212,6 +212,7 @@ Qt::ItemFlags QStringListModel::flags(const QModelIndex &index) const
\a index in the model, to the provided \a value.
The dataChanged() signal is emitted if the item is changed.
+ Returns \c true after emitting the dataChanged() signal.
\sa Qt::ItemDataRole, data()
*/
@@ -249,6 +250,8 @@ bool QStringListModel::clearItemData(const QModelIndex &index)
specified, indicating that the rows are inserted in the top level of
the model.
+ Returns \c true if the insertion was successful.
+
\sa QAbstractItemModel::insertRows()
*/
@@ -275,6 +278,8 @@ bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent)
specified, indicating that the rows are removed in the top level of
the model.
+ Returns \c true if the row removal was successful.
+
\sa QAbstractItemModel::removeRows()
*/
@@ -301,24 +306,23 @@ bool QStringListModel::moveRows(const QModelIndex &sourceParent, int sourceRow,
{
if (sourceRow < 0
|| sourceRow + count - 1 >= rowCount(sourceParent)
- || destinationChild <= 0
+ || destinationChild < 0
|| destinationChild > rowCount(destinationParent)
+ || sourceRow == destinationChild
|| sourceRow == destinationChild - 1
- || count <= 0) {
+ || count <= 0
+ || sourceParent.isValid()
+ || destinationParent.isValid()) {
return false;
}
if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild))
return false;
- /*
- QList::move assumes that the second argument is the index where the item will end up to
- i.e. the valid range for that argument is from 0 to QList::size()-1
- QAbstractItemModel::moveRows when source and destinations have the same parent assumes that
- the item will end up being in the row BEFORE the one indicated by destinationChild
- i.e. the valid range for that argument is from 1 to QList::size()
- For this reason we remove 1 from destinationChild when using it inside QList
- */
- destinationChild--;
- const int fromRow = destinationChild < sourceRow ? (sourceRow + count - 1) : sourceRow;
+
+ int fromRow = sourceRow;
+ if (destinationChild < sourceRow)
+ fromRow += count - 1;
+ else
+ destinationChild--;
while (count--)
lst.move(fromRow, destinationChild);
endMoveRows();
diff --git a/src/corelib/kernel/qbasictimer.cpp b/src/corelib/kernel/qbasictimer.cpp
index ea8f8e2c77..623ecb9b8b 100644
--- a/src/corelib/kernel/qbasictimer.cpp
+++ b/src/corelib/kernel/qbasictimer.cpp
@@ -216,13 +216,11 @@ void QBasicTimer::stop()
{
if (id) {
QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance();
- if (eventDispatcher) {
- if (Q_UNLIKELY(!eventDispatcher->unregisterTimer(id))) {
- qWarning("QBasicTimer::stop: Failed. Possibly trying to stop from a different thread");
- return;
- }
- QAbstractEventDispatcherPrivate::releaseTimerId(id);
+ if (eventDispatcher && !eventDispatcher->unregisterTimer(id)) {
+ qWarning("QBasicTimer::stop: Failed. Possibly trying to stop from a different thread");
+ return;
}
+ QAbstractEventDispatcherPrivate::releaseTimerId(id);
}
id = 0;
}
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp
index 6531cd8e0c..dc46695f80 100644
--- a/src/corelib/kernel/qcoreapplication.cpp
+++ b/src/corelib/kernel/qcoreapplication.cpp
@@ -121,7 +121,6 @@
#endif
#ifdef Q_OS_WASM
-#include <emscripten.h>
#include <emscripten/val.h>
#endif
@@ -135,23 +134,6 @@
QT_BEGIN_NAMESPACE
-#ifndef QT_NO_QOBJECT
-class QMutexUnlocker
-{
-public:
- inline explicit QMutexUnlocker(QMutex *m)
- : mtx(m)
- { }
- inline ~QMutexUnlocker() { unlock(); }
- inline void unlock() { if (mtx) mtx->unlock(); mtx = 0; }
-
-private:
- Q_DISABLE_COPY(QMutexUnlocker)
-
- QMutex *mtx;
-};
-#endif
-
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
extern QString qAppFileName();
#endif
@@ -497,13 +479,6 @@ QCoreApplicationPrivate::QCoreApplicationPrivate(int &aargc, char **aargv, uint
QCoreApplicationPrivate::~QCoreApplicationPrivate()
{
-#ifdef Q_OS_WASM
- EM_ASM(
- // unmount persistent directory as IDBFS
- // see also QTBUG-70002
- FS.unmount('/home/web_user');
- );
-#endif
#ifndef QT_NO_QOBJECT
cleanupThreadData();
#endif
@@ -517,25 +492,27 @@ QCoreApplicationPrivate::~QCoreApplicationPrivate()
void QCoreApplicationPrivate::cleanupThreadData()
{
- if (threadData && !threadData_clean) {
+ auto thisThreadData = threadData.loadRelaxed();
+
+ if (thisThreadData && !threadData_clean) {
#if QT_CONFIG(thread)
- void *data = &threadData->tls;
+ void *data = &thisThreadData->tls;
QThreadStorageData::finish((void **)data);
#endif
// need to clear the state of the mainData, just in case a new QCoreApplication comes along.
- const auto locker = qt_scoped_lock(threadData->postEventList.mutex);
- for (int i = 0; i < threadData->postEventList.size(); ++i) {
- const QPostEvent &pe = threadData->postEventList.at(i);
+ const auto locker = qt_scoped_lock(thisThreadData->postEventList.mutex);
+ for (int i = 0; i < thisThreadData->postEventList.size(); ++i) {
+ const QPostEvent &pe = thisThreadData->postEventList.at(i);
if (pe.event) {
--pe.receiver->d_func()->postedEvents;
pe.event->posted = false;
delete pe.event;
}
}
- threadData->postEventList.clear();
- threadData->postEventList.recursion = 0;
- threadData->quitNow = false;
+ thisThreadData->postEventList.clear();
+ thisThreadData->postEventList.recursion = 0;
+ thisThreadData->quitNow = false;
threadData_clean = true;
}
}
@@ -553,10 +530,10 @@ void QCoreApplicationPrivate::eventDispatcherReady()
{
}
-QBasicAtomicPointer<QThread> QCoreApplicationPrivate::theMainThread = Q_BASIC_ATOMIC_INITIALIZER(0);
+QBasicAtomicPointer<QThread> QCoreApplicationPrivate::theMainThread = Q_BASIC_ATOMIC_INITIALIZER(nullptr);
QThread *QCoreApplicationPrivate::mainThread()
{
- Q_ASSERT(theMainThread.loadRelaxed() != 0);
+ Q_ASSERT(theMainThread.loadRelaxed() != nullptr);
return theMainThread.loadRelaxed();
}
@@ -713,7 +690,7 @@ QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)
#ifdef QT_NO_QOBJECT
: d_ptr(&p)
#else
- : QObject(p, 0)
+ : QObject(p, nullptr)
#endif
{
d_func()->q_ptr = this;
@@ -795,17 +772,8 @@ void QCoreApplicationPrivate::init()
Q_ASSERT_X(!QCoreApplication::self, "QCoreApplication", "there should be only one application object");
QCoreApplication::self = q;
-#ifdef Q_OS_WASM
- EM_ASM(
- // mount and sync persistent filesystem to sandbox
- FS.mount(IDBFS, {}, '/home/web_user');
- FS.syncfs(true, function(err) {
- if (err)
- Module.print(err);
- });
- );
-
#if QT_CONFIG(thread)
+#ifdef Q_OS_WASM
QThreadPrivate::idealThreadCount = emscripten::val::global("navigator")["hardwareConcurrency"].as<int>();
#endif
#endif
@@ -858,7 +826,8 @@ void QCoreApplicationPrivate::init()
#ifndef QT_NO_QOBJECT
// use the event dispatcher created by the app programmer (if any)
Q_ASSERT(!eventDispatcher);
- eventDispatcher = threadData->eventDispatcher.loadRelaxed();
+ auto thisThreadData = threadData.loadRelaxed();
+ eventDispatcher = thisThreadData->eventDispatcher.loadRelaxed();
// otherwise we create one
if (!eventDispatcher)
@@ -866,11 +835,11 @@ void QCoreApplicationPrivate::init()
Q_ASSERT(eventDispatcher);
if (!eventDispatcher->parent()) {
- eventDispatcher->moveToThread(threadData->thread.loadAcquire());
+ eventDispatcher->moveToThread(thisThreadData->thread.loadAcquire());
eventDispatcher->setParent(q);
}
- threadData->eventDispatcher = eventDispatcher;
+ thisThreadData->eventDispatcher = eventDispatcher;
eventDispatcherReady();
#endif
@@ -914,7 +883,7 @@ QCoreApplication::~QCoreApplication()
#endif
#ifndef QT_NO_QOBJECT
- d_func()->threadData->eventDispatcher = nullptr;
+ d_func()->threadData.loadRelaxed()->eventDispatcher = nullptr;
if (QCoreApplicationPrivate::eventDispatcher)
QCoreApplicationPrivate::eventDispatcher->closingDown();
QCoreApplicationPrivate::eventDispatcher = nullptr;
@@ -1170,7 +1139,7 @@ bool QCoreApplication::notify(QObject *receiver, QEvent *event)
static bool doNotify(QObject *receiver, QEvent *event)
{
- if (receiver == 0) { // serious error
+ if (receiver == nullptr) { // serious error
qWarning("QCoreApplication::notify: Unexpected null receiver");
return true;
}
@@ -1185,7 +1154,7 @@ static bool doNotify(QObject *receiver, QEvent *event)
bool QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject *receiver, QEvent *event)
{
// We can't access the application event filters outside of the main thread (race conditions)
- Q_ASSERT(receiver->d_func()->threadData->thread.loadAcquire() == mainThread());
+ Q_ASSERT(receiver->d_func()->threadData.loadRelaxed()->thread.loadAcquire() == mainThread());
if (extraData) {
// application event filters are only called for objects in the GUI thread
@@ -1238,7 +1207,7 @@ bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event)
// send to all application event filters (only does anything in the main thread)
if (QCoreApplication::self
- && receiver->d_func()->threadData->thread.loadAcquire() == mainThread()
+ && receiver->d_func()->threadData.loadRelaxed()->thread.loadAcquire() == mainThread()
&& QCoreApplication::self->d_func()->sendThroughApplicationEventFilters(receiver, event)) {
filtered = true;
return filtered;
@@ -1414,12 +1383,12 @@ int QCoreApplication::exec()
void QCoreApplicationPrivate::execCleanup()
{
- threadData->quitNow = false;
+ threadData.loadRelaxed()->quitNow = false;
in_exec = false;
if (!aboutToQuitEmitted)
emit q_func()->aboutToQuit(QCoreApplication::QPrivateSignal());
aboutToQuitEmitted = true;
- QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
+ QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
}
@@ -1451,7 +1420,7 @@ void QCoreApplication::exit(int returnCode)
{
if (!self)
return;
- QThreadData *data = self->d_func()->threadData;
+ QThreadData *data = self->d_func()->threadData.loadRelaxed();
data->quitNow = true;
for (int i = 0; i < data->eventLoops.size(); ++i) {
QEventLoop *eventLoop = data->eventLoops.at(i);
@@ -1501,6 +1470,38 @@ bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
#endif // QT_NO_QOBJECT
+QCoreApplicationPrivate::QPostEventListLocker QCoreApplicationPrivate::lockThreadPostEventList(QObject *object)
+{
+ QPostEventListLocker locker;
+
+ if (!object) {
+ locker.threadData = QThreadData::current();
+ locker.locker = qt_unique_lock(locker.threadData->postEventList.mutex);
+ return locker;
+ }
+
+ auto &threadData = QObjectPrivate::get(object)->threadData;
+
+ // if object has moved to another thread, follow it
+ for (;;) {
+ // synchronizes with the storeRelease in QObject::moveToThread
+ locker.threadData = threadData.loadAcquire();
+ if (!locker.threadData) {
+ // destruction in progress
+ return locker;
+ }
+
+ auto temporaryLocker = qt_unique_lock(locker.threadData->postEventList.mutex);
+ if (locker.threadData == threadData.loadAcquire()) {
+ locker.locker = std::move(temporaryLocker);
+ break;
+ }
+ }
+
+ Q_ASSERT(locker.threadData);
+ return locker;
+}
+
/*!
\since 4.3
@@ -1530,38 +1531,20 @@ void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority)
{
Q_TRACE_SCOPE(QCoreApplication_postEvent, receiver, event, event->type());
- if (receiver == 0) {
+ if (receiver == nullptr) {
qWarning("QCoreApplication::postEvent: Unexpected null receiver");
delete event;
return;
}
- QThreadData * volatile * pdata = &receiver->d_func()->threadData;
- QThreadData *data = *pdata;
- if (!data) {
+ auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
+ if (!locker.threadData) {
// posting during destruction? just delete the event to prevent a leak
delete event;
return;
}
- // lock the post event mutex
- data->postEventList.mutex.lock();
-
- // if object has moved to another thread, follow it
- while (data != *pdata) {
- data->postEventList.mutex.unlock();
-
- data = *pdata;
- if (!data) {
- // posting during destruction? just delete the event to prevent a leak
- delete event;
- return;
- }
-
- data->postEventList.mutex.lock();
- }
-
- QMutexUnlocker locker(&data->postEventList.mutex);
+ QThreadData *data = locker.threadData;
// if this is one of the compressible events, do compression
if (receiver->d_func()->postedEvents
@@ -1652,7 +1635,7 @@ bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEven
for (int i = 0; i < postedEvents->size(); ++i) {
const QPostEvent &cur = postedEvents->at(i);
if (cur.receiver != receiver
- || cur.event == 0
+ || cur.event == nullptr
|| cur.event->type() != event->type())
continue;
// found an event for this receiver
@@ -1801,7 +1784,7 @@ void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver, int event_type
// null out the event so if sendPostedEvents recurses, it
// will ignore this one, as it's been re-posted.
- const_cast<QPostEvent &>(pe).event = 0;
+ const_cast<QPostEvent &>(pe).event = nullptr;
// re-post the copied event so it isn't lost
data->postEventList.addEvent(pe_copy);
@@ -1821,7 +1804,7 @@ void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver, int event_type
// next, update the data structure so that we're ready
// for the next event.
- const_cast<QPostEvent &>(pe).event = 0;
+ const_cast<QPostEvent &>(pe).event = nullptr;
locker.unlock();
const auto relocker = qScopeGuard([&locker] { locker.lock(); });
@@ -1860,8 +1843,8 @@ void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver, int event_type
void QCoreApplication::removePostedEvents(QObject *receiver, int eventType)
{
- QThreadData *data = receiver ? receiver->d_func()->threadData : QThreadData::current();
- auto locker = qt_unique_lock(data->postEventList.mutex);
+ auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
+ QThreadData *data = locker.threadData;
// the QObject destructor calls this function directly. this can
// happen while the event loop is in the middle of posting events,
@@ -1884,7 +1867,7 @@ void QCoreApplication::removePostedEvents(QObject *receiver, int eventType)
--pe.receiver->d_func()->postedEvents;
pe.event->posted = false;
events.append(pe.event);
- const_cast<QPostEvent &>(pe).event = 0;
+ const_cast<QPostEvent &>(pe).event = nullptr;
} else if (!data->postEventList.recursion) {
if (i != j)
qSwap(data->postEventList[i], data->postEventList[j]);
@@ -1946,7 +1929,7 @@ void QCoreApplicationPrivate::removePostedEvent(QEvent * event)
--pe.receiver->d_func()->postedEvents;
pe.event->posted = false;
delete pe.event;
- const_cast<QPostEvent &>(pe).event = 0;
+ const_cast<QPostEvent &>(pe).event = nullptr;
return;
}
}
@@ -2221,7 +2204,7 @@ QString QCoreApplication::translate(const char *context, const char *sourceText,
// Declared in qglobal.h
QString qtTrId(const char *id, int n)
{
- return QCoreApplication::translate(0, id, 0, n);
+ return QCoreApplication::translate(nullptr, id, nullptr, n);
}
bool QCoreApplicationPrivate::isTranslatorInstalled(QTranslator *translator)
@@ -2973,7 +2956,7 @@ QAbstractEventDispatcher *QCoreApplication::eventDispatcher()
{
if (QCoreApplicationPrivate::theMainThread.loadAcquire())
return QCoreApplicationPrivate::theMainThread.loadRelaxed()->eventDispatcher();
- return 0;
+ return nullptr;
}
/*!
diff --git a/src/corelib/kernel/qcoreapplication_p.h b/src/corelib/kernel/qcoreapplication_p.h
index 3bad42d076..9d2fde619c 100644
--- a/src/corelib/kernel/qcoreapplication_p.h
+++ b/src/corelib/kernel/qcoreapplication_p.h
@@ -61,6 +61,7 @@
#endif
#ifndef QT_NO_QOBJECT
#include "private/qobject_p.h"
+#include "private/qlocking_p.h"
#endif
#ifdef Q_OS_MACOS
@@ -140,6 +141,15 @@ public:
static void checkReceiverThread(QObject *receiver);
void cleanupThreadData();
+
+ struct QPostEventListLocker
+ {
+ QThreadData *threadData;
+ std::unique_lock<QMutex> locker;
+
+ void unlock() { locker.unlock(); }
+ };
+ static QPostEventListLocker lockThreadPostEventList(QObject *object);
#endif // QT_NO_QOBJECT
int &argc;
diff --git a/src/corelib/kernel/qcoreapplication_win.cpp b/src/corelib/kernel/qcoreapplication_win.cpp
index 37c43dee4f..824c0535ed 100644
--- a/src/corelib/kernel/qcoreapplication_win.cpp
+++ b/src/corelib/kernel/qcoreapplication_win.cpp
@@ -918,7 +918,7 @@ QDebug operator<<(QDebug dbg, const MSG &msg)
#ifndef QT_NO_QOBJECT
void QCoreApplicationPrivate::removePostedTimerEvent(QObject *object, int timerId)
{
- QThreadData *data = object->d_func()->threadData;
+ QThreadData *data = object->d_func()->threadData.loadRelaxed();
const auto locker = qt_scoped_lock(data->postEventList.mutex);
if (data->postEventList.size() == 0)
diff --git a/src/corelib/kernel/qcoreevent.cpp b/src/corelib/kernel/qcoreevent.cpp
index 4cfc749386..e3326f00d7 100644
--- a/src/corelib/kernel/qcoreevent.cpp
+++ b/src/corelib/kernel/qcoreevent.cpp
@@ -295,7 +295,7 @@ QT_BEGIN_NAMESPACE
Contructs an event object of type \a type.
*/
QEvent::QEvent(Type type)
- : d(0), t(type), posted(false), spont(false), m_accept(true)
+ : d(nullptr), t(type), posted(false), spont(false), m_accept(true)
{
Q_TRACE(QEvent_ctor, this, t);
}
diff --git a/src/corelib/kernel/qdeadlinetimer.h b/src/corelib/kernel/qdeadlinetimer.h
index 9dd92481d2..99e09eb31f 100644
--- a/src/corelib/kernel/qdeadlinetimer.h
+++ b/src/corelib/kernel/qdeadlinetimer.h
@@ -52,7 +52,7 @@
#include <limits>
-#if QT_HAS_INCLUDE(<chrono>)
+#if __has_include(<chrono>)
# include <chrono>
#endif
@@ -120,7 +120,7 @@ public:
QDeadlineTimer &operator-=(qint64 msecs)
{ *this = *this + (-msecs); return *this; }
-#if QT_HAS_INCLUDE(<chrono>) || defined(Q_CLANG_QDOC)
+#if __has_include(<chrono>) || defined(Q_CLANG_QDOC)
template <class Clock, class Duration>
QDeadlineTimer(std::chrono::time_point<Clock, Duration> deadline_,
Qt::TimerType type_ = Qt::CoarseTimer) : t2(0)
diff --git a/src/corelib/kernel/qeventdispatcher_glib.cpp b/src/corelib/kernel/qeventdispatcher_glib.cpp
index d9746ef6e2..92f3553247 100644
--- a/src/corelib/kernel/qeventdispatcher_glib.cpp
+++ b/src/corelib/kernel/qeventdispatcher_glib.cpp
@@ -114,9 +114,9 @@ static GSourceFuncs socketNotifierSourceFuncs = {
socketNotifierSourcePrepare,
socketNotifierSourceCheck,
socketNotifierSourceDispatch,
- NULL,
- NULL,
- NULL
+ nullptr,
+ nullptr,
+ nullptr
};
struct GTimerSource
@@ -188,9 +188,9 @@ static GSourceFuncs timerSourceFuncs = {
timerSourcePrepare,
timerSourceCheck,
timerSourceDispatch,
- NULL,
- NULL,
- NULL
+ nullptr,
+ nullptr,
+ nullptr
};
struct GIdleTimerSource
@@ -227,7 +227,7 @@ static gboolean idleTimerSourceCheck(GSource *source)
static gboolean idleTimerSourceDispatch(GSource *source, GSourceFunc, gpointer)
{
GTimerSource *timerSource = reinterpret_cast<GIdleTimerSource *>(source)->timerSource;
- (void) timerSourceDispatch(&timerSource->source, 0, 0);
+ (void) timerSourceDispatch(&timerSource->source, nullptr, nullptr);
return true;
}
@@ -235,9 +235,9 @@ static GSourceFuncs idleTimerSourceFuncs = {
idleTimerSourcePrepare,
idleTimerSourceCheck,
idleTimerSourceDispatch,
- NULL,
- NULL,
- NULL
+ nullptr,
+ nullptr,
+ nullptr
};
struct GPostEventSource
@@ -267,7 +267,7 @@ static gboolean postEventSourcePrepare(GSource *s, gint *timeout)
static gboolean postEventSourceCheck(GSource *source)
{
- return postEventSourcePrepare(source, 0);
+ return postEventSourcePrepare(source, nullptr);
}
static gboolean postEventSourceDispatch(GSource *s, GSourceFunc, gpointer)
@@ -283,9 +283,9 @@ static GSourceFuncs postEventSourceFuncs = {
postEventSourcePrepare,
postEventSourceCheck,
postEventSourceDispatch,
- NULL,
- NULL,
- NULL
+ nullptr,
+ nullptr,
+ nullptr
};
@@ -372,10 +372,10 @@ QEventDispatcherGlib::~QEventDispatcherGlib()
d->timerSource->timerList.~QTimerInfoList();
g_source_destroy(&d->timerSource->source);
g_source_unref(&d->timerSource->source);
- d->timerSource = 0;
+ d->timerSource = nullptr;
g_source_destroy(&d->idleTimerSource->source);
g_source_unref(&d->idleTimerSource->source);
- d->idleTimerSource = 0;
+ d->idleTimerSource = nullptr;
// destroy socket notifier source
for (int i = 0; i < d->socketNotifierSource->pollfds.count(); ++i) {
@@ -386,19 +386,19 @@ QEventDispatcherGlib::~QEventDispatcherGlib()
d->socketNotifierSource->pollfds.~QList<GPollFDWithQSocketNotifier *>();
g_source_destroy(&d->socketNotifierSource->source);
g_source_unref(&d->socketNotifierSource->source);
- d->socketNotifierSource = 0;
+ d->socketNotifierSource = nullptr;
// destroy post event source
g_source_destroy(&d->postEventSource->source);
g_source_unref(&d->postEventSource->source);
- d->postEventSource = 0;
+ d->postEventSource = nullptr;
- Q_ASSERT(d->mainContext != 0);
+ Q_ASSERT(d->mainContext != nullptr);
#if GLIB_CHECK_VERSION (2, 22, 0)
g_main_context_pop_thread_default (d->mainContext);
#endif
g_main_context_unref(d->mainContext);
- d->mainContext = 0;
+ d->mainContext = nullptr;
}
bool QEventDispatcherGlib::processEvents(QEventLoop::ProcessEventsFlags flags)
diff --git a/src/corelib/kernel/qeventdispatcher_unix.cpp b/src/corelib/kernel/qeventdispatcher_unix.cpp
index 5bc65b7110..0165ce9075 100644
--- a/src/corelib/kernel/qeventdispatcher_unix.cpp
+++ b/src/corelib/kernel/qeventdispatcher_unix.cpp
@@ -463,13 +463,15 @@ bool QEventDispatcherUNIX::processEvents(QEventLoop::ProcessEventsFlags flags)
// we are awake, broadcast it
emit awake();
- QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData);
+
+ auto threadData = d->threadData.loadRelaxed();
+ QCoreApplicationPrivate::sendPostedEvents(nullptr, 0, threadData);
const bool include_timers = (flags & QEventLoop::X11ExcludeTimers) == 0;
const bool include_notifiers = (flags & QEventLoop::ExcludeSocketNotifiers) == 0;
const bool wait_for_events = flags & QEventLoop::WaitForMoreEvents;
- const bool canWait = (d->threadData->canWaitLocked()
+ const bool canWait = (threadData->canWaitLocked()
&& !d->interrupt.loadRelaxed()
&& wait_for_events);
diff --git a/src/corelib/kernel/qeventdispatcher_unix_p.h b/src/corelib/kernel/qeventdispatcher_unix_p.h
index f37edfc967..581df9242c 100644
--- a/src/corelib/kernel/qeventdispatcher_unix_p.h
+++ b/src/corelib/kernel/qeventdispatcher_unix_p.h
@@ -118,7 +118,7 @@ public:
int remainingTime(int timerId) final;
- void wakeUp() final;
+ void wakeUp() override;
void interrupt() final;
void flush() override;
diff --git a/src/corelib/kernel/qeventdispatcher_win.cpp b/src/corelib/kernel/qeventdispatcher_win.cpp
index 87623f304a..8616631603 100644
--- a/src/corelib/kernel/qeventdispatcher_win.cpp
+++ b/src/corelib/kernel/qeventdispatcher_win.cpp
@@ -636,11 +636,11 @@ void QEventDispatcherWin32::registerSocketNotifier(QSocketNotifier *notifier)
int type = notifier->type();
#ifndef QT_NO_DEBUG
if (sockfd < 0) {
- qWarning("QSocketNotifier: Internal error");
+ qWarning("QEventDispatcherWin32::registerSocketNotifier: invalid socket identifier");
return;
}
if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
- qWarning("QSocketNotifier: socket notifiers cannot be enabled from another thread");
+ qWarning("QEventDispatcherWin32: socket notifiers cannot be enabled from another thread");
return;
}
#endif
@@ -697,11 +697,11 @@ void QEventDispatcherWin32::unregisterSocketNotifier(QSocketNotifier *notifier)
#ifndef QT_NO_DEBUG
int sockfd = notifier->socket();
if (sockfd < 0) {
- qWarning("QSocketNotifier: Internal error");
+ qWarning("QEventDispatcherWin32::unregisterSocketNotifier: invalid socket identifier");
return;
}
if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
- qWarning("QSocketNotifier: socket notifiers cannot be disabled from another thread");
+ qWarning("QEventDispatcherWin32: socket notifiers cannot be disabled from another thread");
return;
}
#endif
@@ -783,8 +783,7 @@ bool QEventDispatcherWin32::unregisterTimer(int timerId)
qWarning("QEventDispatcherWin32::unregisterTimer: invalid argument");
return false;
}
- QThread *currentThread = QThread::currentThread();
- if (thread() != currentThread) {
+ if (thread() != QThread::currentThread()) {
qWarning("QEventDispatcherWin32::unregisterTimer: timers cannot be stopped from another thread");
return false;
}
@@ -811,8 +810,7 @@ bool QEventDispatcherWin32::unregisterTimers(QObject *object)
qWarning("QEventDispatcherWin32::unregisterTimers: invalid argument");
return false;
}
- QThread *currentThread = QThread::currentThread();
- if (object->thread() != thread() || thread() != currentThread) {
+ if (object->thread() != thread() || thread() != QThread::currentThread()) {
qWarning("QEventDispatcherWin32::unregisterTimers: timers cannot be stopped from another thread");
return false;
}
@@ -837,10 +835,12 @@ bool QEventDispatcherWin32::unregisterTimers(QObject *object)
QList<QEventDispatcherWin32::TimerInfo>
QEventDispatcherWin32::registeredTimers(QObject *object) const
{
+#ifndef QT_NO_DEBUG
if (!object) {
qWarning("QEventDispatcherWin32:registeredTimers: invalid argument");
return QList<TimerInfo>();
}
+#endif
Q_D(const QEventDispatcherWin32);
QList<TimerInfo> list;
@@ -853,13 +853,13 @@ QEventDispatcherWin32::registeredTimers(QObject *object) const
bool QEventDispatcherWin32::registerEventNotifier(QWinEventNotifier *notifier)
{
- if (!notifier) {
- qWarning("QWinEventNotifier: Internal error");
- return false;
- } else if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
- qWarning("QWinEventNotifier: event notifiers cannot be enabled from another thread");
+ Q_ASSERT(notifier);
+#ifndef QT_NO_DEBUG
+ if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QEventDispatcherWin32: event notifiers cannot be enabled from another thread");
return false;
}
+#endif
Q_D(QEventDispatcherWin32);
@@ -877,13 +877,13 @@ bool QEventDispatcherWin32::registerEventNotifier(QWinEventNotifier *notifier)
void QEventDispatcherWin32::unregisterEventNotifier(QWinEventNotifier *notifier)
{
- if (!notifier) {
- qWarning("QWinEventNotifier: Internal error");
- return;
- } else if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
- qWarning("QWinEventNotifier: event notifiers cannot be disabled from another thread");
+ Q_ASSERT(notifier);
+#ifndef QT_NO_DEBUG
+ if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
+ qWarning("QEventDispatcherWin32: event notifiers cannot be disabled from another thread");
return;
}
+#endif
Q_D(QEventDispatcherWin32);
@@ -1048,7 +1048,7 @@ bool QEventDispatcherWin32::event(QEvent *e)
void QEventDispatcherWin32::sendPostedEvents()
{
Q_D(QEventDispatcherWin32);
- QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData);
+ QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData.loadRelaxed());
}
HWND QEventDispatcherWin32::internalHwnd()
diff --git a/src/corelib/kernel/qeventloop.cpp b/src/corelib/kernel/qeventloop.cpp
index eacd0c4e73..5a5dfb06aa 100644
--- a/src/corelib/kernel/qeventloop.cpp
+++ b/src/corelib/kernel/qeventloop.cpp
@@ -106,7 +106,7 @@ QEventLoop::QEventLoop(QObject *parent)
if (!QCoreApplication::instance() && QCoreApplicationPrivate::threadRequiresCoreApplication()) {
qWarning("QEventLoop: Cannot be used without QApplication");
} else {
- d->threadData->ensureEventDispatcher();
+ d->threadData.loadRelaxed()->ensureEventDispatcher();
}
}
@@ -133,9 +133,10 @@ QEventLoop::~QEventLoop()
bool QEventLoop::processEvents(ProcessEventsFlags flags)
{
Q_D(QEventLoop);
- if (!d->threadData->hasEventDispatcher())
+ auto threadData = d->threadData.loadRelaxed();
+ if (!threadData->hasEventDispatcher())
return false;
- return d->threadData->eventDispatcher.loadRelaxed()->processEvents(flags);
+ return threadData->eventDispatcher.loadRelaxed()->processEvents(flags);
}
/*!
@@ -164,9 +165,11 @@ bool QEventLoop::processEvents(ProcessEventsFlags flags)
int QEventLoop::exec(ProcessEventsFlags flags)
{
Q_D(QEventLoop);
+ auto threadData = d->threadData.loadRelaxed();
+
//we need to protect from race condition with QThread::exit
- QMutexLocker locker(&static_cast<QThreadPrivate *>(QObjectPrivate::get(d->threadData->thread.loadAcquire()))->mutex);
- if (d->threadData->quitNow)
+ QMutexLocker locker(&static_cast<QThreadPrivate *>(QObjectPrivate::get(threadData->thread.loadAcquire()))->mutex);
+ if (threadData->quitNow)
return -1;
if (d->inExec) {
@@ -183,8 +186,11 @@ int QEventLoop::exec(ProcessEventsFlags flags)
{
d->inExec = true;
d->exit.storeRelease(false);
- ++d->threadData->loopLevel;
- d->threadData->eventLoops.push(d->q_func());
+
+ auto threadData = d->threadData.loadRelaxed();
+ ++threadData->loopLevel;
+ threadData->eventLoops.push(d->q_func());
+
locker.unlock();
}
@@ -198,11 +204,12 @@ int QEventLoop::exec(ProcessEventsFlags flags)
"QCoreApplication::notify() and catch all exceptions there.\n");
}
locker.relock();
- QEventLoop *eventLoop = d->threadData->eventLoops.pop();
+ auto threadData = d->threadData.loadRelaxed();
+ QEventLoop *eventLoop = threadData->eventLoops.pop();
Q_ASSERT_X(eventLoop == d->q_func(), "QEventLoop::exec()", "internal error");
Q_UNUSED(eventLoop); // --release warning
d->inExec = false;
- --d->threadData->loopLevel;
+ --threadData->loopLevel;
}
};
LoopReference ref(d, locker);
@@ -217,7 +224,7 @@ int QEventLoop::exec(ProcessEventsFlags flags)
// exception, which returns control to the browser while preserving the C++ stack.
// Event processing then continues as normal. The sleep call below never returns.
// QTBUG-70185
- if (d->threadData->loopLevel > 1)
+ if (threadData->loopLevel > 1)
emscripten_sleep(1);
#endif
@@ -247,7 +254,7 @@ int QEventLoop::exec(ProcessEventsFlags flags)
void QEventLoop::processEvents(ProcessEventsFlags flags, int maxTime)
{
Q_D(QEventLoop);
- if (!d->threadData->hasEventDispatcher())
+ if (!d->threadData.loadRelaxed()->hasEventDispatcher())
return;
QElapsedTimer start;
@@ -276,21 +283,22 @@ void QEventLoop::processEvents(ProcessEventsFlags flags, int maxTime)
void QEventLoop::exit(int returnCode)
{
Q_D(QEventLoop);
- if (!d->threadData->hasEventDispatcher())
+ auto threadData = d->threadData.loadAcquire();
+ if (!threadData->hasEventDispatcher())
return;
d->returnCode.storeRelaxed(returnCode);
d->exit.storeRelease(true);
- d->threadData->eventDispatcher.loadRelaxed()->interrupt();
+ threadData->eventDispatcher.loadRelaxed()->interrupt();
#ifdef Q_OS_WASM
// QEventLoop::exec() never returns in emscripten. We implement approximate behavior here.
// QTBUG-70185
- if (d->threadData->loopLevel == 1) {
+ if (threadData->loopLevel == 1) {
emscripten_force_exit(returnCode);
} else {
d->inExec = false;
- --d->threadData->loopLevel;
+ --threadData->loopLevel;
}
#endif
}
@@ -316,9 +324,10 @@ bool QEventLoop::isRunning() const
void QEventLoop::wakeUp()
{
Q_D(QEventLoop);
- if (!d->threadData->hasEventDispatcher())
+ auto threadData = d->threadData.loadAcquire();
+ if (!threadData->hasEventDispatcher())
return;
- d->threadData->eventDispatcher.loadRelaxed()->wakeUp();
+ threadData->eventDispatcher.loadRelaxed()->wakeUp();
}
diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp
index b1e1bb8b4a..3883613d49 100644
--- a/src/corelib/kernel/qmetaobject.cpp
+++ b/src/corelib/kernel/qmetaobject.cpp
@@ -154,20 +154,29 @@ QT_BEGIN_NAMESPACE
static inline const QMetaObjectPrivate *priv(const uint* data)
{ return reinterpret_cast<const QMetaObjectPrivate*>(data); }
+static inline const char *rawStringData(const QMetaObject *mo, int index)
+{
+ Q_ASSERT(priv(mo->d.data)->revision >= 7);
+ uint offset = mo->d.stringdata[2*index];
+ return reinterpret_cast<const char *>(mo->d.stringdata) + offset;
+}
+
static inline const QByteArray stringData(const QMetaObject *mo, int index)
{
Q_ASSERT(priv(mo->d.data)->revision >= 7);
- const QByteArrayDataPtr data = { const_cast<QByteArrayData*>(&mo->d.stringdata[index]) };
- Q_ASSERT(data.ptr->ref.isStatic());
- Q_ASSERT(data.ptr->alloc == 0);
- Q_ASSERT(data.ptr->capacityReserved == 0);
- Q_ASSERT(data.ptr->size >= 0);
- return data;
+ uint offset = mo->d.stringdata[2*index];
+ uint length = mo->d.stringdata[2*index + 1];
+ const char *string = reinterpret_cast<const char *>(mo->d.stringdata) + offset;
+ return QByteArray::fromRawData(string, length);
}
-static inline const char *rawStringData(const QMetaObject *mo, int index)
+static inline const char *rawTypeNameFromTypeInfo(const QMetaObject *mo, uint typeInfo)
{
- return stringData(mo, index).data();
+ if (typeInfo & IsUnresolvedType) {
+ return rawStringData(mo, typeInfo & TypeNameIndexMask);
+ } else {
+ return QMetaType::typeName(typeInfo);
+ }
}
static inline QByteArray typeNameFromTypeInfo(const QMetaObject *mo, uint typeInfo)
@@ -181,16 +190,11 @@ static inline QByteArray typeNameFromTypeInfo(const QMetaObject *mo, uint typeIn
}
}
-static inline const char *rawTypeNameFromTypeInfo(const QMetaObject *mo, uint typeInfo)
-{
- return typeNameFromTypeInfo(mo, typeInfo).constData();
-}
-
static inline int typeFromTypeInfo(const QMetaObject *mo, uint typeInfo)
{
if (!(typeInfo & IsUnresolvedType))
return typeInfo;
- return QMetaType::type(stringData(mo, typeInfo & TypeNameIndexMask));
+ return QMetaType::type(rawStringData(mo, typeInfo & TypeNameIndexMask));
}
class QMetaMethodPrivate : public QMetaMethod
@@ -282,14 +286,14 @@ QObject *QMetaObject::newInstance(QGenericArgument val0,
idx = indexOfConstructor(norm.constData());
}
if (idx < 0)
- return 0;
+ return nullptr;
- QObject *returnValue = 0;
+ QObject *returnValue = nullptr;
void *param[] = {&returnValue, val0.data(), val1.data(), val2.data(), val3.data(), val4.data(),
val5.data(), val6.data(), val7.data(), val8.data(), val9.data()};
if (static_metacall(CreateInstance, idx, param) >= 0)
- return 0;
+ return nullptr;
return returnValue;
}
@@ -301,7 +305,7 @@ int QMetaObject::static_metacall(Call cl, int idx, void **argv) const
Q_ASSERT(priv(d.data)->revision >= 6);
if (!d.static_metacall)
return 0;
- d.static_metacall(0, cl, idx, argv);
+ d.static_metacall(nullptr, cl, idx, argv);
return -1;
}
@@ -577,7 +581,7 @@ static bool methodMatch(const QMetaObject *m, int handle,
if (int(m->d.data[handle + 1]) != argc)
return false;
- if (stringData(m, m->d.data[handle]) != name)
+ if (rawStringData(m, m->d.data[handle]) != name)
return false;
int paramsIndex = m->d.data[handle + 2] + 1;
@@ -693,7 +697,7 @@ static void argumentTypesFromString(const char *str, const char *end,
QByteArray QMetaObjectPrivate::decodeMethodSignature(
const char *signature, QArgumentTypeArray &types)
{
- Q_ASSERT(signature != 0);
+ Q_ASSERT(signature != nullptr);
const char *lparens = strchr(signature, '(');
if (!lparens)
return QByteArray();
@@ -846,7 +850,7 @@ int QMetaObjectPrivate::indexOfConstructor(const QMetaObject *m, const QByteArra
*/
int QMetaObjectPrivate::absoluteSignalCount(const QMetaObject *m)
{
- Q_ASSERT(m != 0);
+ Q_ASSERT(m != nullptr);
int n = priv(m->d.data)->signalCount;
for (m = m->d.superdata; m; m = m->d.superdata)
n += priv(m->d.data)->signalCount;
@@ -883,7 +887,7 @@ QMetaMethod QMetaObjectPrivate::signal(const QMetaObject *m, int signal_index)
QMetaMethod result;
if (signal_index < 0)
return result;
- Q_ASSERT(m != 0);
+ Q_ASSERT(m != nullptr);
int i = signal_index;
i -= signalOffset(m);
if (i < 0 && m->d.superdata)
@@ -1033,7 +1037,7 @@ int QMetaObject::indexOfProperty(const char *name) const
QAbstractDynamicMetaObject *me =
const_cast<QAbstractDynamicMetaObject *>(static_cast<const QAbstractDynamicMetaObject *>(this));
- return me->createProperty(name, 0);
+ return me->createProperty(name, nullptr);
}
return -1;
@@ -1147,7 +1151,7 @@ QMetaProperty QMetaObject::property(int index) const
if (!result.menum.isValid()) {
const char *enum_name = type;
const char *scope_name = objectClassName(this);
- char *scope_buffer = 0;
+ char *scope_buffer = nullptr;
const char *colon = strrchr(enum_name, ':');
// ':' will always appear in pairs
@@ -1161,9 +1165,9 @@ QMetaProperty QMetaObject::property(int index) const
enum_name = colon+1;
}
- const QMetaObject *scope = 0;
+ const QMetaObject *scope = nullptr;
if (qstrcmp(scope_name, "Qt") == 0)
- scope = &QObject::staticQtMetaObject;
+ scope = &Qt::staticMetaObject;
else
scope = QMetaObject_findMetaObject(this, scope_name);
if (scope)
@@ -1544,14 +1548,14 @@ bool QMetaObject::invokeMethodImpl(QObject *object, QtPrivate::QSlotObjectBase *
return false;
}
- QCoreApplication::postEvent(object, new QMetaCallEvent(slot, 0, -1, 1));
+ QCoreApplication::postEvent(object, new QMetaCallEvent(slot, nullptr, -1, 1));
} else if (type == Qt::BlockingQueuedConnection) {
#if QT_CONFIG(thread)
if (currentThread == objectThread)
qWarning("QMetaObject::invokeMethod: Dead lock detected");
QSemaphore semaphore;
- QCoreApplication::postEvent(object, new QMetaCallEvent(slot, 0, -1, argv, &semaphore));
+ QCoreApplication::postEvent(object, new QMetaCallEvent(slot, nullptr, -1, argv, &semaphore));
semaphore.acquire();
#endif // QT_CONFIG(thread)
} else {
@@ -1990,7 +1994,7 @@ QList<QByteArray> QMetaMethod::parameterNames() const
const char *QMetaMethod::typeName() const
{
if (!mobj)
- return 0;
+ return nullptr;
return QMetaMethodPrivate::get(this)->rawReturnTypeName();
}
@@ -2022,7 +2026,7 @@ const char *QMetaMethod::typeName() const
const char *QMetaMethod::tag() const
{
if (!mobj)
- return 0;
+ return nullptr;
return QMetaMethodPrivate::get(this)->tag().constData();
}
@@ -2305,7 +2309,7 @@ bool QMetaMethod::invoke(QObject *object,
return false;
}
- QScopedPointer<QMetaCallEvent> event(new QMetaCallEvent(idx_offset, idx_relative, callFunction, 0, -1, paramCount));
+ QScopedPointer<QMetaCallEvent> event(new QMetaCallEvent(idx_offset, idx_relative, callFunction, nullptr, -1, paramCount));
int *types = event->types();
void **args = event->args();
@@ -2342,7 +2346,7 @@ bool QMetaMethod::invoke(QObject *object,
QSemaphore semaphore;
QCoreApplication::postEvent(object, new QMetaCallEvent(idx_offset, idx_relative, callFunction,
- 0, -1, param, &semaphore));
+ nullptr, -1, param, &semaphore));
semaphore.acquire();
#endif // QT_CONFIG(thread)
}
@@ -2565,7 +2569,7 @@ bool QMetaMethod::invokeOnGadget(void* gadget, QGenericReturnArgument returnValu
const char *QMetaEnum::name() const
{
if (!mobj)
- return 0;
+ return nullptr;
return rawStringData(mobj, mobj->d.data[handle]);
}
@@ -2584,7 +2588,7 @@ const char *QMetaEnum::name() const
const char *QMetaEnum::enumName() const
{
if (!mobj)
- return 0;
+ return nullptr;
const bool rev8p = priv(mobj->d.data)->revision >= 8;
if (rev8p)
return rawStringData(mobj, mobj->d.data[handle + 1]);
@@ -2612,13 +2616,13 @@ int QMetaEnum::keyCount() const
const char *QMetaEnum::key(int index) const
{
if (!mobj)
- return 0;
+ return nullptr;
const int offset = priv(mobj->d.data)->revision >= 8 ? 3 : 2;
int count = mobj->d.data[handle + offset];
int data = mobj->d.data[handle + offset + 1];
if (index >= 0 && index < count)
return rawStringData(mobj, mobj->d.data[data + 2*index]);
- return 0;
+ return nullptr;
}
/*!
@@ -2681,7 +2685,7 @@ bool QMetaEnum::isScoped() const
*/
const char *QMetaEnum::scope() const
{
- return mobj ? objectClassName(mobj) : 0;
+ return mobj ? objectClassName(mobj) : nullptr;
}
/*!
@@ -2697,7 +2701,7 @@ const char *QMetaEnum::scope() const
*/
int QMetaEnum::keyToValue(const char *key, bool *ok) const
{
- if (ok != 0)
+ if (ok != nullptr)
*ok = false;
if (!mobj || !key)
return -1;
@@ -2717,7 +2721,7 @@ int QMetaEnum::keyToValue(const char *key, bool *ok) const
const QByteArray className = stringData(mobj, priv(mobj->d.data)->className);
if ((!scope || (className.size() == int(scope) && strncmp(qualified_key, className.constData(), scope) == 0))
&& strcmp(key, rawStringData(mobj, mobj->d.data[data + 2*i])) == 0) {
- if (ok != 0)
+ if (ok != nullptr)
*ok = true;
return mobj->d.data[data + 2*i + 1];
}
@@ -2736,14 +2740,14 @@ int QMetaEnum::keyToValue(const char *key, bool *ok) const
const char* QMetaEnum::valueToKey(int value) const
{
if (!mobj)
- return 0;
+ return nullptr;
const int offset = priv(mobj->d.data)->revision >= 8 ? 3 : 2;
int count = mobj->d.data[handle + offset];
int data = mobj->d.data[handle + offset + 1];
for (int i = 0; i < count; ++i)
if (value == (int)mobj->d.data[data + 2*i + 1])
return rawStringData(mobj, mobj->d.data[data + 2*i]);
- return 0;
+ return nullptr;
}
/*!
@@ -2758,11 +2762,11 @@ const char* QMetaEnum::valueToKey(int value) const
*/
int QMetaEnum::keysToValue(const char *keys, bool *ok) const
{
- if (ok != 0)
+ if (ok != nullptr)
*ok = false;
if (!mobj || !keys)
return -1;
- if (ok != 0)
+ if (ok != nullptr)
*ok = true;
const QString keysString = QString::fromLatin1(keys);
const QVector<QStringRef> splitKeys = keysString.splitRef(QLatin1Char('|'));
@@ -2795,7 +2799,7 @@ int QMetaEnum::keysToValue(const char *keys, bool *ok) const
}
}
if (i < 0) {
- if (ok != 0)
+ if (ok != nullptr)
*ok = false;
value |= -1;
}
@@ -2898,7 +2902,7 @@ static QByteArray qualifiedName(const QMetaEnum &e)
\internal
*/
QMetaProperty::QMetaProperty()
- : mobj(0), handle(0), idx(0)
+ : mobj(nullptr), handle(0), idx(0)
{
}
@@ -2911,7 +2915,7 @@ QMetaProperty::QMetaProperty()
const char *QMetaProperty::name() const
{
if (!mobj)
- return 0;
+ return nullptr;
int handle = priv(mobj->d.data)->propertyData + 3*idx;
return rawStringData(mobj, mobj->d.data[handle]);
}
@@ -2924,7 +2928,7 @@ const char *QMetaProperty::name() const
const char *QMetaProperty::typeName() const
{
if (!mobj)
- return 0;
+ return nullptr;
int handle = priv(mobj->d.data)->propertyData + 3*idx;
return rawTypeNameFromTypeInfo(mobj, mobj->d.data[handle + 1]);
}
@@ -3114,7 +3118,7 @@ QVariant QMetaProperty::read(const QObject *object) const
t = enumMetaTypeId;
} else {
int handle = priv(mobj->d.data)->propertyData + 3*idx;
- const char *typeName = 0;
+ const char *typeName = nullptr;
Q_ASSERT(priv(mobj->d.data)->revision >= 7);
uint typeInfo = mobj->d.data[handle + 1];
if (!(typeInfo & IsUnresolvedType))
@@ -3140,11 +3144,11 @@ QVariant QMetaProperty::read(const QObject *object) const
// changed: result stored directly in value
int status = -1;
QVariant value;
- void *argv[] = { 0, &value, &status };
+ void *argv[] = { nullptr, &value, &status };
if (t == QMetaType::QVariant) {
argv[0] = &value;
} else {
- value = QVariant(t, (void*)0);
+ value = QVariant(t, (void*)nullptr);
argv[0] = value.data();
}
if (priv(mobj->d.data)->flags & PropertyAccessInStaticMetaCall && mobj->d.static_metacall) {
@@ -3198,7 +3202,7 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const
v.convert(QVariant::Int);
} else {
int handle = priv(mobj->d.data)->propertyData + 3*idx;
- const char *typeName = 0;
+ const char *typeName = nullptr;
Q_ASSERT(priv(mobj->d.data)->revision >= 7);
uint typeInfo = mobj->d.data[handle + 1];
if (!(typeInfo & IsUnresolvedType))
@@ -3215,7 +3219,7 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const
if (!value.isValid()) {
if (isResettable())
return reset(object);
- v = QVariant(t, 0);
+ v = QVariant(t, nullptr);
} else if (!v.convert(t)) {
return false;
}
@@ -3231,7 +3235,7 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const
// the flags variable is used by the declarative module to implement
// interception of property writes.
int flags = 0;
- void *argv[] = { 0, &v, &status, &flags };
+ void *argv[] = { nullptr, &v, &status, &flags };
if (t == QMetaType::QVariant)
argv[0] = &v;
else
@@ -3256,7 +3260,7 @@ bool QMetaProperty::reset(QObject *object) const
{
if (!object || !mobj || !isResettable())
return false;
- void *argv[] = { 0 };
+ void *argv[] = { nullptr };
if (priv(mobj->d.data)->flags & PropertyAccessInStaticMetaCall && mobj->d.static_metacall)
mobj->d.static_metacall(object, QMetaObject::ResetProperty, idx, argv);
else
@@ -3640,7 +3644,7 @@ bool QMetaProperty::isEditable(const QObject *object) const
const char *QMetaClassInfo::name() const
{
if (!mobj)
- return 0;
+ return nullptr;
return rawStringData(mobj, mobj->d.data[handle]);
}
@@ -3652,7 +3656,7 @@ const char *QMetaClassInfo::name() const
const char* QMetaClassInfo::value() const
{
if (!mobj)
- return 0;
+ return nullptr;
return rawStringData(mobj, mobj->d.data[handle + 1]);
}
diff --git a/src/corelib/kernel/qmetaobjectbuilder.cpp b/src/corelib/kernel/qmetaobjectbuilder.cpp
index aab9e05182..d02708540a 100644
--- a/src/corelib/kernel/qmetaobjectbuilder.cpp
+++ b/src/corelib/kernel/qmetaobjectbuilder.cpp
@@ -210,7 +210,7 @@ public:
: flags(0)
{
superClass = &QObject::staticMetaObject;
- staticMetacallFunction = 0;
+ staticMetacallFunction = nullptr;
}
bool hasRevisionedProperties() const;
@@ -749,7 +749,7 @@ void QMetaObjectBuilder::addMetaObject
Q_ASSERT(priv(prototype->d.data)->revision >= 2);
const auto *objects = prototype->d.relatedMetaObjects;
if (objects) {
- while (*objects != 0) {
+ while (*objects != nullptr) {
addRelatedMetaObject(*objects);
++objects;
}
@@ -831,7 +831,7 @@ const QMetaObject *QMetaObjectBuilder::relatedMetaObject(int index) const
if (index >= 0 && index < d->relatedMetaObjects.size())
return d->relatedMetaObjects[index];
else
- return 0;
+ return nullptr;
}
/*!
@@ -1103,13 +1103,13 @@ int QMetaStringTable::enter(const QByteArray &value)
int QMetaStringTable::preferredAlignment()
{
- return alignof(QByteArrayData);
+ return alignof(uint);
}
// Returns the size (in bytes) required for serializing this string table.
int QMetaStringTable::blobSize() const
{
- int size = m_entries.size() * sizeof(QByteArrayData);
+ int size = m_entries.size() * 2*sizeof(uint);
Entries::const_iterator it;
for (it = m_entries.constBegin(); it != m_entries.constEnd(); ++it)
size += it.key().size() + 1;
@@ -1120,14 +1120,12 @@ static void writeString(char *out, int i, const QByteArray &str,
const int offsetOfStringdataMember, int &stringdataOffset)
{
int size = str.size();
- qptrdiff offset = offsetOfStringdataMember + stringdataOffset
- - i * sizeof(QByteArrayData);
- const QByteArrayData data =
- Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset);
+ int offset = offsetOfStringdataMember + stringdataOffset;
+ uint offsetLen[2] = { uint(offset), uint(size) };
- memcpy(out + i * sizeof(QByteArrayData), &data, sizeof(QByteArrayData));
+ memcpy(out + 2 * i * sizeof(uint), &offsetLen, 2*sizeof(uint));
- memcpy(out + offsetOfStringdataMember + stringdataOffset, str.constData(), size);
+ memcpy(out + offset, str.constData(), size);
out[offsetOfStringdataMember + stringdataOffset + size] = '\0';
stringdataOffset += size + 1;
@@ -1141,7 +1139,7 @@ void QMetaStringTable::writeBlob(char *out) const
{
Q_ASSERT(!(reinterpret_cast<quintptr>(out) & (preferredAlignment()-1)));
- int offsetOfStringdataMember = m_entries.size() * sizeof(QByteArrayData);
+ int offsetOfStringdataMember = m_entries.size() * 2*sizeof(uint);
int stringdataOffset = 0;
// qt_metacast expects the first string in the string table to be the class name.
@@ -1196,8 +1194,8 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
ALIGN(size, int);
if (buf) {
if (!relocatable) meta->d.superdata = d->superClass;
- meta->d.relatedMetaObjects = 0;
- meta->d.extradata = 0;
+ meta->d.relatedMetaObjects = nullptr;
+ meta->d.extradata = nullptr;
meta->d.static_metacall = d->staticMetacallFunction;
}
@@ -1282,10 +1280,10 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
char *str = reinterpret_cast<char *>(buf + size);
if (buf) {
if (relocatable) {
- meta->d.stringdata = reinterpret_cast<const QByteArrayData *>((quintptr)size);
+ meta->d.stringdata = reinterpret_cast<const uint *>((quintptr)size);
meta->d.data = reinterpret_cast<uint *>((quintptr)pmetaSize);
} else {
- meta->d.stringdata = reinterpret_cast<const QByteArrayData *>(str);
+ meta->d.stringdata = reinterpret_cast<const uint *>(str);
meta->d.data = reinterpret_cast<uint *>(data);
}
}
@@ -1494,7 +1492,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
*/
QMetaObject *QMetaObjectBuilder::toMetaObject() const
{
- int size = buildMetaObject(d, 0, 0, false);
+ int size = buildMetaObject(d, nullptr, 0, false);
char *buf = reinterpret_cast<char *>(malloc(size));
memset(buf, 0, size);
buildMetaObject(d, buf, size, false);
@@ -1517,7 +1515,7 @@ QMetaObject *QMetaObjectBuilder::toMetaObject() const
*/
QByteArray QMetaObjectBuilder::toRelocatableData(bool *ok) const
{
- int size = buildMetaObject(d, 0, 0, true);
+ int size = buildMetaObject(d, nullptr, 0, true);
if (size == -1) {
if (ok) *ok = false;
return QByteArray();
@@ -1553,11 +1551,11 @@ void QMetaObjectBuilder::fromRelocatableData(QMetaObject *output,
quintptr dataOffset = (quintptr)dataMo->d.data;
output->d.superdata = superclass;
- output->d.stringdata = reinterpret_cast<const QByteArrayData *>(buf + stringdataOffset);
+ output->d.stringdata = reinterpret_cast<const uint *>(buf + stringdataOffset);
output->d.data = reinterpret_cast<const uint *>(buf + dataOffset);
- output->d.extradata = 0;
- output->d.relatedMetaObjects = 0;
- output->d.static_metacall = 0;
+ output->d.extradata = nullptr;
+ output->d.relatedMetaObjects = nullptr;
+ output->d.static_metacall = nullptr;
}
/*!
@@ -1720,14 +1718,14 @@ void QMetaObjectBuilder::deserialize
d->enumerators.clear();
d->constructors.clear();
d->relatedMetaObjects.clear();
- d->staticMetacallFunction = 0;
+ d->staticMetacallFunction = nullptr;
// Read the class and super class names.
stream >> d->className;
stream >> name;
if (name.isEmpty()) {
- d->superClass = 0;
- } else if ((cl = resolveClassName(references, name)) != 0) {
+ d->superClass = nullptr;
+ } else if ((cl = resolveClassName(references, name)) != nullptr) {
d->superClass = cl;
} else {
stream.setStatus(QDataStream::ReadCorruptData);
@@ -1877,7 +1875,7 @@ QMetaMethodBuilderPrivate *QMetaMethodBuilder::d_func() const
else if (_mobj && -_index >= 1 && -_index <= int(_mobj->d->constructors.size()))
return &(_mobj->d->constructors[(-_index) - 1]);
else
- return 0;
+ return nullptr;
}
/*!
@@ -2116,7 +2114,7 @@ QMetaPropertyBuilderPrivate *QMetaPropertyBuilder::d_func() const
if (_mobj && _index >= 0 && _index < int(_mobj->d->properties.size()))
return &(_mobj->d->properties[_index]);
else
- return 0;
+ return nullptr;
}
/*!
@@ -2588,7 +2586,7 @@ QMetaEnumBuilderPrivate *QMetaEnumBuilder::d_func() const
if (_mobj && _index >= 0 && _index < int(_mobj->d->enumerators.size()))
return &(_mobj->d->enumerators[_index]);
else
- return 0;
+ return nullptr;
}
/*!
diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp
index 5c1e4b4ddd..79d6d0aa93 100644
--- a/src/corelib/kernel/qmetatype.cpp
+++ b/src/corelib/kernel/qmetatype.cpp
@@ -508,19 +508,43 @@ struct DefinedTypesFilter {
Destructs this object.
*/
+/*!
+ \fn template<typename T> QMetaType QMetaType::fromType()
+ \since 5.15
+
+ Returns the QMetaType corresponding to the type in the template parameter.
+*/
+
+/*! \fn bool operator==(const QMetaType &a, const QMetaType &b)
+ \since 5.15
+ \relates QMetaType
+ \overload
+
+ Returns \c true if the QMetaType \a a represents the same type
+ as the QMetaType \a b, otherwise returns \c false.
+*/
+
+/*! \fn bool operator!=(const QMetaType &a, const QMetaType &c)
+ \since 5.15
+ \relates QMetaType
+ \overload
+
+ Returns \c true if the QMetaType \a a represents a difference type
+ than the QMetaType \a b, otherwise returns \c false.
+*/
+
#define QT_ADD_STATIC_METATYPE(MetaTypeName, MetaTypeId, RealName) \
{ #RealName, sizeof(#RealName) - 1, MetaTypeId },
#define QT_ADD_STATIC_METATYPE_ALIASES_ITER(MetaTypeName, MetaTypeId, AliasingName, RealNameStr) \
{ RealNameStr, sizeof(RealNameStr) - 1, QMetaType::MetaTypeName },
-#define QT_ADD_STATIC_METATYPE_HACKS_ITER(MetaTypeName, TypeId, Name) \
- QT_ADD_STATIC_METATYPE(MetaTypeName, MetaTypeName, Name)
+
static const struct { const char * typeName; int typeNameLength; int type; } types[] = {
QT_FOR_EACH_STATIC_TYPE(QT_ADD_STATIC_METATYPE)
QT_FOR_EACH_STATIC_ALIAS_TYPE(QT_ADD_STATIC_METATYPE_ALIASES_ITER)
- QT_FOR_EACH_STATIC_HACKS_TYPE(QT_ADD_STATIC_METATYPE_HACKS_ITER)
+ QT_ADD_STATIC_METATYPE(_, QMetaTypeId2<qreal>::MetaType, qreal)
{nullptr, 0, QMetaType::UnknownType}
};
@@ -932,7 +956,7 @@ constexpr MetaTypeOffsets<QtPrivate::Indexes<QMetaType::HighestInternalId + 1>::
pointer if no matching type was found. The returned pointer must not be
deleted.
- \sa type(), isRegistered(), Type
+ \sa type(), isRegistered(), Type, name()
*/
const char *QMetaType::typeName(int typeId)
{
@@ -952,6 +976,20 @@ const char *QMetaType::typeName(int typeId)
#undef QT_METATYPE_TYPEID_TYPENAME_CONVERTER
}
+/*!
+ \since 5.15
+
+ Returns the type name associated with this QMetaType, or a null
+ pointer if no matching type was found. The returned pointer must not be
+ deleted.
+
+ \sa typeName()
+*/
+QByteArray QMetaType::name() const
+{
+ return QMetaType::typeName(m_typeId);
+}
+
/*
Similar to QMetaType::type(), but only looks in the static set of types.
*/
@@ -1115,8 +1153,8 @@ static int registerNormalizedType(const NS(QByteArray) &normalizedTypeName,
QCustomTypeInfo inf;
inf.typeName = normalizedTypeName;
#ifndef QT_NO_DATASTREAM
- inf.loadOp = 0;
- inf.saveOp = 0;
+ inf.loadOp = nullptr;
+ inf.saveOp = nullptr;
#endif
inf.alias = -1;
inf.typedConstructor = typedConstructor;
@@ -1957,7 +1995,7 @@ public:
return Q_LIKELY(qMetaTypeWidgetsHelper)
? qMetaTypeWidgetsHelper[type - QMetaType::FirstWidgetsType].metaObject
: nullptr;
- return 0;
+ return nullptr;
}
};
@@ -2216,6 +2254,8 @@ QMetaType QMetaType::typeInfo(const int type)
\since 5.0
Constructs a QMetaType object that contains all information about type \a typeId.
+
+ \note: The default parameter was added in Qt 5.15
*/
QMetaType::QMetaType(const int typeId)
: m_typeId(typeId)
diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h
index caa158841c..7628f5f6b3 100644
--- a/src/corelib/kernel/qmetatype.h
+++ b/src/corelib/kernel/qmetatype.h
@@ -187,10 +187,6 @@ inline Q_DECL_CONSTEXPR int qMetaTypeId();
#define QT_FOR_EACH_STATIC_WIDGETS_CLASS(F)\
F(QSizePolicy, 121, QSizePolicy) \
-// ### FIXME kill that set
-#define QT_FOR_EACH_STATIC_HACKS_TYPE(F)\
- F(QMetaTypeId2<qreal>::MetaType, -1, qreal)
-
// F is a tuple: (QMetaType::TypeName, QMetaType::TypeNameID, AliasingType, "RealType")
#define QT_FOR_EACH_STATIC_ALIAS_TYPE(F)\
F(ULong, -1, ulong, "unsigned long") \
@@ -579,7 +575,7 @@ public:
static bool load(QDataStream &stream, int type, void *data);
#endif
- explicit QMetaType(const int type); // ### Qt6: drop const
+ explicit QMetaType(const int type = QMetaType::UnknownType); // ### Qt6: drop const
inline ~QMetaType();
inline bool isValid() const;
@@ -588,12 +584,24 @@ public:
inline int sizeOf() const;
inline TypeFlags flags() const;
inline const QMetaObject *metaObject() const;
+ QT_PREPEND_NAMESPACE(QByteArray) name() const;
inline void *create(const void *copy = nullptr) const;
inline void destroy(void *data) const;
inline void *construct(void *where, const void *copy = nullptr) const;
inline void destruct(void *data) const;
+ template<typename T>
+ static QMetaType fromType()
+ { return QMetaType(qMetaTypeId<T>()); }
+
+ friend bool operator==(const QMetaType &a, const QMetaType &b)
+ { return a.m_typeId == b.m_typeId; }
+
+ friend bool operator!=(const QMetaType &a, const QMetaType &b)
+ { return a.m_typeId != b.m_typeId; }
+
+
public:
template<typename T>
static bool registerComparators()
@@ -743,8 +751,6 @@ private:
static bool registerDebugStreamOperatorFunction(const QtPrivate::AbstractDebugStreamFunction *f, int type);
#endif
-// ### Qt6: FIXME: Remove the special Q_CC_MSVC handling, it was introduced to maintain BC.
-#if !defined(Q_NO_TEMPLATE_FRIENDS) && !defined(Q_CC_MSVC)
#ifndef Q_CLANG_QDOC
template<typename, bool> friend struct QtPrivate::ValueTypeIsMetaType;
template<typename, typename> friend struct QtPrivate::ConverterMemberFunction;
@@ -754,9 +760,6 @@ private:
template<typename, bool> friend struct QtPrivate::IsMetaTypePair;
template<typename, typename> friend struct QtPrivate::MetaTypeSmartPointerHelper;
#endif
-#else
-public:
-#endif
static bool registerConverterFunction(const QtPrivate::AbstractConverterFunction *f, int from, int to);
static void unregisterConverterFunction(int from, int to);
private:
@@ -1215,9 +1218,12 @@ public:
{ IteratorOwner<typename T::const_iterator>::assign(iterator,
static_cast<const T*>(container)->find(*static_cast<const typename T::key_type*>(p))); }
+ QT_WARNING_PUSH
+ QT_WARNING_DISABLE_DEPRECATED // Hits on the deprecated QHash::iterator::operator--()
template<class T>
static void advanceImpl(void **p, int step)
{ std::advance(*static_cast<typename T::const_iterator*>(*p), step); }
+ QT_WARNING_POP
template<class T>
static void beginImpl(const void *container, void **iterator)
diff --git a/src/corelib/kernel/qmimedata.cpp b/src/corelib/kernel/qmimedata.cpp
index 9a98db21d3..00e5183eb1 100644
--- a/src/corelib/kernel/qmimedata.cpp
+++ b/src/corelib/kernel/qmimedata.cpp
@@ -319,7 +319,7 @@ QVariant QMimeDataPrivate::retrieveTypedData(const QString &format, QVariant::Ty
Constructs a new MIME data object with no data in it.
*/
QMimeData::QMimeData()
- : QObject(*new QMimeDataPrivate, 0)
+ : QObject(*new QMimeDataPrivate, nullptr)
{
}
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp
index d713555bb6..c72383ff4a 100644
--- a/src/corelib/kernel/qobject.cpp
+++ b/src/corelib/kernel/qobject.cpp
@@ -109,7 +109,7 @@ static int *queuedConnectionTypes(const QList<QByteArray> &typeNames)
"(Make sure '%s' is registered using qRegisterMetaType().)",
typeName.constData(), typeName.constData());
delete [] types;
- return 0;
+ return nullptr;
}
}
types[typeNames.count()] = 0;
@@ -133,7 +133,7 @@ static int *queuedConnectionTypes(const QArgumentType *argumentTypes, int argc)
qWarning("QObject::connect: Cannot queue arguments of type '%s'\n"
"(Make sure '%s' is registered using qRegisterMetaType().)",
type.name().constData(), type.name().constData());
- return 0;
+ return nullptr;
}
}
types[argc] = 0;
@@ -160,12 +160,12 @@ extern "C" Q_CORE_EXPORT void qt_removeObject(QObject *)
{}
#endif
-void (*QAbstractDeclarativeData::destroyed)(QAbstractDeclarativeData *, QObject *) = 0;
-void (*QAbstractDeclarativeData::parentChanged)(QAbstractDeclarativeData *, QObject *, QObject *) = 0;
-void (*QAbstractDeclarativeData::signalEmitted)(QAbstractDeclarativeData *, QObject *, int, void **) = 0;
-int (*QAbstractDeclarativeData::receivers)(QAbstractDeclarativeData *, const QObject *, int) = 0;
-bool (*QAbstractDeclarativeData::isSignalConnected)(QAbstractDeclarativeData *, const QObject *, int) = 0;
-void (*QAbstractDeclarativeData::setWidgetParent)(QObject *, QObject *) = 0;
+void (*QAbstractDeclarativeData::destroyed)(QAbstractDeclarativeData *, QObject *) = nullptr;
+void (*QAbstractDeclarativeData::parentChanged)(QAbstractDeclarativeData *, QObject *, QObject *) = nullptr;
+void (*QAbstractDeclarativeData::signalEmitted)(QAbstractDeclarativeData *, QObject *, int, void **) = nullptr;
+int (*QAbstractDeclarativeData::receivers)(QAbstractDeclarativeData *, const QObject *, int) = nullptr;
+bool (*QAbstractDeclarativeData::isSignalConnected)(QAbstractDeclarativeData *, const QObject *, int) = nullptr;
+void (*QAbstractDeclarativeData::setWidgetParent)(QObject *, QObject *) = nullptr;
/*!
\fn QObjectData::QObjectData()
@@ -181,7 +181,7 @@ QMetaObject *QObjectData::dynamicMetaObject() const
}
QObjectPrivate::QObjectPrivate(int version)
- : threadData(0), currentChildBeingDeleted(0)
+ : threadData(nullptr), currentChildBeingDeleted(nullptr)
{
#ifdef QT_BUILD_INTERNAL
// Don't check the version parameter in internal builds.
@@ -194,8 +194,8 @@ QObjectPrivate::QObjectPrivate(int version)
#endif
// QObjectData initialization
- q_ptr = 0;
- parent = 0; // no parent yet. It is set by setParent()
+ q_ptr = nullptr;
+ parent = nullptr; // no parent yet. It is set by setParent()
isWidget = false; // assume not a widget object
blockSig = false; // not blocking signals
wasDeleted = false; // double-delete catcher
@@ -203,19 +203,20 @@ QObjectPrivate::QObjectPrivate(int version)
sendChildEvents = true; // if we should send ChildAdded and ChildRemoved events to parent
receiveChildEvents = true;
postedEvents = 0;
- extraData = 0;
- metaObject = 0;
+ extraData = nullptr;
+ metaObject = nullptr;
isWindow = false;
deleteLaterCalled = false;
}
QObjectPrivate::~QObjectPrivate()
{
+ auto thisThreadData = threadData.loadRelaxed();
if (extraData && !extraData->runningTimers.isEmpty()) {
- if (Q_LIKELY(threadData->thread.loadAcquire() == QThread::currentThread())) {
+ if (Q_LIKELY(thisThreadData->thread.loadAcquire() == QThread::currentThread())) {
// unregister pending timers
- if (threadData->hasEventDispatcher())
- threadData->eventDispatcher.loadRelaxed()->unregisterTimers(q_ptr);
+ if (thisThreadData->hasEventDispatcher())
+ thisThreadData->eventDispatcher.loadRelaxed()->unregisterTimers(q_ptr);
// release the timer ids back to the pool
for (int i = 0; i < extraData->runningTimers.size(); ++i)
@@ -228,7 +229,7 @@ QObjectPrivate::~QObjectPrivate()
if (postedEvents)
QCoreApplication::removePostedEvents(q_ptr, 0);
- threadData->deref();
+ thisThreadData->deref();
if (metaObject) metaObject->objectDestroyed(q_ptr);
@@ -915,12 +916,13 @@ QObject::QObject(QObjectPrivate &dd, QObject *parent)
Q_D(QObject);
d_ptr->q_ptr = this;
- d->threadData = (parent && !parent->thread()) ? parent->d_func()->threadData : QThreadData::current();
- d->threadData->ref();
+ auto threadData = (parent && !parent->thread()) ? parent->d_func()->threadData.loadRelaxed() : QThreadData::current();
+ threadData->ref();
+ d->threadData.storeRelaxed(threadData);
if (parent) {
QT_TRY {
- if (!check_parent_thread(parent, parent ? parent->d_func()->threadData : 0, d->threadData))
- parent = 0;
+ if (!check_parent_thread(parent, parent ? parent->d_func()->threadData.loadRelaxed() : nullptr, threadData))
+ parent = nullptr;
if (d->isWidget) {
if (parent) {
d->parent = parent;
@@ -931,7 +933,7 @@ QObject::QObject(QObjectPrivate &dd, QObject *parent)
setParent(parent);
}
} QT_CATCH(...) {
- d->threadData->deref();
+ threadData->deref();
QT_RETHROW;
}
}
@@ -1081,7 +1083,7 @@ QObject::~QObject()
Q_TRACE(QObject_dtor, this);
if (d->parent) // remove it from parent object
- d->setParent_helper(0);
+ d->setParent_helper(nullptr);
}
QObjectPrivate::Connection::~Connection()
@@ -1151,8 +1153,8 @@ QObjectPrivate::Connection::~Connection()
\relates QObject
Returns the given \a object cast to type T if the object is of type
- T (or of a subclass); otherwise returns 0. If \a object is 0 then
- it will also return 0.
+ T (or of a subclass); otherwise returns \nullptr. If \a object is
+ \nullptr then it will also return \nullptr.
The class T must inherit (directly or indirectly) QObject and be
declared with the \l Q_OBJECT macro.
@@ -1308,7 +1310,7 @@ bool QObject::event(QEvent *e)
case QEvent::ThreadChange: {
Q_D(QObject);
- QThreadData *threadData = d->threadData;
+ QThreadData *threadData = d->threadData.loadRelaxed();
QAbstractEventDispatcher *eventDispatcher = threadData->eventDispatcher.loadRelaxed();
if (eventDispatcher) {
QList<QAbstractEventDispatcher::TimerInfo> timers = eventDispatcher->registeredTimers(this);
@@ -1475,7 +1477,7 @@ bool QObject::blockSignals(bool block) noexcept
*/
QThread *QObject::thread() const
{
- return d_func()->threadData->thread.loadAcquire();
+ return d_func()->threadData.loadRelaxed()->thread.loadAcquire();
}
/*!
@@ -1504,7 +1506,7 @@ QThread *QObject::thread() const
the thread affinity is changed. You can handle this event to
perform any special processing. Note that any new events that are
posted to this object will be handled in the \a targetThread,
- provided it is non-null: when it is \nullptr, no event processing
+ provided it is not \nullptr: when it is \nullptr, no event processing
for this object or its children can happen, as they are no longer
associated with any thread.
@@ -1522,12 +1524,12 @@ void QObject::moveToThread(QThread *targetThread)
{
Q_D(QObject);
- if (d->threadData->thread.loadAcquire() == targetThread) {
+ if (d->threadData.loadRelaxed()->thread.loadAcquire() == targetThread) {
// object is already in this thread
return;
}
- if (d->parent != 0) {
+ if (d->parent != nullptr) {
qWarning("QObject::moveToThread: Cannot move objects with a parent");
return;
}
@@ -1538,13 +1540,14 @@ void QObject::moveToThread(QThread *targetThread)
QThreadData *currentData = QThreadData::current();
QThreadData *targetData = targetThread ? QThreadData::get2(targetThread) : nullptr;
- if (d->threadData->thread.loadAcquire() == 0 && currentData == targetData) {
+ QThreadData *thisThreadData = d->threadData.loadRelaxed();
+ if (!thisThreadData->thread.loadAcquire() && currentData == targetData) {
// one exception to the rule: we allow moving objects with no thread affinity to the current thread
currentData = d->threadData;
- } else if (d->threadData != currentData) {
+ } else if (thisThreadData != currentData) {
qWarning("QObject::moveToThread: Current thread (%p) is not the object's thread (%p).\n"
"Cannot move to target thread (%p)\n",
- currentData->thread.loadRelaxed(), d->threadData->thread.loadRelaxed(), targetData ? targetData->thread.loadRelaxed() : nullptr);
+ currentData->thread.loadRelaxed(), thisThreadData->thread.loadRelaxed(), targetData ? targetData->thread.loadRelaxed() : nullptr);
#ifdef Q_OS_MAC
qWarning("You might be loading two sets of Qt binaries into the same process. "
@@ -1603,7 +1606,7 @@ void QObjectPrivate::setThreadData_helper(QThreadData *currentData, QThreadData
if (pe.receiver == q) {
// move this post event to the targetList
targetData->postEventList.addEvent(pe);
- const_cast<QPostEvent &>(pe).event = 0;
+ const_cast<QPostEvent &>(pe).event = nullptr;
++eventsMoved;
}
}
@@ -1641,8 +1644,10 @@ void QObjectPrivate::setThreadData_helper(QThreadData *currentData, QThreadData
// set new thread data
targetData->ref();
- threadData->deref();
- threadData = targetData;
+ threadData.loadRelaxed()->deref();
+
+ // synchronizes with loadAcquire e.g. in QCoreApplication::postEvent
+ threadData.storeRelease(targetData);
for (int i = 0; i < children.size(); ++i) {
QObject *child = children.at(i);
@@ -1654,7 +1659,7 @@ void QObjectPrivate::_q_reregisterTimers(void *pointer)
{
Q_Q(QObject);
QList<QAbstractEventDispatcher::TimerInfo> *timerList = reinterpret_cast<QList<QAbstractEventDispatcher::TimerInfo> *>(pointer);
- QAbstractEventDispatcher *eventDispatcher = threadData->eventDispatcher.loadRelaxed();
+ QAbstractEventDispatcher *eventDispatcher = threadData.loadRelaxed()->eventDispatcher.loadRelaxed();
for (int i = 0; i < timerList->size(); ++i) {
const QAbstractEventDispatcher::TimerInfo &ti = timerList->at(i);
eventDispatcher->registerTimer(ti.timerId, ti.interval, ti.timerType, q);
@@ -1712,7 +1717,9 @@ int QObject::startTimer(int interval, Qt::TimerType timerType)
qWarning("QObject::startTimer: Timers cannot have negative intervals");
return 0;
}
- if (Q_UNLIKELY(!d->threadData->hasEventDispatcher())) {
+
+ auto thisThreadData = d->threadData.loadRelaxed();
+ if (Q_UNLIKELY(!thisThreadData->hasEventDispatcher())) {
qWarning("QObject::startTimer: Timers can only be used with threads started with QThread");
return 0;
}
@@ -1720,7 +1727,7 @@ int QObject::startTimer(int interval, Qt::TimerType timerType)
qWarning("QObject::startTimer: Timers cannot be started from another thread");
return 0;
}
- int timerId = d->threadData->eventDispatcher.loadRelaxed()->registerTimer(interval, timerType, this);
+ int timerId = thisThreadData->eventDispatcher.loadRelaxed()->registerTimer(interval, timerType, this);
if (!d->extraData)
d->extraData = new QObjectPrivate::ExtraData;
d->extraData->runningTimers.append(timerId);
@@ -1794,8 +1801,9 @@ void QObject::killTimer(int id)
return;
}
- if (d->threadData->hasEventDispatcher())
- d->threadData->eventDispatcher.loadRelaxed()->unregisterTimer(id);
+ auto thisThreadData = d->threadData.loadRelaxed();
+ if (thisThreadData->hasEventDispatcher())
+ thisThreadData->eventDispatcher.loadRelaxed()->unregisterTimer(id);
d->extraData->runningTimers.remove(at);
QAbstractEventDispatcherPrivate::releaseTimerId(id);
@@ -2045,7 +2053,7 @@ void qt_qFindChildren_helper(const QObject *parent, const QRegularExpression &re
QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo, Qt::FindChildOptions options)
{
if (!parent)
- return 0;
+ return nullptr;
const QObjectList &children = parent->children();
QObject *obj;
int i;
@@ -2061,7 +2069,7 @@ QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const
return obj;
}
}
- return 0;
+ return nullptr;
}
/*!
@@ -2089,7 +2097,7 @@ void QObjectPrivate::deleteChildren()
delete currentChildBeingDeleted;
}
children.clear();
- currentChildBeingDeleted = 0;
+ currentChildBeingDeleted = nullptr;
isDeletingChildren = false;
}
@@ -2141,7 +2149,7 @@ void QObjectPrivate::setParent_helper(QObject *o)
// object hierarchies are constrained to a single thread
if (threadData != parent->d_func()->threadData) {
qWarning("QObject::setParent: Cannot set parent, new parent is in a different thread");
- parent = 0;
+ parent = nullptr;
return;
}
parent->d_func()->children.append(q);
@@ -2212,7 +2220,7 @@ void QObject::installEventFilter(QObject *obj)
d->extraData = new QObjectPrivate::ExtraData;
// clean up unused items in the list
- d->extraData->eventFilters.removeAll((QObject*)0);
+ d->extraData->eventFilters.removeAll((QObject*)nullptr);
d->extraData->eventFilters.removeAll(obj);
d->extraData->eventFilters.prepend(obj);
}
@@ -2236,7 +2244,7 @@ void QObject::removeEventFilter(QObject *obj)
if (d->extraData) {
for (int i = 0; i < d->extraData->eventFilters.count(); ++i) {
if (d->extraData->eventFilters.at(i) == obj)
- d->extraData->eventFilters[i] = 0;
+ d->extraData->eventFilters[i] = nullptr;
}
}
}
@@ -2303,7 +2311,7 @@ void QObject::deleteLater()
If the same \a sourceText is used in different roles within the
same context, an additional identifying string may be passed in
- \a disambiguation (0 by default). In Qt 4.4 and earlier, this was
+ \a disambiguation (\nullptr by default). In Qt 4.4 and earlier, this was
the preferred way to pass comments to translators.
Example:
@@ -2359,7 +2367,7 @@ void QObject::deleteLater()
const char *qFlagLocation(const char *method)
{
QThreadData *currentThreadData = QThreadData::current(false);
- if (currentThreadData != 0)
+ if (currentThreadData != nullptr)
currentThreadData->flaggedSignatures.store(method);
return method;
}
@@ -2378,7 +2386,7 @@ static const char * extract_location(const char *member)
if (*location != '\0')
return location;
}
- return 0;
+ return nullptr;
}
static bool check_signal_macro(const QObject *sender, const char *signal,
@@ -2417,7 +2425,7 @@ static void err_method_notfound(const QObject *object,
case QSIGNAL_CODE: type = "signal"; break;
}
const char *loc = extract_location(method);
- if (strchr(method,')') == 0) // common typing mistake
+ if (strchr(method,')') == nullptr) // common typing mistake
qWarning("QObject::%s: Parentheses expected, %s %s::%s%s%s",
func, type, object->metaObject()->className(), method+1,
loc ? " in ": "", loc ? loc : "");
@@ -2477,7 +2485,7 @@ QObject *QObject::sender() const
return cd->currentSender->sender;
}
- return 0;
+ return nullptr;
}
/*!
@@ -2489,7 +2497,7 @@ QObject *QObject::sender() const
For signals with default parameters, this function will always return
the index with all parameters, regardless of which was used with
- connect(). For example, the signal \c {destroyed(QObject *obj = 0)}
+ connect(). For example, the signal \c {destroyed(QObject *obj = \nullptr)}
will have two different indexes (with and without the parameter), but
this function will always return the index with a parameter. This does
not apply when overloading signals with different parameters.
@@ -2630,7 +2638,7 @@ bool QObject::isSignalConnected(const QMetaMethod &signal) const
member in the specified class.
\list
- \li If member.mobj is 0 then both signalIndex and methodIndex are set to -1.
+ \li If member.mobj is \nullptr then both signalIndex and methodIndex are set to -1.
\li If specified member is not a member of obj instance class (or one of
its parent classes) then both signalIndex and methodIndex are set to -1.
@@ -2656,7 +2664,7 @@ void QMetaObjectPrivate::memberIndexes(const QObject *obj,
return;
const QMetaObject *m = obj->metaObject();
// Check that member is member of obj class
- while (m != 0 && m != member.mobj)
+ while (m != nullptr && m != member.mobj)
m = m->d.superdata;
if (!m)
return;
@@ -2767,18 +2775,18 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
const QObject *receiver, const char *method,
Qt::ConnectionType type)
{
- if (sender == 0 || receiver == 0 || signal == 0 || method == 0) {
+ if (sender == nullptr || receiver == nullptr || signal == nullptr || method == nullptr) {
qWarning("QObject::connect: Cannot connect %s::%s to %s::%s",
- sender ? sender->metaObject()->className() : "(null)",
- (signal && *signal) ? signal+1 : "(null)",
- receiver ? receiver->metaObject()->className() : "(null)",
- (method && *method) ? method+1 : "(null)");
- return QMetaObject::Connection(0);
+ sender ? sender->metaObject()->className() : "(nullptr)",
+ (signal && *signal) ? signal+1 : "(nullptr)",
+ receiver ? receiver->metaObject()->className() : "(nullptr)",
+ (method && *method) ? method+1 : "(nullptr)");
+ return QMetaObject::Connection(nullptr);
}
QByteArray tmp_signal_name;
if (!check_signal_macro(sender, signal, "connect", "bind"))
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
const QMetaObject *smeta = sender->metaObject();
const char *signal_arg = signal;
++signal; //skip code
@@ -2801,7 +2809,7 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
if (signal_index < 0) {
err_method_notfound(sender, signal_arg, "connect");
err_info_about_objects("connect", sender, receiver);
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
}
signal_index = QMetaObjectPrivate::originalClone(smeta, signal_index);
signal_index += QMetaObjectPrivate::signalOffset(smeta);
@@ -2810,7 +2818,7 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
int membcode = extract_code(method);
if (!check_method_code(membcode, receiver, method, "connect"))
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
const char *method_arg = method;
++method; // skip code
@@ -2853,7 +2861,7 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
if (method_index_relative < 0) {
err_method_notfound(receiver, method_arg, "connect");
err_info_about_objects("connect", sender, receiver);
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
}
if (!QMetaObjectPrivate::checkConnectArgs(signalTypes.size(), signalTypes.constData(),
@@ -2862,13 +2870,13 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
"\n %s::%s --> %s::%s",
sender->metaObject()->className(), signal,
receiver->metaObject()->className(), method);
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
}
- int *types = 0;
+ int *types = nullptr;
if ((type == Qt::QueuedConnection)
&& !(types = queuedConnectionTypes(signalTypes.constData(), signalTypes.size()))) {
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
}
#ifndef QT_NO_DEBUG
@@ -2905,16 +2913,16 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMetho
const QObject *receiver, const QMetaMethod &method,
Qt::ConnectionType type)
{
- if (sender == 0
- || receiver == 0
+ if (sender == nullptr
+ || receiver == nullptr
|| signal.methodType() != QMetaMethod::Signal
|| method.methodType() == QMetaMethod::Constructor) {
qWarning("QObject::connect: Cannot connect %s::%s to %s::%s",
- sender ? sender->metaObject()->className() : "(null)",
+ sender ? sender->metaObject()->className() : "(nullptr)",
signal.methodSignature().constData(),
- receiver ? receiver->metaObject()->className() : "(null)",
+ receiver ? receiver->metaObject()->className() : "(nullptr)",
method.methodSignature().constData() );
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
}
int signal_index;
@@ -2930,12 +2938,12 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMetho
if (signal_index == -1) {
qWarning("QObject::connect: Can't find signal %s on instance of class %s",
signal.methodSignature().constData(), smeta->className());
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
}
if (method_index == -1) {
qWarning("QObject::connect: Can't find method %s on instance of class %s",
method.methodSignature().constData(), rmeta->className());
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
}
if (!QMetaObject::checkConnectArgs(signal.methodSignature().constData(), method.methodSignature().constData())) {
@@ -2943,19 +2951,19 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMetho
"\n %s::%s --> %s::%s",
smeta->className(), signal.methodSignature().constData(),
rmeta->className(), method.methodSignature().constData());
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
}
- int *types = 0;
+ int *types = nullptr;
if ((type == Qt::QueuedConnection)
&& !(types = queuedConnectionTypes(signal.parameterTypes())))
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
#ifndef QT_NO_DEBUG
check_and_warn_compat(smeta, signal, rmeta, method);
#endif
QMetaObject::Connection handle = QMetaObject::Connection(QMetaObjectPrivate::connect(
- sender, signal_index, signal.enclosingMetaObject(), receiver, method_index, 0, type, types));
+ sender, signal_index, signal.enclosingMetaObject(), receiver, method_index, nullptr, type, types));
return handle;
}
@@ -3014,20 +3022,20 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMetho
\endlist
- 0 may be used as a wildcard, meaning "any signal", "any receiving
+ \nullptr may be used as a wildcard, meaning "any signal", "any receiving
object", or "any slot in the receiving object", respectively.
The \a sender may never be \nullptr. (You cannot disconnect signals
from more than one object in a single call.)
- If \a signal is 0, it disconnects \a receiver and \a method from
+ If \a signal is \nullptr, it disconnects \a receiver and \a method from
any signal. If not, only the specified signal is disconnected.
- If \a receiver is 0, it disconnects anything connected to \a
+ If \a receiver is \nullptr, it disconnects anything connected to \a
signal. If not, slots in objects other than \a receiver are not
disconnected.
- If \a method is 0, it disconnects anything that is connected to \a
+ If \a method is \nullptr, it disconnects anything that is connected to \a
receiver. If not, only slots named \a method will be disconnected,
and all other slots are left alone. The \a method must be \nullptr
if \a receiver is left out, so you cannot disconnect a
@@ -3038,8 +3046,8 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMetho
bool QObject::disconnect(const QObject *sender, const char *signal,
const QObject *receiver, const char *method)
{
- if (sender == 0 || (receiver == 0 && method != 0)) {
- qWarning("QObject::disconnect: Unexpected null parameter");
+ if (sender == nullptr || (receiver == nullptr && method != nullptr)) {
+ qWarning("QObject::disconnect: Unexpected nullptr parameter");
return false;
}
@@ -3110,7 +3118,7 @@ bool QObject::disconnect(const QObject *sender, const char *signal,
}
if (!method) {
- res |= QMetaObjectPrivate::disconnect(sender, signal_index, smeta, receiver, -1, 0);
+ res |= QMetaObjectPrivate::disconnect(sender, signal_index, smeta, receiver, -1, nullptr);
} else {
const QMetaObject *rmeta = receiver->metaObject();
do {
@@ -3121,7 +3129,7 @@ bool QObject::disconnect(const QObject *sender, const char *signal,
rmeta = rmeta->superClass();
if (method_index < 0)
break;
- res |= QMetaObjectPrivate::disconnect(sender, signal_index, smeta, receiver, method_index, 0);
+ res |= QMetaObjectPrivate::disconnect(sender, signal_index, smeta, receiver, method_index, nullptr);
method_found = true;
} while ((rmeta = rmeta->superClass()));
}
@@ -3165,16 +3173,16 @@ bool QObject::disconnect(const QObject *sender, const char *signal,
\endlist
QMetaMethod() may be used as wildcard in the meaning "any signal" or "any slot in receiving object".
- In the same way 0 can be used for \a receiver in the meaning "any receiving object". In this case
- method should also be QMetaMethod(). \a sender parameter should be never 0.
+ In the same way \nullptr can be used for \a receiver in the meaning "any receiving object".
+ In this case method should also be QMetaMethod(). \a sender parameter should be never \nullptr.
\sa disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
*/
bool QObject::disconnect(const QObject *sender, const QMetaMethod &signal,
const QObject *receiver, const QMetaMethod &method)
{
- if (sender == 0 || (receiver == 0 && method.mobj != 0)) {
- qWarning("QObject::disconnect: Unexpected null parameter");
+ if (sender == nullptr || (receiver == nullptr && method.mobj != nullptr)) {
+ qWarning("QObject::disconnect: Unexpected nullptr parameter");
return false;
}
if (signal.mobj) {
@@ -3208,7 +3216,7 @@ bool QObject::disconnect(const QObject *sender, const QMetaMethod &signal,
QMetaObjectPrivate::memberIndexes(sender, signal, &signal_index, &dummy);
QMetaObjectPrivate::memberIndexes(receiver, method, &dummy, &method_index);
}
- // If we are here sender is not null. If signal is not null while signal_index
+ // If we are here sender is not nullptr. If signal is not nullptr while signal_index
// is -1 then this signal is not a member of sender.
if (signal.mobj && signal_index == -1) {
qWarning("QObject::disconect: signal %s not found on class %s",
@@ -3222,7 +3230,7 @@ bool QObject::disconnect(const QObject *sender, const QMetaMethod &signal,
return false;
}
- if (!QMetaObjectPrivate::disconnect(sender, signal_index, signal.mobj, receiver, method_index, 0))
+ if (!QMetaObjectPrivate::disconnect(sender, signal_index, signal.mobj, receiver, method_index, nullptr))
return false;
if (!signal.isValid()) {
@@ -3297,7 +3305,7 @@ void QObject::connectNotify(const QMetaMethod &signal)
\a signal with a specific signal.
If all signals were disconnected from this object (e.g., the
- signal argument to disconnect() was 0), disconnectNotify()
+ signal argument to disconnect() was \nullptr), disconnectNotify()
is only called once, and the \a signal will be an invalid
QMetaMethod (QMetaMethod::isValid() returns \c false).
@@ -3361,7 +3369,7 @@ QMetaObject::Connection QMetaObject::connect(const QObject *sender, int signal_i
signal_index = methodIndexToSignalIndex(&smeta, signal_index);
return Connection(QMetaObjectPrivate::connect(sender, signal_index, smeta,
receiver, method_index,
- 0, //FIXME, we could speed this connection up by computing the relative index
+ nullptr, //FIXME, we could speed this connection up by computing the relative index
type, types));
}
@@ -3437,7 +3445,7 @@ bool QMetaObject::disconnect(const QObject *sender, int signal_index,
const QMetaObject *smeta = sender->metaObject();
signal_index = methodIndexToSignalIndex(&smeta, signal_index);
return QMetaObjectPrivate::disconnect(sender, signal_index, smeta,
- receiver, method_index, 0);
+ receiver, method_index, nullptr);
}
/*!
@@ -3453,7 +3461,7 @@ bool QMetaObject::disconnectOne(const QObject *sender, int signal_index,
const QMetaObject *smeta = sender->metaObject();
signal_index = methodIndexToSignalIndex(&smeta, signal_index);
return QMetaObjectPrivate::disconnect(sender, signal_index, smeta,
- receiver, method_index, 0,
+ receiver, method_index, nullptr,
QMetaObjectPrivate::DisconnectOne);
}
@@ -3662,7 +3670,7 @@ static void queued_activate(QObject *sender, int signal, QObjectPrivate::Connect
argumentTypes = queuedConnectionTypes(m.parameterTypes());
if (!argumentTypes) // cannot queue arguments
argumentTypes = &DIRECT_CONNECTION_ONLY;
- if (!c->argumentTypes.testAndSetOrdered(0, argumentTypes)) {
+ if (!c->argumentTypes.testAndSetOrdered(nullptr, argumentTypes)) {
if (argumentTypes != &DIRECT_CONNECTION_ONLY)
delete [] argumentTypes;
argumentTypes = c->argumentTypes.loadRelaxed();
@@ -3762,7 +3770,7 @@ void doActivate(QObject *sender, int signal_index, void **argv)
list = &signalVector->at(-1);
Qt::HANDLE currentThreadId = QThread::currentThreadId();
- bool inSenderThread = currentThreadId == QObjectPrivate::get(sender)->threadData->threadId.loadRelaxed();
+ bool inSenderThread = currentThreadId == QObjectPrivate::get(sender)->threadData.loadRelaxed()->threadId.loadRelaxed();
// We need to check against the highest connection id to ensure that signals added
// during the signal emission are not emitted in this emission.
@@ -4528,13 +4536,30 @@ QDebug operator<<(QDebug dbg, const QObject *o)
It works exactly like the Q_NAMESPACE macro. However, the external
\c{staticMetaObject} variable that gets defined in the namespace
- is declared with the supplied \c{EXPORT_MACRO} qualifier. This is
+ is declared with the supplied \a EXPORT_MACRO qualifier. This is
useful if the object needs to be exported from a dynamic library.
\sa Q_NAMESPACE, {Creating Shared Libraries}
*/
/*!
+ \macro Q_MOC_INCLUDE
+ \relates QObject
+ \since 6.0
+
+ The Q_MOC_INCLUDE macro can be used within or outside a class, and tell the
+ \l{moc}{Meta Object Compiler} to add an include.
+
+ \code
+ // Put this in your code and the generated code will include this header.
+ Q_MOC_INCLUDE("myheader.h")
+ \endcode
+
+ This is useful if the types you use as properties or signal/slots arguments
+ are forward declared.
+*/
+
+/*!
\macro Q_SIGNALS
\relates QObject
@@ -4845,7 +4870,7 @@ void qDeleteInEventHandler(QObject *o)
\a sender is the sender object
\a signal is a pointer to a pointer to a member signal of the sender
- \a receiver is the receiver object, may not be null, will be equal to sender when
+ \a receiver is the receiver object, may not be \nullptr, will be equal to sender when
connecting to a static function or a functor
\a slot a pointer only used when using Qt::UniqueConnection
\a type the Qt::ConnctionType passed as argument to connect
@@ -4853,7 +4878,7 @@ void qDeleteInEventHandler(QObject *o)
to be used with queued connection
must stay valid at least for the whole time of the connection, this function
do not take ownership. typically static data.
- If null, then the types will be computed when the signal is emit in a queued
+ If \nullptr, then the types will be computed when the signal is emit in a queued
connection from the types from the signature.
\a senderMetaObject is the metaobject used to lookup the signal, the signal must be in
this metaobject
@@ -4864,7 +4889,7 @@ QMetaObject::Connection QObject::connectImpl(const QObject *sender, void **signa
const int *types, const QMetaObject *senderMetaObject)
{
if (!signal) {
- qWarning("QObject::connect: invalid null parameter");
+ qWarning("QObject::connect: invalid nullptr parameter");
if (slotObj)
slotObj->destroyIfLastRef();
return QMetaObject::Connection();
@@ -4880,7 +4905,7 @@ QMetaObject::Connection QObject::connectImpl(const QObject *sender, void **signa
if (!senderMetaObject) {
qWarning("QObject::connect: signal not found in %s", sender->metaObject()->className());
slotObj->destroyIfLastRef();
- return QMetaObject::Connection(0);
+ return QMetaObject::Connection(nullptr);
}
signal_index += QMetaObjectPrivate::signalOffset(senderMetaObject);
return QObjectPrivate::connectImpl(sender, signal_index, receiver, slot, slotObj, type, types, senderMetaObject);
@@ -4904,7 +4929,7 @@ QMetaObject::Connection QObjectPrivate::connectImpl(const QObject *sender, int s
: "Unknown";
const char *receiverString = receiver ? receiver->metaObject()->className()
: "Unknown";
- qWarning("QObject::connect(%s, %s): invalid null parameter", senderString, receiverString);
+ qWarning("QObject::connect(%s, %s): invalid nullptr parameter", senderString, receiverString);
if (slotObj)
slotObj->destroyIfLastRef();
return QMetaObject::Connection();
@@ -5037,20 +5062,20 @@ bool QObject::disconnect(const QMetaObject::Connection &connection)
\endlist
- 0 may be used as a wildcard, meaning "any signal", "any receiving
+ \nullptr may be used as a wildcard, meaning "any signal", "any receiving
object", or "any slot in the receiving object", respectively.
The \a sender may never be \nullptr. (You cannot disconnect signals
from more than one object in a single call.)
- If \a signal is 0, it disconnects \a receiver and \a method from
+ If \a signal is \nullptr, it disconnects \a receiver and \a method from
any signal. If not, only the specified signal is disconnected.
- If \a receiver is 0, it disconnects anything connected to \a
+ If \a receiver is \nullptr, it disconnects anything connected to \a
signal. If not, slots in objects other than \a receiver are not
disconnected.
- If \a method is 0, it disconnects anything that is connected to \a
+ If \a method is \nullptr, it disconnects anything that is connected to \a
receiver. If not, only slots named \a method will be disconnected,
and all other slots are left alone. The \a method must be \nullptr
if \a receiver is left out, so you cannot disconnect a
@@ -5066,8 +5091,8 @@ bool QObject::disconnect(const QMetaObject::Connection &connection)
bool QObject::disconnectImpl(const QObject *sender, void **signal, const QObject *receiver, void **slot, const QMetaObject *senderMetaObject)
{
- if (sender == 0 || (receiver == 0 && slot != 0)) {
- qWarning("QObject::disconnect: Unexpected null parameter");
+ if (sender == nullptr || (receiver == nullptr && slot != nullptr)) {
+ qWarning("QObject::disconnect: Unexpected nullptr parameter");
return false;
}
@@ -5098,7 +5123,7 @@ bool QObject::disconnectImpl(const QObject *sender, void **signal, const QObject
QMetaObject::Connection QObjectPrivate::connect(const QObject *sender, int signal_index, QtPrivate::QSlotObjectBase *slotObj, Qt::ConnectionType type)
{
if (!sender) {
- qWarning("QObject::connect: invalid null parameter");
+ qWarning("QObject::connect: invalid nullptr parameter");
if (slotObj)
slotObj->destroyIfLastRef();
return QMetaObject::Connection();
@@ -5106,7 +5131,7 @@ QMetaObject::Connection QObjectPrivate::connect(const QObject *sender, int signa
const QMetaObject *senderMetaObject = sender->metaObject();
signal_index = methodIndexToSignalIndex(&senderMetaObject, signal_index);
- return QObjectPrivate::connectImpl(sender, signal_index, sender, /*slot*/0, slotObj, type, /*types*/0, senderMetaObject);
+ return QObjectPrivate::connectImpl(sender, signal_index, sender, /*slot*/nullptr, slotObj, type, /*types*/nullptr, senderMetaObject);
}
/*!
@@ -5163,7 +5188,7 @@ QMetaObject::Connection& QMetaObject::Connection::operator=(const QMetaObject::C
Creates a Connection instance.
*/
-QMetaObject::Connection::Connection() : d_ptr(0) {}
+QMetaObject::Connection::Connection() : d_ptr(nullptr) {}
/*!
Destructor for QMetaObject::Connection.
diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h
index 296552c2f2..8d9cc3ab83 100644
--- a/src/corelib/kernel/qobject.h
+++ b/src/corelib/kernel/qobject.h
@@ -55,7 +55,7 @@
#include <QtCore/qobject_impl.h>
-#if QT_HAS_INCLUDE(<chrono>)
+#if __has_include(<chrono>)
# include <chrono>
#endif
@@ -154,7 +154,7 @@ public:
void moveToThread(QThread *thread);
int startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer);
-#if QT_HAS_INCLUDE(<chrono>)
+#if __has_include(<chrono>)
Q_ALWAYS_INLINE
int startTimer(std::chrono::milliseconds time, Qt::TimerType timerType = Qt::CoarseTimer)
{
@@ -434,9 +434,6 @@ protected:
protected:
QScopedPointer<QObjectData> d_ptr;
- static const QMetaObject staticQtMetaObject;
- friend inline const QMetaObject *qt_getQtMetaObject() noexcept;
-
friend struct QMetaObject;
friend struct QMetaObjectPrivate;
friend class QMetaCallEvent;
@@ -467,9 +464,6 @@ inline QMetaObject::Connection QObject::connect(const QObject *asender, const ch
const char *amember, Qt::ConnectionType atype) const
{ return connect(asender, asignal, this, amember, atype); }
-inline const QMetaObject *qt_getQtMetaObject() noexcept
-{ return &QObject::staticQtMetaObject; }
-
#if QT_DEPRECATED_SINCE(5, 0)
template<typename T>
inline QT_DEPRECATED T qFindChild(const QObject *o, const QString &name = QString())
diff --git a/src/corelib/kernel/qobject_p.h b/src/corelib/kernel/qobject_p.h
index db62c03b7a..d6e73fbefc 100644
--- a/src/corelib/kernel/qobject_p.h
+++ b/src/corelib/kernel/qobject_p.h
@@ -362,8 +362,12 @@ public:
}
public:
ExtraData *extraData; // extra data set by the user
- QThreadData *getThreadData() const { return threadData; }
- QThreadData *threadData; // id of the thread that owns the object
+ // This atomic requires acquire/release semantics in a few places,
+ // e.g. QObject::moveToThread must synchronize with QCoreApplication::postEvent,
+ // because postEvent is thread-safe.
+ // However, most of the code paths involving QObject are only reentrant and
+ // not thread-safe, so synchronization should not be necessary there.
+ QAtomicPointer<QThreadData> threadData; // id of the thread that owns the object
using ConnectionDataPointer = QExplicitlySharedDataPointer<ConnectionData>;
QAtomicPointer<ConnectionData> connections;
diff --git a/src/corelib/kernel/qobjectcleanuphandler.cpp b/src/corelib/kernel/qobjectcleanuphandler.cpp
index b6c62af4b3..8bf0e1fcab 100644
--- a/src/corelib/kernel/qobjectcleanuphandler.cpp
+++ b/src/corelib/kernel/qobjectcleanuphandler.cpp
@@ -94,7 +94,7 @@ QObjectCleanupHandler::~QObjectCleanupHandler()
QObject *QObjectCleanupHandler::add(QObject* object)
{
if (!object)
- return 0;
+ return nullptr;
connect(object, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(QObject*)));
cleanupObjects.insert(0, object);
diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h
index 9f654b0318..657cb9940b 100644
--- a/src/corelib/kernel/qobjectdefs.h
+++ b/src/corelib/kernel/qobjectdefs.h
@@ -46,199 +46,15 @@
#endif
#include <QtCore/qnamespace.h>
-
#include <QtCore/qobjectdefs_impl.h>
+#include <QtCore/qtmetamacros.h>
QT_BEGIN_NAMESPACE
-
class QByteArray;
struct QArrayData;
-typedef QArrayData QByteArrayData;
class QString;
-#ifndef Q_MOC_OUTPUT_REVISION
-#define Q_MOC_OUTPUT_REVISION 67
-#endif
-
-// The following macros can be defined by tools that understand Qt
-// to have the information from the macro.
-#ifndef QT_ANNOTATE_CLASS
-# define QT_ANNOTATE_CLASS(type, ...)
-#endif
-#ifndef QT_ANNOTATE_CLASS2
-# define QT_ANNOTATE_CLASS2(type, a1, a2)
-#endif
-#ifndef QT_ANNOTATE_FUNCTION
-# define QT_ANNOTATE_FUNCTION(x)
-#endif
-#ifndef QT_ANNOTATE_ACCESS_SPECIFIER
-# define QT_ANNOTATE_ACCESS_SPECIFIER(x)
-#endif
-
-// The following macros are our "extensions" to C++
-// They are used, strictly speaking, only by the moc.
-
-#ifndef Q_MOC_RUN
-#ifndef QT_NO_META_MACROS
-# if defined(QT_NO_KEYWORDS)
-# define QT_NO_EMIT
-# else
-# ifndef QT_NO_SIGNALS_SLOTS_KEYWORDS
-# define slots Q_SLOTS
-# define signals Q_SIGNALS
-# endif
-# endif
-# define Q_SLOTS QT_ANNOTATE_ACCESS_SPECIFIER(qt_slot)
-# define Q_SIGNALS public QT_ANNOTATE_ACCESS_SPECIFIER(qt_signal)
-# define Q_PRIVATE_SLOT(d, signature) QT_ANNOTATE_CLASS2(qt_private_slot, d, signature)
-# define Q_EMIT
-#ifndef QT_NO_EMIT
-# define emit
-#endif
-#ifndef Q_CLASSINFO
-# define Q_CLASSINFO(name, value)
-#endif
-#define Q_PLUGIN_METADATA(x) QT_ANNOTATE_CLASS(qt_plugin_metadata, x)
-#define Q_INTERFACES(x) QT_ANNOTATE_CLASS(qt_interfaces, x)
-#define Q_PROPERTY(...) QT_ANNOTATE_CLASS(qt_property, __VA_ARGS__)
-#define Q_PRIVATE_PROPERTY(d, text) QT_ANNOTATE_CLASS2(qt_private_property, d, text)
-#ifndef Q_REVISION
-# define Q_REVISION(v)
-#endif
-#define Q_OVERRIDE(text) QT_ANNOTATE_CLASS(qt_override, text)
-#define QDOC_PROPERTY(text) QT_ANNOTATE_CLASS(qt_qdoc_property, text)
-#define Q_ENUMS(x) QT_ANNOTATE_CLASS(qt_enums, x)
-#define Q_FLAGS(x) QT_ANNOTATE_CLASS(qt_enums, x)
-#define Q_ENUM_IMPL(ENUM) \
- friend Q_DECL_CONSTEXPR const QMetaObject *qt_getEnumMetaObject(ENUM) noexcept { return &staticMetaObject; } \
- friend Q_DECL_CONSTEXPR const char *qt_getEnumName(ENUM) noexcept { return #ENUM; }
-#define Q_ENUM(x) Q_ENUMS(x) Q_ENUM_IMPL(x)
-#define Q_FLAG(x) Q_FLAGS(x) Q_ENUM_IMPL(x)
-#define Q_ENUM_NS_IMPL(ENUM) \
- inline Q_DECL_CONSTEXPR const QMetaObject *qt_getEnumMetaObject(ENUM) noexcept { return &staticMetaObject; } \
- inline Q_DECL_CONSTEXPR const char *qt_getEnumName(ENUM) noexcept { return #ENUM; }
-#define Q_ENUM_NS(x) Q_ENUMS(x) Q_ENUM_NS_IMPL(x)
-#define Q_FLAG_NS(x) Q_FLAGS(x) Q_ENUM_NS_IMPL(x)
-#define Q_SCRIPTABLE QT_ANNOTATE_FUNCTION(qt_scriptable)
-#define Q_INVOKABLE QT_ANNOTATE_FUNCTION(qt_invokable)
-#define Q_SIGNAL QT_ANNOTATE_FUNCTION(qt_signal)
-#define Q_SLOT QT_ANNOTATE_FUNCTION(qt_slot)
-#endif // QT_NO_META_MACROS
-
-#ifndef QT_NO_TRANSLATION
-// full set of tr functions
-# define QT_TR_FUNCTIONS \
- static inline QString tr(const char *s, const char *c = nullptr, int n = -1) \
- { return staticMetaObject.tr(s, c, n); } \
- QT_DEPRECATED static inline QString trUtf8(const char *s, const char *c = nullptr, int n = -1) \
- { return staticMetaObject.tr(s, c, n); }
-#else
-// inherit the ones from QObject
-# define QT_TR_FUNCTIONS
-#endif
-
-#ifdef Q_CLANG_QDOC
-#define QT_TR_FUNCTIONS
-#endif
-
-// ### Qt6: remove
-#define Q_OBJECT_CHECK /* empty, unused since Qt 5.2 */
-
-#if defined(Q_CC_INTEL)
-// Cannot redefine the visibility of a method in an exported class
-# define Q_DECL_HIDDEN_STATIC_METACALL
-#else
-# define Q_DECL_HIDDEN_STATIC_METACALL Q_DECL_HIDDEN
-#endif
-
-#if defined(Q_CC_CLANG) && Q_CC_CLANG >= 306
-# define Q_OBJECT_NO_OVERRIDE_WARNING QT_WARNING_DISABLE_CLANG("-Winconsistent-missing-override")
-#elif defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && Q_CC_GNU >= 501
-# define Q_OBJECT_NO_OVERRIDE_WARNING QT_WARNING_DISABLE_GCC("-Wsuggest-override")
-#else
-# define Q_OBJECT_NO_OVERRIDE_WARNING
-#endif
-
-#if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && Q_CC_GNU >= 600
-# define Q_OBJECT_NO_ATTRIBUTES_WARNING QT_WARNING_DISABLE_GCC("-Wattributes")
-#else
-# define Q_OBJECT_NO_ATTRIBUTES_WARNING
-#endif
-
-/* qmake ignore Q_OBJECT */
-#define Q_OBJECT \
-public: \
- QT_WARNING_PUSH \
- Q_OBJECT_NO_OVERRIDE_WARNING \
- static const QMetaObject staticMetaObject; \
- virtual const QMetaObject *metaObject() const; \
- virtual void *qt_metacast(const char *); \
- virtual int qt_metacall(QMetaObject::Call, int, void **); \
- QT_TR_FUNCTIONS \
-private: \
- Q_OBJECT_NO_ATTRIBUTES_WARNING \
- Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
- QT_WARNING_POP \
- struct QPrivateSignal {}; \
- QT_ANNOTATE_CLASS(qt_qobject, "")
-
-/* qmake ignore Q_OBJECT */
-#define Q_OBJECT_FAKE Q_OBJECT QT_ANNOTATE_CLASS(qt_fake, "")
-
-#ifndef QT_NO_META_MACROS
-/* qmake ignore Q_GADGET */
-#define Q_GADGET \
-public: \
- static const QMetaObject staticMetaObject; \
- void qt_check_for_QGADGET_macro(); \
- typedef void QtGadgetHelper; \
-private: \
- QT_WARNING_PUSH \
- Q_OBJECT_NO_ATTRIBUTES_WARNING \
- Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
- QT_WARNING_POP \
- QT_ANNOTATE_CLASS(qt_qgadget, "") \
- /*end*/
-
-/* qmake ignore Q_NAMESPACE_EXPORT */
-#define Q_NAMESPACE_EXPORT(...) \
- extern __VA_ARGS__ const QMetaObject staticMetaObject; \
- QT_ANNOTATE_CLASS(qt_qnamespace, "") \
- /*end*/
-
-/* qmake ignore Q_NAMESPACE */
-#define Q_NAMESPACE Q_NAMESPACE_EXPORT() \
- /*end*/
-
-#endif // QT_NO_META_MACROS
-
-#else // Q_MOC_RUN
-#define slots slots
-#define signals signals
-#define Q_SLOTS Q_SLOTS
-#define Q_SIGNALS Q_SIGNALS
-#define Q_CLASSINFO(name, value) Q_CLASSINFO(name, value)
-#define Q_INTERFACES(x) Q_INTERFACES(x)
-#define Q_PROPERTY(text) Q_PROPERTY(text)
-#define Q_PRIVATE_PROPERTY(d, text) Q_PRIVATE_PROPERTY(d, text)
-#define Q_REVISION(v) Q_REVISION(v)
-#define Q_OVERRIDE(text) Q_OVERRIDE(text)
-#define Q_ENUMS(x) Q_ENUMS(x)
-#define Q_FLAGS(x) Q_FLAGS(x)
-#define Q_ENUM(x) Q_ENUM(x)
-#define Q_FLAGS(x) Q_FLAGS(x)
- /* qmake ignore Q_OBJECT */
-#define Q_OBJECT Q_OBJECT
- /* qmake ignore Q_OBJECT */
-#define Q_OBJECT_FAKE Q_OBJECT_FAKE
- /* qmake ignore Q_GADGET */
-#define Q_GADGET Q_GADGET
-#define Q_SCRIPTABLE Q_SCRIPTABLE
-#define Q_INVOKABLE Q_INVOKABLE
-#define Q_SIGNAL Q_SIGNAL
-#define Q_SLOT Q_SLOT
-#endif //Q_MOC_RUN
#ifndef QT_NO_META_MACROS
// macro for onaming members
@@ -607,7 +423,7 @@ struct Q_CORE_EXPORT QMetaObject
struct { // private data
SuperData superdata;
- const QByteArrayData *stringdata;
+ const uint *stringdata;
const uint *data;
typedef void (*StaticMetacallFunction)(QObject *, QMetaObject::Call, int, void **);
StaticMetacallFunction static_metacall;
diff --git a/src/corelib/kernel/qsharedmemory.cpp b/src/corelib/kernel/qsharedmemory.cpp
index 39f3002394..2d65e0bbe4 100644
--- a/src/corelib/kernel/qsharedmemory.cpp
+++ b/src/corelib/kernel/qsharedmemory.cpp
@@ -441,7 +441,7 @@ bool QSharedMemory::attach(AccessMode mode)
bool QSharedMemory::isAttached() const
{
Q_D(const QSharedMemory);
- return (0 != d->memory);
+ return (nullptr != d->memory);
}
/*!
diff --git a/src/corelib/kernel/qsharedmemory_systemv.cpp b/src/corelib/kernel/qsharedmemory_systemv.cpp
index fea4a65b5c..0ba5f65641 100644
--- a/src/corelib/kernel/qsharedmemory_systemv.cpp
+++ b/src/corelib/kernel/qsharedmemory_systemv.cpp
@@ -182,9 +182,9 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
}
// grab the memory
- memory = shmat(id, 0, (mode == QSharedMemory::ReadOnly ? SHM_RDONLY : 0));
+ memory = shmat(id, nullptr, (mode == QSharedMemory::ReadOnly ? SHM_RDONLY : 0));
if ((void*) - 1 == memory) {
- memory = 0;
+ memory = nullptr;
setErrorString(QLatin1String("QSharedMemory::attach (shmat)"));
return false;
}
@@ -216,7 +216,7 @@ bool QSharedMemoryPrivate::detach()
}
return false;
}
- memory = 0;
+ memory = nullptr;
size = 0;
// Get the number of current attachments
diff --git a/src/corelib/kernel/qsharedmemory_unix.cpp b/src/corelib/kernel/qsharedmemory_unix.cpp
index f6d7e78441..bc0f3b03ca 100644
--- a/src/corelib/kernel/qsharedmemory_unix.cpp
+++ b/src/corelib/kernel/qsharedmemory_unix.cpp
@@ -68,7 +68,7 @@ QSharedMemoryPrivate::QSharedMemoryPrivate() :
#ifndef QT_NO_QOBJECT
QObjectPrivate(),
#endif
- memory(0), size(0), error(QSharedMemory::NoError),
+ memory(nullptr), size(0), error(QSharedMemory::NoError),
#ifndef QT_NO_SYSTEMSEMAPHORE
systemSemaphore(QString()), lockedByMe(false),
#endif
diff --git a/src/corelib/kernel/qsocketnotifier.cpp b/src/corelib/kernel/qsocketnotifier.cpp
index 2a246b1204..78269ee605 100644
--- a/src/corelib/kernel/qsocketnotifier.cpp
+++ b/src/corelib/kernel/qsocketnotifier.cpp
@@ -147,12 +147,14 @@ QSocketNotifier::QSocketNotifier(qintptr socket, Type type, QObject *parent)
d->sntype = type;
d->snenabled = true;
+ auto thisThreadData = d->threadData.loadRelaxed();
+
if (socket < 0)
qWarning("QSocketNotifier: Invalid socket specified");
- else if (!d->threadData->hasEventDispatcher())
+ else if (!thisThreadData->hasEventDispatcher())
qWarning("QSocketNotifier: Can only be used with threads started with QThread");
else
- d->threadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
+ thisThreadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
}
/*!
@@ -234,16 +236,19 @@ void QSocketNotifier::setEnabled(bool enable)
return;
d->snenabled = enable;
- if (!d->threadData->hasEventDispatcher()) // perhaps application/thread is shutting down
+
+ auto thisThreadData = d->threadData.loadRelaxed();
+
+ if (!thisThreadData->hasEventDispatcher()) // perhaps application/thread is shutting down
return;
if (Q_UNLIKELY(thread() != QThread::currentThread())) {
qWarning("QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread");
return;
}
if (d->snenabled)
- d->threadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
+ thisThreadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
else
- d->threadData->eventDispatcher.loadRelaxed()->unregisterSocketNotifier(this);
+ thisThreadData->eventDispatcher.loadRelaxed()->unregisterSocketNotifier(this);
}
diff --git a/src/corelib/kernel/qtestsupport_core.cpp b/src/corelib/kernel/qtestsupport_core.cpp
index 7bd81ed498..8498f7f025 100644
--- a/src/corelib/kernel/qtestsupport_core.cpp
+++ b/src/corelib/kernel/qtestsupport_core.cpp
@@ -55,7 +55,7 @@ Q_CORE_EXPORT void QTestPrivate::qSleep(int ms)
Sleep(uint(ms));
#else
struct timespec ts = { time_t(ms / 1000), (ms % 1000) * 1000 * 1000 };
- nanosleep(&ts, NULL);
+ nanosleep(&ts, nullptr);
#endif
}
diff --git a/src/corelib/kernel/qtimer.cpp b/src/corelib/kernel/qtimer.cpp
index 948f697dc5..25ce0c032f 100644
--- a/src/corelib/kernel/qtimer.cpp
+++ b/src/corelib/kernel/qtimer.cpp
@@ -84,10 +84,10 @@ QT_BEGIN_NAMESPACE
must start and stop the timer in its thread; it is not possible to
start a timer from another thread.
- As a special case, a QTimer with a timeout of 0 will time out as
- soon as all the events in the window system's event queue have
- been processed. This can be used to do heavy work while providing
- a snappy user interface:
+ As a special case, a QTimer with a timeout of 0 will time out as soon as
+ possible, though the ordering between zero timers and other sources of
+ events is unspecified. Zero timers can be used to do some work while still
+ providing a snappy user interface:
\snippet timers/timers.cpp 4
\snippet timers/timers.cpp 5
@@ -277,7 +277,7 @@ protected:
};
QSingleShotTimer::QSingleShotTimer(int msec, Qt::TimerType timerType, const QObject *r, const char *member)
- : QObject(QAbstractEventDispatcher::instance()), hasValidReceiver(true), slotObj(0)
+ : QObject(QAbstractEventDispatcher::instance()), hasValidReceiver(true), slotObj(nullptr)
{
timerId = startTimer(msec, timerType);
connect(this, SIGNAL(timeout()), r, member);
@@ -290,7 +290,7 @@ QSingleShotTimer::QSingleShotTimer(int msec, Qt::TimerType timerType, const QObj
if (r && thread() != r->thread()) {
// Avoid leaking the QSingleShotTimer instance in case the application exits before the timer fires
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, this, &QObject::deleteLater);
- setParent(0);
+ setParent(nullptr);
moveToThread(r->thread());
}
}
@@ -316,7 +316,7 @@ void QSingleShotTimer::timerEvent(QTimerEvent *)
if (Q_LIKELY(!receiver.isNull() || !hasValidReceiver)) {
// We allocate only the return type - we previously checked the function had
// no arguments.
- void *args[1] = { 0 };
+ void *args[1] = { nullptr };
slotObj->call(const_cast<QObject*>(receiver.data()), args);
}
} else {
diff --git a/src/corelib/kernel/qtimer.h b/src/corelib/kernel/qtimer.h
index eb7185c12d..6bbfd741d9 100644
--- a/src/corelib/kernel/qtimer.h
+++ b/src/corelib/kernel/qtimer.h
@@ -47,7 +47,7 @@
#include <QtCore/qbasictimer.h> // conceptual inheritance
#include <QtCore/qobject.h>
-#if QT_HAS_INCLUDE(<chrono>)
+#if __has_include(<chrono>)
# include <chrono>
#endif
@@ -177,7 +177,7 @@ Q_SIGNALS:
void timeout(QPrivateSignal);
public:
-#if QT_HAS_INCLUDE(<chrono>) || defined(Q_QDOC)
+#if __has_include(<chrono>) || defined(Q_QDOC)
void setInterval(std::chrono::milliseconds value)
{
setInterval(int(value.count()));
@@ -223,7 +223,7 @@ private:
static void singleShotImpl(int msec, Qt::TimerType timerType,
const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
-#if QT_HAS_INCLUDE(<chrono>)
+#if __has_include(<chrono>)
static Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval)
{ return defaultTypeFor(int(interval.count())); }
diff --git a/src/corelib/kernel/qtimerinfo_unix.cpp b/src/corelib/kernel/qtimerinfo_unix.cpp
index 39010c19cb..b425ca3dcb 100644
--- a/src/corelib/kernel/qtimerinfo_unix.cpp
+++ b/src/corelib/kernel/qtimerinfo_unix.cpp
@@ -83,7 +83,7 @@ QTimerInfoList::QTimerInfoList()
}
#endif
- firstTimerInfo = 0;
+ firstTimerInfo = nullptr;
}
timespec QTimerInfoList::updateCurrentTime()
@@ -389,7 +389,7 @@ bool QTimerInfoList::timerWait(timespec &tm)
repairTimersIfNeeded();
// Find first waiting timer not already active
- QTimerInfo *t = 0;
+ QTimerInfo *t = nullptr;
for (QTimerInfoList::const_iterator it = constBegin(); it != constEnd(); ++it) {
if (!(*it)->activateRef) {
t = *it;
@@ -450,7 +450,7 @@ void QTimerInfoList::registerTimer(int timerId, int interval, Qt::TimerType time
t->interval = interval;
t->timerType = timerType;
t->obj = object;
- t->activateRef = 0;
+ t->activateRef = nullptr;
timespec expected = updateCurrentTime() + interval;
@@ -514,9 +514,9 @@ bool QTimerInfoList::unregisterTimer(int timerId)
// found it
removeAt(i);
if (t == firstTimerInfo)
- firstTimerInfo = 0;
+ firstTimerInfo = nullptr;
if (t->activateRef)
- *(t->activateRef) = 0;
+ *(t->activateRef) = nullptr;
delete t;
return true;
}
@@ -535,9 +535,9 @@ bool QTimerInfoList::unregisterTimers(QObject *object)
// object found
removeAt(i);
if (t == firstTimerInfo)
- firstTimerInfo = 0;
+ firstTimerInfo = nullptr;
if (t->activateRef)
- *(t->activateRef) = 0;
+ *(t->activateRef) = nullptr;
delete t;
// move back one so that we don't skip the new current item
--i;
@@ -571,7 +571,7 @@ int QTimerInfoList::activateTimers()
return 0; // nothing to do
int n_act = 0, maxCount = 0;
- firstTimerInfo = 0;
+ firstTimerInfo = nullptr;
timespec currentTime = updateCurrentTime();
// qDebug() << "Thread" << QThread::currentThreadId() << "woken up at" << currentTime;
@@ -643,11 +643,11 @@ int QTimerInfoList::activateTimers()
QCoreApplication::sendEvent(currentTimerInfo->obj, &e);
if (currentTimerInfo)
- currentTimerInfo->activateRef = 0;
+ currentTimerInfo->activateRef = nullptr;
}
}
- firstTimerInfo = 0;
+ firstTimerInfo = nullptr;
// qDebug() << "Thread" << QThread::currentThreadId() << "activated" << n_act << "timers";
return n_act;
}
diff --git a/src/corelib/kernel/qtmetamacros.h b/src/corelib/kernel/qtmetamacros.h
new file mode 100644
index 0000000000..1d095c0d7c
--- /dev/null
+++ b/src/corelib/kernel/qtmetamacros.h
@@ -0,0 +1,234 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2019 Olivier Goffart <ogoffart@woboq.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QTMETAMACROS_H
+#define QTMETAMACROS_H
+
+#include <QtCore/qglobal.h>
+
+QT_BEGIN_NAMESPACE
+
+#ifndef Q_MOC_OUTPUT_REVISION
+#define Q_MOC_OUTPUT_REVISION 67
+#endif
+
+// The following macros can be defined by tools that understand Qt
+// to have the information from the macro.
+#ifndef QT_ANNOTATE_CLASS
+# define QT_ANNOTATE_CLASS(type, ...)
+#endif
+#ifndef QT_ANNOTATE_CLASS2
+# define QT_ANNOTATE_CLASS2(type, a1, a2)
+#endif
+#ifndef QT_ANNOTATE_FUNCTION
+# define QT_ANNOTATE_FUNCTION(x)
+#endif
+#ifndef QT_ANNOTATE_ACCESS_SPECIFIER
+# define QT_ANNOTATE_ACCESS_SPECIFIER(x)
+#endif
+
+// The following macros are our "extensions" to C++
+// They are used, strictly speaking, only by the moc.
+
+#ifndef Q_MOC_RUN
+#ifndef QT_NO_META_MACROS
+# if defined(QT_NO_KEYWORDS)
+# define QT_NO_EMIT
+# else
+# ifndef QT_NO_SIGNALS_SLOTS_KEYWORDS
+# define slots Q_SLOTS
+# define signals Q_SIGNALS
+# endif
+# endif
+# define Q_SLOTS QT_ANNOTATE_ACCESS_SPECIFIER(qt_slot)
+# define Q_SIGNALS public QT_ANNOTATE_ACCESS_SPECIFIER(qt_signal)
+# define Q_PRIVATE_SLOT(d, signature) QT_ANNOTATE_CLASS2(qt_private_slot, d, signature)
+# define Q_EMIT
+#ifndef QT_NO_EMIT
+# define emit
+#endif
+#ifndef Q_CLASSINFO
+# define Q_CLASSINFO(name, value)
+#endif
+#define Q_PLUGIN_METADATA(x) QT_ANNOTATE_CLASS(qt_plugin_metadata, x)
+#define Q_INTERFACES(x) QT_ANNOTATE_CLASS(qt_interfaces, x)
+#define Q_PROPERTY(...) QT_ANNOTATE_CLASS(qt_property, __VA_ARGS__)
+#define Q_PRIVATE_PROPERTY(d, text) QT_ANNOTATE_CLASS2(qt_private_property, d, text)
+#ifndef Q_REVISION
+# define Q_REVISION(v)
+#endif
+#define Q_OVERRIDE(text) QT_ANNOTATE_CLASS(qt_override, text)
+#define QDOC_PROPERTY(text) QT_ANNOTATE_CLASS(qt_qdoc_property, text)
+#define Q_ENUMS(x) QT_ANNOTATE_CLASS(qt_enums, x)
+#define Q_FLAGS(x) QT_ANNOTATE_CLASS(qt_enums, x)
+#define Q_ENUM_IMPL(ENUM) \
+ friend Q_DECL_CONSTEXPR const QMetaObject *qt_getEnumMetaObject(ENUM) noexcept { return &staticMetaObject; } \
+ friend Q_DECL_CONSTEXPR const char *qt_getEnumName(ENUM) noexcept { return #ENUM; }
+#define Q_ENUM(x) Q_ENUMS(x) Q_ENUM_IMPL(x)
+#define Q_FLAG(x) Q_FLAGS(x) Q_ENUM_IMPL(x)
+#define Q_ENUM_NS_IMPL(ENUM) \
+ inline Q_DECL_CONSTEXPR const QMetaObject *qt_getEnumMetaObject(ENUM) noexcept { return &staticMetaObject; } \
+ inline Q_DECL_CONSTEXPR const char *qt_getEnumName(ENUM) noexcept { return #ENUM; }
+#define Q_ENUM_NS(x) Q_ENUMS(x) Q_ENUM_NS_IMPL(x)
+#define Q_FLAG_NS(x) Q_FLAGS(x) Q_ENUM_NS_IMPL(x)
+#define Q_SCRIPTABLE QT_ANNOTATE_FUNCTION(qt_scriptable)
+#define Q_INVOKABLE QT_ANNOTATE_FUNCTION(qt_invokable)
+#define Q_SIGNAL QT_ANNOTATE_FUNCTION(qt_signal)
+#define Q_SLOT QT_ANNOTATE_FUNCTION(qt_slot)
+#define Q_MOC_INCLUDE(...) QT_ANNOTATE_CLASS(qt_moc_include, __VA_ARGS__)
+#endif // QT_NO_META_MACROS
+
+#ifndef QT_NO_TRANSLATION
+// full set of tr functions
+# define QT_TR_FUNCTIONS \
+ static inline QString tr(const char *s, const char *c = nullptr, int n = -1) \
+ { return staticMetaObject.tr(s, c, n); } \
+ QT_DEPRECATED static inline QString trUtf8(const char *s, const char *c = nullptr, int n = -1) \
+ { return staticMetaObject.tr(s, c, n); }
+#else
+// inherit the ones from QObject
+# define QT_TR_FUNCTIONS
+#endif
+
+#ifdef Q_CLANG_QDOC
+#define QT_TR_FUNCTIONS
+#endif
+
+// ### Qt6: remove
+#define Q_OBJECT_CHECK /* empty, unused since Qt 5.2 */
+
+#if defined(Q_CC_INTEL)
+// Cannot redefine the visibility of a method in an exported class
+# define Q_DECL_HIDDEN_STATIC_METACALL
+#else
+# define Q_DECL_HIDDEN_STATIC_METACALL Q_DECL_HIDDEN
+#endif
+
+#if defined(Q_CC_CLANG) && Q_CC_CLANG >= 306
+# define Q_OBJECT_NO_OVERRIDE_WARNING QT_WARNING_DISABLE_CLANG("-Winconsistent-missing-override")
+#elif defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && Q_CC_GNU >= 501
+# define Q_OBJECT_NO_OVERRIDE_WARNING QT_WARNING_DISABLE_GCC("-Wsuggest-override")
+#else
+# define Q_OBJECT_NO_OVERRIDE_WARNING
+#endif
+
+#if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && Q_CC_GNU >= 600
+# define Q_OBJECT_NO_ATTRIBUTES_WARNING QT_WARNING_DISABLE_GCC("-Wattributes")
+#else
+# define Q_OBJECT_NO_ATTRIBUTES_WARNING
+#endif
+
+/* qmake ignore Q_OBJECT */
+#define Q_OBJECT \
+public: \
+ QT_WARNING_PUSH \
+ Q_OBJECT_NO_OVERRIDE_WARNING \
+ static const QMetaObject staticMetaObject; \
+ virtual const QMetaObject *metaObject() const; \
+ virtual void *qt_metacast(const char *); \
+ virtual int qt_metacall(QMetaObject::Call, int, void **); \
+ QT_TR_FUNCTIONS \
+private: \
+ Q_OBJECT_NO_ATTRIBUTES_WARNING \
+ Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
+ QT_WARNING_POP \
+ struct QPrivateSignal {}; \
+ QT_ANNOTATE_CLASS(qt_qobject, "")
+
+/* qmake ignore Q_OBJECT */
+#define Q_OBJECT_FAKE Q_OBJECT QT_ANNOTATE_CLASS(qt_fake, "")
+
+#ifndef QT_NO_META_MACROS
+/* qmake ignore Q_GADGET */
+#define Q_GADGET \
+public: \
+ static const QMetaObject staticMetaObject; \
+ void qt_check_for_QGADGET_macro(); \
+ typedef void QtGadgetHelper; \
+private: \
+ QT_WARNING_PUSH \
+ Q_OBJECT_NO_ATTRIBUTES_WARNING \
+ Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
+ QT_WARNING_POP \
+ QT_ANNOTATE_CLASS(qt_qgadget, "") \
+ /*end*/
+
+/* qmake ignore Q_NAMESPACE_EXPORT */
+#define Q_NAMESPACE_EXPORT(...) \
+ extern __VA_ARGS__ const QMetaObject staticMetaObject; \
+ QT_ANNOTATE_CLASS(qt_qnamespace, "") \
+ /*end*/
+
+/* qmake ignore Q_NAMESPACE */
+#define Q_NAMESPACE Q_NAMESPACE_EXPORT() \
+ /*end*/
+
+#endif // QT_NO_META_MACROS
+
+#else // Q_MOC_RUN
+#define slots slots
+#define signals signals
+#define Q_SLOTS Q_SLOTS
+#define Q_SIGNALS Q_SIGNALS
+#define Q_CLASSINFO(name, value) Q_CLASSINFO(name, value)
+#define Q_INTERFACES(x) Q_INTERFACES(x)
+#define Q_PROPERTY(text) Q_PROPERTY(text)
+#define Q_PRIVATE_PROPERTY(d, text) Q_PRIVATE_PROPERTY(d, text)
+#define Q_REVISION(v) Q_REVISION(v)
+#define Q_OVERRIDE(text) Q_OVERRIDE(text)
+#define Q_ENUMS(x) Q_ENUMS(x)
+#define Q_FLAGS(x) Q_FLAGS(x)
+#define Q_ENUM(x) Q_ENUM(x)
+#define Q_FLAGS(x) Q_FLAGS(x)
+ /* qmake ignore Q_OBJECT */
+#define Q_OBJECT Q_OBJECT
+ /* qmake ignore Q_OBJECT */
+#define Q_OBJECT_FAKE Q_OBJECT_FAKE
+ /* qmake ignore Q_GADGET */
+#define Q_GADGET Q_GADGET
+#define Q_SCRIPTABLE Q_SCRIPTABLE
+#define Q_INVOKABLE Q_INVOKABLE
+#define Q_SIGNAL Q_SIGNAL
+#define Q_SLOT Q_SLOT
+#endif //Q_MOC_RUN
+
+QT_END_NAMESPACE
+
+#endif // QTMETAMACROS_H
diff --git a/src/corelib/kernel/qtranslator.cpp b/src/corelib/kernel/qtranslator.cpp
index 31c1277b03..4bd8874630 100644
--- a/src/corelib/kernel/qtranslator.cpp
+++ b/src/corelib/kernel/qtranslator.cpp
@@ -289,8 +289,8 @@ public:
#if defined(QT_USE_MMAP)
used_mmap(0),
#endif
- unmapPointer(0), unmapLength(0), resource(0),
- messageArray(0), offsetArray(0), contextArray(0), numerusRulesArray(0),
+ unmapPointer(nullptr), unmapLength(0), resource(nullptr),
+ messageArray(nullptr), offsetArray(nullptr), contextArray(nullptr), numerusRulesArray(nullptr),
messageLength(0), offsetLength(0), contextLength(0), numerusRulesLength(0) {}
#if defined(QT_USE_MMAP)
@@ -539,7 +539,7 @@ bool QTranslatorPrivate::do_load(const QString &realname, const QString &directo
ok = true;
} else {
delete resource;
- resource = 0;
+ resource = nullptr;
}
}
@@ -610,8 +610,8 @@ bool QTranslatorPrivate::do_load(const QString &realname, const QString &directo
delete [] unmapPointer;
delete d->resource;
- d->resource = 0;
- d->unmapPointer = 0;
+ d->resource = nullptr;
+ d->unmapPointer = nullptr;
d->unmapLength = 0;
return false;
@@ -874,10 +874,10 @@ bool QTranslatorPrivate::do_load(const uchar *data, qsizetype len, const QString
}
if (!ok) {
- messageArray = 0;
- contextArray = 0;
- offsetArray = 0;
- numerusRulesArray = 0;
+ messageArray = nullptr;
+ contextArray = nullptr;
+ offsetArray = nullptr;
+ numerusRulesArray = nullptr;
messageLength = 0;
contextLength = 0;
offsetLength = 0;
@@ -890,7 +890,7 @@ bool QTranslatorPrivate::do_load(const uchar *data, qsizetype len, const QString
static QString getMessage(const uchar *m, const uchar *end, const char *context,
const char *sourceText, const char *comment, uint numerus)
{
- const uchar *tn = 0;
+ const uchar *tn = nullptr;
uint tn_length = 0;
const uint sourceTextLen = uint(strlen(sourceText));
const uint contextLen = uint(strlen(context));
@@ -957,11 +957,11 @@ end:
QString QTranslatorPrivate::do_translate(const char *context, const char *sourceText,
const char *comment, int n) const
{
- if (context == 0)
+ if (context == nullptr)
context = "";
- if (sourceText == 0)
+ if (sourceText == nullptr)
sourceText = "";
- if (comment == 0)
+ if (comment == nullptr)
comment = "";
uint numerus = 0;
@@ -1076,13 +1076,13 @@ void QTranslatorPrivate::clear()
}
delete resource;
- resource = 0;
- unmapPointer = 0;
+ resource = nullptr;
+ unmapPointer = nullptr;
unmapLength = 0;
- messageArray = 0;
- contextArray = 0;
- offsetArray = 0;
- numerusRulesArray = 0;
+ messageArray = nullptr;
+ contextArray = nullptr;
+ offsetArray = nullptr;
+ numerusRulesArray = nullptr;
messageLength = 0;
contextLength = 0;
offsetLength = 0;
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp
index 60d1a91eef..d1bd2d308f 100644
--- a/src/corelib/kernel/qvariant.cpp
+++ b/src/corelib/kernel/qvariant.cpp
@@ -944,7 +944,7 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
const QVariantHash *hash = v_cast<QVariantHash>(d);
const auto end = hash->end();
for (auto it = hash->begin(); it != end; ++it)
- map->insertMulti(it.key(), it.value());
+ static_cast<QMultiMap<QString, QVariant> *>(map)->insert(it.key(), it.value());
#ifndef QT_BOOTSTRAPPED
} else if (d->type == QMetaType::QCborValue) {
if (!v_cast<QCborValue>(d)->isMap())
@@ -972,7 +972,7 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
const QVariantMap *map = v_cast<QVariantMap>(d);
const auto end = map->end();
for (auto it = map->begin(); it != end; ++it)
- hash->insertMulti(it.key(), it.value());
+ static_cast<QMultiHash<QString, QVariant> *>(hash)->insert(it.key(), it.value());
#ifndef QT_BOOTSTRAPPED
} else if (d->type == QMetaType::QCborValue) {
if (!v_cast<QCborValue>(d)->isMap())
@@ -1544,7 +1544,7 @@ static void customStreamDebug(QDebug dbg, const QVariant &variant) {
#ifndef QT_BOOTSTRAPPED
QMetaType::TypeFlags flags = QMetaType::typeFlags(variant.userType());
if (flags & QMetaType::PointerToQObject)
- dbg.nospace() << variant.value<QObject*>();
+ dbg.nospace() << qvariant_cast<QObject*>(variant);
#else
Q_UNUSED(dbg);
Q_UNUSED(variant);
@@ -2525,7 +2525,7 @@ void QVariant::load(QDataStream &s)
return;
}
}
- create(typeId, 0);
+ create(typeId, nullptr);
d.is_null = is_null;
if (!isValid()) {
@@ -2583,8 +2583,8 @@ void QVariant::save(QDataStream &s) const
#endif
// and as a result these types received lower ids too
typeId +=1;
- } else if (typeId == QMetaType::QPolygonF) {
- // This existed in Qt 4 only as a custom type
+ } else if (typeId == QMetaType::QPolygonF || typeId == QMetaType::QUuid) {
+ // These existed in Qt 4 only as a custom type
typeId = 127;
fakeUserType = true;
}
@@ -3840,58 +3840,6 @@ bool QVariant::convert(const int type, void *ptr) const
QMetaType::registerComparators().
*/
-/*!
- \fn bool QVariant::operator<(const QVariant &v) const
-
- Compares this QVariant with \a v and returns \c true if this is less than \a v.
-
- \note Comparability might not be availabe for the type stored in this QVariant
- or in \a v.
-
- \warning To make this function work with a custom type registered with
- qRegisterMetaType(), its comparison operator must be registered using
- QMetaType::registerComparators().
-*/
-
-/*!
- \fn bool QVariant::operator<=(const QVariant &v) const
-
- Compares this QVariant with \a v and returns \c true if this is less or equal than \a v.
-
- \note Comparability might not be available for the type stored in this QVariant
- or in \a v.
-
- \warning To make this function work with a custom type registered with
- qRegisterMetaType(), its comparison operator must be registered using
- QMetaType::registerComparators().
-*/
-
-/*!
- \fn bool QVariant::operator>(const QVariant &v) const
-
- Compares this QVariant with \a v and returns \c true if this is larger than \a v.
-
- \note Comparability might not be available for the type stored in this QVariant
- or in \a v.
-
- \warning To make this function work with a custom type registered with
- qRegisterMetaType(), its comparison operator must be registered using
- QMetaType::registerComparators().
-*/
-
-/*!
- \fn bool QVariant::operator>=(const QVariant &v) const
-
- Compares this QVariant with \a v and returns \c true if this is larger or equal than \a v.
-
- \note Comparability might not be available for the type stored in this QVariant
- or in \a v.
-
- \warning To make this function work with a custom type registered with
- qRegisterMetaType(), its comparison operator must be registered using
- QMetaType::registerComparators().
-*/
-
static bool qIsNumericType(uint tp)
{
static const qulonglong numericTypeBits =
@@ -4072,73 +4020,6 @@ bool QVariant::cmp(const QVariant &v) const
/*!
\internal
*/
-int QVariant::compare(const QVariant &v) const
-{
- // try numerics first, with C++ type promotion rules (no conversion)
- if (qIsNumericType(d.type) && qIsNumericType(v.d.type))
- return numericCompare(&d, &v.d);
-
- // check for equality next, as more types implement operator== than operator<
- if (cmp(v))
- return 0;
-
- const QVariant *v1 = this;
- const QVariant *v2 = &v;
- QVariant converted1;
- QVariant converted2;
-
- if (d.type != v.d.type) {
- // if both types differ, try to convert
- if (v2->canConvert(v1->d.type)) {
- converted2 = *v2;
- if (converted2.convert(v1->d.type))
- v2 = &converted2;
- }
- if (v1->d.type != v2->d.type && v1->canConvert(v2->d.type)) {
- converted1 = *v1;
- if (converted1.convert(v2->d.type))
- v1 = &converted1;
- }
- if (v1->d.type != v2->d.type) {
- // if conversion fails, default to toString
- int r = v1->toString().compare(v2->toString(), Qt::CaseInsensitive);
- if (r == 0) {
- // cmp(v) returned false, so we should try to agree with it.
- return (v1->d.type < v2->d.type) ? -1 : 1;
- }
- return r;
- }
-
- // did we end up with two numerics? If so, restart
- if (qIsNumericType(v1->d.type) && qIsNumericType(v2->d.type))
- return v1->compare(*v2);
- }
- if (v1->d.type >= QMetaType::User) {
- int result;
- if (QMetaType::compare(QT_PREPEND_NAMESPACE(constData(d)), QT_PREPEND_NAMESPACE(constData(v2->d)), d.type, &result))
- return result;
- }
- switch (v1->d.type) {
- case QVariant::Date:
- return v1->toDate() < v2->toDate() ? -1 : 1;
- case QVariant::Time:
- return v1->toTime() < v2->toTime() ? -1 : 1;
- case QVariant::DateTime:
- return v1->toDateTime() < v2->toDateTime() ? -1 : 1;
- case QVariant::StringList:
- return v1->toStringList() < v2->toStringList() ? -1 : 1;
- }
- int r = v1->toString().compare(v2->toString(), Qt::CaseInsensitive);
- if (r == 0) {
- // cmp(v) returned false, so we should try to agree with it.
- return (d.type < v.d.type) ? -1 : 1;
- }
- return r;
-}
-
-/*!
- \internal
- */
const void *QVariant::constData() const
{
@@ -4533,15 +4414,24 @@ QSequentialIterable::const_iterator QSequentialIterable::end() const
return it;
}
+static const QVariant variantFromVariantDataHelper(const QtMetaTypePrivate::VariantData &d) {
+ QVariant v;
+ if (d.metaTypeId == qMetaTypeId<QVariant>())
+ v = *reinterpret_cast<const QVariant*>(d.data);
+ else
+ v = QVariant(d.metaTypeId, d.data, d.flags & ~QVariantConstructionFlags::ShouldDeleteVariantData);
+ if (d.flags & QVariantConstructionFlags::ShouldDeleteVariantData)
+ QMetaType::destroy(d.metaTypeId, const_cast<void *>(d.data));
+ return v;
+}
+
/*!
Returns the element at position \a idx in the container.
*/
QVariant QSequentialIterable::at(int idx) const
{
const QtMetaTypePrivate::VariantData d = m_impl.at(idx);
- if (d.metaTypeId == qMetaTypeId<QVariant>())
- return *reinterpret_cast<const QVariant*>(d.data);
- return QVariant(d.metaTypeId, d.data, d.flags);
+ return variantFromVariantDataHelper(d);
}
/*!
@@ -4618,9 +4508,7 @@ QSequentialIterable::const_iterator::operator=(const const_iterator &other)
const QVariant QSequentialIterable::const_iterator::operator*() const
{
const QtMetaTypePrivate::VariantData d = m_impl.getCurrent();
- if (d.metaTypeId == qMetaTypeId<QVariant>())
- return *reinterpret_cast<const QVariant*>(d.data);
- return QVariant(d.metaTypeId, d.data, d.flags);
+ return variantFromVariantDataHelper(d);
}
/*!
@@ -4952,10 +4840,7 @@ QAssociativeIterable::const_iterator::operator=(const const_iterator &other)
const QVariant QAssociativeIterable::const_iterator::operator*() const
{
const QtMetaTypePrivate::VariantData d = m_impl.getCurrentValue();
- QVariant v(d.metaTypeId, d.data, d.flags);
- if (d.metaTypeId == qMetaTypeId<QVariant>())
- return *reinterpret_cast<const QVariant*>(d.data);
- return v;
+ return variantFromVariantDataHelper(d);
}
/*!
@@ -4964,10 +4849,7 @@ const QVariant QAssociativeIterable::const_iterator::operator*() const
const QVariant QAssociativeIterable::const_iterator::key() const
{
const QtMetaTypePrivate::VariantData d = m_impl.getCurrentKey();
- QVariant v(d.metaTypeId, d.data, d.flags);
- if (d.metaTypeId == qMetaTypeId<QVariant>())
- return *reinterpret_cast<const QVariant*>(d.data);
- return v;
+ return variantFromVariantDataHelper(d);
}
/*!
@@ -4975,11 +4857,7 @@ const QVariant QAssociativeIterable::const_iterator::key() const
*/
const QVariant QAssociativeIterable::const_iterator::value() const
{
- const QtMetaTypePrivate::VariantData d = m_impl.getCurrentValue();
- QVariant v(d.metaTypeId, d.data, d.flags);
- if (d.metaTypeId == qMetaTypeId<QVariant>())
- return *reinterpret_cast<const QVariant*>(d.data);
- return v;
+ return operator*();
}
/*!
diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h
index 331adea4e7..353bfa7ce7 100644
--- a/src/corelib/kernel/qvariant.h
+++ b/src/corelib/kernel/qvariant.h
@@ -53,7 +53,7 @@
#include <QtCore/qbytearraylist.h>
#endif
-#if QT_HAS_INCLUDE(<variant>) && __cplusplus >= 201703L
+#if __has_include(<variant>) && __cplusplus >= 201703L
#include <variant>
#elif defined(Q_CLANG_QDOC)
namespace std { template<typename...> struct variant; }
@@ -372,7 +372,7 @@ class Q_CORE_EXPORT QVariant
static inline QVariant fromValue(const T &value)
{ return QVariant(qMetaTypeId<T>(), &value, QTypeInfo<T>::isPointer); }
-#if (QT_HAS_INCLUDE(<variant>) && __cplusplus >= 201703L) || defined(Q_CLANG_QDOC)
+#if (__has_include(<variant>) && __cplusplus >= 201703L) || defined(Q_CLANG_QDOC)
template<typename... Types>
static inline QVariant fromStdVariant(const std::variant<Types...> &value)
{
@@ -396,7 +396,7 @@ class Q_CORE_EXPORT QVariant
struct Private
{
inline Private() noexcept : type(Invalid), is_shared(false), is_null(true)
- { data.ptr = nullptr; }
+ {}
// Internal constructor for initialized variants.
explicit inline Private(uint variantType) noexcept
@@ -412,6 +412,7 @@ class Q_CORE_EXPORT QVariant
#endif
union Data
{
+ void *threeptr[3] = { nullptr, nullptr, nullptr };
char c;
uchar uc;
short s;
@@ -465,14 +466,6 @@ class Q_CORE_EXPORT QVariant
{ return cmp(v); }
inline bool operator!=(const QVariant &v) const
{ return !cmp(v); }
- inline bool operator<(const QVariant &v) const
- { return compare(v) < 0; }
- inline bool operator<=(const QVariant &v) const
- { return compare(v) <= 0; }
- inline bool operator>(const QVariant &v) const
- { return compare(v) > 0; }
- inline bool operator>=(const QVariant &v) const
- { return compare(v) >= 0; }
protected:
friend inline bool operator==(const QVariant &, const QVariantComparisonHelper &);
@@ -491,7 +484,6 @@ public:
Private d;
void create(int type, const void *copy);
bool cmp(const QVariant &other) const;
- int compare(const QVariant &other) const;
bool convert(const int t, void *ptr) const; // ### Qt6: drop const
private:
@@ -546,7 +538,7 @@ inline QVariant QVariant::fromValue(const QVariant &value)
return value;
}
-#if QT_HAS_INCLUDE(<variant>) && __cplusplus >= 201703L
+#if __has_include(<variant>) && __cplusplus >= 201703L
template<>
inline QVariant QVariant::fromValue(const std::monostate &)
{
@@ -779,7 +771,7 @@ namespace QtPrivate {
return QSequentialIterable(QtMetaTypePrivate::QSequentialIterableImpl(reinterpret_cast<const QByteArrayList*>(v.constData())));
}
#endif
- return QSequentialIterable(v.value<QtMetaTypePrivate::QSequentialIterableImpl>());
+ return QSequentialIterable(qvariant_cast<QtMetaTypePrivate::QSequentialIterableImpl>(v));
}
};
template<>
@@ -794,7 +786,7 @@ namespace QtPrivate {
if (typeId == qMetaTypeId<QVariantHash>()) {
return QAssociativeIterable(QtMetaTypePrivate::QAssociativeIterableImpl(reinterpret_cast<const QVariantHash*>(v.constData())));
}
- return QAssociativeIterable(v.value<QtMetaTypePrivate::QAssociativeIterableImpl>());
+ return QAssociativeIterable(qvariant_cast<QtMetaTypePrivate::QAssociativeIterableImpl>(v));
}
};
template<>
@@ -803,7 +795,8 @@ namespace QtPrivate {
static QVariantList invoke(const QVariant &v)
{
const int typeId = v.userType();
- if (typeId == qMetaTypeId<QStringList>() || typeId == qMetaTypeId<QByteArrayList>() || QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>())) {
+ if (typeId == qMetaTypeId<QStringList>() || typeId == qMetaTypeId<QByteArrayList>() ||
+ (QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>()) && !QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QVariantList>()))) {
QSequentialIterable iter = QVariantValueHelperInterface<QSequentialIterable>::invoke(v);
QVariantList l;
l.reserve(iter.size());
@@ -820,12 +813,12 @@ namespace QtPrivate {
static QVariantHash invoke(const QVariant &v)
{
const int typeId = v.userType();
- if (typeId == qMetaTypeId<QVariantMap>() || QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>())) {
+ if (typeId == qMetaTypeId<QVariantMap>() || ((QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>())) && !QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QVariantHash>()))) {
QAssociativeIterable iter = QVariantValueHelperInterface<QAssociativeIterable>::invoke(v);
QVariantHash l;
l.reserve(iter.size());
for (QAssociativeIterable::const_iterator it = iter.begin(), end = iter.end(); it != end; ++it)
- l.insertMulti(it.key().toString(), it.value());
+ static_cast<QMultiHash<QString, QVariant> &>(l).insert(it.key().toString(), it.value());
return l;
}
return QVariantValueHelper<QVariantHash>::invoke(v);
@@ -837,11 +830,11 @@ namespace QtPrivate {
static QVariantMap invoke(const QVariant &v)
{
const int typeId = v.userType();
- if (typeId == qMetaTypeId<QVariantHash>() || QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>())) {
+ if (typeId == qMetaTypeId<QVariantHash>() || (QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>()) && !QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QVariantMap>()))) {
QAssociativeIterable iter = QVariantValueHelperInterface<QAssociativeIterable>::invoke(v);
QVariantMap l;
for (QAssociativeIterable::const_iterator it = iter.begin(), end = iter.end(); it != end; ++it)
- l.insertMulti(it.key().toString(), it.value());
+ static_cast<QMultiMap<QString, QVariant> &>(l).insert(it.key().toString(), it.value());
return l;
}
return QVariantValueHelper<QVariantMap>::invoke(v);
@@ -853,12 +846,9 @@ namespace QtPrivate {
static QPair<QVariant, QVariant> invoke(const QVariant &v)
{
const int typeId = v.userType();
- if (typeId == qMetaTypeId<QPair<QVariant, QVariant> >())
- return QVariantValueHelper<QPair<QVariant, QVariant> >::invoke(v);
- if (QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QtMetaTypePrivate::QPairVariantInterfaceImpl>())) {
+ if (QMetaType::hasRegisteredConverterFunction(typeId, qMetaTypeId<QtMetaTypePrivate::QPairVariantInterfaceImpl>()) && !(typeId == qMetaTypeId<QPair<QVariant, QVariant> >())) {
QtMetaTypePrivate::QPairVariantInterfaceImpl pi = v.value<QtMetaTypePrivate::QPairVariantInterfaceImpl>();
-
const QtMetaTypePrivate::VariantData d1 = pi.first();
QVariant v1(d1.metaTypeId, d1.data, d1.flags);
if (d1.metaTypeId == qMetaTypeId<QVariant>())
diff --git a/src/corelib/kernel/qvariant_p.h b/src/corelib/kernel/qvariant_p.h
index 3d87beac83..b8b63b5e6f 100644
--- a/src/corelib/kernel/qvariant_p.h
+++ b/src/corelib/kernel/qvariant_p.h
@@ -105,6 +105,11 @@ inline T *v_cast(QVariant::Private *d, T * = nullptr)
#endif
+enum QVariantConstructionFlags : uint {
+ Default = 0x0,
+ PointerType = 0x1,
+ ShouldDeleteVariantData = 0x2 // only used in Q*Iterable
+};
//a simple template that avoids to allocate 2 memory chunks when creating a QVariant
template <class T> class QVariantPrivateSharedEx : public QVariant::PrivateShared
diff --git a/src/corelib/kernel/qwineventnotifier.cpp b/src/corelib/kernel/qwineventnotifier.cpp
index d2ae9668fe..db5d44b276 100644
--- a/src/corelib/kernel/qwineventnotifier.cpp
+++ b/src/corelib/kernel/qwineventnotifier.cpp
@@ -124,7 +124,7 @@ QWinEventNotifier::QWinEventNotifier(HANDLE hEvent, QObject *parent)
: QObject(*new QWinEventNotifierPrivate(hEvent, false), parent)
{
Q_D(QWinEventNotifier);
- QAbstractEventDispatcher *eventDispatcher = d->threadData->eventDispatcher.loadRelaxed();
+ QAbstractEventDispatcher *eventDispatcher = d->threadData.loadRelaxed()->eventDispatcher.loadRelaxed();
if (Q_UNLIKELY(!eventDispatcher)) {
qWarning("QWinEventNotifier: Can only be used with threads started with QThread");
return;
@@ -197,7 +197,7 @@ void QWinEventNotifier::setEnabled(bool enable)
return;
d->enabled = enable;
- QAbstractEventDispatcher *eventDispatcher = d->threadData->eventDispatcher.loadRelaxed();
+ QAbstractEventDispatcher *eventDispatcher = d->threadData.loadRelaxed()->eventDispatcher.loadRelaxed();
if (!eventDispatcher) { // perhaps application is shutting down
if (!enable && d->waitHandle != nullptr)
d->unregisterWaitObject();
@@ -256,7 +256,7 @@ void QWinEventNotifierPrivate::unregisterWaitObject()
static void CALLBACK wfsoCallback(void *context, BOOLEAN /*ignore*/)
{
QWinEventNotifierPrivate *nd = reinterpret_cast<QWinEventNotifierPrivate *>(context);
- QAbstractEventDispatcher *eventDispatcher = nd->threadData->eventDispatcher.loadRelaxed();
+ QAbstractEventDispatcher *eventDispatcher = nd->threadData.loadRelaxed()->eventDispatcher.loadRelaxed();
// Happens when Q(Core)Application is destroyed before QWinEventNotifier.
// https://bugreports.qt.io/browse/QTBUG-70214
diff --git a/src/corelib/mimetypes/mime/generate.bat b/src/corelib/mimetypes/mime/generate.bat
new file mode 100644
index 0000000000..f63fc63693
--- /dev/null
+++ b/src/corelib/mimetypes/mime/generate.bat
@@ -0,0 +1,73 @@
+:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
+::
+:: Copyright (C) 2019 Intel Corporation.
+:: Contact: https://www.qt.io/licensing/
+::
+:: This file is part of the tools applications of the Qt Toolkit.
+::
+:: $QT_BEGIN_LICENSE:GPL-EXCEPT$
+:: Commercial License Usage
+:: Licensees holding valid commercial Qt licenses may use this file in
+:: accordance with the commercial license agreement provided with the
+:: Software or, alternatively, in accordance with the terms contained in
+:: a written agreement between you and The Qt Company. For licensing terms
+:: and conditions see https://www.qt.io/terms-conditions. For further
+:: information use the contact form at https://www.qt.io/contact-us.
+::
+:: GNU General Public License Usage
+:: Alternatively, this file may be used under the terms of the GNU
+:: General Public License version 3 as published by the Free Software
+:: Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+:: included in the packaging of this file. Please review the following
+:: information to ensure the GNU General Public License requirements will
+:: be met: https://www.gnu.org/licenses/gpl-3.0.html.
+::
+:: $QT_END_LICENSE$
+::
+:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
+
+@echo off
+setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
+set me=%~dp0
+
+:: Check if certain tools are in PATH
+for %%C in (gzip.exe zstd.exe perl.exe) do set %%C=%%~$PATH:C
+
+:: If perl is in PATH, just let it do everything
+if not "%perl.exe%" == "" goto PuntToPerl
+
+set COMPRESS=
+set MACRO=MIME_DATABASE_IS_UNCOMPRESSED
+if not "%gzip.exe%" == "" (
+ set COMPRESS=gzip -9
+ set MACRO=MIME_DATABASE_IS_GZIP
+)
+
+:: Check if zstd support was enabled
+if /i "%~1" == "--zstd" (
+ shift
+ if not "%zstd.exe%" == "" (
+ set COMPRESS=zstd -19
+ set MACRO=MIME_DATABASE_IS_ZSTD
+ )
+)
+
+if not "%COMPRESS%" == "" goto CompressedCommon
+
+:: No Compression and no Perl
+:: Just hex-dump with Powershell
+powershell -ExecutionPolicy Bypass %me%hexdump.ps1 %1 %1
+exit /b %errorlevel%
+
+:CompressedCommon
+:: Compress to a temporary file, then hex-dump using Powershell
+echo #define %MACRO%
+set tempfile=generate%RANDOM%.tmp
+%COMPRESS% < %1 > %tempfile%
+powershell -ExecutionPolicy Bypass %me%hexdump.ps1 %tempfile% %1
+del %tempfile%
+exit /b %errorlevel%
+
+:PuntToPerl
+perl %me%generate.pl %*
+exit /b %errorlevel%
diff --git a/src/corelib/mimetypes/mime/generate.pl b/src/corelib/mimetypes/mime/generate.pl
new file mode 100644
index 0000000000..0f87d61f8e
--- /dev/null
+++ b/src/corelib/mimetypes/mime/generate.pl
@@ -0,0 +1,113 @@
+#!/usr/bin/perl
+#############################################################################
+##
+## Copyright (C) 2019 Intel Corporation.
+## Contact: https://www.qt.io/licensing/
+##
+## This file is the build configuration utility of the Qt Toolkit.
+##
+## $QT_BEGIN_LICENSE:GPL-EXCEPT$
+## Commercial License Usage
+## Licensees holding valid commercial Qt licenses may use this file in
+## accordance with the commercial license agreement provided with the
+## Software or, alternatively, in accordance with the terms contained in
+## a written agreement between you and The Qt Company. For licensing terms
+## and conditions see https://www.qt.io/terms-conditions. For further
+## information use the contact form at https://www.qt.io/contact-us.
+##
+## GNU General Public License Usage
+## Alternatively, this file may be used under the terms of the GNU
+## General Public License version 3 as published by the Free Software
+## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+## included in the packaging of this file. Please review the following
+## information to ensure the GNU General Public License requirements will
+## be met: https://www.gnu.org/licenses/gpl-3.0.html.
+##
+## $QT_END_LICENSE$
+##
+#############################################################################
+use strict;
+use warnings;
+use Config;
+local $/; # Enable "slurp" mode
+
+sub checkCommand($) {
+ use File::Spec::Functions;
+ my $cmd = $_[0] . $Config{_exe};
+ for my $path (path()) {
+ return 1 if -x catfile($path, $cmd);
+ }
+ return 0;
+}
+
+my $data;
+my $compress;
+my $macro;
+my $zlib = eval 'use Compress::Zlib; use IO::Compress::Gzip; return 1;';
+my $fname = shift @ARGV;
+
+if ($zlib) {
+ # Prefer internal zlib support (useful on Windows where gzip.exe isn't
+ # always presnet)
+ $macro = "MIME_DATABASE_IS_GZIP";
+} elsif (checkCommand("gzip")) {
+ # No builtin support for compression (old Perl?)
+ $compress = "gzip -c9";
+ $macro = "MIME_DATABASE_IS_GZIP";
+}
+
+# Check if Qt is being built with zstd support
+if ($fname eq "--zstd") {
+ $fname = shift @ARGV;
+ if (checkCommand("zstd")) {
+ $compress = "zstd -cq19 --single-thread";
+ $macro = "MIME_DATABASE_IS_ZSTD";
+ }
+}
+
+# Check if xml (from xmlstarlet) is in $PATH
+my $cmd;
+if (checkCommand("xml")) {
+ # Minify the data before compressing
+ $cmd = "xml sel -D -B -t -c / $fname";
+ $cmd .= "| $compress" if $compress;
+} elsif ($compress) {
+ $cmd = "$compress < $fname"
+}
+if ($cmd) {
+ # Run the command and read everything
+ open CMD, "$cmd |";
+ $data = <CMD>;
+ close CMD;
+ die("Failed to run $cmd") if ($? >> 8);
+} else {
+ # No command, just read the file
+ open F, "<$fname";
+ $data = <F>;
+ close F;
+}
+
+# Do we need to compress with zlib?
+if (!$compress && $zlib) {
+ $data = eval q{
+ use Compress::Zlib;
+ use IO::Compress::Gzip qw(gzip);
+ my $compressed;
+ gzip \$data => \$compressed,
+ Minimal => 1,
+ Level => Z_BEST_COMPRESSION;
+ return $compressed;
+ };
+}
+
+# Now print as hex
+printf "#define %s\n", $macro if $macro;
+printf "static const unsigned char mimetype_database[] = {";
+my $i = 0;
+map {
+ printf "\n " if $i++ % 12 == 0;
+ printf "0x%02x, ", ord $_
+} split //, $data;
+printf "\n};\n";
+printf "static constexpr size_t MimeTypeDatabaseOriginalSize = %d;\n",
+ (stat $fname)[7];
diff --git a/src/corelib/mimetypes/mime/hexdump.ps1 b/src/corelib/mimetypes/mime/hexdump.ps1
new file mode 100644
index 0000000000..25ce8138fa
--- /dev/null
+++ b/src/corelib/mimetypes/mime/hexdump.ps1
@@ -0,0 +1,43 @@
+#############################################################################
+##
+## Copyright (C) 2019 Intel Corporation.
+## Contact: https://www.qt.io/licensing/
+##
+## This file is the build configuration utility of the Qt Toolkit.
+##
+## $QT_BEGIN_LICENSE:GPL-EXCEPT$
+## Commercial License Usage
+## Licensees holding valid commercial Qt licenses may use this file in
+## accordance with the commercial license agreement provided with the
+## Software or, alternatively, in accordance with the terms contained in
+## a written agreement between you and The Qt Company. For licensing terms
+## and conditions see https://www.qt.io/terms-conditions. For further
+## information use the contact form at https://www.qt.io/contact-us.
+##
+## GNU General Public License Usage
+## Alternatively, this file may be used under the terms of the GNU
+## General Public License version 3 as published by the Free Software
+## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+## included in the packaging of this file. Please review the following
+## information to ensure the GNU General Public License requirements will
+## be met: https://www.gnu.org/licenses/gpl-3.0.html.
+##
+## $QT_END_LICENSE$
+##
+#############################################################################
+
+param([String]$path, [String]$orig)
+
+"static const unsigned char mimetype_database[] = {"
+ForEach ($byte in Get-Content -Encoding byte -ReadCount 16 -path $path) {
+# if (($byte -eq 0).count -ne 16) {
+ $hex = $byte | Foreach-Object {
+ " 0x" + ("{0:x}" -f $_).PadLeft( 2, "0" ) + ","
+ }
+ " $hex"
+# }
+}
+"};"
+
+$file = Get-Childitem -file $orig
+"static constexpr size_t MimeTypeDatabaseOriginalSize = " + $file.length + ";"
diff --git a/src/corelib/mimetypes/mimetypes.pri b/src/corelib/mimetypes/mimetypes.pri
index 62bbe348e4..8cbe7b69ae 100644
--- a/src/corelib/mimetypes/mimetypes.pri
+++ b/src/corelib/mimetypes/mimetypes.pri
@@ -21,5 +21,32 @@ qtConfig(mimetype) {
mimetypes/qmimeglobpattern.cpp \
mimetypes/qmimeprovider.cpp
- qtConfig(mimetype-database): RESOURCES += mimetypes/mimetypes.qrc
+ MIME_DATABASE = mimetypes/mime/packages/freedesktop.org.xml
+ OTHER_FILES += $$MIME_DATABASE
+
+ qtConfig(mimetype-database) {
+ outpath = .rcc
+ debug_and_release {
+ CONFIG(debug, debug|release): outpath = .rcc/debug
+ else: outpath = .rcc/release
+ }
+
+ mimedb.depends = $$PWD/mime/generate.pl
+ equals(MAKEFILE_GENERATOR, MSVC.NET)|equals(MAKEFILE_GENERATOR, MSBUILD)|isEmpty(QMAKE_SH) {
+ mimedb.commands = cmd /c $$shell_path($$PWD/mime/generate.bat)
+ mimedb.depends += $$PWD/mime/generate.bat $$PWD/mime/hexdump.ps1
+ } else {
+ mimedb.commands = perl $${mimedb.depends}
+ }
+
+ qtConfig(zstd): mimedb.commands += --zstd
+ mimedb.commands += ${QMAKE_FILE_IN} > ${QMAKE_FILE_OUT}
+
+ mimedb.output = $$outpath/qmimeprovider_database.cpp
+ mimedb.input = MIME_DATABASE
+ mimedb.variable_out = INCLUDED_SOURCES
+ QMAKE_EXTRA_COMPILERS += mimedb
+ INCLUDEPATH += $$outpath
+ unset(outpath)
+ }
}
diff --git a/src/corelib/mimetypes/qmimedatabase.cpp b/src/corelib/mimetypes/qmimedatabase.cpp
index ce84a15831..f1d0dc859e 100644
--- a/src/corelib/mimetypes/qmimedatabase.cpp
+++ b/src/corelib/mimetypes/qmimedatabase.cpp
@@ -102,13 +102,18 @@ void QMimeDatabasePrivate::loadProviders()
const auto fdoIterator = std::find_if(mimeDirs.constBegin(), mimeDirs.constEnd(), [](const QString &mimeDir) -> bool {
return QFileInfo::exists(mimeDir + QStringLiteral("/packages/freedesktop.org.xml")); }
);
- if (fdoIterator == mimeDirs.constEnd())
- mimeDirs.prepend(QLatin1String(":/qt-project.org/qmime"));
//qDebug() << "mime dirs:" << mimeDirs;
Providers currentProviders;
std::swap(m_providers, currentProviders);
- m_providers.reserve(mimeDirs.size());
+
+ if (QMimeXMLProvider::InternalDatabaseAvailable && fdoIterator == mimeDirs.constEnd()) {
+ m_providers.reserve(mimeDirs.size() + 1);
+ m_providers.push_back(Providers::value_type(new QMimeXMLProvider(this, QMimeXMLProvider::InternalDatabase)));
+ } else {
+ m_providers.reserve(mimeDirs.size());
+ }
+
for (const QString &mimeDir : qAsConst(mimeDirs)) {
const QString cacheFile = mimeDir + QStringLiteral("/mime.cache");
QFileInfo fileInfo(cacheFile);
@@ -487,7 +492,7 @@ QMimeDatabase::QMimeDatabase() :
*/
QMimeDatabase::~QMimeDatabase()
{
- d = 0;
+ d = nullptr;
}
/*!
diff --git a/src/corelib/mimetypes/qmimeprovider.cpp b/src/corelib/mimetypes/qmimeprovider.cpp
index a3a6b9615c..4aee772366 100644
--- a/src/corelib/mimetypes/qmimeprovider.cpp
+++ b/src/corelib/mimetypes/qmimeprovider.cpp
@@ -1,7 +1,8 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2015 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author David Faure <david.faure@kdab.com>
+** Copyright (C) 2018 The Qt Company Ltd.
+** Copyright (C) 2018 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author David Faure <david.faure@kdab.com>
+** Copyright (C) 2019 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -45,6 +46,7 @@
#include "qmimemagicrulematcher_p.h"
#include <QXmlStreamReader>
+#include <QBuffer>
#include <QDir>
#include <QFile>
#include <QByteArrayMatcher>
@@ -52,12 +54,33 @@
#include <QDateTime>
#include <QtEndian>
-static void initResources()
-{
#if QT_CONFIG(mimetype_database)
- Q_INIT_RESOURCE(mimetypes);
+# if defined(Q_CC_MSVC)
+# pragma section(".qtmimedatabase", read, shared)
+__declspec(allocate(".qtmimedatabase")) __declspec(align(4096))
+# elif defined(Q_OS_DARWIN)
+__attribute__((section("__TEXT,.qtmimedatabase"), aligned(4096)))
+# elif (defined(Q_OF_ELF) || defined(Q_OS_WIN)) && defined(Q_CC_GNU)
+__attribute__((section(".qtmimedatabase"), aligned(4096)))
+# endif
+
+# include "qmimeprovider_database.cpp"
+
+# ifdef MIME_DATABASE_IS_ZSTD
+# if !QT_CONFIG(zstd)
+# error "MIME database is zstd but no support compiled in!"
+# endif
+# include <zstd.h>
+# endif
+# ifdef MIME_DATABASE_IS_GZIP
+# ifdef QT_NO_COMPRESS
+# error "MIME database is zlib but no support compiled in!"
+# endif
+# define ZLIB_CONST
+# include <zconf.h>
+# include <zlib.h>
+# endif
#endif
-}
QT_BEGIN_NAMESPACE
@@ -130,7 +153,7 @@ bool QMimeBinaryProvider::CacheFile::reload()
if (file.isOpen()) {
file.close();
}
- data = 0;
+ data = nullptr;
return load();
}
@@ -306,7 +329,7 @@ bool QMimeBinaryProvider::matchMagicRule(QMimeBinaryProvider::CacheFile *cacheFi
const int valueLength = cacheFile->getUint32(off + 12);
const int valueOffset = cacheFile->getUint32(off + 16);
const int maskOffset = cacheFile->getUint32(off + 20);
- const char *mask = maskOffset ? cacheFile->getCharStar(maskOffset) : NULL;
+ const char *mask = maskOffset ? cacheFile->getCharStar(maskOffset) : nullptr;
if (!QMimeMagicRule::matchSubstring(dataPtr, dataSize, rangeStart, rangeLength, valueLength, cacheFile->getCharStar(valueOffset), mask))
continue;
@@ -597,10 +620,55 @@ void QMimeBinaryProvider::loadGenericIcon(QMimeTypePrivate &data)
////
+#if QT_CONFIG(mimetype_database)
+static QString internalMimeFileName()
+{
+ return QStringLiteral("<internal MIME data>");
+}
+
+QMimeXMLProvider::QMimeXMLProvider(QMimeDatabasePrivate *db, InternalDatabaseEnum)
+ : QMimeProviderBase(db, internalMimeFileName())
+{
+ Q_STATIC_ASSERT_X(sizeof(mimetype_database), "Bundled MIME database is empty");
+ Q_STATIC_ASSERT_X(sizeof(mimetype_database) <= MimeTypeDatabaseOriginalSize,
+ "Compressed MIME database is larger than the original size");
+ Q_STATIC_ASSERT_X(MimeTypeDatabaseOriginalSize <= 16*1024*1024,
+ "Bundled MIME database is too big");
+ const char *data = reinterpret_cast<const char *>(mimetype_database);
+ qsizetype size = MimeTypeDatabaseOriginalSize;
+
+#ifdef MIME_DATABASE_IS_ZSTD
+ // uncompress with libzstd
+ std::unique_ptr<char []> uncompressed(new char[size]);
+ size = ZSTD_decompress(uncompressed.get(), size, mimetype_database, sizeof(mimetype_database));
+ Q_ASSERT(!ZSTD_isError(size));
+ data = uncompressed.get();
+#elif defined(MIME_DATABASE_IS_GZIP)
+ std::unique_ptr<char []> uncompressed(new char[size]);
+ z_stream zs = {};
+ zs.next_in = mimetype_database;
+ zs.avail_in = sizeof(mimetype_database);
+ zs.next_out = reinterpret_cast<Bytef *>(uncompressed.get());
+ zs.avail_out = size;
+
+ int res = inflateInit2(&zs, MAX_WBITS | 32);
+ Q_ASSERT(res == Z_OK);
+ res = inflate(&zs, Z_FINISH);
+ Q_ASSERT(res == Z_STREAM_END);
+ res = inflateEnd(&zs);
+ Q_ASSERT(res == Z_OK);
+
+ data = uncompressed.get();
+ size = zs.total_out;
+#endif
+
+ load(data, size);
+}
+#endif
+
QMimeXMLProvider::QMimeXMLProvider(QMimeDatabasePrivate *db, const QString &directory)
: QMimeProviderBase(db, directory)
{
- initResources();
ensureLoaded();
}
@@ -692,6 +760,19 @@ bool QMimeXMLProvider::load(const QString &fileName, QString *errorMessage)
return parser.parse(&file, fileName, errorMessage);
}
+#if QT_CONFIG(mimetype_database)
+void QMimeXMLProvider::load(const char *data, qsizetype len)
+{
+ QBuffer buffer;
+ buffer.setData(QByteArray::fromRawData(data, len));
+ buffer.open(QIODevice::ReadOnly);
+ QString errorMessage;
+ QMimeTypeParser parser(*this);
+ if (!parser.parse(&buffer, internalMimeFileName(), &errorMessage))
+ qWarning("QMimeDatabase: Error loading internal MIME data\n%s", qPrintable(errorMessage));
+}
+#endif
+
void QMimeXMLProvider::addGlobPattern(const QMimeGlobPattern &glob)
{
m_mimeTypeGlobs.addGlob(glob);
diff --git a/src/corelib/mimetypes/qmimeprovider_p.h b/src/corelib/mimetypes/qmimeprovider_p.h
index b6268210c0..0629df8a95 100644
--- a/src/corelib/mimetypes/qmimeprovider_p.h
+++ b/src/corelib/mimetypes/qmimeprovider_p.h
@@ -132,6 +132,16 @@ private:
class QMimeXMLProvider : public QMimeProviderBase
{
public:
+ enum InternalDatabaseEnum { InternalDatabase };
+#if QT_CONFIG(mimetype_database)
+ enum : bool { InternalDatabaseAvailable = true };
+ QMimeXMLProvider(QMimeDatabasePrivate *db, InternalDatabaseEnum);
+#else
+ enum : bool { InternalDatabaseAvailable = false };
+ QMimeXMLProvider(QMimeDatabasePrivate *db, InternalDatabaseEnum)
+ : QMimeProviderBase(db, QString())
+ { Q_UNREACHABLE() };
+#endif
QMimeXMLProvider(QMimeDatabasePrivate *db, const QString &directory);
~QMimeXMLProvider();
@@ -156,6 +166,7 @@ public:
private:
void load(const QString &fileName);
+ void load(const char *data, qsizetype len);
typedef QHash<QString, QMimeType> NameMimeTypeMap;
NameMimeTypeMap m_nameMimeTypeMap;
diff --git a/src/corelib/plugin/qfactoryloader.cpp b/src/corelib/plugin/qfactoryloader.cpp
index 18f10c9b43..14de8db1c6 100644
--- a/src/corelib/plugin/qfactoryloader.cpp
+++ b/src/corelib/plugin/qfactoryloader.cpp
@@ -212,7 +212,7 @@ void QFactoryLoader::update()
QStringList(QLatin1String("libplugins_%1_*.so").arg(d->suffix)),
#endif
QDir::Files);
- QLibraryPrivate *library = 0;
+ QLibraryPrivate *library = nullptr;
for (int j = 0; j < plugins.count(); ++j) {
QString fileName = QDir::cleanPath(path + QLatin1Char('/') + plugins.at(j));
@@ -383,7 +383,7 @@ QObject *QFactoryLoader::instance(int index) const
{
Q_D(const QFactoryLoader);
if (index < 0)
- return 0;
+ return nullptr;
#if QT_CONFIG(library)
QMutexLocker lock(&d->mutex);
@@ -399,7 +399,7 @@ QObject *QFactoryLoader::instance(int index) const
return obj;
}
}
- return 0;
+ return nullptr;
}
index -= d->libraryList.size();
lock.unlock();
@@ -416,7 +416,7 @@ QObject *QFactoryLoader::instance(int index) const
--index;
}
- return 0;
+ return nullptr;
}
QMultiMap<int, QString> QFactoryLoader::keyMap() const
diff --git a/src/corelib/plugin/qlibrary.cpp b/src/corelib/plugin/qlibrary.cpp
index 39a170db3f..406a83c7dd 100644
--- a/src/corelib/plugin/qlibrary.cpp
+++ b/src/corelib/plugin/qlibrary.cpp
@@ -256,7 +256,7 @@ static bool findPatternUnloaded(const QString &library, QLibraryPrivate *lib)
qsizetype fdlen = qMin(file.size(), MaxMemoryMapSize);
const char *filedata = reinterpret_cast<char *>(file.map(0, fdlen));
- if (filedata == 0) {
+ if (filedata == nullptr) {
// Try reading the data into memory instead (up to 64 MB).
data = file.read(64 * 1024 * 1024);
filedata = data.constData();
@@ -387,12 +387,12 @@ private:
};
static QBasicMutex qt_library_mutex;
-static QLibraryStore *qt_library_data = 0;
+static QLibraryStore *qt_library_data = nullptr;
static bool qt_library_data_once;
QLibraryStore::~QLibraryStore()
{
- qt_library_data = 0;
+ qt_library_data = nullptr;
}
inline void QLibraryStore::cleanup()
@@ -459,7 +459,7 @@ inline QLibraryPrivate *QLibraryStore::findOrCreate(const QString &fileName, con
QLibraryStore *data = instance();
// check if this library is already loaded
- QLibraryPrivate *lib = 0;
+ QLibraryPrivate *lib = nullptr;
if (Q_LIKELY(data)) {
lib = data->libraryMap.value(fileName);
if (lib)
@@ -498,7 +498,7 @@ inline void QLibraryStore::releaseLibrary(QLibraryPrivate *lib)
}
QLibraryPrivate::QLibraryPrivate(const QString &canonicalFileName, const QString &version, QLibrary::LoadHints loadHints)
- : pHnd(0), fileName(canonicalFileName), fullVersion(version), instance(0),
+ : pHnd(nullptr), fileName(canonicalFileName), fullVersion(version), instance(nullptr),
libraryRefCount(0), libraryUnloadCount(0), pluginState(MightBeAPlugin)
{
loadHintsInt.storeRelaxed(loadHints);
@@ -528,7 +528,7 @@ void QLibraryPrivate::mergeLoadHints(QLibrary::LoadHints lh)
QFunctionPointer QLibraryPrivate::resolve(const char *symbol)
{
if (!pHnd)
- return 0;
+ return nullptr;
return resolve_sys(symbol);
}
@@ -584,12 +584,12 @@ bool QLibraryPrivate::unload(UnloadFlag flag)
//when the library is unloaded, we release the reference on it so that 'this'
//can get deleted
libraryRefCount.deref();
- pHnd = 0;
- instance = 0;
+ pHnd = nullptr;
+ instance = nullptr;
}
}
- return (pHnd == 0);
+ return (pHnd == nullptr);
}
void QLibraryPrivate::release()
@@ -847,7 +847,7 @@ bool QLibrary::isLoaded() const
Constructs a library with the given \a parent.
*/
QLibrary::QLibrary(QObject *parent)
- :QObject(parent), d(0), did_load(false)
+ :QObject(parent), d(nullptr), did_load(false)
{
}
@@ -862,7 +862,7 @@ QLibrary::QLibrary(QObject *parent)
".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.)
*/
QLibrary::QLibrary(const QString& fileName, QObject *parent)
- :QObject(parent), d(0), did_load(false)
+ :QObject(parent), d(nullptr), did_load(false)
{
setFileName(fileName);
}
@@ -879,7 +879,7 @@ QLibrary::QLibrary(const QString& fileName, QObject *parent)
".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.)
*/
QLibrary::QLibrary(const QString& fileName, int verNum, QObject *parent)
- :QObject(parent), d(0), did_load(false)
+ :QObject(parent), d(nullptr), did_load(false)
{
setFileNameAndVersion(fileName, verNum);
}
@@ -895,7 +895,7 @@ QLibrary::QLibrary(const QString& fileName, int verNum, QObject *parent)
".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.)
*/
QLibrary::QLibrary(const QString& fileName, const QString &version, QObject *parent)
- :QObject(parent), d(0), did_load(false)
+ :QObject(parent), d(nullptr), did_load(false)
{
setFileNameAndVersion(fileName, version);
}
@@ -942,7 +942,7 @@ void QLibrary::setFileName(const QString &fileName)
if (d) {
lh = d->loadHints();
d->release();
- d = 0;
+ d = nullptr;
did_load = false;
}
d = QLibraryPrivate::findOrCreate(fileName, QString(), lh);
@@ -970,7 +970,7 @@ void QLibrary::setFileNameAndVersion(const QString &fileName, int verNum)
if (d) {
lh = d->loadHints();
d->release();
- d = 0;
+ d = nullptr;
did_load = false;
}
d = QLibraryPrivate::findOrCreate(fileName, verNum >= 0 ? QString::number(verNum) : QString(), lh);
@@ -991,7 +991,7 @@ void QLibrary::setFileNameAndVersion(const QString &fileName, const QString &ver
if (d) {
lh = d->loadHints();
d->release();
- d = 0;
+ d = nullptr;
did_load = false;
}
d = QLibraryPrivate::findOrCreate(fileName, version, lh);
@@ -1020,7 +1020,7 @@ void QLibrary::setFileNameAndVersion(const QString &fileName, const QString &ver
QFunctionPointer QLibrary::resolve(const char *symbol)
{
if (!isLoaded() && !load())
- return 0;
+ return nullptr;
return d->resolve(symbol);
}
diff --git a/src/corelib/plugin/qlibrary_p.h b/src/corelib/plugin/qlibrary_p.h
index db5afac98e..a58547a2c3 100644
--- a/src/corelib/plugin/qlibrary_p.h
+++ b/src/corelib/plugin/qlibrary_p.h
@@ -96,7 +96,7 @@ public:
void setLoadHints(QLibrary::LoadHints lh);
static QLibraryPrivate *findOrCreate(const QString &fileName, const QString &version = QString(),
- QLibrary::LoadHints loadHints = nullptr);
+ QLibrary::LoadHints loadHints = { });
static QStringList suffixes_sys(const QString &fullVersion);
static QStringList prefixes_sys();
diff --git a/src/corelib/plugin/qlibrary_unix.cpp b/src/corelib/plugin/qlibrary_unix.cpp
index f0de1010d7..6eb84b327b 100644
--- a/src/corelib/plugin/qlibrary_unix.cpp
+++ b/src/corelib/plugin/qlibrary_unix.cpp
@@ -277,7 +277,7 @@ bool QLibraryPrivate::load_sys()
qualifiedFileName = attempt;
errorString.clear();
}
- return (pHnd != 0);
+ return (pHnd != nullptr);
}
bool QLibraryPrivate::unload_sys()
diff --git a/src/corelib/plugin/qpluginloader.cpp b/src/corelib/plugin/qpluginloader.cpp
index cadff4f32b..aed1704d5f 100644
--- a/src/corelib/plugin/qpluginloader.cpp
+++ b/src/corelib/plugin/qpluginloader.cpp
@@ -136,7 +136,7 @@ QT_BEGIN_NAMESPACE
Constructs a plugin loader with the given \a parent.
*/
QPluginLoader::QPluginLoader(QObject *parent)
- : QObject(parent), d(0), did_load(false)
+ : QObject(parent), d(nullptr), did_load(false)
{
}
@@ -152,7 +152,7 @@ QPluginLoader::QPluginLoader(QObject *parent)
\sa setFileName()
*/
QPluginLoader::QPluginLoader(const QString &fileName, QObject *parent)
- : QObject(parent), d(0), did_load(false)
+ : QObject(parent), d(nullptr), did_load(false)
{
setFileName(fileName);
setLoadHints(QLibrary::PreventUnloadHint);
@@ -195,7 +195,7 @@ QPluginLoader::~QPluginLoader()
QObject *QPluginLoader::instance()
{
if (!isLoaded() && !load())
- return 0;
+ return nullptr;
if (!d->inst && d->instance)
d->inst = d->instance();
return d->inst.data();
@@ -305,7 +305,6 @@ static QString locatePlugin(const QString& fileName)
paths.append(fileName.left(slash)); // don't include the '/'
} else {
paths = QCoreApplication::libraryPaths();
- paths.prepend(QStringLiteral(".")); // search in current dir first
}
for (const QString &path : qAsConst(paths)) {
@@ -364,7 +363,7 @@ void QPluginLoader::setFileName(const QString &fileName)
if (d) {
lh = d->loadHints();
d->release();
- d = 0;
+ d = nullptr;
did_load = false;
}
diff --git a/src/corelib/serialization/qbinaryjsonvalue.cpp b/src/corelib/serialization/qbinaryjsonvalue.cpp
index 5e3a01ad38..7d0bc3d366 100644
--- a/src/corelib/serialization/qbinaryjsonvalue.cpp
+++ b/src/corelib/serialization/qbinaryjsonvalue.cpp
@@ -63,12 +63,9 @@ QBinaryJsonValue::QBinaryJsonValue(QBinaryJsonPrivate::MutableData *data,
case QJsonValue::Double:
dbl = v.toDouble(parent);
break;
- case QJsonValue::String: {
- QString s = v.toString(parent);
- stringData = s.data_ptr();
- stringData->ref.ref();
+ case QJsonValue::String:
+ stringData = v.toString(parent);
break;
- }
case QJsonValue::Array:
case QJsonValue::Object:
d = data;
@@ -80,9 +77,9 @@ QBinaryJsonValue::QBinaryJsonValue(QBinaryJsonPrivate::MutableData *data,
}
QBinaryJsonValue::QBinaryJsonValue(QString string)
- : stringData(*reinterpret_cast<QStringData **>(&string)), t(QJsonValue::String)
+ : d(nullptr), t(QJsonValue::String)
{
- stringData->ref.ref();
+ stringData = std::move(string);
}
QBinaryJsonValue::QBinaryJsonValue(const QBinaryJsonArray &a)
@@ -101,9 +98,6 @@ QBinaryJsonValue::QBinaryJsonValue(const QBinaryJsonObject &o)
QBinaryJsonValue::~QBinaryJsonValue()
{
- if (t == QJsonValue::String && stringData && !stringData->ref.deref())
- free(stringData);
-
if (d && !d->ref.deref())
delete d;
}
@@ -134,9 +128,7 @@ QString QBinaryJsonValue::toString() const
{
if (t != QJsonValue::String)
return QString();
- stringData->ref.ref(); // the constructor below doesn't add a ref.
- QStringDataPtr holder = { stringData };
- return QString(holder);
+ return stringData;
}
void QBinaryJsonValue::detach()
diff --git a/src/corelib/serialization/qbinaryjsonvalue_p.h b/src/corelib/serialization/qbinaryjsonvalue_p.h
index 498fc62ecd..c3b943250c 100644
--- a/src/corelib/serialization/qbinaryjsonvalue_p.h
+++ b/src/corelib/serialization/qbinaryjsonvalue_p.h
@@ -86,6 +86,7 @@ public:
QBinaryJsonValue(QBinaryJsonValue &&other) noexcept
: ui(other.ui),
+ stringData(std::move(other.stringData)),
d(other.d),
t(other.t)
{
@@ -96,6 +97,7 @@ public:
QBinaryJsonValue &operator =(QBinaryJsonValue &&other) noexcept
{
+ qSwap(stringData, other.stringData);
qSwap(ui, other.ui);
qSwap(d, other.d);
qSwap(t, other.t);
@@ -122,9 +124,9 @@ private:
quint64 ui;
bool b;
double dbl;
- QStringData *stringData;
const QBinaryJsonPrivate::Base *base;
};
+ QString stringData;
QBinaryJsonPrivate::MutableData *d = nullptr; // needed for Objects and Arrays
QJsonValue::Type t = QJsonValue::Null;
};
diff --git a/src/corelib/serialization/qcborarray.h b/src/corelib/serialization/qcborarray.h
index e06544f245..fe06b8630f 100644
--- a/src/corelib/serialization/qcborarray.h
+++ b/src/corelib/serialization/qcborarray.h
@@ -214,7 +214,7 @@ public:
bool contains(const QCborValue &value) const;
int compare(const QCborArray &other) const noexcept Q_DECL_PURE_FUNCTION;
-#if 0 && QT_HAS_INCLUDE(<compare>)
+#if 0 && __has_include(<compare>)
std::strong_ordering operator<=>(const QCborArray &other) const
{
int c = compare(other);
diff --git a/src/corelib/serialization/qcborcommon.cpp b/src/corelib/serialization/qcborcommon.cpp
new file mode 100644
index 0000000000..37fb198744
--- /dev/null
+++ b/src/corelib/serialization/qcborcommon.cpp
@@ -0,0 +1,328 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 Intel Corporation.
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#define CBOR_NO_ENCODER_API
+#define CBOR_NO_PARSER_API
+#include "qcborcommon_p.h"
+
+#include <QtCore/qdatastream.h>
+
+QT_BEGIN_NAMESPACE
+
+#include <cborerrorstrings.c>
+
+/*!
+ \headerfile <QtCborCommon>
+
+ \brief The <QtCborCommon> header contains definitions common to both the
+ streaming classes (QCborStreamReader and QCborStreamWriter) and to
+ QCborValue.
+ */
+
+/*!
+ \enum QCborSimpleType
+ \relates <QtCborCommon>
+
+ This enum contains the possible "Simple Types" for CBOR. Simple Types range
+ from 0 to 255 and are types that carry no further value.
+
+ The following values are currently known:
+
+ \value False A "false" boolean.
+ \value True A "true" boolean.
+ \value Null Absence of value (null).
+ \value Undefined Missing or deleted value, usually an error.
+
+ Qt CBOR API supports encoding and decoding any Simple Type, whether one of
+ those above or any other value.
+
+ Applications should only use further values if a corresponding specification
+ has been published, otherwise interpretation and validation by the remote
+ may fail. Values 24 to 31 are reserved and must not be used.
+
+ The current authoritative list is maintained by IANA in the
+ \l{https://www.iana.org/assignments/cbor-simple-values/cbor-simple-values.xml}{Simple
+ Values registry}.
+
+ \sa QCborStreamWriter::append(QCborSimpleType), QCborStreamReader::isSimpleType(),
+ QCborStreamReader::toSimpleType(), QCborValue::isSimpleType(), QCborValue::toSimpleType()
+ */
+
+#if !defined(QT_NO_DATASTREAM)
+QDataStream &operator<<(QDataStream &ds, QCborSimpleType st)
+{
+ return ds << quint8(st);
+}
+
+QDataStream &operator>>(QDataStream &ds, QCborSimpleType &st)
+{
+ quint8 v;
+ ds >> v;
+ st = QCborSimpleType(v);
+ return ds;
+}
+#endif
+
+/*!
+ \enum QCborTag
+ \relates <QtCborCommon>
+
+ This enum contains no enumeration and is used only to provide type-safe
+ access to a CBOR tag.
+
+ CBOR tags are 64-bit numbers that are attached to generic CBOR types to
+ provide further semantic meaning. QCborTag may be constructed from an
+ enumeration found in QCborKnownTags or directly by providing the numeric
+ representation.
+
+ For example, the following creates a QCborValue containing a byte array
+ tagged with a tag 2.
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 0
+
+ \sa QCborKnownTags, QCborStreamWriter::append(QCborTag),
+ QCborStreamReader::isTag(), QCborStreamReader::toTag(),
+ QCborValue::isTag(), QCborValue::tag()
+ */
+
+/*!
+ \enum QCborKnownTags
+ \relates <QtCborCommon>
+
+ This enum contains a list of CBOR tags, known at the time of the Qt
+ implementation. This list is not meant to be complete and contains only
+ tags that are either backed by an RFC or specifically used by the Qt
+ implementation.
+
+ The authoritative list is maintained by IANA in the
+ \l{https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml}{CBOR tag
+ registry}.
+
+ \value DateTimeString A date and time string, formatted according to RFC 3339, as refined
+ by RFC 4287. It is the same format as Qt::ISODate and
+ Qt::ISODateWithMs.
+ \value UnixTime_t A numerical representation of seconds elapsed since
+ 1970-01-01T00:00Z.
+ \value PositiveBignum A positive number of arbitrary length, encoded as a byte array in
+ network byte order. For example, the number 2\sup{64} is represented by
+ a byte array containing the byte value 0x01 followed by 8 zero bytes.
+ \value NegativeBignum A negative number of arbirary length, encoded as the absolute value
+ of that number, minus one. For example, a byte array containing
+ byte value 0x02 followed by 8 zero bytes represents the number
+ -2\sup{65} - 1.
+ \value Decimal A decimal fraction, encoded as an array of two integers: the first
+ is the exponent of the power of 10, the second the integral
+ mantissa. The value 273.15 would be encoded as array \c{[-2, 27315]}.
+ \value Bigfloat Similar to Decimal, but the exponent is a power of 2 instead.
+ \value COSE_Encrypt0 An \c Encrypt0 map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
+ (CBOR Object Signing and Encryption).
+ \value COSE_Mac0 A \c Mac0 map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
+ (CBOR Object Signing and Encryption).
+ \value COSE_Sign1 A \c Sign1 map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
+ (CBOR Object Signing and Encryption).
+ \value ExpectedBase64url Indicates that the byte array should be encoded using Base64url
+ if the stream is converted to JSON.
+ \value ExpectedBase64 Indicates that the byte array should be encoded using Base64
+ if the stream is converted to JSON.
+ \value ExpectedBase16 Indicates that the byte array should be encoded using Base16 (hex)
+ if the stream is converted to JSON.
+ \value EncodedCbor Indicates that the byte array contains a CBOR stream.
+ \value Url Indicates that the string contains a URL.
+ \value Base64url Indicates that the string contains data encoded using Base64url.
+ \value Base64 Indicates that the string contains data encoded using Base64.
+ \value RegularExpression Indicates that the string contains a Perl-Compatible Regular
+ Expression pattern.
+ \value MimeMessage Indicates that the string contains a MIME message (according to
+ \l{https://tools.ietf.org/html/rfc2045}){RFC 2045}.
+ \value Uuid Indicates that the byte array contains a UUID.
+ \value COSE_Encrypt An \c Encrypt map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
+ (CBOR Object Signing and Encryption).
+ \value COSE_Mac A \c Mac map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
+ (CBOR Object Signing and Encryption).
+ \value COSE_Sign A \c Sign map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
+ (CBOR Object Signing and Encryption).
+ \value Signature No change in interpretation; this tag can be used as the outermost
+ tag in a CBOR stream as the file header.
+
+ The following tags are interpreted by QCborValue during decoding and will
+ produce objects with extended Qt types, and it will use those tags when
+ encoding the same extended types.
+
+ \value DateTimeString \l QDateTime
+ \value UnixTime_t \l QDateTime (only in decoding)
+ \value Url \l QUrl
+ \value Uuid \l QUuid
+
+ Additionally, if a QCborValue containing a QByteArray is tagged using one of
+ \c ExpectedBase64url, \c ExpectedBase64 or \c ExpectedBase16, QCborValue
+ will use the expected encoding when converting to JSON (see
+ QCborValue::toJsonValue).
+
+ \sa QCborTag, QCborStreamWriter::append(QCborTag),
+ QCborStreamReader::isTag(), QCborStreamReader::toTag(),
+ QCborValue::isTag(), QCborValue::tag()
+ */
+
+/*!
+ \class QCborError
+ \inmodule QtCore
+ \relates <QtCborCommon>
+ \reentrant
+ \since 5.12
+
+ \brief The QCborError class holds the error condition found while parsing or
+ validating a CBOR stream.
+
+ \sa QCborStreamReader, QCborValue, QCborParserError
+ */
+
+/*!
+ \enum QCborError::Code
+
+ This enum contains the possible error condition codes.
+
+ \value NoError No error was detected.
+ \value UnknownError An unknown error occurred and no further details are available.
+ \value AdvancePastEnd QCborStreamReader::next() was called but there are no more elements in
+ the current context.
+ \value InputOutputError An I/O error with the QIODevice occurred.
+ \value GarbageAtEnd Data was found in the input stream after the last element.
+ \value EndOfFile The end of the input stream was unexpectedly reached while processing an
+ element.
+ \value UnexpectedBreak The CBOR stream contains a Break where it is not allowed (data is
+ corrupt and the error is not recoverable).
+ \value UnknownType The CBOR stream contains an unknown/unparseable Type (data is corrupt
+ and the and the error is not recoverable).
+ \value IllegalType The CBOR stream contains a known type in a position it is not allowed
+ to exist (data is corrupt and the error is not recoverable).
+ \value IllegalNumber The CBOR stream appears to be encoding a number larger than 64-bit
+ (data is corrupt and the error is not recoverable).
+ \value IllegalSimpleType The CBOR stream contains a Simple Type encoded incorrectly (data is
+ corrupt and the error is not recoverable).
+ \value InvalidUtf8String The CBOR stream contains a text string that does not decode properly
+ as UTF-8 (data is corrupt and the error is not recoverable).
+ \value DataTooLarge CBOR string, map or array is too big and cannot be parsed by Qt
+ (internal limitation, but the error is not recoverable).
+ \value NestingTooDeep Too many levels of arrays or maps encountered while processing the
+ input (internal limitation, but the error is not recoverable).
+ \value UnsupportedType The CBOR stream contains a known type that the implementation does not
+ support (internal limitation, but the error is not recoverable).
+ */
+
+/*!
+ \variable QCborError::c
+ \internal
+ */
+
+/*!
+ \fn QCborError::operator Code() const
+
+ Returns the error code that this QCborError object stores.
+ */
+
+/*!
+ Returns a text string that matches the error code in this QCborError object.
+
+ Note: the string is not translated. Applications whose interface allow users
+ to parse CBOR streams need to provide their own, translated strings.
+
+ \sa QCborError::Code
+ */
+QString QCborError::toString() const
+{
+ switch (c) {
+ case NoError:
+ Q_STATIC_ASSERT(int(NoError) == int(CborNoError));
+ return QString();
+
+ case UnknownError:
+ Q_STATIC_ASSERT(int(UnknownError) == int(CborUnknownError));
+ return QStringLiteral("Unknown error");
+ case AdvancePastEnd:
+ Q_STATIC_ASSERT(int(AdvancePastEnd) == int(CborErrorAdvancePastEOF));
+ return QStringLiteral("Read past end of buffer (more bytes needed)");
+ case InputOutputError:
+ Q_STATIC_ASSERT(int(InputOutputError) == int(CborErrorIO));
+ return QStringLiteral("Input/Output error");
+ case GarbageAtEnd:
+ Q_STATIC_ASSERT(int(GarbageAtEnd) == int(CborErrorGarbageAtEnd));
+ return QStringLiteral("Data found after the end of the stream");
+ case EndOfFile:
+ Q_STATIC_ASSERT(int(EndOfFile) == int(CborErrorUnexpectedEOF));
+ return QStringLiteral("Unexpected end of input data (more bytes needed)");
+ case UnexpectedBreak:
+ Q_STATIC_ASSERT(int(UnexpectedBreak) == int(CborErrorUnexpectedBreak));
+ return QStringLiteral("Invalid CBOR stream: unexpected 'break' byte");
+ case UnknownType:
+ Q_STATIC_ASSERT(int(UnknownType) == int(CborErrorUnknownType));
+ return QStringLiteral("Invalid CBOR stream: unknown type");
+ case IllegalType:
+ Q_STATIC_ASSERT(int(IllegalType) == int(CborErrorIllegalType));
+ return QStringLiteral("Invalid CBOR stream: illegal type found");
+ case IllegalNumber:
+ Q_STATIC_ASSERT(int(IllegalNumber) == int(CborErrorIllegalNumber));
+ return QStringLiteral("Invalid CBOR stream: illegal number encoding (future extension)");
+ case IllegalSimpleType:
+ Q_STATIC_ASSERT(int(IllegalSimpleType) == int(CborErrorIllegalSimpleType));
+ return QStringLiteral("Invalid CBOR stream: illegal simple type");
+ case InvalidUtf8String:
+ Q_STATIC_ASSERT(int(InvalidUtf8String) == int(CborErrorInvalidUtf8TextString));
+ return QStringLiteral("Invalid CBOR stream: invalid UTF-8 text string");
+ case DataTooLarge:
+ Q_STATIC_ASSERT(int(DataTooLarge) == int(CborErrorDataTooLarge));
+ return QStringLiteral("Internal limitation: data set too large");
+ case NestingTooDeep:
+ Q_STATIC_ASSERT(int(NestingTooDeep) == int(CborErrorNestingTooDeep));
+ return QStringLiteral("Internal limitation: data nesting too deep");
+ case UnsupportedType:
+ Q_STATIC_ASSERT(int(UnsupportedType) == int(CborErrorUnsupportedType));
+ return QStringLiteral("Internal limitation: unsupported type");
+ }
+
+ // get the error string from TinyCBOR
+ CborError err = CborError(int(c));
+ return QString::fromLatin1(cbor_error_string(err));
+}
+
+QT_END_NAMESPACE
+
+#ifndef QT_BOOTSTRAPPED
+#include "moc_qcborcommon.cpp"
+#endif
diff --git a/src/corelib/serialization/qcborcommon.h b/src/corelib/serialization/qcborcommon.h
index 3dfe50cd09..bec46399ce 100644
--- a/src/corelib/serialization/qcborcommon.h
+++ b/src/corelib/serialization/qcborcommon.h
@@ -148,6 +148,8 @@ inline uint qHash(QCborTag tag, uint seed = 0)
return qHash(quint64(tag), seed);
}
+enum class QCborNegativeInteger : quint64 {};
+
QT_END_NAMESPACE
Q_DECLARE_METATYPE(QCborTag)
diff --git a/src/corelib/serialization/qcborcommon_p.h b/src/corelib/serialization/qcborcommon_p.h
new file mode 100644
index 0000000000..9b7f4b7099
--- /dev/null
+++ b/src/corelib/serialization/qcborcommon_p.h
@@ -0,0 +1,84 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 Intel Corporation.
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCBORCOMMON_P_H
+#define QCBORCOMMON_P_H
+
+#include "qcborcommon.h"
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+QT_BEGIN_NAMESPACE
+
+#ifdef QT_NO_DEBUG
+# define NDEBUG 1
+#endif
+#undef assert
+#define assert Q_ASSERT
+
+QT_WARNING_PUSH
+QT_WARNING_DISABLE_GCC("-Wunused-function")
+QT_WARNING_DISABLE_CLANG("-Wunused-function")
+QT_WARNING_DISABLE_CLANG("-Wundefined-internal")
+
+#define CBOR_NO_VALIDATION_API 1
+#define CBOR_NO_PRETTY_API 1
+#define CBOR_API static inline
+#define CBOR_PRIVATE_API static inline
+#define CBOR_INLINE_API static inline
+
+#include <cbor.h>
+
+QT_WARNING_POP
+
+Q_DECLARE_TYPEINFO(CborValue, Q_PRIMITIVE_TYPE);
+
+QT_END_NAMESPACE
+
+#endif // QCBORCOMMON_P_H
diff --git a/src/corelib/serialization/qcbormap.h b/src/corelib/serialization/qcbormap.h
index 4aea901eef..6636ce776a 100644
--- a/src/corelib/serialization/qcbormap.h
+++ b/src/corelib/serialization/qcbormap.h
@@ -245,7 +245,7 @@ public:
{ const_iterator it = find(key); return it != end(); }
int compare(const QCborMap &other) const noexcept Q_DECL_PURE_FUNCTION;
-#if 0 && QT_HAS_INCLUDE(<compare>)
+#if 0 && __has_include(<compare>)
std::strong_ordering operator<=>(const QCborMap &other) const
{
int c = compare(other);
diff --git a/src/corelib/serialization/qcborstream.h b/src/corelib/serialization/qcborstream.h
index 08bf680cca..f2b88820cd 100644
--- a/src/corelib/serialization/qcborstream.h
+++ b/src/corelib/serialization/qcborstream.h
@@ -1,5 +1,6 @@
/****************************************************************************
**
+** Copyright (C) 2019 The Qt Company Ltd.
** Copyright (C) 2018 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
@@ -40,230 +41,17 @@
#ifndef QCBORSTREAM_H
#define QCBORSTREAM_H
-#include <QtCore/qbytearray.h>
-#include <QtCore/qcborcommon.h>
-#include <QtCore/qfloat16.h>
-#include <QtCore/qscopedpointer.h>
-#include <QtCore/qstring.h>
-#include <QtCore/qstringview.h>
+#include <QtCore/qglobal.h>
-QT_REQUIRE_CONFIG(cborstream);
-
-// See qcborcommon.h for why we check
-#if defined(QT_X11_DEFINES_FOUND)
-# undef True
-# undef False
+#if QT_CONFIG(cborstreamreader)
+#include <QtCore/qcborstreamreader.h>
#endif
-QT_BEGIN_NAMESPACE
-
-class QIODevice;
-
-enum class QCborNegativeInteger : quint64 {};
-
-class QCborStreamWriterPrivate;
-class Q_CORE_EXPORT QCborStreamWriter
-{
-public:
- explicit QCborStreamWriter(QIODevice *device);
- explicit QCborStreamWriter(QByteArray *data);
- ~QCborStreamWriter();
- Q_DISABLE_COPY(QCborStreamWriter)
-
- void setDevice(QIODevice *device);
- QIODevice *device() const;
-
- void append(quint64 u);
- void append(qint64 i);
- void append(QCborNegativeInteger n);
- void append(const QByteArray &ba) { appendByteString(ba.constData(), ba.size()); }
- void append(QLatin1String str);
- void append(QStringView str);
- void append(QCborTag tag);
- void append(QCborKnownTags tag) { append(QCborTag(tag)); }
- void append(QCborSimpleType st);
- void append(std::nullptr_t) { append(QCborSimpleType::Null); }
- void append(qfloat16 f);
- void append(float f);
- void append(double d);
-
- void appendByteString(const char *data, qsizetype len);
- void appendTextString(const char *utf8, qsizetype len);
-
- // convenience
- void append(bool b) { append(b ? QCborSimpleType::True : QCborSimpleType::False); }
- void appendNull() { append(QCborSimpleType::Null); }
- void appendUndefined() { append(QCborSimpleType::Undefined); }
-
-#ifndef Q_QDOC
- // overloads to make normal code not complain
- void append(int i) { append(qint64(i)); }
- void append(uint u) { append(quint64(u)); }
-#endif
-#ifndef QT_NO_CAST_FROM_ASCII
- void append(const char *str, qsizetype size = -1)
- { appendTextString(str, (str && size == -1) ? int(strlen(str)) : size); }
+#if QT_CONFIG(cborstreamwriter)
+#include <QtCore/qcborstreamwriter.h>
#endif
- void startArray();
- void startArray(quint64 count);
- bool endArray();
- void startMap();
- void startMap(quint64 count);
- bool endMap();
-
- // no API for encoding chunked strings
-
-private:
- QScopedPointer<QCborStreamWriterPrivate> d;
-};
-
-class QCborStreamReaderPrivate;
-class Q_CORE_EXPORT QCborStreamReader
-{
- Q_GADGET
-public:
- enum Type : quint8 {
- UnsignedInteger = 0x00,
- NegativeInteger = 0x20,
- ByteString = 0x40,
- ByteArray = ByteString,
- TextString = 0x60,
- String = TextString,
- Array = 0x80,
- Map = 0xa0,
- Tag = 0xc0,
- SimpleType = 0xe0,
- HalfFloat = 0xf9,
- Float16 = HalfFloat,
- Float = 0xfa,
- Double = 0xfb,
-
- Invalid = 0xff
- };
- Q_ENUM(Type)
-
- enum StringResultCode {
- EndOfString = 0,
- Ok = 1,
- Error = -1
- };
- template <typename Container> struct StringResult {
- Container data;
- StringResultCode status = Error;
- };
- Q_ENUM(StringResultCode)
-
- QCborStreamReader();
- QCborStreamReader(const char *data, qsizetype len);
- QCborStreamReader(const quint8 *data, qsizetype len);
- explicit QCborStreamReader(const QByteArray &data);
- explicit QCborStreamReader(QIODevice *device);
- ~QCborStreamReader();
- Q_DISABLE_COPY(QCborStreamReader)
-
- void setDevice(QIODevice *device);
- QIODevice *device() const;
- void addData(const QByteArray &data);
- void addData(const char *data, qsizetype len);
- void addData(const quint8 *data, qsizetype len)
- { addData(reinterpret_cast<const char *>(data), len); }
- void reparse();
- void clear();
- void reset();
-
- QCborError lastError();
-
- qint64 currentOffset() const;
-
- bool isValid() const { return !isInvalid(); }
-
- int containerDepth() const;
- QCborStreamReader::Type parentContainerType() const;
- bool hasNext() const noexcept Q_DECL_PURE_FUNCTION;
- bool next(int maxRecursion = 10000);
-
- Type type() const { return QCborStreamReader::Type(type_); }
- bool isUnsignedInteger() const { return type() == UnsignedInteger; }
- bool isNegativeInteger() const { return type() == NegativeInteger; }
- bool isInteger() const { return quint8(type()) <= quint8(NegativeInteger); }
- bool isByteArray() const { return type() == ByteArray; }
- bool isString() const { return type() == String; }
- bool isArray() const { return type() == Array; }
- bool isMap() const { return type() == Map; }
- bool isTag() const { return type() == Tag; }
- bool isSimpleType() const { return type() == SimpleType; }
- bool isFloat16() const { return type() == Float16; }
- bool isFloat() const { return type() == Float; }
- bool isDouble() const { return type() == Double; }
- bool isInvalid() const { return type() == Invalid; }
-
- bool isSimpleType(QCborSimpleType st) const { return isSimpleType() && toSimpleType() == st; }
- bool isFalse() const { return isSimpleType(QCborSimpleType::False); }
- bool isTrue() const { return isSimpleType(QCborSimpleType::True); }
- bool isBool() const { return isFalse() || isTrue(); }
- bool isNull() const { return isSimpleType(QCborSimpleType::Null); }
- bool isUndefined() const { return isSimpleType(QCborSimpleType::Undefined); }
-
- bool isLengthKnown() const noexcept Q_DECL_PURE_FUNCTION;
- quint64 length() const;
-
- bool isContainer() const { return isMap() || isArray(); }
- bool enterContainer() { Q_ASSERT(isContainer()); return _enterContainer_helper(); }
- bool leaveContainer();
-
- StringResult<QString> readString() { Q_ASSERT(isString()); return _readString_helper(); }
- StringResult<QByteArray> readByteArray(){ Q_ASSERT(isByteArray()); return _readByteArray_helper(); }
- qsizetype currentStringChunkSize() const{ Q_ASSERT(isString() || isByteArray()); return _currentStringChunkSize(); }
- StringResult<qsizetype> readStringChunk(char *ptr, qsizetype maxlen);
-
- bool toBool() const { Q_ASSERT(isBool()); return value64 - int(QCborSimpleType::False); }
- QCborTag toTag() const { Q_ASSERT(isTag()); return QCborTag(value64); }
- quint64 toUnsignedInteger() const { Q_ASSERT(isUnsignedInteger()); return value64; }
- QCborNegativeInteger toNegativeInteger() const { Q_ASSERT(isNegativeInteger()); return QCborNegativeInteger(value64 + 1); }
- QCborSimpleType toSimpleType() const{ Q_ASSERT(isSimpleType()); return QCborSimpleType(value64); }
- qfloat16 toFloat16() const { Q_ASSERT(isFloat16()); return _toFloatingPoint<qfloat16>(); }
- float toFloat() const { Q_ASSERT(isFloat()); return _toFloatingPoint<float>(); }
- double toDouble() const { Q_ASSERT(isDouble()); return _toFloatingPoint<double>(); }
-
- qint64 toInteger() const
- {
- Q_ASSERT(isInteger());
- qint64 v = qint64(value64);
- if (isNegativeInteger())
- return -v - 1;
- return v;
- }
-
-private:
- void preparse();
- bool _enterContainer_helper();
- StringResult<QString> _readString_helper();
- StringResult<QByteArray> _readByteArray_helper();
- qsizetype _currentStringChunkSize() const;
-
- template <typename FP> FP _toFloatingPoint() const noexcept
- {
- using UIntFP = typename QIntegerForSizeof<FP>::Unsigned;
- UIntFP u = UIntFP(value64);
- FP f;
- memcpy(static_cast<void *>(&f), &u, sizeof(f));
- return f;
- }
-
- friend QCborStreamReaderPrivate;
- friend class QCborContainerPrivate;
- quint64 value64;
- QScopedPointer<QCborStreamReaderPrivate> d;
- quint8 type_;
- quint8 reserved[3] = {};
-};
-
+QT_BEGIN_NAMESPACE
QT_END_NAMESPACE
-#if defined(QT_X11_DEFINES_FOUND)
-# define True 1
-# define False 0
-#endif
-
#endif // QCBORSTREAM_H
diff --git a/src/corelib/serialization/qcborstream.cpp b/src/corelib/serialization/qcborstreamreader.cpp
index a232f7eef7..c983436606 100644
--- a/src/corelib/serialization/qcborstream.cpp
+++ b/src/corelib/serialization/qcborstreamreader.cpp
@@ -37,84 +37,46 @@
**
****************************************************************************/
-#include "qcborstream.h"
+#include "qcborstreamreader.h"
+
+#define CBOR_NO_ENCODER_API
+#include <private/qcborcommon_p.h>
#include <private/qnumeric_p.h>
#include <private/qutfcodec_p.h>
-#include <qbuffer.h>
#include <qdebug.h>
#include <qstack.h>
-#include <qdatastream.h>
QT_BEGIN_NAMESPACE
-#ifdef QT_NO_DEBUG
-# define NDEBUG 1
-#endif
-#undef assert
-#define assert Q_ASSERT
-
-QT_WARNING_PUSH
-QT_WARNING_DISABLE_GCC("-Wunused-function")
-QT_WARNING_DISABLE_CLANG("-Wunused-function")
-QT_WARNING_DISABLE_CLANG("-Wundefined-internal")
-QT_WARNING_DISABLE_MSVC(4334) // '<<': result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
-
-#define CBOR_ENCODER_NO_CHECK_USER
-
-#define CBOR_NO_VALIDATION_API 1
-#define CBOR_NO_PRETTY_API 1
-#define CBOR_API static inline
-#define CBOR_PRIVATE_API static inline
-#define CBOR_INLINE_API static inline
-
-#include <cbor.h>
-
-static CborError qt_cbor_encoder_write_callback(void *token, const void *data, size_t len, CborEncoderAppendType);
static bool qt_cbor_decoder_can_read(void *token, size_t len);
static void qt_cbor_decoder_advance(void *token, size_t len);
static void *qt_cbor_decoder_read(void *token, void *userptr, size_t offset, size_t len);
static CborError qt_cbor_decoder_transfer_string(void *token, const void **userptr, size_t offset, size_t len);
-#define CBOR_ENCODER_WRITER_CONTROL 1
-#define CBOR_ENCODER_WRITE_FUNCTION qt_cbor_encoder_write_callback
-
#define CBOR_PARSER_READER_CONTROL 1
#define CBOR_PARSER_CAN_READ_BYTES_FUNCTION qt_cbor_decoder_can_read
#define CBOR_PARSER_ADVANCE_BYTES_FUNCTION qt_cbor_decoder_advance
#define CBOR_PARSER_TRANSFER_STRING_FUNCTION qt_cbor_decoder_transfer_string
#define CBOR_PARSER_READ_BYTES_FUNCTION qt_cbor_decoder_read
-#include "../3rdparty/tinycbor/src/cborencoder.c"
-#include "../3rdparty/tinycbor/src/cborerrorstrings.c"
-#include "../3rdparty/tinycbor/src/cborparser.c"
+QT_WARNING_PUSH
+QT_WARNING_DISABLE_MSVC(4334) // '<<': result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
+
+#include <cborparser.c>
+
+QT_WARNING_POP
-// silence compilers that complain about this being a static function declared
-// but never defined
-static CborError cbor_encoder_close_container_checked(CborEncoder*, const CborEncoder*)
-{
- Q_UNREACHABLE();
- return CborErrorInternalError;
-}
static CborError _cbor_value_dup_string(const CborValue *, void **, size_t *, CborValue *)
{
Q_UNREACHABLE();
return CborErrorInternalError;
}
-static CborError cbor_value_get_half_float_as_float(const CborValue *, float *)
+static CborError Q_DECL_UNUSED cbor_value_get_half_float_as_float(const CborValue *, float *)
{
Q_UNREACHABLE();
return CborErrorInternalError;
}
-static CborError cbor_encode_float_as_half_float(CborEncoder *, float)
-{
- Q_UNREACHABLE();
- return CborErrorInternalError;
-}
-QT_WARNING_POP
-
-Q_DECLARE_TYPEINFO(CborEncoder, Q_PRIMITIVE_TYPE);
-Q_DECLARE_TYPEINFO(CborValue, Q_PRIMITIVE_TYPE);
// confirm our constants match TinyCBOR's
Q_STATIC_ASSERT(int(QCborStreamReader::UnsignedInteger) == CborIntegerType);
@@ -130,1083 +92,6 @@ Q_STATIC_ASSERT(int(QCborStreamReader::Double) == CborDoubleType);
Q_STATIC_ASSERT(int(QCborStreamReader::Invalid) == CborInvalidType);
/*!
- \headerfile <QtCborCommon>
-
- \brief The <QtCborCommon> header contains definitions common to both the
- streaming classes (QCborStreamReader and QCborStreamWriter) and to
- QCborValue.
- */
-
-/*!
- \enum QCborSimpleType
- \relates <QtCborCommon>
-
- This enum contains the possible "Simple Types" for CBOR. Simple Types range
- from 0 to 255 and are types that carry no further value.
-
- The following values are currently known:
-
- \value False A "false" boolean.
- \value True A "true" boolean.
- \value Null Absence of value (null).
- \value Undefined Missing or deleted value, usually an error.
-
- Qt CBOR API supports encoding and decoding any Simple Type, whether one of
- those above or any other value.
-
- Applications should only use further values if a corresponding specification
- has been published, otherwise interpretation and validation by the remote
- may fail. Values 24 to 31 are reserved and must not be used.
-
- The current authoritative list is maintained by IANA in the
- \l{https://www.iana.org/assignments/cbor-simple-values/cbor-simple-values.xml}{Simple
- Values registry}.
-
- \sa QCborStreamWriter::append(QCborSimpleType), QCborStreamReader::isSimpleType(),
- QCborStreamReader::toSimpleType(), QCborValue::isSimpleType(), QCborValue::toSimpleType()
- */
-
-#if !defined(QT_NO_DATASTREAM)
-QDataStream &operator<<(QDataStream &ds, QCborSimpleType st)
-{
- return ds << quint8(st);
-}
-
-QDataStream &operator>>(QDataStream &ds, QCborSimpleType &st)
-{
- quint8 v;
- ds >> v;
- st = QCborSimpleType(v);
- return ds;
-}
-#endif
-
-/*!
- \enum QCborTag
- \relates <QtCborCommon>
-
- This enum contains no enumeration and is used only to provide type-safe
- access to a CBOR tag.
-
- CBOR tags are 64-bit numbers that are attached to generic CBOR types to
- provide further semantic meaning. QCborTag may be constructed from an
- enumeration found in QCborKnownTags or directly by providing the numeric
- representation.
-
- For example, the following creates a QCborValue containing a byte array
- tagged with a tag 2.
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 0
-
- \sa QCborKnownTags, QCborStreamWriter::append(QCborTag),
- QCborStreamReader::isTag(), QCborStreamReader::toTag(),
- QCborValue::isTag(), QCborValue::tag()
- */
-
-/*!
- \enum QCborKnownTags
- \relates <QtCborCommon>
-
- This enum contains a list of CBOR tags, known at the time of the Qt
- implementation. This list is not meant to be complete and contains only
- tags that are either backed by an RFC or specifically used by the Qt
- implementation.
-
- The authoritative list is maintained by IANA in the
- \l{https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml}{CBOR tag
- registry}.
-
- \value DateTimeString A date and time string, formatted according to RFC 3339, as refined
- by RFC 4287. It is the same format as Qt::ISODate and
- Qt::ISODateWithMs.
- \value UnixTime_t A numerical representation of seconds elapsed since
- 1970-01-01T00:00Z.
- \value PositiveBignum A positive number of arbitrary length, encoded as a byte array in
- network byte order. For example, the number 2\sup{64} is represented by
- a byte array containing the byte value 0x01 followed by 8 zero bytes.
- \value NegativeBignum A negative number of arbirary length, encoded as the absolute value
- of that number, minus one. For example, a byte array containing
- byte value 0x02 followed by 8 zero bytes represents the number
- -2\sup{65} - 1.
- \value Decimal A decimal fraction, encoded as an array of two integers: the first
- is the exponent of the power of 10, the second the integral
- mantissa. The value 273.15 would be encoded as array \c{[-2, 27315]}.
- \value Bigfloat Similar to Decimal, but the exponent is a power of 2 instead.
- \value COSE_Encrypt0 An \c Encrypt0 map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
- (CBOR Object Signing and Encryption).
- \value COSE_Mac0 A \c Mac0 map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
- (CBOR Object Signing and Encryption).
- \value COSE_Sign1 A \c Sign1 map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
- (CBOR Object Signing and Encryption).
- \value ExpectedBase64url Indicates that the byte array should be encoded using Base64url
- if the stream is converted to JSON.
- \value ExpectedBase64 Indicates that the byte array should be encoded using Base64
- if the stream is converted to JSON.
- \value ExpectedBase16 Indicates that the byte array should be encoded using Base16 (hex)
- if the stream is converted to JSON.
- \value EncodedCbor Indicates that the byte array contains a CBOR stream.
- \value Url Indicates that the string contains a URL.
- \value Base64url Indicates that the string contains data encoded using Base64url.
- \value Base64 Indicates that the string contains data encoded using Base64.
- \value RegularExpression Indicates that the string contains a Perl-Compatible Regular
- Expression pattern.
- \value MimeMessage Indicates that the string contains a MIME message (according to
- \l{https://tools.ietf.org/html/rfc2045}){RFC 2045}.
- \value Uuid Indicates that the byte array contains a UUID.
- \value COSE_Encrypt An \c Encrypt map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
- (CBOR Object Signing and Encryption).
- \value COSE_Mac A \c Mac map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
- (CBOR Object Signing and Encryption).
- \value COSE_Sign A \c Sign map as specified by \l{https://tools.ietf.org/html/rfc8152}{RFC 8152}
- (CBOR Object Signing and Encryption).
- \value Signature No change in interpretation; this tag can be used as the outermost
- tag in a CBOR stream as the file header.
-
- The following tags are interpreted by QCborValue during decoding and will
- produce objects with extended Qt types, and it will use those tags when
- encoding the same extended types.
-
- \value DateTimeString \l QDateTime
- \value UnixTime_t \l QDateTime (only in decoding)
- \value Url \l QUrl
- \value Uuid \l QUuid
-
- Additionally, if a QCborValue containing a QByteArray is tagged using one of
- \c ExpectedBase64url, \c ExpectedBase64 or \c ExpectedBase16, QCborValue
- will use the expected encoding when converting to JSON (see
- QCborValue::toJsonValue).
-
- \sa QCborTag, QCborStreamWriter::append(QCborTag),
- QCborStreamReader::isTag(), QCborStreamReader::toTag(),
- QCborValue::isTag(), QCborValue::tag()
- */
-
-/*!
- \class QCborError
- \inmodule QtCore
- \relates <QtCborCommon>
- \reentrant
- \since 5.12
-
- \brief The QCborError class holds the error condition found while parsing or
- validating a CBOR stream.
-
- \sa QCborStreamReader, QCborValue, QCborParserError
- */
-
-/*!
- \enum QCborError::Code
-
- This enum contains the possible error condition codes.
-
- \value NoError No error was detected.
- \value UnknownError An unknown error occurred and no further details are available.
- \value AdvancePastEnd QCborStreamReader::next() was called but there are no more elements in
- the current context.
- \value InputOutputError An I/O error with the QIODevice occurred.
- \value GarbageAtEnd Data was found in the input stream after the last element.
- \value EndOfFile The end of the input stream was unexpectedly reached while processing an
- element.
- \value UnexpectedBreak The CBOR stream contains a Break where it is not allowed (data is
- corrupt and the error is not recoverable).
- \value UnknownType The CBOR stream contains an unknown/unparseable Type (data is corrupt
- and the and the error is not recoverable).
- \value IllegalType The CBOR stream contains a known type in a position it is not allowed
- to exist (data is corrupt and the error is not recoverable).
- \value IllegalNumber The CBOR stream appears to be encoding a number larger than 64-bit
- (data is corrupt and the error is not recoverable).
- \value IllegalSimpleType The CBOR stream contains a Simple Type encoded incorrectly (data is
- corrupt and the error is not recoverable).
- \value InvalidUtf8String The CBOR stream contains a text string that does not decode properly
- as UTF-8 (data is corrupt and the error is not recoverable).
- \value DataTooLarge CBOR string, map or array is too big and cannot be parsed by Qt
- (internal limitation, but the error is not recoverable).
- \value NestingTooDeep Too many levels of arrays or maps encountered while processing the
- input (internal limitation, but the error is not recoverable).
- \value UnsupportedType The CBOR stream contains a known type that the implementation does not
- support (internal limitation, but the error is not recoverable).
- */
-
-// Convert from CborError to QCborError.
-//
-// Centralized in a function in case we need to make more adjustments in the
-// future.
-static QCborError fromCborError(CborError err)
-{
- return { QCborError::Code(int(err)) };
-}
-
-// Convert to CborError from QCborError.
-//
-// Centralized in a function in case we need to make more adjustments in the
-// future.
-static CborError toCborError(QCborError c)
-{
- return CborError(int(c.c));
-}
-
-/*!
- \variable QCborError::c
- \internal
- */
-
-/*!
- \fn QCborError::operator Code() const
-
- Returns the error code that this QCborError object stores.
- */
-
-/*!
- Returns a text string that matches the error code in this QCborError object.
-
- Note: the string is not translated. Applications whose interface allow users
- to parse CBOR streams need to provide their own, translated strings.
-
- \sa QCborError::Code
- */
-QString QCborError::toString() const
-{
- switch (c) {
- case NoError:
- Q_STATIC_ASSERT(int(NoError) == int(CborNoError));
- return QString();
-
- case UnknownError:
- Q_STATIC_ASSERT(int(UnknownError) == int(CborUnknownError));
- return QStringLiteral("Unknown error");
- case AdvancePastEnd:
- Q_STATIC_ASSERT(int(AdvancePastEnd) == int(CborErrorAdvancePastEOF));
- return QStringLiteral("Read past end of buffer (more bytes needed)");
- case InputOutputError:
- Q_STATIC_ASSERT(int(InputOutputError) == int(CborErrorIO));
- return QStringLiteral("Input/Output error");
- case GarbageAtEnd:
- Q_STATIC_ASSERT(int(GarbageAtEnd) == int(CborErrorGarbageAtEnd));
- return QStringLiteral("Data found after the end of the stream");
- case EndOfFile:
- Q_STATIC_ASSERT(int(EndOfFile) == int(CborErrorUnexpectedEOF));
- return QStringLiteral("Unexpected end of input data (more bytes needed)");
- case UnexpectedBreak:
- Q_STATIC_ASSERT(int(UnexpectedBreak) == int(CborErrorUnexpectedBreak));
- return QStringLiteral("Invalid CBOR stream: unexpected 'break' byte");
- case UnknownType:
- Q_STATIC_ASSERT(int(UnknownType) == int(CborErrorUnknownType));
- return QStringLiteral("Invalid CBOR stream: unknown type");
- case IllegalType:
- Q_STATIC_ASSERT(int(IllegalType) == int(CborErrorIllegalType));
- return QStringLiteral("Invalid CBOR stream: illegal type found");
- case IllegalNumber:
- Q_STATIC_ASSERT(int(IllegalNumber) == int(CborErrorIllegalNumber));
- return QStringLiteral("Invalid CBOR stream: illegal number encoding (future extension)");
- case IllegalSimpleType:
- Q_STATIC_ASSERT(int(IllegalSimpleType) == int(CborErrorIllegalSimpleType));
- return QStringLiteral("Invalid CBOR stream: illegal simple type");
- case InvalidUtf8String:
- Q_STATIC_ASSERT(int(InvalidUtf8String) == int(CborErrorInvalidUtf8TextString));
- return QStringLiteral("Invalid CBOR stream: invalid UTF-8 text string");
- case DataTooLarge:
- Q_STATIC_ASSERT(int(DataTooLarge) == int(CborErrorDataTooLarge));
- return QStringLiteral("Internal limitation: data set too large");
- case NestingTooDeep:
- Q_STATIC_ASSERT(int(NestingTooDeep) == int(CborErrorNestingTooDeep));
- return QStringLiteral("Internal limitation: data nesting too deep");
- case UnsupportedType:
- Q_STATIC_ASSERT(int(UnsupportedType) == int(CborErrorUnsupportedType));
- return QStringLiteral("Internal limitation: unsupported type");
- }
-
- // get the error string from TinyCBOR
- CborError err = toCborError(*this);
- return QString::fromLatin1(cbor_error_string(err));
-}
-
-/*!
- \class QCborStreamWriter
- \inmodule QtCore
- \ingroup cbor
- \reentrant
- \since 5.12
-
- \brief The QCborStreamWriter class is a simple CBOR encoder operating on a
- one-way stream.
-
- This class can be used to quickly encode a stream of CBOR content directly
- to either a QByteArray or QIODevice. CBOR is the Concise Binary Object
- Representation, a very compact form of binary data encoding that is
- compatible with JSON. It was created by the IETF Constrained RESTful
- Environments (CoRE) WG, which has used it in many new RFCs. It is meant to
- be used alongside the \l{https://tools.ietf.org/html/rfc7252}{CoAP
- protocol}.
-
- QCborStreamWriter provides a StAX-like API, similar to that of
- \l{QXmlStreamWriter}. It is rather low-level and requires a bit of knowledge
- of CBOR encoding. For a simpler API, see \l{QCborValue} and especially the
- encoding function QCborValue::toCbor().
-
- The typical use of QCborStreamWriter is to create the object on the target
- QByteArray or QIODevice, then call one of the append() overloads with the
- desired type to be encoded. To create arrays and maps, QCborStreamWriter
- provides startArray() and startMap() overloads, which must be terminated by
- the corresponding endArray() and endMap() functions.
-
- The following example encodes the equivalent of this JSON content:
-
- \div{class="pre"}
- {
- "label": "journald",
- "autoDetect": false,
- "condition": "libs.journald",
- "output": [ "privateFeature" ]
- }
- \enddiv
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 1
-
- \section1 CBOR support
-
- QCborStreamWriter supports all CBOR features required to create canonical
- and strict streams. It implements almost all of the features specified in
- \l {https://tools.ietf.org/html/rfc7049}{RFC 7049}.
-
- The following table lists the CBOR features that QCborStreamWriter supports.
-
- \table
- \header \li Feature \li Support
- \row \li Unsigned numbers \li Yes (full range)
- \row \li Negative numbers \li Yes (full range)
- \row \li Byte strings \li Yes
- \row \li Text strings \li Yes
- \row \li Chunked strings \li No
- \row \li Tags \li Yes (arbitrary)
- \row \li Booleans \li Yes
- \row \li Null \li Yes
- \row \li Undefined \li Yes
- \row \li Arbitrary simple values \li Yes
- \row \li Half-precision float (16-bit) \li Yes
- \row \li Single-precision float (32-bit) \li Yes
- \row \li Double-precision float (64-bit) \li Yes
- \row \li Infinities and NaN floating point \li Yes
- \row \li Determinate-length arrays and maps \li Yes
- \row \li Indeterminate-length arrays and maps \li Yes
- \row \li Map key types other than strings and integers \li Yes (arbitrary)
- \endtable
-
- \section2 Canonical CBOR encoding
-
- Canonical CBOR encoding is defined by
- \l{https://tools.ietf.org/html/rfc7049#section-3.9}{Section 3.9 of RFC
- 7049}. Canonical encoding is not a requirement for Qt's CBOR decoding
- functionality, but it may be required for some protocols. In particular,
- protocols that require the ability to reproduce the same stream identically
- may require this.
-
- In order to be considered "canonical", a CBOR stream must meet the
- following requirements:
-
- \list
- \li Integers must be as small as possible. QCborStreamWriter always
- does this (no user action is required and it is not possible
- to write overlong integers).
- \li Array, map and string lengths must be as short as possible. As
- above, QCborStreamWriter automatically does this.
- \li Arrays, maps and strings must use explicit length. QCborStreamWriter
- always does this for strings; for arrays and maps, be sure to call
- startArray() and startMap() overloads with explicit length.
- \li Keys in every map must be sorted in ascending order. QCborStreamWriter
- offers no help in this item: the developer must ensure that before
- calling append() for the map pairs.
- \li Floating point values should be as small as possible. QCborStreamWriter
- will not convert floating point values; it is up to the developer
- to perform this check prior to calling append() (see those functions'
- examples).
- \endlist
-
- \section2 Strict CBOR mode
-
- Strict mode is defined by
- \l{https://tools.ietf.org/html/rfc7049#section-3.10}{Section 3.10 of RFC
- 7049}. As for Canonical encoding above, QCborStreamWriter makes it possible
- to create strict CBOR streams, but does not require them or validate that
- the output is so.
-
- \list
- \li Keys in a map must be unique. QCborStreamWriter performs no validation
- of map keys.
- \li Tags may be required to be paired only with the correct types,
- according to their specification. QCborStreamWriter performs no
- validation of tag usage.
- \li Text Strings must be properly-encoded UTF-8. QCborStreamWriter always
- writes proper UTF-8 for strings added with append(), but performs no
- validation for strings added with appendTextString().
- \endlist
-
- \section2 Invalid CBOR stream
-
- It is also possible to misuse QCborStreamWriter and produce invalid CBOR
- streams that will fail to be decoded by a receiver. The following actions
- will produce invalid streams:
-
- \list
- \li Append a tag and not append the corresponding tagged value
- (QCborStreamWriter produces no diagnostic).
- \li Append too many or too few items to an array or map with explicit
- length (endMap() and endArray() will return false and
- QCborStreamWriter will log with qWarning()).
- \endlist
-
- \sa QCborStreamReader, QCborValue, QXmlStreamWriter
- */
-
-class QCborStreamWriterPrivate
-{
-public:
- static Q_CONSTEXPR quint64 IndefiniteLength = (std::numeric_limits<quint64>::max)();
-
- QIODevice *device;
- CborEncoder encoder;
- QStack<CborEncoder> containerStack;
- bool deleteDevice = false;
-
- QCborStreamWriterPrivate(QIODevice *device)
- : device(device)
- {
- cbor_encoder_init_writer(&encoder, qt_cbor_encoder_write_callback, this);
- }
-
- ~QCborStreamWriterPrivate()
- {
- if (deleteDevice)
- delete device;
- }
-
- template <typename... Args> void executeAppend(CborError (*f)(CborEncoder *, Args...), Args... args)
- {
- f(&encoder, std::forward<Args>(args)...);
- }
-
- void createContainer(CborError (*f)(CborEncoder *, CborEncoder *, size_t), quint64 len = IndefiniteLength)
- {
- Q_STATIC_ASSERT(size_t(IndefiniteLength) == CborIndefiniteLength);
- if (sizeof(len) != sizeof(size_t) && len != IndefiniteLength) {
- if (Q_UNLIKELY(len >= CborIndefiniteLength)) {
- // TinyCBOR can't do this in 32-bit mode
- qWarning("QCborStreamWriter: container of size %llu is too big for a 32-bit build; "
- "will use indeterminate length instead", len);
- len = CborIndefiniteLength;
- }
- }
-
- containerStack.push(encoder);
- f(&containerStack.top(), &encoder, len);
- }
-
- bool closeContainer()
- {
- if (containerStack.isEmpty()) {
- qWarning("QCborStreamWriter: closing map or array that wasn't open");
- return false;
- }
-
- CborEncoder container = containerStack.pop();
- CborError err = cbor_encoder_close_container(&container, &encoder);
- encoder = container;
-
- if (Q_UNLIKELY(err)) {
- if (err == CborErrorTooFewItems)
- qWarning("QCborStreamWriter: not enough items added to array or map");
- else if (err == CborErrorTooManyItems)
- qWarning("QCborStreamWriter: too many items added to array or map");
- return false;
- }
-
- return true;
- }
-};
-
-static CborError qt_cbor_encoder_write_callback(void *self, const void *data, size_t len, CborEncoderAppendType)
-{
- auto that = static_cast<QCborStreamWriterPrivate *>(self);
- if (!that->device)
- return CborNoError;
- qint64 written = that->device->write(static_cast<const char *>(data), len);
- return (written == qsizetype(len) ? CborNoError : CborErrorIO);
-}
-
-/*!
- Creates a QCborStreamWriter object that will write the stream to \a device.
- The device must be opened before the first append() call is made. This
- constructor can be used with any class that derives from QIODevice, such as
- QFile, QProcess or QTcpSocket.
-
- QCborStreamWriter has no buffering, so every append() call will result in
- one or more calls to the device's \l {QIODevice::}{write()} method.
-
- The following example writes an empty map to a file:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 2
-
- QCborStreamWriter does not take ownership of \a device.
-
- \sa device(), setDevice()
- */
-QCborStreamWriter::QCborStreamWriter(QIODevice *device)
- : d(new QCborStreamWriterPrivate(device))
-{
-}
-
-/*!
- Creates a QCborStreamWriter object that will append the stream to \a data.
- All streaming is done immediately to the byte array, without the need for
- flushing any buffers.
-
- The following example writes a number to a byte array then returns
- it.
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 3
-
- QCborStreamWriter does not take ownership of \a data.
- */
-QCborStreamWriter::QCborStreamWriter(QByteArray *data)
- : d(new QCborStreamWriterPrivate(new QBuffer(data)))
-{
- d->deleteDevice = true;
- d->device->open(QIODevice::WriteOnly | QIODevice::Unbuffered);
-}
-
-/*!
- Destroys this QCborStreamWriter object and frees any resources associated.
-
- QCborStreamWriter does not perform error checking to see if all required
- items were written to the stream prior to the object being destroyed. It is
- the programmer's responsibility to ensure that it was done.
- */
-QCborStreamWriter::~QCborStreamWriter()
-{
-}
-
-/*!
- Replaces the device or byte array that this QCborStreamWriter object is
- writing to with \a device.
-
- \sa device()
- */
-void QCborStreamWriter::setDevice(QIODevice *device)
-{
- if (d->deleteDevice)
- delete d->device;
- d->device = device;
- d->deleteDevice = false;
-}
-
-/*!
- Returns the QIODevice that this QCborStreamWriter object is writing to. The
- device must have previously been set with either the constructor or with
- setDevice().
-
- If this object was created by writing to a QByteArray, this function will
- return an internal instance of QBuffer, which is owned by QCborStreamWriter.
-
- \sa setDevice()
- */
-QIODevice *QCborStreamWriter::device() const
-{
- return d->device;
-}
-
-/*!
- \overload
-
- Appends the 64-bit unsigned value \a u to the CBOR stream, creating a CBOR
- Unsigned Integer value. In the following example, we write the values 0,
- 2\sup{32} and \c UINT64_MAX:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 4
-
- \sa QCborStreamReader::isUnsignedInteger(), QCborStreamReader::toUnsignedInteger()
- */
-void QCborStreamWriter::append(quint64 u)
-{
- d->executeAppend(cbor_encode_uint, uint64_t(u));
-}
-
-/*!
- \overload
-
- Appends the 64-bit signed value \a i to the CBOR stream. This will create
- either a CBOR Unsigned Integer or CBOR NegativeInteger value based on the
- sign of the parameter. In the following example, we write the values 0, -1,
- 2\sup{32} and \c INT64_MAX:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 5
-
- \sa QCborStreamReader::isInteger(), QCborStreamReader::toInteger()
- */
-void QCborStreamWriter::append(qint64 i)
-{
- d->executeAppend(cbor_encode_int, int64_t(i));
-}
-
-/*!
- \overload
-
- Appends the 64-bit negative value \a n to the CBOR stream.
- QCborNegativeInteger is a 64-bit enum that holds the absolute value of the
- negative number we want to write. If n is zero, the value written will be
- equivalent to 2\sup{64} (that is, -18,446,744,073,709,551,616).
-
- In the following example, we write the values -1, -2\sup{32} and INT64_MIN:
- \snippet code/src_corelib_serialization_qcborstream.cpp 6
-
- Note how this function can be used to encode numbers that cannot fit a
- standard computer's 64-bit signed integer like \l qint64. That is, if \a n
- is larger than \c{std::numeric_limits<qint64>::max()} or is 0, this will
- represent a negative number smaller than
- \c{std::numeric_limits<qint64>::min()}.
-
- \sa QCborStreamReader::isNegativeInteger(), QCborStreamReader::toNegativeInteger()
- */
-void QCborStreamWriter::append(QCborNegativeInteger n)
-{
- d->executeAppend(cbor_encode_negative_int, uint64_t(n));
-}
-
-/*!
- \fn void QCborStreamWriter::append(const QByteArray &ba)
- \overload
-
- Appends the byte array \a ba to the stream, creating a CBOR Byte String
- value. QCborStreamWriter will attempt to write the entire string in one
- chunk.
-
- The following example will load and append the contents of a file to the
- stream:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 7
-
- As the example shows, unlike JSON, CBOR requires no escaping for binary
- content.
-
- \sa appendByteString(), QCborStreamReader::isByteArray(),
- QCborStreamReader::readByteArray()
- */
-
-/*!
- \overload
-
- Appends the text string \a str to the stream, creating a CBOR Text String
- value. QCborStreamWriter will attempt to write the entire string in one
- chunk.
-
- The following example appends a simple string to the stream:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 8
-
- \b{Performance note}: CBOR requires that all Text Strings be encoded in
- UTF-8, so this function will iterate over the characters in the string to
- determine whether the contents are US-ASCII or not. If the string is found
- to contain characters outside of US-ASCII, it will allocate memory and
- convert to UTF-8. If this check is unnecessary, use appendTextString()
- instead.
-
- \sa QCborStreamReader::isString(), QCborStreamReader::readString()
- */
-void QCborStreamWriter::append(QLatin1String str)
-{
- // We've got Latin-1 but CBOR wants UTF-8, so check if the string is the
- // common subset (US-ASCII).
- if (QtPrivate::isAscii(str)) {
- // it is plain US-ASCII
- appendTextString(str.latin1(), str.size());
- } else {
- // non-ASCII, so we need a pass-through UTF-16
- append(QString(str));
- }
-}
-
-/*!
- \overload
-
- Appends the text string \a str to the stream, creating a CBOR Text String
- value. QCborStreamWriter will attempt to write the entire string in one
- chunk.
-
- The following example writes an arbitrary QString to the stream:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 9
-
- \sa QCborStreamReader::isString(), QCborStreamReader::readString()
- */
-void QCborStreamWriter::append(QStringView str)
-{
- QByteArray utf8 = str.toUtf8();
- appendTextString(utf8.constData(), utf8.size());
-}
-
-/*!
- \overload
-
- Appends the CBOR tag \a tag to the stream, creating a CBOR Tag value. All
- tags must be followed by another type which they provide meaning for.
-
- In the following example, we append a CBOR Tag 36 (Regular Expression) and a
- QRegularExpression's pattern to the stream:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 10
-
- \sa QCborStreamReader::isTag(), QCborStreamReader::toTag()
- */
-void QCborStreamWriter::append(QCborTag tag)
-{
- d->executeAppend(cbor_encode_tag, CborTag(tag));
-}
-
-/*!
- \fn void QCborStreamWriter::append(QCborKnownTags tag)
- \overload
-
- Appends the CBOR tag \a tag to the stream, creating a CBOR Tag value. All
- tags must be followed by another type which they provide meaning for.
-
- In the following example, we append a CBOR Tag 1 (Unix \c time_t) and an
- integer representing the current time to the stream, obtained using the \c
- time() function:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 11
-
- \sa QCborStreamReader::isTag(), QCborStreamReader::toTag()
- */
-
-/*!
- \overload
-
- Appends the CBOR simple type \a st to the stream, creating a CBOR Simple
- Type value. In the following example, we write the simple type for Null as
- well as for type 32, which Qt has no support for.
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 12
-
- \note Using Simple Types for which there is no specification can lead to
- validation errors by the remote receiver. In addition, simple type values 24
- through 31 (inclusive) are reserved and must not be used.
-
- \sa QCborStreamReader::isSimpleType(), QCborStreamReader::toSimpleType()
- */
-void QCborStreamWriter::append(QCborSimpleType st)
-{
- d->executeAppend(cbor_encode_simple_value, uint8_t(st));
-}
-
-/*!
- \overload
-
- Appends the floating point number \a f to the stream, creating a CBOR 16-bit
- Half-Precision Floating Point value. The following code can be used to convert
- a C++ \tt float to \c qfloat16 if there's no loss of precision and append it, or
- instead append the \tt float.
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 13
-
- \sa QCborStreamReader::isFloat16(), QCborStreamReader::toFloat16()
- */
-void QCborStreamWriter::append(qfloat16 f)
-{
- d->executeAppend(cbor_encode_half_float, static_cast<const void *>(&f));
-}
-
-/*!
- \overload
-
- Appends the floating point number \a f to the stream, creating a CBOR 32-bit
- Single-Precision Floating Point value. The following code can be used to convert
- a C++ \tt double to \tt float if there's no loss of precision and append it, or
- instead append the \tt double.
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 14
-
- \sa QCborStreamReader::isFloat(), QCborStreamReader::toFloat()
- */
-void QCborStreamWriter::append(float f)
-{
- d->executeAppend(cbor_encode_float, f);
-}
-
-/*!
- \overload
-
- Appends the floating point number \a d to the stream, creating a CBOR 64-bit
- Double-Precision Floating Point value. QCborStreamWriter always appends the
- number as-is, performing no check for whether the number is the canonical
- form for NaN, an infinite, whether it is denormal or if it could be written
- with a shorter format.
-
- The following code performs all those checks, except for the denormal one,
- which is expected to be taken into account by the system FPU or floating
- point emulation directly.
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 15
-
- Determining if a double can be converted to an integral with no loss of
- precision is left as an exercise to the reader.
-
- \sa QCborStreamReader::isDouble(), QCborStreamReader::toDouble()
- */
-void QCborStreamWriter::append(double d)
-{
- this->d->executeAppend(cbor_encode_double, d);
-}
-
-/*!
- Appends \a len bytes of data starting from \a data to the stream, creating a
- CBOR Byte String value. QCborStreamWriter will attempt to write the entire
- string in one chunk.
-
- Unlike the QByteArray overload of append(), this function is not limited by
- QByteArray's size limits. However, note that neither
- QCborStreamReader::readByteArray() nor QCborValue support reading CBOR
- streams with byte arrays larger than 2 GB.
-
- \sa append(), appendTextString(),
- QCborStreamReader::isByteArray(), QCborStreamReader::readByteArray()
- */
-void QCborStreamWriter::appendByteString(const char *data, qsizetype len)
-{
- d->executeAppend(cbor_encode_byte_string, reinterpret_cast<const uint8_t *>(data), size_t(len));
-}
-
-/*!
- Appends \a len bytes of text starting from \a utf8 to the stream, creating a
- CBOR Text String value. QCborStreamWriter will attempt to write the entire
- string in one chunk.
-
- The string pointed to by \a utf8 is expected to be properly encoded UTF-8.
- QCborStreamWriter performs no validation that this is the case.
-
- Unlike the QLatin1String overload of append(), this function is not limited
- to 2 GB. However, note that neither QCborStreamReader::readString() nor
- QCborValue support reading CBOR streams with text strings larger than 2 GB.
-
- \sa append(QLatin1String), append(QStringView),
- QCborStreamReader::isString(), QCborStreamReader::readString()
- */
-void QCborStreamWriter::appendTextString(const char *utf8, qsizetype len)
-{
- d->executeAppend(cbor_encode_text_string, utf8, size_t(len));
-}
-
-/*!
- \fn void QCborStreamWriter::append(const char *str, qsizetype size)
- \overload
-
- Appends \a size bytes of text starting from \a str to the stream, creating a
- CBOR Text String value. QCborStreamWriter will attempt to write the entire
- string in one chunk. If \a size is -1, this function will write \c strlen(\a
- str) bytes.
-
- The string pointed to by \a str is expected to be properly encoded UTF-8.
- QCborStreamWriter performs no validation that this is the case.
-
- Unlike the QLatin1String overload of append(), this function is not limited
- to 2 GB. However, note that neither QCborStreamReader nor QCborValue support
- reading CBOR streams with text strings larger than 2 GB.
-
- \sa append(QLatin1String), append(QStringView),
- QCborStreamReader::isString(), QCborStreamReader::readString()
- */
-
-/*!
- \fn void QCborStreamWriter::append(bool b)
- \overload
-
- Appends the boolean value \a b to the stream, creating either a CBOR False
- value or a CBOR True value. This function is equivalent to (and implemented
- as):
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 16
-
- \sa appendNull(), appendUndefined(),
- QCborStreamReader::isBool(), QCborStreamReader::toBool()
- */
-
-/*!
- \fn void QCborStreamWriter::append(std::nullptr_t)
- \overload
-
- Appends a CBOR Null value to the stream. This function is equivalent to (and
- implemented as): The parameter is ignored.
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 17
-
- \sa appendNull(), append(QCborSimpleType), QCborStreamReader::isNull()
- */
-
-/*!
- \fn void QCborStreamWriter::appendNull()
-
- Appends a CBOR Null value to the stream. This function is equivalent to (and
- implemented as):
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 18
-
- \sa append(std::nullptr_t), append(QCborSimpleType), QCborStreamReader::isNull()
- */
-
-/*!
- \fn void QCborStreamWriter::appendUndefined()
-
- Appends a CBOR Undefined value to the stream. This function is equivalent to (and
- implemented as):
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 19
-
- \sa append(QCborSimpleType), QCborStreamReader::isUndefined()
- */
-
-/*!
- Starts a CBOR Array with indeterminate length in the CBOR stream. Each
- startArray() call must be paired with one endArray() call and the current
- CBOR element extends until the end of the array.
-
- The array created by this function has no explicit length. Instead, its
- length is implied by the elements contained in it. Note, however, that use
- of indeterminate-length arrays is not compliant with canonical CBOR encoding.
-
- The following example appends elements from the linked list of strings
- passed as input:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 20
-
- \sa startArray(quint64), endArray(), startMap(), QCborStreamReader::isArray(),
- QCborStreamReader::isLengthKnown()
- */
-void QCborStreamWriter::startArray()
-{
- d->createContainer(cbor_encoder_create_array);
-}
-
-/*!
- \overload
-
- Starts a CBOR Array with explicit length of \a count items in the CBOR
- stream. Each startArray call must be paired with one endArray() call and the
- current CBOR element extends until the end of the array.
-
- The array created by this function has an explicit length and therefore
- exactly \a count items must be added to the CBOR stream. Adding fewer or
- more items will result in failure during endArray() and the CBOR stream will
- be corrupt. However, explicit-length arrays are required by canonical CBOR
- encoding.
-
- The following example appends all strings found in the \l QStringList passed as input:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 21
-
- \b{Size limitations}: The parameter to this function is quint64, which would
- seem to allow up to 2\sup{64}-1 elements in the array. However, both
- QCborStreamWriter and QCborStreamReader are currently limited to 2\sup{32}-2
- items on 32-bit systems and 2\sup{64}-2 items on 64-bit ones. Also note that
- QCborArray is currently limited to 2\sup{27} elements in any platform.
-
- \sa startArray(), endArray(), startMap(), QCborStreamReader::isArray(),
- QCborStreamReader::isLengthKnown()
- */
-void QCborStreamWriter::startArray(quint64 count)
-{
- d->createContainer(cbor_encoder_create_array, count);
-}
-
-/*!
- Terminates the array started by either overload of startArray() and returns
- true if the correct number of elements was added to the array. This function
- must be called for every startArray() used.
-
- A return of false indicates error in the application and an unrecoverable
- error in this stream. QCborStreamWriter also writes a warning using
- qWarning() if that happens.
-
- Calling this function when the current container is not an array is also an
- error, though QCborStreamWriter cannot currently detect this condition.
-
- \sa startArray(), startArray(quint64), endMap()
- */
-bool QCborStreamWriter::endArray()
-{
- return d->closeContainer();
-}
-
-/*!
- Starts a CBOR Map with indeterminate length in the CBOR stream. Each
- startMap() call must be paired with one endMap() call and the current CBOR
- element extends until the end of the map.
-
- The map created by this function has no explicit length. Instead, its length
- is implied by the elements contained in it. Note, however, that use of
- indeterminate-length maps is not compliant with canonical CBOR encoding
- (canonical encoding also requires keys to be unique and in sorted order).
-
- The following example appends elements from the linked list of int and
- string pairs passed as input:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 22
-
- \sa startMap(quint64), endMap(), startArray(), QCborStreamReader::isMap(),
- QCborStreamReader::isLengthKnown()
- */
-void QCborStreamWriter::startMap()
-{
- d->createContainer(cbor_encoder_create_map);
-}
-
-/*!
- \overload
-
- Starts a CBOR Map with explicit length of \a count items in the CBOR
- stream. Each startMap call must be paired with one endMap() call and the
- current CBOR element extends until the end of the map.
-
- The map created by this function has an explicit length and therefore
- exactly \a count pairs of items must be added to the CBOR stream. Adding
- fewer or more items will result in failure during endMap() and the CBOR
- stream will be corrupt. However, explicit-length map are required by
- canonical CBOR encoding.
-
- The following example appends all strings found in the \l QMap passed as input:
-
- \snippet code/src_corelib_serialization_qcborstream.cpp 23
-
- \b{Size limitations}: The parameter to this function is quint64, which would
- seem to allow up to 2\sup{64}-1 pairs in the map. However, both
- QCborStreamWriter and QCborStreamReader are currently limited to 2\sup{31}-1
- items on 32-bit systems and 2\sup{63}-1 items on 64-bit ones. Also note that
- QCborMap is currently limited to 2\sup{26} elements in any platform.
-
- \sa startMap(), endMap(), startArray(), QCborStreamReader::isMap(),
- QCborStreamReader::isLengthKnown()
- */
-void QCborStreamWriter::startMap(quint64 count)
-{
- d->createContainer(cbor_encoder_create_map, count);
-}
-
-/*!
- Terminates the map started by either overload of startMap() and returns
- true if the correct number of elements was added to the array. This function
- must be called for every startMap() used.
-
- A return of false indicates error in the application and an unrecoverable
- error in this stream. QCborStreamWriter also writes a warning using
- qWarning() if that happens.
-
- Calling this function when the current container is not a map is also an
- error, though QCborStreamWriter cannot currently detect this condition.
-
- \sa startMap(), startMap(quint64), endArray()
- */
-bool QCborStreamWriter::endMap()
-{
- return d->closeContainer();
-}
-
-/*!
\class QCborStreamReader
\inmodule QtCore
\ingroup cbor
@@ -1756,7 +641,7 @@ public:
if (err != CborErrorUnexpectedEOF)
corrupt = true;
- lastError = fromCborError(err);
+ lastError = QCborError { QCborError::Code(int(err)) };
}
void updateBufferAfterString(qsizetype offset, qsizetype size)
@@ -2645,5 +1530,4 @@ QCborStreamReader::readStringChunk(char *ptr, qsizetype maxlen)
QT_END_NAMESPACE
-#include "moc_qcborcommon.cpp"
-#include "moc_qcborstream.cpp"
+#include "moc_qcborstreamreader.cpp"
diff --git a/src/corelib/serialization/qcborstreamreader.h b/src/corelib/serialization/qcborstreamreader.h
new file mode 100644
index 0000000000..6d5feccfcf
--- /dev/null
+++ b/src/corelib/serialization/qcborstreamreader.h
@@ -0,0 +1,210 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 Intel Corporation.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCBORSTREAMREADER_H
+#define QCBORSTREAMREADER_H
+
+#include <QtCore/qbytearray.h>
+#include <QtCore/qcborcommon.h>
+#include <QtCore/qfloat16.h>
+#include <QtCore/qscopedpointer.h>
+#include <QtCore/qstring.h>
+#include <QtCore/qstringview.h>
+
+QT_REQUIRE_CONFIG(cborstreamreader);
+
+// See qcborcommon.h for why we check
+#if defined(QT_X11_DEFINES_FOUND)
+# undef True
+# undef False
+#endif
+
+QT_BEGIN_NAMESPACE
+
+class QIODevice;
+
+class QCborStreamReaderPrivate;
+class Q_CORE_EXPORT QCborStreamReader
+{
+ Q_GADGET
+public:
+ enum Type : quint8 {
+ UnsignedInteger = 0x00,
+ NegativeInteger = 0x20,
+ ByteString = 0x40,
+ ByteArray = ByteString,
+ TextString = 0x60,
+ String = TextString,
+ Array = 0x80,
+ Map = 0xa0,
+ Tag = 0xc0,
+ SimpleType = 0xe0,
+ HalfFloat = 0xf9,
+ Float16 = HalfFloat,
+ Float = 0xfa,
+ Double = 0xfb,
+
+ Invalid = 0xff
+ };
+ Q_ENUM(Type)
+
+ enum StringResultCode {
+ EndOfString = 0,
+ Ok = 1,
+ Error = -1
+ };
+ template <typename Container> struct StringResult {
+ Container data;
+ StringResultCode status = Error;
+ };
+ Q_ENUM(StringResultCode)
+
+ QCborStreamReader();
+ QCborStreamReader(const char *data, qsizetype len);
+ QCborStreamReader(const quint8 *data, qsizetype len);
+ explicit QCborStreamReader(const QByteArray &data);
+ explicit QCborStreamReader(QIODevice *device);
+ ~QCborStreamReader();
+ Q_DISABLE_COPY(QCborStreamReader)
+
+ void setDevice(QIODevice *device);
+ QIODevice *device() const;
+ void addData(const QByteArray &data);
+ void addData(const char *data, qsizetype len);
+ void addData(const quint8 *data, qsizetype len)
+ { addData(reinterpret_cast<const char *>(data), len); }
+ void reparse();
+ void clear();
+ void reset();
+
+ QCborError lastError();
+
+ qint64 currentOffset() const;
+
+ bool isValid() const { return !isInvalid(); }
+
+ int containerDepth() const;
+ QCborStreamReader::Type parentContainerType() const;
+ bool hasNext() const noexcept Q_DECL_PURE_FUNCTION;
+ bool next(int maxRecursion = 10000);
+
+ Type type() const { return QCborStreamReader::Type(type_); }
+ bool isUnsignedInteger() const { return type() == UnsignedInteger; }
+ bool isNegativeInteger() const { return type() == NegativeInteger; }
+ bool isInteger() const { return quint8(type()) <= quint8(NegativeInteger); }
+ bool isByteArray() const { return type() == ByteArray; }
+ bool isString() const { return type() == String; }
+ bool isArray() const { return type() == Array; }
+ bool isMap() const { return type() == Map; }
+ bool isTag() const { return type() == Tag; }
+ bool isSimpleType() const { return type() == SimpleType; }
+ bool isFloat16() const { return type() == Float16; }
+ bool isFloat() const { return type() == Float; }
+ bool isDouble() const { return type() == Double; }
+ bool isInvalid() const { return type() == Invalid; }
+
+ bool isSimpleType(QCborSimpleType st) const { return isSimpleType() && toSimpleType() == st; }
+ bool isFalse() const { return isSimpleType(QCborSimpleType::False); }
+ bool isTrue() const { return isSimpleType(QCborSimpleType::True); }
+ bool isBool() const { return isFalse() || isTrue(); }
+ bool isNull() const { return isSimpleType(QCborSimpleType::Null); }
+ bool isUndefined() const { return isSimpleType(QCborSimpleType::Undefined); }
+
+ bool isLengthKnown() const noexcept Q_DECL_PURE_FUNCTION;
+ quint64 length() const;
+
+ bool isContainer() const { return isMap() || isArray(); }
+ bool enterContainer() { Q_ASSERT(isContainer()); return _enterContainer_helper(); }
+ bool leaveContainer();
+
+ StringResult<QString> readString() { Q_ASSERT(isString()); return _readString_helper(); }
+ StringResult<QByteArray> readByteArray(){ Q_ASSERT(isByteArray()); return _readByteArray_helper(); }
+ qsizetype currentStringChunkSize() const{ Q_ASSERT(isString() || isByteArray()); return _currentStringChunkSize(); }
+ StringResult<qsizetype> readStringChunk(char *ptr, qsizetype maxlen);
+
+ bool toBool() const { Q_ASSERT(isBool()); return value64 - int(QCborSimpleType::False); }
+ QCborTag toTag() const { Q_ASSERT(isTag()); return QCborTag(value64); }
+ quint64 toUnsignedInteger() const { Q_ASSERT(isUnsignedInteger()); return value64; }
+ QCborNegativeInteger toNegativeInteger() const { Q_ASSERT(isNegativeInteger()); return QCborNegativeInteger(value64 + 1); }
+ QCborSimpleType toSimpleType() const{ Q_ASSERT(isSimpleType()); return QCborSimpleType(value64); }
+ qfloat16 toFloat16() const { Q_ASSERT(isFloat16()); return _toFloatingPoint<qfloat16>(); }
+ float toFloat() const { Q_ASSERT(isFloat()); return _toFloatingPoint<float>(); }
+ double toDouble() const { Q_ASSERT(isDouble()); return _toFloatingPoint<double>(); }
+
+ qint64 toInteger() const
+ {
+ Q_ASSERT(isInteger());
+ qint64 v = qint64(value64);
+ if (isNegativeInteger())
+ return -v - 1;
+ return v;
+ }
+
+private:
+ void preparse();
+ bool _enterContainer_helper();
+ StringResult<QString> _readString_helper();
+ StringResult<QByteArray> _readByteArray_helper();
+ qsizetype _currentStringChunkSize() const;
+
+ template <typename FP> FP _toFloatingPoint() const noexcept
+ {
+ using UIntFP = typename QIntegerForSizeof<FP>::Unsigned;
+ UIntFP u = UIntFP(value64);
+ FP f;
+ memcpy(static_cast<void *>(&f), &u, sizeof(f));
+ return f;
+ }
+
+ friend QCborStreamReaderPrivate;
+ friend class QCborContainerPrivate;
+ quint64 value64;
+ QScopedPointer<QCborStreamReaderPrivate> d;
+ quint8 type_;
+ quint8 reserved[3] = {};
+};
+
+QT_END_NAMESPACE
+
+#if defined(QT_X11_DEFINES_FOUND)
+# define True 1
+# define False 0
+#endif
+
+#endif // QCBORSTREAMREADER_H
diff --git a/src/corelib/serialization/qcborstreamwriter.cpp b/src/corelib/serialization/qcborstreamwriter.cpp
new file mode 100644
index 0000000000..9d78785416
--- /dev/null
+++ b/src/corelib/serialization/qcborstreamwriter.cpp
@@ -0,0 +1,868 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 Intel Corporation.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcborstreamwriter.h"
+
+#define CBOR_NO_PARSER_API
+#include <private/qcborcommon_p.h>
+
+#include <private/qnumeric_p.h>
+#include <qbuffer.h>
+#include <qdebug.h>
+#include <qstack.h>
+
+QT_BEGIN_NAMESPACE
+
+static CborError qt_cbor_encoder_write_callback(void *token, const void *data, size_t len, CborEncoderAppendType);
+#define CBOR_ENCODER_WRITER_CONTROL 1
+#define CBOR_ENCODER_WRITE_FUNCTION qt_cbor_encoder_write_callback
+#define CBOR_ENCODER_NO_CHECK_USER
+
+QT_WARNING_PUSH
+QT_WARNING_DISABLE_MSVC(4334) // '<<': result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
+
+#include <cborencoder.c>
+
+QT_WARNING_POP
+
+// silence compilers that complain about this being a static function declared
+// but never defined
+static CborError Q_DECL_UNUSED cbor_encoder_close_container_checked(CborEncoder*, const CborEncoder*)
+{
+ Q_UNREACHABLE();
+ return CborErrorInternalError;
+}
+
+static CborError Q_DECL_UNUSED cbor_encode_float_as_half_float(CborEncoder *, float)
+{
+ Q_UNREACHABLE();
+ return CborErrorInternalError;
+}
+
+Q_DECLARE_TYPEINFO(CborEncoder, Q_PRIMITIVE_TYPE);
+
+/*!
+ \class QCborStreamWriter
+ \inmodule QtCore
+ \ingroup cbor
+ \reentrant
+ \since 5.12
+
+ \brief The QCborStreamWriter class is a simple CBOR encoder operating on a
+ one-way stream.
+
+ This class can be used to quickly encode a stream of CBOR content directly
+ to either a QByteArray or QIODevice. CBOR is the Concise Binary Object
+ Representation, a very compact form of binary data encoding that is
+ compatible with JSON. It was created by the IETF Constrained RESTful
+ Environments (CoRE) WG, which has used it in many new RFCs. It is meant to
+ be used alongside the \l{https://tools.ietf.org/html/rfc7252}{CoAP
+ protocol}.
+
+ QCborStreamWriter provides a StAX-like API, similar to that of
+ \l{QXmlStreamWriter}. It is rather low-level and requires a bit of knowledge
+ of CBOR encoding. For a simpler API, see \l{QCborValue} and especially the
+ encoding function QCborValue::toCbor().
+
+ The typical use of QCborStreamWriter is to create the object on the target
+ QByteArray or QIODevice, then call one of the append() overloads with the
+ desired type to be encoded. To create arrays and maps, QCborStreamWriter
+ provides startArray() and startMap() overloads, which must be terminated by
+ the corresponding endArray() and endMap() functions.
+
+ The following example encodes the equivalent of this JSON content:
+
+ \div{class="pre"}
+ {
+ "label": "journald",
+ "autoDetect": false,
+ "condition": "libs.journald",
+ "output": [ "privateFeature" ]
+ }
+ \enddiv
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 1
+
+ \section1 CBOR support
+
+ QCborStreamWriter supports all CBOR features required to create canonical
+ and strict streams. It implements almost all of the features specified in
+ \l {https://tools.ietf.org/html/rfc7049}{RFC 7049}.
+
+ The following table lists the CBOR features that QCborStreamWriter supports.
+
+ \table
+ \header \li Feature \li Support
+ \row \li Unsigned numbers \li Yes (full range)
+ \row \li Negative numbers \li Yes (full range)
+ \row \li Byte strings \li Yes
+ \row \li Text strings \li Yes
+ \row \li Chunked strings \li No
+ \row \li Tags \li Yes (arbitrary)
+ \row \li Booleans \li Yes
+ \row \li Null \li Yes
+ \row \li Undefined \li Yes
+ \row \li Arbitrary simple values \li Yes
+ \row \li Half-precision float (16-bit) \li Yes
+ \row \li Single-precision float (32-bit) \li Yes
+ \row \li Double-precision float (64-bit) \li Yes
+ \row \li Infinities and NaN floating point \li Yes
+ \row \li Determinate-length arrays and maps \li Yes
+ \row \li Indeterminate-length arrays and maps \li Yes
+ \row \li Map key types other than strings and integers \li Yes (arbitrary)
+ \endtable
+
+ \section2 Canonical CBOR encoding
+
+ Canonical CBOR encoding is defined by
+ \l{https://tools.ietf.org/html/rfc7049#section-3.9}{Section 3.9 of RFC
+ 7049}. Canonical encoding is not a requirement for Qt's CBOR decoding
+ functionality, but it may be required for some protocols. In particular,
+ protocols that require the ability to reproduce the same stream identically
+ may require this.
+
+ In order to be considered "canonical", a CBOR stream must meet the
+ following requirements:
+
+ \list
+ \li Integers must be as small as possible. QCborStreamWriter always
+ does this (no user action is required and it is not possible
+ to write overlong integers).
+ \li Array, map and string lengths must be as short as possible. As
+ above, QCborStreamWriter automatically does this.
+ \li Arrays, maps and strings must use explicit length. QCborStreamWriter
+ always does this for strings; for arrays and maps, be sure to call
+ startArray() and startMap() overloads with explicit length.
+ \li Keys in every map must be sorted in ascending order. QCborStreamWriter
+ offers no help in this item: the developer must ensure that before
+ calling append() for the map pairs.
+ \li Floating point values should be as small as possible. QCborStreamWriter
+ will not convert floating point values; it is up to the developer
+ to perform this check prior to calling append() (see those functions'
+ examples).
+ \endlist
+
+ \section2 Strict CBOR mode
+
+ Strict mode is defined by
+ \l{https://tools.ietf.org/html/rfc7049#section-3.10}{Section 3.10 of RFC
+ 7049}. As for Canonical encoding above, QCborStreamWriter makes it possible
+ to create strict CBOR streams, but does not require them or validate that
+ the output is so.
+
+ \list
+ \li Keys in a map must be unique. QCborStreamWriter performs no validation
+ of map keys.
+ \li Tags may be required to be paired only with the correct types,
+ according to their specification. QCborStreamWriter performs no
+ validation of tag usage.
+ \li Text Strings must be properly-encoded UTF-8. QCborStreamWriter always
+ writes proper UTF-8 for strings added with append(), but performs no
+ validation for strings added with appendTextString().
+ \endlist
+
+ \section2 Invalid CBOR stream
+
+ It is also possible to misuse QCborStreamWriter and produce invalid CBOR
+ streams that will fail to be decoded by a receiver. The following actions
+ will produce invalid streams:
+
+ \list
+ \li Append a tag and not append the corresponding tagged value
+ (QCborStreamWriter produces no diagnostic).
+ \li Append too many or too few items to an array or map with explicit
+ length (endMap() and endArray() will return false and
+ QCborStreamWriter will log with qWarning()).
+ \endlist
+
+ \sa QCborStreamReader, QCborValue, QXmlStreamWriter
+ */
+
+class QCborStreamWriterPrivate
+{
+public:
+ static Q_CONSTEXPR quint64 IndefiniteLength = (std::numeric_limits<quint64>::max)();
+
+ QIODevice *device;
+ CborEncoder encoder;
+ QStack<CborEncoder> containerStack;
+ bool deleteDevice = false;
+
+ QCborStreamWriterPrivate(QIODevice *device)
+ : device(device)
+ {
+ cbor_encoder_init_writer(&encoder, qt_cbor_encoder_write_callback, this);
+ }
+
+ ~QCborStreamWriterPrivate()
+ {
+ if (deleteDevice)
+ delete device;
+ }
+
+ template <typename... Args> void executeAppend(CborError (*f)(CborEncoder *, Args...), Args... args)
+ {
+ f(&encoder, std::forward<Args>(args)...);
+ }
+
+ void createContainer(CborError (*f)(CborEncoder *, CborEncoder *, size_t), quint64 len = IndefiniteLength)
+ {
+ Q_STATIC_ASSERT(size_t(IndefiniteLength) == CborIndefiniteLength);
+ if (sizeof(len) != sizeof(size_t) && len != IndefiniteLength) {
+ if (Q_UNLIKELY(len >= CborIndefiniteLength)) {
+ // TinyCBOR can't do this in 32-bit mode
+ qWarning("QCborStreamWriter: container of size %llu is too big for a 32-bit build; "
+ "will use indeterminate length instead", len);
+ len = CborIndefiniteLength;
+ }
+ }
+
+ containerStack.push(encoder);
+ f(&containerStack.top(), &encoder, len);
+ }
+
+ bool closeContainer()
+ {
+ if (containerStack.isEmpty()) {
+ qWarning("QCborStreamWriter: closing map or array that wasn't open");
+ return false;
+ }
+
+ CborEncoder container = containerStack.pop();
+ CborError err = cbor_encoder_close_container(&container, &encoder);
+ encoder = container;
+
+ if (Q_UNLIKELY(err)) {
+ if (err == CborErrorTooFewItems)
+ qWarning("QCborStreamWriter: not enough items added to array or map");
+ else if (err == CborErrorTooManyItems)
+ qWarning("QCborStreamWriter: too many items added to array or map");
+ return false;
+ }
+
+ return true;
+ }
+};
+
+static CborError qt_cbor_encoder_write_callback(void *self, const void *data, size_t len, CborEncoderAppendType)
+{
+ auto that = static_cast<QCborStreamWriterPrivate *>(self);
+ if (!that->device)
+ return CborNoError;
+ qint64 written = that->device->write(static_cast<const char *>(data), len);
+ return (written == qsizetype(len) ? CborNoError : CborErrorIO);
+}
+
+/*!
+ Creates a QCborStreamWriter object that will write the stream to \a device.
+ The device must be opened before the first append() call is made. This
+ constructor can be used with any class that derives from QIODevice, such as
+ QFile, QProcess or QTcpSocket.
+
+ QCborStreamWriter has no buffering, so every append() call will result in
+ one or more calls to the device's \l {QIODevice::}{write()} method.
+
+ The following example writes an empty map to a file:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 2
+
+ QCborStreamWriter does not take ownership of \a device.
+
+ \sa device(), setDevice()
+ */
+QCborStreamWriter::QCborStreamWriter(QIODevice *device)
+ : d(new QCborStreamWriterPrivate(device))
+{
+}
+
+/*!
+ Creates a QCborStreamWriter object that will append the stream to \a data.
+ All streaming is done immediately to the byte array, without the need for
+ flushing any buffers.
+
+ The following example writes a number to a byte array then returns
+ it.
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 3
+
+ QCborStreamWriter does not take ownership of \a data.
+ */
+QCborStreamWriter::QCborStreamWriter(QByteArray *data)
+ : d(new QCborStreamWriterPrivate(new QBuffer(data)))
+{
+ d->deleteDevice = true;
+ d->device->open(QIODevice::WriteOnly | QIODevice::Unbuffered);
+}
+
+/*!
+ Destroys this QCborStreamWriter object and frees any resources associated.
+
+ QCborStreamWriter does not perform error checking to see if all required
+ items were written to the stream prior to the object being destroyed. It is
+ the programmer's responsibility to ensure that it was done.
+ */
+QCborStreamWriter::~QCborStreamWriter()
+{
+}
+
+/*!
+ Replaces the device or byte array that this QCborStreamWriter object is
+ writing to with \a device.
+
+ \sa device()
+ */
+void QCborStreamWriter::setDevice(QIODevice *device)
+{
+ if (d->deleteDevice)
+ delete d->device;
+ d->device = device;
+ d->deleteDevice = false;
+}
+
+/*!
+ Returns the QIODevice that this QCborStreamWriter object is writing to. The
+ device must have previously been set with either the constructor or with
+ setDevice().
+
+ If this object was created by writing to a QByteArray, this function will
+ return an internal instance of QBuffer, which is owned by QCborStreamWriter.
+
+ \sa setDevice()
+ */
+QIODevice *QCborStreamWriter::device() const
+{
+ return d->device;
+}
+
+/*!
+ \overload
+
+ Appends the 64-bit unsigned value \a u to the CBOR stream, creating a CBOR
+ Unsigned Integer value. In the following example, we write the values 0,
+ 2\sup{32} and \c UINT64_MAX:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 4
+
+ \sa QCborStreamReader::isUnsignedInteger(), QCborStreamReader::toUnsignedInteger()
+ */
+void QCborStreamWriter::append(quint64 u)
+{
+ d->executeAppend(cbor_encode_uint, uint64_t(u));
+}
+
+/*!
+ \overload
+
+ Appends the 64-bit signed value \a i to the CBOR stream. This will create
+ either a CBOR Unsigned Integer or CBOR NegativeInteger value based on the
+ sign of the parameter. In the following example, we write the values 0, -1,
+ 2\sup{32} and \c INT64_MAX:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 5
+
+ \sa QCborStreamReader::isInteger(), QCborStreamReader::toInteger()
+ */
+void QCborStreamWriter::append(qint64 i)
+{
+ d->executeAppend(cbor_encode_int, int64_t(i));
+}
+
+/*!
+ \overload
+
+ Appends the 64-bit negative value \a n to the CBOR stream.
+ QCborNegativeInteger is a 64-bit enum that holds the absolute value of the
+ negative number we want to write. If n is zero, the value written will be
+ equivalent to 2\sup{64} (that is, -18,446,744,073,709,551,616).
+
+ In the following example, we write the values -1, -2\sup{32} and INT64_MIN:
+ \snippet code/src_corelib_serialization_qcborstream.cpp 6
+
+ Note how this function can be used to encode numbers that cannot fit a
+ standard computer's 64-bit signed integer like \l qint64. That is, if \a n
+ is larger than \c{std::numeric_limits<qint64>::max()} or is 0, this will
+ represent a negative number smaller than
+ \c{std::numeric_limits<qint64>::min()}.
+
+ \sa QCborStreamReader::isNegativeInteger(), QCborStreamReader::toNegativeInteger()
+ */
+void QCborStreamWriter::append(QCborNegativeInteger n)
+{
+ d->executeAppend(cbor_encode_negative_int, uint64_t(n));
+}
+
+/*!
+ \fn void QCborStreamWriter::append(const QByteArray &ba)
+ \overload
+
+ Appends the byte array \a ba to the stream, creating a CBOR Byte String
+ value. QCborStreamWriter will attempt to write the entire string in one
+ chunk.
+
+ The following example will load and append the contents of a file to the
+ stream:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 7
+
+ As the example shows, unlike JSON, CBOR requires no escaping for binary
+ content.
+
+ \sa appendByteString(), QCborStreamReader::isByteArray(),
+ QCborStreamReader::readByteArray()
+ */
+
+/*!
+ \overload
+
+ Appends the text string \a str to the stream, creating a CBOR Text String
+ value. QCborStreamWriter will attempt to write the entire string in one
+ chunk.
+
+ The following example appends a simple string to the stream:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 8
+
+ \b{Performance note}: CBOR requires that all Text Strings be encoded in
+ UTF-8, so this function will iterate over the characters in the string to
+ determine whether the contents are US-ASCII or not. If the string is found
+ to contain characters outside of US-ASCII, it will allocate memory and
+ convert to UTF-8. If this check is unnecessary, use appendTextString()
+ instead.
+
+ \sa QCborStreamReader::isString(), QCborStreamReader::readString()
+ */
+void QCborStreamWriter::append(QLatin1String str)
+{
+ // We've got Latin-1 but CBOR wants UTF-8, so check if the string is the
+ // common subset (US-ASCII).
+ if (QtPrivate::isAscii(str)) {
+ // it is plain US-ASCII
+ appendTextString(str.latin1(), str.size());
+ } else {
+ // non-ASCII, so we need a pass-through UTF-16
+ append(QString(str));
+ }
+}
+
+/*!
+ \overload
+
+ Appends the text string \a str to the stream, creating a CBOR Text String
+ value. QCborStreamWriter will attempt to write the entire string in one
+ chunk.
+
+ The following example writes an arbitrary QString to the stream:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 9
+
+ \sa QCborStreamReader::isString(), QCborStreamReader::readString()
+ */
+void QCborStreamWriter::append(QStringView str)
+{
+ QByteArray utf8 = str.toUtf8();
+ appendTextString(utf8.constData(), utf8.size());
+}
+
+/*!
+ \overload
+
+ Appends the CBOR tag \a tag to the stream, creating a CBOR Tag value. All
+ tags must be followed by another type which they provide meaning for.
+
+ In the following example, we append a CBOR Tag 36 (Regular Expression) and a
+ QRegularExpression's pattern to the stream:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 10
+
+ \sa QCborStreamReader::isTag(), QCborStreamReader::toTag()
+ */
+void QCborStreamWriter::append(QCborTag tag)
+{
+ d->executeAppend(cbor_encode_tag, CborTag(tag));
+}
+
+/*!
+ \fn void QCborStreamWriter::append(QCborKnownTags tag)
+ \overload
+
+ Appends the CBOR tag \a tag to the stream, creating a CBOR Tag value. All
+ tags must be followed by another type which they provide meaning for.
+
+ In the following example, we append a CBOR Tag 1 (Unix \c time_t) and an
+ integer representing the current time to the stream, obtained using the \c
+ time() function:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 11
+
+ \sa QCborStreamReader::isTag(), QCborStreamReader::toTag()
+ */
+
+/*!
+ \overload
+
+ Appends the CBOR simple type \a st to the stream, creating a CBOR Simple
+ Type value. In the following example, we write the simple type for Null as
+ well as for type 32, which Qt has no support for.
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 12
+
+ \note Using Simple Types for which there is no specification can lead to
+ validation errors by the remote receiver. In addition, simple type values 24
+ through 31 (inclusive) are reserved and must not be used.
+
+ \sa QCborStreamReader::isSimpleType(), QCborStreamReader::toSimpleType()
+ */
+void QCborStreamWriter::append(QCborSimpleType st)
+{
+ d->executeAppend(cbor_encode_simple_value, uint8_t(st));
+}
+
+#ifndef QT_BOOTSTRAPPED
+/*!
+ \overload
+
+ Appends the floating point number \a f to the stream, creating a CBOR 16-bit
+ Half-Precision Floating Point value. The following code can be used to convert
+ a C++ \tt float to \c qfloat16 if there's no loss of precision and append it, or
+ instead append the \tt float.
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 13
+
+ \sa QCborStreamReader::isFloat16(), QCborStreamReader::toFloat16()
+ */
+void QCborStreamWriter::append(qfloat16 f)
+{
+ d->executeAppend(cbor_encode_half_float, static_cast<const void *>(&f));
+}
+#endif // QT_BOOTSTRAPPED
+
+/*!
+ \overload
+
+ Appends the floating point number \a f to the stream, creating a CBOR 32-bit
+ Single-Precision Floating Point value. The following code can be used to convert
+ a C++ \tt double to \tt float if there's no loss of precision and append it, or
+ instead append the \tt double.
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 14
+
+ \sa QCborStreamReader::isFloat(), QCborStreamReader::toFloat()
+ */
+void QCborStreamWriter::append(float f)
+{
+ d->executeAppend(cbor_encode_float, f);
+}
+
+/*!
+ \overload
+
+ Appends the floating point number \a d to the stream, creating a CBOR 64-bit
+ Double-Precision Floating Point value. QCborStreamWriter always appends the
+ number as-is, performing no check for whether the number is the canonical
+ form for NaN, an infinite, whether it is denormal or if it could be written
+ with a shorter format.
+
+ The following code performs all those checks, except for the denormal one,
+ which is expected to be taken into account by the system FPU or floating
+ point emulation directly.
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 15
+
+ Determining if a double can be converted to an integral with no loss of
+ precision is left as an exercise to the reader.
+
+ \sa QCborStreamReader::isDouble(), QCborStreamReader::toDouble()
+ */
+void QCborStreamWriter::append(double d)
+{
+ this->d->executeAppend(cbor_encode_double, d);
+}
+
+/*!
+ Appends \a len bytes of data starting from \a data to the stream, creating a
+ CBOR Byte String value. QCborStreamWriter will attempt to write the entire
+ string in one chunk.
+
+ Unlike the QByteArray overload of append(), this function is not limited by
+ QByteArray's size limits. However, note that neither
+ QCborStreamReader::readByteArray() nor QCborValue support reading CBOR
+ streams with byte arrays larger than 2 GB.
+
+ \sa append(), appendTextString(),
+ QCborStreamReader::isByteArray(), QCborStreamReader::readByteArray()
+ */
+void QCborStreamWriter::appendByteString(const char *data, qsizetype len)
+{
+ d->executeAppend(cbor_encode_byte_string, reinterpret_cast<const uint8_t *>(data), size_t(len));
+}
+
+/*!
+ Appends \a len bytes of text starting from \a utf8 to the stream, creating a
+ CBOR Text String value. QCborStreamWriter will attempt to write the entire
+ string in one chunk.
+
+ The string pointed to by \a utf8 is expected to be properly encoded UTF-8.
+ QCborStreamWriter performs no validation that this is the case.
+
+ Unlike the QLatin1String overload of append(), this function is not limited
+ to 2 GB. However, note that neither QCborStreamReader::readString() nor
+ QCborValue support reading CBOR streams with text strings larger than 2 GB.
+
+ \sa append(QLatin1String), append(QStringView),
+ QCborStreamReader::isString(), QCborStreamReader::readString()
+ */
+void QCborStreamWriter::appendTextString(const char *utf8, qsizetype len)
+{
+ d->executeAppend(cbor_encode_text_string, utf8, size_t(len));
+}
+
+/*!
+ \fn void QCborStreamWriter::append(const char *str, qsizetype size)
+ \overload
+
+ Appends \a size bytes of text starting from \a str to the stream, creating a
+ CBOR Text String value. QCborStreamWriter will attempt to write the entire
+ string in one chunk. If \a size is -1, this function will write \c strlen(\a
+ str) bytes.
+
+ The string pointed to by \a str is expected to be properly encoded UTF-8.
+ QCborStreamWriter performs no validation that this is the case.
+
+ Unlike the QLatin1String overload of append(), this function is not limited
+ to 2 GB. However, note that neither QCborStreamReader nor QCborValue support
+ reading CBOR streams with text strings larger than 2 GB.
+
+ \sa append(QLatin1String), append(QStringView),
+ QCborStreamReader::isString(), QCborStreamReader::readString()
+ */
+
+/*!
+ \fn void QCborStreamWriter::append(bool b)
+ \overload
+
+ Appends the boolean value \a b to the stream, creating either a CBOR False
+ value or a CBOR True value. This function is equivalent to (and implemented
+ as):
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 16
+
+ \sa appendNull(), appendUndefined(),
+ QCborStreamReader::isBool(), QCborStreamReader::toBool()
+ */
+
+/*!
+ \fn void QCborStreamWriter::append(std::nullptr_t)
+ \overload
+
+ Appends a CBOR Null value to the stream. This function is equivalent to (and
+ implemented as): The parameter is ignored.
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 17
+
+ \sa appendNull(), append(QCborSimpleType), QCborStreamReader::isNull()
+ */
+
+/*!
+ \fn void QCborStreamWriter::appendNull()
+
+ Appends a CBOR Null value to the stream. This function is equivalent to (and
+ implemented as):
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 18
+
+ \sa append(std::nullptr_t), append(QCborSimpleType), QCborStreamReader::isNull()
+ */
+
+/*!
+ \fn void QCborStreamWriter::appendUndefined()
+
+ Appends a CBOR Undefined value to the stream. This function is equivalent to (and
+ implemented as):
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 19
+
+ \sa append(QCborSimpleType), QCborStreamReader::isUndefined()
+ */
+
+/*!
+ Starts a CBOR Array with indeterminate length in the CBOR stream. Each
+ startArray() call must be paired with one endArray() call and the current
+ CBOR element extends until the end of the array.
+
+ The array created by this function has no explicit length. Instead, its
+ length is implied by the elements contained in it. Note, however, that use
+ of indeterminate-length arrays is not compliant with canonical CBOR encoding.
+
+ The following example appends elements from the linked list of strings
+ passed as input:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 20
+
+ \sa startArray(quint64), endArray(), startMap(), QCborStreamReader::isArray(),
+ QCborStreamReader::isLengthKnown()
+ */
+void QCborStreamWriter::startArray()
+{
+ d->createContainer(cbor_encoder_create_array);
+}
+
+/*!
+ \overload
+
+ Starts a CBOR Array with explicit length of \a count items in the CBOR
+ stream. Each startArray call must be paired with one endArray() call and the
+ current CBOR element extends until the end of the array.
+
+ The array created by this function has an explicit length and therefore
+ exactly \a count items must be added to the CBOR stream. Adding fewer or
+ more items will result in failure during endArray() and the CBOR stream will
+ be corrupt. However, explicit-length arrays are required by canonical CBOR
+ encoding.
+
+ The following example appends all strings found in the \l QStringList passed as input:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 21
+
+ \b{Size limitations}: The parameter to this function is quint64, which would
+ seem to allow up to 2\sup{64}-1 elements in the array. However, both
+ QCborStreamWriter and QCborStreamReader are currently limited to 2\sup{32}-2
+ items on 32-bit systems and 2\sup{64}-2 items on 64-bit ones. Also note that
+ QCborArray is currently limited to 2\sup{27} elements in any platform.
+
+ \sa startArray(), endArray(), startMap(), QCborStreamReader::isArray(),
+ QCborStreamReader::isLengthKnown()
+ */
+void QCborStreamWriter::startArray(quint64 count)
+{
+ d->createContainer(cbor_encoder_create_array, count);
+}
+
+/*!
+ Terminates the array started by either overload of startArray() and returns
+ true if the correct number of elements was added to the array. This function
+ must be called for every startArray() used.
+
+ A return of false indicates error in the application and an unrecoverable
+ error in this stream. QCborStreamWriter also writes a warning using
+ qWarning() if that happens.
+
+ Calling this function when the current container is not an array is also an
+ error, though QCborStreamWriter cannot currently detect this condition.
+
+ \sa startArray(), startArray(quint64), endMap()
+ */
+bool QCborStreamWriter::endArray()
+{
+ return d->closeContainer();
+}
+
+/*!
+ Starts a CBOR Map with indeterminate length in the CBOR stream. Each
+ startMap() call must be paired with one endMap() call and the current CBOR
+ element extends until the end of the map.
+
+ The map created by this function has no explicit length. Instead, its length
+ is implied by the elements contained in it. Note, however, that use of
+ indeterminate-length maps is not compliant with canonical CBOR encoding
+ (canonical encoding also requires keys to be unique and in sorted order).
+
+ The following example appends elements from the linked list of int and
+ string pairs passed as input:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 22
+
+ \sa startMap(quint64), endMap(), startArray(), QCborStreamReader::isMap(),
+ QCborStreamReader::isLengthKnown()
+ */
+void QCborStreamWriter::startMap()
+{
+ d->createContainer(cbor_encoder_create_map);
+}
+
+/*!
+ \overload
+
+ Starts a CBOR Map with explicit length of \a count items in the CBOR
+ stream. Each startMap call must be paired with one endMap() call and the
+ current CBOR element extends until the end of the map.
+
+ The map created by this function has an explicit length and therefore
+ exactly \a count pairs of items must be added to the CBOR stream. Adding
+ fewer or more items will result in failure during endMap() and the CBOR
+ stream will be corrupt. However, explicit-length map are required by
+ canonical CBOR encoding.
+
+ The following example appends all strings found in the \l QMap passed as input:
+
+ \snippet code/src_corelib_serialization_qcborstream.cpp 23
+
+ \b{Size limitations}: The parameter to this function is quint64, which would
+ seem to allow up to 2\sup{64}-1 pairs in the map. However, both
+ QCborStreamWriter and QCborStreamReader are currently limited to 2\sup{31}-1
+ items on 32-bit systems and 2\sup{63}-1 items on 64-bit ones. Also note that
+ QCborMap is currently limited to 2\sup{26} elements in any platform.
+
+ \sa startMap(), endMap(), startArray(), QCborStreamReader::isMap(),
+ QCborStreamReader::isLengthKnown()
+ */
+void QCborStreamWriter::startMap(quint64 count)
+{
+ d->createContainer(cbor_encoder_create_map, count);
+}
+
+/*!
+ Terminates the map started by either overload of startMap() and returns
+ true if the correct number of elements was added to the array. This function
+ must be called for every startMap() used.
+
+ A return of false indicates error in the application and an unrecoverable
+ error in this stream. QCborStreamWriter also writes a warning using
+ qWarning() if that happens.
+
+ Calling this function when the current container is not a map is also an
+ error, though QCborStreamWriter cannot currently detect this condition.
+
+ \sa startMap(), startMap(quint64), endArray()
+ */
+bool QCborStreamWriter::endMap()
+{
+ return d->closeContainer();
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/serialization/qcborstreamwriter.h b/src/corelib/serialization/qcborstreamwriter.h
new file mode 100644
index 0000000000..f8c94ceb93
--- /dev/null
+++ b/src/corelib/serialization/qcborstreamwriter.h
@@ -0,0 +1,130 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 Intel Corporation.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCBORSTREAMWRITER_H
+#define QCBORSTREAMWRITER_H
+
+#include <QtCore/qbytearray.h>
+#include <QtCore/qcborcommon.h>
+#include <QtCore/qscopedpointer.h>
+#include <QtCore/qstring.h>
+#include <QtCore/qstringview.h>
+#ifndef QT_BOOTSTRAPPED
+#include <QtCore/qfloat16.h>
+#endif
+
+QT_REQUIRE_CONFIG(cborstreamwriter);
+
+// See qcborcommon.h for why we check
+#if defined(QT_X11_DEFINES_FOUND)
+# undef True
+# undef False
+#endif
+
+QT_BEGIN_NAMESPACE
+
+class QIODevice;
+
+class QCborStreamWriterPrivate;
+class Q_CORE_EXPORT QCborStreamWriter
+{
+public:
+ explicit QCborStreamWriter(QIODevice *device);
+ explicit QCborStreamWriter(QByteArray *data);
+ ~QCborStreamWriter();
+ Q_DISABLE_COPY(QCborStreamWriter)
+
+ void setDevice(QIODevice *device);
+ QIODevice *device() const;
+
+ void append(quint64 u);
+ void append(qint64 i);
+ void append(QCborNegativeInteger n);
+ void append(const QByteArray &ba) { appendByteString(ba.constData(), ba.size()); }
+ void append(QLatin1String str);
+ void append(QStringView str);
+ void append(QCborTag tag);
+ void append(QCborKnownTags tag) { append(QCborTag(tag)); }
+ void append(QCborSimpleType st);
+ void append(std::nullptr_t) { append(QCborSimpleType::Null); }
+#ifndef QT_BOOTSTRAPPED
+ void append(qfloat16 f);
+#endif
+ void append(float f);
+ void append(double d);
+
+ void appendByteString(const char *data, qsizetype len);
+ void appendTextString(const char *utf8, qsizetype len);
+
+ // convenience
+ void append(bool b) { append(b ? QCborSimpleType::True : QCborSimpleType::False); }
+ void appendNull() { append(QCborSimpleType::Null); }
+ void appendUndefined() { append(QCborSimpleType::Undefined); }
+
+#ifndef Q_QDOC
+ // overloads to make normal code not complain
+ void append(int i) { append(qint64(i)); }
+ void append(uint u) { append(quint64(u)); }
+#endif
+#ifndef QT_NO_CAST_FROM_ASCII
+ void append(const char *str, qsizetype size = -1)
+ { appendTextString(str, (str && size == -1) ? int(strlen(str)) : size); }
+#endif
+
+ void startArray();
+ void startArray(quint64 count);
+ bool endArray();
+ void startMap();
+ void startMap(quint64 count);
+ bool endMap();
+
+ // no API for encoding chunked strings
+
+private:
+ QScopedPointer<QCborStreamWriterPrivate> d;
+};
+
+QT_END_NAMESPACE
+
+#if defined(QT_X11_DEFINES_FOUND)
+# define True 1
+# define False 0
+#endif
+
+#endif // QCBORSTREAMWRITER_H
diff --git a/src/corelib/serialization/qcborvalue.cpp b/src/corelib/serialization/qcborvalue.cpp
index c2f670de06..db2840704c 100644
--- a/src/corelib/serialization/qcborvalue.cpp
+++ b/src/corelib/serialization/qcborvalue.cpp
@@ -43,8 +43,12 @@
#include "qcborarray.h"
#include "qcbormap.h"
-#if QT_CONFIG(cborstream)
-#include "qcborstream.h"
+#if QT_CONFIG(cborstreamreader)
+#include "qcborstreamreader.h"
+#endif
+
+#if QT_CONFIG(cborstreamwriter)
+#include "qcborstreamwriter.h"
#endif
#include <qendian.h>
@@ -761,16 +765,99 @@ QT_BEGIN_NAMESPACE
using namespace QtCbor;
-#if QT_CONFIG(cborstream)
+static QCborValue::Type convertToExtendedType(QCborContainerPrivate *d)
+{
+ qint64 tag = d->elements.at(0).value;
+ auto &e = d->elements[1];
+ const ByteData *b = d->byteData(e);
+
+ auto replaceByteData = [&](const char *buf, qsizetype len, Element::ValueFlags f) {
+ d->data.clear();
+ d->usedData = 0;
+ e.flags = Element::HasByteData | f;
+ e.value = d->addByteData(buf, len);
+ };
+
+ switch (tag) {
+ case qint64(QCborKnownTags::DateTimeString):
+ case qint64(QCborKnownTags::UnixTime_t): {
+ QDateTime dt;
+ if (tag == qint64(QCborKnownTags::DateTimeString) && b &&
+ e.type == QCborValue::String && (e.flags & Element::StringIsUtf16) == 0) {
+ // The data is supposed to be US-ASCII. If it isn't (contains UTF-8),
+ // QDateTime::fromString will fail anyway.
+ dt = QDateTime::fromString(b->asLatin1(), Qt::ISODateWithMs);
+ } else if (tag == qint64(QCborKnownTags::UnixTime_t) && e.type == QCborValue::Integer) {
+ dt = QDateTime::fromSecsSinceEpoch(e.value, Qt::UTC);
+ } else if (tag == qint64(QCborKnownTags::UnixTime_t) && e.type == QCborValue::Double) {
+ dt = QDateTime::fromMSecsSinceEpoch(qint64(e.fpvalue() * 1000), Qt::UTC);
+ }
+ if (dt.isValid()) {
+ QByteArray text = dt.toString(Qt::ISODateWithMs).toLatin1();
+ replaceByteData(text, text.size(), Element::StringIsAscii);
+ e.type = QCborValue::String;
+ d->elements[0].value = qint64(QCborKnownTags::DateTimeString);
+ return QCborValue::DateTime;
+ }
+ break;
+ }
+
+#ifndef QT_BOOTSTRAPPED
+ case qint64(QCborKnownTags::Url):
+ if (e.type == QCborValue::String) {
+ if (b) {
+ // normalize to a short (decoded) form, so as to save space
+ QUrl url(e.flags & Element::StringIsUtf16 ?
+ b->asQStringRaw() :
+ b->toUtf8String());
+ QByteArray encoded = url.toString(QUrl::DecodeReserved).toUtf8();
+ replaceByteData(encoded, encoded.size(), {});
+ }
+ return QCborValue::Url;
+ }
+ break;
+#endif // QT_BOOTSTRAPPED
+
+#if QT_CONFIG(regularexpression)
+ case quint64(QCborKnownTags::RegularExpression):
+ if (e.type == QCborValue::String) {
+ // no normalization is necessary
+ return QCborValue::RegularExpression;
+ }
+ break;
+#endif // QT_CONFIG(regularexpression)
+
+ case qint64(QCborKnownTags::Uuid):
+ if (e.type == QCborValue::ByteArray) {
+ // force the size to 16
+ char buf[sizeof(QUuid)] = {};
+ if (b)
+ memcpy(buf, b->byte(), qMin(sizeof(buf), size_t(b->len)));
+ replaceByteData(buf, sizeof(buf), {});
+
+ return QCborValue::Uuid;
+ }
+ break;
+ }
+
+ // no enriching happened
+ return QCborValue::Tag;
+}
+
+#if QT_CONFIG(cborstreamreader)
// in qcborstream.cpp
extern void qt_cbor_stream_set_error(QCborStreamReaderPrivate *d, QCborError error);
+#endif
+#if QT_CONFIG(cborstreamwriter)
static void writeDoubleToCbor(QCborStreamWriter &writer, double d, QCborValue::EncodingOptions opt)
{
if (qt_is_nan(d)) {
- if (opt & QCborValue::UseFloat16) {
+ if (opt & QCborValue::UseFloat) {
+#ifndef QT_BOOTSTRAPPED
if ((opt & QCborValue::UseFloat16) == QCborValue::UseFloat16)
return writer.append(std::numeric_limits<qfloat16>::quiet_NaN());
+#endif
return writer.append(std::numeric_limits<float>::quiet_NaN());
}
return writer.append(qt_qnan());
@@ -787,15 +874,17 @@ static void writeDoubleToCbor(QCborStreamWriter &writer, double d, QCborValue::E
}
}
- if (opt & QCborValue::UseFloat16) {
+ if (opt & QCborValue::UseFloat) {
float f = float(d);
if (f == d) {
// no data loss, we could use float
+#ifndef QT_BOOTSTRAPPED
if ((opt & QCborValue::UseFloat16) == QCborValue::UseFloat16) {
qfloat16 f16 = f;
if (f16 == f)
return writer.append(f16);
}
+#endif
return writer.append(f);
}
@@ -803,7 +892,7 @@ static void writeDoubleToCbor(QCborStreamWriter &writer, double d, QCborValue::E
writer.append(d);
}
-#endif // QT_CONFIG(cborstream)
+#endif // QT_CONFIG(cborstreamwriter)
static inline int typeOrder(Element e1, Element e2)
{
@@ -1226,7 +1315,7 @@ int QCborMap::compare(const QCborMap &other) const noexcept
return compareContainer(d.data(), other.d.data());
}
-#if QT_CONFIG(cborstream)
+#if QT_CONFIG(cborstreamwriter)
static void encodeToCbor(QCborStreamWriter &writer, const QCborContainerPrivate *d, qsizetype idx,
QCborValue::EncodingOptions opt)
{
@@ -1314,7 +1403,9 @@ static void encodeToCbor(QCborStreamWriter &writer, const QCborContainerPrivate
qWarning("QCborValue: found unknown type 0x%x", e.type);
}
}
+#endif // QT_CONFIG(cborstreamwriter)
+#if QT_CONFIG(cborstreamreader)
static inline double integerOutOfRange(const QCborStreamReader &reader)
{
Q_ASSERT(reader.isInteger());
@@ -1390,77 +1481,10 @@ static QCborValue taggedValueFromCbor(QCborStreamReader &reader)
d->decodeValueFromCbor(reader);
}
- QCborValue::Type type = QCborValue::Tag;
+ QCborValue::Type type;
if (reader.lastError() == QCborError::NoError) {
// post-process to create our extended types
- qint64 tag = d->elements.at(0).value;
- auto &e = d->elements[1];
- const ByteData *b = d->byteData(e);
-
- auto replaceByteData = [&](const char *buf, qsizetype len) {
- d->data.clear();
- d->usedData = 0;
- e.flags = Element::HasByteData | Element::StringIsAscii;
- e.value = d->addByteData(buf, len);
- };
-
- switch (tag) {
- case qint64(QCborKnownTags::DateTimeString):
- case qint64(QCborKnownTags::UnixTime_t): {
- QDateTime dt;
- if (tag == qint64(QCborKnownTags::DateTimeString) && b &&
- e.type == QCborValue::String && (e.flags & Element::StringIsUtf16) == 0) {
- // The data is supposed to be US-ASCII. If it isn't,
- // QDateTime::fromString will fail anyway.
- dt = QDateTime::fromString(b->asLatin1(), Qt::ISODateWithMs);
- } else if (tag == qint64(QCborKnownTags::UnixTime_t) && e.type == QCborValue::Integer) {
- dt = QDateTime::fromSecsSinceEpoch(e.value, Qt::UTC);
- } else if (tag == qint64(QCborKnownTags::UnixTime_t) && e.type == QCborValue::Double) {
- dt = QDateTime::fromMSecsSinceEpoch(qint64(e.fpvalue() * 1000), Qt::UTC);
- }
- if (dt.isValid()) {
- QByteArray text = dt.toString(Qt::ISODateWithMs).toLatin1();
- replaceByteData(text, text.size());
- e.type = QCborValue::String;
- d->elements[0].value = qint64(QCborKnownTags::DateTimeString);
- type = QCborValue::DateTime;
- }
- break;
- }
-
- case qint64(QCborKnownTags::Url):
- if (e.type == QCborValue::String) {
- if (b) {
- // normalize to a short (decoded) form, so as to save space
- QUrl url(e.flags & Element::StringIsUtf16 ?
- b->asQStringRaw() :
- b->toUtf8String());
- QByteArray encoded = url.toString(QUrl::DecodeReserved).toUtf8();
- replaceByteData(encoded, encoded.size());
- }
- type = QCborValue::Url;
- }
- break;
-
- case quint64(QCborKnownTags::RegularExpression):
- if (e.type == QCborValue::String) {
- // no normalization is necessary
- type = QCborValue::RegularExpression;
- }
- break;
-
- case qint64(QCborKnownTags::Uuid):
- if (e.type == QCborValue::ByteArray) {
- // force the size to 16
- char buf[sizeof(QUuid)] = {};
- if (b)
- memcpy(buf, b->byte(), qMin(sizeof(buf), size_t(b->len)));
- replaceByteData(buf, sizeof(buf));
-
- type = QCborValue::Uuid;
- }
- break;
- }
+ type = convertToExtendedType(d);
} else {
// decoding error
type = QCborValue::Invalid;
@@ -1638,7 +1662,7 @@ void QCborContainerPrivate::decodeFromCbor(QCborStreamReader &reader)
if (reader.lastError() == QCborError::NoError)
reader.leaveContainer();
}
-#endif // QT_CONFIG(cborstream)
+#endif // QT_CONFIG(cborstreamreader)
/*!
Creates a QCborValue with byte array value \a ba. The value can later be
@@ -1724,21 +1748,22 @@ QCborValue::QCborValue(const QCborMap &m)
}
/*!
- \fn QCborValue::QCborValue(QCborTag t, const QCborValue &tv)
- \fn QCborValue::QCborValue(QCborKnownTags t, const QCborValue &tv)
+ \fn QCborValue::QCborValue(QCborTag tag, const QCborValue &tv)
+ \fn QCborValue::QCborValue(QCborKnownTags tag, const QCborValue &tv)
Creates a QCborValue for the extended type represented by the tag value \a
- t, tagging value \a tv. The tag can later be retrieved using tag() and
+ tag, tagging value \a tv. The tag can later be retrieved using tag() and
the tagged value using taggedValue().
\sa isTag(), tag(), taggedValue(), QCborKnownTags
*/
-QCborValue::QCborValue(QCborTag t, const QCborValue &tv)
+QCborValue::QCborValue(QCborTag tag, const QCborValue &tv)
: n(-1), container(new QCborContainerPrivate), t(Tag)
{
container->ref.storeRelaxed(1);
- container->append(t);
+ container->append(tag);
container->append(tv);
+ t = convertToExtendedType(container);
}
/*!
@@ -2337,7 +2362,7 @@ QCborValueRef QCborValue::operator[](qint64 key)
return { container, index };
}
-#if QT_CONFIG(cborstream)
+#if QT_CONFIG(cborstreamreader)
/*!
Decodes one item from the CBOR stream found in \a reader and returns the
equivalent representation. This function is recursive: if the item is a map
@@ -2452,7 +2477,9 @@ QCborValue QCborValue::fromCbor(const QByteArray &ba, QCborParserError *error)
overload of this function that accepts a QByteArray, also passing \a error,
if provided.
*/
+#endif // QT_CONFIG(cborstreamreader)
+#if QT_CONFIG(cborstreamwriter)
/*!
Encodes this QCborValue object to its CBOR representation, using the
options specified in \a opt, and return the byte array containing that
@@ -2575,7 +2602,7 @@ void QCborValueRef::toCbor(QCborStreamWriter &writer, QCborValue::EncodingOption
{
concrete().toCbor(writer, opt);
}
-#endif // QT_CONFIG(cborstream)
+#endif // QT_CONFIG(cborstreamwriter)
void QCborValueRef::assign(QCborValueRef that, const QCborValue &other)
{
diff --git a/src/corelib/serialization/qcborvalue.h b/src/corelib/serialization/qcborvalue.h
index 9c613dfcfb..f7064ac6e1 100644
--- a/src/corelib/serialization/qcborvalue.h
+++ b/src/corelib/serialization/qcborvalue.h
@@ -59,7 +59,7 @@
# undef False
#endif
-#if 0 && QT_HAS_INCLUDE(<compare>)
+#if 0 && __has_include(<compare>)
# include <compare>
#endif
@@ -90,7 +90,9 @@ public:
enum EncodingOption {
SortKeysInMaps = 0x01,
UseFloat = 0x02,
+#ifndef QT_BOOTSTRAPPED
UseFloat16 = UseFloat | 0x04,
+#endif
UseIntegers = 0x08,
NoTransformation = 0
@@ -265,7 +267,7 @@ public:
QCborValueRef operator[](const QString & key);
int compare(const QCborValue &other) const;
-#if 0 && QT_HAS_INCLUDE(<compare>)
+#if 0 && __has_include(<compare>)
std::strong_ordering operator<=>(const QCborValue &other) const
{
int c = compare(other);
@@ -287,13 +289,15 @@ public:
static QCborValue fromJsonValue(const QJsonValue &v);
QJsonValue toJsonValue() const;
-#if QT_CONFIG(cborstream)
+#if QT_CONFIG(cborstreamreader)
static QCborValue fromCbor(QCborStreamReader &reader);
static QCborValue fromCbor(const QByteArray &ba, QCborParserError *error = nullptr);
static QCborValue fromCbor(const char *data, qsizetype len, QCborParserError *error = nullptr)
{ return fromCbor(QByteArray(data, int(len)), error); }
static QCborValue fromCbor(const quint8 *data, qsizetype len, QCborParserError *error = nullptr)
{ return fromCbor(QByteArray(reinterpret_cast<const char *>(data), int(len)), error); }
+#endif // QT_CONFIG(cborstreamreader)
+#if QT_CONFIG(cborstreamwriter)
QByteArray toCbor(EncodingOptions opt = NoTransformation);
void toCbor(QCborStreamWriter &writer, EncodingOptions opt = NoTransformation);
#endif
@@ -328,9 +332,9 @@ private:
return Type(quint8(st) | SimpleType);
}
- Q_DECL_CONSTEXPR static bool isTag_helper(Type t)
+ Q_DECL_CONSTEXPR static bool isTag_helper(Type tt)
{
- return t == Tag || t >= 0x10000;
+ return tt == Tag || tt >= 0x10000;
}
};
Q_DECLARE_SHARED(QCborValue)
@@ -421,7 +425,7 @@ public:
int compare(const QCborValue &other) const
{ return concrete().compare(other); }
-#if 0 && QT_HAS_INCLUDE(<compare>)
+#if 0 && __has_include(<compare>)
std::strong_ordering operator<=>(const QCborValue &other) const
{
int c = compare(other);
@@ -441,7 +445,7 @@ public:
QVariant toVariant() const { return concrete().toVariant(); }
QJsonValue toJsonValue() const;
-#if QT_CONFIG(cborstream)
+#if QT_CONFIG(cborstreamwriter)
QByteArray toCbor(QCborValue::EncodingOptions opt = QCborValue::NoTransformation)
{ return concrete().toCbor(opt); }
void toCbor(QCborStreamWriter &writer, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
diff --git a/src/corelib/serialization/qcborvalue_p.h b/src/corelib/serialization/qcborvalue_p.h
index d18d108969..53ba81fa18 100644
--- a/src/corelib/serialization/qcborvalue_p.h
+++ b/src/corelib/serialization/qcborvalue_p.h
@@ -196,8 +196,7 @@ public:
if (value.container)
return replaceAt_complex(e, value, disp);
- e.value = value.value_helper();
- e.type = value.type();
+ e = { value.value_helper(), value.type() };
if (value.isContainer())
e.container = nullptr;
}
@@ -236,6 +235,15 @@ public:
elements.append(QtCbor::Element(addByteData(data, len), type,
QtCbor::Element::HasByteData | extraFlags));
}
+ void appendAsciiString(const QString &s);
+ void appendAsciiString(const char *str, qsizetype len)
+ {
+ appendByteData(str, len, QCborValue::String, QtCbor::Element::StringIsAscii);
+ }
+ void appendUtf8String(const char *str, qsizetype len)
+ {
+ appendByteData(str, len, QCborValue::String);
+ }
void append(QLatin1String s)
{
if (!QtPrivate::isAscii(s))
diff --git a/src/corelib/serialization/qdatastream.h b/src/corelib/serialization/qdatastream.h
index 20d3953652..b7c16018d5 100644
--- a/src/corelib/serialization/qdatastream.h
+++ b/src/corelib/serialization/qdatastream.h
@@ -296,7 +296,10 @@ QDataStream &readAssociativeContainer(QDataStream &s, Container &c)
c.clear();
break;
}
+QT_WARNING_PUSH
+QT_WARNING_DISABLE_DEPRECATED
c.insertMulti(k, t);
+QT_WARNING_POP
}
return s;
@@ -316,14 +319,31 @@ template <typename Container>
QDataStream &writeAssociativeContainer(QDataStream &s, const Container &c)
{
s << quint32(c.size());
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && QT_DEPRECATED_SINCE(5, 15)
// Deserialization should occur in the reverse order.
// Otherwise, value() will return the least recently inserted
// value instead of the most recently inserted one.
auto it = c.constEnd();
auto begin = c.constBegin();
while (it != begin) {
+ QT_WARNING_PUSH
+ QT_WARNING_DISABLE_DEPRECATED
--it;
+ QT_WARNING_POP
s << it.key() << it.value();
+#else
+ auto it = c.constBegin();
+ auto end = c.constEnd();
+ while (it != end) {
+ const auto rangeStart = it++;
+ while (it != end && rangeStart.key() == it.key())
+ ++it;
+ const qint64 last = std::distance(rangeStart, it) - 1;
+ for (qint64 i = last; i >= 0; --i) {
+ auto next = std::next(rangeStart, i);
+ s << next.key() << next.value();
+ }
+#endif
}
return s;
diff --git a/src/corelib/serialization/qjsonarray.cpp b/src/corelib/serialization/qjsonarray.cpp
index c5d4bf0315..08702771a8 100644
--- a/src/corelib/serialization/qjsonarray.cpp
+++ b/src/corelib/serialization/qjsonarray.cpp
@@ -175,12 +175,6 @@ void QJsonArray::initialize()
*/
QJsonArray::~QJsonArray() = default;
-/*!
- Creates a copy of \a other.
-
- Since QJsonArray is implicitly shared, the copy is shallow
- as long as the object doesn't get modified.
- */
QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args)
{
initialize();
@@ -188,6 +182,12 @@ QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args)
append(arg);
}
+/*!
+ Creates a copy of \a other.
+
+ Since QJsonArray is implicitly shared, the copy is shallow
+ as long as the object doesn't get modified.
+ */
QJsonArray::QJsonArray(const QJsonArray &other)
{
a = other.a;
diff --git a/src/corelib/serialization/qjsoncbor.cpp b/src/corelib/serialization/qjsoncbor.cpp
index 997cef0106..fbea84fe73 100644
--- a/src/corelib/serialization/qjsoncbor.cpp
+++ b/src/corelib/serialization/qjsoncbor.cpp
@@ -755,13 +755,13 @@ QCborValue QCborValue::fromVariant(const QVariant &variant)
return QCborMap::fromJsonObject(doc.object());
}
case QMetaType::QCborValue:
- return variant.value<QCborValue>();
+ return qvariant_cast<QCborValue>(variant);
case QMetaType::QCborArray:
- return variant.value<QCborArray>();
+ return qvariant_cast<QCborArray>(variant);
case QMetaType::QCborMap:
- return variant.value<QCborMap>();
+ return qvariant_cast<QCborMap>(variant);
case QMetaType::QCborSimpleType:
- return variant.value<QCborSimpleType>();
+ return qvariant_cast<QCborSimpleType>(variant);
#endif
default:
break;
diff --git a/src/corelib/serialization/qjsondocument.cpp b/src/corelib/serialization/qjsondocument.cpp
index b9b1902f34..fe0500bdef 100644
--- a/src/corelib/serialization/qjsondocument.cpp
+++ b/src/corelib/serialization/qjsondocument.cpp
@@ -237,7 +237,7 @@ QJsonDocument &QJsonDocument::operator =(const QJsonDocument &other)
the application.
*/
-#if QT_CONFIG(binaryjson)
+#if QT_CONFIG(binaryjson) && QT_DEPRECATED_SINCE(5, 15)
/*!
Creates a QJsonDocument that uses the first \a size bytes from
\a data. It assumes \a data contains a binary encoded JSON document.
@@ -385,10 +385,13 @@ QJsonDocument QJsonDocument::fromBinaryData(const QByteArray &data, DataValidati
QByteArray QJsonDocument::toBinaryData() const
{
int size = 0;
+QT_WARNING_PUSH
+QT_WARNING_DISABLE_DEPRECATED
const char *raw = rawData(&size);
+QT_WARNING_POP
return QByteArray(raw, size);
}
-#endif // QT_CONFIG(binaryjson)
+#endif // QT_CONFIG(binaryjson) && QT_DEPRECATED_SINCE(5, 15)
/*!
Creates a QJsonDocument from the QVariant \a variant.
diff --git a/src/corelib/serialization/qjsondocument.h b/src/corelib/serialization/qjsondocument.h
index 325e47b531..758bbfd9dd 100644
--- a/src/corelib/serialization/qjsondocument.h
+++ b/src/corelib/serialization/qjsondocument.h
@@ -111,13 +111,19 @@ public:
BypassValidation
};
-#if QT_CONFIG(binaryjson)
+#if QT_CONFIG(binaryjson) && QT_DEPRECATED_SINCE(5, 15)
+ QT_DEPRECATED_X("Use CBOR format instead")
static QJsonDocument fromRawData(const char *data, int size, DataValidation validation = Validate);
+
+ QT_DEPRECATED_X("Use CBOR format instead")
const char *rawData(int *size) const;
+ QT_DEPRECATED_X("Use CBOR format instead")
static QJsonDocument fromBinaryData(const QByteArray &data, DataValidation validation = Validate);
+
+ QT_DEPRECATED_X("Use CBOR format instead")
QByteArray toBinaryData() const;
-#endif // QT_CONFIG(binaryjson)
+#endif // QT_CONFIG(binaryjson) && QT_DEPRECATED_SINCE(5, 15)
static QJsonDocument fromVariant(const QVariant &variant);
QVariant toVariant() const;
diff --git a/src/corelib/serialization/qjsonobject.cpp b/src/corelib/serialization/qjsonobject.cpp
index ae37481f31..a6987279d3 100644
--- a/src/corelib/serialization/qjsonobject.cpp
+++ b/src/corelib/serialization/qjsonobject.cpp
@@ -1013,7 +1013,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
Returns a pointer to a modifiable reference to the current item.
*/
-/*! \fn QJsonValueRef QJsonObject::iterator::operator[](int j) const
+/*! \fn const QJsonValueRef QJsonObject::iterator::operator[](int j)
Returns a modifiable reference to the item at offset \a j from the
item pointed to by this iterator (the item at position \c{*this + j}).
@@ -1264,7 +1264,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
Returns a pointer to the current item.
*/
-/*! \fn QJsonValue QJsonObject::const_iterator::operator[](int j) const
+/*! \fn const QJsonValue QJsonObject::const_iterator::operator[](int j)
Returns the item at offset \a j from the item pointed to by this iterator (the item at
position \c{*this + j}).
diff --git a/src/corelib/serialization/qjsonobject.h b/src/corelib/serialization/qjsonobject.h
index cfb44c9c55..c31be0353d 100644
--- a/src/corelib/serialization/qjsonobject.h
+++ b/src/corelib/serialization/qjsonobject.h
@@ -221,7 +221,7 @@ public:
inline const_iterator operator-(int j) const { return operator+(-j); }
inline const_iterator &operator+=(int j) { i += j; return *this; }
inline const_iterator &operator-=(int j) { i -= j; return *this; }
- int operator-(iterator j) const { return i - j.i; }
+ int operator-(const_iterator j) const { return i - j.i; }
inline bool operator==(const iterator &other) const { return i == other.i; }
inline bool operator!=(const iterator &other) const { return i != other.i; }
diff --git a/src/corelib/serialization/qjsonparser.cpp b/src/corelib/serialization/qjsonparser.cpp
index aab8112d7f..d7ce702ff7 100644
--- a/src/corelib/serialization/qjsonparser.cpp
+++ b/src/corelib/serialization/qjsonparser.cpp
@@ -894,9 +894,10 @@ bool Parser::parseString()
// no escape sequences, we are done
if (isUtf8) {
- container->appendByteData(start, json - start - 1, QCborValue::String,
- isAscii ? QtCbor::Element::StringIsAscii
- : QtCbor::Element::ValueFlags {});
+ if (isAscii)
+ container->appendAsciiString(start, json - start - 1);
+ else
+ container->appendUtf8String(start, json - start - 1);
END;
return true;
}
diff --git a/src/corelib/serialization/qjsonvalue.cpp b/src/corelib/serialization/qjsonvalue.cpp
index db06a33a9f..4a7fab21d7 100644
--- a/src/corelib/serialization/qjsonvalue.cpp
+++ b/src/corelib/serialization/qjsonvalue.cpp
@@ -149,7 +149,7 @@ QJsonValue::QJsonValue(bool b)
}
/*!
- Creates a value of type Double, with value \a n.
+ Creates a value of type Double, with value \a v.
*/
QJsonValue::QJsonValue(double v)
: d(nullptr)
@@ -166,7 +166,7 @@ QJsonValue::QJsonValue(double v)
/*!
\overload
- Creates a value of type Double, with value \a n.
+ Creates a value of type Double, with value \a v.
*/
QJsonValue::QJsonValue(int v)
: n(v), t(QCborValue::Integer)
@@ -175,7 +175,7 @@ QJsonValue::QJsonValue(int v)
/*!
\overload
- Creates a value of type Double, with value \a n.
+ Creates a value of type Double, with value \a v.
NOTE: the integer limits for IEEE 754 double precision data is 2^53 (-9007199254740992 to +9007199254740992).
If you pass in values outside this range expect a loss of precision to occur.
*/
@@ -490,11 +490,11 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant)
return doc.isArray() ? QJsonValue(doc.array()) : QJsonValue(doc.object());
}
case QMetaType::QCborValue:
- return variant.value<QCborValue>().toJsonValue();
+ return qvariant_cast<QCborValue>(variant).toJsonValue();
case QMetaType::QCborArray:
- return variant.value<QCborArray>().toJsonArray();
+ return qvariant_cast<QCborArray>(variant).toJsonArray();
case QMetaType::QCborMap:
- return variant.value<QCborMap>().toJsonObject();
+ return qvariant_cast<QCborMap>(variant).toJsonObject();
#endif
default:
break;
diff --git a/src/corelib/serialization/qjsonwriter.cpp b/src/corelib/serialization/qjsonwriter.cpp
index 31fb16c112..590b59f09c 100644
--- a/src/corelib/serialization/qjsonwriter.cpp
+++ b/src/corelib/serialization/qjsonwriter.cpp
@@ -60,7 +60,8 @@ static inline uchar hexdig(uint u)
static QByteArray escapedString(const QString &s)
{
- QByteArray ba(s.length(), Qt::Uninitialized);
+ // give it a minimum size to ensure the resize() below always adds enough space
+ QByteArray ba(qMax(s.length(), 16), Qt::Uninitialized);
uchar *cursor = reinterpret_cast<uchar *>(const_cast<char *>(ba.constData()));
const uchar *ba_end = cursor + ba.length();
diff --git a/src/corelib/serialization/qtextstream.cpp b/src/corelib/serialization/qtextstream.cpp
index cf59cc54c7..4d92b1e0da 100644
--- a/src/corelib/serialization/qtextstream.cpp
+++ b/src/corelib/serialization/qtextstream.cpp
@@ -2689,11 +2689,8 @@ QTextStream &QTextStream::operator<<(const void *ptr)
d->params.numberFlags = oldFlags;
return *this;
}
-#if defined(Q_QDOC) || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+
namespace Qt {
-#else
-namespace QTextStreamFunctions {
-#endif
/*!
\relates QTextStream
@@ -3020,7 +3017,7 @@ QTextStream &ws(QTextStream &stream)
return stream;
}
-} // namespace QTextStreamFunctions
+} // namespace Qt
/*!
\fn QTextStreamManipulator qSetFieldWidth(int width)
@@ -3045,11 +3042,7 @@ QTextStream &ws(QTextStream &stream)
#if QT_CONFIG(textcodec)
-#if defined(Q_QDOC) || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
namespace Qt {
-#else
-namespace QTextStreamFunctions {
-#endif
/*!
\relates QTextStream
@@ -3064,7 +3057,7 @@ QTextStream &bom(QTextStream &stream)
return stream;
}
-} // namespace QTextStreamFunctions
+} // namespace Qt
/*!
Sets the codec for this stream to \a codec. The codec is used for
@@ -3215,6 +3208,45 @@ QLocale QTextStream::locale() const
return d->locale;
}
+#if QT_DEPRECATED_SINCE(5, 15) && !defined(Q_QDOC)
+// Deprecated source compatible migration versions:
+namespace QTextStreamFunctions {
+QTextStream &bin(QTextStream &s) { return Qt::bin(s); }
+QTextStream &oct(QTextStream &s) { return Qt::oct(s); }
+QTextStream &dec(QTextStream &s) { return Qt::dec(s); }
+QTextStream &hex(QTextStream &s) { return Qt::hex(s); }
+
+QTextStream &showbase(QTextStream &s) { return Qt::showbase(s); }
+QTextStream &forcesign(QTextStream &s) { return Qt::forcesign(s); }
+QTextStream &forcepoint(QTextStream &s) { return Qt::forcepoint(s); }
+QTextStream &noshowbase(QTextStream &s) { return Qt::noshowbase(s); }
+QTextStream &noforcesign(QTextStream &s) { return Qt::noforcesign(s); }
+QTextStream &noforcepoint(QTextStream &s) { return Qt::noforcepoint(s); }
+
+QTextStream &uppercasebase(QTextStream &s) { return Qt::uppercasebase(s); }
+QTextStream &uppercasedigits(QTextStream &s) { return Qt::uppercasedigits(s); }
+QTextStream &lowercasebase(QTextStream &s) { return Qt::lowercasebase(s); }
+QTextStream &lowercasedigits(QTextStream &s) { return Qt::lowercasedigits(s); }
+
+QTextStream &fixed(QTextStream &s) { return Qt::fixed(s); }
+QTextStream &scientific(QTextStream &s) { return Qt::scientific(s); }
+
+QTextStream &left(QTextStream &s) { return Qt::left(s); }
+QTextStream &right(QTextStream &s) { return Qt::right(s); }
+QTextStream &center(QTextStream &s) { return Qt::center(s); }
+
+QTextStream &endl(QTextStream &s) { return Qt::endl(s); }
+QTextStream &flush(QTextStream &s) { return Qt::flush(s); }
+QTextStream &reset(QTextStream &s) { return Qt::reset(s); }
+
+QTextStream &ws(QTextStream &s) { return Qt::ws(s); }
+
+#if QT_CONFIG(textcodec)
+QTextStream &bom(QTextStream &s) { return Qt::bom(s); }
+#endif
+} // namespace QTextStreamFunctions
+#endif
+
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC)
// Binary compatible definitions for Qt<5.14
Q_CORE_EXPORT QTextStream &bin(QTextStream &s) { return Qt::bin(s); }
diff --git a/src/corelib/serialization/qtextstream.h b/src/corelib/serialization/qtextstream.h
index 935ec16536..6f93826d8a 100644
--- a/src/corelib/serialization/qtextstream.h
+++ b/src/corelib/serialization/qtextstream.h
@@ -233,13 +233,7 @@ inline QTextStream &operator<<(QTextStream &s, QTextStreamFunction f)
inline QTextStream &operator<<(QTextStream &s, QTextStreamManipulator m)
{ m.exec(s); return s; }
-#if defined(Q_QDOC) || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
namespace Qt {
-#else
-// This namespace only exists for 'using namespace' declarations.
-namespace QTextStreamFunctions {
-#endif
-
Q_CORE_EXPORT QTextStream &bin(QTextStream &s);
Q_CORE_EXPORT QTextStream &oct(QTextStream &s);
Q_CORE_EXPORT QTextStream &dec(QTextStream &s);
@@ -272,12 +266,36 @@ Q_CORE_EXPORT QTextStream &bom(QTextStream &s);
Q_CORE_EXPORT QTextStream &ws(QTextStream &s);
-} // namespace QTextStreamFunctions
+} // namespace Qt
-#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC)
-namespace Qt {
-using namespace QTextStreamFunctions;
-}
+#if QT_DEPRECATED_SINCE(5, 15) && !defined(Q_QDOC)
+// This namespace only exists for 'using namespace' declarations.
+namespace QTextStreamFunctions {
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::bin") QTextStream &bin(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::oct") QTextStream &oct(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::dec") QTextStream &dec(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::hex") QTextStream &hex(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::showbase") QTextStream &showbase(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::forcesign") QTextStream &forcesign(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::forcepoint") QTextStream &forcepoint(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::noshowbase") QTextStream &noshowbase(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::noforcesign") QTextStream &noforcesign(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::noforcepoint") QTextStream &noforcepoint(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::uppercasebase") QTextStream &uppercasebase(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::uppercasedigits") QTextStream &uppercasedigits(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::lowercasebase") QTextStream &lowercasebase(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::lowercasedigits") QTextStream &lowercasedigits(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::fixed") QTextStream &fixed(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::scientific") QTextStream &scientific(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::left") QTextStream &left(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::right") QTextStream &right(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::center") QTextStream &center(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::endl") QTextStream &endl(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::flush") QTextStream &flush(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::reset") QTextStream &reset(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::bom") QTextStream &bom(QTextStream &s);
+Q_CORE_EXPORT QT_DEPRECATED_X("Use Qt::ws") QTextStream &ws(QTextStream &s);
+} // namespace QTextStreamFunctions
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wheader-hygiene")
@@ -285,7 +303,7 @@ QT_WARNING_DISABLE_CLANG("-Wheader-hygiene")
// conflicting definitions compiler errors.
using namespace QTextStreamFunctions;
QT_WARNING_POP
-#endif // QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC)
+#endif // QT_DEPRECATED_SINCE(5, 15) && !defined(Q_QDOC)
inline QTextStreamManipulator qSetFieldWidth(int width)
{
diff --git a/src/corelib/serialization/serialization.pri b/src/corelib/serialization/serialization.pri
index 7407e20d9e..ff653ca8f3 100644
--- a/src/corelib/serialization/serialization.pri
+++ b/src/corelib/serialization/serialization.pri
@@ -3,7 +3,9 @@
HEADERS += \
serialization/qcborarray.h \
serialization/qcborcommon.h \
+ serialization/qcborcommon_p.h \
serialization/qcbormap.h \
+ serialization/qcborstream.h \
serialization/qcborvalue.h \
serialization/qcborvalue_p.h \
serialization/qdatastream.h \
@@ -22,6 +24,7 @@ HEADERS += \
serialization/qxmlutils_p.h
SOURCES += \
+ serialization/qcborcommon.cpp \
serialization/qcbordiagnostic.cpp \
serialization/qcborvalue.cpp \
serialization/qdatastream.cpp \
@@ -36,12 +39,20 @@ SOURCES += \
serialization/qxmlstream.cpp \
serialization/qxmlutils.cpp
-qtConfig(cborstream): {
+qtConfig(cborstreamreader): {
SOURCES += \
- serialization/qcborstream.cpp
+ serialization/qcborstreamreader.cpp
HEADERS += \
- serialization/qcborstream.h
+ serialization/qcborstreamreader.h
+}
+
+qtConfig(cborstreamwriter): {
+ SOURCES += \
+ serialization/qcborstreamwriter.cpp
+
+ HEADERS += \
+ serialization/qcborstreamwriter.h
}
qtConfig(binaryjson): {
diff --git a/src/corelib/statemachine/qabstractstate.cpp b/src/corelib/statemachine/qabstractstate.cpp
index 0db44bc427..10f54c3e18 100644
--- a/src/corelib/statemachine/qabstractstate.cpp
+++ b/src/corelib/statemachine/qabstractstate.cpp
@@ -84,19 +84,19 @@ QT_BEGIN_NAMESPACE
QAbstractStatePrivate::QAbstractStatePrivate(StateType type)
- : stateType(type), isMachine(false), active(false), parentState(0)
+ : stateType(type), isMachine(false), active(false), parentState(nullptr)
{
}
QStateMachine *QAbstractStatePrivate::machine() const
{
QObject *par = parent;
- while (par != 0) {
+ while (par != nullptr) {
if (QStateMachine *mach = qobject_cast<QStateMachine*>(par))
return mach;
par = par->parent();
}
- return 0;
+ return nullptr;
}
void QAbstractStatePrivate::callOnEntry(QEvent *e)
diff --git a/src/corelib/statemachine/qabstracttransition.cpp b/src/corelib/statemachine/qabstracttransition.cpp
index d841fd3c8b..df70b54721 100644
--- a/src/corelib/statemachine/qabstracttransition.cpp
+++ b/src/corelib/statemachine/qabstracttransition.cpp
@@ -144,7 +144,7 @@ QStateMachine *QAbstractTransitionPrivate::machine() const
Q_Q(const QAbstractTransition);
if (QHistoryState *parent = qobject_cast<QHistoryState *>(q->parent()))
return parent->machine();
- return 0;
+ return nullptr;
}
bool QAbstractTransitionPrivate::callEventTest(QEvent *e)
@@ -223,7 +223,7 @@ void QAbstractTransition::setTargetState(QAbstractState* target)
{
Q_D(QAbstractTransition);
if ((d->targetStates.size() == 1 && target == d->targetStates.at(0).data()) ||
- (d->targetStates.isEmpty() && target == 0)) {
+ (d->targetStates.isEmpty() && target == nullptr)) {
return;
}
if (!target)
diff --git a/src/corelib/statemachine/qeventtransition.cpp b/src/corelib/statemachine/qeventtransition.cpp
index a90f147773..5dcbcfff47 100644
--- a/src/corelib/statemachine/qeventtransition.cpp
+++ b/src/corelib/statemachine/qeventtransition.cpp
@@ -100,7 +100,7 @@ QT_BEGIN_NAMESPACE
*/
QEventTransitionPrivate::QEventTransitionPrivate()
{
- object = 0;
+ object = nullptr;
eventType = QEvent::None;
registered = false;
}
diff --git a/src/corelib/statemachine/qhistorystate.cpp b/src/corelib/statemachine/qhistorystate.cpp
index ccf04d4799..e5b8075b96 100644
--- a/src/corelib/statemachine/qhistorystate.cpp
+++ b/src/corelib/statemachine/qhistorystate.cpp
@@ -147,7 +147,7 @@ protected:
QHistoryStatePrivate::QHistoryStatePrivate()
: QAbstractStatePrivate(HistoryState)
- , defaultTransition(0)
+ , defaultTransition(nullptr)
, historyType(QHistoryState::ShallowHistory)
{
}
diff --git a/src/corelib/statemachine/qsignaltransition.cpp b/src/corelib/statemachine/qsignaltransition.cpp
index 59e0c0d788..8e57695fd7 100644
--- a/src/corelib/statemachine/qsignaltransition.cpp
+++ b/src/corelib/statemachine/qsignaltransition.cpp
@@ -107,7 +107,7 @@ QT_BEGIN_NAMESPACE
QSignalTransitionPrivate::QSignalTransitionPrivate()
{
- sender = 0;
+ sender = nullptr;
signalIndex = -1;
}
diff --git a/src/corelib/statemachine/qstate.cpp b/src/corelib/statemachine/qstate.cpp
index 62dd4f0284..f641d25a96 100644
--- a/src/corelib/statemachine/qstate.cpp
+++ b/src/corelib/statemachine/qstate.cpp
@@ -146,7 +146,7 @@ QT_BEGIN_NAMESPACE
QStatePrivate::QStatePrivate()
: QAbstractStatePrivate(StandardState),
- errorState(0), initialState(0), childMode(QState::ExclusiveStates),
+ errorState(nullptr), initialState(nullptr), childMode(QState::ExclusiveStates),
childStatesListNeedsRefresh(true), transitionsListNeedsRefresh(true)
{
}
@@ -293,11 +293,11 @@ QAbstractState *QState::errorState() const
void QState::setErrorState(QAbstractState *state)
{
Q_D(QState);
- if (state != 0 && qobject_cast<QStateMachine*>(state)) {
+ if (state != nullptr && qobject_cast<QStateMachine*>(state)) {
qWarning("QStateMachine::setErrorState: root state cannot be error state");
return;
}
- if (state != 0 && (!state->machine() || ((state->machine() != machine()) && !qobject_cast<QStateMachine*>(this)))) {
+ if (state != nullptr && (!state->machine() || ((state->machine() != machine()) && !qobject_cast<QStateMachine*>(this)))) {
qWarning("QState::setErrorState: error state cannot belong "
"to a different state machine");
return;
@@ -360,15 +360,15 @@ QSignalTransition *QState::addTransition(const QObject *sender, const char *sign
{
if (!sender) {
qWarning("QState::addTransition: sender cannot be null");
- return 0;
+ return nullptr;
}
if (!signal) {
qWarning("QState::addTransition: signal cannot be null");
- return 0;
+ return nullptr;
}
if (!target) {
qWarning("QState::addTransition: cannot add transition to null state");
- return 0;
+ return nullptr;
}
int offset = (*signal == '0'+QSIGNAL_CODE) ? 1 : 0;
const QMetaObject *meta = sender->metaObject();
@@ -376,7 +376,7 @@ QSignalTransition *QState::addTransition(const QObject *sender, const char *sign
if (meta->indexOfSignal(QMetaObject::normalizedSignature(signal+offset)) == -1) {
qWarning("QState::addTransition: no such signal %s::%s",
meta->className(), signal+offset);
- return 0;
+ return nullptr;
}
}
QSignalTransition *trans = new QSignalTransition(sender, signal);
@@ -409,7 +409,7 @@ QAbstractTransition *QState::addTransition(QAbstractState *target)
{
if (!target) {
qWarning("QState::addTransition: cannot add transition to null state");
- return 0;
+ return nullptr;
}
UnconditionalTransition *trans = new UnconditionalTransition(target);
addTransition(trans);
@@ -438,7 +438,7 @@ void QState::removeTransition(QAbstractTransition *transition)
QStateMachinePrivate *mach = QStateMachinePrivate::get(d->machine());
if (mach)
mach->unregisterTransition(transition);
- transition->setParent(0);
+ transition->setParent(nullptr);
}
/*!
@@ -544,7 +544,7 @@ bool QState::event(QEvent *e)
d->childStatesListNeedsRefresh = true;
d->transitionsListNeedsRefresh = true;
if ((e->type() == QEvent::ChildRemoved) && (static_cast<QChildEvent *>(e)->child() == d->initialState))
- d->initialState = 0;
+ d->initialState = nullptr;
}
return QAbstractState::event(e);
}
diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp
index 009659d998..fb551d5197 100644
--- a/src/corelib/statemachine/qstatemachine.cpp
+++ b/src/corelib/statemachine/qstatemachine.cpp
@@ -197,7 +197,7 @@ struct CalculationCache {
bool transitionDomainIsKnown : 1;
TransitionInfo()
- : transitionDomain(0)
+ : transitionDomain(nullptr)
, effectiveTargetStatesIsKnown(false)
, exitSetIsKnown(false)
, transitionDomainIsKnown(false)
@@ -289,9 +289,9 @@ child of a child, etc.) Otherwise returns 'false'.
*/
static inline bool isDescendant(const QAbstractState *state1, const QAbstractState *state2)
{
- Q_ASSERT(state1 != 0);
+ Q_ASSERT(state1 != nullptr);
- for (QAbstractState *it = state1->parentState(); it != 0; it = it->parentState()) {
+ for (QAbstractState *it = state1->parentState(); it != nullptr; it = it->parentState()) {
if (it == state2)
return true;
}
@@ -311,7 +311,7 @@ static bool containsDecendantOf(const QSet<QAbstractState *> &states, const QAbs
static int descendantDepth(const QAbstractState *state, const QAbstractState *ancestor)
{
int depth = 0;
- for (const QAbstractState *it = state; it != 0; it = it->parentState()) {
+ for (const QAbstractState *it = state; it != nullptr; it = it->parentState()) {
if (it == ancestor)
break;
++depth;
@@ -332,7 +332,7 @@ this returns the empty set.
*/
static QVector<QState*> getProperAncestors(const QAbstractState *state, const QAbstractState *upperBound)
{
- Q_ASSERT(state != 0);
+ Q_ASSERT(state != nullptr);
QVector<QState*> result;
result.reserve(16);
for (QState *it = state->parentState(); it && it != upperBound; it = it->parentState()) {
@@ -405,7 +405,7 @@ QStateMachinePrivate::QStateMachinePrivate()
stopProcessingReason = EventQueueEmpty;
error = QStateMachine::NoError;
globalRestorePolicy = QState::DontRestoreProperties;
- signalEventGenerator = 0;
+ signalEventGenerator = nullptr;
#if QT_CONFIG(animation)
animated = true;
#endif
@@ -437,7 +437,7 @@ static QEvent *cloneEvent(QEvent *e)
Q_ASSERT_X(false, "cloneEvent()", "not implemented");
break;
}
- return 0;
+ return nullptr;
}
const QStateMachinePrivate::Handler qt_kernel_statemachine_handler = {
@@ -474,10 +474,10 @@ bool QStateMachinePrivate::transitionStateEntryLessThan(QAbstractTransition *t1,
} else if (isDescendant(s2, s1)) {
return false;
} else {
- Q_ASSERT(s1->machine() != 0);
+ Q_ASSERT(s1->machine() != nullptr);
QStateMachinePrivate *mach = QStateMachinePrivate::get(s1->machine());
QState *lca = mach->findLCA(QList<QAbstractState*>() << s1 << s2);
- Q_ASSERT(lca != 0);
+ Q_ASSERT(lca != nullptr);
int s1Depth = descendantDepth(s1, lca);
int s2Depth = descendantDepth(s2, lca);
if (s1Depth == s2Depth)
@@ -497,10 +497,10 @@ bool QStateMachinePrivate::stateEntryLessThan(QAbstractState *s1, QAbstractState
} else if (isDescendant(s2, s1)) {
return true;
} else {
- Q_ASSERT(s1->machine() != 0);
+ Q_ASSERT(s1->machine() != nullptr);
QStateMachinePrivate *mach = QStateMachinePrivate::get(s1->machine());
QState *lca = mach->findLCA(QList<QAbstractState*>() << s1 << s2);
- Q_ASSERT(lca != 0);
+ Q_ASSERT(lca != nullptr);
return (indexOfDescendant(lca, s1) < indexOfDescendant(lca, s2));
}
}
@@ -515,10 +515,10 @@ bool QStateMachinePrivate::stateExitLessThan(QAbstractState *s1, QAbstractState
} else if (isDescendant(s2, s1)) {
return false;
} else {
- Q_ASSERT(s1->machine() != 0);
+ Q_ASSERT(s1->machine() != nullptr);
QStateMachinePrivate *mach = QStateMachinePrivate::get(s1->machine());
QState *lca = mach->findLCA(QList<QAbstractState*>() << s1 << s2);
- Q_ASSERT(lca != 0);
+ Q_ASSERT(lca != nullptr);
return (indexOfDescendant(lca, s2) < indexOfDescendant(lca, s1));
}
}
@@ -526,7 +526,7 @@ bool QStateMachinePrivate::stateExitLessThan(QAbstractState *s1, QAbstractState
QState *QStateMachinePrivate::findLCA(const QList<QAbstractState*> &states, bool onlyCompound)
{
if (states.isEmpty())
- return 0;
+ return nullptr;
QVector<QState*> ancestors = getProperAncestors(states.at(0), rootState()->parentState());
for (int i = 0; i < ancestors.size(); ++i) {
QState *anc = ancestors.at(i);
@@ -792,7 +792,7 @@ QSet<QAbstractState*> QStateMachinePrivate::computeExitSet_Unordered(QAbstractTr
lst.prepend(t->sourceState());
domain = findLCCA(lst);
- Q_ASSERT(domain != 0);
+ Q_ASSERT(domain != nullptr);
}
for (QAbstractState* s : qAsConst(configuration)) {
@@ -918,7 +918,7 @@ QAbstractState *QStateMachinePrivate::getTransitionDomain(QAbstractTransition *t
Q_ASSERT(cache);
if (effectiveTargetStates.isEmpty())
- return 0;
+ return nullptr;
QAbstractState *domain = nullptr;
if (cache->transitionDomain(t, &domain))
@@ -1234,28 +1234,28 @@ QState *QStateMachinePrivate::toStandardState(QAbstractState *state)
{
if (state && (QAbstractStatePrivate::get(state)->stateType == QAbstractStatePrivate::StandardState))
return static_cast<QState*>(state);
- return 0;
+ return nullptr;
}
const QState *QStateMachinePrivate::toStandardState(const QAbstractState *state)
{
if (state && (QAbstractStatePrivate::get(state)->stateType == QAbstractStatePrivate::StandardState))
return static_cast<const QState*>(state);
- return 0;
+ return nullptr;
}
QFinalState *QStateMachinePrivate::toFinalState(QAbstractState *state)
{
if (state && (QAbstractStatePrivate::get(state)->stateType == QAbstractStatePrivate::FinalState))
return static_cast<QFinalState*>(state);
- return 0;
+ return nullptr;
}
QHistoryState *QStateMachinePrivate::toHistoryState(QAbstractState *state)
{
if (state && (QAbstractStatePrivate::get(state)->stateType == QAbstractStatePrivate::HistoryState))
return static_cast<QHistoryState*>(state);
- return 0;
+ return nullptr;
}
bool QStateMachinePrivate::isInFinalState(QAbstractState* s) const
@@ -1455,13 +1455,13 @@ QHash<QAbstractState*, QVector<QPropertyAssignment> > QStateMachinePrivate::comp
QAbstractState *QStateMachinePrivate::findErrorState(QAbstractState *context)
{
// Find error state recursively in parent hierarchy if not set explicitly for context state
- QAbstractState *errorState = 0;
- if (context != 0) {
+ QAbstractState *errorState = nullptr;
+ if (context != nullptr) {
QState *s = toStandardState(context);
- if (s != 0)
+ if (s != nullptr)
errorState = s->errorState();
- if (errorState == 0)
+ if (errorState == nullptr)
errorState = findErrorState(context->parentState());
}
@@ -1475,21 +1475,21 @@ void QStateMachinePrivate::setError(QStateMachine::Error errorCode, QAbstractSta
error = errorCode;
switch (errorCode) {
case QStateMachine::NoInitialStateError:
- Q_ASSERT(currentContext != 0);
+ Q_ASSERT(currentContext != nullptr);
errorString = QStateMachine::tr("Missing initial state in compound state '%1'")
.arg(currentContext->objectName());
break;
case QStateMachine::NoDefaultStateInHistoryStateError:
- Q_ASSERT(currentContext != 0);
+ Q_ASSERT(currentContext != nullptr);
errorString = QStateMachine::tr("Missing default state in history state '%1'")
.arg(currentContext->objectName());
break;
case QStateMachine::NoCommonAncestorForTransitionError:
- Q_ASSERT(currentContext != 0);
+ Q_ASSERT(currentContext != nullptr);
errorString = QStateMachine::tr("No common ancestor for targets and source of transition from state '%1'")
.arg(currentContext->objectName());
@@ -1513,11 +1513,11 @@ void QStateMachinePrivate::setError(QStateMachine::Error errorCode, QAbstractSta
// Avoid infinite loop if the error state itself has an error
if (currentContext == currentErrorState)
- currentErrorState = 0;
+ currentErrorState = nullptr;
Q_ASSERT(currentErrorState != rootState());
- if (currentErrorState != 0) {
+ if (currentErrorState != nullptr) {
#ifdef QSTATEMACHINE_DEBUG
qDebug() << q << ": entering error state" << currentErrorState << "from" << currentContext;
#endif
@@ -1549,7 +1549,7 @@ QStateMachinePrivate::initializeAnimation(QAbstractAnimation *abstractAnimation,
}
} else {
QPropertyAnimation *animation = qobject_cast<QPropertyAnimation *>(abstractAnimation);
- if (animation != 0
+ if (animation != nullptr
&& prop.object == animation->targetObject()
&& prop.propertyName == animation->propertyName()) {
@@ -1568,7 +1568,7 @@ void QStateMachinePrivate::_q_animationFinished()
{
Q_Q(QStateMachine);
QAbstractAnimation *anim = qobject_cast<QAbstractAnimation*>(q->sender());
- Q_ASSERT(anim != 0);
+ Q_ASSERT(anim != nullptr);
QObject::disconnect(anim, SIGNAL(finished()), q, SLOT(_q_animationFinished()));
if (resetAnimationEndValues.contains(anim)) {
qobject_cast<QVariantAnimation*>(anim)->setEndValue(QVariant()); // ### generalize
@@ -1576,7 +1576,7 @@ void QStateMachinePrivate::_q_animationFinished()
}
QAbstractState *state = stateForAnimation.take(anim);
- Q_ASSERT(state != 0);
+ Q_ASSERT(state != nullptr);
#ifndef QT_NO_PROPERTIES
// Set the final property value.
@@ -1638,7 +1638,7 @@ void QStateMachinePrivate::terminateActiveAnimations(QAbstractState *state,
resetAnimationEndValues.remove(anim);
}
QPropertyAssignment assn = propertyForAnimation.take(anim);
- Q_ASSERT(assn.object != 0);
+ Q_ASSERT(assn.object != nullptr);
// If there is no property assignment that sets this property,
// set the property to its target value.
bool found = false;
@@ -1745,7 +1745,7 @@ QAbstractTransition *QStateMachinePrivate::createInitialTransition() const
};
QState *root = rootState();
- Q_ASSERT(root != 0);
+ Q_ASSERT(root != nullptr);
QList<QAbstractState *> targets;
switch (root->childMode()) {
case QState::ExclusiveStates:
@@ -1891,26 +1891,26 @@ void QStateMachinePrivate::_q_process()
enabledTransitions = selectTransitions(e, &calculationCache);
if (enabledTransitions.isEmpty()) {
delete e;
- e = 0;
+ e = nullptr;
}
- while (enabledTransitions.isEmpty() && ((e = dequeueInternalEvent()) != 0)) {
+ while (enabledTransitions.isEmpty() && ((e = dequeueInternalEvent()) != nullptr)) {
#ifdef QSTATEMACHINE_DEBUG
qDebug() << q << ": dequeued internal event" << e << "of type" << e->type();
#endif
enabledTransitions = selectTransitions(e, &calculationCache);
if (enabledTransitions.isEmpty()) {
delete e;
- e = 0;
+ e = nullptr;
}
}
- while (enabledTransitions.isEmpty() && ((e = dequeueExternalEvent()) != 0)) {
+ while (enabledTransitions.isEmpty() && ((e = dequeueExternalEvent()) != nullptr)) {
#ifdef QSTATEMACHINE_DEBUG
qDebug() << q << ": dequeued external event" << e << "of type" << e->type();
#endif
enabledTransitions = selectTransitions(e, &calculationCache);
if (enabledTransitions.isEmpty()) {
delete e;
- e = 0;
+ e = nullptr;
}
}
if (enabledTransitions.isEmpty()) {
@@ -2009,7 +2009,7 @@ QEvent *QStateMachinePrivate::dequeueInternalEvent()
{
QMutexLocker locker(&internalEventMutex);
if (internalEventQueue.isEmpty())
- return 0;
+ return nullptr;
return internalEventQueue.takeFirst();
}
@@ -2017,7 +2017,7 @@ QEvent *QStateMachinePrivate::dequeueExternalEvent()
{
QMutexLocker locker(&externalEventMutex);
if (externalEventQueue.isEmpty())
- return 0;
+ return nullptr;
return externalEventQueue.takeFirst();
}
@@ -2175,15 +2175,15 @@ void QStateMachinePrivate::goToState(QAbstractState *targetState)
return;
Q_ASSERT(state == Running);
- QState *sourceState = 0;
+ QState *sourceState = nullptr;
QSet<QAbstractState*>::const_iterator it;
for (it = configuration.constBegin(); it != configuration.constEnd(); ++it) {
sourceState = toStandardState(*it);
- if (sourceState != 0)
+ if (sourceState != nullptr)
break;
}
- Q_ASSERT(sourceState != 0);
+ Q_ASSERT(sourceState != nullptr);
// Reuse previous GoToStateTransition in case of several calls to
// goToState() in a row.
GoToStateTransition *trans = sourceState->findChild<GoToStateTransition*>();
@@ -2327,7 +2327,7 @@ void QStateMachinePrivate::unregisterSignalTransition(QSignalTransition *transit
Q_ASSERT(connectedSignalIndexes.size() > signalIndex);
Q_ASSERT(connectedSignalIndexes.at(signalIndex) != 0);
if (--connectedSignalIndexes[signalIndex] == 0) {
- Q_ASSERT(signalEventGenerator != 0);
+ Q_ASSERT(signalEventGenerator != nullptr);
static const int generatorMethodOffset = QSignalEventGenerator::staticMetaObject.methodOffset();
QMetaObject::disconnect(sender, signalIndex, signalEventGenerator, generatorMethodOffset);
int sum = 0;
@@ -2454,7 +2454,7 @@ void QStateMachinePrivate::handleTransitionSignal(QObject *sender, int signalInd
Constructs a new state machine with the given \a parent.
*/
QStateMachine::QStateMachine(QObject *parent)
- : QState(*new QStateMachinePrivate, /*parentState=*/0)
+ : QState(*new QStateMachinePrivate, /*parentState=*/nullptr)
{
// Can't pass the parent to the QState constructor, as it expects a QState
// But this works as expected regardless of whether parent is a QState or not
@@ -2472,7 +2472,7 @@ QStateMachine::QStateMachine(QObject *parent)
state machine is invalid, and might work incorrectly.
*/
QStateMachine::QStateMachine(QState::ChildMode childMode, QObject *parent)
- : QState(*new QStateMachinePrivate, /*parentState=*/0)
+ : QState(*new QStateMachinePrivate, /*parentState=*/nullptr)
{
Q_D(QStateMachine);
d->childMode = childMode;
@@ -2495,7 +2495,7 @@ QStateMachine::QStateMachine(QState::ChildMode childMode, QObject *parent)
\internal
*/
QStateMachine::QStateMachine(QStateMachinePrivate &dd, QObject *parent)
- : QState(dd, /*parentState=*/0)
+ : QState(dd, /*parentState=*/nullptr)
{
setParent(parent);
}
@@ -2637,7 +2637,7 @@ void QStateMachine::removeState(QAbstractState *state)
state, QAbstractStatePrivate::get(state)->machine(), this);
return;
}
- state->setParent(0);
+ state->setParent(nullptr);
}
bool QStateMachine::isRunning() const
@@ -2661,7 +2661,7 @@ void QStateMachine::start()
{
Q_D(QStateMachine);
- if ((childMode() == QState::ExclusiveStates) && (initialState() == 0)) {
+ if ((childMode() == QState::ExclusiveStates) && (initialState() == nullptr)) {
qWarning("QStateMachine::start: No initial state set for machine. Refusing to start.");
return;
}
@@ -2897,7 +2897,7 @@ bool QStateMachine::event(QEvent *e)
d->delayedEventsMutex.lock();
int id = d->timerIdToDelayedEventId.take(tid);
QStateMachinePrivate::DelayedEvent ee = d->delayedEvents.take(id);
- if (ee.event != 0) {
+ if (ee.event != nullptr) {
Q_ASSERT(ee.timerId == tid);
killTimer(tid);
d->delayedEventIdFreeList.release(id);
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp
index 4a00c664c0..ac5f2afb57 100644
--- a/src/corelib/text/qbytearray.cpp
+++ b/src/corelib/text/qbytearray.cpp
@@ -2,6 +2,7 @@
**
** Copyright (C) 2019 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
+** Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -41,6 +42,7 @@
#include "qbytearray.h"
#include "qbytearraymatcher.h"
#include "private/qtools_p.h"
+#include "qhashfunctions.h"
#include "qstring.h"
#include "qlist.h"
#include "qlocale.h"
@@ -63,7 +65,7 @@
#include <string.h>
#include <stdlib.h>
-#define IS_RAW_DATA(d) ((d)->offset != sizeof(QByteArrayData))
+#define IS_RAW_DATA(d) ((d)->flags() & QArrayData::RawDataType)
QT_BEGIN_NAMESPACE
@@ -332,7 +334,7 @@ int qstricmp(const char *str1, const char *str2)
return int(Incomplete);
};
-#if defined(__SSE4_1__) && !(defined(__SANITIZE_ADDRESS__) || QT_HAS_FEATURE(address_sanitizer))
+#if defined(__SSE4_1__) && !(defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer))
enum { PageSize = 4096, PageMask = PageSize - 1 };
const __m128i zero = _mm_setzero_si128();
forever {
@@ -731,27 +733,25 @@ QByteArray qUncompress(const uchar* data, int nbytes)
return invalidCompressedData();
}
- QScopedPointer<QByteArray::Data, QByteArrayDataDeleter> d(QByteArray::Data::allocate(expectedSize + 1));
+ QPair<QByteArray::Data *, char *> pair = QByteArray::Data::allocate(expectedSize + 1);
+ QScopedPointer<QByteArray::Data, QByteArrayDataDeleter> d(pair.first);
if (Q_UNLIKELY(d.data() == nullptr))
return invalidCompressedData();
- d->size = expectedSize;
forever {
ulong alloc = len;
- int res = ::uncompress((uchar*)d->data(), &len,
+ int res = ::uncompress((uchar*)pair.second, &len,
data+4, nbytes-4);
switch (res) {
- case Z_OK:
+ case Z_OK: {
Q_ASSERT(len <= alloc);
Q_UNUSED(alloc);
- d->size = len;
- d->data()[len] = 0;
- {
- QByteArrayDataPtr dataPtr = { d.take() };
- return QByteArray(dataPtr);
- }
+ QByteArray::DataPointer dataPtr = { d.take(), pair.second, uint(len) };
+ pair.second[len] = '\0';
+ return QByteArray(dataPtr);
+ }
case Z_MEM_ERROR:
qWarning("qUncompress: Z_MEM_ERROR: Not enough memory");
@@ -764,11 +764,12 @@ QByteArray qUncompress(const uchar* data, int nbytes)
return invalidCompressedData();
} else {
// grow the block
- QByteArray::Data *p = QByteArray::Data::reallocateUnaligned(d.data(), len + 1);
- if (Q_UNLIKELY(p == nullptr))
+ pair = QByteArray::Data::reallocateUnaligned(d.data(), pair.second, len + 1);
+ Q_CHECK_PTR(pair.first);
+ if (Q_UNLIKELY(pair.first == nullptr))
return invalidCompressedData();
d.take(); // don't free
- d.reset(p);
+ d.reset(pair.first);
}
continue;
@@ -987,10 +988,20 @@ QByteArray qUncompress(const uchar* data, int nbytes)
four.
\value OmitTrailingEquals Omits adding the padding equal signs at the end of the encoded
data.
-
- QByteArray::fromBase64() ignores the KeepTrailingEquals and
- OmitTrailingEquals options and will not flag errors in case they are
- missing or if there are too many of them.
+ \value IgnoreBase64DecodingErrors When decoding Base64-encoded data, ignores errors
+ in the input; invalid characters are simply skipped.
+ This enum value has been added in Qt 5.15.
+ \value AbortOnBase64DecodingErrors When decoding Base64-encoded data, stops at the first
+ decoding error.
+ This enum value has been added in Qt 5.15.
+
+ QByteArray::fromBase64Encoding() and QByteArray::fromBase64()
+ ignore the KeepTrailingEquals and OmitTrailingEquals options. If
+ the IgnoreBase64DecodingErrors option is specified, they will not
+ flag errors in case trailing equal signs are missing or if there
+ are too many of them. If instead the AbortOnBase64DecodingErrors is
+ specified, then the input must either have no padding or have the
+ correct amount of equal signs.
*/
/*! \fn QByteArray::iterator QByteArray::begin()
@@ -1184,9 +1195,6 @@ QByteArray qUncompress(const uchar* data, int nbytes)
*/
QByteArray &QByteArray::operator=(const QByteArray & other) noexcept
{
- other.d->ref.ref();
- if (!d->ref.deref())
- Data::deallocate(d);
d = other.d;
return *this;
}
@@ -1200,25 +1208,23 @@ QByteArray &QByteArray::operator=(const QByteArray & other) noexcept
QByteArray &QByteArray::operator=(const char *str)
{
- Data *x;
- if (!str) {
- x = Data::sharedNull();
- } else if (!*str) {
- x = Data::allocate(0);
+ if (!str || !*str) {
+ QPair<Data *, char *> pair;
+ if (!str) {
+ pair = qMakePair(Data::sharedNull(), Data::sharedNullData());
+ } else {
+ pair = Data::allocate(0);
+ }
+ d = QByteArrayData(pair.first, pair.second, 0);
} else {
const int len = int(strlen(str));
- const uint fullLen = len + 1;
- if (d->ref.isShared() || fullLen > d->alloc
- || (len < d->size && fullLen < uint(d->alloc >> 1)))
+ const uint fullLen = uint(len) + 1;
+ if (d->needsDetach() || fullLen > d->allocatedCapacity()
+ || (len < size() && fullLen < (d->allocatedCapacity() >> 1)))
reallocData(fullLen, d->detachFlags());
- x = d;
- memcpy(x->data(), str, fullLen); // include null terminator
- x->size = len;
+ memcpy(d.data(), str, fullLen); // include null terminator
+ d.size = len;
}
- x->ref.ref();
- if (!d->ref.deref())
- Data::deallocate(d);
- d = x;
return *this;
}
@@ -1421,7 +1427,7 @@ QByteArray &QByteArray::operator=(const char *str)
\sa operator[]()
*/
-/*! \fn QByteRef QByteArray::operator[](int i)
+/*! \fn char &QByteArray::operator[](int i)
Returns the byte at index position \a i as a modifiable reference.
@@ -1432,21 +1438,6 @@ QByteArray &QByteArray::operator=(const char *str)
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 9
- The return value is of type QByteRef, a helper class for
- QByteArray. When you get an object of type QByteRef, you can use
- it as if it were a char &. If you assign to it, the assignment
- will apply to the character in the QByteArray from which you got
- the reference.
-
- \note Before Qt 5.14 it was possible to use this operator to access
- a character at an out-of-bounds position in the byte array, and
- then assign to such a position, causing the byte array to be
- automatically resized. Furthermore, assigning a value to the
- returned QByteRef would cause a detach of the byte array, even if the
- byte array has been copied in the meanwhile (and the QByteRef kept
- alive while the copy was taken). These behaviors are deprecated,
- and will be changed in a future version of Qt.
-
\sa at()
*/
@@ -1457,16 +1448,6 @@ QByteArray &QByteArray::operator=(const char *str)
Same as at(\a i).
*/
-/*! \fn QByteRef QByteArray::operator[](uint i)
-
- \overload
-*/
-
-/*! \fn char QByteArray::operator[](uint i) const
-
- \overload
-*/
-
/*!
\fn char QByteArray::front() const
\since 5.10
@@ -1498,7 +1479,7 @@ QByteArray &QByteArray::operator=(const char *str)
*/
/*!
- \fn QByteRef QByteArray::front()
+ \fn char &QByteArray::front()
\since 5.10
Returns a reference to the first character in the byte array.
@@ -1513,7 +1494,7 @@ QByteArray &QByteArray::operator=(const char *str)
*/
/*!
- \fn QByteRef QByteArray::back()
+ \fn char &QByteArray::back()
\since 5.10
Returns a reference to the last character in the byte array.
@@ -1564,7 +1545,7 @@ QByteArray &QByteArray::operator=(const char *str)
*/
void QByteArray::truncate(int pos)
{
- if (pos < d->size)
+ if (pos < size())
resize(pos);
}
@@ -1584,7 +1565,7 @@ void QByteArray::truncate(int pos)
void QByteArray::chop(int n)
{
if (n > 0)
- resize(d->size - n);
+ resize(size() - n);
}
@@ -1688,19 +1669,13 @@ void QByteArray::chop(int n)
QByteArray::QByteArray(const char *data, int size)
{
if (!data) {
- d = Data::sharedNull();
+ d = DataPointer();
} else {
if (size < 0)
size = int(strlen(data));
- if (!size) {
- d = Data::allocate(0);
- } else {
- d = Data::allocate(uint(size) + 1u);
- Q_CHECK_PTR(d);
- d->size = size;
- memcpy(d->data(), data, size);
- d->data()[size] = '\0';
- }
+ d = DataPointer(Data::allocate(uint(size) + 1u), size);
+ memcpy(d.data(), data, size);
+ d.data()[size] = '\0';
}
}
@@ -1714,13 +1689,11 @@ QByteArray::QByteArray(const char *data, int size)
QByteArray::QByteArray(int size, char ch)
{
if (size <= 0) {
- d = Data::allocate(0);
+ d = DataPointer(Data::allocate(0), 0);
} else {
- d = Data::allocate(uint(size) + 1u);
- Q_CHECK_PTR(d);
- d->size = size;
- memset(d->data(), ch, size);
- d->data()[size] = '\0';
+ d = DataPointer(Data::allocate(uint(size) + 1u), size);
+ memset(d.data(), ch, size);
+ d.data()[size] = '\0';
}
}
@@ -1732,10 +1705,8 @@ QByteArray::QByteArray(int size, char ch)
QByteArray::QByteArray(int size, Qt::Initialization)
{
- d = Data::allocate(uint(size) + 1u);
- Q_CHECK_PTR(d);
- d->size = size;
- d->data()[size] = '\0';
+ d = DataPointer(Data::allocate(uint(size) + 1u), size);
+ d.data()[size] = '\0';
}
/*!
@@ -1755,31 +1726,21 @@ void QByteArray::resize(int size)
if (size < 0)
size = 0;
- if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) {
- d->size = size;
+ if (!d->isShared() && !d->isMutable() && size < int(d.size)) {
+ d.size = size;
return;
}
- if (d->size == 0 && d->ref.isStatic()) {
- //
- // Optimize the idiom:
- // QByteArray a;
- // a.resize(sz);
- // ...
- // which is used in place of the Qt 3 idiom:
- // QByteArray a(sz);
- //
- Data *x = Data::allocate(uint(size) + 1u);
- Q_CHECK_PTR(x);
- x->size = size;
- x->data()[size] = '\0';
- d = x;
+ if (size == 0 && !(d->flags() & Data::CapacityReserved)) {
+ d = DataPointer(Data::allocate(0), 0);
} else {
- if (d->ref.isShared() || uint(size) + 1u > d->alloc)
- reallocData(uint(size) + 1u, d->detachFlags() | Data::Grow);
- if (d->alloc) {
- d->size = size;
- d->data()[size] = '\0';
+ if (d->needsDetach() || size > capacity()
+ || (!(d->flags() & Data::CapacityReserved) && size < int(d.size)
+ && size < (capacity() >> 1)))
+ reallocData(uint(size) + 1u, d->detachFlags() | Data::GrowsForward);
+ d.size = size;
+ if (d->isMutable()) {
+ d.data()[size] = '\0';
}
}
}
@@ -1797,33 +1758,27 @@ void QByteArray::resize(int size)
QByteArray &QByteArray::fill(char ch, int size)
{
- resize(size < 0 ? d->size : size);
- if (d->size)
- memset(d->data(), ch, d->size);
+ resize(size < 0 ? this->size() : size);
+ if (this->size())
+ memset(d.data(), ch, this->size());
return *this;
}
-void QByteArray::reallocData(uint alloc, Data::AllocationOptions options)
+void QByteArray::reallocData(uint alloc, Data::ArrayOptions options)
{
- if (d->ref.isShared() || IS_RAW_DATA(d)) {
- Data *x = Data::allocate(alloc, options);
- Q_CHECK_PTR(x);
- x->size = qMin(int(alloc) - 1, d->size);
- ::memcpy(x->data(), d->data(), x->size);
- x->data()[x->size] = '\0';
- if (!d->ref.deref())
- Data::deallocate(d);
- d = x;
+ if (d->needsDetach()) {
+ DataPointer dd(Data::allocate(alloc, options), qMin(int(alloc) - 1, d.size));
+ ::memcpy(dd.data(), d.data(), dd.size);
+ dd.data()[dd.size] = 0;
+ d = dd;
} else {
- Data *x = Data::reallocateUnaligned(d, alloc, options);
- Q_CHECK_PTR(x);
- d = x;
+ d.reallocate(alloc, options);
}
}
void QByteArray::expand(int i)
{
- resize(qMax(i + 1, d->size));
+ resize(qMax(i + 1, size()));
}
/*!
@@ -1869,9 +1824,9 @@ QByteArray QByteArray::nulTerminated() const
QByteArray &QByteArray::prepend(const QByteArray &ba)
{
- if (d->size == 0 && d->ref.isStatic() && !IS_RAW_DATA(ba.d)) {
+ if (size() == 0 && d->isStatic() && !IS_RAW_DATA(ba.d)) {
*this = ba;
- } else if (ba.d->size != 0) {
+ } else if (ba.size() != 0) {
QByteArray tmp = *this;
*this = ba;
append(tmp);
@@ -1900,12 +1855,12 @@ QByteArray &QByteArray::prepend(const char *str)
QByteArray &QByteArray::prepend(const char *str, int len)
{
if (str) {
- if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
- reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow);
- memmove(d->data()+len, d->data(), d->size);
- memcpy(d->data(), str, len);
- d->size += len;
- d->data()[d->size] = '\0';
+ if (d->needsDetach() || size() + len > capacity())
+ reallocData(uint(size() + len) + 1u, d->detachFlags() | Data::GrowsForward);
+ memmove(d.data()+len, d.data(), d.size);
+ memcpy(d.data(), str, len);
+ d.size += len;
+ d.data()[d.size] = '\0';
}
return *this;
}
@@ -1926,12 +1881,12 @@ QByteArray &QByteArray::prepend(const char *str, int len)
QByteArray &QByteArray::prepend(char ch)
{
- if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
- reallocData(uint(d->size) + 2u, d->detachFlags() | Data::Grow);
- memmove(d->data()+1, d->data(), d->size);
- d->data()[0] = ch;
- ++d->size;
- d->data()[d->size] = '\0';
+ if (d->needsDetach() || size() + 1 > capacity())
+ reallocData(uint(size()) + 2u, d->detachFlags() | Data::GrowsForward);
+ memmove(d.data()+1, d.data(), d.size);
+ d.data()[0] = ch;
+ ++d.size;
+ d.data()[d.size] = '\0';
return *this;
}
@@ -1961,14 +1916,14 @@ QByteArray &QByteArray::prepend(char ch)
QByteArray &QByteArray::append(const QByteArray &ba)
{
- if (d->size == 0 && d->ref.isStatic() && !IS_RAW_DATA(ba.d)) {
+ if (size() == 0 && d->isStatic() && !IS_RAW_DATA(ba.d)) {
*this = ba;
- } else if (ba.d->size != 0) {
- if (d->ref.isShared() || uint(d->size + ba.d->size) + 1u > d->alloc)
- reallocData(uint(d->size + ba.d->size) + 1u, d->detachFlags() | Data::Grow);
- memcpy(d->data() + d->size, ba.d->data(), ba.d->size);
- d->size += ba.d->size;
- d->data()[d->size] = '\0';
+ } else if (ba.size() != 0) {
+ if (d->needsDetach() || size() + ba.size() > capacity())
+ reallocData(uint(size() + ba.size()) + 1u, d->detachFlags() | Data::GrowsForward);
+ memcpy(d.data() + d.size, ba.data(), ba.size());
+ d.size += ba.size();
+ d.data()[d.size] = '\0';
}
return *this;
}
@@ -1996,10 +1951,10 @@ QByteArray& QByteArray::append(const char *str)
{
if (str) {
const int len = int(strlen(str));
- if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
- reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow);
- memcpy(d->data() + d->size, str, len + 1); // include null terminator
- d->size += len;
+ if (d->needsDetach() || size() + len > capacity())
+ reallocData(uint(size() + len) + 1u, d->detachFlags() | Data::GrowsForward);
+ memcpy(d.data() + d.size, str, len + 1); // include null terminator
+ d.size += len;
}
return *this;
}
@@ -2021,11 +1976,11 @@ QByteArray &QByteArray::append(const char *str, int len)
if (len < 0)
len = qstrlen(str);
if (str && len) {
- if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
- reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow);
- memcpy(d->data() + d->size, str, len); // include null terminator
- d->size += len;
- d->data()[d->size] = '\0';
+ if (d->needsDetach() || size() + len > capacity())
+ reallocData(uint(size() + len) + 1u, d->detachFlags() | Data::GrowsForward);
+ memcpy(d.data() + d.size, str, len);
+ d.size += len;
+ d.data()[d.size] = '\0';
}
return *this;
}
@@ -2049,10 +2004,10 @@ QByteArray &QByteArray::append(const char *str, int len)
QByteArray& QByteArray::append(char ch)
{
- if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
- reallocData(uint(d->size) + 2u, d->detachFlags() | Data::Grow);
- d->data()[d->size++] = ch;
- d->data()[d->size] = '\0';
+ if (d->needsDetach() || size() + 1 > capacity())
+ reallocData(uint(size()) + 2u, d->detachFlags() | Data::GrowsForward);
+ d.data()[d.size++] = ch;
+ d.data()[d.size] = '\0';
return *this;
}
@@ -2093,7 +2048,7 @@ static inline QByteArray &qbytearray_insert(QByteArray *ba,
QByteArray &QByteArray::insert(int i, const QByteArray &ba)
{
QByteArray copy(ba);
- return qbytearray_insert(this, i, copy.d->data(), copy.d->size);
+ return qbytearray_insert(this, i, copy.constData(), copy.size());
}
/*!
@@ -2175,7 +2130,7 @@ QByteArray &QByteArray::insert(int i, int count, char ch)
int oldsize = size();
resize(qMax(i, oldsize) + count);
- char *dst = d->data();
+ char *dst = d.data();
if (i > oldsize)
::memset(dst + oldsize, 0x20, i - oldsize);
else if (i < oldsize)
@@ -2200,14 +2155,14 @@ QByteArray &QByteArray::insert(int i, int count, char ch)
QByteArray &QByteArray::remove(int pos, int len)
{
- if (len <= 0 || uint(pos) >= uint(d->size))
+ if (len <= 0 || uint(pos) >= uint(size()))
return *this;
detach();
- if (len >= d->size - pos) {
+ if (len >= size() - pos) {
resize(pos);
} else {
- memmove(d->data() + pos, d->data() + pos + len, d->size - pos - len);
- resize(d->size - len);
+ memmove(d.data() + pos, d.data() + pos + len, size() - pos - len);
+ resize(size() - len);
}
return *this;
}
@@ -2224,9 +2179,9 @@ QByteArray &QByteArray::remove(int pos, int len)
QByteArray &QByteArray::replace(int pos, int len, const QByteArray &after)
{
- if (len == after.d->size && (pos + len <= d->size)) {
+ if (len == after.size() && (pos + len <= size())) {
detach();
- memmove(d->data() + pos, after.d->data(), len*sizeof(char));
+ memmove(d.data() + pos, after.data(), len*sizeof(char));
return *this;
} else {
QByteArray copy(after);
@@ -2261,9 +2216,9 @@ QByteArray &QByteArray::replace(int pos, int len, const char *after)
*/
QByteArray &QByteArray::replace(int pos, int len, const char *after, int alen)
{
- if (len == alen && (pos + len <= d->size)) {
+ if (len == alen && (pos + len <= size())) {
detach();
- memcpy(d->data() + pos, after, len*sizeof(char));
+ memcpy(d.data() + pos, after, len*sizeof(char));
return *this;
} else {
remove(pos, len);
@@ -2286,14 +2241,7 @@ QByteArray &QByteArray::replace(int pos, int len, const char *after, int alen)
QByteArray &QByteArray::replace(const QByteArray &before, const QByteArray &after)
{
- if (isNull() || before.d == after.d)
- return *this;
-
- QByteArray aft = after;
- if (after.d == d)
- aft.detach();
-
- return replace(before.constData(), before.size(), aft.constData(), aft.size());
+ return replace(before.constData(), before.size(), after.constData(), after.size());
}
/*!
@@ -2306,11 +2254,7 @@ QByteArray &QByteArray::replace(const QByteArray &before, const QByteArray &afte
QByteArray &QByteArray::replace(const char *c, const QByteArray &after)
{
- QByteArray aft = after;
- if (after.d == d)
- aft.detach();
-
- return replace(c, qstrlen(c), aft.constData(), aft.size());
+ return replace(c, qstrlen(c), after.constData(), after.size());
}
/*!
@@ -2330,13 +2274,13 @@ QByteArray &QByteArray::replace(const char *before, int bsize, const char *after
// protect against before or after being part of this
const char *a = after;
const char *b = before;
- if (after >= d->data() && after < d->data() + d->size) {
+ if (after >= constBegin() && after < constEnd()) {
char *copy = (char *)malloc(asize);
Q_CHECK_PTR(copy);
memcpy(copy, after, asize);
a = copy;
}
- if (before >= d->data() && before < d->data() + d->size) {
+ if (before >= constBegin() && before < constEnd()) {
char *copy = (char *)malloc(bsize);
Q_CHECK_PTR(copy);
memcpy(copy, before, bsize);
@@ -2345,13 +2289,13 @@ QByteArray &QByteArray::replace(const char *before, int bsize, const char *after
QByteArrayMatcher matcher(before, bsize);
int index = 0;
- int len = d->size;
- char *d = data();
+ int len = size();
+ char *d = data(); // detaches
if (bsize == asize) {
if (bsize) {
while ((index = matcher.indexIn(*this, index)) != -1) {
- memcpy(d + index, after, asize);
+ memcpy(d + index, a, asize);
index += bsize;
}
}
@@ -2370,7 +2314,7 @@ QByteArray &QByteArray::replace(const char *before, int bsize, const char *after
to = index;
}
if (asize) {
- memcpy(d + to, after, asize);
+ memcpy(d + to, a, asize);
to += asize;
}
index += bsize;
@@ -2413,7 +2357,7 @@ QByteArray &QByteArray::replace(const char *before, int bsize, const char *after
resize(newlen);
len = newlen;
}
- d = this->d->data();
+ d = this->d.data(); // data(), without the detach() check
while(pos) {
pos--;
@@ -2422,7 +2366,7 @@ QByteArray &QByteArray::replace(const char *before, int bsize, const char *after
int moveto = insertstart + asize;
memmove(d + moveto, d + movestart, (moveend - movestart));
if (asize)
- memcpy(d + insertstart, after, asize);
+ memcpy(d + insertstart, a, asize);
moveend = movestart - bsize;
}
}
@@ -2485,8 +2429,7 @@ QByteArray &QByteArray::replace(const char *before, int bsize, const char *after
QByteArray &QByteArray::replace(char before, const QByteArray &after)
{
char b[2] = { before, '\0' };
- QByteArray cb = fromRawData(b, 1);
- return replace(cb, after);
+ return replace(b, 1, after.constData(), after.size());
}
/*! \fn QByteArray &QByteArray::replace(char before, const QString &after)
@@ -2520,9 +2463,9 @@ QByteArray &QByteArray::replace(char before, const QByteArray &after)
QByteArray &QByteArray::replace(char before, char after)
{
- if (d->size) {
+ if (!isEmpty()) {
char *i = data();
- char *e = i + d->size;
+ char *e = i + size();
for (; i != e; ++i)
if (*i == before)
* i = after;
@@ -2563,7 +2506,7 @@ QList<QByteArray> QByteArray::split(char sep) const
*/
QByteArray QByteArray::repeated(int times) const
{
- if (d->size == 0)
+ if (isEmpty())
return *this;
if (times <= 1) {
@@ -2572,27 +2515,27 @@ QByteArray QByteArray::repeated(int times) const
return QByteArray();
}
- const int resultSize = times * d->size;
+ const int resultSize = times * size();
QByteArray result;
result.reserve(resultSize);
- if (result.d->alloc != uint(resultSize) + 1u)
+ if (result.capacity() != resultSize)
return QByteArray(); // not enough memory
- memcpy(result.d->data(), d->data(), d->size);
+ memcpy(result.d.data(), data(), size());
- int sizeSoFar = d->size;
- char *end = result.d->data() + sizeSoFar;
+ int sizeSoFar = size();
+ char *end = result.d.data() + sizeSoFar;
const int halfResultSize = resultSize >> 1;
while (sizeSoFar <= halfResultSize) {
- memcpy(end, result.d->data(), sizeSoFar);
+ memcpy(end, result.d.data(), sizeSoFar);
end += sizeSoFar;
sizeSoFar <<= 1;
}
- memcpy(end, result.d->data(), resultSize - sizeSoFar);
- result.d->data()[resultSize] = '\0';
- result.d->size = resultSize;
+ memcpy(end, result.d.data(), resultSize - sizeSoFar);
+ result.d.data()[resultSize] = '\0';
+ result.d.size = resultSize;
return result;
}
@@ -2614,17 +2557,17 @@ QByteArray QByteArray::repeated(int times) const
int QByteArray::indexOf(const QByteArray &ba, int from) const
{
- const int ol = ba.d->size;
+ const int ol = ba.size();
if (ol == 0)
return from;
if (ol == 1)
- return indexOf(*ba.d->data(), from);
+ return indexOf(ba[0], from);
- const int l = d->size;
- if (from > d->size || ol + from > l)
+ const int l = size();
+ if (from > l || ol + from > l)
return -1;
- return qFindByteArray(d->data(), d->size, from, ba.d->data(), ol);
+ return qFindByteArray(data(), size(), from, ba.data(), ol);
}
/*! \fn int QByteArray::indexOf(const QString &str, int from) const
@@ -2658,13 +2601,13 @@ int QByteArray::indexOf(const char *c, int from) const
if (ol == 1)
return indexOf(*c, from);
- const int l = d->size;
- if (from > d->size || ol + from > l)
+ const int l = size();
+ if (from > l || ol + from > l)
return -1;
if (ol == 0)
return from;
- return qFindByteArray(d->data(), d->size, from, c, ol);
+ return qFindByteArray(data(), size(), from, c, ol);
}
/*!
@@ -2683,13 +2626,13 @@ int QByteArray::indexOf(const char *c, int from) const
int QByteArray::indexOf(char ch, int from) const
{
if (from < 0)
- from = qMax(from + d->size, 0);
- if (from < d->size) {
- const char *n = d->data() + from - 1;
- const char *e = d->data() + d->size;
+ from = qMax(from + size(), 0);
+ if (from < size()) {
+ const char *n = data() + from - 1;
+ const char *e = end();
while (++n != e)
if (*n == ch)
- return n - d->data();
+ return n - data();
}
return -1;
}
@@ -2744,11 +2687,11 @@ static int lastIndexOfHelper(const char *haystack, int l, const char *needle, in
int QByteArray::lastIndexOf(const QByteArray &ba, int from) const
{
- const int ol = ba.d->size;
+ const int ol = ba.size();
if (ol == 1)
- return lastIndexOf(*ba.d->data(), from);
+ return lastIndexOf(ba[0], from);
- return lastIndexOfHelper(d->data(), d->size, ba.d->data(), ol, from);
+ return lastIndexOfHelper(data(), size(), ba.data(), ol, from);
}
/*! \fn int QByteArray::lastIndexOf(const QString &str, int from) const
@@ -2783,7 +2726,7 @@ int QByteArray::lastIndexOf(const char *str, int from) const
if (ol == 1)
return lastIndexOf(*str, from);
- return lastIndexOfHelper(d->data(), d->size, str, ol, from);
+ return lastIndexOfHelper(data(), size(), str, ol, from);
}
/*!
@@ -2803,12 +2746,12 @@ int QByteArray::lastIndexOf(const char *str, int from) const
int QByteArray::lastIndexOf(char ch, int from) const
{
if (from < 0)
- from += d->size;
- else if (from > d->size)
- from = d->size-1;
+ from += size();
+ else if (from > size())
+ from = size()-1;
if (from >= 0) {
- const char *b = d->data();
- const char *n = d->data() + from + 1;
+ const char *b = data();
+ const char *n = b + from + 1;
while (n-- != b)
if (*n == ch)
return n - b;
@@ -2827,7 +2770,7 @@ int QByteArray::count(const QByteArray &ba) const
{
int num = 0;
int i = -1;
- if (d->size > 500 && ba.d->size > 5) {
+ if (size() > 500 && ba.size() > 5) {
QByteArrayMatcher matcher(ba);
while ((i = matcher.indexIn(*this, i + 1)) != -1)
++num;
@@ -2862,8 +2805,8 @@ int QByteArray::count(const char *str) const
int QByteArray::count(char ch) const
{
int num = 0;
- const char *i = d->data() + d->size;
- const char *b = d->data();
+ const char *i = end();
+ const char *b = begin();
while (i != b)
if (*--i == ch)
++num;
@@ -2913,11 +2856,11 @@ int QByteArray::count(char ch) const
*/
bool QByteArray::startsWith(const QByteArray &ba) const
{
- if (d == ba.d || ba.d->size == 0)
- return true;
- if (d->size < ba.d->size)
+ if (size() < ba.size())
return false;
- return memcmp(d->data(), ba.d->data(), ba.d->size) == 0;
+ if (data() == ba.data() || ba.size() == 0)
+ return true;
+ return memcmp(data(), ba.data(), ba.size()) == 0;
}
/*! \overload
@@ -2930,9 +2873,9 @@ bool QByteArray::startsWith(const char *str) const
if (!str || !*str)
return true;
const int len = int(strlen(str));
- if (d->size < len)
+ if (size() < len)
return false;
- return qstrncmp(d->data(), str, len) == 0;
+ return qstrncmp(data(), str, len) == 0;
}
/*! \overload
@@ -2942,9 +2885,9 @@ bool QByteArray::startsWith(const char *str) const
*/
bool QByteArray::startsWith(char ch) const
{
- if (d->size == 0)
+ if (size() == 0)
return false;
- return d->data()[0] == ch;
+ return data()[0] == ch;
}
/*!
@@ -2958,11 +2901,11 @@ bool QByteArray::startsWith(char ch) const
*/
bool QByteArray::endsWith(const QByteArray &ba) const
{
- if (d == ba.d || ba.d->size == 0)
- return true;
- if (d->size < ba.d->size)
+ if (size() < ba.size())
return false;
- return memcmp(d->data() + d->size - ba.d->size, ba.d->data(), ba.d->size) == 0;
+ if (end() == ba.end() || ba.size() == 0)
+ return true;
+ return memcmp(end() - ba.size(), ba.data(), ba.size()) == 0;
}
/*! \overload
@@ -2975,9 +2918,9 @@ bool QByteArray::endsWith(const char *str) const
if (!str || !*str)
return true;
const int len = int(strlen(str));
- if (d->size < len)
+ if (size() < len)
return false;
- return qstrncmp(d->data() + d->size - len, str, len) == 0;
+ return qstrncmp(end() - len, str, len) == 0;
}
/*
@@ -3059,9 +3002,9 @@ bool QByteArray::isLower() const
*/
bool QByteArray::endsWith(char ch) const
{
- if (d->size == 0)
+ if (size() == 0)
return false;
- return d->data()[d->size - 1] == ch;
+ return data()[size() - 1] == ch;
}
/*!
@@ -3079,11 +3022,11 @@ bool QByteArray::endsWith(char ch) const
QByteArray QByteArray::left(int len) const
{
- if (len >= d->size)
+ if (len >= size())
return *this;
if (len < 0)
len = 0;
- return QByteArray(d->data(), len);
+ return QByteArray(data(), len);
}
/*!
@@ -3101,11 +3044,11 @@ QByteArray QByteArray::left(int len) const
QByteArray QByteArray::right(int len) const
{
- if (len >= d->size)
+ if (len >= size())
return *this;
if (len < 0)
len = 0;
- return QByteArray(d->data() + d->size - len, len);
+ return QByteArray(end() - len, len);
}
/*!
@@ -3130,13 +3073,14 @@ QByteArray QByteArray::mid(int pos, int len) const
return QByteArray();
case QContainerImplHelper::Empty:
{
- QByteArrayDataPtr empty = { Data::allocate(0) };
+ auto alloc = Data::allocate(0);
+ QByteArray::DataPointer empty = { alloc.first, alloc.second, 0 };
return QByteArray(empty);
}
case QContainerImplHelper::Full:
return *this;
case QContainerImplHelper::Subset:
- return QByteArray(d->data() + pos, len);
+ return QByteArray(d.data() + pos, len);
}
Q_UNREACHABLE();
return QByteArray();
@@ -3239,9 +3183,7 @@ QByteArray QByteArray::toUpper_helper(QByteArray &a)
void QByteArray::clear()
{
- if (!d->ref.deref())
- Data::deallocate(d);
- d = Data::sharedNull();
+ d.clear();
}
#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
@@ -3718,13 +3660,13 @@ QByteArray QByteArray::trimmed_helper(QByteArray &a)
QByteArray QByteArray::leftJustified(int width, char fill, bool truncate) const
{
QByteArray result;
- int len = d->size;
+ int len = size();
int padlen = width - len;
if (padlen > 0) {
result.resize(len+padlen);
if (len)
- memcpy(result.d->data(), d->data(), len);
- memset(result.d->data()+len, fill, padlen);
+ memcpy(result.d.data(), data(), len);
+ memset(result.d.data()+len, fill, padlen);
} else {
if (truncate)
result = left(width);
@@ -3755,13 +3697,13 @@ QByteArray QByteArray::leftJustified(int width, char fill, bool truncate) const
QByteArray QByteArray::rightJustified(int width, char fill, bool truncate) const
{
QByteArray result;
- int len = d->size;
+ int len = size();
int padlen = width - len;
if (padlen > 0) {
result.resize(len+padlen);
if (len)
- memcpy(result.d->data()+padlen, data(), len);
- memset(result.d->data(), fill, padlen);
+ memcpy(result.d.data()+padlen, data(), len);
+ memset(result.d.data(), fill, padlen);
} else {
if (truncate)
result = left(width);
@@ -3771,7 +3713,10 @@ QByteArray QByteArray::rightJustified(int width, char fill, bool truncate) const
return result;
}
-bool QByteArray::isNull() const { return d == QArrayData::sharedNull(); }
+bool QByteArray::isNull() const
+{
+ return d->isNull();
+}
static qlonglong toIntegral_helper(const char *data, bool *ok, int base, qlonglong)
{
@@ -4098,19 +4043,19 @@ QByteArray QByteArray::toBase64(Base64Options options) const
const char padchar = '=';
int padlen = 0;
- QByteArray tmp((d->size + 2) / 3 * 4, Qt::Uninitialized);
+ QByteArray tmp((size() + 2) / 3 * 4, Qt::Uninitialized);
int i = 0;
char *out = tmp.data();
- while (i < d->size) {
+ while (i < size()) {
// encode 3 bytes at a time
int chunk = 0;
- chunk |= int(uchar(d->data()[i++])) << 16;
- if (i == d->size) {
+ chunk |= int(uchar(data()[i++])) << 16;
+ if (i == size()) {
padlen = 2;
} else {
- chunk |= int(uchar(d->data()[i++])) << 8;
- if (i == d->size)
+ chunk |= int(uchar(data()[i++])) << 8;
+ if (i == size())
padlen = 1;
else
chunk |= int(uchar(data()[i++]));
@@ -4440,17 +4385,14 @@ QByteArray QByteArray::number(double n, char f, int prec)
QByteArray QByteArray::fromRawData(const char *data, int size)
{
- Data *x;
+ QByteArray::DataPointer x;
if (!data) {
- x = Data::sharedNull();
} else if (!size) {
- x = Data::allocate(0);
+ x = DataPointer(Data::allocate(0), 0);
} else {
x = Data::fromRawData(data, size);
- Q_CHECK_PTR(x);
}
- QByteArrayDataPtr dataPtr = { x };
- return QByteArray(dataPtr);
+ return QByteArray(x);
}
/*!
@@ -4469,77 +4411,178 @@ QByteArray QByteArray::fromRawData(const char *data, int size)
*/
QByteArray &QByteArray::setRawData(const char *data, uint size)
{
- if (d->ref.isShared() || d->alloc) {
- *this = fromRawData(data, size);
- } else {
- if (data) {
- d->size = size;
- d->offset = data - reinterpret_cast<char *>(d);
- } else {
- d->offset = sizeof(QByteArrayData);
- d->size = 0;
- }
+ if (!data || !size) {
+ clear();
}
+// else if (d->isShared() || (d->flags() & Data::RawDataType) == 0) {
+ *this = fromRawData(data, size);
+// } else {
+// d.size = size;
+// d.data() = const_cast<char *>(data);
+// }
return *this;
}
-/*!
- \since 5.2
-
- Returns a decoded copy of the Base64 array \a base64, using the alphabet
- defined by \a options. Input is not checked for validity; invalid
- characters in the input are skipped, enabling the decoding process to
- continue with subsequent characters.
-
- For example:
-
- \snippet code/src_corelib_tools_qbytearray.cpp 44
-
- The algorithm used to decode Base64-encoded data is defined in \l{RFC 4648}.
+namespace {
+struct fromBase64_helper_result {
+ qsizetype decodedLength;
+ QByteArray::Base64DecodingStatus status;
+};
- \sa toBase64()
-*/
-QByteArray QByteArray::fromBase64(const QByteArray &base64, Base64Options options)
+fromBase64_helper_result fromBase64_helper(const char *input, qsizetype inputSize,
+ char *output /* may alias input */,
+ QByteArray::Base64Options options)
{
+ fromBase64_helper_result result{ 0, QByteArray::Base64DecodingStatus::Ok };
+
unsigned int buf = 0;
int nbits = 0;
- QByteArray tmp((base64.size() * 3) / 4, Qt::Uninitialized);
- int offset = 0;
- for (int i = 0; i < base64.size(); ++i) {
- int ch = base64.at(i);
+ qsizetype offset = 0;
+ for (qsizetype i = 0; i < inputSize; ++i) {
+ int ch = input[i];
int d;
- if (ch >= 'A' && ch <= 'Z')
+ if (ch >= 'A' && ch <= 'Z') {
d = ch - 'A';
- else if (ch >= 'a' && ch <= 'z')
+ } else if (ch >= 'a' && ch <= 'z') {
d = ch - 'a' + 26;
- else if (ch >= '0' && ch <= '9')
+ } else if (ch >= '0' && ch <= '9') {
d = ch - '0' + 52;
- else if (ch == '+' && (options & Base64UrlEncoding) == 0)
+ } else if (ch == '+' && (options & QByteArray::Base64UrlEncoding) == 0) {
d = 62;
- else if (ch == '-' && (options & Base64UrlEncoding) != 0)
+ } else if (ch == '-' && (options & QByteArray::Base64UrlEncoding) != 0) {
d = 62;
- else if (ch == '/' && (options & Base64UrlEncoding) == 0)
+ } else if (ch == '/' && (options & QByteArray::Base64UrlEncoding) == 0) {
d = 63;
- else if (ch == '_' && (options & Base64UrlEncoding) != 0)
+ } else if (ch == '_' && (options & QByteArray::Base64UrlEncoding) != 0) {
d = 63;
- else
- d = -1;
+ } else {
+ if (options & QByteArray::AbortOnBase64DecodingErrors) {
+ if (ch == '=') {
+ // can have 1 or 2 '=' signs, in both cases padding base64Size to
+ // a multiple of 4. Any other case is illegal.
+ if ((inputSize % 4) != 0) {
+ result.status = QByteArray::Base64DecodingStatus::IllegalInputLength;
+ return result;
+ } else if ((i == inputSize - 1) ||
+ (i == inputSize - 2 && input[++i] == '=')) {
+ d = -1; // ... and exit the loop, normally
+ } else {
+ result.status = QByteArray::Base64DecodingStatus::IllegalPadding;
+ return result;
+ }
+ } else {
+ result.status = QByteArray::Base64DecodingStatus::IllegalCharacter;
+ return result;
+ }
+ } else {
+ d = -1;
+ }
+ }
if (d != -1) {
buf = (buf << 6) | d;
nbits += 6;
if (nbits >= 8) {
nbits -= 8;
- tmp[offset++] = buf >> nbits;
+ Q_ASSERT(offset < i);
+ output[offset++] = buf >> nbits;
buf &= (1 << nbits) - 1;
}
}
}
- tmp.truncate(offset);
- return tmp;
+ result.decodedLength = offset;
+ return result;
+}
+} // anonymous namespace
+
+/*!
+ \fn QByteArray::FromBase64Result QByteArray::fromBase64Encoding(QByteArray &&base64, Base64Options options)
+ \fn QByteArray::FromBase64Result QByteArray::fromBase64Encoding(const QByteArray &base64, Base64Options options)
+ \since 5.15
+ \overload
+
+ Decodes the Base64 array \a base64, using the options
+ defined by \a options. If \a options contains \c{IgnoreBase64DecodingErrors}
+ (the default), the input is not checked for validity; invalid
+ characters in the input are skipped, enabling the decoding process to
+ continue with subsequent characters. If \a options contains
+ \c{AbortOnBase64DecodingErrors}, then decoding will stop at the first
+ invalid character.
+
+ For example:
+
+ \snippet code/src_corelib_tools_qbytearray.cpp 44ter
+
+ The algorithm used to decode Base64-encoded data is defined in \l{RFC 4648}.
+
+ Returns a QByteArrayFromBase64Result object, containing the decoded
+ data and a flag telling whether decoding was successful. If the
+ \c{AbortOnBase64DecodingErrors} option was passed and the input
+ data was invalid, it is unspecified what the decoded data contains.
+
+ \sa toBase64()
+*/
+QByteArray::FromBase64Result QByteArray::fromBase64Encoding(QByteArray &&base64, Base64Options options)
+{
+ // try to avoid a detach when calling data(), as it would over-allocate
+ // (we need less space when decoding than the one required by the full copy)
+ if (base64.isDetached()) {
+ const auto base64result = fromBase64_helper(base64.data(),
+ base64.size(),
+ base64.data(), // in-place
+ options);
+ base64.truncate(int(base64result.decodedLength));
+ return { std::move(base64), base64result.status };
+ }
+
+ return fromBase64Encoding(base64, options);
+}
+
+
+QByteArray::FromBase64Result QByteArray::fromBase64Encoding(const QByteArray &base64, Base64Options options)
+{
+ const auto base64Size = base64.size();
+ QByteArray result((base64Size * 3) / 4, Qt::Uninitialized);
+ const auto base64result = fromBase64_helper(base64.data(),
+ base64Size,
+ const_cast<char *>(result.constData()),
+ options);
+ result.truncate(int(base64result.decodedLength));
+ return { std::move(result), base64result.status };
+}
+
+/*!
+ \since 5.2
+
+ Returns a decoded copy of the Base64 array \a base64, using the options
+ defined by \a options. If \a options contains \c{IgnoreBase64DecodingErrors}
+ (the default), the input is not checked for validity; invalid
+ characters in the input are skipped, enabling the decoding process to
+ continue with subsequent characters. If \a options contains
+ \c{AbortOnBase64DecodingErrors}, then decoding will stop at the first
+ invalid character.
+
+ For example:
+
+ \snippet code/src_corelib_tools_qbytearray.cpp 44
+
+ The algorithm used to decode Base64-encoded data is defined in \l{RFC 4648}.
+
+ Returns the decoded data, or, if the \c{AbortOnBase64DecodingErrors}
+ option was passed and the input data was invalid, an empty byte array.
+
+ \note The fromBase64Encoding() function is recommended in new code.
+
+ \sa toBase64(), fromBase64Encoding()
+*/
+QByteArray QByteArray::fromBase64(const QByteArray &base64, Base64Options options)
+{
+ if (auto result = fromBase64Encoding(base64, options))
+ return std::move(result.decoded);
+ return QByteArray();
}
/*!
@@ -4591,14 +4634,14 @@ QByteArray QByteArray::fromHex(const QByteArray &hexEncoded)
*/
QByteArray QByteArray::toHex(char separator) const
{
- if (!d->size)
+ if (isEmpty())
return QByteArray();
- const int length = separator ? (d->size * 3 - 1) : (d->size * 2);
+ const int length = separator ? (size() * 3 - 1) : (size() * 2);
QByteArray hex(length, Qt::Uninitialized);
char *hexData = hex.data();
- const uchar *data = (const uchar *)d->data();
- for (int i = 0, o = 0; i < d->size; ++i) {
+ const uchar *data = (const uchar *)this->data();
+ for (int i = 0, o = 0; i < size(); ++i) {
hexData[o++] = QtMiscUtils::toHexLower(data[i] >> 4);
hexData[o++] = QtMiscUtils::toHexLower(data[i] & 0xf);
@@ -4910,41 +4953,87 @@ QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteA
\sa QStringLiteral
*/
-namespace QtPrivate {
-namespace DeprecatedRefClassBehavior {
-void warn(WarningType w, EmittingClass c)
-{
- static const char deprecatedBehaviorString[] =
- "The corresponding behavior is deprecated, and will be changed"
- " in a future version of Qt.";
+/*!
+ \class QByteArray::FromBase64Result
+ \inmodule QtCore
+ \ingroup tools
+ \since 5.15
- const char *emittingClassName = nullptr;
- const char *containerClassName = nullptr;
+ \brief The QByteArray::FromBase64Result class holds the result of
+ a call to QByteArray::fromBase64Encoding.
- switch (c) {
- case EmittingClass::QByteRef:
- emittingClassName = "QByteRef";
- containerClassName = "QByteArray";
- break;
- case EmittingClass::QCharRef:
- emittingClassName = "QCharRef";
- containerClassName = "QString";
- break;
- }
+ Objects of this class can be used to check whether the conversion
+ was successful, and if so, retrieve the decoded QByteArray. The
+ conversion operators defined for QByteArray::FromBase64Result make
+ its usage straightforward:
- switch (w) {
- case WarningType::OutOfRange:
- qWarning("Using %s with an index pointing outside the valid range of a %s. %s",
- emittingClassName, containerClassName, deprecatedBehaviorString);
- break;
- case WarningType::DelayedDetach:
- qWarning("Using %s with on a %s that is not already detached. %s",
- emittingClassName, containerClassName, deprecatedBehaviorString);
- break;
- }
-}
-} // namespace DeprecatedRefClassBehavior
-} // namespace QtPrivate
+ \snippet code/src_corelib_tools_qbytearray.cpp 44ter
+
+ In alternative, it is possible to access the conversion status
+ and the decoded data directly:
+
+ \snippet code/src_corelib_tools_qbytearray.cpp 44quater
+
+ \sa QByteArray::fromBase64
+*/
+
+/*!
+ \variable QByteArray::FromBase64Result::decoded
+
+ Contains the decoded byte array.
+*/
+
+/*!
+ \variable QByteArray::FromBase64Result::decodingStatus
+
+ Contains whether the decoding was successful, expressed as a value
+ of type QByteArray::Base64DecodingStatus.
+*/
+
+/*!
+ \fn QByteArray::FromBase64Result::operator bool() const
+
+ Returns whether the decoding was successful. This is equivalent
+ to checking whether the \c{decodingStatus} member is equal to
+ QByteArray::Base64DecodingStatus::Ok.
+*/
+/*!
+ \fn QByteArray &QByteArray::FromBase64Result::operator*() const
+
+ Returns the decoded byte array.
+*/
+
+/*!
+ \fn bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
+ \relates QByteArray::FromBase64Result
+
+ Returns \c true if \a lhs and \a rhs are equal, otherwise returns \c false.
+
+ \a lhs and \a rhs are equal if and only if they contain the same decoding
+ status and, if the status is QByteArray::Base64DecodingStatus::Ok, if and
+ only if they contain the same decoded data.
+*/
+
+/*!
+ \fn bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
+ \relates QByteArray::FromBase64Result
+
+ Returns \c true if \a lhs and \a rhs are different, otherwise returns \c false.
+*/
+
+/*!
+ \relates QByteArray::FromBase64Result
+
+ Returns the hash value for \a key, using
+ \a seed to seed the calculation.
+*/
+uint qHash(const QByteArray::FromBase64Result &key, uint seed) noexcept
+{
+ QtPrivate::QHashCombine hash;
+ seed = hash(seed, key.decoded);
+ seed = hash(seed, static_cast<int>(key.decodingStatus));
+ return seed;
+}
QT_END_NAMESPACE
diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h
index 1376e26260..e3fec1e62c 100644
--- a/src/corelib/text/qbytearray.h
+++ b/src/corelib/text/qbytearray.h
@@ -44,6 +44,7 @@
#include <QtCore/qrefcount.h>
#include <QtCore/qnamespace.h>
#include <QtCore/qarraydata.h>
+#include <QtCore/qarraydatapointer.h>
#include <QtCore/qcontainerfwd.h>
#include <stdlib.h>
@@ -110,44 +111,20 @@ Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
Q_CORE_EXPORT quint16 qChecksum(const char *s, uint len,
Qt::ChecksumType standard = Qt::ChecksumIso3309);
-class QByteRef;
class QString;
class QDataStream;
-typedef QArrayData QByteArrayData;
-
-template<int N> struct QStaticByteArrayData
-{
- QByteArrayData ba;
- char data[N + 1];
-
- QByteArrayData *data_ptr() const
- {
- Q_ASSERT(ba.ref.isStatic());
- return const_cast<QByteArrayData *>(&ba);
- }
-};
-
-struct QByteArrayDataPtr
-{
- QByteArrayData *ptr;
-};
-
-#define Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \
- Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset)
- /**/
-
-#define Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(size) \
- Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, sizeof(QByteArrayData)) \
- /**/
+using QByteArrayData = QArrayDataPointer<char>;
# define QByteArrayLiteral(str) \
([]() -> QByteArray { \
enum { Size = sizeof(str) - 1 }; \
- static const QStaticByteArrayData<Size> qbytearray_literal = { \
- Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(Size), \
- str }; \
- QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; \
+ static const QArrayData qbytearray_literal = { \
+ Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }; \
+ QByteArrayData holder = { \
+ static_cast<QTypedArrayData<char> *>(const_cast<QArrayData *>(&qbytearray_literal)), \
+ const_cast<char *>(str), \
+ Size }; \
const QByteArray ba(holder); \
return ba; \
}()) \
@@ -155,19 +132,33 @@ struct QByteArrayDataPtr
class Q_CORE_EXPORT QByteArray
{
+public:
+ using DataPointer = QByteArrayData;
private:
typedef QTypedArrayData<char> Data;
+ DataPointer d;
public:
+
enum Base64Option {
Base64Encoding = 0,
Base64UrlEncoding = 1,
KeepTrailingEquals = 0,
- OmitTrailingEquals = 2
+ OmitTrailingEquals = 2,
+
+ IgnoreBase64DecodingErrors = 0,
+ AbortOnBase64DecodingErrors = 4,
};
Q_DECLARE_FLAGS(Base64Options, Base64Option)
+ enum class Base64DecodingStatus {
+ Ok,
+ IllegalInputLength,
+ IllegalCharacter,
+ IllegalPadding,
+ };
+
inline QByteArray() noexcept;
QByteArray(const char *, int size = -1);
QByteArray(int size, char c);
@@ -177,14 +168,13 @@ public:
QByteArray &operator=(const QByteArray &) noexcept;
QByteArray &operator=(const char *str);
- inline QByteArray(QByteArray && other) noexcept : d(other.d) { other.d = Data::sharedNull(); }
+ inline QByteArray(QByteArray && other) noexcept
+ { qSwap(d, other.d); }
inline QByteArray &operator=(QByteArray &&other) noexcept
{ qSwap(d, other.d); return *this; }
-
inline void swap(QByteArray &other) noexcept
{ qSwap(d, other.d); }
- inline int size() const;
inline bool isEmpty() const;
void resize(int size);
@@ -203,18 +193,17 @@ public:
inline const char *constData() const;
inline void detach();
inline bool isDetached() const;
- inline bool isSharedWith(const QByteArray &other) const { return d == other.d; }
+ inline bool isSharedWith(const QByteArray &other) const
+ { return data() == other.data() && size() == other.size(); }
void clear();
inline char at(int i) const;
inline char operator[](int i) const;
- inline char operator[](uint i) const;
- Q_REQUIRED_RESULT inline QByteRef operator[](int i);
- Q_REQUIRED_RESULT inline QByteRef operator[](uint i);
+ Q_REQUIRED_RESULT inline char &operator[](int i);
Q_REQUIRED_RESULT char front() const { return at(0); }
- Q_REQUIRED_RESULT inline QByteRef front();
+ Q_REQUIRED_RESULT inline char &front();
Q_REQUIRED_RESULT char back() const { return at(size() - 1); }
- Q_REQUIRED_RESULT inline QByteRef back();
+ Q_REQUIRED_RESULT inline char &back();
int indexOf(char c, int from = 0) const;
int indexOf(const char *c, int from = 0) const;
@@ -254,7 +243,7 @@ public:
void chop(int n);
#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
-# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_CPP_ATTRIBUTE(nodiscard)
+# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !__has_cpp_attribute(nodiscard)
// required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941
# pragma push_macro("Q_REQUIRED_RESULT")
# undef Q_REQUIRED_RESULT
@@ -377,8 +366,11 @@ public:
Q_REQUIRED_RESULT static QByteArray number(qulonglong, int base = 10);
Q_REQUIRED_RESULT static QByteArray number(double, char f = 'g', int prec = 6);
Q_REQUIRED_RESULT static QByteArray fromRawData(const char *, int size);
- Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64,
- Base64Options options = Base64Encoding);
+
+ class FromBase64Result;
+ Q_REQUIRED_RESULT static FromBase64Result fromBase64Encoding(QByteArray &&base64, Base64Options options = Base64Encoding);
+ Q_REQUIRED_RESULT static FromBase64Result fromBase64Encoding(const QByteArray &base64, Base64Options options = Base64Encoding);
+ Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64, Base64Options options = Base64Encoding);
Q_REQUIRED_RESULT static QByteArray fromHex(const QByteArray &hexEncoded);
Q_REQUIRED_RESULT static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent = '%');
@@ -433,19 +425,20 @@ public:
static inline QByteArray fromStdString(const std::string &s);
inline std::string toStdString() const;
- inline int count() const { return d->size; }
- int length() const { return d->size; }
+ inline int size() const { return d->size; }
+ inline int count() const { return size(); }
+ inline int length() const { return size(); }
bool isNull() const;
- inline QByteArray(QByteArrayDataPtr dd)
- : d(static_cast<Data *>(dd.ptr))
+ inline DataPointer &data_ptr() { return d; }
+ explicit inline QByteArray(const DataPointer &dd)
+ : d(dd)
{
}
private:
operator QNoImplicitBoolCast() const;
- Data *d;
- void reallocData(uint alloc, Data::AllocationOptions options);
+ void reallocData(uint alloc, Data::ArrayOptions options);
void expand(int i);
QByteArray nulTerminated() const;
@@ -458,171 +451,86 @@ private:
static QByteArray simplified_helper(const QByteArray &a);
static QByteArray simplified_helper(QByteArray &a);
- friend class QByteRef;
friend class QString;
friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes);
-public:
- typedef Data * DataPtr;
- inline DataPtr &data_ptr() { return d; }
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QByteArray::Base64Options)
-inline QByteArray::QByteArray() noexcept : d(Data::sharedNull()) { }
-inline QByteArray::~QByteArray() { if (!d->ref.deref()) Data::deallocate(d); }
-inline int QByteArray::size() const
-{ return d->size; }
+inline QByteArray::QByteArray() noexcept {}
+inline QByteArray::~QByteArray() {}
inline char QByteArray::at(int i) const
-{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
+{ Q_ASSERT(uint(i) < uint(size())); return d.data()[i]; }
inline char QByteArray::operator[](int i) const
-{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
-inline char QByteArray::operator[](uint i) const
-{ Q_ASSERT(i < uint(size())); return d->data()[i]; }
+{ Q_ASSERT(uint(i) < uint(size())); return d.data()[i]; }
inline bool QByteArray::isEmpty() const
-{ return d->size == 0; }
+{ return size() == 0; }
#ifndef QT_NO_CAST_FROM_BYTEARRAY
inline QByteArray::operator const char *() const
-{ return d->data(); }
+{ return data(); }
inline QByteArray::operator const void *() const
-{ return d->data(); }
+{ return data(); }
#endif
inline char *QByteArray::data()
-{ detach(); return d->data(); }
+{ detach(); return d.data(); }
inline const char *QByteArray::data() const
-{ return d->data(); }
+{ return d.data(); }
inline const char *QByteArray::constData() const
-{ return d->data(); }
+{ return d.data(); }
inline void QByteArray::detach()
-{ if (d->ref.isShared() || (d->offset != sizeof(QByteArrayData))) reallocData(uint(d->size) + 1u, d->detachFlags()); }
+{ if (d->needsDetach()) reallocData(uint(size()) + 1u, d->detachFlags()); }
inline bool QByteArray::isDetached() const
-{ return !d->ref.isShared(); }
+{ return !d->isShared(); }
inline QByteArray::QByteArray(const QByteArray &a) noexcept : d(a.d)
-{ d->ref.ref(); }
+{}
inline int QByteArray::capacity() const
-{ return d->alloc ? d->alloc - 1 : 0; }
+{ const auto realCapacity = d->constAllocatedCapacity(); return realCapacity ? int(realCapacity) - 1 : 0; }
inline void QByteArray::reserve(int asize)
{
- if (d->ref.isShared() || uint(asize) + 1u > d->alloc) {
+ if (d->needsDetach() || asize > capacity()) {
reallocData(qMax(uint(size()), uint(asize)) + 1u, d->detachFlags() | Data::CapacityReserved);
} else {
- // cannot set unconditionally, since d could be the shared_null or
- // otherwise static
- d->capacityReserved = true;
+ d->flags() |= Data::CapacityReserved;
}
}
inline void QByteArray::squeeze()
{
- if (d->ref.isShared() || uint(d->size) + 1u < d->alloc) {
- reallocData(uint(d->size) + 1u, d->detachFlags() & ~Data::CapacityReserved);
+ if ((d->flags() & Data::CapacityReserved) == 0)
+ return;
+ if (d->needsDetach() || size() < capacity()) {
+ reallocData(uint(size()) + 1u, d->detachFlags() & ~Data::CapacityReserved);
} else {
- // cannot set unconditionally, since d could be shared_null or
- // otherwise static.
- d->capacityReserved = false;
+ d->flags() &= uint(~Data::CapacityReserved);
}
}
-namespace QtPrivate {
-namespace DeprecatedRefClassBehavior {
- enum class EmittingClass {
- QByteRef,
- QCharRef,
- };
-
- enum class WarningType {
- OutOfRange,
- DelayedDetach,
- };
-
- Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void warn(WarningType w, EmittingClass c);
-} // namespace DeprecatedAssignmentOperatorBehavior
-} // namespace QtPrivate
-
-class
-#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
-Q_CORE_EXPORT
-#endif
-QByteRef { // ### Qt 7: remove
- QByteArray &a;
- int i;
- inline QByteRef(QByteArray &array, int idx)
- : a(array),i(idx) {}
- friend class QByteArray;
-public:
- inline operator char() const
- {
- using namespace QtPrivate::DeprecatedRefClassBehavior;
- if (Q_LIKELY(i < a.d->size))
- return a.d->data()[i];
-#ifdef QT_DEBUG
- warn(WarningType::OutOfRange, EmittingClass::QByteRef);
-#endif
- return char(0);
- }
- inline QByteRef &operator=(char c)
- {
- using namespace QtPrivate::DeprecatedRefClassBehavior;
- if (Q_UNLIKELY(i >= a.d->size)) {
-#ifdef QT_DEBUG
- warn(WarningType::OutOfRange, EmittingClass::QByteRef);
-#endif
- a.expand(i);
- } else {
-#ifdef QT_DEBUG
- if (Q_UNLIKELY(!a.isDetached()))
- warn(WarningType::DelayedDetach, EmittingClass::QByteRef);
-#endif
- a.detach();
- }
- a.d->data()[i] = c;
- return *this;
- }
- inline QByteRef &operator=(const QByteRef &c)
- {
- return operator=(char(c));
- }
- inline bool operator==(char c) const
- { return a.d->data()[i] == c; }
- inline bool operator!=(char c) const
- { return a.d->data()[i] != c; }
- inline bool operator>(char c) const
- { return a.d->data()[i] > c; }
- inline bool operator>=(char c) const
- { return a.d->data()[i] >= c; }
- inline bool operator<(char c) const
- { return a.d->data()[i] < c; }
- inline bool operator<=(char c) const
- { return a.d->data()[i] <= c; }
-};
-
-inline QByteRef QByteArray::operator[](int i)
-{ Q_ASSERT(i >= 0); detach(); return QByteRef(*this, i); }
-inline QByteRef QByteArray::operator[](uint i)
-{ detach(); return QByteRef(*this, i); }
-inline QByteRef QByteArray::front() { return operator[](0); }
-inline QByteRef QByteArray::back() { return operator[](size() - 1); }
+inline char &QByteArray::operator[](int i)
+{ Q_ASSERT(i >= 0 && i < size()); return data()[i]; }
+inline char &QByteArray::front() { return operator[](0); }
+inline char &QByteArray::back() { return operator[](size() - 1); }
inline QByteArray::iterator QByteArray::begin()
-{ detach(); return d->data(); }
+{ return data(); }
inline QByteArray::const_iterator QByteArray::begin() const
-{ return d->data(); }
+{ return data(); }
inline QByteArray::const_iterator QByteArray::cbegin() const
-{ return d->data(); }
+{ return data(); }
inline QByteArray::const_iterator QByteArray::constBegin() const
-{ return d->data(); }
+{ return data(); }
inline QByteArray::iterator QByteArray::end()
-{ detach(); return d->data() + d->size; }
+{ return data() + size(); }
inline QByteArray::const_iterator QByteArray::end() const
-{ return d->data() + d->size; }
+{ return data() + size(); }
inline QByteArray::const_iterator QByteArray::cend() const
-{ return d->data() + d->size; }
+{ return data() + size(); }
inline QByteArray::const_iterator QByteArray::constEnd() const
-{ return d->data() + d->size; }
+{ return data() + size(); }
inline QByteArray &QByteArray::append(int n, char ch)
-{ return insert(d->size, n, ch); }
+{ return insert(size(), n, ch); }
inline QByteArray &QByteArray::prepend(int n, char ch)
{ return insert(0, n, ch); }
inline QByteArray &QByteArray::operator+=(char c)
@@ -747,6 +655,50 @@ inline QByteArray qUncompress(const QByteArray& data)
Q_DECLARE_SHARED(QByteArray)
+class QByteArray::FromBase64Result
+{
+public:
+ QByteArray decoded;
+ QByteArray::Base64DecodingStatus decodingStatus;
+
+ void swap(QByteArray::FromBase64Result &other) noexcept
+ {
+ qSwap(decoded, other.decoded);
+ qSwap(decodingStatus, other.decodingStatus);
+ }
+
+ explicit operator bool() const noexcept { return decodingStatus == QByteArray::Base64DecodingStatus::Ok; }
+
+#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(Q_QDOC)
+ QByteArray &operator*() & noexcept { return decoded; }
+ const QByteArray &operator*() const & noexcept { return decoded; }
+ QByteArray &&operator*() && noexcept { return std::move(decoded); }
+#else
+ QByteArray &operator*() noexcept { return decoded; }
+ const QByteArray &operator*() const noexcept { return decoded; }
+#endif
+};
+
+Q_DECLARE_SHARED(QByteArray::FromBase64Result)
+
+inline bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
+{
+ if (lhs.decodingStatus != rhs.decodingStatus)
+ return false;
+
+ if (lhs.decodingStatus == QByteArray::Base64DecodingStatus::Ok && lhs.decoded != rhs.decoded)
+ return false;
+
+ return true;
+}
+
+inline bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
+{
+ return !operator==(lhs, rhs);
+}
+
+Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QByteArray::FromBase64Result &key, uint seed = 0) noexcept;
+
QT_END_NAMESPACE
#endif // QBYTEARRAY_H
diff --git a/src/corelib/text/qbytearray_p.h b/src/corelib/text/qbytearray_p.h
index 3c6257f786..d6a5e277b3 100644
--- a/src/corelib/text/qbytearray_p.h
+++ b/src/corelib/text/qbytearray_p.h
@@ -58,7 +58,7 @@ QT_BEGIN_NAMESPACE
enum {
// Define as enum to force inlining. Don't expose MaxAllocSize in a public header.
- MaxByteArraySize = MaxAllocSize - sizeof(std::remove_pointer<QByteArray::DataPtr>::type)
+ MaxByteArraySize = MaxAllocSize - sizeof(std::remove_pointer<QByteArray::DataPointer>::type)
};
QT_END_NAMESPACE
diff --git a/src/corelib/text/qbytearraylist.h b/src/corelib/text/qbytearraylist.h
index 4b6c926960..d02fa6d20f 100644
--- a/src/corelib/text/qbytearraylist.h
+++ b/src/corelib/text/qbytearraylist.h
@@ -80,9 +80,6 @@ public:
inline QByteArray join(char sep) const
{ return QtPrivate::QByteArrayList_join(self(), &sep, 1); }
- inline int indexOf(const char *needle, int from = 0) const
- { return QtPrivate::QByteArrayList_indexOf(self(), needle, from); }
-
private:
typedef QVector<QByteArray> Self;
Self *self() { return static_cast<Self *>(this); }
diff --git a/src/corelib/text/qchar.cpp b/src/corelib/text/qchar.cpp
index 9b03a93278..61b61125b1 100644
--- a/src/corelib/text/qchar.cpp
+++ b/src/corelib/text/qchar.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -189,6 +189,9 @@ QT_BEGIN_NAMESPACE
\value Unicode_8_0 Version 8.0 Since Qt 5.6
\value Unicode_9_0 Version 9.0 Since Qt 5.11
\value Unicode_10_0 Version 10.0 Since Qt 5.11
+ \value Unicode_11_0 Version 11.0 Since Qt 5.15
+ \value Unicode_12_0 Version 12.0 Since Qt 5.15
+ \value Unicode_12_1 Version 12.1 Since Qt 5.15
\value Unicode_Unassigned The value is not assigned to any character
in version 8.0 of Unicode.
@@ -288,145 +291,156 @@ QT_BEGIN_NAMESPACE
\value Script_Common For characters that may be used with multiple scripts
and that do not inherit their script from the preceding characters.
- \value Script_Latin
- \value Script_Greek
- \value Script_Cyrillic
- \value Script_Armenian
- \value Script_Hebrew
+ \value Script_Adlam Since Qt 5.11
+ \value Script_Ahom Since Qt 5.6
+ \value Script_AnatolianHieroglyphs Since Qt 5.6
\value Script_Arabic
- \value Script_Syriac
- \value Script_Thaana
- \value Script_Devanagari
+ \value Script_Armenian
+ \value Script_Avestan
+ \value Script_Balinese
+ \value Script_Bamum
+ \value Script_BassaVah Since Qt 5.5
+ \value Script_Batak
\value Script_Bengali
- \value Script_Gurmukhi
- \value Script_Gujarati
- \value Script_Oriya
- \value Script_Tamil
- \value Script_Telugu
- \value Script_Kannada
- \value Script_Malayalam
- \value Script_Sinhala
- \value Script_Thai
- \value Script_Lao
- \value Script_Tibetan
- \value Script_Myanmar
- \value Script_Georgian
- \value Script_Hangul
- \value Script_Ethiopic
- \value Script_Cherokee
- \value Script_CanadianAboriginal
- \value Script_Ogham
- \value Script_Runic
- \value Script_Khmer
- \value Script_Mongolian
- \value Script_Hiragana
- \value Script_Katakana
+ \value Script_Bhaiksuki Since Qt 5.11
\value Script_Bopomofo
- \value Script_Han
- \value Script_Yi
- \value Script_OldItalic
- \value Script_Gothic
- \value Script_Deseret
- \value Script_Tagalog
- \value Script_Hanunoo
- \value Script_Buhid
- \value Script_Tagbanwa
- \value Script_Coptic
- \value Script_Limbu
- \value Script_TaiLe
- \value Script_LinearB
- \value Script_Ugaritic
- \value Script_Shavian
- \value Script_Osmanya
- \value Script_Cypriot
+ \value Script_Brahmi
\value Script_Braille
\value Script_Buginese
- \value Script_NewTaiLue
- \value Script_Glagolitic
- \value Script_Tifinagh
- \value Script_SylotiNagri
- \value Script_OldPersian
- \value Script_Kharoshthi
- \value Script_Balinese
- \value Script_Cuneiform
- \value Script_Phoenician
- \value Script_PhagsPa
- \value Script_Nko
- \value Script_Sundanese
- \value Script_Lepcha
- \value Script_OlChiki
- \value Script_Vai
- \value Script_Saurashtra
- \value Script_KayahLi
- \value Script_Rejang
- \value Script_Lycian
+ \value Script_Buhid
+ \value Script_CanadianAboriginal
\value Script_Carian
- \value Script_Lydian
+ \value Script_CaucasianAlbanian Since Qt 5.5
+ \value Script_Chakma
\value Script_Cham
- \value Script_TaiTham
- \value Script_TaiViet
- \value Script_Avestan
+ \value Script_Cherokee
+ \value Script_Coptic
+ \value Script_Cuneiform
+ \value Script_Cypriot
+ \value Script_Cyrillic
+ \value Script_Deseret
+ \value Script_Devanagari
+ \value Script_Dogra Since Qt 5.15
+ \value Script_Duployan Since Qt 5.5
\value Script_EgyptianHieroglyphs
- \value Script_Samaritan
- \value Script_Lisu
- \value Script_Bamum
- \value Script_Javanese
- \value Script_MeeteiMayek
+ \value Script_Elbasan Since Qt 5.5
+ \value Script_Elymaic Since Qt 5.15
+ \value Script_Ethiopic
+ \value Script_Georgian
+ \value Script_Glagolitic
+ \value Script_Gothic
+ \value Script_Grantha Since Qt 5.5
+ \value Script_Greek
+ \value Script_Gujarati
+ \value Script_GunjalaGondi Since Qt 5.15
+ \value Script_Gurmukhi
+ \value Script_Han
+ \value Script_Hangul
+ \value Script_HanifiRohingya Since Qt 5.15
+ \value Script_Hanunoo
+ \value Script_Hatran Since Qt 5.6
+ \value Script_Hebrew
+ \value Script_Hiragana
\value Script_ImperialAramaic
- \value Script_OldSouthArabian
- \value Script_InscriptionalParthian
\value Script_InscriptionalPahlavi
- \value Script_OldTurkic
+ \value Script_InscriptionalParthian
+ \value Script_Javanese
\value Script_Kaithi
- \value Script_Batak
- \value Script_Brahmi
+ \value Script_Kannada
+ \value Script_Katakana
+ \value Script_KayahLi
+ \value Script_Kharoshthi
+ \value Script_Khmer
+ \value Script_Khojki Since Qt 5.5
+ \value Script_Khudawadi Since Qt 5.5
+ \value Script_Lao
+ \value Script_Latin
+ \value Script_Lepcha
+ \value Script_Limbu
+ \value Script_LinearA Since Qt 5.5
+ \value Script_LinearB
+ \value Script_Lisu
+ \value Script_Lycian
+ \value Script_Lydian
+ \value Script_Mahajani Since Qt 5.5
+ \value Script_Makasar Since Qt 5.15
+ \value Script_Malayalam
\value Script_Mandaic
- \value Script_Chakma
+ \value Script_Manichaean Since Qt 5.5
+ \value Script_Marchen Since Qt 5.11
+ \value Script_MasaramGondi Since Qt 5.11
+ \value Script_Medefaidrin Since Qt 5.15
+ \value Script_MeeteiMayek
+ \value Script_MendeKikakui Since Qt 5.5
\value Script_MeroiticCursive
\value Script_MeroiticHieroglyphs
\value Script_Miao
+ \value Script_Modi Since Qt 5.5
+ \value Script_Mongolian
+ \value Script_Mro Since Qt 5.5
+ \value Script_Multani Since Qt 5.6
+ \value Script_Myanmar
+ \value Script_Nabataean Since Qt 5.5
+ \value Script_Nandinagari Since Qt 5.15
+ \value Script_Newa Since Qt 5.11
+ \value Script_NewTaiLue
+ \value Script_Nko
+ \value Script_Nushu Since Qt 5.11
+ \value Script_NyiakengPuachueHmong Since Qt 5.15
+ \value Script_Ogham
+ \value Script_OlChiki
+ \value Script_OldHungarian Since Qt 5.6
+ \value Script_OldItalic
+ \value Script_OldNorthArabian Since Qt 5.5
+ \value Script_OldPermic Since Qt 5.5
+ \value Script_OldPersian
+ \value Script_OldSogdian Since Qt 5.15
+ \value Script_OldSouthArabian
+ \value Script_OldTurkic
+ \value Script_Oriya
+ \value Script_Osage Since Qt 5.11
+ \value Script_Osmanya
+ \value Script_PahawhHmong Since Qt 5.5
+ \value Script_Palmyrene Since Qt 5.5
+ \value Script_PauCinHau Since Qt 5.5
+ \value Script_PhagsPa
+ \value Script_Phoenician
+ \value Script_PsalterPahlavi Since Qt 5.5
+ \value Script_Rejang
+ \value Script_Runic
+ \value Script_Samaritan
+ \value Script_Saurashtra
\value Script_Sharada
+ \value Script_Shavian
+ \value Script_Siddham Since Qt 5.5
+ \value Script_SignWriting Since Qt 5.6
+ \value Script_Sinhala
+ \value Script_Sogdian Since Qt 5.15
\value Script_SoraSompeng
+ \value Script_Soyombo Since Qt 5.11
+ \value Script_Sundanese
+ \value Script_SylotiNagri
+ \value Script_Syriac
+ \value Script_Tagalog
+ \value Script_Tagbanwa
+ \value Script_TaiLe
+ \value Script_TaiTham
+ \value Script_TaiViet
\value Script_Takri
- \value Script_CaucasianAlbanian
- \value Script_BassaVah
- \value Script_Duployan
- \value Script_Elbasan
- \value Script_Grantha
- \value Script_PahawhHmong
- \value Script_Khojki
- \value Script_LinearA
- \value Script_Mahajani
- \value Script_Manichaean
- \value Script_MendeKikakui
- \value Script_Modi
- \value Script_Mro
- \value Script_OldNorthArabian
- \value Script_Nabataean
- \value Script_Palmyrene
- \value Script_PauCinHau
- \value Script_OldPermic
- \value Script_PsalterPahlavi
- \value Script_Siddham
- \value Script_Khudawadi
- \value Script_Tirhuta
- \value Script_WarangCiti
- \value Script_Ahom
- \value Script_AnatolianHieroglyphs
- \value Script_Hatran
- \value Script_Multani
- \value Script_OldHungarian
- \value Script_SignWriting
- \value Script_Adlam
- \value Script_Bhaiksuki
- \value Script_Marchen
- \value Script_Newa
- \value Script_Osage
- \value Script_Tangut
- \value Script_MasaramGondi
- \value Script_Nushu
- \value Script_Soyombo
- \value Script_ZanabazarSquare
+ \value Script_Tamil
+ \value Script_Tangut Since Qt 5.11
+ \value Script_Telugu
+ \value Script_Thaana
+ \value Script_Thai
+ \value Script_Tibetan
+ \value Script_Tifinagh
+ \value Script_Tirhuta Since Qt 5.5
+ \value Script_Ugaritic
+ \value Script_Vai
+ \value Script_Wancho Since Qt 5.15
+ \value Script_WarangCiti Since Qt 5.5
+ \value Script_Yi
+ \value Script_ZanabazarSquare Since Qt 5.11
\omitvalue ScriptCount
diff --git a/src/corelib/text/qchar.h b/src/corelib/text/qchar.h
index 85a380e7cf..9d0741af8b 100644
--- a/src/corelib/text/qchar.h
+++ b/src/corelib/text/qchar.h
@@ -437,9 +437,8 @@ public:
Unicode_10_0,
Unicode_11_0,
Unicode_12_0,
- Unicode_12_1,
+ Unicode_12_1
};
- // ****** WHEN ADDING FUNCTIONS, CONSIDER ADDING TO QCharRef TOO
inline Category category() const noexcept { return QChar::category(ucs); }
inline Direction direction() const noexcept { return QChar::direction(ucs); }
diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp
index 75a5bc802e..93a8abb1ce 100644
--- a/src/corelib/text/qlocale.cpp
+++ b/src/corelib/text/qlocale.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2019 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
@@ -79,7 +79,7 @@
QT_BEGIN_NAMESPACE
#ifndef QT_NO_SYSTEMLOCALE
-static QSystemLocale *_systemLocale = 0;
+static QSystemLocale *_systemLocale = nullptr;
class QSystemLocaleSingleton: public QSystemLocale
{
public:
@@ -651,7 +651,6 @@ int qt_repeatCount(QStringView s)
}
static const QLocaleData *default_data = nullptr;
-static QLocale::NumberOptions default_number_options = QLocale::DefaultNumberOptions;
static const QLocaleData *const c_data = locale_data;
static QLocalePrivate *c_private()
@@ -695,7 +694,7 @@ QSystemLocale::QSystemLocale(bool)
QSystemLocale::~QSystemLocale()
{
if (_systemLocale == this) {
- _systemLocale = 0;
+ _systemLocale = nullptr;
globalLocaleData.m_language_id = 0;
}
@@ -834,7 +833,7 @@ QDataStream &operator>>(QDataStream &ds, QLocale &l)
static const int locale_data_size = sizeof(locale_data)/sizeof(QLocaleData) - 1;
Q_GLOBAL_STATIC_WITH_ARGS(QSharedDataPointer<QLocalePrivate>, defaultLocalePrivate,
- (QLocalePrivate::create(defaultData(), default_number_options)))
+ (QLocalePrivate::create(defaultData())))
Q_GLOBAL_STATIC_WITH_ARGS(QExplicitlySharedDataPointer<QLocalePrivate>, systemLocalePrivate,
(QLocalePrivate::create(systemData())))
@@ -862,8 +861,9 @@ static QLocalePrivate *findLocalePrivate(QLocale::Language language, QLocale::Sc
QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions;
// If not found, should default to system
- if (data->m_language_id == QLocale::C && language != QLocale::C) {
- numberOptions = default_number_options;
+ if (data->m_language_id == QLocale::C) {
+ if (defaultLocalePrivate.exists())
+ numberOptions = defaultLocalePrivate->data()->m_numberOptions;
data = defaultData();
}
return QLocalePrivate::create(data, offset, numberOptions);
@@ -1048,6 +1048,8 @@ uint qHash(const QLocale &key, uint seed) noexcept
Sets the \a options related to number conversions for this
QLocale instance.
+
+ \sa numberOptions()
*/
void QLocale::setNumberOptions(NumberOptions options)
{
@@ -1060,7 +1062,10 @@ void QLocale::setNumberOptions(NumberOptions options)
Returns the options related to number conversions for this
QLocale instance.
- By default, no options are set for the standard locales.
+ By default, no options are set for the standard locales, except
+ for the "C" locale, which has OmitGroupSeparator set by default.
+
+ \sa setNumberOptions(), toString(), groupSeparator()
*/
QLocale::NumberOptions QLocale::numberOptions() const
{
@@ -1170,12 +1175,9 @@ QString QLocale::createSeparatedList(const QStringList &list) const
void QLocale::setDefault(const QLocale &locale)
{
default_data = locale.d->m_data;
- default_number_options = locale.numberOptions();
- if (defaultLocalePrivate.exists()) {
- // update the cached private
+ if (defaultLocalePrivate.exists()) // update the cached private
*defaultLocalePrivate = locale.d;
- }
}
/*!
@@ -1962,14 +1964,15 @@ double QLocale::toDouble(QStringView s, bool *ok) const
/*!
Returns a localized string representation of \a i.
- \sa toLongLong()
+ \sa toLongLong(), numberOptions(), zeroDigit(), positiveSign()
*/
QString QLocale::toString(qlonglong i) const
{
int flags = d->m_numberOptions & OmitGroupSeparator
? 0
- : QLocaleData::ThousandsGroup;
+ : (d->m_data->m_country_id == Country::India)
+ ? QLocaleData::IndianNumberGrouping : QLocaleData::ThousandsGroup;
return d->m_data->longLongToString(i, -1, 10, -1, flags);
}
@@ -1977,14 +1980,15 @@ QString QLocale::toString(qlonglong i) const
/*!
\overload
- \sa toULongLong()
+ \sa toULongLong(), numberOptions(), zeroDigit(), positiveSign()
*/
QString QLocale::toString(qulonglong i) const
{
int flags = d->m_numberOptions & OmitGroupSeparator
? 0
- : QLocaleData::ThousandsGroup;
+ : (d->m_data->m_country_id == Country::India)
+ ? QLocaleData::IndianNumberGrouping : QLocaleData::ThousandsGroup;
return d->m_data->unsLongLongToString(i, -1, 10, -1, flags);
}
@@ -2429,7 +2433,7 @@ QTime QLocale::toTime(const QString &string, const QString &format, QCalendar ca
QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString, cal);
dt.setDefaultLocale(*this);
if (dt.parseFormat(format))
- dt.fromString(string, 0, &time);
+ dt.fromString(string, nullptr, &time);
#else
Q_UNUSED(cal);
Q_UNUSED(string);
@@ -2468,7 +2472,7 @@ QDate QLocale::toDate(const QString &string, const QString &format, QCalendar ca
QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString, cal);
dt.setDefaultLocale(*this);
if (dt.parseFormat(format))
- dt.fromString(string, &date, 0);
+ dt.fromString(string, &date, nullptr);
#else
Q_UNUSED(string);
Q_UNUSED(format);
@@ -2523,6 +2527,12 @@ QDateTime QLocale::toDateTime(const QString &string, const QString &format, QCal
\since 4.1
Returns the decimal point character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa groupSeparator(), toString()
*/
QChar QLocale::decimalPoint() const
{
@@ -2533,6 +2543,12 @@ QChar QLocale::decimalPoint() const
\since 4.1
Returns the group separator character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa decimalPoint(), toString()
*/
QChar QLocale::groupSeparator() const
{
@@ -2543,6 +2559,12 @@ QChar QLocale::groupSeparator() const
\since 4.1
Returns the percent character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa toString()
*/
QChar QLocale::percent() const
{
@@ -2553,6 +2575,12 @@ QChar QLocale::percent() const
\since 4.1
Returns the zero digit character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa toString()
*/
QChar QLocale::zeroDigit() const
{
@@ -2563,6 +2591,12 @@ QChar QLocale::zeroDigit() const
\since 4.1
Returns the negative sign character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa positiveSign(), toString()
*/
QChar QLocale::negativeSign() const
{
@@ -2573,6 +2607,12 @@ QChar QLocale::negativeSign() const
\since 4.5
Returns the positive sign character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa negativeSign(), toString()
*/
QChar QLocale::positiveSign() const
{
@@ -2582,7 +2622,14 @@ QChar QLocale::positiveSign() const
/*!
\since 4.1
- Returns the exponential character of this locale.
+ Returns the exponential character of this locale, used to separate exponent
+ from mantissa in some floating-point numeric representations.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa toString(double, char, int)
*/
QChar QLocale::exponential() const
{
@@ -2607,7 +2654,7 @@ static char qToLower(char c)
\a f and \a prec have the same meaning as in QString::number(double, char, int).
- \sa toDouble()
+ \sa toDouble(), numberOptions(), exponential(), decimalPoint(), zeroDigit(), positiveSign(), percent()
*/
QString QLocale::toString(double i, char f, int prec) const
@@ -3065,7 +3112,7 @@ QList<Qt::DayOfWeek> QLocale::weekdays() const
if (d->m_data == systemData()) {
QVariant res = systemLocale()->query(QSystemLocale::Weekdays, QVariant());
if (!res.isNull())
- return static_cast<QList<Qt::DayOfWeek> >(res.value<QList<Qt::DayOfWeek> >());
+ return static_cast<QList<Qt::DayOfWeek> >(qvariant_cast<QList<Qt::DayOfWeek> >(res));
}
#endif
QList<Qt::DayOfWeek> weekdays;
@@ -3626,10 +3673,19 @@ QT_WARNING_DISABLE_MSVC(4146)
QT_WARNING_POP
uint cnt_thousand_sep = 0;
- if (flags & ThousandsGroup && base == 10) {
- for (int i = num_str.length() - 3; i > 0; i -= 3) {
- num_str.insert(i, group);
- ++cnt_thousand_sep;
+ if (base == 10){
+ if (flags & ThousandsGroup) {
+ for (int i = num_str.length() - 3; i > 0; i -= 3) {
+ num_str.insert(i, group);
+ ++cnt_thousand_sep;
+ }
+ } else if (flags & IndianNumberGrouping) {
+ if (num_str.length() > 3)
+ num_str.insert(num_str.length() - 3 , group);
+ for (int i = num_str.length() - 6; i > 0; i -= 2) {
+ num_str.insert(i, group);
+ ++cnt_thousand_sep;
+ }
}
}
@@ -3713,10 +3769,19 @@ QString QLocaleData::unsLongLongToString(const QChar zero, const QChar group,
}
uint cnt_thousand_sep = 0;
- if (flags & ThousandsGroup && base == 10) {
- for (int i = num_str.length() - 3; i > 0; i -=3) {
- num_str.insert(i, group);
- ++cnt_thousand_sep;
+ if (base == 10) {
+ if (flags & ThousandsGroup) {
+ for (int i = num_str.length() - 3; i > 0; i -=3) {
+ num_str.insert(i, group);
+ ++cnt_thousand_sep;
+ }
+ } else if (flags & IndianNumberGrouping) {
+ if (num_str.length() > 3)
+ num_str.insert(num_str.length() - 3 , group);
+ for (int i = num_str.length() - 6; i > 0; i -= 2) {
+ num_str.insert(i, group);
+ ++cnt_thousand_sep;
+ }
}
}
@@ -3851,7 +3916,10 @@ bool QLocaleData::numberToCLocale(QStringView s, QLocale::NumberOptions number_o
// check distance from the last separator or from the beginning of the digits
// ### FIXME: Some locales allow other groupings!
// See https://en.wikipedia.org/wiki/Thousands_separator
- if (last_separator_idx != -1 && idx - last_separator_idx != 4)
+ if (m_country_id == QLocale::India) {
+ if (last_separator_idx != -1 && idx - last_separator_idx != 3)
+ return false;
+ } else if (last_separator_idx != -1 && idx - last_separator_idx != 4)
return false;
if (last_separator_idx == -1
&& (start_of_digits_idx == -1 || idx - start_of_digits_idx > 3)) {
diff --git a/src/corelib/text/qlocale.h b/src/corelib/text/qlocale.h
index 8b73eb5493..513e0f097b 100644
--- a/src/corelib/text/qlocale.h
+++ b/src/corelib/text/qlocale.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -1044,8 +1044,8 @@ public:
QDateTime toDateTime(const QString &string, const QString &format, QCalendar cal) const;
#endif
- // ### Qt 5: We need to return QString from these function since
- // unicode data contains several characters for these fields.
+ // ### Qt 6: We need to return QString from these function since
+ // UTF-16 may need surrogate pairs to represent these fields.
QChar decimalPoint() const;
QChar groupSeparator() const;
QChar percent() const;
diff --git a/src/corelib/text/qlocale.qdoc b/src/corelib/text/qlocale.qdoc
index 6c9d6f9d11..592f9d0785 100644
--- a/src/corelib/text/qlocale.qdoc
+++ b/src/corelib/text/qlocale.qdoc
@@ -174,7 +174,7 @@
\value Chewa Obsolete, please use Nyanja
\value Chickasaw Since Qt 5.14
\value Chiga
- \value Chinese
+ \value Chinese (Mandarin)
\value Church
\value Chuvash
\value ClassicalMandaic Since Qt 5.1
@@ -1202,14 +1202,6 @@
*/
/*!
-\fn QString QLocale::toString(ushort i) const
-
-\overload
-
-\sa toUShort()
-*/
-
-/*!
\fn QString QLocale::toString(int i) const
\overload
diff --git a/src/corelib/text/qlocale_mac.mm b/src/corelib/text/qlocale_mac.mm
index 9719278426..31ede1352b 100644
--- a/src/corelib/text/qlocale_mac.mm
+++ b/src/corelib/text/qlocale_mac.mm
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -67,16 +67,16 @@ static QByteArray envVarLocale()
return lang;
}
-static QByteArray getMacLocaleName()
+static QString getMacLocaleName()
{
- QByteArray result = envVarLocale();
+ QString result = QString::fromLocal8Bit(envVarLocale());
QString lang, script, cntry;
if (result.isEmpty()
- || (result != "C" && !qt_splitLocaleName(QString::fromLocal8Bit(result), lang, script, cntry))) {
+ || (result != QLatin1String("C") && !qt_splitLocaleName(result, lang, script, cntry))) {
QCFType<CFLocaleRef> l = CFLocaleCopyCurrent();
CFStringRef locale = CFLocaleGetIdentifier(l);
- result = QString::fromCFString(locale).toUtf8();
+ result = QString::fromCFString(locale);
}
return result;
}
@@ -402,10 +402,10 @@ static QVariant macQuoteString(QSystemLocale::QueryType type, const QStringRef &
QLocale QSystemLocale::fallbackUiLocale() const
{
- return QLocale(QString::fromUtf8(getMacLocaleName().constData()));
+ return QLocale(getMacLocaleName());
}
-QVariant QSystemLocale::query(QueryType type, QVariant in = QVariant()) const
+QVariant QSystemLocale::query(QueryType type, QVariant in) const
{
QMacAutoReleasePool pool;
switch(type) {
diff --git a/src/corelib/text/qlocale_p.h b/src/corelib/text/qlocale_p.h
index edee3a89c7..bb24009523 100644
--- a/src/corelib/text/qlocale_p.h
+++ b/src/corelib/text/qlocale_p.h
@@ -213,7 +213,8 @@ public:
ShowBase = 0x80,
UppercaseBase = 0x100,
ZeroPadExponent = 0x200,
- ForcePoint = 0x400
+ ForcePoint = 0x400,
+ IndianNumberGrouping= 0x800
};
enum NumberMode { IntegerMode, DoubleStandardMode, DoubleScientificMode };
diff --git a/src/corelib/text/qlocale_tools.cpp b/src/corelib/text/qlocale_tools.cpp
index c246028b4d..0da769d694 100644
--- a/src/corelib/text/qlocale_tools.cpp
+++ b/src/corelib/text/qlocale_tools.cpp
@@ -322,7 +322,7 @@ double qt_asciiToDouble(const char *num, int numLen, bool &ok, int &processed,
conv_flags = double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES
| double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES;
}
- double_conversion::StringToDoubleConverter conv(conv_flags, 0.0, qt_qnan(), 0, 0);
+ double_conversion::StringToDoubleConverter conv(conv_flags, 0.0, qt_qnan(), nullptr, nullptr);
d = conv.StringToDouble(num, numLen, &processed);
if (!qIsFinite(d)) {
diff --git a/src/corelib/text/qlocale_unix.cpp b/src/corelib/text/qlocale_unix.cpp
index ff4274d932..5e1e47eae7 100644
--- a/src/corelib/text/qlocale_unix.cpp
+++ b/src/corelib/text/qlocale_unix.cpp
@@ -283,9 +283,9 @@ QVariant QSystemLocale::query(QueryType type, QVariant in) const
return d->uiLanguages.isEmpty() ? QVariant() : QVariant(d->uiLanguages);
}
case StringToStandardQuotation:
- return lc_messages.quoteString(in.value<QStringRef>());
+ return lc_messages.quoteString(qvariant_cast<QStringRef>(in));
case StringToAlternateQuotation:
- return lc_messages.quoteString(in.value<QStringRef>(), QLocale::AlternateQuotation);
+ return lc_messages.quoteString(qvariant_cast<QStringRef>(in), QLocale::AlternateQuotation);
case ListToSeparatedString:
return lc_messages.createSeparatedList(in.toStringList());
case LocaleChanged:
diff --git a/src/corelib/text/qlocale_win.cpp b/src/corelib/text/qlocale_win.cpp
index 79ea67f966..4b4152c519 100644
--- a/src/corelib/text/qlocale_win.cpp
+++ b/src/corelib/text/qlocale_win.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
@@ -106,11 +106,11 @@ struct QSystemLocalePrivate
{
QSystemLocalePrivate();
- QChar zeroDigit();
- QChar decimalPoint();
- QChar groupSeparator();
- QChar negativeSign();
- QChar positiveSign();
+ QString zeroDigit();
+ QString decimalPoint();
+ QString groupSeparator();
+ QString negativeSign();
+ QString positiveSign();
QVariant dateFormat(QLocale::FormatType);
QVariant timeFormat(QLocale::FormatType);
QVariant dateTimeFormat(QLocale::FormatType);
@@ -147,12 +147,11 @@ private:
WCHAR lcName[LOCALE_NAME_MAX_LENGTH];
#endif
SubstitutionType substitutionType;
- QChar zero;
+ QString zero; // cached value for zeroDigit()
int getLocaleInfo(LCTYPE type, LPWSTR data, int size);
QString getLocaleInfo(LCTYPE type, int maxlen = 0);
int getLocaleInfo_int(LCTYPE type, int maxlen = 0);
- QChar getLocaleInfo_qchar(LCTYPE type);
int getCurrencyFormat(DWORD flags, LPCWSTR value, const CURRENCYFMTW *format, LPWSTR data, int size);
int getDateFormat(DWORD flags, const SYSTEMTIME * date, LPCWSTR format, LPWSTR data, int size);
@@ -236,12 +235,6 @@ int QSystemLocalePrivate::getLocaleInfo_int(LCTYPE type, int maxlen)
return ok ? v : 0;
}
-QChar QSystemLocalePrivate::getLocaleInfo_qchar(LCTYPE type)
-{
- QString str = getLocaleInfo(type);
- return str.isEmpty() ? QChar() : str.at(0);
-}
-
QSystemLocalePrivate::SubstitutionType QSystemLocalePrivate::substitution()
{
if (substitutionType == SUnknown) {
@@ -257,13 +250,12 @@ QSystemLocalePrivate::SubstitutionType QSystemLocalePrivate::substitution()
else if (buf[0] == '2')
substitutionType = QSystemLocalePrivate::SAlways;
else {
- wchar_t digits[11];
+ wchar_t digits[11]; // See zeroDigit() for why 11.
if (!getLocaleInfo(LOCALE_SNATIVEDIGITS, digits, 11)) {
substitutionType = QSystemLocalePrivate::SNever;
return substitutionType;
}
- const wchar_t zero = digits[0];
- if (buf[0] == zero + 2)
+ if (buf[0] == digits[0] + 2)
substitutionType = QSystemLocalePrivate::SAlways;
else
substitutionType = QSystemLocalePrivate::SNever;
@@ -274,40 +266,75 @@ QSystemLocalePrivate::SubstitutionType QSystemLocalePrivate::substitution()
QString &QSystemLocalePrivate::substituteDigits(QString &string)
{
- ushort zero = zeroDigit().unicode();
- ushort *qch = reinterpret_cast<ushort *>(string.data());
- for (ushort *end = qch + string.size(); qch != end; ++qch) {
- if (*qch >= '0' && *qch <= '9')
- *qch = zero + (*qch - '0');
+ zeroDigit(); // Ensure zero is set.
+ switch (zero.size()) {
+ case 1: {
+ const ushort offset = zero.at(0).unicode() - '0';
+ if (!offset) // Nothing to do
+ break;
+ Q_ASSERT(offset > 9);
+ ushort *const qch = reinterpret_cast<ushort *>(string.data());
+ for (int i = 0, stop = string.size(); i < stop; ++i) {
+ ushort &ch = qch[i];
+ if (ch >= '0' && ch <= '9')
+ ch += offset;
+ }
+ break;
+ }
+ case 2: {
+ // Surrogate pair (high, low):
+ uint digit = QChar::surrogateToUcs4(zero.at(0), zero.at(1));
+ for (int i = 0; i < 10; i++) {
+ const QChar s[2] = { QChar::highSurrogate(digit + i), QChar::lowSurrogate(digit + i) };
+ string.replace(QString(QLatin1Char('0' + i)), QString(s, 2));
+ }
+ break;
+ }
+ default:
+ Q_ASSERT(!"Expected zero digit to be a single UCS2 code-point or a surrogate pair");
+ case 0: // Apparently this locale info was not available.
+ break;
}
return string;
}
-QChar QSystemLocalePrivate::zeroDigit()
+QString QSystemLocalePrivate::zeroDigit()
{
- if (zero.isNull())
- zero = getLocaleInfo_qchar(LOCALE_SNATIVEDIGITS);
+ if (zero.isEmpty()) {
+ /* Ten digits plus a terminator.
+
+ https://docs.microsoft.com/en-us/windows/win32/intl/locale-snative-constants
+ "Native equivalents of ASCII 0 through 9. The maximum number of
+ characters allowed for this string is eleven, including a terminating
+ null character."
+ */
+ wchar_t digits[11];
+ if (getLocaleInfo(LOCALE_SNATIVEDIGITS, digits, 11)) {
+ // assert all(digits[i] == i + digits[0] for i in range(1, 10)), assumed above
+ zero = QString::fromWCharArray(digits, 1);
+ }
+ }
return zero;
}
-QChar QSystemLocalePrivate::decimalPoint()
+QString QSystemLocalePrivate::decimalPoint()
{
- return getLocaleInfo_qchar(LOCALE_SDECIMAL);
+ return getLocaleInfo(LOCALE_SDECIMAL);
}
-QChar QSystemLocalePrivate::groupSeparator()
+QString QSystemLocalePrivate::groupSeparator()
{
- return getLocaleInfo_qchar(LOCALE_STHOUSAND);
+ return getLocaleInfo(LOCALE_STHOUSAND);
}
-QChar QSystemLocalePrivate::negativeSign()
+QString QSystemLocalePrivate::negativeSign()
{
- return getLocaleInfo_qchar(LOCALE_SNEGATIVESIGN);
+ return getLocaleInfo(LOCALE_SNEGATIVESIGN);
}
-QChar QSystemLocalePrivate::positiveSign()
+QString QSystemLocalePrivate::positiveSign()
{
- return getLocaleInfo_qchar(LOCALE_SPOSITIVESIGN);
+ return getLocaleInfo(LOCALE_SPOSITIVESIGN);
}
QVariant QSystemLocalePrivate::dateFormat(QLocale::FormatType type)
@@ -677,7 +704,7 @@ void QSystemLocalePrivate::update()
GetUserDefaultLocaleName(lcName, LOCALE_NAME_MAX_LENGTH);
#endif
substitutionType = SUnknown;
- zero = QChar();
+ zero.resize(0);
}
QString QSystemLocalePrivate::winToQtFormat(QStringView sys_fmt)
@@ -749,7 +776,7 @@ QLocale QSystemLocale::fallbackUiLocale() const
return QLocale(QString::fromLatin1(getWinLocaleName()));
}
-QVariant QSystemLocale::query(QueryType type, QVariant in = QVariant()) const
+QVariant QSystemLocale::query(QueryType type, QVariant in) const
{
QSystemLocalePrivate *d = systemLocalePrivate();
switch(type) {
diff --git a/src/corelib/text/qregexp.cpp b/src/corelib/text/qregexp.cpp
index ac4d9bbc36..9301a7e573 100644
--- a/src/corelib/text/qregexp.cpp
+++ b/src/corelib/text/qregexp.cpp
@@ -2528,7 +2528,7 @@ void QRegExpEngine::Box::cat(const Box &b)
eng->addCatTransitions(rs, b.ls);
addAnchorsToEngine(b);
if (minl == 0) {
- lanchors.unite(b.lanchors);
+ lanchors.insert(b.lanchors);
if (skipanchors != 0) {
for (int i = 0; i < b.ls.size(); i++) {
int a = eng->anchorConcatenation(lanchors.value(b.ls.at(i), 0), skipanchors);
@@ -2538,7 +2538,7 @@ void QRegExpEngine::Box::cat(const Box &b)
mergeInto(&ls, b.ls);
}
if (b.minl == 0) {
- ranchors.unite(b.ranchors);
+ ranchors.insert(b.ranchors);
if (b.skipanchors != 0) {
for (int i = 0; i < rs.size(); i++) {
int a = eng->anchorConcatenation(ranchors.value(rs.at(i), 0), b.skipanchors);
@@ -2596,9 +2596,9 @@ void QRegExpEngine::Box::cat(const Box &b)
void QRegExpEngine::Box::orx(const Box &b)
{
mergeInto(&ls, b.ls);
- lanchors.unite(b.lanchors);
+ lanchors.insert(b.lanchors);
mergeInto(&rs, b.rs);
- ranchors.unite(b.ranchors);
+ ranchors.insert(b.ranchors);
if (b.minl == 0) {
if (minl == 0)
diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp
index 8d2187eb28..d74b759aa9 100644
--- a/src/corelib/text/qregularexpression.cpp
+++ b/src/corelib/text/qregularexpression.cpp
@@ -1,7 +1,7 @@
/****************************************************************************
**
-** Copyright (C) 2016 Giuseppe D'Angelo <dangelog@gmail.com>.
-** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
+** Copyright (C) 2020 Giuseppe D'Angelo <dangelog@gmail.com>.
+** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
@@ -443,6 +443,38 @@ QT_BEGIN_NAMESPACE
Other differences are outlined below.
+ \section2 Different pattern syntax
+
+ Porting a regular expression from QRegExp to QRegularExpression may require
+ changes to the pattern itself.
+
+ In certain scenarios, QRegExp was too lenient and accepted patterns that
+ are simply invalid when using QRegularExpression. These are somehow easy
+ to detect, because the QRegularExpression objects built with these patterns
+ are not valid (cf. isValid()).
+
+ In other cases, a pattern ported from QRegExp to QRegularExpression may
+ silently change semantics. Therefore, it is necessary to review the
+ patterns used. The most notable cases of silent incompatibility are:
+
+ \list
+
+ \li Curly braces are needed in order to use a hexadecimal escape like
+ \c{\xHHHH} with more than 2 digits. A pattern like \c{\x2022} neeeds to
+ be ported to \c{\x{2022}}, or it will match a space (\c{0x20}) followed
+ by the string \c{"22"}. In general, it is highly recommended to always use
+ curly braces with the \c{\x} escape, no matter the amount of digits
+ specified.
+
+ \li A 0-to-n quantification like \c{{,n}} needs to be ported to \c{{0,n}} to
+ preserve semantics. Otherwise, a pattern such as \c{\d{,3}} would
+ actually match a digit followed by the exact string \c{"{,3}"}.
+
+ \li QRegExp by default does Unicode-aware matching, while
+ QRegularExpression requires a separate option; see below for more details.
+
+ \endlist
+
\section2 Porting from QRegExp::exactMatch()
QRegExp::exactMatch() in Qt 4 served two purposes: it exactly matched
@@ -831,7 +863,7 @@ struct QRegularExpressionPrivate : QSharedData
QRegularExpression::MatchType matchType,
QRegularExpression::MatchOptions matchOptions,
CheckSubjectStringOption checkSubjectStringOption = CheckSubjectString,
- const QRegularExpressionMatchPrivate *previous = 0) const;
+ const QRegularExpressionMatchPrivate *previous = nullptr) const;
int captureIndexForName(QStringView name) const;
@@ -912,7 +944,7 @@ QRegularExpression::QRegularExpression(QRegularExpressionPrivate &dd)
*/
QRegularExpressionPrivate::QRegularExpressionPrivate()
: QSharedData(),
- patternOptions(0),
+ patternOptions(),
pattern(),
mutex(),
compiledPattern(nullptr),
@@ -990,7 +1022,7 @@ void QRegularExpressionPrivate::compilePattern()
options,
&errorCode,
&patternErrorOffset,
- NULL);
+ nullptr);
if (!compiledPattern) {
errorOffset = static_cast<int>(patternErrorOffset);
@@ -1049,7 +1081,7 @@ public:
{
// The default JIT stack size in PCRE is 32K,
// we allocate from 32K up to 512K.
- stack = pcre2_jit_stack_create_16(32 * 1024, 512 * 1024, NULL);
+ stack = pcre2_jit_stack_create_16(32 * 1024, 512 * 1024, nullptr);
}
/*!
\internal
@@ -1073,7 +1105,7 @@ static pcre2_jit_stack_16 *qtPcreCallback(void *)
if (jitStacks()->hasLocalData())
return jitStacks()->localData()->stack;
- return 0;
+ return nullptr;
}
/*!
@@ -1240,9 +1272,9 @@ QRegularExpressionMatchPrivate *QRegularExpressionPrivate::doMatch(const QString
previousMatchWasEmpty = true;
}
- pcre2_match_context_16 *matchContext = pcre2_match_context_create_16(NULL);
- pcre2_jit_stack_assign_16(matchContext, &qtPcreCallback, NULL);
- pcre2_match_data_16 *matchData = pcre2_match_data_create_from_pattern_16(compiledPattern, NULL);
+ pcre2_match_context_16 *matchContext = pcre2_match_context_create_16(nullptr);
+ pcre2_jit_stack_assign_16(matchContext, &qtPcreCallback, nullptr);
+ pcre2_match_data_16 *matchData = pcre2_match_data_create_from_pattern_16(compiledPattern, nullptr);
const unsigned short * const subjectUtf16 = subject.utf16() + subjectStart;
@@ -1797,7 +1829,19 @@ uint qHash(const QRegularExpression &key, uint seed) noexcept
return seed;
}
+#if QT_STRINGVIEW_LEVEL < 2
+/*!
+ \overload
+*/
+QString QRegularExpression::escape(const QString &str)
+{
+ return escape(QStringView(str));
+}
+#endif // QT_STRINGVIEW_LEVEL < 2
+
/*!
+ \since 5.15
+
Escapes all characters of \a str so that they no longer have any special
meaning when used as a regular expression pattern string, and returns
the escaped string. For instance:
@@ -1815,7 +1859,7 @@ uint qHash(const QRegularExpression &key, uint seed) noexcept
inside \a str is escaped with the sequence \c{"\\0"} (backslash +
\c{'0'}), instead of \c{"\\\0"} (backslash + \c{NUL}).
*/
-QString QRegularExpression::escape(const QString &str)
+QString QRegularExpression::escape(QStringView str)
{
QString result;
const int count = str.size();
@@ -1850,8 +1894,19 @@ QString QRegularExpression::escape(const QString &str)
return result;
}
+#if QT_STRINGVIEW_LEVEL < 2
/*!
\since 5.12
+ \overload
+*/
+QString QRegularExpression::wildcardToRegularExpression(const QString &pattern)
+{
+ return wildcardToRegularExpression(QStringView(pattern));
+}
+#endif // QT_STRINGVIEW_LEVEL < 2
+
+/*!
+ \since 5.15
Returns a regular expression representation of the given glob \a pattern.
The transformation is targeting file path globbing, which means in particular
@@ -1860,6 +1915,10 @@ QString QRegularExpression::escape(const QString &str)
\snippet code/src_corelib_tools_qregularexpression.cpp 31
+ The returned regular expression is already fully anchored. In other
+ words, there is no need of calling anchoredPattern() again on the
+ result.
+
\warning Unlike QRegExp, this implementation follows closely the definition
of wildcard for glob patterns:
\table
@@ -1886,23 +1945,23 @@ QString QRegularExpression::escape(const QString &str)
\note The backslash (\\) character is \e not an escape char in this context.
In order to match one of the special characters, place it in square brackets
- (for example, "[?]").
+ (for example, \c{[?]}).
More information about the implementation can be found in:
\list
\li \l {https://en.wikipedia.org/wiki/Glob_(programming)} {The Wikipedia Glob article}
- \li \c man 7 glob
+ \li \c {man 7 glob}
\endlist
\sa escape()
*/
-QString QRegularExpression::wildcardToRegularExpression(const QString &pattern)
+QString QRegularExpression::wildcardToRegularExpression(QStringView pattern)
{
const int wclen = pattern.length();
QString rx;
rx.reserve(wclen + wclen / 16);
int i = 0;
- const QChar *wc = pattern.unicode();
+ const QChar *wc = pattern.data();
#ifdef Q_OS_WIN
const QLatin1Char nativePathSeparator('\\');
@@ -1974,16 +2033,31 @@ QString QRegularExpression::wildcardToRegularExpression(const QString &pattern)
return anchoredPattern(rx);
}
+#if QT_STRINGVIEW_LEVEL < 2
/*!
\fn QRegularExpression::anchoredPattern(const QString &expression)
\since 5.12
+ \overload
+*/
+#endif // QT_STRINGVIEW_LEVEL < 2
+
+/*!
+ \since 5.15
+
Returns the \a expression wrapped between the \c{\A} and \c{\z} anchors to
be used for exact matching.
\sa {Porting from QRegExp's Exact Matching}
*/
+QString QRegularExpression::anchoredPattern(QStringView expression)
+{
+ return QString()
+ + QLatin1String("\\A(?:")
+ + expression
+ + QLatin1String(")\\z");
+}
/*!
\since 5.1
@@ -2835,7 +2909,7 @@ static const char *pcreCompileErrorCodes[] =
QT_TRANSLATE_NOOP("QRegularExpression", "numbers out of order in {} quantifier"),
QT_TRANSLATE_NOOP("QRegularExpression", "number too big in {} quantifier"),
QT_TRANSLATE_NOOP("QRegularExpression", "missing terminating ] for character class"),
- QT_TRANSLATE_NOOP("QRegularExpression", "invalid escape sequence in character class"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "escape sequence is invalid in character class"),
QT_TRANSLATE_NOOP("QRegularExpression", "range out of order in character class"),
QT_TRANSLATE_NOOP("QRegularExpression", "quantifier does not follow a repeatable item"),
QT_TRANSLATE_NOOP("QRegularExpression", "internal error: unexpected repeat"),
@@ -2852,46 +2926,46 @@ static const char *pcreCompileErrorCodes[] =
QT_TRANSLATE_NOOP("QRegularExpression", "failed to allocate heap memory"),
QT_TRANSLATE_NOOP("QRegularExpression", "unmatched closing parenthesis"),
QT_TRANSLATE_NOOP("QRegularExpression", "internal error: code overflow"),
- QT_TRANSLATE_NOOP("QRegularExpression", "letter or underscore expected after (?< or (?'"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "missing closing parenthesis for condition"),
QT_TRANSLATE_NOOP("QRegularExpression", "lookbehind assertion is not fixed length"),
- QT_TRANSLATE_NOOP("QRegularExpression", "malformed number or name after (?("),
- QT_TRANSLATE_NOOP("QRegularExpression", "conditional group contains more than two branches"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "a relative value of zero is not allowed"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "conditional subpattern contains more than two branches"),
QT_TRANSLATE_NOOP("QRegularExpression", "assertion expected after (?( or (?(?C)"),
- QT_TRANSLATE_NOOP("QRegularExpression", "(?R or (?[+-]digits must be followed by )"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "digit expected after (?+ or (?-"),
QT_TRANSLATE_NOOP("QRegularExpression", "unknown POSIX class name"),
QT_TRANSLATE_NOOP("QRegularExpression", "internal error in pcre2_study(): should not occur"),
QT_TRANSLATE_NOOP("QRegularExpression", "this version of PCRE2 does not have Unicode support"),
QT_TRANSLATE_NOOP("QRegularExpression", "parentheses are too deeply nested (stack check)"),
QT_TRANSLATE_NOOP("QRegularExpression", "character code point value in \\x{} or \\o{} is too large"),
- QT_TRANSLATE_NOOP("QRegularExpression", "invalid condition (?(0)"),
- QT_TRANSLATE_NOOP("QRegularExpression", "\\C is not allowed in a lookbehind assertion"),
- QT_TRANSLATE_NOOP("QRegularExpression", "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "lookbehind is too complicated"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "\\C is not allowed in a lookbehind assertion in UTF-" "16" " mode"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "PCRE2 does not support \\F, \\L, \\l, \\N{name}, \\U, or \\u"),
QT_TRANSLATE_NOOP("QRegularExpression", "number after (?C is greater than 255"),
QT_TRANSLATE_NOOP("QRegularExpression", "closing parenthesis for (?C expected"),
QT_TRANSLATE_NOOP("QRegularExpression", "invalid escape sequence in (*VERB) name"),
QT_TRANSLATE_NOOP("QRegularExpression", "unrecognized character after (?P"),
- QT_TRANSLATE_NOOP("QRegularExpression", "syntax error in subpattern name (missing terminator)"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "syntax error in subpattern name (missing terminator?)"),
QT_TRANSLATE_NOOP("QRegularExpression", "two named subpatterns have the same name (PCRE2_DUPNAMES not set)"),
- QT_TRANSLATE_NOOP("QRegularExpression", "group name must start with a non-digit"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "subpattern name must start with a non-digit"),
QT_TRANSLATE_NOOP("QRegularExpression", "this version of PCRE2 does not have support for \\P, \\p, or \\X"),
QT_TRANSLATE_NOOP("QRegularExpression", "malformed \\P or \\p sequence"),
QT_TRANSLATE_NOOP("QRegularExpression", "unknown property name after \\P or \\p"),
- QT_TRANSLATE_NOOP("QRegularExpression", "subpattern name is too long (maximum " "10000" " characters)"),
- QT_TRANSLATE_NOOP("QRegularExpression", "too many named subpatterns (maximum " "256" ")"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "subpattern name is too long (maximum " "32" " code units)"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "too many named subpatterns (maximum " "10000" ")"),
QT_TRANSLATE_NOOP("QRegularExpression", "invalid range in character class"),
QT_TRANSLATE_NOOP("QRegularExpression", "octal value is greater than \\377 in 8-bit non-UTF-8 mode"),
QT_TRANSLATE_NOOP("QRegularExpression", "internal error: overran compiling workspace"),
QT_TRANSLATE_NOOP("QRegularExpression", "internal error: previously-checked referenced subpattern not found"),
- QT_TRANSLATE_NOOP("QRegularExpression", "DEFINE group contains more than one branch"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "DEFINE subpattern contains more than one branch"),
QT_TRANSLATE_NOOP("QRegularExpression", "missing opening brace after \\o"),
QT_TRANSLATE_NOOP("QRegularExpression", "internal error: unknown newline setting"),
QT_TRANSLATE_NOOP("QRegularExpression", "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number"),
- QT_TRANSLATE_NOOP("QRegularExpression", "a numbered reference must not be zero"),
- QT_TRANSLATE_NOOP("QRegularExpression", "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "(?R (recursive pattern call) must be followed by a closing parenthesis"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "obsolete error (should not occur)"),
QT_TRANSLATE_NOOP("QRegularExpression", "(*VERB) not recognized or malformed"),
- QT_TRANSLATE_NOOP("QRegularExpression", "number is too big"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "subpattern number is too big"),
QT_TRANSLATE_NOOP("QRegularExpression", "subpattern name expected"),
- QT_TRANSLATE_NOOP("QRegularExpression", "digit expected after (?+"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "internal error: parsed pattern overflow"),
QT_TRANSLATE_NOOP("QRegularExpression", "non-octal character in \\o{} (closing brace missing?)"),
QT_TRANSLATE_NOOP("QRegularExpression", "different names for subpatterns of the same number are not allowed"),
QT_TRANSLATE_NOOP("QRegularExpression", "(*MARK) must have an argument"),
@@ -2899,16 +2973,16 @@ static const char *pcreCompileErrorCodes[] =
QT_TRANSLATE_NOOP("QRegularExpression", "\\c must be followed by a printable ASCII character"),
QT_TRANSLATE_NOOP("QRegularExpression", "\\c must be followed by a letter or one of [\\]^_?"),
QT_TRANSLATE_NOOP("QRegularExpression", "\\k is not followed by a braced, angle-bracketed, or quoted name"),
- QT_TRANSLATE_NOOP("QRegularExpression", "internal error: unknown opcode in find_fixedlength()"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "internal error: unknown meta code in check_lookbehinds()"),
QT_TRANSLATE_NOOP("QRegularExpression", "\\N is not supported in a class"),
- QT_TRANSLATE_NOOP("QRegularExpression", "SPARE ERROR"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "callout string is too long"),
QT_TRANSLATE_NOOP("QRegularExpression", "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)"),
QT_TRANSLATE_NOOP("QRegularExpression", "using UTF is disabled by the application"),
QT_TRANSLATE_NOOP("QRegularExpression", "using UCP is disabled by the application"),
QT_TRANSLATE_NOOP("QRegularExpression", "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)"),
QT_TRANSLATE_NOOP("QRegularExpression", "character code point value in \\u.... sequence is too large"),
- QT_TRANSLATE_NOOP("QRegularExpression", "digits missing in \\x{} or \\o{}"),
- QT_TRANSLATE_NOOP("QRegularExpression", "syntax error in (?(VERSION condition"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "digits missing in \\x{} or \\o{} or \\N{U+}"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "syntax error or number too big in (?(VERSION condition"),
QT_TRANSLATE_NOOP("QRegularExpression", "internal error: unknown opcode in auto_possessify()"),
QT_TRANSLATE_NOOP("QRegularExpression", "missing terminating delimiter for callout with string argument"),
QT_TRANSLATE_NOOP("QRegularExpression", "unrecognized string delimiter follows (?C"),
@@ -2918,6 +2992,16 @@ static const char *pcreCompileErrorCodes[] =
QT_TRANSLATE_NOOP("QRegularExpression", "regular expression is too complicated"),
QT_TRANSLATE_NOOP("QRegularExpression", "lookbehind assertion is too long"),
QT_TRANSLATE_NOOP("QRegularExpression", "pattern string is longer than the limit set by the application"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "internal error: unknown code in parsed pattern"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "internal error: bad code value in parsed_skip()"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES is not allowed in UTF-16 mode"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "invalid option bits with PCRE2_LITERAL"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "\\N{U+dddd} is supported only in Unicode (UTF) mode"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "invalid hyphen in option setting"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "(*alpha_assertion) not recognized"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "script runs require Unicode support, which this version of PCRE2 does not have"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "too many capturing groups (maximum 65535)"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "atomic assertion expected after (?( or (?(?C)"),
QT_TRANSLATE_NOOP("QRegularExpression", "no error"),
QT_TRANSLATE_NOOP("QRegularExpression", "no match"),
QT_TRANSLATE_NOOP("QRegularExpression", "partial match"),
@@ -2955,7 +3039,7 @@ static const char *pcreCompileErrorCodes[] =
QT_TRANSLATE_NOOP("QRegularExpression", "bad option value"),
QT_TRANSLATE_NOOP("QRegularExpression", "invalid replacement string"),
QT_TRANSLATE_NOOP("QRegularExpression", "bad offset into UTF string"),
- QT_TRANSLATE_NOOP("QRegularExpression", "callout error code"), /* Never returned by PCRE2 itself */
+ QT_TRANSLATE_NOOP("QRegularExpression", "callout error code"),
QT_TRANSLATE_NOOP("QRegularExpression", "invalid data in workspace for DFA restart"),
QT_TRANSLATE_NOOP("QRegularExpression", "too much recursion for DFA matching"),
QT_TRANSLATE_NOOP("QRegularExpression", "backreference condition or recursion test is not supported for DFA matching"),
@@ -2971,15 +3055,20 @@ static const char *pcreCompileErrorCodes[] =
QT_TRANSLATE_NOOP("QRegularExpression", "non-unique substring name"),
QT_TRANSLATE_NOOP("QRegularExpression", "NULL argument passed"),
QT_TRANSLATE_NOOP("QRegularExpression", "nested recursion at the same subject position"),
- QT_TRANSLATE_NOOP("QRegularExpression", "recursion limit exceeded"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "matching depth limit exceeded"),
QT_TRANSLATE_NOOP("QRegularExpression", "requested value is not available"),
QT_TRANSLATE_NOOP("QRegularExpression", "requested value is not set"),
QT_TRANSLATE_NOOP("QRegularExpression", "offset limit set without PCRE2_USE_OFFSET_LIMIT"),
QT_TRANSLATE_NOOP("QRegularExpression", "bad escape sequence in replacement string"),
QT_TRANSLATE_NOOP("QRegularExpression", "expected closing curly bracket in replacement string"),
QT_TRANSLATE_NOOP("QRegularExpression", "bad substitution in replacement string"),
- QT_TRANSLATE_NOOP("QRegularExpression", "match with end before start is not supported"),
- QT_TRANSLATE_NOOP("QRegularExpression", "too many replacements (more than INT_MAX)")
+ QT_TRANSLATE_NOOP("QRegularExpression", "match with end before start or start moved backwards is not supported"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "too many replacements (more than INT_MAX)"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "bad serialized data"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "heap limit exceeded"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "invalid syntax"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "internal error - duplicate substitution match"),
+ QT_TRANSLATE_NOOP("QRegularExpression", "PCRE2_MATCH_INVALID_UTF is not supported for DFA matching")
};
#endif // #if 0
diff --git a/src/corelib/text/qregularexpression.h b/src/corelib/text/qregularexpression.h
index f799a38ae4..4fa258b080 100644
--- a/src/corelib/text/qregularexpression.h
+++ b/src/corelib/text/qregularexpression.h
@@ -1,7 +1,7 @@
/****************************************************************************
**
-** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>.
-** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
+** Copyright (C) 2020 Giuseppe D'Angelo <dangelog@gmail.com>.
+** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -42,9 +42,8 @@
#define QREGULAREXPRESSION_H
#include <QtCore/qglobal.h>
-
#include <QtCore/qstring.h>
-#include <QtCore/qstringlist.h>
+#include <QtCore/qstringview.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qvariant.h>
@@ -52,7 +51,8 @@ QT_REQUIRE_CONFIG(regularexpression);
QT_BEGIN_NAMESPACE
-class QStringView;
+class QStringList;
+class QLatin1String;
class QRegularExpressionMatch;
class QRegularExpressionMatchIterator;
@@ -137,14 +137,18 @@ public:
void optimize() const;
+#if QT_STRINGVIEW_LEVEL < 2
static QString escape(const QString &str);
static QString wildcardToRegularExpression(const QString &str);
static inline QString anchoredPattern(const QString &expression)
{
- return QLatin1String("\\A(?:")
- + expression
- + QLatin1String(")\\z");
+ return anchoredPattern(QStringView(expression));
}
+#endif
+
+ static QString escape(QStringView str);
+ static QString wildcardToRegularExpression(QStringView str);
+ static QString anchoredPattern(QStringView expression);
bool operator==(const QRegularExpression &re) const;
inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); }
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index dcdc87181e..877f5df462 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -100,7 +100,7 @@
#define ULLONG_MAX quint64_C(18446744073709551615)
#endif
-#define IS_RAW_DATA(d) ((d)->offset != sizeof(QStringData))
+#define IS_RAW_DATA(d) ((d.d)->flags & QArrayData::RawDataType)
QT_BEGIN_NAMESPACE
@@ -146,6 +146,9 @@ qsizetype qFindStringBoyerMoore(QStringView haystack, qsizetype from, QStringVie
static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept;
template <typename Haystack>
static inline qsizetype qLastIndexOf(Haystack haystack, QChar needle, qsizetype from, Qt::CaseSensitivity cs) noexcept;
+template <>
+inline qsizetype qLastIndexOf(QString haystack, QChar needle,
+ qsizetype from, Qt::CaseSensitivity cs) noexcept = delete; // unwanted, would detach
static inline qsizetype qt_string_count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs);
static inline qsizetype qt_string_count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs);
@@ -160,7 +163,7 @@ qsizetype QtPrivate::qustrlen(const ushort *str) noexcept
{
qsizetype result = 0;
-#ifdef __SSE2__
+#if defined(__SSE2__) && !(defined(__SANITIZE_ADDRESS__) || QT_HAS_FEATURE(address_sanitizer))
// find the 16-byte alignment immediately prior or equal to str
quintptr misalignment = quintptr(str) & 0xf;
Q_ASSERT((misalignment & 1) == 0);
@@ -588,6 +591,20 @@ bool QtPrivate::isLatin1(QStringView s) noexcept
return true;
}
+bool QtPrivate::isValidUtf16(QStringView s) noexcept
+{
+ Q_CONSTEXPR uint InvalidCodePoint = UINT_MAX;
+
+ QStringIterator i(s);
+ while (i.hasNext()) {
+ uint c = i.next(InvalidCodePoint);
+ if (c == InvalidCodePoint)
+ return false;
+ }
+
+ return true;
+}
+
// conversion between Latin 1 and UTF-16
void qt_from_latin1(ushort *dst, const char *str, size_t size) noexcept
{
@@ -912,7 +929,7 @@ static int ucstrncmp(const QChar *a, const QChar *b, size_t l)
};
// we're going to read a[0..15] and b[0..15] (32 bytes)
- for ( ; a + offset + 16 <= end; offset += 16) {
+ for ( ; end - a >= offset + 16; offset += 16) {
#ifdef __AVX2__
__m256i a_data = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(a + offset));
__m256i b_data = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(b + offset));
@@ -936,7 +953,7 @@ static int ucstrncmp(const QChar *a, const QChar *b, size_t l)
}
// we're going to read a[0..7] and b[0..7] (16 bytes)
- if (a + offset + 8 <= end) {
+ if (end - a >= offset + 8) {
__m128i a_data = _mm_loadu_si128(reinterpret_cast<const __m128i *>(a + offset));
__m128i b_data = _mm_loadu_si128(reinterpret_cast<const __m128i *>(b + offset));
if (isDifferent(a_data, b_data))
@@ -946,7 +963,7 @@ static int ucstrncmp(const QChar *a, const QChar *b, size_t l)
}
// we're going to read a[0..3] and b[0..3] (8 bytes)
- if (a + offset + 4 <= end) {
+ if (end - a >= offset + 4) {
__m128i a_data = _mm_loadl_epi64(reinterpret_cast<const __m128i *>(a + offset));
__m128i b_data = _mm_loadl_epi64(reinterpret_cast<const __m128i *>(b + offset));
if (isDifferent(a_data, b_data))
@@ -967,7 +984,7 @@ static int ucstrncmp(const QChar *a, const QChar *b, size_t l)
if (l >= 8) {
const QChar *end = a + l;
const uint16x8_t mask = { 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7 };
- while (a + 7 < end) {
+ while (end - a > 7) {
uint16x8_t da = vld1q_u16(reinterpret_cast<const uint16_t *>(a));
uint16x8_t db = vld1q_u16(reinterpret_cast<const uint16_t *>(b));
@@ -1360,28 +1377,6 @@ const QString::Null QString::null = { };
*/
/*!
- \class QCharRef
- \inmodule QtCore
- \reentrant
- \brief The QCharRef class is a helper class for QString.
-
- \internal
-
- \ingroup string-processing
-
- When you get an object of type QCharRef, if you can assign to it,
- the assignment will apply to the character in the string from
- which you got the reference. That is its whole purpose in life.
- The QCharRef becomes invalid once modifications are made to the
- string: if you want to keep the character, copy it into a QChar.
-
- Most of the QChar member functions also exist in QCharRef.
- However, they are not explicitly documented here.
-
- \sa QString::operator[](), QString::at(), QChar
-*/
-
-/*!
\class QString
\inmodule QtCore
\reentrant
@@ -2100,8 +2095,8 @@ int QString::toUcs4_helper(const ushort *uc, int length, uint *out)
*/
QString::QString(const QChar *unicode, int size)
{
- if (!unicode) {
- d = Data::sharedNull();
+ if (!unicode) {
+ d.clear();
} else {
if (size < 0) {
size = 0;
@@ -2109,13 +2104,11 @@ QString::QString(const QChar *unicode, int size)
++size;
}
if (!size) {
- d = Data::allocate(0);
+ d = DataPointer(Data::allocate(0), 0);
} else {
- d = Data::allocate(size + 1);
- Q_CHECK_PTR(d);
- d->size = size;
- memcpy(d->data(), unicode, size * sizeof(QChar));
- d->data()[size] = '\0';
+ d = DataPointer(Data::allocate(size + 1), size);
+ memcpy(d.data(), unicode, size * sizeof(QChar));
+ d.data()[size] = '\0';
}
}
}
@@ -2128,15 +2121,13 @@ QString::QString(const QChar *unicode, int size)
*/
QString::QString(int size, QChar ch)
{
- if (size <= 0) {
- d = Data::allocate(0);
+ if (size <= 0) {
+ d = DataPointer(Data::allocate(0), 0);
} else {
- d = Data::allocate(size + 1);
- Q_CHECK_PTR(d);
- d->size = size;
- d->data()[size] = '\0';
- ushort *i = d->data() + size;
- ushort *b = d->data();
+ d = DataPointer(Data::allocate(size + 1), size);
+ d.data()[size] = '\0';
+ ushort *i = d.data() + size;
+ ushort *b = d.data();
const ushort value = ch.unicode();
while (i != b)
*--i = value;
@@ -2151,10 +2142,8 @@ QString::QString(int size, QChar ch)
*/
QString::QString(int size, Qt::Initialization)
{
- d = Data::allocate(size + 1);
- Q_CHECK_PTR(d);
- d->size = size;
- d->data()[size] = '\0';
+ d = DataPointer(Data::allocate(size + 1), size);
+ d.data()[size] = '\0';
}
/*! \fn QString::QString(QLatin1String str)
@@ -2169,11 +2158,9 @@ QString::QString(int size, Qt::Initialization)
*/
QString::QString(QChar ch)
{
- d = Data::allocate(2);
- Q_CHECK_PTR(d);
- d->size = 1;
- d->data()[0] = ch.unicode();
- d->data()[1] = '\0';
+ d = DataPointer(Data::allocate(2), 1);
+ d.data()[0] = ch.unicode();
+ d.data()[1] = '\0';
}
/*! \fn QString::QString(const QByteArray &ba)
@@ -2195,7 +2182,7 @@ QString::QString(QChar ch)
\internal
*/
-/*! \fn QString::QString(QStringDataPtr)
+/*! \fn QString::QString(QStringPrivate)
\internal
*/
@@ -2265,16 +2252,16 @@ void QString::resize(int size)
if (size < 0)
size = 0;
- if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) {
- d->size = size;
+ if (!d->isShared() && !d->isMutable() && size < int(d.size)) {
+ d.size = size;
return;
}
- if (d->ref.isShared() || uint(size) + 1u > d->alloc)
+ if (d->needsDetach() || size > capacity())
reallocData(uint(size) + 1u, true);
- if (d->alloc) {
- d->size = size;
- d->data()[size] = '\0';
+ d.size = size;
+ if (d->isMutable()) {
+ d.data()[size] = '\0';
}
}
@@ -2294,7 +2281,7 @@ void QString::resize(int size, QChar fillChar)
resize(size);
const int difference = length() - oldSize;
if (difference > 0)
- std::fill_n(d->begin() + oldSize, difference, fillChar.unicode());
+ std::fill_n(d.data() + oldSize, difference, fillChar.unicode());
}
/*! \fn int QString::capacity() const
@@ -2351,28 +2338,22 @@ void QString::reallocData(uint alloc, bool grow)
{
auto allocOptions = d->detachFlags();
if (grow)
- allocOptions |= QArrayData::Grow;
-
- if (d->ref.isShared() || IS_RAW_DATA(d)) {
- Data *x = Data::allocate(alloc, allocOptions);
- Q_CHECK_PTR(x);
- x->size = qMin(int(alloc) - 1, d->size);
- ::memcpy(x->data(), d->data(), x->size * sizeof(QChar));
- x->data()[x->size] = 0;
- if (!d->ref.deref())
- Data::deallocate(d);
- d = x;
+ allocOptions |= QArrayData::GrowsForward;
+
+ if (d->needsDetach()) {
+ DataPointer dd(Data::allocate(alloc, allocOptions), qMin(int(alloc) - 1, d.size));
+ ::memcpy(dd.data(), d.data(), dd.size * sizeof(QChar));
+ dd.data()[dd.size] = 0;
+ d = dd;
} else {
- Data *p = Data::reallocateUnaligned(d, alloc, allocOptions);
- Q_CHECK_PTR(p);
- d = p;
+ d.reallocate(alloc, allocOptions);
}
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void QString::expand(int i)
{
- resize(qMax(i + 1, d->size), QLatin1Char(' '));
+ resize(qMax(i + 1, size()), QLatin1Char(' '));
}
#endif
@@ -2391,9 +2372,6 @@ void QString::expand(int i)
QString &QString::operator=(const QString &other) noexcept
{
- other.d->ref.ref();
- if (!d->ref.deref())
- Data::deallocate(d);
d = other.d;
return *this;
}
@@ -2415,9 +2393,9 @@ QString &QString::operator=(const QString &other) noexcept
QString &QString::operator=(QLatin1String other)
{
if (isDetached() && other.size() <= capacity()) { // assumes d->alloc == 0 -> !isDetached() (sharedNull)
- d->size = other.size();
- d->data()[other.size()] = 0;
- qt_from_latin1(d->data(), other.latin1(), other.size());
+ d.size = other.size();
+ d.data()[other.size()] = 0;
+ qt_from_latin1(d.data(), other.latin1(), other.size());
} else {
*this = fromLatin1(other.latin1(), other.size());
}
@@ -2480,10 +2458,9 @@ QString &QString::operator=(QChar ch)
{
if (isDetached() && capacity() >= 1) { // assumes d->alloc == 0 -> !isDetached() (sharedNull)
// re-use existing capacity:
- ushort *dat = d->data();
- dat[0] = ch.unicode();
- dat[1] = 0;
- d->size = 1;
+ d.data()[0] = ch.unicode();
+ d.data()[1] = 0;
+ d.size = 1;
} else {
operator=(QString(ch));
}
@@ -2569,13 +2546,13 @@ QString &QString::insert(int i, QLatin1String str)
return *this;
int len = str.size();
- if (Q_UNLIKELY(i > d->size))
+ if (Q_UNLIKELY(i > size()))
resize(i + len, QLatin1Char(' '));
else
- resize(d->size + len);
+ resize(size() + len);
- ::memmove(d->data() + i + len, d->data() + i, (d->size - i - len) * sizeof(QChar));
- qt_from_latin1(d->data() + i, s, uint(len));
+ ::memmove(d.data() + i + len, d.data() + i, (d.size - i - len) * sizeof(QChar));
+ qt_from_latin1(d.data() + i, s, uint(len));
return *this;
}
@@ -2592,7 +2569,7 @@ QString& QString::insert(int i, const QChar *unicode, int size)
return *this;
const ushort *s = (const ushort *)unicode;
- if (s >= d->data() && s < d->data() + d->alloc) {
+ if (s >= d.data() && s < d.data() + d.size) {
// Part of me - take a copy
ushort *tmp = static_cast<ushort *>(::malloc(size * sizeof(QChar)));
Q_CHECK_PTR(tmp);
@@ -2602,13 +2579,13 @@ QString& QString::insert(int i, const QChar *unicode, int size)
return *this;
}
- if (Q_UNLIKELY(i > d->size))
+ if (Q_UNLIKELY(i > int(d.size)))
resize(i + size, QLatin1Char(' '));
else
- resize(d->size + size);
+ resize(d.size + size);
- ::memmove(d->data() + i + size, d->data() + i, (d->size - i - size) * sizeof(QChar));
- memcpy(d->data() + i, s, size * sizeof(QChar));
+ ::memmove(d.data() + i + size, d.data() + i, (d.size - i - size) * sizeof(QChar));
+ memcpy(d.data() + i, s, size * sizeof(QChar));
return *this;
}
@@ -2622,15 +2599,15 @@ QString& QString::insert(int i, const QChar *unicode, int size)
QString& QString::insert(int i, QChar ch)
{
if (i < 0)
- i += d->size;
+ i += d.size;
if (i < 0)
return *this;
- if (Q_UNLIKELY(i > d->size))
+ if (Q_UNLIKELY(i > size()))
resize(i + 1, QLatin1Char(' '));
else
- resize(d->size + 1);
- ::memmove(d->data() + i + 1, d->data() + i, (d->size - i - 1) * sizeof(QChar));
- d->data()[i] = ch.unicode();
+ resize(d.size + 1);
+ ::memmove(d.data() + i + 1, d.data() + i, (d.size - i - 1) * sizeof(QChar));
+ d.data()[i] = ch.unicode();
return *this;
}
@@ -2654,15 +2631,15 @@ QString& QString::insert(int i, QChar ch)
*/
QString &QString::append(const QString &str)
{
- if (str.d != Data::sharedNull()) {
- if (d == Data::sharedNull()) {
+ if (!str.isNull()) {
+ if (isNull()) {
operator=(str);
} else {
- if (d->ref.isShared() || uint(d->size + str.d->size) + 1u > d->alloc)
- reallocData(uint(d->size + str.d->size) + 1u, true);
- memcpy(d->data() + d->size, str.d->data(), str.d->size * sizeof(QChar));
- d->size += str.d->size;
- d->data()[d->size] = '\0';
+ if (d->needsDetach() || size() + str.size() > capacity())
+ reallocData(uint(size() + str.size()) + 1u, true);
+ memcpy(d.data() + d.size, str.d.data(), str.d.size * sizeof(QChar));
+ d.size += str.d.size;
+ d.data()[d.size] = '\0';
}
}
return *this;
@@ -2677,11 +2654,11 @@ QString &QString::append(const QString &str)
QString &QString::append(const QChar *str, int len)
{
if (str && len > 0) {
- if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
- reallocData(uint(d->size + len) + 1u, true);
- memcpy(d->data() + d->size, str, len * sizeof(QChar));
- d->size += len;
- d->data()[d->size] = '\0';
+ if (d->needsDetach() || size() + len > capacity())
+ reallocData(uint(size() + len) + 1u, true);
+ memcpy(d.data() + d.size, str, len * sizeof(QChar));
+ d.size += len;
+ d.data()[d.size] = '\0';
}
return *this;
}
@@ -2696,12 +2673,12 @@ QString &QString::append(QLatin1String str)
const char *s = str.latin1();
if (s) {
int len = str.size();
- if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
- reallocData(uint(d->size + len) + 1u, true);
- ushort *i = d->data() + d->size;
+ if (d->needsDetach() || size() + len > capacity())
+ reallocData(uint(size() + len) + 1u, true);
+ ushort *i = d.data() + d.size;
qt_from_latin1(i, s, uint(len));
i[len] = '\0';
- d->size += len;
+ d.size += len;
}
return *this;
}
@@ -2743,10 +2720,10 @@ QString &QString::append(QLatin1String str)
*/
QString &QString::append(QChar ch)
{
- if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
- reallocData(uint(d->size) + 2u, true);
- d->data()[d->size++] = ch.unicode();
- d->data()[d->size] = '\0';
+ if (d->needsDetach() || size() + 1 > capacity())
+ reallocData(uint(d.size) + 2u, true);
+ d.data()[d.size++] = ch.unicode();
+ d.data()[d.size] = '\0';
return *this;
}
@@ -2839,16 +2816,16 @@ QString &QString::append(QChar ch)
QString &QString::remove(int pos, int len)
{
if (pos < 0) // count from end of string
- pos += d->size;
- if (uint(pos) >= uint(d->size)) {
+ pos += size();
+ if (uint(pos) >= uint(size())) {
// range problems
- } else if (len >= d->size - pos) {
+ } else if (len >= size() - pos) {
resize(pos); // truncate
} else if (len > 0) {
detach();
- memmove(d->data() + pos, d->data() + pos + len,
- (d->size - pos - len + 1) * sizeof(ushort));
- d->size -= len;
+ memmove(d.data() + pos, d.data() + pos + len,
+ (d.size - pos - len + 1) * sizeof(ushort));
+ d.size -= len;
}
return *this;
}
@@ -2993,10 +2970,10 @@ QString &QString::replace(int pos, int len, const QString &after)
*/
QString &QString::replace(int pos, int len, const QChar *unicode, int size)
{
- if (uint(pos) > uint(d->size))
+ if (uint(pos) > uint(this->size()))
return *this;
- if (len > d->size - pos)
- len = d->size - pos;
+ if (len > this->size() - pos)
+ len = this->size() - pos;
uint index = pos;
replace_helper(&index, 1, len, unicode, size);
@@ -3060,10 +3037,10 @@ bool pointsIntoRange(const QChar *ptr, const ushort *base, int len)
*/
void QString::replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen)
{
- // Copy after if it lies inside our own d->data() area (which we could
+ // Copy after if it lies inside our own d.b area (which we could
// possibly invalidate via a realloc or modify by replacement).
QChar *afterBuffer = nullptr;
- if (pointsIntoRange(after, d->data(), d->size)) // Use copy in place of vulnerable original:
+ if (pointsIntoRange(after, d.data(), d.size)) // Use copy in place of vulnerable original:
after = afterBuffer = textCopy(after, alen);
QT_TRY {
@@ -3071,36 +3048,36 @@ void QString::replace_helper(uint *indices, int nIndices, int blen, const QChar
// replace in place
detach();
for (int i = 0; i < nIndices; ++i)
- memcpy(d->data() + indices[i], after, alen * sizeof(QChar));
+ memcpy(d.data() + indices[i], after, alen * sizeof(QChar));
} else if (alen < blen) {
// replace from front
detach();
uint to = indices[0];
if (alen)
- memcpy(d->data()+to, after, alen*sizeof(QChar));
+ memcpy(d.data()+to, after, alen*sizeof(QChar));
to += alen;
uint movestart = indices[0] + blen;
for (int i = 1; i < nIndices; ++i) {
int msize = indices[i] - movestart;
if (msize > 0) {
- memmove(d->data() + to, d->data() + movestart, msize * sizeof(QChar));
+ memmove(d.data() + to, d.data() + movestart, msize * sizeof(QChar));
to += msize;
}
if (alen) {
- memcpy(d->data() + to, after, alen * sizeof(QChar));
+ memcpy(d.data() + to, after, alen * sizeof(QChar));
to += alen;
}
movestart = indices[i] + blen;
}
- int msize = d->size - movestart;
+ int msize = d.size - movestart;
if (msize > 0)
- memmove(d->data() + to, d->data() + movestart, msize * sizeof(QChar));
- resize(d->size - nIndices*(blen-alen));
+ memmove(d.data() + to, d.data() + movestart, msize * sizeof(QChar));
+ resize(d.size - nIndices*(blen-alen));
} else {
// replace from back
int adjust = nIndices*(alen-blen);
- int newLen = d->size + adjust;
- int moveend = d->size;
+ int newLen = d.size + adjust;
+ int moveend = d.size;
resize(newLen);
while (nIndices) {
@@ -3108,9 +3085,9 @@ void QString::replace_helper(uint *indices, int nIndices, int blen, const QChar
int movestart = indices[nIndices] + blen;
int insertstart = indices[nIndices] + nIndices*(alen-blen);
int moveto = insertstart + alen;
- memmove(d->data() + moveto, d->data() + movestart,
+ memmove(d.data() + moveto, d.data() + movestart,
(moveend - movestart)*sizeof(QChar));
- memcpy(d->data() + insertstart, after, alen * sizeof(QChar));
+ memcpy(d.data() + insertstart, after, alen * sizeof(QChar));
moveend = movestart-blen;
}
}
@@ -3136,7 +3113,7 @@ QString &QString::replace(const QChar *before, int blen,
const QChar *after, int alen,
Qt::CaseSensitivity cs)
{
- if (d->size == 0) {
+ if (d.size == 0) {
if (blen)
return *this;
} else {
@@ -3171,10 +3148,10 @@ QString &QString::replace(const QChar *before, int blen,
We're about to change data, that before and after might point
into, and we'll need that data for our next batch of indices.
*/
- if (!afterBuffer && pointsIntoRange(after, d->data(), d->size))
+ if (!afterBuffer && pointsIntoRange(after, d.data(), d.size))
after = afterBuffer = textCopy(after, alen);
- if (!beforeBuffer && pointsIntoRange(before, d->data(), d->size)) {
+ if (!beforeBuffer && pointsIntoRange(before, d.data(), d.size)) {
beforeBuffer = textCopy(before, blen);
matcher = QStringMatcher(beforeBuffer, blen, cs);
}
@@ -3203,13 +3180,13 @@ QString &QString::replace(const QChar *before, int blen,
*/
QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs)
{
- if (after.d->size == 0)
+ if (after.size() == 0)
return remove(ch, cs);
- if (after.d->size == 1)
+ if (after.size() == 1)
return replace(ch, after.front(), cs);
- if (d->size == 0)
+ if (size() == 0)
return *this;
ushort cc = (cs == Qt::CaseSensitive ? ch.unicode() : ch.toCaseFolded().unicode());
@@ -3219,14 +3196,14 @@ QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs
uint indices[1024];
uint pos = 0;
if (cs == Qt::CaseSensitive) {
- while (pos < 1024 && index < d->size) {
- if (d->data()[index] == cc)
+ while (pos < 1024 && index < size()) {
+ if (d.data()[index] == cc)
indices[pos++] = index;
index++;
}
} else {
- while (pos < 1024 && index < d->size) {
- if (QChar::toCaseFolded(d->data()[index]) == cc)
+ while (pos < 1024 && index < size()) {
+ if (QChar::toCaseFolded(d.data()[index]) == cc)
indices[pos++] = index;
index++;
}
@@ -3234,12 +3211,12 @@ QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs
if (!pos) // Nothing to replace
break;
- replace_helper(indices, pos, 1, after.constData(), after.d->size);
+ replace_helper(indices, pos, 1, after.constData(), after.size());
if (Q_LIKELY(index == -1)) // Nothing left to replace
break;
// The call to replace_helper just moved what index points at:
- index += pos*(after.d->size - 1);
+ index += pos*(after.size() - 1);
}
return *this;
}
@@ -3254,13 +3231,13 @@ QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs
*/
QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs)
{
- if (d->size) {
+ if (d.size) {
const int idx = indexOf(before, 0, cs);
if (idx != -1) {
detach();
const ushort a = after.unicode();
- ushort *i = d->data();
- const ushort *e = i + d->size;
+ ushort *i = d.data();
+ ushort *const e = i + d.size;
i += idx;
*i = a;
if (cs == Qt::CaseSensitive) {
@@ -3321,7 +3298,7 @@ QString &QString::replace(QLatin1String before, const QString &after, Qt::CaseSe
int blen = before.size();
QVarLengthArray<ushort> b(blen);
qt_from_latin1(b.data(), before.latin1(), blen);
- return replace((const QChar *)b.data(), blen, after.constData(), after.d->size, cs);
+ return replace((const QChar *)b.data(), blen, after.constData(), after.d.size, cs);
}
/*!
@@ -3341,7 +3318,7 @@ QString &QString::replace(const QString &before, QLatin1String after, Qt::CaseSe
int alen = after.size();
QVarLengthArray<ushort> a(alen);
qt_from_latin1(a.data(), after.latin1(), alen);
- return replace(before.constData(), before.d->size, (const QChar *)a.data(), alen, cs);
+ return replace(before.constData(), before.d.size, (const QChar *)a.data(), alen, cs);
}
/*!
@@ -3377,7 +3354,7 @@ QString &QString::replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs)
*/
bool operator==(const QString &s1, const QString &s2) noexcept
{
- if (s1.d->size != s2.d->size)
+ if (s1.d.size != s2.d.size)
return false;
return qt_compare_strings(s1, s2, Qt::CaseSensitive) == 0;
@@ -3390,7 +3367,7 @@ bool operator==(const QString &s1, const QString &s2) noexcept
*/
bool QString::operator==(QLatin1String other) const noexcept
{
- if (d->size != other.size())
+ if (size() != other.size())
return false;
return qt_compare_strings(*this, other, Qt::CaseSensitive) == 0;
@@ -3817,7 +3794,7 @@ int QString::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) co
int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
{
// ### Qt6: qsizetype
- return int(QtPrivate::lastIndexOf(*this, from, str, cs));
+ return int(QtPrivate::lastIndexOf(QStringView(*this), from, str, cs));
}
#endif // QT_STRINGVIEW_LEVEL < 2
@@ -3856,7 +3833,7 @@ int QString::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) co
int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
{
// ### Qt6: qsizetype
- return int(qLastIndexOf(*this, ch, from, cs));
+ return int(qLastIndexOf(QStringView(*this), ch, from, cs));
}
#if QT_STRINGVIEW_LEVEL < 2
@@ -3936,7 +3913,7 @@ QString& QString::replace(const QRegExp &rx, const QString &after)
if (isEmpty() && rx2.indexIn(*this) == -1)
return *this;
- reallocData(uint(d->size) + 1u);
+ reallocData(uint(d.size) + 1u);
int index = 0;
int numCaptures = rx2.captureCount();
@@ -4035,8 +4012,8 @@ QString& QString::replace(const QRegExp &rx, const QString &after)
}
if (!pos)
break;
- replacements[pos].pos = d->size;
- int newlen = d->size + adjust;
+ replacements[pos].pos = d.size;
+ int newlen = d.size + adjust;
// to continue searching at the right position after we did
// the first round of replacements
@@ -4051,14 +4028,14 @@ QString& QString::replace(const QRegExp &rx, const QString &after)
while (i < pos) {
int copyend = replacements[i].pos;
int size = copyend - copystart;
- memcpy(static_cast<void*>(uc), static_cast<const void *>(d->data() + copystart), size * sizeof(QChar));
+ memcpy(static_cast<void*>(uc), static_cast<const void *>(d.data() + copystart), size * sizeof(QChar));
uc += size;
- memcpy(static_cast<void *>(uc), static_cast<const void *>(after.d->data()), al * sizeof(QChar));
+ memcpy(static_cast<void *>(uc), static_cast<const void *>(after.d.data()), al * sizeof(QChar));
uc += al;
copystart = copyend + replacements[i].length;
i++;
}
- memcpy(static_cast<void *>(uc), static_cast<const void *>(d->data() + copystart), (d->size - copystart) * sizeof(QChar));
+ memcpy(static_cast<void *>(uc), static_cast<const void *>(d.data() + copystart), (d.size - copystart) * sizeof(QChar));
newstring.resize(newlen);
*this = newstring;
caretMode = QRegExp::CaretWontMatch;
@@ -4098,7 +4075,7 @@ QString &QString::replace(const QRegularExpression &re, const QString &after)
if (!iterator.hasNext()) // no matches at all
return *this;
- reallocData(uint(d->size) + 1u);
+ reallocData(uint(d.size) + 1u);
int numCaptures = re.captureCount();
@@ -4850,9 +4827,9 @@ QString QString::section(const QRegularExpression &re, int start, int end, Secti
*/
QString QString::left(int n) const
{
- if (uint(n) >= uint(d->size))
+ if (uint(n) >= uint(size()))
return *this;
- return QString((const QChar*) d->data(), n);
+ return QString((const QChar*) d.data(), n);
}
/*!
@@ -4868,9 +4845,9 @@ QString QString::left(int n) const
*/
QString QString::right(int n) const
{
- if (uint(n) >= uint(d->size))
+ if (uint(n) >= uint(size()))
return *this;
- return QString((const QChar*) d->data() + d->size - n, n);
+ return QString(constData() + size() - n, n);
}
/*!
@@ -4893,18 +4870,19 @@ QString QString::right(int n) const
QString QString::mid(int position, int n) const
{
using namespace QtPrivate;
- switch (QContainerImplHelper::mid(d->size, &position, &n)) {
+ switch (QContainerImplHelper::mid(size(), &position, &n)) {
case QContainerImplHelper::Null:
return QString();
case QContainerImplHelper::Empty:
{
- QStringDataPtr empty = { Data::allocate(0) };
+ QPair<Data *, ushort *> pair = Data::allocate(0);
+ DataPointer empty = { pair.first, pair.second, 0 };
return QString(empty);
}
case QContainerImplHelper::Full:
return *this;
case QContainerImplHelper::Subset:
- return QString((const QChar*)d->data() + position, n);
+ return QString(constData() + position, n);
}
Q_UNREACHABLE();
return QString();
@@ -5058,21 +5036,25 @@ bool QString::endsWith(QChar c, Qt::CaseSensitivity cs) const
}
/*!
- Returns \c true if the string only contains uppercase letters,
- otherwise returns \c false.
+ Returns \c true if the string is uppercase, that is, it's identical
+ to its toUpper() folding.
+
+ Note that this does \e not mean that the string does not contain
+ lowercase letters (some lowercase letters do not have a uppercase
+ folding; they are left unchanged by toUpper()).
+ For more information, refer to the Unicode standard, section 3.13.
+
\since 5.12
- \sa QChar::isUpper(), isLower()
+ \sa QChar::toUpper(), isLower()
*/
bool QString::isUpper() const
{
- if (isEmpty())
- return false;
-
- const QChar *d = data();
+ QStringIterator it(*this);
- for (int i = 0, max = size(); i < max; ++i) {
- if (!d[i].isUpper())
+ while (it.hasNext()) {
+ uint uc = it.nextUnchecked();
+ if (qGetProp(uc)->cases[QUnicodeTables::UpperCase].diff)
return false;
}
@@ -5080,21 +5062,25 @@ bool QString::isUpper() const
}
/*!
- Returns \c true if the string only contains lowercase letters,
- otherwise returns \c false.
+ Returns \c true if the string is lowercase, that is, it's identical
+ to its toLower() folding.
+
+ Note that this does \e not mean that the string does not contain
+ uppercase letters (some uppercase letters do not have a lowercase
+ folding; they are left unchanged by toLower()).
+ For more information, refer to the Unicode standard, section 3.13.
+
\since 5.12
- \sa QChar::isLower(), isUpper()
+ \sa QChar::toLower(), isUpper()
*/
bool QString::isLower() const
{
- if (isEmpty())
- return false;
+ QStringIterator it(*this);
- const QChar *d = data();
-
- for (int i = 0, max = size(); i < max; ++i) {
- if (!d[i].isLower())
+ while (it.hasNext()) {
+ uint uc = it.nextUnchecked();
+ if (qGetProp(uc)->cases[QUnicodeTables::LowerCase].diff)
return false;
}
@@ -5146,29 +5132,29 @@ QByteArray QString::toLatin1_helper_inplace(QString &s)
// We can return our own buffer to the caller.
// Conversion to Latin-1 always shrinks the buffer by half.
- const ushort *data = reinterpret_cast<const ushort *>(s.constData());
- uint length = s.size();
+ const ushort *data = s.d.data();
+ int length = s.d.size;
- // Swap the d pointers.
+ // Move the d pointer over to the bytearray.
// Kids, avert your eyes. Don't try this at home.
- QArrayData *ba_d = s.d;
- // multiply the allocated capacity by sizeof(ushort)
- ba_d->alloc *= sizeof(ushort);
+ // this relies on the fact that we use QArrayData for everything behind the scenes which has the same layout
+ static_assert(sizeof(QByteArray::DataPointer) == sizeof(QString::DataPointer), "sizes have to be equal");
+ QByteArray::DataPointer ba_d(reinterpret_cast<QByteArray::Data *>(s.d.d_ptr()), reinterpret_cast<char *>(s.d.data()), length);
+ ba_d.ref();
+ s.clear();
- // reset ourselves to QString()
- s.d = QString().d;
+ char *ddata = ba_d.data();
- // do the in-place conversion
- uchar *dst = reinterpret_cast<uchar *>(ba_d->data());
- qt_to_latin1(dst, data, length);
- dst[length] = '\0';
+ // multiply the allocated capacity by sizeof(ushort)
+ ba_d.d_ptr()->alloc *= sizeof(ushort);
- QByteArrayDataPtr badptr = { ba_d };
- return QByteArray(badptr);
+ // do the in-place conversion
+ qt_to_latin1(reinterpret_cast<uchar *>(ddata), data, length);
+ ddata[length] = '\0';
+ return QByteArray(ba_d);
}
-
/*!
\fn QByteArray QString::toLatin1() const
@@ -5348,34 +5334,25 @@ QVector<uint> QtPrivate::convertToUcs4(QStringView string)
return qt_convert_to_ucs4(string);
}
-QString::Data *QString::fromLatin1_helper(const char *str, int size)
+QString::DataPointer QString::fromLatin1_helper(const char *str, int size)
{
- Data *d;
+ DataPointer d;
if (!str) {
- d = Data::sharedNull();
+ // nothing to do
} else if (size == 0 || (!*str && size < 0)) {
- d = Data::allocate(0);
+ d = DataPointer(Data::allocate(0), 0);
} else {
if (size < 0)
size = qstrlen(str);
- d = Data::allocate(size + 1);
- Q_CHECK_PTR(d);
- d->size = size;
- d->data()[size] = '\0';
- ushort *dst = d->data();
+ d = DataPointer(Data::allocate(size + 1), size);
+ d.data()[size] = '\0';
+ ushort *dst = d.data();
qt_from_latin1(dst, str, uint(size));
}
return d;
}
-QString::Data *QString::fromAscii_helper(const char *str, int size)
-{
- QString s = fromUtf8(str, size);
- s.d->ref.ref();
- return s.d;
-}
-
/*! \fn QString QString::fromLatin1(const char *str, int size)
Returns a QString initialized with the first \a size characters
of the Latin-1 string \a str.
@@ -5418,7 +5395,8 @@ QString QString::fromLocal8Bit_helper(const char *str, int size)
if (!str)
return QString();
if (size == 0 || (!*str && size < 0)) {
- QStringDataPtr empty = { Data::allocate(0) };
+ QPair<Data *, ushort *> pair = Data::allocate(0);
+ QString::DataPointer empty = { pair.first, pair.second, 0 };
return QString(empty);
}
#if QT_CONFIG(textcodec)
@@ -5588,7 +5566,7 @@ QString& QString::setUnicode(const QChar *unicode, int size)
{
resize(size);
if (unicode && size)
- memcpy(d->data(), unicode, size * sizeof(QChar));
+ memcpy(d.data(), unicode, size * sizeof(QChar));
return *this;
}
@@ -5710,7 +5688,7 @@ QString QString::trimmed_helper(QString &str)
*/
/*!
- \fn QCharRef QString::operator[](int position)
+ \fn QChar &QString::operator[](int position)
Returns the character at the specified \a position in the string as a
modifiable reference.
@@ -5719,20 +5697,6 @@ QString QString::trimmed_helper(QString &str)
\snippet qstring/main.cpp 85
- The return value is of type QCharRef, a helper class for QString.
- When you get an object of type QCharRef, you can use it as if it
- were a reference to a QChar. If you assign to it, the assignment will apply to
- the character in the QString from which you got the reference.
-
- \note Before Qt 5.14 it was possible to use this operator to access
- a character at an out-of-bounds position in the string, and
- then assign to such a position, causing the string to be
- automatically resized. Furthermore, assigning a value to the
- returned QCharRef would cause a detach of the string, even if the
- string has been copied in the meanwhile (and the QCharRef kept
- alive while the copy was taken). These behaviors are deprecated,
- and will be changed in a future version of Qt.
-
\sa at()
*/
@@ -5742,19 +5706,6 @@ QString QString::trimmed_helper(QString &str)
\overload operator[]()
*/
-/*! \fn QCharRef QString::operator[](uint position)
-
-\overload operator[]()
-
-Returns the character at the specified \a position in the string as a
-modifiable reference.
-*/
-
-/*! \fn const QChar QString::operator[](uint position) const
- Equivalent to \c at(position).
-\overload operator[]()
-*/
-
/*!
\fn QChar QString::front() const
\since 5.10
@@ -5786,7 +5737,7 @@ modifiable reference.
*/
/*!
- \fn QCharRef QString::front()
+ \fn QChar &QString::front()
\since 5.10
Returns a reference to the first character in the string.
@@ -5801,7 +5752,7 @@ modifiable reference.
*/
/*!
- \fn QCharRef QString::back()
+ \fn QChar &QString::back()
\since 5.10
Returns a reference to the last character in the string.
@@ -5834,7 +5785,7 @@ modifiable reference.
void QString::truncate(int pos)
{
- if (pos < d->size)
+ if (pos < size())
resize(pos);
}
@@ -5856,7 +5807,7 @@ void QString::truncate(int pos)
void QString::chop(int n)
{
if (n > 0)
- resize(d->size - n);
+ resize(d.size - n);
}
/*!
@@ -5873,10 +5824,10 @@ void QString::chop(int n)
QString& QString::fill(QChar ch, int size)
{
- resize(size < 0 ? d->size : size);
- if (d->size) {
- QChar *i = (QChar*)d->data() + d->size;
- QChar *b = (QChar*)d->data();
+ resize(size < 0 ? d.size : size);
+ if (d.size) {
+ QChar *i = (QChar*)d.data() + d.size;
+ QChar *b = (QChar*)d.data();
while (i != b)
*--i = ch;
}
@@ -6457,11 +6408,11 @@ int QString::localeAwareCompare_helper(const QChar *data1, int length1,
const ushort *QString::utf16() const
{
- if (IS_RAW_DATA(d)) {
+ if (!d->isMutable()) {
// ensure '\0'-termination for ::fromRawData strings
- const_cast<QString*>(this)->reallocData(uint(d->size) + 1u);
+ const_cast<QString*>(this)->reallocData(uint(d.size) + 1u);
}
- return d->data();
+ return d.data();
}
/*!
@@ -6490,8 +6441,8 @@ QString QString::leftJustified(int width, QChar fill, bool truncate) const
if (padlen > 0) {
result.resize(len+padlen);
if (len)
- memcpy(result.d->data(), d->data(), sizeof(QChar)*len);
- QChar *uc = (QChar*)result.d->data() + len;
+ memcpy(result.d.data(), d.data(), sizeof(QChar)*len);
+ QChar *uc = (QChar*)result.d.data() + len;
while (padlen--)
* uc++ = fill;
} else {
@@ -6528,11 +6479,11 @@ QString QString::rightJustified(int width, QChar fill, bool truncate) const
int padlen = width - len;
if (padlen > 0) {
result.resize(len+padlen);
- QChar *uc = (QChar*)result.d->data();
+ QChar *uc = (QChar*)result.d.data();
while (padlen--)
* uc++ = fill;
if (len)
- memcpy(static_cast<void *>(uc), static_cast<const void *>(d->data()), sizeof(QChar)*len);
+ memcpy(static_cast<void *>(uc), static_cast<const void *>(d.data()), sizeof(QChar)*len);
} else {
if (truncate)
result = left(width);
@@ -7941,7 +7892,7 @@ QVector<QStringRef> QString::splitRef(const QRegularExpression &re, SplitBehavio
*/
QString QString::repeated(int times) const
{
- if (d->size == 0)
+ if (d.size == 0)
return *this;
if (times <= 1) {
@@ -7950,27 +7901,27 @@ QString QString::repeated(int times) const
return QString();
}
- const int resultSize = times * d->size;
+ const int resultSize = times * d.size;
QString result;
result.reserve(resultSize);
- if (result.d->alloc != uint(resultSize) + 1u)
+ if (result.capacity() != resultSize)
return QString(); // not enough memory
- memcpy(result.d->data(), d->data(), d->size * sizeof(ushort));
+ memcpy(result.d.data(), d.data(), d.size * sizeof(ushort));
- int sizeSoFar = d->size;
- ushort *end = result.d->data() + sizeSoFar;
+ int sizeSoFar = d.size;
+ ushort *end = result.d.data() + sizeSoFar;
const int halfResultSize = resultSize >> 1;
while (sizeSoFar <= halfResultSize) {
- memcpy(end, result.d->data(), sizeSoFar * sizeof(ushort));
+ memcpy(end, result.d.data(), sizeSoFar * sizeof(ushort));
end += sizeSoFar;
sizeSoFar <<= 1;
}
- memcpy(end, result.d->data(), (resultSize - sizeSoFar) * sizeof(ushort));
- result.d->data()[resultSize] = '\0';
- result.d->size = resultSize;
+ memcpy(end, result.d.data(), (resultSize - sizeSoFar) * sizeof(ushort));
+ result.d.data()[resultSize] = '\0';
+ result.d.size = resultSize;
return result;
}
@@ -8970,8 +8921,8 @@ QString QtPrivate::argToQString(QLatin1String pattern, size_t n, const ArgBase *
*/
bool QString::isSimpleText() const
{
- const ushort *p = d->data();
- const ushort * const end = p + d->size;
+ const ushort *p = d.data();
+ const ushort * const end = p + d.size;
while (p < end) {
ushort uc = *p;
// sort out regions of complex text formatting
@@ -8995,6 +8946,21 @@ bool QString::isRightToLeft() const
return QtPrivate::isRightToLeft(QStringView(*this));
}
+/*!
+ \fn bool QString::isValidUtf16() const noexcept
+ \since 5.15
+
+ Returns \c true if the string contains valid UTF-16 encoded data,
+ or \c false otherwise.
+
+ Note that this function does not perform any special validation of the
+ data; it merely checks if it can be successfully decoded from UTF-16.
+ The data is assumed to be in host byte order; the presence of a BOM
+ is meaningless.
+
+ \sa QStringView::isValidUtf16()
+*/
+
/*! \fn QChar *QString::data()
Returns a pointer to the data stored in the QString. The pointer
@@ -9120,17 +9086,14 @@ bool QString::isRightToLeft() const
*/
QString QString::fromRawData(const QChar *unicode, int size)
{
- Data *x;
+ QString::DataPointer x;
if (!unicode) {
- x = Data::sharedNull();
} else if (!size) {
- x = Data::allocate(0);
+ x = DataPointer(Data::allocate(0), 0);
} else {
x = Data::fromRawData(reinterpret_cast<const ushort *>(unicode), size);
- Q_CHECK_PTR(x);
}
- QStringDataPtr dataPtr = { x };
- return QString(dataPtr);
+ return QString(x);
}
/*!
@@ -9149,17 +9112,10 @@ QString QString::fromRawData(const QChar *unicode, int size)
*/
QString &QString::setRawData(const QChar *unicode, int size)
{
- if (d->ref.isShared() || d->alloc) {
- *this = fromRawData(unicode, size);
- } else {
- if (unicode) {
- d->size = size;
- d->offset = reinterpret_cast<const char *>(unicode) - reinterpret_cast<char *>(d);
- } else {
- d->offset = sizeof(QStringData);
- d->size = 0;
- }
+ if (!unicode || !size) {
+ clear();
}
+ *this = fromRawData(unicode, size);
return *this;
}
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h
index 4635962057..65d702ff1c 100644
--- a/src/corelib/text/qstring.h
+++ b/src/corelib/text/qstring.h
@@ -1,7 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2016 Intel Corporation.
+** Copyright (C) 2019 Intel Corporation.
** Copyright (C) 2019 Mail.ru Group.
** Contact: https://www.qt.io/licensing/
**
@@ -48,7 +48,7 @@
#include <QtCore/qchar.h>
#include <QtCore/qbytearray.h>
-#include <QtCore/qrefcount.h>
+#include <QtCore/qarraydata.h>
#include <QtCore/qnamespace.h>
#include <QtCore/qstringliteral.h>
#include <QtCore/qstringalgorithms.h>
@@ -70,7 +70,6 @@ Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
QT_BEGIN_NAMESPACE
-class QCharRef;
class QRegExp;
class QRegularExpression;
class QRegularExpressionMatch;
@@ -246,8 +245,9 @@ qsizetype QStringView::lastIndexOf(QLatin1String s, qsizetype from, Qt::CaseSens
class Q_CORE_EXPORT QString
{
+ typedef QTypedArrayData<ushort> Data;
public:
- typedef QStringData Data;
+ typedef QStringPrivate DataPointer;
inline QString() noexcept;
explicit QString(const QChar *unicode, int size = -1);
@@ -259,12 +259,13 @@ public:
QString &operator=(QChar c);
QString &operator=(const QString &) noexcept;
QString &operator=(QLatin1String latin1);
- inline QString(QString && other) noexcept : d(other.d) { other.d = Data::sharedNull(); }
+ inline QString(QString &&other) noexcept
+ { qSwap(d, other.d); }
inline QString &operator=(QString &&other) noexcept
{ qSwap(d, other.d); return *this; }
inline void swap(QString &other) noexcept { qSwap(d, other.d); }
- inline int size() const { return d->size; }
- inline int count() const { return d->size; }
+ inline int size() const { return int(d.size); }
+ inline int count() const { return int(d.size); }
inline int length() const;
inline bool isEmpty() const;
void resize(int size);
@@ -274,7 +275,7 @@ public:
void truncate(int pos);
void chop(int n);
- int capacity() const;
+ inline int capacity() const;
inline void reserve(int size);
inline void squeeze();
@@ -285,19 +286,17 @@ public:
inline void detach();
inline bool isDetached() const;
- inline bool isSharedWith(const QString &other) const { return d == other.d; }
+ inline bool isSharedWith(const QString &other) const { return d.isSharedWith(other.d); }
void clear();
inline const QChar at(int i) const;
const QChar operator[](int i) const;
- Q_REQUIRED_RESULT QCharRef operator[](int i);
- const QChar operator[](uint i) const;
- Q_REQUIRED_RESULT QCharRef operator[](uint i);
+ Q_REQUIRED_RESULT QChar &operator[](int i);
Q_REQUIRED_RESULT inline QChar front() const { return at(0); }
- Q_REQUIRED_RESULT inline QCharRef front();
+ Q_REQUIRED_RESULT inline QChar &front();
Q_REQUIRED_RESULT inline QChar back() const { return at(size() - 1); }
- Q_REQUIRED_RESULT inline QCharRef back();
+ Q_REQUIRED_RESULT inline QChar &back();
Q_REQUIRED_RESULT QString arg(qlonglong a, int fieldwidth=0, int base=10,
QChar fillChar = QLatin1Char(' ')) const;
@@ -486,7 +485,7 @@ public:
Q_REQUIRED_RESULT QString rightJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const;
#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
-# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_CPP_ATTRIBUTE(nodiscard)
+# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !__has_cpp_attribute(nodiscard)
// required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941
# pragma push_macro("Q_REQUIRED_RESULT")
# undef Q_REQUIRED_RESULT
@@ -542,10 +541,10 @@ public:
inline QString &prepend(QLatin1String s) { return insert(0, s); }
inline QString &operator+=(QChar c) {
- if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
- reallocData(uint(d->size) + 2u, true);
- d->data()[d->size++] = c.unicode();
- d->data()[d->size] = '\0';
+ if (d->needsDetach() || int(d.size + 1) > capacity())
+ reallocData(uint(d.size) + 2u, true);
+ d->data()[d.size++] = c.unicode();
+ d->data()[d.size] = '\0';
return *this;
}
@@ -658,8 +657,7 @@ public:
// note - this are all inline so we can benefit from strlen() compile time optimizations
static inline QString fromLatin1(const char *str, int size = -1)
{
- QStringDataPtr dataPtr = { fromLatin1_helper(str, (str && size == -1) ? int(strlen(str)) : size) };
- return QString(dataPtr);
+ return QString(fromLatin1_helper(str, (str && size == -1) ? int(strlen(str)) : size));
}
static inline QString fromUtf8(const char *str, int size = -1)
{
@@ -791,10 +789,10 @@ public:
#endif
#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
inline QT_ASCII_CAST_WARN QString(const char *ch)
- : d(fromAscii_helper(ch, ch ? int(strlen(ch)) : -1))
+ : QString(fromUtf8(ch))
{}
inline QT_ASCII_CAST_WARN QString(const QByteArray &a)
- : d(fromAscii_helper(a.constData(), qstrnlen(a.constData(), a.size())))
+ : QString(fromUtf8(a))
{}
inline QT_ASCII_CAST_WARN QString &operator=(const char *ch)
{ return (*this = fromUtf8(ch)); }
@@ -910,17 +908,19 @@ public:
struct Null { };
QT_DEPRECATED_X("use QString()")
static const Null null;
- inline QString(const Null &): d(Data::sharedNull()) {}
+ inline QString(const Null &) {}
inline QString &operator=(const Null &) { *this = QString(); return *this; }
#endif
- inline bool isNull() const { return d == Data::sharedNull(); }
+ inline bool isNull() const { return d->isNull(); }
bool isSimpleText() const;
bool isRightToLeft() const;
+ Q_REQUIRED_RESULT bool isValidUtf16() const noexcept
+ { return QStringView(*this).isValidUtf16(); }
QString(int size, Qt::Initialization);
- Q_DECL_CONSTEXPR inline QString(QStringDataPtr dd) : d(dd.ptr) {}
+ explicit QString(DataPointer dd) : d(dd) {}
private:
#if defined(QT_NO_CAST_FROM_ASCII)
@@ -932,7 +932,7 @@ private:
QString &operator=(const QByteArray &a);
#endif
- Data *d;
+ DataPointer d;
friend inline bool operator==(QChar, const QString &) noexcept;
friend inline bool operator< (QChar, const QString &) noexcept;
@@ -970,8 +970,7 @@ private:
static QString trimmed_helper(QString &str);
static QString simplified_helper(const QString &str);
static QString simplified_helper(QString &str);
- static Data *fromLatin1_helper(const char *str, int size = -1);
- static Data *fromAscii_helper(const char *str, int size = -1);
+ static DataPointer fromLatin1_helper(const char *str, int size = -1);
static QString fromUtf8_helper(const char *str, int size);
static QString fromLocal8Bit_helper(const char *, int size);
static QByteArray toLatin1_helper(const QString &);
@@ -982,7 +981,6 @@ private:
static qlonglong toIntegral_helper(const QChar *data, int len, bool *ok, int base);
static qulonglong toIntegral_helper(const QChar *data, uint len, bool *ok, int base);
void replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen);
- friend class QCharRef;
friend class QTextCodec;
friend class QStringRef;
friend class QStringView;
@@ -1007,8 +1005,7 @@ private:
}
public:
- typedef Data * DataPtr;
- inline DataPtr &data_ptr() { return d; }
+ inline DataPointer &data_ptr() { return d; }
};
//
@@ -1023,33 +1020,31 @@ QString QStringView::toString() const
inline QString::QString(QLatin1String aLatin1) : d(fromLatin1_helper(aLatin1.latin1(), aLatin1.size()))
{ }
inline int QString::length() const
-{ return d->size; }
+{ return int(d.size); }
inline const QChar QString::at(int i) const
-{ Q_ASSERT(uint(i) < uint(size())); return QChar(d->data()[i]); }
+{ Q_ASSERT(uint(i) < uint(size())); return QChar(d.data()[i]); }
inline const QChar QString::operator[](int i) const
-{ Q_ASSERT(uint(i) < uint(size())); return QChar(d->data()[i]); }
-inline const QChar QString::operator[](uint i) const
-{ Q_ASSERT(i < uint(size())); return QChar(d->data()[i]); }
+{ Q_ASSERT(uint(i) < uint(size())); return QChar(d.data()[i]); }
inline bool QString::isEmpty() const
-{ return d->size == 0; }
+{ return d.size == 0; }
inline const QChar *QString::unicode() const
-{ return reinterpret_cast<const QChar*>(d->data()); }
+{ return reinterpret_cast<const QChar*>(d.data()); }
inline const QChar *QString::data() const
-{ return reinterpret_cast<const QChar*>(d->data()); }
+{ return reinterpret_cast<const QChar*>(d.data()); }
inline QChar *QString::data()
-{ detach(); return reinterpret_cast<QChar*>(d->data()); }
+{ detach(); return reinterpret_cast<QChar*>(d.data()); }
inline const QChar *QString::constData() const
-{ return reinterpret_cast<const QChar*>(d->data()); }
+{ return reinterpret_cast<const QChar*>(d.data()); }
inline void QString::detach()
-{ if (d->ref.isShared() || (d->offset != sizeof(QStringData))) reallocData(uint(d->size) + 1u); }
+{ if (d->needsDetach()) reallocData(d.size + 1u); }
inline bool QString::isDetached() const
-{ return !d->ref.isShared(); }
+{ return !d->isShared(); }
inline void QString::clear()
{ if (!isNull()) *this = QString(); }
inline QString::QString(const QString &other) noexcept : d(other.d)
-{ Q_ASSERT(&other != this); d->ref.ref(); }
+{ }
inline int QString::capacity() const
-{ return d->alloc ? d->alloc - 1 : 0; }
+{ const auto realCapacity = d->constAllocatedCapacity(); return realCapacity ? int(realCapacity) - 1 : 0; }
inline QString &QString::setNum(short n, int base)
{ return setNum(qlonglong(n), base); }
inline QString &QString::setNum(ushort n, int base)
@@ -1136,178 +1131,51 @@ inline QString QString::fromWCharArray(const wchar_t *string, int size)
: fromUcs4(reinterpret_cast<const uint *>(string), size);
}
-class
-#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
-Q_CORE_EXPORT
-#endif
-QCharRef { // ### Qt 7: remove
- QString &s;
- int i;
- inline QCharRef(QString &str, int idx)
- : s(str),i(idx) {}
- friend class QString;
-public:
-
- // most QChar operations repeated here
-
- // all this is not documented: We just say "like QChar" and let it be.
- inline operator QChar() const
- {
- using namespace QtPrivate::DeprecatedRefClassBehavior;
- if (Q_LIKELY(i < s.d->size))
- return QChar(s.d->data()[i]);
-#ifdef QT_DEBUG
- warn(WarningType::OutOfRange, EmittingClass::QCharRef);
-#endif
- return QChar();
- }
- inline QCharRef &operator=(QChar c)
- {
- using namespace QtPrivate::DeprecatedRefClassBehavior;
- if (Q_UNLIKELY(i >= s.d->size)) {
-#ifdef QT_DEBUG
- warn(WarningType::OutOfRange, EmittingClass::QCharRef);
-#endif
- s.resize(i + 1, QLatin1Char(' '));
- } else {
-#ifdef QT_DEBUG
- if (Q_UNLIKELY(!s.isDetached()))
- warn(WarningType::DelayedDetach, EmittingClass::QCharRef);
-#endif
- s.detach();
- }
- s.d->data()[i] = c.unicode();
- return *this;
- }
-
- // An operator= for each QChar cast constructors
-#ifndef QT_NO_CAST_FROM_ASCII
- inline QT_ASCII_CAST_WARN QCharRef &operator=(char c)
- { return operator=(QChar::fromLatin1(c)); }
- inline QT_ASCII_CAST_WARN QCharRef &operator=(uchar c)
- { return operator=(QChar::fromLatin1(c)); }
-#endif
- inline QCharRef &operator=(const QCharRef &c) { return operator=(QChar(c)); }
- inline QCharRef &operator=(ushort rc) { return operator=(QChar(rc)); }
- inline QCharRef &operator=(short rc) { return operator=(QChar(rc)); }
- inline QCharRef &operator=(uint rc) { return operator=(QChar(rc)); }
- inline QCharRef &operator=(int rc) { return operator=(QChar(rc)); }
-
- // each function...
- inline bool isNull() const { return QChar(*this).isNull(); }
- inline bool isPrint() const { return QChar(*this).isPrint(); }
- inline bool isPunct() const { return QChar(*this).isPunct(); }
- inline bool isSpace() const { return QChar(*this).isSpace(); }
- inline bool isMark() const { return QChar(*this).isMark(); }
- inline bool isLetter() const { return QChar(*this).isLetter(); }
- inline bool isNumber() const { return QChar(*this).isNumber(); }
- inline bool isLetterOrNumber() { return QChar(*this).isLetterOrNumber(); }
- inline bool isDigit() const { return QChar(*this).isDigit(); }
- inline bool isLower() const { return QChar(*this).isLower(); }
- inline bool isUpper() const { return QChar(*this).isUpper(); }
- inline bool isTitleCase() const { return QChar(*this).isTitleCase(); }
-
- inline int digitValue() const { return QChar(*this).digitValue(); }
- QChar toLower() const { return QChar(*this).toLower(); }
- QChar toUpper() const { return QChar(*this).toUpper(); }
- QChar toTitleCase () const { return QChar(*this).toTitleCase(); }
-
- QChar::Category category() const { return QChar(*this).category(); }
- QChar::Direction direction() const { return QChar(*this).direction(); }
- QChar::JoiningType joiningType() const { return QChar(*this).joiningType(); }
-#if QT_DEPRECATED_SINCE(5, 3)
- QT_DEPRECATED QChar::Joining joining() const
- {
- switch (QChar(*this).joiningType()) {
- case QChar::Joining_Causing: return QChar::Center;
- case QChar::Joining_Dual: return QChar::Dual;
- case QChar::Joining_Right: return QChar::Right;
- case QChar::Joining_None:
- case QChar::Joining_Left:
- case QChar::Joining_Transparent:
- default: return QChar::OtherJoining;
- }
- }
-#endif
- bool hasMirrored() const { return QChar(*this).hasMirrored(); }
- QChar mirroredChar() const { return QChar(*this).mirroredChar(); }
- QString decomposition() const { return QChar(*this).decomposition(); }
- QChar::Decomposition decompositionTag() const { return QChar(*this).decompositionTag(); }
- uchar combiningClass() const { return QChar(*this).combiningClass(); }
-
- inline QChar::Script script() const { return QChar(*this).script(); }
-
- QChar::UnicodeVersion unicodeVersion() const { return QChar(*this).unicodeVersion(); }
-
- inline uchar cell() const { return QChar(*this).cell(); }
- inline uchar row() const { return QChar(*this).row(); }
- inline void setCell(uchar cell);
- inline void setRow(uchar row);
-
-#if QT_DEPRECATED_SINCE(5, 0)
- QT_DEPRECATED char toAscii() const { return QChar(*this).toLatin1(); }
-#endif
- char toLatin1() const { return QChar(*this).toLatin1(); }
- ushort unicode() const { return QChar(*this).unicode(); }
- ushort& unicode() { return s.data()[i].unicode(); }
-
-};
-Q_DECLARE_TYPEINFO(QCharRef, Q_MOVABLE_TYPE);
-
-inline void QCharRef::setRow(uchar arow) { QChar(*this).setRow(arow); }
-inline void QCharRef::setCell(uchar acell) { QChar(*this).setCell(acell); }
-
-
-inline QString::QString() noexcept : d(Data::sharedNull()) {}
-inline QString::~QString() { if (!d->ref.deref()) Data::deallocate(d); }
+inline QString::QString() noexcept {}
+inline QString::~QString() {}
inline void QString::reserve(int asize)
{
- if (d->ref.isShared() || uint(asize) >= d->alloc)
- reallocData(qMax(asize, d->size) + 1u);
+ if (d->needsDetach() || asize >= capacity())
+ reallocData(uint(qMax(asize, size())) + 1u);
- if (!d->capacityReserved) {
- // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const)
- d->capacityReserved = true;
- }
+ // we're not shared anymore, for sure
+ d->flags() |= Data::CapacityReserved;
}
inline void QString::squeeze()
{
- if (d->ref.isShared() || uint(d->size) + 1u < d->alloc)
- reallocData(uint(d->size) + 1u);
+ if ((d->flags() & Data::CapacityReserved) == 0)
+ return;
+ if (d->needsDetach() || int(d.size) < capacity())
+ reallocData(uint(d.size) + 1u);
- if (d->capacityReserved) {
- // cannot set unconditionally, since d could be shared_null or
- // otherwise static.
- d->capacityReserved = false;
- }
+ // we're not shared anymore, for sure
+ d->flags() &= uint(~Data::CapacityReserved);
}
inline QString &QString::setUtf16(const ushort *autf16, int asize)
{ return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); }
-inline QCharRef QString::operator[](int i)
-{ Q_ASSERT(i >= 0); detach(); return QCharRef(*this, i); }
-inline QCharRef QString::operator[](uint i)
-{ detach(); return QCharRef(*this, i); }
-inline QCharRef QString::front() { return operator[](0); }
-inline QCharRef QString::back() { return operator[](size() - 1); }
+inline QChar &QString::operator[](int i)
+{ Q_ASSERT(i >= 0 && i < size()); return data()[i]; }
+inline QChar &QString::front() { return operator[](0); }
+inline QChar &QString::back() { return operator[](size() - 1); }
inline QString::iterator QString::begin()
-{ detach(); return reinterpret_cast<QChar*>(d->data()); }
+{ detach(); return reinterpret_cast<QChar*>(d.data()); }
inline QString::const_iterator QString::begin() const
-{ return reinterpret_cast<const QChar*>(d->data()); }
+{ return reinterpret_cast<const QChar*>(d.data()); }
inline QString::const_iterator QString::cbegin() const
-{ return reinterpret_cast<const QChar*>(d->data()); }
+{ return reinterpret_cast<const QChar*>(d.data()); }
inline QString::const_iterator QString::constBegin() const
-{ return reinterpret_cast<const QChar*>(d->data()); }
+{ return reinterpret_cast<const QChar*>(d.data()); }
inline QString::iterator QString::end()
-{ detach(); return reinterpret_cast<QChar*>(d->data() + d->size); }
+{ detach(); return reinterpret_cast<QChar*>(d.data() + d.size); }
inline QString::const_iterator QString::end() const
-{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
+{ return reinterpret_cast<const QChar*>(d.data() + d.size); }
inline QString::const_iterator QString::cend() const
-{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
+{ return reinterpret_cast<const QChar*>(d.data() + d.size); }
inline QString::const_iterator QString::constEnd() const
-{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
+{ return reinterpret_cast<const QChar*>(d.data() + d.size); }
#if QT_STRINGVIEW_LEVEL < 2
inline bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const
{ return indexOf(s, 0, cs) != -1; }
@@ -1534,7 +1402,8 @@ inline QString QString::fromStdU32String(const std::u32string &s)
inline std::u32string QString::toStdU32String() const
{
std::u32string u32str(length(), char32_t(0));
- int len = toUcs4_helper(d->data(), length(), reinterpret_cast<uint*>(&u32str[0]));
+ int len = toUcs4_helper(reinterpret_cast<const ushort *>(constData()), length(),
+ reinterpret_cast<uint*>(&u32str[0]));
u32str.resize(len);
return u32str;
}
@@ -1666,7 +1535,7 @@ public:
inline const QChar *unicode() const
{
if (!m_string)
- return reinterpret_cast<const QChar *>(QString::Data::sharedNull()->data());
+ return reinterpret_cast<const QChar *>(QString::Data::sharedNullData());
return m_string->unicode() + m_position;
}
inline const QChar *data() const { return unicode(); }
diff --git a/src/corelib/text/qstringalgorithms.h b/src/corelib/text/qstringalgorithms.h
index d54e376aa9..0b7774b4f3 100644
--- a/src/corelib/text/qstringalgorithms.h
+++ b/src/corelib/text/qstringalgorithms.h
@@ -99,6 +99,7 @@ Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QLatin1String
Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QStringView s) noexcept;
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline bool isLatin1(QLatin1String s) noexcept;
Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLatin1(QStringView s) noexcept;
+Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf16(QStringView s) noexcept;
} // namespace QtPRivate
diff --git a/src/corelib/text/qstringbuilder.cpp b/src/corelib/text/qstringbuilder.cpp
index cf443ec369..29bd216e80 100644
--- a/src/corelib/text/qstringbuilder.cpp
+++ b/src/corelib/text/qstringbuilder.cpp
@@ -73,7 +73,7 @@ QT_BEGIN_NAMESPACE
\list
\li QString, QStringRef, (since 5.10:) QStringView
- \li QChar, QCharRef, QLatin1Char, (since 5.10:) \c char16_t,
+ \li QChar, QLatin1Char, (since 5.10:) \c char16_t,
\li QLatin1String,
\li (since 5.10:) \c{const char16_t[]} (\c{u"foo"}),
\li QByteArray, \c char, \c{const char[]}.
@@ -108,7 +108,7 @@ QT_BEGIN_NAMESPACE
This function is usable with arguments of type \c QString,
\c QLatin1String, \c QStringRef,
- \c QChar, \c QCharRef, \c QLatin1Char, and \c char.
+ \c QChar, \c QLatin1Char, and \c char.
*/
/* \fn template <typename A, typename B> QByteArray QStringBuilder<A, B>::toLatin1() const
diff --git a/src/corelib/text/qstringbuilder.h b/src/corelib/text/qstringbuilder.h
index 288d98d633..45fd270b66 100644
--- a/src/corelib/text/qstringbuilder.h
+++ b/src/corelib/text/qstringbuilder.h
@@ -231,16 +231,6 @@ template <> struct QConcatenable<QChar::SpecialCharacter> : private QAbstractCon
{ *out++ = c; }
};
-template <> struct QConcatenable<QCharRef> : private QAbstractConcatenable
-{
- typedef QCharRef type;
- typedef QString ConvertTo;
- enum { ExactSize = true };
- static int size(QCharRef) { return 1; }
- static inline void appendTo(QCharRef c, QChar *&out)
- { *out++ = QChar(c); }
-};
-
template <> struct QConcatenable<QLatin1String> : private QAbstractConcatenable
{
typedef QLatin1String type;
diff --git a/src/corelib/text/qstringliteral.h b/src/corelib/text/qstringliteral.h
index 2a7e607c63..742d38de7d 100644
--- a/src/corelib/text/qstringliteral.h
+++ b/src/corelib/text/qstringliteral.h
@@ -42,6 +42,7 @@
#define QSTRINGLITERAL_H
#include <QtCore/qarraydata.h>
+#include <QtCore/qarraydatapointer.h>
#if 0
#pragma qt_class(QStringLiteral)
@@ -49,8 +50,6 @@
QT_BEGIN_NAMESPACE
-typedef QTypedArrayData<ushort> QStringData;
-
// all our supported compilers support Unicode string literals,
// even if their Q_COMPILER_UNICODE_STRING has been revoked due
// to lacking stdlib support. But QStringLiteral only needs the
@@ -65,44 +64,23 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2,
#define QStringLiteral(str) \
([]() noexcept -> QString { \
enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
- static const QStaticStringData<Size> qstring_literal = { \
- Q_STATIC_STRING_DATA_HEADER_INITIALIZER(Size), \
- QT_UNICODE_LITERAL(str) }; \
- QStringDataPtr holder = { qstring_literal.data_ptr() }; \
- const QString qstring_literal_temp(holder); \
- return qstring_literal_temp; \
+ static const QArrayData qstring_literal = { \
+ Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 \
+ }; \
+ QStringPrivate holder = { \
+ static_cast<QTypedArrayData<ushort> *>(const_cast<QArrayData *>(&qstring_literal)), \
+ reinterpret_cast<ushort *>(const_cast<qunicodechar *>(QT_UNICODE_LITERAL(str))), \
+ Size \
+ }; \
+ return QString(holder); \
}()) \
/**/
-#define Q_STATIC_STRING_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \
- { Q_REFCOUNT_INITIALIZE_STATIC, size, 0, 0, offset } \
- /**/
-
-#define Q_STATIC_STRING_DATA_HEADER_INITIALIZER(size) \
- Q_STATIC_STRING_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, sizeof(QStringData)) \
- /**/
-
#if QT_DEPRECATED_SINCE(5, 14)
# define QStringViewLiteral(str) QStringView(QT_UNICODE_LITERAL(str), QtPrivate::Deprecated)
#endif
-template <int N>
-struct QStaticStringData
-{
- QArrayData str;
- qunicodechar data[N + 1];
-
- QStringData *data_ptr() const
- {
- Q_ASSERT(str.ref.isStatic());
- return const_cast<QStringData *>(static_cast<const QStringData*>(&str));
- }
-};
-
-struct QStringDataPtr
-{
- QStringData *ptr;
-};
+using QStringPrivate = QArrayDataPointer<ushort>;
QT_END_NAMESPACE
diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp
index 75de827583..c4ddb06ea4 100644
--- a/src/corelib/text/qstringview.cpp
+++ b/src/corelib/text/qstringview.cpp
@@ -865,6 +865,21 @@ QT_BEGIN_NAMESPACE
*/
/*!
+ \fn bool QStringView::isValidUtf16() const
+ \since 5.15
+
+ Returns \c true if the string contains valid UTF-16 encoded data,
+ or \c false otherwise.
+
+ Note that this function does not perform any special validation of the
+ data; it merely checks if it can be successfully decoded from UTF-16.
+ The data is assumed to be in host byte order; the presence of a BOM
+ is meaningless.
+
+ \sa QString::isValidUtf16()
+*/
+
+/*!
\fn QStringView::toWCharArray(wchar_t *array) const
\since 5.14
diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h
index 4ab4d2570f..06391ffef4 100644
--- a/src/corelib/text/qstringview.h
+++ b/src/corelib/text/qstringview.h
@@ -294,6 +294,8 @@ public:
Q_REQUIRED_RESULT bool isRightToLeft() const noexcept
{ return QtPrivate::isRightToLeft(*this); }
+ Q_REQUIRED_RESULT bool isValidUtf16() const noexcept
+ { return QtPrivate::isValidUtf16(*this); }
Q_REQUIRED_RESULT inline int toWCharArray(wchar_t *array) const; // defined in qstring.h
diff --git a/src/corelib/text/qtextboundaryfinder.cpp b/src/corelib/text/qtextboundaryfinder.cpp
index 67dd15377b..ebdba6b2c5 100644
--- a/src/corelib/text/qtextboundaryfinder.cpp
+++ b/src/corelib/text/qtextboundaryfinder.cpp
@@ -71,7 +71,7 @@ static void init(QTextBoundaryFinder::BoundaryType type, const QChar *chars, int
}
}
- QUnicodeTools::CharAttributeOptions options = 0;
+ QUnicodeTools::CharAttributeOptions options;
switch (type) {
case QTextBoundaryFinder::Grapheme: options |= QUnicodeTools::GraphemeBreaks; break;
case QTextBoundaryFinder::Word: options |= QUnicodeTools::WordBreaks; break;
@@ -161,10 +161,10 @@ static void init(QTextBoundaryFinder::BoundaryType type, const QChar *chars, int
*/
QTextBoundaryFinder::QTextBoundaryFinder()
: t(Grapheme)
- , chars(0)
+ , chars(nullptr)
, length(0)
, freePrivate(true)
- , d(0)
+ , d(nullptr)
{
}
@@ -178,7 +178,7 @@ QTextBoundaryFinder::QTextBoundaryFinder(const QTextBoundaryFinder &other)
, length(other.length)
, pos(other.pos)
, freePrivate(true)
- , d(0)
+ , d(nullptr)
{
if (other.d) {
Q_ASSERT(length > 0);
@@ -199,7 +199,7 @@ QTextBoundaryFinder &QTextBoundaryFinder::operator=(const QTextBoundaryFinder &o
if (other.d) {
Q_ASSERT(other.length > 0);
uint newCapacity = (other.length + 1) * sizeof(QCharAttributes);
- QTextBoundaryFinderPrivate *newD = (QTextBoundaryFinderPrivate *) realloc(freePrivate ? d : 0, newCapacity);
+ QTextBoundaryFinderPrivate *newD = (QTextBoundaryFinderPrivate *) realloc(freePrivate ? d : nullptr, newCapacity);
Q_CHECK_PTR(newD);
freePrivate = true;
d = newD;
@@ -216,7 +216,7 @@ QTextBoundaryFinder &QTextBoundaryFinder::operator=(const QTextBoundaryFinder &o
} else {
if (freePrivate)
free(d);
- d = 0;
+ d = nullptr;
}
return *this;
@@ -242,7 +242,7 @@ QTextBoundaryFinder::QTextBoundaryFinder(BoundaryType type, const QString &strin
, length(string.length())
, pos(0)
, freePrivate(true)
- , d(0)
+ , d(nullptr)
{
if (length > 0) {
d = (QTextBoundaryFinderPrivate *) malloc((length + 1) * sizeof(QCharAttributes));
@@ -271,7 +271,7 @@ QTextBoundaryFinder::QTextBoundaryFinder(BoundaryType type, const QChar *chars,
, length(length)
, pos(0)
, freePrivate(true)
- , d(0)
+ , d(nullptr)
{
if (!chars) {
length = 0;
diff --git a/src/corelib/text/qunicodetables_p.h b/src/corelib/text/qunicodetables_p.h
index 79878a859f..81efc09773 100644
--- a/src/corelib/text/qunicodetables_p.h
+++ b/src/corelib/text/qunicodetables_p.h
@@ -122,7 +122,8 @@ enum GraphemeBreakClass {
Graphemebreak_E_Modifier,
Graphemebreak_Glue_After_Zwj,
Graphemebreak_E_Base_GAZ,
- NumGraphemeBreakClasses,
+
+ NumGraphemeBreakClasses
};
enum WordBreakClass {
@@ -149,7 +150,8 @@ enum WordBreakClass {
WordBreak_Glue_After_Zwj,
WordBreak_E_Base_GAZ,
WordBreak_WSegSpace,
- NumWordBreakClasses,
+
+ NumWordBreakClasses
};
enum SentenceBreakClass {
@@ -167,6 +169,7 @@ enum SentenceBreakClass {
SentenceBreak_SContinue,
SentenceBreak_STerm,
SentenceBreak_Close,
+
NumSentenceBreakClasses
};
@@ -182,6 +185,7 @@ enum LineBreakClass {
LineBreak_EB, LineBreak_EM, LineBreak_ZWJ,
LineBreak_SA, LineBreak_SG, LineBreak_SP,
LineBreak_CR, LineBreak_LF, LineBreak_BK,
+
NumLineBreakClasses
};
diff --git a/src/corelib/thread/qatomic.cpp b/src/corelib/thread/qatomic.cpp
index b1a7edad91..5c3ad9412f 100644
--- a/src/corelib/thread/qatomic.cpp
+++ b/src/corelib/thread/qatomic.cpp
@@ -234,22 +234,26 @@
\sa QAtomicPointer
*/
-/*! \fn QAtomicInt::QAtomicInt(int value)
+/*!
+ \fn QAtomicInt::QAtomicInt(int value)
Constructs a QAtomicInt with the given \a value.
*/
-/*! \fn QAtomicInteger<T>::QAtomicInteger(T value)
+/*!
+ \fn template <typename T> QAtomicInteger<T>::QAtomicInteger(T value)
Constructs a QAtomicInteger with the given \a value.
*/
-/*! \fn template <typename T> QAtomicInteger<T>::QAtomicInteger(const QAtomicInteger &other)
+/*!
+ \fn template <typename T> QAtomicInteger<T>::QAtomicInteger(const QAtomicInteger &other)
Constructs a copy of \a other.
*/
-/*! \fn template <typename T> QAtomicInteger &QAtomicInteger<T>::operator=(const QAtomicInteger &other)
+/*!
+ \fn template <typename T> QAtomicInteger &QAtomicInteger<T>::operator=(const QAtomicInteger &other)
Assigns \a other to this QAtomicInteger and returns a reference to
this QAtomicInteger.
@@ -344,19 +348,22 @@
\sa storeRelaxed(), storeRelease()
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::isReferenceCountingNative()
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::isReferenceCountingNative()
Returns \c true if reference counting is implemented using atomic
processor instructions, false otherwise.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::isReferenceCountingWaitFree()
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::isReferenceCountingWaitFree()
Returns \c true if atomic reference counting is wait-free, false
otherwise.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::ref()
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::ref()
Atomically increments the value of this QAtomicInteger. Returns \c true
if the new value is non-zero, false otherwise.
@@ -394,7 +401,8 @@
\sa ref(), operator++(), operator--(int)
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::deref()
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::deref()
Atomically decrements the value of this QAtomicInteger. Returns \c true
if the new value is non-zero, false otherwise.
@@ -432,18 +440,21 @@
\sa deref(), operator--(), operator++(int)
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::isTestAndSetNative()
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::isTestAndSetNative()
Returns \c true if test-and-set is implemented using atomic processor
instructions, false otherwise.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::isTestAndSetWaitFree()
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::isTestAndSetWaitFree()
Returns \c true if atomic test-and-set is wait-free, false otherwise.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::testAndSetRelaxed(T expectedValue, T newValue)
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::testAndSetRelaxed(T expectedValue, T newValue)
Atomic test-and-set.
@@ -457,7 +468,8 @@
processor to freely reorder memory accesses.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::testAndSetAcquire(T expectedValue, T newValue)
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::testAndSetAcquire(T expectedValue, T newValue)
Atomic test-and-set.
@@ -472,7 +484,8 @@
be re-ordered before the atomic operation.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::testAndSetRelease(T expectedValue, T newValue)
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::testAndSetRelease(T expectedValue, T newValue)
Atomic test-and-set.
@@ -487,7 +500,8 @@
re-ordered after the atomic operation.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::testAndSetOrdered(T expectedValue, T newValue)
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::testAndSetOrdered(T expectedValue, T newValue)
Atomic test-and-set.
@@ -502,19 +516,22 @@
may not be re-ordered.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::isFetchAndStoreNative()
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::isFetchAndStoreNative()
Returns \c true if fetch-and-store is implemented using atomic
processor instructions, false otherwise.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::isFetchAndStoreWaitFree()
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::isFetchAndStoreWaitFree()
Returns \c true if atomic fetch-and-store is wait-free, false
otherwise.
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreRelaxed(T newValue)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreRelaxed(T newValue)
Atomic fetch-and-store.
@@ -526,7 +543,8 @@
processor to freely reorder memory accesses.
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreAcquire(T newValue)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreAcquire(T newValue)
Atomic fetch-and-store.
@@ -539,7 +557,8 @@
be re-ordered before the atomic operation.
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreRelease(T newValue)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreRelease(T newValue)
Atomic fetch-and-store.
@@ -552,7 +571,8 @@
re-ordered after the atomic operation.
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreOrdered(T newValue)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreOrdered(T newValue)
Atomic fetch-and-store.
@@ -565,19 +585,22 @@
may not be re-ordered.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::isFetchAndAddNative()
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::isFetchAndAddNative()
Returns \c true if fetch-and-add is implemented using atomic
processor instructions, false otherwise.
*/
-/*! \fn template <typename T> bool QAtomicInteger<T>::isFetchAndAddWaitFree()
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::isFetchAndAddWaitFree()
Returns \c true if atomic fetch-and-add is wait-free, false
otherwise.
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndAddRelaxed(T valueToAdd)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndAddRelaxed(T valueToAdd)
Atomic fetch-and-add.
@@ -591,7 +614,8 @@
\sa operator+=(), fetchAndSubRelaxed()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndAddAcquire(T valueToAdd)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndAddAcquire(T valueToAdd)
Atomic fetch-and-add.
@@ -606,7 +630,8 @@
\sa operator+=(), fetchAndSubAcquire()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndAddRelease(T valueToAdd)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndAddRelease(T valueToAdd)
Atomic fetch-and-add.
@@ -621,7 +646,8 @@
\sa operator+=(), fetchAndSubRelease()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndAddOrdered(T valueToAdd)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndAddOrdered(T valueToAdd)
Atomic fetch-and-add.
@@ -636,7 +662,8 @@
\sa operator+=(), fetchAndSubOrdered()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::operator+=(T value)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::operator+=(T value)
\since 5.3
Atomic add-and-fetch.
@@ -650,7 +677,8 @@
\sa fetchAndAddOrdered(), operator-=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndSubRelaxed(T valueToSub)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndSubRelaxed(T valueToSub)
\since 5.3
Atomic fetch-and-sub.
@@ -665,7 +693,8 @@
\sa operator-=(), fetchAndAddRelaxed()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndSubAcquire(T valueToSub)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndSubAcquire(T valueToSub)
\since 5.3
Atomic fetch-and-sub.
@@ -681,7 +710,8 @@
\sa operator-=(), fetchAndAddAcquire()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndSubRelease(T valueToSub)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndSubRelease(T valueToSub)
\since 5.3
Atomic fetch-and-sub.
@@ -697,7 +727,8 @@
\sa operator-=(), fetchAndAddRelease()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndSubOrdered(T valueToSub)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndSubOrdered(T valueToSub)
\since 5.3
Atomic fetch-and-sub.
@@ -713,7 +744,8 @@
\sa operator-=(), fetchAndAddOrdered()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::operator-=(T value)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::operator-=(T value)
\since 5.3
Atomic sub-and-fetch.
@@ -727,7 +759,8 @@
\sa fetchAndSubOrdered(), operator+=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndOrRelaxed(T valueToOr)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndOrRelaxed(T valueToOr)
\since 5.3
Atomic fetch-and-or.
@@ -742,7 +775,8 @@
\sa operator|=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndOrAcquire(T valueToOr)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndOrAcquire(T valueToOr)
\since 5.3
Atomic fetch-and-or.
@@ -758,7 +792,8 @@
\sa operator|=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndOrRelease(T valueToOr)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndOrRelease(T valueToOr)
\since 5.3
Atomic fetch-and-or.
@@ -774,7 +809,8 @@
\sa operator|=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndOrOrdered(T valueToOr)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndOrOrdered(T valueToOr)
\since 5.3
Atomic fetch-and-or.
@@ -790,7 +826,8 @@
\sa operator|=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::operator|=(T value)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::operator|=(T value)
\since 5.3
Atomic or-and-fetch.
@@ -804,7 +841,8 @@
\sa fetchAndOrOrdered()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndXorRelaxed(T valueToXor)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndXorRelaxed(T valueToXor)
\since 5.3
Atomic fetch-and-xor.
@@ -819,7 +857,8 @@
\sa operator^=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndXorAcquire(T valueToXor)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndXorAcquire(T valueToXor)
\since 5.3
Atomic fetch-and-xor.
@@ -835,7 +874,8 @@
\sa operator^=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndXorRelease(T valueToXor)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndXorRelease(T valueToXor)
\since 5.3
Atomic fetch-and-xor.
@@ -851,7 +891,8 @@
\sa operator^=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndXorOrdered(T valueToXor)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndXorOrdered(T valueToXor)
\since 5.3
Atomic fetch-and-xor.
@@ -867,7 +908,8 @@
\sa operator^=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::operator^=(T value)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::operator^=(T value)
\since 5.3
Atomic xor-and-fetch.
@@ -881,7 +923,8 @@
\sa fetchAndXorOrdered()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndAndRelaxed(T valueToAnd)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndAndRelaxed(T valueToAnd)
\since 5.3
Atomic fetch-and-and.
@@ -896,7 +939,8 @@
\sa operator&=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndAndAcquire(T valueToAnd)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndAndAcquire(T valueToAnd)
\since 5.3
Atomic fetch-and-and.
@@ -912,7 +956,8 @@
\sa operator&=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndAndRelease(T valueToAnd)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndAndRelease(T valueToAnd)
\since 5.3
Atomic fetch-and-and.
@@ -928,7 +973,8 @@
\sa operator&=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::fetchAndAndOrdered(T valueToAnd)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::fetchAndAndOrdered(T valueToAnd)
\since 5.3
Atomic fetch-and-and.
@@ -944,7 +990,8 @@
\sa operator&=()
*/
-/*! \fn template <typename T> T QAtomicInteger<T>::operator&=(T value)
+/*!
+ \fn template <typename T> T QAtomicInteger<T>::operator&=(T value)
\since 5.3
Atomic add-and-fetch.
@@ -1287,17 +1334,20 @@
\sa QAtomicInteger
*/
-/*! \fn template <typename T> QAtomicPointer<T>::QAtomicPointer(T *value)
+/*!
+ \fn template <typename T> QAtomicPointer<T>::QAtomicPointer(T *value)
Constructs a QAtomicPointer with the given \a value.
*/
-/*! \fn template <typename T> QAtomicPointer<T>::QAtomicPointer(const QAtomicPointer<T> &other)
+/*!
+ \fn template <typename T> QAtomicPointer<T>::QAtomicPointer(const QAtomicPointer<T> &other)
Constructs a copy of \a other.
*/
-/*! \fn template <typename T> QAtomicPointer &QAtomicPointer<T>::operator=(const QAtomicPointer &other)
+/*!
+ \fn template <typename T> QAtomicPointer &QAtomicPointer<T>::operator=(const QAtomicPointer &other)
Assigns \a other to this QAtomicPointer and returns a reference to
this QAtomicPointer.
@@ -1369,18 +1419,21 @@
\sa storeRelaxed(), loadRelaxed()
*/
-/*! \fn template <typename T> bool QAtomicPointer<T>::isTestAndSetNative()
+/*!
+ \fn template <typename T> bool QAtomicPointer<T>::isTestAndSetNative()
Returns \c true if test-and-set is implemented using atomic processor
instructions, false otherwise.
*/
-/*! \fn template <typename T> bool QAtomicPointer<T>::isTestAndSetWaitFree()
+/*!
+ \fn template <typename T> bool QAtomicPointer<T>::isTestAndSetWaitFree()
Returns \c true if atomic test-and-set is wait-free, false otherwise.
*/
-/*! \fn template <typename T> bool QAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
+/*!
+ \fn template <typename T> bool QAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
Atomic test-and-set.
@@ -1394,7 +1447,8 @@
processor to freely reorder memory accesses.
*/
-/*! \fn template <typename T> bool QAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
+/*!
+ \fn template <typename T> bool QAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
Atomic test-and-set.
@@ -1409,7 +1463,8 @@
be re-ordered before the atomic operation.
*/
-/*! \fn template <typename T> bool QAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
+/*!
+ \fn template <typename T> bool QAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
Atomic test-and-set.
@@ -1424,7 +1479,8 @@
re-ordered after the atomic operation.
*/
-/*! \fn template <typename T> bool QAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
+/*!
+ \fn template <typename T> bool QAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
Atomic test-and-set.
@@ -1439,19 +1495,22 @@
may not be re-ordered.
*/
-/*! \fn template <typename T> bool QAtomicPointer<T>::isFetchAndStoreNative()
+/*!
+ \fn template <typename T> bool QAtomicPointer<T>::isFetchAndStoreNative()
Returns \c true if fetch-and-store is implemented using atomic
processor instructions, false otherwise.
*/
-/*! \fn template <typename T> bool QAtomicPointer<T>::isFetchAndStoreWaitFree()
+/*!
+ \fn template <typename T> bool QAtomicPointer<T>::isFetchAndStoreWaitFree()
Returns \c true if atomic fetch-and-store is wait-free, false
otherwise.
*/
-/*! \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreRelaxed(T *newValue)
+/*!
+ \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreRelaxed(T *newValue)
Atomic fetch-and-store.
@@ -1463,7 +1522,8 @@
processor to freely reorder memory accesses.
*/
-/*! \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreAcquire(T *newValue)
+/*!
+ \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreAcquire(T *newValue)
Atomic fetch-and-store.
@@ -1476,7 +1536,8 @@
be re-ordered before the atomic operation.
*/
-/*! \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreRelease(T *newValue)
+/*!
+ \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreRelease(T *newValue)
Atomic fetch-and-store.
@@ -1489,7 +1550,8 @@
re-ordered after the atomic operation.
*/
-/*! \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreOrdered(T *newValue)
+/*!
+ \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreOrdered(T *newValue)
Atomic fetch-and-store.
@@ -1502,19 +1564,22 @@
may not be re-ordered.
*/
-/*! \fn template <typename T> bool QAtomicPointer<T>::isFetchAndAddNative()
+/*!
+ \fn template <typename T> bool QAtomicPointer<T>::isFetchAndAddNative()
Returns \c true if fetch-and-add is implemented using atomic
processor instructions, false otherwise.
*/
-/*! \fn template <typename T> bool QAtomicPointer<T>::isFetchAndAddWaitFree()
+/*!
+ \fn template <typename T> bool QAtomicPointer<T>::isFetchAndAddWaitFree()
Returns \c true if atomic fetch-and-add is wait-free, false
otherwise.
*/
-/*! \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddRelaxed(qptrdiff valueToAdd)
+/*!
+ \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddRelaxed(qptrdiff valueToAdd)
Atomic fetch-and-add.
@@ -1526,7 +1591,8 @@
processor to freely reorder memory accesses.
*/
-/*! \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddAcquire(qptrdiff valueToAdd)
+/*!
+ \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddAcquire(qptrdiff valueToAdd)
Atomic fetch-and-add.
@@ -1539,7 +1605,8 @@
be re-ordered before the atomic operation.
*/
-/*! \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddRelease(qptrdiff valueToAdd)
+/*!
+ \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddRelease(qptrdiff valueToAdd)
Atomic fetch-and-add.
@@ -1552,7 +1619,8 @@
re-ordered after the atomic operation.
*/
-/*! \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
+/*!
+ \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
Atomic fetch-and-add.
diff --git a/src/corelib/thread/qatomic.h b/src/corelib/thread/qatomic.h
index a3b9be0729..aa57ddc610 100644
--- a/src/corelib/thread/qatomic.h
+++ b/src/corelib/thread/qatomic.h
@@ -50,6 +50,10 @@ QT_BEGIN_NAMESPACE
QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Wextra")
+#ifdef Q_CLANG_QDOC
+# undef QT_BASIC_ATOMIC_HAS_CONSTRUCTORS
+#endif
+
// High-level atomic integer operations
template <typename T>
class QAtomicInteger : public QBasicAtomicInteger<T>
@@ -194,7 +198,9 @@ public:
#ifdef Q_QDOC
T *load() const;
T *loadAcquire() const;
+ T *loadRelaxed() const;
void store(T *newValue);
+ void storeRelaxed(T *newValue);
void storeRelease(T *newValue);
static Q_DECL_CONSTEXPR bool isTestAndSetNative();
diff --git a/src/corelib/thread/qbasicatomic.h b/src/corelib/thread/qbasicatomic.h
index 9804e60119..c9c95cf6ce 100644
--- a/src/corelib/thread/qbasicatomic.h
+++ b/src/corelib/thread/qbasicatomic.h
@@ -47,9 +47,9 @@
# include <QtCore/qatomic_bootstrap.h>
// If C++11 atomics are supported, use them!
-// Note that constexpr support is sometimes disabled in QNX builds but its
-// library has <atomic>.
-#elif defined(Q_COMPILER_ATOMICS) && (defined(Q_COMPILER_CONSTEXPR) || defined(Q_OS_QNX))
+// Note that constexpr support is sometimes disabled in QNX or INTEGRITY builds,
+// but their libraries have <atomic>.
+#elif defined(Q_COMPILER_ATOMICS) && (defined(Q_COMPILER_CONSTEXPR) || defined(Q_OS_QNX) || defined(Q_OS_INTEGRITY))
# include <QtCore/qatomic_cxx11.h>
// We only support one fallback: MSVC, because even on version 2015, it lacks full constexpr support
diff --git a/src/corelib/thread/qexception.cpp b/src/corelib/thread/qexception.cpp
index a3e30d5a7a..f9c63085b7 100644
--- a/src/corelib/thread/qexception.cpp
+++ b/src/corelib/thread/qexception.cpp
@@ -199,7 +199,7 @@ void ExceptionStore::setException(const QException &e)
bool ExceptionStore::hasException() const
{
- return (exceptionHolder.exception() != 0);
+ return (exceptionHolder.exception() != nullptr);
}
ExceptionHolder ExceptionStore::exception()
diff --git a/src/corelib/thread/qfutex_p.h b/src/corelib/thread/qfutex_p.h
index 7bec4554b7..f287b752d7 100644
--- a/src/corelib/thread/qfutex_p.h
+++ b/src/corelib/thread/qfutex_p.h
@@ -81,7 +81,7 @@ QT_END_NAMESPACE
// if not defined in linux/futex.h
# define FUTEX_PRIVATE_FLAG 128 // added in v2.6.22
-# if QT_HAS_FEATURE(thread_sanitizer) || defined(__SANITIZE_THREAD__)
+# if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
# include <sanitizer/tsan_interface.h>
inline void _q_tsan_acquire(void *addr, void *addr2)
{
@@ -98,7 +98,7 @@ inline void _q_tsan_release(void *addr, void *addr2)
# else
inline void _q_tsan_acquire(void *, void *) {}
inline void _q_tsan_release(void *, void *) {}
-# endif // QT_HAS_FEATURE(thread_sanitizer) || defined(__SANITIZE_THREAD__)
+# endif // __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
QT_BEGIN_NAMESPACE
namespace QtLinuxFutex {
diff --git a/src/corelib/thread/qfutureinterface.cpp b/src/corelib/thread/qfutureinterface.cpp
index 1303be10b1..e6380a8732 100644
--- a/src/corelib/thread/qfutureinterface.cpp
+++ b/src/corelib/thread/qfutureinterface.cpp
@@ -466,7 +466,7 @@ bool QFutureInterfaceBase::derefT() const
QFutureInterfaceBasePrivate::QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState)
: refCount(1), m_progressValue(0), m_progressMinimum(0), m_progressMaximum(0),
state(initialState),
- manualProgress(false), m_expectedResultCount(0), runnable(0), m_pool(0)
+ manualProgress(false), m_expectedResultCount(0), runnable(nullptr), m_pool(nullptr)
{
progressTime.invalidate();
}
diff --git a/src/corelib/thread/qmutex.cpp b/src/corelib/thread/qmutex.cpp
index 9e52f286ee..f3883278e3 100644
--- a/src/corelib/thread/qmutex.cpp
+++ b/src/corelib/thread/qmutex.cpp
@@ -70,7 +70,7 @@ class QRecursiveMutexPrivate : public QMutexData
{
public:
QRecursiveMutexPrivate()
- : QMutexData(QMutex::Recursive), owner(0), count(0) {}
+ : QMutexData(QMutex::Recursive), owner(nullptr), count(0) {}
// written to by the thread that first owns 'mutex';
// read during attempts to acquire ownership of 'mutex' from any other thread:
@@ -186,7 +186,7 @@ public:
*/
QMutex::QMutex(RecursionMode mode)
{
- d_ptr.storeRelaxed(mode == Recursive ? new QRecursiveMutexPrivate : 0);
+ d_ptr.storeRelaxed(mode == Recursive ? new QRecursiveMutexPrivate : nullptr);
}
/*!
@@ -799,7 +799,7 @@ inline void QRecursiveMutexPrivate::unlock() noexcept
if (count > 0) {
count--;
} else {
- owner.storeRelaxed(0);
+ owner.storeRelaxed(nullptr);
mutex.QBasicMutex::unlock();
}
}
diff --git a/src/corelib/thread/qmutex.h b/src/corelib/thread/qmutex.h
index c693ff65d8..93c4bf23e8 100644
--- a/src/corelib/thread/qmutex.h
+++ b/src/corelib/thread/qmutex.h
@@ -44,7 +44,7 @@
#include <QtCore/qatomic.h>
#include <new>
-#if QT_HAS_INCLUDE(<chrono>)
+#if __has_include(<chrono>)
# include <chrono>
# include <limits>
#endif
@@ -147,7 +147,7 @@ public:
// Lockable concept
bool try_lock() QT_MUTEX_LOCK_NOEXCEPT { return tryLock(); }
-#if QT_HAS_INCLUDE(<chrono>) || defined(Q_CLANG_QDOC)
+#if __has_include(<chrono>) || defined(Q_CLANG_QDOC)
// TimedLockable concept
template <class Rep, class Period>
bool try_lock_for(std::chrono::duration<Rep, Period> duration)
@@ -175,7 +175,7 @@ private:
friend class QRecursiveMutex;
friend class ::tst_QMutex;
-#if QT_HAS_INCLUDE(<chrono>)
+#if __has_include(<chrono>)
template<class Rep, class Period>
static int convertToMilliseconds(std::chrono::duration<Rep, Period> duration)
{
@@ -213,7 +213,7 @@ public:
using QMutex::tryLock;
using QMutex::unlock;
using QMutex::try_lock;
-#if QT_HAS_INCLUDE(<chrono>)
+#if __has_include(<chrono>)
using QMutex::try_lock_for;
using QMutex::try_lock_until;
#endif
@@ -295,7 +295,7 @@ public:
inline void unlock() noexcept {}
inline bool isRecursive() const noexcept { return true; }
-#if QT_HAS_INCLUDE(<chrono>)
+#if __has_include(<chrono>)
template <class Rep, class Period>
inline bool try_lock_for(std::chrono::duration<Rep, Period> duration) noexcept
{
diff --git a/src/corelib/thread/qmutex_linux.cpp b/src/corelib/thread/qmutex_linux.cpp
index 3270875471..72002838cf 100644
--- a/src/corelib/thread/qmutex_linux.cpp
+++ b/src/corelib/thread/qmutex_linux.cpp
@@ -106,7 +106,7 @@ static inline QMutexData *dummyFutexValue()
}
template <bool IsTimed> static inline
-bool lockInternal_helper(QBasicAtomicPointer<QMutexData> &d_ptr, int timeout = -1, QElapsedTimer *elapsedTimer = 0) noexcept
+bool lockInternal_helper(QBasicAtomicPointer<QMutexData> &d_ptr, int timeout = -1, QElapsedTimer *elapsedTimer = nullptr) noexcept
{
if (!IsTimed)
timeout = -1;
@@ -175,7 +175,7 @@ void QBasicMutex::unlockInternal() noexcept
Q_UNUSED(d);
Q_ASSERT(!isRecursive());
- d_ptr.storeRelease(0);
+ d_ptr.storeRelease(nullptr);
futexWakeOne(d_ptr);
}
diff --git a/src/corelib/thread/qorderedmutexlocker_p.h b/src/corelib/thread/qorderedmutexlocker_p.h
index 570c526225..83edfd5879 100644
--- a/src/corelib/thread/qorderedmutexlocker_p.h
+++ b/src/corelib/thread/qorderedmutexlocker_p.h
@@ -69,7 +69,7 @@ class QOrderedMutexLocker
public:
QOrderedMutexLocker(QBasicMutex *m1, QBasicMutex *m2)
: mtx1((m1 == m2) ? m1 : (std::less<QBasicMutex *>()(m1, m2) ? m1 : m2)),
- mtx2((m1 == m2) ? 0 : (std::less<QBasicMutex *>()(m1, m2) ? m2 : m1)),
+ mtx2((m1 == m2) ? nullptr : (std::less<QBasicMutex *>()(m1, m2) ? m2 : m1)),
locked(false)
{
relock();
diff --git a/src/corelib/thread/qreadwritelock.cpp b/src/corelib/thread/qreadwritelock.cpp
index 14654986a0..8c28507d5a 100644
--- a/src/corelib/thread/qreadwritelock.cpp
+++ b/src/corelib/thread/qreadwritelock.cpp
@@ -227,7 +227,7 @@ bool QReadWriteLock::tryLockForRead(int timeout)
return true;
while (true) {
- if (d == 0) {
+ if (d == nullptr) {
if (!d_ptr.testAndSetAcquire(nullptr, dummyLockedForRead, d))
continue;
return true;
@@ -341,7 +341,7 @@ bool QReadWriteLock::tryLockForWrite(int timeout)
return true;
while (true) {
- if (d == 0) {
+ if (d == nullptr) {
if (!d_ptr.testAndSetAcquire(d, dummyLockedForWrite, d))
continue;
return true;
@@ -477,7 +477,7 @@ bool QReadWriteLockPrivate::lockForRead(int timeout)
if (elapsed > timeout)
return false;
waitingReaders++;
- readerCond.wait(&mutex, timeout - elapsed);
+ readerCond.wait(&mutex, QDeadlineTimer(timeout - elapsed));
} else {
waitingReaders++;
readerCond.wait(&mutex);
@@ -511,7 +511,7 @@ bool QReadWriteLockPrivate::lockForWrite(int timeout)
return false;
}
waitingWriters++;
- writerCond.wait(&mutex, timeout - elapsed);
+ writerCond.wait(&mutex, QDeadlineTimer(timeout - elapsed));
} else {
waitingWriters++;
writerCond.wait(&mutex);
@@ -581,7 +581,7 @@ void QReadWriteLockPrivate::recursiveUnlock()
if (self == currentWriter) {
if (--writerCount > 0)
return;
- currentWriter = 0;
+ currentWriter = nullptr;
} else {
auto it = currentReaders.find(self);
if (it == currentReaders.end()) {
diff --git a/src/corelib/thread/qresultstore.cpp b/src/corelib/thread/qresultstore.cpp
index 1b3bc20eca..0b82b938e1 100644
--- a/src/corelib/thread/qresultstore.cpp
+++ b/src/corelib/thread/qresultstore.cpp
@@ -192,7 +192,7 @@ int ResultStoreBase::addResults(int index, const void *results, int vectorSize,
ResultItem filteredIn(results, vectorSize);
insertResultItem(index, filteredIn);
}
- ResultItem filteredAway(0, totalCount - vectorSize);
+ ResultItem filteredAway(nullptr, totalCount - vectorSize);
return insertResultItem(index + vectorSize, filteredAway);
}
}
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp
index 6a59043881..1437768f1c 100644
--- a/src/corelib/thread/qthread.cpp
+++ b/src/corelib/thread/qthread.cpp
@@ -49,6 +49,8 @@
#include "qthread_p.h"
#include "private/qcoreapplication_p.h"
+#include <limits>
+
QT_BEGIN_NAMESPACE
/*
@@ -57,7 +59,7 @@ QT_BEGIN_NAMESPACE
QThreadData::QThreadData(int initialRefCount)
: _ref(initialRefCount), loopLevel(0), scopeLevel(0),
- eventDispatcher(0),
+ eventDispatcher(nullptr),
quitNow(false), canWait(true), isAdopted(false), requiresCoreApplication(true)
{
// fprintf(stderr, "QThreadData %p created\n", this);
@@ -397,7 +399,7 @@ QThreadPrivate::~QThreadPrivate()
QThread *QThread::currentThread()
{
QThreadData *data = QThreadData::current();
- Q_ASSERT(data != 0);
+ Q_ASSERT(data != nullptr);
return data->thread.loadAcquire();
}
@@ -449,7 +451,7 @@ QThread::~QThread()
if (d->running && !d->finished && !d->data->isAdopted)
qFatal("QThread: Destroyed while thread is still running");
- d->data->thread = 0;
+ d->data->thread = nullptr;
}
}
@@ -726,7 +728,8 @@ QThread::Priority QThread::priority() const
*/
/*!
- \fn bool QThread::wait(unsigned long time)
+ \fn bool QThread::wait(QDeadlineTimer deadline)
+ \since 5.15
Blocks the thread until either of these conditions is met:
@@ -735,12 +738,14 @@ QThread::Priority QThread::priority() const
execution (i.e. when it returns from \l{run()}). This function
will return true if the thread has finished. It also returns
true if the thread has not been started yet.
- \li \a time milliseconds has elapsed. If \a time is ULONG_MAX (the
- default), then the wait will never timeout (the thread must
- return from \l{run()}). This function will return false if the
- wait timed out.
+ \li The \a deadline is reached. This function will return false if the
+ deadline is reached.
\endlist
+ A deadline timer set to \c QDeadlineTimer::Forever (the default) will never
+ time out: in this case, the function only returns when the thread returns
+ from \l{run()} or if the thread has not yet started.
+
This provides similar functionality to the POSIX \c
pthread_join() function.
@@ -833,9 +838,9 @@ void QThread::exit(int returnCode)
}
}
-bool QThread::wait(unsigned long time)
+bool QThread::wait(QDeadlineTimer deadline)
{
- Q_UNUSED(time);
+ Q_UNUSED(deadline);
return false;
}
@@ -966,6 +971,17 @@ void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
}
}
+/*!
+ \fn bool QThread::wait(unsigned long time)
+ \overload
+*/
+bool QThread::wait(unsigned long time)
+{
+ if (time == std::numeric_limits<unsigned long>::max())
+ return wait(QDeadlineTimer(QDeadlineTimer::Forever));
+ return wait(QDeadlineTimer(time));
+}
+
#if QT_CONFIG(thread)
/*!
diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h
index 8141f945b6..635dd94522 100644
--- a/src/corelib/thread/qthread.h
+++ b/src/corelib/thread/qthread.h
@@ -42,6 +42,7 @@
#define QTHREAD_H
#include <QtCore/qobject.h>
+#include <QtCore/qdeadlinetimer.h>
// For QThread::create. The configure-time test just checks for the availability
// of std::future and std::async; for the C++17 codepath we perform some extra
@@ -57,8 +58,6 @@
# endif
#endif
-#include <limits.h>
-
QT_BEGIN_NAMESPACE
@@ -135,8 +134,9 @@ public Q_SLOTS:
void quit();
public:
- // default argument causes thread to block indefinetely
- bool wait(unsigned long time = ULONG_MAX);
+ bool wait(QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever));
+ // ### Qt6 inline this function
+ bool wait(unsigned long time);
static void sleep(unsigned long);
static void msleep(unsigned long);
diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp
index 21abe372eb..727d72a334 100644
--- a/src/corelib/thread/qthread_unix.cpp
+++ b/src/corelib/thread/qthread_unix.cpp
@@ -109,7 +109,7 @@ Q_STATIC_ASSERT(sizeof(pthread_t) <= sizeof(Qt::HANDLE));
enum { ThreadPriorityResetFlag = 0x80000000 };
-static thread_local QThreadData *currentThreadData = 0;
+static thread_local QThreadData *currentThreadData = nullptr;
static pthread_once_t current_thread_data_once = PTHREAD_ONCE_INIT;
static pthread_key_t current_thread_data_key;
@@ -144,7 +144,7 @@ static void destroy_current_thread_data(void *p)
#if defined(Q_OS_VXWORKS)
(void *)1);
#else
- 0);
+ nullptr);
#endif
}
@@ -182,8 +182,8 @@ static void set_thread_data(QThreadData *data)
static void clear_thread_data()
{
- currentThreadData = 0;
- pthread_setspecific(current_thread_data_key, 0);
+ currentThreadData = nullptr;
+ pthread_setspecific(current_thread_data_key, nullptr);
}
template <typename T>
@@ -226,7 +226,7 @@ QThreadData *QThreadData::current(bool createIfNecessary)
} QT_CATCH(...) {
clear_thread_data();
data->deref();
- data = 0;
+ data = nullptr;
QT_RETHROW;
}
data->deref();
@@ -294,7 +294,7 @@ static void setCurrentThreadName(const char *name)
void *QThreadPrivate::start(void *arg)
{
#if !defined(Q_OS_ANDROID)
- pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, nullptr);
#endif
pthread_cleanup_push(QThreadPrivate::finish, arg);
@@ -336,7 +336,7 @@ void *QThreadPrivate::start(void *arg)
emit thr->started(QThread::QPrivateSignal());
#if !defined(Q_OS_ANDROID)
- pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, nullptr);
pthread_testcancel();
#endif
thr->run();
@@ -360,7 +360,7 @@ void *QThreadPrivate::start(void *arg)
// thrown.
pthread_cleanup_pop(1);
- return 0;
+ return nullptr;
}
void QThreadPrivate::finish(void *arg)
@@ -379,13 +379,13 @@ void QThreadPrivate::finish(void *arg)
void *data = &d->data->tls;
locker.unlock();
emit thr->finished(QThread::QPrivateSignal());
- QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
+ QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
QThreadStorageData::finish((void **)data);
locker.relock();
QAbstractEventDispatcher *eventDispatcher = d->data->eventDispatcher.loadRelaxed();
if (eventDispatcher) {
- d->data->eventDispatcher = 0;
+ d->data->eventDispatcher = nullptr;
locker.unlock();
eventDispatcher->closingDown();
delete eventDispatcher;
@@ -771,7 +771,7 @@ void QThread::terminate()
#endif
}
-bool QThread::wait(unsigned long time)
+bool QThread::wait(QDeadlineTimer deadline)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
@@ -785,7 +785,7 @@ bool QThread::wait(unsigned long time)
return true;
while (d->running) {
- if (!d->thread_done.wait(locker.mutex(), time))
+ if (!d->thread_done.wait(locker.mutex(), deadline))
return false;
}
return true;
@@ -794,14 +794,14 @@ bool QThread::wait(unsigned long time)
void QThread::setTerminationEnabled(bool enabled)
{
QThread *thr = currentThread();
- Q_ASSERT_X(thr != 0, "QThread::setTerminationEnabled()",
+ Q_ASSERT_X(thr != nullptr, "QThread::setTerminationEnabled()",
"Current thread was not started with QThread.");
Q_UNUSED(thr)
#if defined(Q_OS_ANDROID)
Q_UNUSED(enabled);
#else
- pthread_setcancelstate(enabled ? PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE, NULL);
+ pthread_setcancelstate(enabled ? PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE, nullptr);
if (enabled)
pthread_testcancel();
#endif
diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp
index 996bcf0a71..44cb5653bf 100644
--- a/src/corelib/thread/qthread_win.cpp
+++ b/src/corelib/thread/qthread_win.cpp
@@ -610,7 +610,7 @@ void QThread::terminate()
QThreadPrivate::finish(this, false);
}
-bool QThread::wait(unsigned long time)
+bool QThread::wait(QDeadlineTimer deadline)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
@@ -627,9 +627,9 @@ bool QThread::wait(unsigned long time)
bool ret = false;
#ifndef Q_OS_WINRT
- switch (WaitForSingleObject(d->handle, time)) {
+ switch (WaitForSingleObject(d->handle, deadline.remainingTime())) {
#else
- switch (WaitForSingleObjectEx(d->handle, time, false)) {
+ switch (WaitForSingleObjectEx(d->handle, deadline.remainingTime(), false)) {
#endif
case WAIT_OBJECT_0:
ret = true;
diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp
index 4d2389f699..5f23a78c8a 100644
--- a/src/corelib/thread/qthreadpool.cpp
+++ b/src/corelib/thread/qthreadpool.cpp
@@ -136,7 +136,7 @@ void QThreadPoolThread::run()
manager->waitingThreads.enqueue(this);
registerThreadInactive();
// wait for work, exiting after the expiry timeout is reached
- runnableReady.wait(locker.mutex(), manager->expiryTimeout);
+ runnableReady.wait(locker.mutex(), QDeadlineTimer(manager->expiryTimeout));
++manager->activeThreads;
if (manager->waitingThreads.removeOne(this))
expired = true;
diff --git a/src/corelib/thread/qthreadstorage.cpp b/src/corelib/thread/qthreadstorage.cpp
index fdc484d2d2..464559ffa5 100644
--- a/src/corelib/thread/qthreadstorage.cpp
+++ b/src/corelib/thread/qthreadstorage.cpp
@@ -116,7 +116,7 @@ void **QThreadStorageData::get() const
QThreadData *data = QThreadData::current();
if (!data) {
qWarning("QThreadStorage::get: QThreadStorage can only be used with threads started with QThread");
- return 0;
+ return nullptr;
}
QVector<void *> &tls = data->tls;
if (tls.size() <= id)
@@ -128,7 +128,7 @@ void **QThreadStorageData::get() const
*v,
data->thread.loadRelaxed());
- return *v ? v : 0;
+ return *v ? v : nullptr;
}
void **QThreadStorageData::set(void *p)
@@ -136,7 +136,7 @@ void **QThreadStorageData::set(void *p)
QThreadData *data = QThreadData::current();
if (!data) {
qWarning("QThreadStorage::set: QThreadStorage can only be used with threads started with QThread");
- return 0;
+ return nullptr;
}
QVector<void *> &tls = data->tls;
if (tls.size() <= id)
@@ -144,7 +144,7 @@ void **QThreadStorageData::set(void *p)
void *&value = tls[id];
// delete any previous data
- if (value != 0) {
+ if (value != nullptr) {
DEBUG_MSG("QThreadStorageData: Deleting previous storage %d, data %p, for thread %p",
id,
value,
@@ -156,7 +156,7 @@ void **QThreadStorageData::set(void *p)
locker.unlock();
void *q = value;
- value = 0;
+ value = nullptr;
if (destructor)
destructor(q);
@@ -178,7 +178,7 @@ void QThreadStorageData::finish(void **p)
while (!tls->isEmpty()) {
void *&value = tls->last();
void *q = value;
- value = 0;
+ value = nullptr;
int i = tls->size() - 1;
tls->resize(i);
diff --git a/src/corelib/thread/qwaitcondition.h b/src/corelib/thread/qwaitcondition.h
index 11520e4cfe..0a47ac3717 100644
--- a/src/corelib/thread/qwaitcondition.h
+++ b/src/corelib/thread/qwaitcondition.h
@@ -40,15 +40,12 @@
#ifndef QWAITCONDITION_H
#define QWAITCONDITION_H
-#include <QtCore/qglobal.h>
-
-#include <limits.h>
+#include <QtCore/QDeadlineTimer>
QT_BEGIN_NAMESPACE
#if QT_CONFIG(thread)
-class QDeadlineTimer;
class QWaitConditionPrivate;
class QMutex;
class QReadWriteLock;
@@ -59,11 +56,13 @@ public:
QWaitCondition();
~QWaitCondition();
- // ### Qt 6: remove unsigned long overloads
- bool wait(QMutex *lockedMutex, unsigned long time = ULONG_MAX);
- bool wait(QMutex *lockedMutex, QDeadlineTimer deadline);
- bool wait(QReadWriteLock *lockedReadWriteLock, unsigned long time = ULONG_MAX);
- bool wait(QReadWriteLock *lockedReadWriteLock, QDeadlineTimer deadline);
+ bool wait(QMutex *lockedMutex,
+ QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever));
+ bool wait(QMutex *lockedMutex, unsigned long time);
+
+ bool wait(QReadWriteLock *lockedReadWriteLock,
+ QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever));
+ bool wait(QReadWriteLock *lockedReadWriteLock, unsigned long time);
void wakeOne();
void wakeAll();
@@ -80,21 +79,26 @@ private:
#else
class QMutex;
+class QReadWriteLock;
+
class Q_CORE_EXPORT QWaitCondition
{
public:
QWaitCondition() {}
~QWaitCondition() {}
- bool wait(QMutex *mutex, unsigned long time = ULONG_MAX)
- {
- Q_UNUSED(mutex);
- Q_UNUSED(time);
- return true;
- }
+ bool wait(QMutex *, QDeadlineTimer = QDeadlineTimer(QDeadlineTimer::Forever))
+ { return true; }
+ bool wait(QReadWriteLock *, QDeadlineTimer = QDeadlineTimer(QDeadlineTimer::Forever))
+ { return true; }
+ bool wait(QMutex *, unsigned long) { return true; }
+ bool wait(QReadWriteLock *, unsigned long) { return true; }
void wakeOne() {}
void wakeAll() {}
+
+ void notify_one() { wakeOne(); }
+ void notify_all() { wakeAll(); }
};
#endif // QT_CONFIG(thread)
diff --git a/src/corelib/thread/qwaitcondition.qdoc b/src/corelib/thread/qwaitcondition.qdoc
index eebc28f059..014d549477 100644
--- a/src/corelib/thread/qwaitcondition.qdoc
+++ b/src/corelib/thread/qwaitcondition.qdoc
@@ -121,8 +121,18 @@
/*!
\fn bool QWaitCondition::wait(QMutex *lockedMutex, unsigned long time)
+ \overload
+*/
+/*!
+ \fn bool QWaitCondition::wait(QReadWriteLock *lockedReadWriteLock, unsigned long time)
+ \overload
+*/
- Releases the \a lockedMutex and waits on the wait condition. The
+/*!
+ \fn bool QWaitCondition::wait(QMutex *lockedMutex, QDeadlineTimer deadline)
+ \since 5.12
+
+ Releases the \a lockedMutex and waits on the wait condition. The
\a lockedMutex must be initially locked by the calling thread. If \a
lockedMutex is not in a locked state, the behavior is undefined. If
\a lockedMutex is a recursive mutex, this function
@@ -132,10 +142,10 @@
\list
\li Another thread signals it using wakeOne() or wakeAll(). This
function will return true in this case.
- \li \a time milliseconds has elapsed. If \a time is \c ULONG_MAX
- (the default), then the wait will never timeout (the event
- must be signalled). This function will return false if the
- wait timed out.
+ \li the deadline given by \a deadline is reached. If \a deadline is
+ \c QDeadlineTimer::Forever (the default), then the wait will never
+ timeout (the event must be signalled). This function will return
+ false if the wait timed out.
\endlist
The \a lockedMutex will be returned to the same locked state. This
@@ -146,8 +156,8 @@
*/
/*!
- \fn bool QWaitCondition::wait(QReadWriteLock *lockedReadWriteLock, unsigned long time)
- \since 4.4
+ \fn bool QWaitCondition::wait(QReadWriteLock *lockedReadWriteLock, QDeadlineTimer deadline)
+ \since 5.12
Releases the \a lockedReadWriteLock and waits on the wait
condition. The \a lockedReadWriteLock must be initially locked by the
@@ -160,10 +170,10 @@
\list
\li Another thread signals it using wakeOne() or wakeAll(). This
function will return true in this case.
- \li \a time milliseconds has elapsed. If \a time is \c ULONG_MAX
- (the default), then the wait will never timeout (the event
- must be signalled). This function will return false if the
- wait timed out.
+ \li the deadline given by \a deadline is reached. If \a deadline is
+ \c QDeadlineTimer::Forever (the default), then the wait will never
+ timeout (the event must be signalled). This function will return
+ false if the wait timed out.
\endlist
The \a lockedReadWriteLock will be returned to the same locked
diff --git a/src/corelib/thread/qwaitcondition_unix.cpp b/src/corelib/thread/qwaitcondition_unix.cpp
index dd7475cec5..88b058f410 100644
--- a/src/corelib/thread/qwaitcondition_unix.cpp
+++ b/src/corelib/thread/qwaitcondition_unix.cpp
@@ -173,7 +173,7 @@ public:
QWaitCondition::QWaitCondition()
{
d = new QWaitConditionPrivate;
- report_error(pthread_mutex_init(&d->mutex, NULL), "QWaitCondition", "mutex init");
+ report_error(pthread_mutex_init(&d->mutex, nullptr), "QWaitCondition", "mutex init");
qt_initialize_pthread_cond(&d->cond, "QWaitCondition");
d->waiters = d->wakeups = 0;
}
diff --git a/src/corelib/time/qcalendar.cpp b/src/corelib/time/qcalendar.cpp
index d308aeba2b..9d485f181e 100644
--- a/src/corelib/time/qcalendar.cpp
+++ b/src/corelib/time/qcalendar.cpp
@@ -100,7 +100,7 @@ struct Registry {
if (id == QCalendar::System::User) {
byId.push_back(calendar);
} else {
- Q_ASSERT(byId.at(size_t(id)) == nullptr);
+ Q_ASSERT(byId[size_t(id)] == nullptr);
byId[size_t(id)] = calendar;
}
if (id == QCalendar::System::Gregorian) {
@@ -618,7 +618,7 @@ const QCalendarBackend *QCalendarBackend::fromEnum(QCalendar::System system)
if (calendarRegistry.isDestroyed() || system == QCalendar::System::User)
return nullptr;
Q_ASSERT(calendarRegistry->byId.size() >= size_t(system));
- if (auto *c = calendarRegistry->byId.at(size_t(system)))
+ if (auto *c = calendarRegistry->byId[size_t(system)])
return c;
switch (system) {
case QCalendar::System::Gregorian:
@@ -723,7 +723,7 @@ QCalendar::QCalendar(QLatin1String name)
QCalendar::QCalendar(QStringView name)
: d(QCalendarBackend::fromName(name)) {}
-/*
+/*!
\fn bool QCalendar::isValid() const
Returns true if this is a valid calendar object.
diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp
index 95a7255a04..67d37f19d8 100644
--- a/src/corelib/time/qdatetime.cpp
+++ b/src/corelib/time/qdatetime.cpp
@@ -127,7 +127,7 @@ static const char qt_shortMonthNames[][4] = {
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
-static int qt_monthNumberFromShortName(QStringRef shortName)
+static int qt_monthNumberFromShortName(QStringView shortName)
{
for (unsigned int i = 0; i < sizeof(qt_shortMonthNames) / sizeof(qt_shortMonthNames[0]); ++i) {
if (shortName == QLatin1String(qt_shortMonthNames[i], 3))
@@ -136,9 +136,9 @@ static int qt_monthNumberFromShortName(QStringRef shortName)
return -1;
}
static int qt_monthNumberFromShortName(const QString &shortName)
-{ return qt_monthNumberFromShortName(QStringRef(&shortName)); }
+{ return qt_monthNumberFromShortName(QStringView(shortName)); }
-static int fromShortMonthName(const QStringRef &monthName, int year)
+static int fromShortMonthName(QStringView monthName, int year)
{
// Assume that English monthnames are the default
int month = qt_monthNumberFromShortName(monthName);
@@ -164,8 +164,8 @@ static ParsedRfcDateTime rfcDateImpl(const QString &s)
{
ParsedRfcDateTime result;
- // Matches "Wdy, dd Mon yyyy HH:mm:ss ±hhmm" (Wdy, being optional)
- QRegExp rex(QStringLiteral("^(?:[A-Z][a-z]+,)?[ \\t]*(\\d{1,2})[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d)(?::(\\d\\d))?)?[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?"));
+ // Matches "[ddd,] dd MMM yyyy[ hh:mm[:ss]] [±hhmm]" - correct RFC 822, 2822, 5322 format
+ QRegExp rex(QStringLiteral("^[ \\t]*(?:[A-Z][a-z]+,)?[ \\t]*(\\d{1,2})[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d)(?::(\\d\\d))?)?[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?"));
if (s.indexOf(rex) == 0) {
const QStringList cap = rex.capturedTexts();
result.date = QDate(cap[3].toInt(), qt_monthNumberFromShortName(cap[2]), cap[1].toInt());
@@ -176,8 +176,8 @@ static ParsedRfcDateTime rfcDateImpl(const QString &s)
const int minOffset = cap[9].toInt();
result.utcOffset = ((hourOffset * 60 + minOffset) * (positiveOffset ? 60 : -60));
} else {
- // Matches "Wdy Mon dd HH:mm:ss yyyy"
- QRegExp rex(QStringLiteral("^[A-Z][a-z]+[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d):(\\d\\d))?[ \\t]+(\\d\\d\\d\\d)[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?"));
+ // Matches "ddd MMM dd[ hh:mm:ss] yyyy [±hhmm]" - permissive RFC 850, 1036 (read only)
+ QRegExp rex(QStringLiteral("^[ \\t]*[A-Z][a-z]+[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d):(\\d\\d))?[ \\t]+(\\d\\d\\d\\d)[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?"));
if (s.indexOf(rex) == 0) {
const QStringList cap = rex.capturedTexts();
result.date = QDate(cap[6].toInt(), qt_monthNumberFromShortName(cap[1]), cap[2].toInt());
@@ -207,7 +207,7 @@ static QString toOffsetString(Qt::DateFormat format, int offset)
#if QT_CONFIG(datestring)
// Parse offset in [+-]HH[[:]mm] format
-static int fromOffsetString(const QStringRef &offsetString, bool *valid) noexcept
+static int fromOffsetString(QStringView offsetString, bool *valid) noexcept
{
*valid = false;
@@ -228,22 +228,23 @@ static int fromOffsetString(const QStringRef &offsetString, bool *valid) noexcep
return 0;
// Split the hour and minute parts
- const QStringRef time = offsetString.mid(1);
- int hhLen = time.indexOf(QLatin1Char(':'));
- int mmIndex;
+ const QStringView time = offsetString.mid(1);
+ qsizetype hhLen = time.indexOf(QLatin1Char(':'));
+ qsizetype mmIndex;
if (hhLen == -1)
mmIndex = hhLen = 2; // [+-]HHmm or [+-]HH format
else
mmIndex = hhLen + 1;
- const QStringRef hhRef = time.left(hhLen);
+ const QLocale C = QLocale::c();
+ const QStringView hhRef = time.left(qMin(hhLen, time.size()));
bool ok = false;
- const int hour = hhRef.toInt(&ok);
+ const int hour = C.toInt(hhRef, &ok);
if (!ok)
return 0;
- const QStringRef mmRef = time.mid(mmIndex);
- const int minute = mmRef.isEmpty() ? 0 : mmRef.toInt(&ok);
+ const QStringView mmRef = time.mid(qMin(mmIndex, time.size()));
+ const int minute = mmRef.isEmpty() ? 0 : C.toInt(mmRef, &ok);
if (!ok || minute < 0 || minute > 59)
return 0;
@@ -1213,25 +1214,25 @@ QString QDate::toString(Qt::DateFormat format) const
\table
\header \li Expression \li Output
- \row \li d \li the day as number without a leading zero (1 to 31)
- \row \li dd \li the day as number with a leading zero (01 to 31)
+ \row \li d \li The day as a number without a leading zero (1 to 31)
+ \row \li dd \li The day as a number with a leading zero (01 to 31)
\row \li ddd
- \li the abbreviated localized day name (e.g. 'Mon' to 'Sun').
+ \li The abbreviated localized day name (e.g. 'Mon' to 'Sun').
Uses the system locale to localize the name, i.e. QLocale::system().
\row \li dddd
- \li the long localized day name (e.g. 'Monday' to 'Sunday').
+ \li The long localized day name (e.g. 'Monday' to 'Sunday').
Uses the system locale to localize the name, i.e. QLocale::system().
- \row \li M \li the month as number without a leading zero (1 to 12)
- \row \li MM \li the month as number with a leading zero (01 to 12)
+ \row \li M \li The month as a number without a leading zero (1 to 12)
+ \row \li MM \li The month as a number with a leading zero (01 to 12)
\row \li MMM
- \li the abbreviated localized month name (e.g. 'Jan' to 'Dec').
+ \li The abbreviated localized month name (e.g. 'Jan' to 'Dec').
Uses the system locale to localize the name, i.e. QLocale::system().
\row \li MMMM
- \li the long localized month name (e.g. 'January' to 'December').
+ \li The long localized month name (e.g. 'January' to 'December').
Uses the system locale to localize the name, i.e. QLocale::system().
- \row \li yy \li the year as two digit number (00 to 99)
- \row \li yyyy \li the year as four digit number. If the year is negative,
- a minus sign is prepended in addition.
+ \row \li yy \li The year as a two digit number (00 to 99)
+ \row \li yyyy \li The year as a four digit number. If the year is negative,
+ a minus sign is prepended, making five characters.
\endtable
Any sequence of characters enclosed in single quotes will be included
@@ -1625,6 +1626,29 @@ qint64 QDate::daysTo(const QDate &d) const
*/
#if QT_CONFIG(datestring)
+namespace {
+
+struct ParsedInt { int value = 0; bool ok = false; };
+
+/*
+ /internal
+
+ Read an int that must be the whole text. QStringRef::toInt() will ignore
+ spaces happily; but ISO date format should not.
+*/
+ParsedInt readInt(QStringView text)
+{
+ ParsedInt result;
+ for (const auto &ch : text) {
+ if (ch.isSpace())
+ return result;
+ }
+ result.value = QLocale::c().toInt(text, &result.ok);
+ return result;
+}
+
+}
+
/*!
Returns the QDate represented by the \a string, using the
\a format given, or an invalid date if the string cannot be
@@ -1676,17 +1700,18 @@ QDate QDate::fromString(const QString &string, Qt::DateFormat format)
return QDate(year, month, day);
}
#endif // textdate
- case Qt::ISODate: {
- // Semi-strict parsing, must be long enough and have non-numeric separators
- if (string.size() < 10 || string.at(4).isDigit() || string.at(7).isDigit()
- || (string.size() > 10 && string.at(10).isDigit())) {
- return QDate();
- }
- const int year = string.midRef(0, 4).toInt();
- if (year <= 0 || year > 9999)
- return QDate();
- return QDate(year, string.midRef(5, 2).toInt(), string.midRef(8, 2).toInt());
+ case Qt::ISODate:
+ // Semi-strict parsing, must be long enough and have punctuators as separators
+ if (string.size() >= 10 && string.at(4).isPunct() && string.at(7).isPunct()
+ && (string.size() == 10 || !string.at(10).isDigit())) {
+ QStringView view(string);
+ const ParsedInt year = readInt(view.mid(0, 4));
+ const ParsedInt month = readInt(view.mid(5, 2));
+ const ParsedInt day = readInt(view.mid(8, 2));
+ if (year.ok && year.value > 0 && year.value <= 9999 && month.ok && day.ok)
+ return QDate(year.value, month.value, day.value);
}
+ break;
}
return QDate();
}
@@ -1719,11 +1744,15 @@ QDate QDate::fromString(const QString &string, Qt::DateFormat format)
\row \li MMMM
\li The long localized month name (e.g. 'January' to 'December').
Uses the system locale to localize the name, i.e. QLocale::system().
- \row \li yy \li The year as two digit number (00 to 99)
- \row \li yyyy \li The year as four digit number. If the year is negative,
- a minus sign is prepended in addition.
+ \row \li yy \li The year as a two digit number (00 to 99)
+ \row \li yyyy \li The year as a four digit number, possibly plus a leading
+ minus sign for negative years.
\endtable
+ \note Unlike the other version of this function, day and month names must
+ be given in the user's local language. It is only possible to use the English
+ names if the user's language is English.
+
All other input characters will be treated as text. Any sequence
of characters that are enclosed in single quotes will also be
treated as text and will not be used as an expression. For example:
@@ -1765,7 +1794,7 @@ QDate QDate::fromString(const QString &string, const QString &format, QCalendar
QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString, cal);
// dt.setDefaultLocale(QLocale::c()); ### Qt 6
if (dt.parseFormat(format))
- dt.fromString(string, &date, 0);
+ dt.fromString(string, &date, nullptr);
#else
Q_UNUSED(string);
Q_UNUSED(format);
@@ -2063,30 +2092,30 @@ QString QTime::toString(Qt::DateFormat format) const
\table
\header \li Expression \li Output
\row \li h
- \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
+ \li The hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
\row \li hh
- \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
+ \li The hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
\row \li H
- \li the hour without a leading zero (0 to 23, even with AM/PM display)
+ \li The hour without a leading zero (0 to 23, even with AM/PM display)
\row \li HH
- \li the hour with a leading zero (00 to 23, even with AM/PM display)
- \row \li m \li the minute without a leading zero (0 to 59)
- \row \li mm \li the minute with a leading zero (00 to 59)
- \row \li s \li the whole second, without any leading zero (0 to 59)
- \row \li ss \li the whole second, with a leading zero where applicable (00 to 59)
- \row \li z \li the fractional part of the second, to go after a decimal
+ \li The hour with a leading zero (00 to 23, even with AM/PM display)
+ \row \li m \li The minute without a leading zero (0 to 59)
+ \row \li mm \li The minute with a leading zero (00 to 59)
+ \row \li s \li The whole second, without any leading zero (0 to 59)
+ \row \li ss \li The whole second, with a leading zero where applicable (00 to 59)
+ \row \li z \li The fractional part of the second, to go after a decimal
point, without trailing zeroes (0 to 999). Thus "\c{s.z}"
reports the seconds to full available (millisecond) precision
without trailing zeroes.
- \row \li zzz \li the fractional part of the second, to millisecond
+ \row \li zzz \li The fractional part of the second, to millisecond
precision, including trailing zeroes where applicable (000 to 999).
\row \li AP or A
- \li use AM/PM display. \e A/AP will be replaced by either
- QLocale::amText() or QLocale::pmText().
+ \li Use AM/PM display. \e A/AP will be replaced by an upper-case
+ version of either QLocale::amText() or QLocale::pmText().
\row \li ap or a
- \li use am/pm display. \e a/ap will be replaced by a lower-case version of
- QLocale::amText() or QLocale::pmText().
- \row \li t \li the timezone (for example "CEST")
+ \li Use am/pm display. \e a/ap will be replaced by a lower-case version
+ of either QLocale::amText() or QLocale::pmText().
+ \row \li t \li The timezone (for example "CEST")
\endtable
Any sequence of characters enclosed in single quotes will be included
@@ -2320,22 +2349,21 @@ int QTime::msecsTo(const QTime &t) const
#if QT_CONFIG(datestring)
-static QTime fromIsoTimeString(const QStringRef &string, Qt::DateFormat format, bool *isMidnight24)
+static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format, bool *isMidnight24)
{
if (isMidnight24)
*isMidnight24 = false;
const int size = string.size();
- if (size < 5)
+ if (size < 5 || string.at(2) != QLatin1Char(':'))
return QTime();
- bool ok = false;
- int hour = string.mid(0, 2).toInt(&ok);
- if (!ok)
- return QTime();
- const int minute = string.mid(3, 2).toInt(&ok);
- if (!ok)
+ ParsedInt hour = readInt(string.mid(0, 2));
+ ParsedInt minute = readInt(string.mid(3, 2));
+ if (!hour.ok || !minute.ok)
return QTime();
+ // FIXME: ISO 8601 allows [,.]\d+ after hour, just as it does after minute
+
int second = 0;
int msec = 0;
@@ -2354,40 +2382,57 @@ static QTime fromIsoTimeString(const QStringRef &string, Qt::DateFormat format,
// seconds is 4. E.g. 12:34,99999 will expand to 12:34:59.9994. The milliseconds
// will then be rounded up AND clamped to 999.
- const QStringRef minuteFractionStr = string.mid(6, 5);
- const long minuteFractionInt = minuteFractionStr.toLong(&ok);
- if (!ok)
+ const QStringView minuteFractionStr = string.mid(6, qMin(qsizetype(5), string.size() - 6));
+ const ParsedInt parsed = readInt(minuteFractionStr);
+ if (!parsed.ok)
return QTime();
- const float minuteFraction = double(minuteFractionInt) / (std::pow(double(10), minuteFractionStr.count()));
+ const float secondWithMs
+ = double(parsed.value) * 60 / (std::pow(double(10), minuteFractionStr.size()));
- const float secondWithMs = minuteFraction * 60;
- const float secondNoMs = std::floor(secondWithMs);
- const float secondFraction = secondWithMs - secondNoMs;
- second = secondNoMs;
+ second = std::floor(secondWithMs);
+ const float secondFraction = secondWithMs - second;
msec = qMin(qRound(secondFraction * 1000.0), 999);
- } else {
+ } else if (string.at(5) == QLatin1Char(':')) {
// HH:mm:ss or HH:mm:ss.zzz
- second = string.mid(6, 2).toInt(&ok);
- if (!ok)
+ const ParsedInt parsed = readInt(string.mid(6, qMin(qsizetype(2), string.size() - 6)));
+ if (!parsed.ok)
return QTime();
- if (size > 8 && (string.at(8) == QLatin1Char(',') || string.at(8) == QLatin1Char('.'))) {
- const QStringRef msecStr(string.mid(9, 4));
- int msecInt = msecStr.isEmpty() ? 0 : msecStr.toInt(&ok);
+ second = parsed.value;
+ if (size <= 8) {
+ // No fractional part to read
+ } else if (string.at(8) == QLatin1Char(',') || string.at(8) == QLatin1Char('.')) {
+ QStringView msecStr(string.mid(9, qMin(qsizetype(4), string.size() - 9)));
+ bool ok = true;
+ // Can't use readInt() here, as we *do* allow trailing space - but not leading:
+ if (!msecStr.isEmpty() && !msecStr.at(0).isDigit())
+ return QTime();
+ msecStr = msecStr.trimmed();
+ int msecInt = msecStr.isEmpty() ? 0 : QLocale::c().toInt(msecStr, &ok);
if (!ok)
return QTime();
- const double secondFraction(msecInt / (std::pow(double(10), msecStr.count())));
+ const double secondFraction(msecInt / (std::pow(double(10), msecStr.size())));
msec = qMin(qRound(secondFraction * 1000.0), 999);
+ } else {
+#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) // behavior change
+ // Stray cruft after date-time: tolerate trailing space, but nothing else.
+ for (const auto &ch : string.mid(8)) {
+ if (!ch.isSpace())
+ return QTime();
+ }
+#endif
}
+ } else {
+ return QTime();
}
const bool isISODate = format == Qt::ISODate || format == Qt::ISODateWithMs;
- if (isISODate && hour == 24 && minute == 0 && second == 0 && msec == 0) {
+ if (isISODate && hour.value == 24 && minute.value == 0 && second == 0 && msec == 0) {
if (isMidnight24)
*isMidnight24 = true;
- hour = 0;
+ hour.value = 0;
}
- return QTime(hour, minute, second, msec);
+ return QTime(hour.value, minute.value, second, msec);
}
/*!
@@ -2424,7 +2469,7 @@ QTime QTime::fromString(const QString &string, Qt::DateFormat format)
case Qt::ISODateWithMs:
case Qt::TextDate:
default:
- return fromIsoTimeString(QStringRef(&string), format, nullptr);
+ return fromIsoTimeString(QStringView(string), format, nullptr);
}
}
@@ -2437,23 +2482,30 @@ QTime QTime::fromString(const QString &string, Qt::DateFormat format)
\table
\header \li Expression \li Output
\row \li h
- \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
+ \li The hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
\row \li hh
- \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
- \row \li m \li the minute without a leading zero (0 to 59)
- \row \li mm \li the minute with a leading zero (00 to 59)
- \row \li s \li the whole second, without any leading zero (0 to 59)
- \row \li ss \li the whole second, with a leading zero where applicable (00 to 59)
- \row \li z \li the fractional part of the second, to go after a decimal
+ \li The hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
+ \row \li H
+ \li The hour without a leading zero (0 to 23, even with AM/PM display)
+ \row \li HH
+ \li The hour with a leading zero (00 to 23, even with AM/PM display)
+ \row \li m \li The minute without a leading zero (0 to 59)
+ \row \li mm \li The minute with a leading zero (00 to 59)
+ \row \li s \li The whole second, without any leading zero (0 to 59)
+ \row \li ss \li The whole second, with a leading zero where applicable (00 to 59)
+ \row \li z \li The fractional part of the second, to go after a decimal
point, without trailing zeroes (0 to 999). Thus "\c{s.z}"
reports the seconds to full available (millisecond) precision
without trailing zeroes.
- \row \li zzz \li the fractional part of the second, to millisecond
+ \row \li zzz \li The fractional part of the second, to millisecond
precision, including trailing zeroes where applicable (000 to 999).
- \row \li AP
- \li interpret as an AM/PM time. \e AP must be either "AM" or "PM".
- \row \li ap
- \li Interpret as an AM/PM time. \e ap must be either "am" or "pm".
+ \row \li AP or A
+ \li Interpret as an AM/PM time. \e A/AP will match an upper-case
+ version of either QLocale::amText() or QLocale::pmText().
+ \row \li ap or a
+ \li Interpret as an am/pm time. \e a/ap will match a lower-case version
+ of either QLocale::amText() or QLocale::pmText().
+ \row \li t \li the timezone (for example "CEST")
\endtable
All other input characters will be treated as text. Any sequence
@@ -2488,7 +2540,7 @@ QTime QTime::fromString(const QString &string, const QString &format)
QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString, QCalendar());
// dt.setDefaultLocale(QLocale::c()); ### Qt 6
if (dt.parseFormat(format))
- dt.fromString(string, 0, &time);
+ dt.fromString(string, nullptr, &time);
#else
Q_UNUSED(string);
Q_UNUSED(format);
@@ -3255,7 +3307,7 @@ inline QDateTime::Data::Data(Qt::TimeSpec spec)
// the structure is too small, we need to detach
d = new QDateTimePrivate;
d->ref.ref();
- d->m_status = mergeSpec(nullptr, spec);
+ d->m_status = mergeSpec({}, spec);
}
}
@@ -3399,6 +3451,7 @@ inline qint64 QDateTimePrivate::zoneMSecsToEpochMSecs(qint64 zoneMSecs, const QT
DaylightStatus hint,
QDate *zoneDate, QTime *zoneTime)
{
+ Q_ASSERT(zone.isValid());
// Get the effective data from QTimeZone
QTimeZonePrivate::Data data = zone.d->dataForLocalTime(zoneMSecs, int(hint));
// Docs state any time before 1970-01-01 will *not* have any DST applied
@@ -3788,8 +3841,9 @@ QTimeZone QDateTime::timeZone() const
case Qt::OffsetFromUTC:
return QTimeZone(d->m_offsetFromUtc);
case Qt::TimeZone:
- Q_ASSERT(d->m_timeZone.isValid());
- return d->m_timeZone;
+ if (d->m_timeZone.isValid())
+ return d->m_timeZone;
+ break;
case Qt::LocalTime:
return QTimeZone::systemTimeZone();
}
@@ -3873,6 +3927,7 @@ QString QDateTime::timeZoneAbbreviation() const
#if !QT_CONFIG(timezone)
break;
#else
+ Q_ASSERT(d->m_timeZone.isValid());
return d->m_timeZone.d->abbreviation(toMSecsSinceEpoch());
#endif // timezone
case Qt::LocalTime: {
@@ -3909,6 +3964,7 @@ bool QDateTime::isDaylightTime() const
#if !QT_CONFIG(timezone)
break;
#else
+ Q_ASSERT(d->m_timeZone.isValid());
return d->m_timeZone.d->isDaylightTime(toMSecsSinceEpoch());
#endif // timezone
case Qt::LocalTime: {
@@ -4033,6 +4089,10 @@ void QDateTime::setTimeZone(const QTimeZone &toZone)
*/
qint64 QDateTime::toMSecsSinceEpoch() const
{
+ // Note: QDateTimeParser relies on this producing a useful result, even when
+ // !isValid(), at least when the invalidity is a time in a fall-back (that
+ // we'll have adjusted to lie outside it, but marked invalid because it's
+ // not what was asked for). Other things may be doing similar.
switch (getSpec(d)) {
case Qt::UTC:
return getMSecs(d);
@@ -4047,12 +4107,13 @@ qint64 QDateTime::toMSecsSinceEpoch() const
}
case Qt::TimeZone:
-#if !QT_CONFIG(timezone)
- return 0;
-#else
- return QDateTimePrivate::zoneMSecsToEpochMSecs(d->m_msecs, d->m_timeZone,
- extractDaylightStatus(getStatus(d)));
+#if QT_CONFIG(timezone)
+ if (d->m_timeZone.isValid()) {
+ return QDateTimePrivate::zoneMSecsToEpochMSecs(d->m_msecs, d->m_timeZone,
+ extractDaylightStatus(getStatus(d)));
+ }
#endif
+ return 0;
}
Q_UNREACHABLE();
return 0;
@@ -4147,9 +4208,11 @@ void QDateTime::setMSecsSinceEpoch(qint64 msecs)
case Qt::TimeZone:
Q_ASSERT(!d.isShort());
#if QT_CONFIG(timezone)
+ d.detach();
+ if (!d->m_timeZone.isValid())
+ break;
// Docs state any LocalTime before 1970-01-01 will *not* have any DST applied
// but all affected times afterwards will have DST applied.
- d.detach();
if (msecs >= 0) {
status = mergeDaylightStatus(status,
d->m_timeZone.d->isDaylightTime(msecs)
@@ -4358,61 +4421,9 @@ QString QDateTime::toString(Qt::DateFormat format) const
\fn QString QDateTime::toString(const QString &format) const
\fn QString QDateTime::toString(QStringView format) const
- Returns the datetime as a string. The \a format parameter
- determines the format of the result string.
-
- These expressions may be used for the date:
-
- \table
- \header \li Expression \li Output
- \row \li d \li the day as number without a leading zero (1 to 31)
- \row \li dd \li the day as number with a leading zero (01 to 31)
- \row \li ddd
- \li the abbreviated localized day name (e.g. 'Mon' to 'Sun').
- Uses the system locale to localize the name, i.e. QLocale::system().
- \row \li dddd
- \li the long localized day name (e.g. 'Monday' to 'Sunday').
- Uses the system locale to localize the name, i.e. QLocale::system().
- \row \li M \li the month as number without a leading zero (1-12)
- \row \li MM \li the month as number with a leading zero (01-12)
- \row \li MMM
- \li the abbreviated localized month name (e.g. 'Jan' to 'Dec').
- Uses the system locale to localize the name, i.e. QLocale::system().
- \row \li MMMM
- \li the long localized month name (e.g. 'January' to 'December').
- Uses the system locale to localize the name, i.e. QLocale::system().
- \row \li yy \li the year as two digit number (00-99)
- \row \li yyyy \li the year as four digit number
- \endtable
-
- These expressions may be used for the time:
-
- \table
- \header \li Expression \li Output
- \row \li h
- \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
- \row \li hh
- \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
- \row \li H
- \li the hour without a leading zero (0 to 23, even with AM/PM display)
- \row \li HH
- \li the hour with a leading zero (00 to 23, even with AM/PM display)
- \row \li m \li the minute without a leading zero (0 to 59)
- \row \li mm \li the minute with a leading zero (00 to 59)
- \row \li s \li the whole second without a leading zero (0 to 59)
- \row \li ss \li the whole second with a leading zero where applicable (00 to 59)
- \row \li z \li the fractional part of the second, to go after a decimal
- point, without trailing zeroes (0 to 999). Thus "\c{s.z}"
- reports the seconds to full available (millisecond) precision
- without trailing zeroes.
- \row \li zzz \li the fractional part of the second, to millisecond
- precision, including trailing zeroes where applicable (000 to 999).
- \row \li AP or A
- \li use AM/PM display. \e A/AP will be replaced by either "AM" or "PM".
- \row \li ap or a
- \li use am/pm display. \e a/ap will be replaced by either "am" or "pm".
- \row \li t \li the timezone (for example "CEST")
- \endtable
+ Returns the datetime as a string. The \a format parameter determines the
+ format of the result string. See QTime::toString() and QDate::toString() for
+ the supported specifiers for time and date, respectively.
Any sequence of characters enclosed in single quotes will be included
verbatim in the output string (stripped of the quotes), even if it contains
@@ -4474,7 +4485,7 @@ static inline void massageAdjustedDateTime(const QDateTimeData &d, QDate *date,
QDateTimePrivate::DaylightStatus status = QDateTimePrivate::UnknownDaylightTime;
localMSecsToEpochMSecs(timeToMSecs(*date, *time), &status, date, time);
#if QT_CONFIG(timezone)
- } else if (spec == Qt::TimeZone) {
+ } else if (spec == Qt::TimeZone && d->m_timeZone.isValid()) {
QDateTimePrivate::zoneMSecsToEpochMSecs(timeToMSecs(*date, *time),
d->m_timeZone,
QDateTimePrivate::UnknownDaylightTime,
@@ -5135,7 +5146,8 @@ QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone
{
QDateTime dt;
dt.setTimeZone(timeZone);
- dt.setMSecsSinceEpoch(msecs);
+ if (timeZone.isValid())
+ dt.setMSecsSinceEpoch(msecs);
return dt;
}
@@ -5238,16 +5250,17 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
if (!date.isValid())
return QDateTime();
if (size == 10)
- return QDateTime(date);
+ return date.startOfDay();
Qt::TimeSpec spec = Qt::LocalTime;
- QStringRef isoString(&string);
- isoString = isoString.mid(10); // trim "yyyy-MM-dd"
+ QStringView isoString = QStringView(string).mid(10); // trim "yyyy-MM-dd"
- // Must be left with T and at least one digit for the hour:
+ // Must be left with T (or space) and at least one digit for the hour:
if (isoString.size() < 2
- || !(isoString.startsWith(QLatin1Char('T'))
- // FIXME: QSql relies on QVariant::toDateTime() accepting a space here:
+ || !(isoString.startsWith(QLatin1Char('T'), Qt::CaseInsensitive)
+ // RFC 3339 (section 5.6) allows a space here. (It actually
+ // allows any separator one considers more readable, merely
+ // giving space as an example - but let's not go wild !)
|| isoString.startsWith(QLatin1Char(' ')))) {
return QDateTime();
}
@@ -5255,7 +5268,7 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
int offset = 0;
// Check end of string for Time Zone definition, either Z for UTC or [+-]HH:mm for Offset
- if (isoString.endsWith(QLatin1Char('Z'))) {
+ if (isoString.endsWith(QLatin1Char('Z'), Qt::CaseInsensitive)) {
spec = Qt::UTC;
isoString.chop(1); // trim 'Z'
} else {
@@ -5386,7 +5399,7 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
if (parts.count() == 5)
return QDateTime(date, time, Qt::LocalTime);
- QStringRef tz = parts.at(5);
+ QStringView tz = parts.at(5);
if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive))
return QDateTime();
tz = tz.mid(3);
@@ -5409,65 +5422,13 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
Returns the QDateTime represented by the \a string, using the \a
format given, or an invalid datetime if the string cannot be parsed.
- Uses the calendar \a cal if supplied, else Gregorian. The illustrative
- values and ranges below are given for the latter; other calendars may have
- different ranges or values.
-
- These expressions may be used for the date part of the format string:
+ Uses the calendar \a cal if supplied, else Gregorian.
- \table
- \header \li Expression \li Output
- \row \li d \li the day as number without a leading zero (1 to 31)
- \row \li dd \li the day as number with a leading zero (01 to 31)
- \row \li ddd
- \li the abbreviated localized day name (e.g. 'Mon' to 'Sun').
- \row \li dddd
- \li the long localized day name (e.g. 'Monday' to 'Sunday').
- \row \li M \li the month as number without a leading zero (1-12)
- \row \li MM \li the month as number with a leading zero (01-12)
- \row \li MMM
- \li the abbreviated localized month name (e.g. 'Jan' to 'Dec').
- \row \li MMMM
- \li the long localized month name (e.g. 'January' to 'December').
- \row \li yy \li the year as two digit number (00-99)
- \row \li yyyy \li the year as four digit number
- \endtable
-
- \note Unlike the other version of this function, day and month names must
- be given in the user's local language. It is only possible to use the English
- names if the user's language is English.
-
- These expressions may be used for the time part of the format string:
-
- \table
- \header \li Expression \li Output
- \row \li h
- \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
- \row \li hh
- \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
- \row \li H
- \li the hour without a leading zero (0 to 23, even with AM/PM display)
- \row \li HH
- \li the hour with a leading zero (00 to 23, even with AM/PM display)
- \row \li m \li the minute without a leading zero (0 to 59)
- \row \li mm \li the minute with a leading zero (00 to 59)
- \row \li s \li the whole second without a leading zero (0 to 59)
- \row \li ss \li the whole second with a leading zero where applicable (00 to 59)
- \row \li z \li the fractional part of the second, to go after a decimal
- point, without trailing zeroes (0 to 999). Thus "\c{s.z}"
- reports the seconds to full available (millisecond) precision
- without trailing zeroes.
- \row \li zzz \li the fractional part of the second, to millisecond
- precision, including trailing zeroes where applicable (000 to 999).
- \row \li AP or A
- \li interpret as an AM/PM time. \e AP must be either "AM" or "PM".
- \row \li ap or a
- \li Interpret as an AM/PM time. \e ap must be either "am" or "pm".
- \endtable
-
- All other input characters will be treated as text. Any sequence
- of characters that are enclosed in single quotes will also be
- treated as text and not be used as an expression.
+ See QDate::fromString() and QTime::fromString() for the expressions
+ recognized in the format string to represent parts of the date and time.
+ All other input characters will be treated as text. Any sequence of
+ characters that are enclosed in single quotes will also be treated as text
+ and not be used as an expression.
\snippet code/src_corelib_tools_qdatetime.cpp 12
diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp
index 2c566e3584..31d8e6cc20 100644
--- a/src/corelib/time/qdatetimeparser.cpp
+++ b/src/corelib/time/qdatetimeparser.cpp
@@ -137,13 +137,12 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
#endif
return false;
}
- const SectionNode &node = sectionNodes.at(index);
- const QDate date = v.date();
+ QCalendar::YearMonthDay date = calendar.partsFromDate(v.date());
+ if (!date.isValid())
+ return false;
+
const QTime time = v.time();
- int year = date.year(calendar);
- int month = date.month(calendar);
- int day = date.day(calendar);
int hour = time.hour();
int minute = time.minute();
int second = time.second();
@@ -152,14 +151,15 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
// Only offset from UTC is amenable to setting an int value:
int offset = tspec == Qt::OffsetFromUTC ? v.offsetFromUtc() : 0;
+ const SectionNode &node = sectionNodes.at(index);
switch (node.type) {
case Hour24Section: case Hour12Section: hour = newVal; break;
case MinuteSection: minute = newVal; break;
case SecondSection: second = newVal; break;
case MSecSection: msec = newVal; break;
case YearSection2Digits:
- case YearSection: year = newVal; break;
- case MonthSection: month = newVal; break;
+ case YearSection: date.year = newVal; break;
+ case MonthSection: date.month = newVal; break;
case DaySection:
case DayOfWeekSectionShort:
case DayOfWeekSectionLong:
@@ -169,7 +169,7 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
// to 31 for february should return true
return false;
}
- day = newVal;
+ date.day = newVal;
break;
case TimeZoneSection:
if (newVal < absoluteMin(index) || newVal > absoluteMax(index))
@@ -185,15 +185,14 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
}
if (!(node.type & DaySectionMask)) {
- if (day < cachedDay)
- day = cachedDay;
- const int max = calendar.daysInMonth(month, year);
- if (day > max) {
- day = max;
- }
+ if (date.day < cachedDay)
+ date.day = cachedDay;
+ const int max = calendar.daysInMonth(date.month, date.year);
+ if (date.day > max)
+ date.day = max;
}
- const QDate newDate(year, month, day, calendar);
+ const QDate newDate = calendar.dateFromParts(date);
const QTime newTime(hour, minute, second, msec);
if (!newDate.isValid() || !newTime.isValid())
return false;
@@ -369,13 +368,6 @@ static QString unquote(const QStringRef &str)
}
return ret;
}
-/*!
- \internal
-
- Parses the format \a newFormat. If successful, returns \c true and
- sets up the format. Else keeps the old format and returns \c false.
-
-*/
static inline int countRepeat(const QString &str, int index, int maxCount)
{
@@ -394,7 +386,12 @@ static inline void appendSeparator(QStringList *list, const QString &string, int
list->append(lastQuote >= from ? unquote(separator) : separator.toString());
}
+/*!
+ \internal
+ Parses the format \a newFormat. If successful, returns \c true and sets up
+ the format. Else keeps the old format and returns \c false.
+*/
bool QDateTimeParser::parseFormat(const QString &newFormat)
{
const QLatin1Char quote('\'');
@@ -407,7 +404,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
QDTPDEBUGN("parseFormat: %s", newFormat.toLatin1().constData());
QVector<SectionNode> newSectionNodes;
- Sections newDisplay = 0;
+ Sections newDisplay;
QStringList newSeparators;
int i, index = 0;
int add = 0;
@@ -1154,7 +1151,7 @@ QDateTimeParser::scanString(const QDateTime &defaultValue,
}
pos += separator.size();
sectionNodes[index].pos = pos;
- int *current = 0;
+ int *current = nullptr;
const SectionNode sn = sectionNodes.at(index);
ParsedSection sect;
@@ -1365,7 +1362,7 @@ QDateTimeParser::scanString(const QDateTime &defaultValue,
// given date (which might be a spring-forward, skipping an hour).
if (parserType == QVariant::DateTime && !(isSet & HourSectionMask) && !when.isValid()) {
qint64 msecs = when.toMSecsSinceEpoch();
- // Fortunately, that gets a useful answer ...
+ // Fortunately, that gets a useful answer, even though when is invalid ...
const QDateTime replace =
#if QT_CONFIG(timezone)
tspec == Qt::TimeZone
@@ -1799,7 +1796,7 @@ int QDateTimeParser::SectionNode::maxChange() const
QDateTimeParser::FieldInfo QDateTimeParser::fieldInfo(int index) const
{
- FieldInfo ret = 0;
+ FieldInfo ret;
const SectionNode &sn = sectionNode(index);
switch (sn.type) {
case MSecSection:
diff --git a/src/corelib/time/qdatetimeparser_p.h b/src/corelib/time/qdatetimeparser_p.h
index d128e35ddc..ff8da956f0 100644
--- a/src/corelib/time/qdatetimeparser_p.h
+++ b/src/corelib/time/qdatetimeparser_p.h
@@ -83,7 +83,7 @@ public:
DateTimeEdit
};
QDateTimeParser(QVariant::Type t, Context ctx, const QCalendar &cal = QCalendar())
- : currentSectionIndex(-1), display(nullptr), cachedDay(-1), parserType(t),
+ : currentSectionIndex(-1), cachedDay(-1), parserType(t),
fixday(false), spec(Qt::LocalTime), context(ctx), calendar(cal)
{
defaultLocale = QLocale::system();
diff --git a/src/corelib/time/qtimezone.cpp b/src/corelib/time/qtimezone.cpp
index 410a16e3c5..87d8ea75f1 100644
--- a/src/corelib/time/qtimezone.cpp
+++ b/src/corelib/time/qtimezone.cpp
@@ -217,7 +217,7 @@ Q_GLOBAL_STATIC(QTimeZoneSingleton, global_tz);
This class includes data obtained from the CLDR data files under the terms
of the Unicode Data Files and Software License. See
- \l{Unicode Common Locale Data Repository (CLDR)} for details.
+ \l{unicode-cldr}{Unicode Common Locale Data Repository (CLDR)} for details.
\sa QDateTime
*/
@@ -318,7 +318,7 @@ Q_GLOBAL_STATIC(QTimeZoneSingleton, global_tz);
*/
QTimeZone::QTimeZone() noexcept
- : d(0)
+ : d(nullptr)
{
}
diff --git a/src/corelib/time/qtimezoneprivate.cpp b/src/corelib/time/qtimezoneprivate.cpp
index 72a0e3c24e..facdf6661d 100644
--- a/src/corelib/time/qtimezoneprivate.cpp
+++ b/src/corelib/time/qtimezoneprivate.cpp
@@ -381,18 +381,15 @@ QTimeZonePrivate::Data QTimeZonePrivate::dataForLocalTime(qint64 forLocalMSecs,
On the first pass, the case we consider is what hint told us to expect
(except when hint was -1 and didn't actually tell us what to expect),
so it's likely right. We only get a second pass if the first failed,
- by which time the second case, that we're trying, is likely right. If
- an overwhelming majority of calls have hint == -1, the Q_LIKELY here
- shall be wrong half the time; otherwise, its errors shall be rarer
- than that.
+ by which time the second case, that we're trying, is likely right.
*/
if (nextFirst ? i == 0 : i) {
Q_ASSERT(nextStart != invalidMSecs());
- if (Q_LIKELY(nextStart <= nextTran.atMSecsSinceEpoch))
+ if (nextStart <= nextTran.atMSecsSinceEpoch)
return nextTran;
} else {
// If next is invalid, nextFirst is false, to route us here first:
- if (nextStart == invalidMSecs() || Q_LIKELY(nextStart > tran.atMSecsSinceEpoch))
+ if (nextStart == invalidMSecs() || nextStart > tran.atMSecsSinceEpoch)
return tran;
}
}
@@ -421,7 +418,7 @@ QTimeZonePrivate::Data QTimeZonePrivate::dataForLocalTime(qint64 forLocalMSecs,
int early = offsetFromUtc(recent);
int late = offsetFromUtc(imminent);
- if (Q_LIKELY(early == late)) { // > 99% of the time
+ if (early == late) { // > 99% of the time
utcEpochMSecs = forLocalMSecs - early * 1000;
} else {
// Close to a DST transition: early > late is near a fall-back,
@@ -433,7 +430,7 @@ QTimeZonePrivate::Data QTimeZonePrivate::dataForLocalTime(qint64 forLocalMSecs,
const qint64 forStd = forLocalMSecs - offsetInStd * 1000;
// Best guess at the answer:
const qint64 hinted = hint > 0 ? forDst : forStd;
- if (Q_LIKELY(offsetFromUtc(hinted) == (hint > 0 ? offsetInDst : offsetInStd))) {
+ if (offsetFromUtc(hinted) == (hint > 0 ? offsetInDst : offsetInStd)) {
utcEpochMSecs = hinted;
} else if (hint <= 0 && offsetFromUtc(forDst) == offsetInDst) {
utcEpochMSecs = forDst;
@@ -633,7 +630,13 @@ bool QTimeZonePrivate::isValidId(const QByteArray &ianaId)
// Somewhat slack hand-rolled version:
const int MinSectionLength = 1;
+#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
+ // Android has its own naming of zones.
+ // "Canada/East-Saskatchewan" has a 17-character second component.
+ const int MaxSectionLength = 17;
+#else
const int MaxSectionLength = 14;
+#endif
int sectionLength = 0;
for (const char *it = ianaId.begin(), * const end = ianaId.end(); it != end; ++it, ++sectionLength) {
const char ch = *it;
diff --git a/src/corelib/time/qtimezoneprivate_android.cpp b/src/corelib/time/qtimezoneprivate_android.cpp
index be4f374fdd..5cb8155dcc 100644
--- a/src/corelib/time/qtimezoneprivate_android.cpp
+++ b/src/corelib/time/qtimezoneprivate_android.cpp
@@ -1,5 +1,6 @@
/****************************************************************************
**
+** Copyright (C) 2019 The Qt Company Ltd.
** Copyright (C) 2014 Drew Parsons <dparsons@emerall.com>
** Contact: https://www.qt.io/licensing/
**
@@ -53,9 +54,10 @@ QT_BEGIN_NAMESPACE
QAndroidTimeZonePrivate::QAndroidTimeZonePrivate()
: QTimeZonePrivate()
{
- // start with system time zone
- androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod("java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;");
- init("UTC");
+ // Keep in sync with systemTimeZoneId():
+ androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod(
+ "java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;");
+ m_id = androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;").toString().toUtf8();
}
// Create a named time zone
@@ -76,32 +78,33 @@ QAndroidTimeZonePrivate::~QAndroidTimeZonePrivate()
{
}
-
void QAndroidTimeZonePrivate::init(const QByteArray &ianaId)
{
- QJNIObjectPrivate jo_ianaId = QJNIObjectPrivate::fromString( QString::fromUtf8(ianaId) );
- androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod( "java.util.TimeZone", "getTimeZone", "(Ljava/lang/String;)Ljava/util/TimeZone;", static_cast<jstring>(jo_ianaId.object()) );
+ const QString iana = QString::fromUtf8(ianaId);
+ androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod(
+ "java.util.TimeZone", "getTimeZone", "(Ljava/lang/String;)Ljava/util/TimeZone;",
+ static_cast<jstring>(QJNIObjectPrivate::fromString(iana).object()));
+
+ // The ID or display name of the zone we've got, if it looks like what we asked for:
+ const auto match = [iana](const QJNIObjectPrivate &jname) -> QByteArray {
+ const QString name = jname.toString();
+ if (iana.compare(name, Qt::CaseInsensitive))
+ return name.toUtf8();
+
+ return QByteArray();
+ };
// Painfully, JNI gives us back a default zone object if it doesn't
// recognize the name; so check for whether ianaId is a recognized name of
// the zone object we got and ignore the zone if not.
// Try checking ianaId against getID(), getDisplayName():
- QJNIObjectPrivate jname = androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;");
- bool found = (jname.toString().toUtf8() == ianaId);
- for (int style = 1; !found && style-- > 0;) {
- for (int dst = 1; !found && dst-- > 0;) {
- jname = androidTimeZone.callObjectMethod("getDisplayName", "(ZI;)Ljava/lang/String;",
- bool(dst), style);
- found = (jname.toString().toUtf8() == ianaId);
+ m_id = match(androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;"));
+ for (int style = 1; m_id.isEmpty() && style-- > 0;) {
+ for (int dst = 1; m_id.isEmpty() && dst-- > 0;) {
+ m_id = match(androidTimeZone.callObjectMethod(
+ "getDisplayName", "(ZI;)Ljava/lang/String;", bool(dst), style));
}
}
-
- if (!found)
- m_id.clear();
- else if (ianaId.isEmpty())
- m_id = systemTimeZoneId();
- else
- m_id = ianaId;
}
QAndroidTimeZonePrivate *QAndroidTimeZonePrivate::clone() const
@@ -225,11 +228,10 @@ QTimeZonePrivate::Data QAndroidTimeZonePrivate::previousTransition(qint64 before
QByteArray QAndroidTimeZonePrivate::systemTimeZoneId() const
{
- QJNIObjectPrivate androidSystemTimeZone = QJNIObjectPrivate::callStaticObjectMethod("java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;");
- QJNIObjectPrivate systemTZIdAndroid = androidSystemTimeZone.callObjectMethod<jstring>("getID");
- QByteArray systemTZid = systemTZIdAndroid.toString().toUtf8();
-
- return systemTZid;
+ // Keep in sync with default constructor:
+ QJNIObjectPrivate androidSystemTimeZone = QJNIObjectPrivate::callStaticObjectMethod(
+ "java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;");
+ return androidSystemTimeZone.callObjectMethod<jstring>("getID").toString().toUtf8();
}
QList<QByteArray> QAndroidTimeZonePrivate::availableTimeZoneIds() const
diff --git a/src/corelib/time/qtimezoneprivate_icu.cpp b/src/corelib/time/qtimezoneprivate_icu.cpp
index 5570ce7571..8a92bbb387 100644
--- a/src/corelib/time/qtimezoneprivate_icu.cpp
+++ b/src/corelib/time/qtimezoneprivate_icu.cpp
@@ -273,7 +273,7 @@ static int ucalDaylightOffset(const QByteArray &id)
// Create the system default time zone
QIcuTimeZonePrivate::QIcuTimeZonePrivate()
- : m_ucal(0)
+ : m_ucal(nullptr)
{
// TODO No ICU C API to obtain sysem tz, assume default hasn't been changed
init(ucalDefaultTimeZoneId());
@@ -281,7 +281,7 @@ QIcuTimeZonePrivate::QIcuTimeZonePrivate()
// Create a named time zone
QIcuTimeZonePrivate::QIcuTimeZonePrivate(const QByteArray &ianaId)
- : m_ucal(0)
+ : m_ucal(nullptr)
{
// Need to check validity here as ICu will create a GMT tz if name is invalid
if (availableTimeZoneIds().contains(ianaId))
@@ -289,14 +289,14 @@ QIcuTimeZonePrivate::QIcuTimeZonePrivate(const QByteArray &ianaId)
}
QIcuTimeZonePrivate::QIcuTimeZonePrivate(const QIcuTimeZonePrivate &other)
- : QTimeZonePrivate(other), m_ucal(0)
+ : QTimeZonePrivate(other), m_ucal(nullptr)
{
// Clone the ucal so we don't close the shared object
UErrorCode status = U_ZERO_ERROR;
m_ucal = ucal_clone(other.m_ucal, &status);
if (!U_SUCCESS(status)) {
m_id.clear();
- m_ucal = 0;
+ m_ucal = nullptr;
}
}
@@ -322,7 +322,7 @@ void QIcuTimeZonePrivate::init(const QByteArray &ianaId)
if (!U_SUCCESS(status)) {
m_id.clear();
- m_ucal = 0;
+ m_ucal = nullptr;
}
}
@@ -493,7 +493,7 @@ QList<QByteArray> QIcuTimeZonePrivate::availableTimeZoneIds(int offsetFromUtc) c
// TODO Available directly in C++ api but not C api, from 4.8 onwards new filter method works
#if U_ICU_VERSION_MAJOR_NUM >= 49 || (U_ICU_VERSION_MAJOR_NUM == 4 && U_ICU_VERSION_MINOR_NUM == 8)
UErrorCode status = U_ZERO_ERROR;
- UEnumeration *uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, 0,
+ UEnumeration *uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, nullptr,
&offsetFromUtc, &status);
QList<QByteArray> result;
if (U_SUCCESS(status))
diff --git a/src/corelib/time/qtimezoneprivate_mac.mm b/src/corelib/time/qtimezoneprivate_mac.mm
index d3c4fbe5da..1fb48a31d3 100644
--- a/src/corelib/time/qtimezoneprivate_mac.mm
+++ b/src/corelib/time/qtimezoneprivate_mac.mm
@@ -1,5 +1,6 @@
/****************************************************************************
**
+** Copyright (C) 2019 The Qt Company Ltd.
** Copyright (C) 2013 John Layt <jlayt@kde.org>
** Contact: https://www.qt.io/licensing/
**
@@ -59,22 +60,24 @@ QT_BEGIN_NAMESPACE
// Create the system default time zone
QMacTimeZonePrivate::QMacTimeZonePrivate()
- : m_nstz(0)
{
- init(systemTimeZoneId());
+ // Reset the cached system tz then instantiate it:
+ [NSTimeZone resetSystemTimeZone];
+ m_nstz = [NSTimeZone.systemTimeZone retain];
+ Q_ASSERT(m_nstz);
+ m_id = QString::fromNSString(m_nstz.name).toUtf8();
}
// Create a named time zone
QMacTimeZonePrivate::QMacTimeZonePrivate(const QByteArray &ianaId)
- : m_nstz(0)
+ : m_nstz(nil)
{
init(ianaId);
}
QMacTimeZonePrivate::QMacTimeZonePrivate(const QMacTimeZonePrivate &other)
- : QTimeZonePrivate(other), m_nstz(0)
+ : QTimeZonePrivate(other), m_nstz([other.m_nstz copy])
{
- m_nstz = [other.m_nstz copy];
}
QMacTimeZonePrivate::~QMacTimeZonePrivate()
@@ -94,11 +97,21 @@ void QMacTimeZonePrivate::init(const QByteArray &ianaId)
if (m_nstz)
m_id = ianaId;
}
+ if (!m_nstz) {
+ // macOS has been seen returning a systemTimeZone which reports its name
+ // as Asia/Kolkata, which doesn't appear in knownTimeZoneNames (which
+ // calls the zone Asia/Calcutta). So explicitly check for the name
+ // systemTimeZoneId() returns, and use systemTimeZone if we get it:
+ m_nstz = [NSTimeZone.systemTimeZone retain];
+ Q_ASSERT(m_nstz);
+ if (QString::fromNSString(m_nstz.name).toUtf8() == ianaId)
+ m_id = ianaId;
+ }
}
QString QMacTimeZonePrivate::comment() const
{
- return QString::fromNSString([m_nstz description]);
+ return QString::fromNSString(m_nstz.description);
}
QString QMacTimeZonePrivate::displayName(QTimeZone::TimeType timeType,
@@ -201,7 +214,7 @@ bool QMacTimeZonePrivate::hasTransitions() const
// TODO Not sure what is returned in event of no transitions, assume will be before requested date
NSDate *epoch = [NSDate dateWithTimeIntervalSince1970:0];
const NSDate *date = [m_nstz nextDaylightSavingTimeTransitionAfterDate:epoch];
- const bool result = ([date timeIntervalSince1970] > [epoch timeIntervalSince1970]);
+ const bool result = (date.timeIntervalSince1970 > epoch.timeIntervalSince1970);
return result;
}
@@ -211,7 +224,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::nextTransition(qint64 afterMSecsSinc
const NSTimeInterval seconds = afterMSecsSinceEpoch / 1000.0;
NSDate *nextDate = [NSDate dateWithTimeIntervalSince1970:seconds];
nextDate = [m_nstz nextDaylightSavingTimeTransitionAfterDate:nextDate];
- const NSTimeInterval nextSecs = [nextDate timeIntervalSince1970];
+ const NSTimeInterval nextSecs = nextDate.timeIntervalSince1970;
if (nextDate == nil || nextSecs <= seconds) {
[nextDate release];
return invalidData();
@@ -237,7 +250,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::previousTransition(qint64 beforeMSec
NSDate *nextDate = [NSDate dateWithTimeIntervalSince1970:nextSecs];
nextDate = [m_nstz nextDaylightSavingTimeTransitionAfterDate:nextDate];
if (nextDate != nil
- && (tranSecs = [nextDate timeIntervalSince1970]) < endSecs) {
+ && (tranSecs = nextDate.timeIntervalSince1970) < endSecs) {
// There's a transition within the last year before endSecs:
nextSecs = tranSecs;
} else {
@@ -246,7 +259,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::previousTransition(qint64 beforeMSec
nextDate = [m_nstz nextDaylightSavingTimeTransitionAfterDate:nextDate];
if (nextDate != nil) {
NSTimeInterval lateSecs = nextSecs;
- nextSecs = [nextDate timeIntervalSince1970];
+ nextSecs = nextDate.timeIntervalSince1970;
Q_ASSERT(nextSecs <= endSecs - year || nextSecs == tranSecs);
/*
We're looking at the first ever transition for our zone, at
@@ -272,8 +285,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::previousTransition(qint64 beforeMSec
NSTimeInterval middle = nextSecs / 2 + lateSecs / 2;
NSDate *split = [NSDate dateWithTimeIntervalSince1970:middle];
split = [m_nstz nextDaylightSavingTimeTransitionAfterDate:split];
- if (split != nil
- && (tranSecs = [split timeIntervalSince1970]) < endSecs) {
+ if (split != nil && (tranSecs = split.timeIntervalSince1970) < endSecs) {
nextDate = split;
nextSecs = tranSecs;
} else {
@@ -290,7 +302,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::previousTransition(qint64 beforeMSec
while (nextDate != nil && nextSecs < endSecs) {
prevSecs = nextSecs;
nextDate = [m_nstz nextDaylightSavingTimeTransitionAfterDate:nextDate];
- nextSecs = [nextDate timeIntervalSince1970];
+ nextSecs = nextDate.timeIntervalSince1970;
if (nextSecs <= prevSecs) // presumably no later data available
break;
}
@@ -305,18 +317,19 @@ QByteArray QMacTimeZonePrivate::systemTimeZoneId() const
{
// Reset the cached system tz then return the name
[NSTimeZone resetSystemTimeZone];
- return QString::fromNSString([[NSTimeZone systemTimeZone] name]).toUtf8();
+ Q_ASSERT(NSTimeZone.systemTimeZone);
+ return QString::fromNSString(NSTimeZone.systemTimeZone.name).toUtf8();
}
QList<QByteArray> QMacTimeZonePrivate::availableTimeZoneIds() const
{
- NSEnumerator *enumerator = [[NSTimeZone knownTimeZoneNames] objectEnumerator];
- QByteArray tzid = QString::fromNSString([enumerator nextObject]).toUtf8();
+ NSEnumerator *enumerator = NSTimeZone.knownTimeZoneNames.objectEnumerator;
+ QByteArray tzid = QString::fromNSString(enumerator.nextObject).toUtf8();
QList<QByteArray> list;
while (!tzid.isEmpty()) {
list << tzid;
- tzid = QString::fromNSString([enumerator nextObject]).toUtf8();
+ tzid = QString::fromNSString(enumerator.nextObject).toUtf8();
}
std::sort(list.begin(), list.end());
diff --git a/src/corelib/time/qtimezoneprivate_tz.cpp b/src/corelib/time/qtimezoneprivate_tz.cpp
index 3c2695a789..5e55c6897d 100644
--- a/src/corelib/time/qtimezoneprivate_tz.cpp
+++ b/src/corelib/time/qtimezoneprivate_tz.cpp
@@ -512,7 +512,7 @@ PosixZone PosixZone::parse(const char *&pos, const char *end)
if (zoneEnd < end && (zoneEnd[0] == '+' || zoneEnd[0] == '-'))
++zoneEnd;
while (zoneEnd < end) {
- if (strchr(offsetChars, char(*zoneEnd)) == NULL)
+ if (strchr(offsetChars, char(*zoneEnd)) == nullptr)
break;
++zoneEnd;
}
diff --git a/src/corelib/tools/qalgorithms.h b/src/corelib/tools/qalgorithms.h
index b01ce0db58..aa79e0d4a9 100644
--- a/src/corelib/tools/qalgorithms.h
+++ b/src/corelib/tools/qalgorithms.h
@@ -535,7 +535,7 @@ QT_DEPRECATED_X("Use std::binary_search") Q_OUTOFLINE_TEMPLATE RandomAccessItera
# define QT_HAS_BUILTIN_CTZS
Q_DECL_CONSTEXPR Q_ALWAYS_INLINE uint qt_builtin_ctzs(quint16 v) noexcept
{
-# if QT_HAS_BUILTIN(__builtin_ctzs)
+# if __has_builtin(__builtin_ctzs)
return __builtin_ctzs(v);
# else
return __builtin_ctz(v);
@@ -544,7 +544,7 @@ Q_DECL_CONSTEXPR Q_ALWAYS_INLINE uint qt_builtin_ctzs(quint16 v) noexcept
#define QT_HAS_BUILTIN_CLZS
Q_DECL_CONSTEXPR Q_ALWAYS_INLINE uint qt_builtin_clzs(quint16 v) noexcept
{
-# if QT_HAS_BUILTIN(__builtin_clzs)
+# if __has_builtin(__builtin_clzs)
return __builtin_clzs(v);
# else
return __builtin_clz(v) - 16U;
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp
index ed7dfe2e41..497eae1f7f 100644
--- a/src/corelib/tools/qarraydata.cpp
+++ b/src/corelib/tools/qarraydata.cpp
@@ -153,24 +153,24 @@ QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Wmissing-field-initializers")
const QArrayData QArrayData::shared_null[2] = {
- { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, sizeof(QArrayData) }, // shared null
+ { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }, // shared null
/* zero initialized terminator */};
-static const QArrayData qt_array[3] = {
- { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, sizeof(QArrayData) }, // shared empty
- { { Q_BASIC_ATOMIC_INITIALIZER(0) }, 0, 0, 0, sizeof(QArrayData) }, // unsharable empty
+static const QArrayData emptyNotNullShared[2] = {
+ { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }, // shared empty
/* zero initialized terminator */};
QT_WARNING_POP
-static const QArrayData &qt_array_empty = qt_array[0];
+static const QArrayData &qt_array_empty = emptyNotNullShared[0];
+
static inline size_t calculateBlockSize(size_t &capacity, size_t objectSize, size_t headerSize,
uint options)
{
// Calculate the byte size
// allocSize = objectSize * capacity + headerSize, but checked for overflow
// plus padded to grow in size
- if (options & QArrayData::Grow) {
+ if (options & QArrayData::GrowsForward) {
auto r = qCalculateGrowingBlockSize(capacity, objectSize, headerSize);
capacity = r.elementCount;
return r.size;
@@ -179,66 +179,93 @@ static inline size_t calculateBlockSize(size_t &capacity, size_t objectSize, siz
}
}
+static QArrayData *allocateData(size_t allocSize, uint options)
+{
+ QArrayData *header = static_cast<QArrayData *>(::malloc(allocSize));
+ if (header) {
+ header->ref_.storeRelaxed(1);
+ header->flags = options;
+ header->alloc = 0;
+ }
+ return header;
+}
+
static QArrayData *reallocateData(QArrayData *header, size_t allocSize, uint options)
{
header = static_cast<QArrayData *>(::realloc(header, allocSize));
if (header)
- header->capacityReserved = bool(options & QArrayData::CapacityReserved);
+ header->flags = options;
return header;
}
-QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment,
- size_t capacity, AllocationOptions options) noexcept
+void *QArrayData::allocate(QArrayData **dptr, size_t objectSize, size_t alignment,
+ size_t capacity, ArrayOptions options) noexcept
{
+ Q_ASSERT(dptr);
// Alignment is a power of two
Q_ASSERT(alignment >= alignof(QArrayData)
&& !(alignment & (alignment - 1)));
- // Don't allocate empty headers
- if (!(options & RawData) && !capacity)
- return const_cast<QArrayData *>(&qt_array_empty);
+ if (capacity == 0) {
+ // optimization for empty headers
+ *dptr = const_cast<QArrayData *>(&qt_array_empty);
+ return sharedNullData();
+ }
size_t headerSize = sizeof(QArrayData);
- // Allocate extra (alignment - alignof(QArrayData)) padding bytes so we
- // can properly align the data array. This assumes malloc is able to
- // provide appropriate alignment for the header -- as it should!
- // Padding is skipped when allocating a header for RawData.
- if (!(options & RawData))
- headerSize += (alignment - alignof(QArrayData));
+ if (alignment > alignof(QArrayData)) {
+ // Allocate extra (alignment - Q_ALIGNOF(QArrayData)) padding bytes so we
+ // can properly align the data array. This assumes malloc is able to
+ // provide appropriate alignment for the header -- as it should!
+ headerSize += alignment - alignof(QArrayData);
+ }
if (headerSize > size_t(MaxAllocSize))
return nullptr;
size_t allocSize = calculateBlockSize(capacity, objectSize, headerSize, options);
- QArrayData *header = static_cast<QArrayData *>(::malloc(allocSize));
+ options |= AllocatedDataType | MutableData;
+ options &= ~ImmutableHeader;
+ QArrayData *header = allocateData(allocSize, options);
+ quintptr data = 0;
if (header) {
- quintptr data = (quintptr(header) + sizeof(QArrayData) + alignment - 1)
+ // find where offset should point to so that data() is aligned to alignment bytes
+ data = (quintptr(header) + sizeof(QArrayData) + alignment - 1)
& ~(alignment - 1);
-
- header->ref.atomic.storeRelaxed(1);
- header->size = 0;
- header->alloc = capacity;
- header->capacityReserved = bool(options & CapacityReserved);
- header->offset = data - quintptr(header);
+ header->alloc = uint(capacity);
}
+ *dptr = header;
+ return reinterpret_cast<void *>(data);
+}
+
+QArrayData *QArrayData::prepareRawData(ArrayOptions options) Q_DECL_NOTHROW
+{
+ QArrayData *header = allocateData(sizeof(QArrayData), (options & ~DataTypeBits) | RawDataType);
+ if (header)
+ header->alloc = 0;
return header;
}
-QArrayData *QArrayData::reallocateUnaligned(QArrayData *data, size_t objectSize, size_t capacity,
- AllocationOptions options) noexcept
+QPair<QArrayData *, void *>
+QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer,
+ size_t objectSize, size_t capacity, ArrayOptions options) noexcept
{
Q_ASSERT(data);
Q_ASSERT(data->isMutable());
- Q_ASSERT(!data->ref.isShared());
+ Q_ASSERT(!data->isShared());
size_t headerSize = sizeof(QArrayData);
size_t allocSize = calculateBlockSize(capacity, objectSize, headerSize, options);
- QArrayData *header = static_cast<QArrayData *>(reallocateData(data, allocSize, options));
- if (header)
- header->alloc = capacity;
- return header;
+ qptrdiff offset = reinterpret_cast<char *>(dataPointer) - reinterpret_cast<char *>(data);
+ options |= AllocatedDataType | MutableData;
+ QArrayData *header = reallocateData(data, allocSize, options);
+ if (header) {
+ header->alloc = uint(capacity);
+ dataPointer = reinterpret_cast<char *>(header) + offset;
+ }
+ return qMakePair(static_cast<QArrayData *>(header), dataPointer);
}
void QArrayData::deallocate(QArrayData *data, size_t objectSize,
@@ -249,7 +276,7 @@ void QArrayData::deallocate(QArrayData *data, size_t objectSize,
&& !(alignment & (alignment - 1)));
Q_UNUSED(objectSize) Q_UNUSED(alignment)
- Q_ASSERT_X(data == 0 || !data->ref.isStatic(), "QArrayData::deallocate",
+ Q_ASSERT_X(data == nullptr || !data->isStatic(), "QArrayData::deallocate",
"Static data cannot be deleted");
::free(data);
}
diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h
index 0063cf046f..483f6d5edb 100644
--- a/src/corelib/tools/qarraydata.h
+++ b/src/corelib/tools/qarraydata.h
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2019 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -40,32 +41,64 @@
#ifndef QARRAYDATA_H
#define QARRAYDATA_H
-#include <QtCore/qrefcount.h>
+#include <QtCore/qpair.h>
+#include <QtCore/qatomic.h>
#include <string.h>
QT_BEGIN_NAMESPACE
+template <class T> struct QTypedArrayData;
+
struct Q_CORE_EXPORT QArrayData
{
- QtPrivate::RefCount ref;
- int size;
- uint alloc : 31;
- uint capacityReserved : 1;
+ enum ArrayOption {
+ RawDataType = 0x0001, //!< this class is really a QArrayData
+ AllocatedDataType = 0x0002, //!< this class is really a QArrayAllocatedData
+ DataTypeBits = 0x000f,
+
+ CapacityReserved = 0x0010, //!< the capacity was reserved by the user, try to keep it
+ GrowsForward = 0x0020, //!< allocate with eyes towards growing through append()
+ GrowsBackwards = 0x0040, //!< allocate with eyes towards growing through prepend()
+ MutableData = 0x0080, //!< the data can be changed; doesn't say anything about the header
+ ImmutableHeader = 0x0100, //!< the header is static, it can't be changed
+
+ /// this option is used by the Q_ARRAY_LITERAL and similar macros
+ StaticDataFlags = RawDataType | ImmutableHeader,
+ /// this option is used by the allocate() function
+ DefaultAllocationFlags = MutableData,
+ /// this option is used by the prepareRawData() function
+ DefaultRawFlags = 0
+ };
+ Q_DECLARE_FLAGS(ArrayOptions, ArrayOption)
- qptrdiff offset; // in bytes from beginning of header
+ QBasicAtomicInt ref_;
+ uint flags;
+ uint alloc;
+
+ inline size_t allocatedCapacity()
+ {
+ return alloc;
+ }
+
+ inline size_t constAllocatedCapacity() const
+ {
+ return alloc;
+ }
- void *data()
+ /// Returns true if sharing took place
+ bool ref()
{
- Q_ASSERT(size == 0
- || offset < 0 || size_t(offset) >= sizeof(QArrayData));
- return reinterpret_cast<char *>(this) + offset;
+ if (!isStatic())
+ ref_.ref();
+ return true;
}
- const void *data() const
+ /// Returns false if deallocation is necessary
+ bool deref()
{
- Q_ASSERT(size == 0
- || offset < 0 || size_t(offset) >= sizeof(QArrayData));
- return reinterpret_cast<const char *>(this) + offset;
+ if (isStatic())
+ return true;
+ return ref_.deref();
}
// This refers to array data mutability, not "header data" represented by
@@ -73,54 +106,93 @@ struct Q_CORE_EXPORT QArrayData
// follow COW principles.
bool isMutable() const
{
- return alloc != 0;
+ return flags & MutableData;
}
- enum AllocationOption {
- CapacityReserved = 0x1,
- RawData = 0x4,
- Grow = 0x8,
+ bool isStatic() const
+ {
+ return flags & ImmutableHeader;
+ }
- Default = 0
- };
+ bool isShared() const
+ {
+ return ref_.loadRelaxed() != 1;
+ }
- Q_DECLARE_FLAGS(AllocationOptions, AllocationOption)
+ // Returns true if a detach is necessary before modifying the data
+ // This method is intentionally not const: if you want to know whether
+ // detaching is necessary, you should be in a non-const function already
+ bool needsDetach()
+ {
+ // requires two conditionals
+ return !isMutable() || isShared();
+ }
size_t detachCapacity(size_t newSize) const
{
- if (capacityReserved && newSize < alloc)
- return alloc;
+ if (flags & CapacityReserved && newSize < constAllocatedCapacity())
+ return constAllocatedCapacity();
return newSize;
}
- AllocationOptions detachFlags() const
+ ArrayOptions detachFlags() const
{
- AllocationOptions result;
- if (capacityReserved)
+ ArrayOptions result = DefaultAllocationFlags;
+ if (flags & CapacityReserved)
result |= CapacityReserved;
return result;
}
- AllocationOptions cloneFlags() const
+ ArrayOptions cloneFlags() const
{
- AllocationOptions result;
- if (capacityReserved)
+ ArrayOptions result = DefaultAllocationFlags;
+ if (flags & CapacityReserved)
result |= CapacityReserved;
return result;
}
- Q_REQUIRED_RESULT static QArrayData *allocate(size_t objectSize, size_t alignment,
- size_t capacity, AllocationOptions options = Default) noexcept;
+ Q_REQUIRED_RESULT
+#if defined(Q_CC_GNU)
+ __attribute__((__malloc__))
+#endif
+ static void *allocate(QArrayData **pdata, size_t objectSize, size_t alignment,
+ size_t capacity, ArrayOptions options = DefaultAllocationFlags) noexcept;
Q_REQUIRED_RESULT static QArrayData *reallocateUnaligned(QArrayData *data, size_t objectSize,
- size_t newCapacity, AllocationOptions newOptions = Default) noexcept;
+ size_t newCapacity, ArrayOptions newOptions = DefaultAllocationFlags) noexcept;
+ Q_REQUIRED_RESULT static QPair<QArrayData *, void *> reallocateUnaligned(QArrayData *data, void *dataPointer,
+ size_t objectSize, size_t newCapacity, ArrayOptions newOptions = DefaultAllocationFlags) Q_DECL_NOTHROW;
+ Q_REQUIRED_RESULT static QArrayData *prepareRawData(ArrayOptions options = ArrayOptions(RawDataType))
+ Q_DECL_NOTHROW;
static void deallocate(QArrayData *data, size_t objectSize,
size_t alignment) noexcept;
static const QArrayData shared_null[2];
static QArrayData *sharedNull() noexcept { return const_cast<QArrayData*>(shared_null); }
+ static void *sharedNullData()
+ {
+ QArrayData *const null = const_cast<QArrayData *>(&shared_null[1]);
+ return null;
+ }
+};
+
+Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::ArrayOptions)
+
+template <class T, size_t N>
+struct QStaticArrayData
+{
+ // static arrays are of type RawDataType
+ QArrayData header;
+ T data[N];
};
-Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::AllocationOptions)
+// Support for returning QArrayDataPointer<T> from functions
+template <class T>
+struct QArrayDataPointerRef
+{
+ QTypedArrayData<T> *ptr;
+ T *data;
+ uint size;
+};
template <class T>
struct QTypedArrayData
@@ -202,32 +274,28 @@ struct QTypedArrayData
typedef const T* const_iterator;
#endif
- T *data() { return static_cast<T *>(QArrayData::data()); }
- const T *data() const { return static_cast<const T *>(QArrayData::data()); }
-
- iterator begin(iterator = iterator()) { return data(); }
- iterator end(iterator = iterator()) { return data() + size; }
- const_iterator begin(const_iterator = const_iterator()) const { return data(); }
- const_iterator end(const_iterator = const_iterator()) const { return data() + size; }
- const_iterator constBegin(const_iterator = const_iterator()) const { return data(); }
- const_iterator constEnd(const_iterator = const_iterator()) const { return data() + size; }
-
class AlignmentDummy { QArrayData header; T data; };
- Q_REQUIRED_RESULT static QTypedArrayData *allocate(size_t capacity,
- AllocationOptions options = Default)
+ Q_REQUIRED_RESULT static QPair<QTypedArrayData *, T *> allocate(size_t capacity,
+ ArrayOptions options = DefaultAllocationFlags)
{
Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData));
- return static_cast<QTypedArrayData *>(QArrayData::allocate(sizeof(T),
- alignof(AlignmentDummy), capacity, options));
+ QArrayData *d;
+ void *result = QArrayData::allocate(&d, sizeof(T), alignof(AlignmentDummy), capacity, options);
+#if (defined(Q_CC_GNU) && Q_CC_GNU >= 407) || QT_HAS_BUILTIN(__builtin_assume_aligned)
+ result = __builtin_assume_aligned(result, Q_ALIGNOF(AlignmentDummy));
+#endif
+ return qMakePair(static_cast<QTypedArrayData *>(d), static_cast<T *>(result));
}
- static QTypedArrayData *reallocateUnaligned(QTypedArrayData *data, size_t capacity,
- AllocationOptions options = Default)
+ static QPair<QTypedArrayData *, T *>
+ reallocateUnaligned(QTypedArrayData *data, T *dataPointer, size_t capacity,
+ ArrayOptions options = DefaultAllocationFlags)
{
Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData));
- return static_cast<QTypedArrayData *>(QArrayData::reallocateUnaligned(data, sizeof(T),
- capacity, options));
+ QPair<QArrayData *, void *> pair =
+ QArrayData::reallocateUnaligned(data, dataPointer, sizeof(T), capacity, options);
+ return qMakePair(static_cast<QTypedArrayData *>(pair.first), static_cast<T *>(pair.second));
}
static void deallocate(QArrayData *data)
@@ -236,17 +304,15 @@ struct QTypedArrayData
QArrayData::deallocate(data, sizeof(T), alignof(AlignmentDummy));
}
- static QTypedArrayData *fromRawData(const T *data, size_t n,
- AllocationOptions options = Default)
+ static QArrayDataPointerRef<T> fromRawData(const T *data, size_t n,
+ ArrayOptions options = DefaultRawFlags)
{
Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData));
- QTypedArrayData *result = allocate(0, options | RawData);
- if (result) {
- Q_ASSERT(!result->ref.isShared()); // No shared empty, please!
-
- result->offset = reinterpret_cast<const char *>(data)
- - reinterpret_cast<const char *>(result);
- result->size = int(n);
+ QArrayDataPointerRef<T> result = {
+ static_cast<QTypedArrayData *>(prepareRawData(options)), const_cast<T *>(data), uint(n)
+ };
+ if (result.ptr) {
+ Q_ASSERT(!result.ptr->isShared()); // No shared empty, please!
}
return result;
}
@@ -262,31 +328,14 @@ struct QTypedArrayData
Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData));
return allocate(/* capacity */ 0);
}
-};
-
-template <class T, size_t N>
-struct QStaticArrayData
-{
- QArrayData header;
- T data[N];
-};
-// Support for returning QArrayDataPointer<T> from functions
-template <class T>
-struct QArrayDataPointerRef
-{
- QTypedArrayData<T> *ptr;
+ static T *sharedNullData()
+ {
+ Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData));
+ return static_cast<T *>(QArrayData::sharedNullData());
+ }
};
-#define Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \
- { Q_REFCOUNT_INITIALIZE_STATIC, size, 0, 0, offset } \
- /**/
-
-#define Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(type, size) \
- Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size,\
- ((sizeof(QArrayData) + (alignof(type) - 1)) & ~(alignof(type) - 1) )) \
- /**/
-
////////////////////////////////////////////////////////////////////////////////
// Q_ARRAY_LITERAL
@@ -322,15 +371,16 @@ struct QArrayDataPointerRef
Q_ARRAY_LITERAL_CHECK_LITERAL_TYPE(Type); \
\
/* Portable compile-time array size computation */ \
- Q_CONSTEXPR Type data[] = { __VA_ARGS__ }; Q_UNUSED(data); \
+ static Type const data[] = { __VA_ARGS__ }; \
enum { Size = sizeof(data) / sizeof(data[0]) }; \
\
- static const QStaticArrayData<Type, Size> literal = { \
- Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(Type, Size), { __VA_ARGS__ } }; \
+ static const QArrayData literal = { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }; \
\
QArrayDataPointerRef<Type> ref = \
{ static_cast<QTypedArrayData<Type> *>( \
- const_cast<QArrayData *>(&literal.header)) }; \
+ const_cast<QArrayData *>(&literal)), \
+ const_cast<Type *>(data), \
+ Size }; \
/**/
namespace QtPrivate {
diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h
index 8e19525f07..0d2be5d51c 100644
--- a/src/corelib/tools/qarraydataops.h
+++ b/src/corelib/tools/qarraydataops.h
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -41,12 +42,15 @@
#define QARRAYDATAOPS_H
#include <QtCore/qarraydata.h>
+#include <QtCore/qcontainertools_impl.h>
#include <new>
#include <string.h>
QT_BEGIN_NAMESPACE
+template <class T> struct QArrayDataPointer;
+
namespace QtPrivate {
QT_WARNING_PUSH
@@ -56,48 +60,67 @@ QT_WARNING_DISABLE_GCC("-Wstringop-overflow")
template <class T>
struct QPodArrayOps
- : QTypedArrayData<T>
+ : public QArrayDataPointer<T>
{
+ typedef typename QArrayDataPointer<T>::parameter_type parameter_type;
+
void appendInitialize(size_t newSize)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(newSize > uint(this->size));
- Q_ASSERT(newSize <= this->alloc);
+ Q_ASSERT(newSize <= this->allocatedCapacity());
::memset(static_cast<void *>(this->end()), 0, (newSize - this->size) * sizeof(T));
this->size = int(newSize);
}
+ template<typename iterator>
+ void copyAppend(iterator b, iterator e, QtPrivate::IfIsForwardIterator<iterator> = true)
+ {
+ Q_ASSERT(this->isMutable() || b == e);
+ Q_ASSERT(!this->isShared() || b == e);
+ Q_ASSERT(std::distance(b, e) >= 0 && size_t(std::distance(b, e)) <= this->allocatedCapacity() - this->size);
+
+ T *iter = this->end();
+ for (; b != e; ++iter, ++b) {
+ new (iter) T(*b);
+ ++this->size;
+ }
+ }
+
void copyAppend(const T *b, const T *e)
{
- Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
- Q_ASSERT(b < e);
- Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+ Q_ASSERT(this->isMutable() || b == e);
+ Q_ASSERT(!this->isShared() || b == e);
+ Q_ASSERT(b <= e);
+ Q_ASSERT(size_t(e - b) <= this->allocatedCapacity() - this->size);
::memcpy(static_cast<void *>(this->end()), static_cast<const void *>(b),
(e - b) * sizeof(T));
this->size += e - b;
}
- void copyAppend(size_t n, const T &t)
+ void moveAppend(T *b, T *e)
+ { copyAppend(b, e); }
+
+ void copyAppend(size_t n, parameter_type t)
{
- Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
- Q_ASSERT(n <= this->alloc - uint(this->size));
+ Q_ASSERT(this->isMutable() || n == 0);
+ Q_ASSERT(!this->isShared() || n == 0);
+ Q_ASSERT(n <= uint(this->allocatedCapacity() - this->size));
T *iter = this->end();
const T *const end = iter + n;
for (; iter != end; ++iter)
- ::memcpy(iter, &t, sizeof(T));
+ *iter = t;
this->size += int(n);
}
void truncate(size_t newSize)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(newSize < size_t(this->size));
this->size = int(newSize);
@@ -106,7 +129,7 @@ struct QPodArrayOps
void destroyAll() // Call from destructors, ONLY!
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(this->ref.atomic.loadRelaxed() == 0);
+ Q_ASSERT(this->d->ref_.loadRelaxed() == 0);
// As this is to be called only from destructor, it doesn't need to be
// exception safe; size not updated.
@@ -115,11 +138,11 @@ struct QPodArrayOps
void insert(T *where, const T *b, const T *e)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
- Q_ASSERT(b < e);
+ Q_ASSERT(b <= e);
Q_ASSERT(e <= where || b > this->end()); // No overlap
- Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+ Q_ASSERT(size_t(e - b) <= this->allocatedCapacity() - this->size);
::memmove(static_cast<void *>(where + (e - b)), static_cast<void *>(where),
(static_cast<const T*>(this->end()) - where) * sizeof(T));
@@ -127,43 +150,97 @@ struct QPodArrayOps
this->size += (e - b);
}
+ void insert(T *where, size_t n, parameter_type t)
+ {
+ Q_ASSERT(!this->isShared());
+ Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
+ Q_ASSERT(this->allocatedCapacity() - this->size >= n);
+
+ ::memmove(static_cast<void *>(where + n), static_cast<void *>(where),
+ (static_cast<const T*>(this->end()) - where) * sizeof(T));
+ this->size += int(n); // PODs can't throw on copy
+ while (n--)
+ *where++ = t;
+ }
+
+ void insert(T *where, T &&t)
+ {
+ Q_ASSERT(!this->isShared());
+ Q_ASSERT(where >= this->begin() && where <= this->end());
+ Q_ASSERT(this->allocatedCapacity() - this->size >= 1);
+
+ ::memmove(static_cast<void *>(where + 1), static_cast<void *>(where),
+ (static_cast<const T*>(this->end()) - where) * sizeof(T));
+ this->size += 1;
+ new (where) T(std::move(t));
+ }
+
+
void erase(T *b, T *e)
{
Q_ASSERT(this->isMutable());
Q_ASSERT(b < e);
Q_ASSERT(b >= this->begin() && b < this->end());
- Q_ASSERT(e > this->begin() && e < this->end());
+ Q_ASSERT(e > this->begin() && e <= this->end());
::memmove(static_cast<void *>(b), static_cast<void *>(e),
(static_cast<T *>(this->end()) - e) * sizeof(T));
this->size -= (e - b);
}
+
+ void assign(T *b, T *e, parameter_type t)
+ {
+ Q_ASSERT(b <= e);
+ Q_ASSERT(b >= this->begin() && e <= this->end());
+
+ while (b != e)
+ ::memcpy(static_cast<void *>(b++), static_cast<const void *>(&t), sizeof(T));
+ }
+
+ bool compare(const T *begin1, const T *begin2, size_t n) const
+ {
+ // only use memcmp for fundamental types or pointers.
+ // Other types could have padding in the data structure or custom comparison
+ // operators that would break the comparison using memcmp
+ if (QArrayDataPointer<T>::pass_parameter_by_value)
+ return ::memcmp(begin1, begin2, n * sizeof(T)) == 0;
+ const T *end1 = begin1 + n;
+ while (begin1 != end1) {
+ if (*begin1 == *begin2)
+ ++begin1, ++begin2;
+ else
+ return false;
+ }
+ return true;
+ }
};
QT_WARNING_POP
template <class T>
struct QGenericArrayOps
- : QTypedArrayData<T>
+ : public QArrayDataPointer<T>
{
+ typedef typename QArrayDataPointer<T>::parameter_type parameter_type;
+
void appendInitialize(size_t newSize)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(newSize > uint(this->size));
- Q_ASSERT(newSize <= this->alloc);
+ Q_ASSERT(newSize <= this->allocatedCapacity());
- T *const begin = this->begin();
+ T *const b = this->begin();
do {
- new (begin + this->size) T;
+ new (b + this->size) T;
} while (uint(++this->size) != newSize);
}
- void copyAppend(const T *b, const T *e)
+ template<typename iterator>
+ void copyAppend(iterator b, iterator e, QtPrivate::IfIsForwardIterator<iterator> = true)
{
- Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
- Q_ASSERT(b < e);
- Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+ Q_ASSERT(this->isMutable() || b == e);
+ Q_ASSERT(!this->isShared() || b == e);
+ Q_ASSERT(std::distance(b, e) >= 0 && size_t(std::distance(b, e)) <= this->allocatedCapacity() - this->size);
T *iter = this->end();
for (; b != e; ++iter, ++b) {
@@ -172,11 +249,37 @@ struct QGenericArrayOps
}
}
- void copyAppend(size_t n, const T &t)
+ void copyAppend(const T *b, const T *e)
{
- Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
- Q_ASSERT(n <= this->alloc - uint(this->size));
+ Q_ASSERT(this->isMutable() || b == e);
+ Q_ASSERT(!this->isShared() || b == e);
+ Q_ASSERT(std::distance(b, e) >= 0 && size_t(std::distance(b, e)) <= this->allocatedCapacity() - this->size);
+
+ T *iter = this->end();
+ this->size += e - b;
+ for (; b != e; ++iter, ++b)
+ new (iter) T(*b);
+ }
+
+ void moveAppend(T *b, T *e)
+ {
+ Q_ASSERT(this->isMutable() || b == e);
+ Q_ASSERT(!this->isShared() || b == e);
+ Q_ASSERT(b <= e);
+ Q_ASSERT(size_t(e - b) <= this->allocatedCapacity() - this->size);
+
+ T *iter = this->end();
+ for (; b != e; ++iter, ++b) {
+ new (iter) T(std::move(*b));
+ ++this->size;
+ }
+ }
+
+ void copyAppend(size_t n, parameter_type t)
+ {
+ Q_ASSERT(this->isMutable() || n == 0);
+ Q_ASSERT(!this->isShared() || n == 0);
+ Q_ASSERT(n <= size_t(this->allocatedCapacity() - this->size));
T *iter = this->end();
const T *const end = iter + n;
@@ -189,7 +292,7 @@ struct QGenericArrayOps
void truncate(size_t newSize)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(newSize < size_t(this->size));
const T *const b = this->begin();
@@ -204,7 +307,7 @@ struct QGenericArrayOps
// As this is to be called only from destructor, it doesn't need to be
// exception safe; size not updated.
- Q_ASSERT(this->ref.atomic.loadRelaxed() == 0);
+ Q_ASSERT(this->d->ref_.loadRelaxed() == 0);
const T *const b = this->begin();
const T *i = this->end();
@@ -216,11 +319,11 @@ struct QGenericArrayOps
void insert(T *where, const T *b, const T *e)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
- Q_ASSERT(b < e);
+ Q_ASSERT(b <= e);
Q_ASSERT(e <= where || b > this->end()); // No overlap
- Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+ Q_ASSERT(size_t(e - b) <= this->allocatedCapacity() - this->size);
// Array may be truncated at where in case of exceptions
@@ -279,25 +382,139 @@ struct QGenericArrayOps
}
}
+ void insert(T *where, size_t n, parameter_type t)
+ {
+ Q_ASSERT(!this->isShared());
+ Q_ASSERT(where >= this->begin() && where <= this->end());
+ Q_ASSERT(this->allocatedCapacity() - this->size >= n);
+
+ // Array may be truncated at where in case of exceptions
+ T *const end = this->end();
+ const T *readIter = end;
+ T *writeIter = end + n;
+
+ const T *const step1End = where + qMax<size_t>(n, end - where);
+
+ struct Destructor
+ {
+ Destructor(T *&it)
+ : iter(&it)
+ , end(it)
+ {
+ }
+
+ void commit()
+ {
+ iter = &end;
+ }
+
+ ~Destructor()
+ {
+ for (; *iter != end; --*iter)
+ (*iter)->~T();
+ }
+
+ T **iter;
+ T *end;
+ } destroyer(writeIter);
+
+ // Construct new elements in array
+ do {
+ --readIter, --writeIter;
+ new (writeIter) T(*readIter);
+ } while (writeIter != step1End);
+
+ while (writeIter != end) {
+ --n, --writeIter;
+ new (writeIter) T(t);
+ }
+
+ destroyer.commit();
+ this->size += destroyer.end - end;
+
+ // Copy assign over existing elements
+ while (readIter != where) {
+ --readIter, --writeIter;
+ *writeIter = *readIter;
+ }
+
+ while (writeIter != where) {
+ --n, --writeIter;
+ *writeIter = t;
+ }
+ }
+
+ void insert(T *where, T &&t)
+ {
+ Q_ASSERT(!this->isShared());
+ Q_ASSERT(where >= this->begin() && where <= this->end());
+ Q_ASSERT(this->allocatedCapacity() - this->size >= 1);
+
+ // Array may be truncated at where in case of exceptions
+ T *const end = this->end();
+
+ if (where != end) {
+ // Move elements in array
+ T *readIter = end - 1;
+ T *writeIter = end;
+ new (writeIter) T(std::move(*readIter));
+ while (readIter > where) {
+ --readIter;
+ --writeIter;
+ *writeIter = std::move(*readIter);
+ }
+ *where = std::move(t);
+ } else {
+ new (where) T(std::move(t));
+ }
+
+ ++this->size;
+ }
+
void erase(T *b, T *e)
{
Q_ASSERT(this->isMutable());
Q_ASSERT(b < e);
Q_ASSERT(b >= this->begin() && b < this->end());
- Q_ASSERT(e > this->begin() && e < this->end());
+ Q_ASSERT(e > this->begin() && e <= this->end());
const T *const end = this->end();
- do {
+ // move (by assignment) the elements from e to end
+ // onto b to the new end
+ while (e != end) {
*b = *e;
++b, ++e;
- } while (e != end);
+ }
+ // destroy the final elements at the end
+ // here, b points to the new end and e to the actual end
do {
(--e)->~T();
--this->size;
} while (e != b);
}
+
+ void assign(T *b, T *e, parameter_type t)
+ {
+ Q_ASSERT(b <= e);
+ Q_ASSERT(b >= this->begin() && e <= this->end());
+
+ while (b != e)
+ *b++ = t;
+ }
+
+ bool compare(const T *begin1, const T *begin2, size_t n) const
+ {
+ const T *end1 = begin1 + n;
+ while (begin1 != end1) {
+ if (*begin1 == *begin2)
+ ++begin1, ++begin2;
+ else
+ return false;
+ }
+ return true;
+ }
};
template <class T>
@@ -308,15 +525,16 @@ struct QMovableArrayOps
// using QGenericArrayOps<T>::copyAppend;
// using QGenericArrayOps<T>::truncate;
// using QGenericArrayOps<T>::destroyAll;
+ typedef typename QGenericArrayOps<T>::parameter_type parameter_type;
void insert(T *where, const T *b, const T *e)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
- Q_ASSERT(b < e);
+ Q_ASSERT(b <= e);
Q_ASSERT(e <= where || b > this->end()); // No overlap
- Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+ Q_ASSERT(size_t(e - b) <= this->allocatedCapacity() - this->size);
// Provides strong exception safety guarantee,
// provided T::~T() nothrow
@@ -376,12 +594,79 @@ struct QMovableArrayOps
this->size += (e - b);
}
+ void insert(T *where, size_t n, parameter_type t)
+ {
+ Q_ASSERT(!this->isShared());
+ Q_ASSERT(where >= this->begin() && where <= this->end());
+ Q_ASSERT(this->allocatedCapacity() - this->size >= n);
+
+ // Provides strong exception safety guarantee,
+ // provided T::~T() nothrow
+
+ struct ReversibleDisplace
+ {
+ ReversibleDisplace(T *start, T *finish, size_t diff)
+ : begin(start)
+ , end(finish)
+ , displace(diff)
+ {
+ ::memmove(static_cast<void *>(begin + displace), static_cast<void *>(begin),
+ (end - begin) * sizeof(T));
+ }
+
+ void commit() { displace = 0; }
+
+ ~ReversibleDisplace()
+ {
+ if (displace)
+ ::memmove(static_cast<void *>(begin), static_cast<void *>(begin + displace),
+ (end - begin) * sizeof(T));
+ }
+
+ T *const begin;
+ T *const end;
+ size_t displace;
+
+ } displace(where, this->end(), n);
+
+ struct CopyConstructor
+ {
+ CopyConstructor(T *w) : where(w) {}
+
+ void copy(size_t count, parameter_type proto)
+ {
+ n = 0;
+ while (count--) {
+ new (where + n) T(proto);
+ ++n;
+ }
+ n = 0;
+ }
+
+ ~CopyConstructor()
+ {
+ while (n)
+ where[--n].~T();
+ }
+
+ T *const where;
+ size_t n;
+ } copier(where);
+
+ copier.copy(n, t);
+ displace.commit();
+ this->size += int(n);
+ }
+
+ // use moving insert
+ using QGenericArrayOps<T>::insert;
+
void erase(T *b, T *e)
{
Q_ASSERT(this->isMutable());
Q_ASSERT(b < e);
Q_ASSERT(b >= this->begin() && b < this->end());
- Q_ASSERT(e > this->begin() && e < this->end());
+ Q_ASSERT(e > this->begin() && e <= this->end());
struct Mover
{
@@ -405,11 +690,51 @@ struct QMovableArrayOps
int &size;
} mover(e, this->end(), this->size);
+ // destroy the elements we're erasing
do {
// Exceptions or not, dtor called once per instance
(--e)->~T();
} while (e != b);
}
+
+ void moveAppend(T *b, T *e)
+ {
+ Q_ASSERT(this->isMutable());
+ Q_ASSERT(!this->isShared());
+ Q_ASSERT(b <= e);
+ Q_ASSERT(e <= this->begin() || b > this->end()); // No overlap
+ Q_ASSERT(size_t(e - b) <= this->allocatedCapacity() - this->size);
+
+ // Provides strong exception safety guarantee,
+ // provided T::~T() nothrow
+
+ struct CopyConstructor
+ {
+ CopyConstructor(T *w) : where(w) {}
+
+ void copy(T *src, const T *const srcEnd)
+ {
+ n = 0;
+ for (; src != srcEnd; ++src) {
+ new (where + n) T(std::move(*src));
+ ++n;
+ }
+ n = 0;
+ }
+
+ ~CopyConstructor()
+ {
+ while (n)
+ where[--n].~T();
+ }
+
+ T *const where;
+ size_t n;
+ } copier(this->end());
+
+ copier.copy(b, e);
+ this->size += (e - b);
+ }
};
template <class T, class = void>
diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h
index b8a1ef5d68..a17cf4a101 100644
--- a/src/corelib/tools/qarraydatapointer.h
+++ b/src/corelib/tools/qarraydatapointer.h
@@ -41,6 +41,7 @@
#define QARRAYDATAPOINTER_H
#include <QtCore/qarraydataops.h>
+#include <QtCore/qcontainertools_impl.h>
QT_BEGIN_NAMESPACE
@@ -52,30 +53,40 @@ private:
typedef QArrayDataOps<T> DataOps;
public:
+ typedef typename Data::iterator iterator;
+ typedef typename Data::const_iterator const_iterator;
+ enum { pass_parameter_by_value = std::is_fundamental<T>::value || std::is_pointer<T>::value };
+
+ typedef typename std::conditional<pass_parameter_by_value, T, const T &>::type parameter_type;
+
QArrayDataPointer() noexcept
- : d(Data::sharedNull())
+ : d(Data::sharedNull()), ptr(Data::sharedNullData()), size(0)
+ {
+ }
+
+ QArrayDataPointer(const QArrayDataPointer &other) noexcept
+ : d(other.d), ptr(other.ptr), size(other.size)
{
+ other.d->ref();
}
- QArrayDataPointer(const QArrayDataPointer &other)
- : d(other.d->ref.ref()
- ? other.d
- : other.clone(other.d->cloneFlags()))
+ QArrayDataPointer(Data *header, T *adata, size_t n = 0) noexcept
+ : d(header), ptr(adata), size(int(n))
{
}
- explicit QArrayDataPointer(QTypedArrayData<T> *ptr)
- : d(ptr)
+ explicit QArrayDataPointer(QPair<QTypedArrayData<T> *, T *> adata, size_t n = 0)
+ : d(adata.first), ptr(adata.second), size(int(n))
{
- Q_CHECK_PTR(ptr);
+ Q_CHECK_PTR(d);
}
- QArrayDataPointer(QArrayDataPointerRef<T> ref)
- : d(ref.ptr)
+ QArrayDataPointer(QArrayDataPointerRef<T> dd) noexcept
+ : d(dd.ptr), ptr(dd.data), size(dd.size)
{
}
- QArrayDataPointer &operator=(const QArrayDataPointer &other)
+ QArrayDataPointer &operator=(const QArrayDataPointer &other) noexcept
{
QArrayDataPointer tmp(other);
this->swap(tmp);
@@ -83,9 +94,11 @@ public:
}
QArrayDataPointer(QArrayDataPointer &&other) noexcept
- : d(other.d)
+ : d(other.d), ptr(other.ptr), size(other.size)
{
other.d = Data::sharedNull();
+ other.ptr = Data::sharedNullData();
+ other.size = 0;
}
QArrayDataPointer &operator=(QArrayDataPointer &&other) noexcept
@@ -95,97 +108,142 @@ public:
return *this;
}
- DataOps &operator*() const
+ DataOps &operator*() noexcept
+ {
+ Q_ASSERT(d);
+ return *static_cast<DataOps *>(this);
+ }
+
+ DataOps *operator->() noexcept
+ {
+ Q_ASSERT(d);
+ return static_cast<DataOps *>(this);
+ }
+
+ const DataOps &operator*() const noexcept
{
Q_ASSERT(d);
- return *static_cast<DataOps *>(d);
+ return *static_cast<const DataOps *>(this);
}
- DataOps *operator->() const
+ const DataOps *operator->() const noexcept
{
Q_ASSERT(d);
- return static_cast<DataOps *>(d);
+ return static_cast<const DataOps *>(this);
}
~QArrayDataPointer()
{
- if (!d->ref.deref()) {
- if (d->isMutable())
+ if (!deref()) {
+ if (isMutable())
(*this)->destroyAll();
Data::deallocate(d);
}
}
- bool isNull() const
+ bool isNull() const noexcept
{
return d == Data::sharedNull();
}
- Data *data() const
- {
- return d;
- }
+ T *data() noexcept { return ptr; }
+ const T *data() const noexcept { return ptr; }
- bool needsDetach() const
- {
- return (!d->isMutable() || d->ref.isShared());
- }
+ iterator begin(iterator = iterator()) noexcept { return data(); }
+ iterator end(iterator = iterator()) noexcept { return data() + size; }
+ const_iterator begin(const_iterator = const_iterator()) const noexcept { return data(); }
+ const_iterator end(const_iterator = const_iterator()) const noexcept { return data() + size; }
+ const_iterator constBegin(const_iterator = const_iterator()) const noexcept { return data(); }
+ const_iterator constEnd(const_iterator = const_iterator()) const noexcept { return data() + size; }
void swap(QArrayDataPointer &other) noexcept
{
qSwap(d, other.d);
+ qSwap(ptr, other.ptr);
+ qSwap(size, other.size);
}
- void clear()
+ void clear() Q_DECL_NOEXCEPT_EXPR(std::is_nothrow_destructible<T>::value)
{
- QArrayDataPointer tmp(d);
- d = Data::sharedNull();
+ QArrayDataPointer tmp;
+ swap(tmp);
}
bool detach()
{
- if (needsDetach()) {
- Data *copy = clone(d->detachFlags());
- QArrayDataPointer old(d);
- d = copy;
+ if (d->needsDetach()) {
+ QPair<Data *, T *> copy = clone(d->detachFlags());
+ QArrayDataPointer old(d, ptr, size);
+ d = copy.first;
+ ptr = copy.second;
return true;
}
return false;
}
-private:
- Q_REQUIRED_RESULT Data *clone(QArrayData::AllocationOptions options) const
+ // forwards from QArrayData
+ size_t allocatedCapacity() noexcept { return d->allocatedCapacity(); }
+ size_t constAllocatedCapacity() const noexcept { return d->constAllocatedCapacity(); }
+ int refCounterValue() const noexcept { return d->refCounterValue(); }
+ bool ref() noexcept { return d->ref(); }
+ bool deref() noexcept { return d->deref(); }
+ bool isMutable() const noexcept { return d->isMutable(); }
+ bool isStatic() const noexcept { return d->isStatic(); }
+ bool isShared() const noexcept { return d->isShared(); }
+ bool isSharedWith(const QArrayDataPointer &other) const noexcept { return d && d == other.d; }
+ bool needsDetach() const noexcept { return d->needsDetach(); }
+ size_t detachCapacity(size_t newSize) const noexcept { return d->detachCapacity(newSize); }
+ typename Data::ArrayOptions &flags() noexcept { return reinterpret_cast<typename Data::ArrayOptions &>(d->flags); }
+ typename Data::ArrayOptions flags() const noexcept { return typename Data::ArrayOption(d->flags); }
+ typename Data::ArrayOptions detachFlags() const noexcept { return d->detachFlags(); }
+ typename Data::ArrayOptions cloneFlags() const noexcept { return d->cloneFlags(); }
+
+ void reallocate(uint alloc, typename Data::ArrayOptions options)
{
- Data *x = Data::allocate(d->detachCapacity(d->size), options);
- Q_CHECK_PTR(x);
- QArrayDataPointer copy(x);
+ auto pair = Data::reallocateUnaligned(d, ptr, alloc, options);
+ d = pair.first;
+ ptr = pair.second;
+ }
+ Data *d_ptr() { return d; }
- if (d->size)
- copy->copyAppend(d->begin(), d->end());
+private:
+ Q_REQUIRED_RESULT QPair<Data *, T *> clone(QArrayData::ArrayOptions options) const
+ {
+ QPair<Data *, T *> pair = Data::allocate(d->detachCapacity(size),
+ options);
+ Q_CHECK_PTR(pair.first);
+ QArrayDataPointer copy(pair.first, pair.second, 0);
+ if (size)
+ copy->copyAppend(begin(), end());
- Data *result = copy.d;
+ pair.first = copy.d;
copy.d = Data::sharedNull();
- return result;
+ return pair;
}
+protected:
Data *d;
+ T *ptr;
+
+public:
+ int size;
};
template <class T>
-inline bool operator==(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs)
+inline bool operator==(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs) noexcept
{
- return lhs.data() == rhs.data();
+ return lhs.data() == rhs.data() && lhs.size == rhs.size;
}
template <class T>
-inline bool operator!=(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs)
+inline bool operator!=(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs) noexcept
{
- return lhs.data() != rhs.data();
+ return lhs.data() != rhs.data() || lhs.size != rhs.size;
}
template <class T>
-inline void swap(QArrayDataPointer<T> &p1, QArrayDataPointer<T> &p2)
+inline void qSwap(QArrayDataPointer<T> &p1, QArrayDataPointer<T> &p2) noexcept
{
p1.swap(p2);
}
diff --git a/src/corelib/tools/qbitarray.cpp b/src/corelib/tools/qbitarray.cpp
index f0b81cce66..a1dd537d4d 100644
--- a/src/corelib/tools/qbitarray.cpp
+++ b/src/corelib/tools/qbitarray.cpp
@@ -342,6 +342,38 @@ QBitArray QBitArray::fromBits(const char *data, qsizetype size)
return result;
}
+/*!
+ \since 6.0
+
+ Returns the array of bit converted to an int. The conversion is based on \a endianness.
+ Converts up to the first 32 bits of the array to \c quint32 and returns it,
+ obeying \a endianness. If \a ok is not a null pointer, and the array has more
+ than 32 bits, \a ok is set to false and this function returns zero; otherwise,
+ it's set to true.
+*/
+quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcept
+{
+ const qsizetype _size = size();
+ if (_size > 32) {
+ if (ok)
+ *ok = false;
+ return 0;
+ }
+
+ if (ok)
+ *ok = true;
+
+ quint32 factor = 1;
+ quint32 total = 0;
+ for (qsizetype i = 0; i < _size; ++i, factor *= 2) {
+ const auto index = endianness == QSysInfo::Endian::LittleEndian ? i : (_size - i - 1);
+ if (testBit(index))
+ total += factor;
+ }
+
+ return total;
+}
+
/*! \fn bool QBitArray::isDetached() const
\internal
diff --git a/src/corelib/tools/qbitarray.h b/src/corelib/tools/qbitarray.h
index 9b0e931aca..ac3a8771d9 100644
--- a/src/corelib/tools/qbitarray.h
+++ b/src/corelib/tools/qbitarray.h
@@ -105,8 +105,10 @@ public:
const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; }
static QBitArray fromBits(const char *data, qsizetype len);
+ quint32 toUInt32(QSysInfo::Endian endianness, bool *ok = nullptr) const noexcept;
+
public:
- typedef QByteArray::DataPtr DataPtr;
+ typedef QByteArray::DataPointer DataPtr;
inline DataPtr &data_ptr() { return d.data_ptr(); }
};
diff --git a/src/corelib/tools/qeasingcurve.cpp b/src/corelib/tools/qeasingcurve.cpp
index d7f69d3e0a..1207ce0659 100644
--- a/src/corelib/tools/qeasingcurve.cpp
+++ b/src/corelib/tools/qeasingcurve.cpp
@@ -443,12 +443,12 @@ class QEasingCurvePrivate
public:
QEasingCurvePrivate()
: type(QEasingCurve::Linear),
- config(0),
+ config(nullptr),
func(&easeNone)
{ }
QEasingCurvePrivate(const QEasingCurvePrivate &other)
: type(other.type),
- config(other.config ? other.config->copy() : 0),
+ config(other.config ? other.config->copy() : nullptr),
func(other.func)
{ }
~QEasingCurvePrivate() { delete config; }
@@ -590,7 +590,7 @@ struct BezierEase : public QEasingCurveFunction
if (!(x < 1))
return 1;
- SingleCubicBezier *singleCubicBezier = 0;
+ SingleCubicBezier *singleCubicBezier = nullptr;
getBezierSegment(singleCubicBezier, x);
return evaluateSegmentForY(*singleCubicBezier, findTForX(*singleCubicBezier, x));
@@ -1097,7 +1097,7 @@ static QEasingCurve::EasingFunction curveToFunc(QEasingCurve::Type curve)
case QEasingCurve::CosineCurve:
return &easeCosineCurve;
default:
- return 0;
+ return nullptr;
};
}
@@ -1127,7 +1127,7 @@ static QEasingCurveFunction *curveToFunctionObject(QEasingCurve::Type type)
return new QEasingCurveFunction(type, qreal(0.3), qreal(1.0), qreal(1.70158));
}
- return 0;
+ return nullptr;
}
/*!
@@ -1417,7 +1417,7 @@ void QEasingCurvePrivate::setType_helper(QEasingCurve::Type newType)
tcbPoints = std::move(config->_tcbPoints);
delete config;
- config = 0;
+ config = nullptr;
}
if (isConfigFunction(newType) || (amp != -1.0) || (period != -1.0) || (overshoot != -1.0) ||
@@ -1431,11 +1431,11 @@ void QEasingCurvePrivate::setType_helper(QEasingCurve::Type newType)
config->_o = overshoot;
config->_bezierCurves = std::move(bezierCurves);
config->_tcbPoints = std::move(tcbPoints);
- func = 0;
+ func = nullptr;
} else if (newType != QEasingCurve::Custom) {
func = curveToFunc(newType);
}
- Q_ASSERT((func == 0) == (config != 0));
+ Q_ASSERT((func == nullptr) == (config != nullptr));
type = newType;
}
@@ -1482,7 +1482,7 @@ void QEasingCurve::setCustomType(EasingFunction func)
*/
QEasingCurve::EasingFunction QEasingCurve::customType() const
{
- return d_ptr->type == Custom ? d_ptr->func : 0;
+ return d_ptr->type == Custom ? d_ptr->func : nullptr;
}
/*!
@@ -1507,7 +1507,7 @@ QDebug operator<<(QDebug debug, const QEasingCurve &item)
{
QDebugStateSaver saver(debug);
debug << "type:" << item.d_ptr->type
- << "func:" << item.d_ptr->func;
+ << "func:" << reinterpret_cast<const void *>(item.d_ptr->func);
if (item.d_ptr->config) {
debug << QString::fromLatin1("period:%1").arg(item.d_ptr->config->_p, 0, 'f', 20)
<< QString::fromLatin1("amp:%1").arg(item.d_ptr->config->_a, 0, 'f', 20)
diff --git a/src/corelib/tools/qflatmap_p.h b/src/corelib/tools/qflatmap_p.h
new file mode 100644
index 0000000000..590b0d942f
--- /dev/null
+++ b/src/corelib/tools/qflatmap_p.h
@@ -0,0 +1,982 @@
+/****************************************************************************
+**
+** Copyright (C) 2020 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QFLATMAP_P_H
+#define QFLATMAP_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of a number of Qt sources files. This header file may change from
+// version to version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "qvector.h"
+
+#include <algorithm>
+#include <functional>
+#include <initializer_list>
+#include <iterator>
+#include <numeric>
+#include <type_traits>
+#include <utility>
+
+QT_BEGIN_NAMESPACE
+
+/*
+ QFlatMap provides an associative container backed by sorted sequential
+ containers. By default, QVector is used.
+
+ Keys and values are stored in two separate containers. This provides improved
+ cache locality for key iteration and makes keys() and values() fast
+ operations.
+
+ One can customize the underlying container type by passing the KeyContainer
+ and MappedContainer template arguments:
+ QFlatMap<float, int, std::less<float>, std::vector<float>, std::vector<int>>
+*/
+
+namespace Qt {
+
+struct OrderedUniqueRange_t {};
+constexpr OrderedUniqueRange_t OrderedUniqueRange = {};
+
+} // namespace Qt
+
+template <class Key, class T, class Compare>
+class QFlatMapValueCompare : protected Compare
+{
+public:
+ QFlatMapValueCompare() = default;
+ QFlatMapValueCompare(const Compare &key_compare)
+ : Compare(key_compare)
+ {
+ }
+
+ using value_type = std::pair<const Key, T>;
+ static constexpr bool is_comparator_noexcept = noexcept(
+ std::declval<Compare>()(std::declval<Key>(), std::declval<Key>()));
+
+ bool operator()(const value_type &lhs, const value_type &rhs) const
+ noexcept(is_comparator_noexcept)
+ {
+ return Compare::operator()(lhs.first, rhs.first);
+ }
+};
+
+template <class Key, class T,
+ class Compare = std::less<Key>,
+ class KeyContainer = QVector<Key>,
+ class MappedContainer = QVector<T>>
+class QFlatMap : private QFlatMapValueCompare<Key, T, Compare>
+{
+ using full_map_t = QFlatMap<Key, T, Compare, KeyContainer, MappedContainer>;
+
+ template <class U>
+ class mock_pointer
+ {
+ U ref;
+ public:
+ mock_pointer(U r)
+ : ref(r)
+ {
+ }
+
+ U *operator->()
+ {
+ return &ref;
+ }
+ };
+
+public:
+ using key_type = Key;
+ using mapped_type = T;
+ using value_compare = QFlatMapValueCompare<Key, T, Compare>;
+ using value_type = typename value_compare::value_type;
+ using key_container_type = KeyContainer;
+ using mapped_container_type = MappedContainer;
+ using size_type = typename key_container_type::size_type;
+ using key_compare = Compare;
+
+ struct containers
+ {
+ key_container_type keys;
+ mapped_container_type values;
+ };
+
+ class iterator
+ {
+ public:
+ using difference_type = ptrdiff_t;
+ using value_type = std::pair<const Key, T>;
+ using reference = std::pair<const Key &, T &>;
+ using pointer = mock_pointer<reference>;
+ using iterator_category = std::random_access_iterator_tag;
+
+ iterator() = default;
+
+ iterator(containers *ac, size_type ai)
+ : c(ac), i(ai)
+ {
+ }
+
+ reference operator*()
+ {
+ return { c->keys[i], c->values[i] };
+ }
+
+ pointer operator->()
+ {
+ return { operator*() };
+ }
+
+ bool operator==(const iterator &o) const
+ {
+ return c == o.c && i == o.i;
+ }
+
+ bool operator!=(const iterator &o) const
+ {
+ return !operator==(o);
+ }
+
+ iterator &operator++()
+ {
+ ++i;
+ return *this;
+ }
+
+ iterator operator++(int)
+ {
+
+ iterator r = *this;
+ i++;
+ return r;
+ }
+
+ iterator &operator--()
+ {
+ --i;
+ return *this;
+ }
+
+ iterator operator--(int)
+ {
+ iterator r = *this;
+ i--;
+ return r;
+ }
+
+ iterator &operator+=(size_type n)
+ {
+ i += n;
+ return *this;
+ }
+
+ friend iterator operator+(size_type n, const iterator a)
+ {
+ iterator ret = a;
+ return ret += n;
+ }
+
+ friend iterator operator+(const iterator a, size_type n)
+ {
+ return n + a;
+ }
+
+ iterator &operator-=(size_type n)
+ {
+ i -= n;
+ return *this;
+ }
+
+ friend iterator operator-(const iterator a, size_type n)
+ {
+ iterator ret = a;
+ return ret -= n;
+ }
+
+ friend difference_type operator-(const iterator b, const iterator a)
+ {
+ return b.i - a.i;
+ }
+
+ reference operator[](size_type n)
+ {
+ size_type k = i + n;
+ return { c->keys[k], c->values[k] };
+ }
+
+ bool operator<(const iterator &other) const
+ {
+ return i < other.i;
+ }
+
+ bool operator>(const iterator &other) const
+ {
+ return i > other.i;
+ }
+
+ bool operator<=(const iterator &other) const
+ {
+ return i <= other.i;
+ }
+
+ bool operator>=(const iterator &other) const
+ {
+ return i >= other.i;
+ }
+
+ const Key &key() const { return c->keys[i]; }
+ T &value() { return c->values[i]; }
+
+ private:
+ containers *c = nullptr;
+ size_type i = 0;
+ friend full_map_t;
+ };
+
+ class const_iterator
+ {
+ public:
+ using difference_type = ptrdiff_t;
+ using value_type = std::pair<const Key, const T>;
+ using reference = std::pair<const Key &, const T &>;
+ using pointer = mock_pointer<reference>;
+ using iterator_category = std::random_access_iterator_tag;
+
+ const_iterator() = default;
+
+ const_iterator(const containers *ac, size_type ai)
+ : c(ac), i(ai)
+ {
+ }
+
+ const_iterator(iterator o)
+ : c(o.c), i(o.i)
+ {
+ }
+
+ reference operator*()
+ {
+ return { c->keys[i], c->values[i] };
+ }
+
+ pointer operator->()
+ {
+ return { operator*() };
+ }
+
+ bool operator==(const const_iterator &o) const
+ {
+ return c == o.c && i == o.i;
+ }
+
+ bool operator!=(const const_iterator &o) const
+ {
+ return !operator==(o);
+ }
+
+ const_iterator &operator++()
+ {
+ ++i;
+ return *this;
+ }
+
+ const_iterator operator++(int)
+ {
+
+ const_iterator r = *this;
+ i++;
+ return r;
+ }
+
+ const_iterator &operator--()
+ {
+ --i;
+ return *this;
+ }
+
+ const_iterator operator--(int)
+ {
+ const_iterator r = *this;
+ i--;
+ return r;
+ }
+
+ const_iterator &operator+=(size_type n)
+ {
+ i += n;
+ return *this;
+ }
+
+ friend const_iterator operator+(size_type n, const const_iterator a)
+ {
+ const_iterator ret = a;
+ return ret += n;
+ }
+
+ friend const_iterator operator+(const const_iterator a, size_type n)
+ {
+ return n + a;
+ }
+
+ const_iterator &operator-=(size_type n)
+ {
+ i -= n;
+ return *this;
+ }
+
+ friend const_iterator operator-(const const_iterator a, size_type n)
+ {
+ const_iterator ret = a;
+ return ret -= n;
+ }
+
+ friend difference_type operator-(const const_iterator b, const const_iterator a)
+ {
+ return b.i - a.i;
+ }
+
+ reference operator[](size_type n)
+ {
+ size_type k = i + n;
+ return { c->keys[k], c->values[k] };
+ }
+
+ bool operator<(const const_iterator &other) const
+ {
+ return i < other.i;
+ }
+
+ bool operator>(const const_iterator &other) const
+ {
+ return i > other.i;
+ }
+
+ bool operator<=(const const_iterator &other) const
+ {
+ return i <= other.i;
+ }
+
+ bool operator>=(const const_iterator &other) const
+ {
+ return i >= other.i;
+ }
+
+ const Key &key() const { return c->keys[i]; }
+ const T &value() { return c->values[i]; }
+
+ private:
+ const containers *c = nullptr;
+ size_type i = 0;
+ friend full_map_t;
+ };
+
+private:
+ template <class, class = void>
+ struct is_marked_transparent_type : std::false_type { };
+
+ template <class X>
+ struct is_marked_transparent_type<X, typename X::is_transparent> : std::true_type { };
+
+ template <class X>
+ using is_marked_transparent = typename std::enable_if<
+ is_marked_transparent_type<X>::value>::type *;
+
+ template <typename It>
+ using is_compatible_iterator = typename std::enable_if<
+ std::is_same<value_type, typename std::iterator_traits<It>::value_type>::value>::type *;
+
+public:
+ QFlatMap() = default;
+
+ explicit QFlatMap(const key_container_type &keys, const mapped_container_type &values)
+ : c{keys, values}
+ {
+ ensureOrderedUnique();
+ }
+
+ explicit QFlatMap(key_container_type &&keys, const mapped_container_type &values)
+ {
+ c.keys = std::move(keys);
+ c.values = values;
+ ensureOrderedUnique();
+ }
+
+ explicit QFlatMap(const key_container_type &keys, mapped_container_type &&values)
+ {
+ c.keys = keys;
+ c.values = std::move(values);
+ ensureOrderedUnique();
+ }
+
+ explicit QFlatMap(key_container_type &&keys, mapped_container_type &&values)
+ {
+ c.keys = std::move(keys);
+ c.values = std::move(values);
+ ensureOrderedUnique();
+ }
+
+ explicit QFlatMap(std::initializer_list<value_type> lst)
+ : QFlatMap(lst.begin(), lst.end())
+ {
+ }
+
+ template <class InputIt, is_compatible_iterator<InputIt> = nullptr>
+ explicit QFlatMap(InputIt first, InputIt last)
+ {
+ initWithRange(first, last);
+ ensureOrderedUnique();
+ }
+
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, const key_container_type &keys,
+ const mapped_container_type &values)
+ {
+ c.keys = keys;
+ c.values = values;
+ }
+
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, key_container_type &&keys,
+ const mapped_container_type &values)
+ {
+ c.keys = std::move(keys);
+ c.values = values;
+ }
+
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, const key_container_type &keys,
+ mapped_container_type &&values)
+ {
+ c.keys = keys;
+ c.values = std::move(values);
+ }
+
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, key_container_type &&keys,
+ mapped_container_type &&values)
+ {
+ c.keys = std::move(keys);
+ c.values = std::move(values);
+ }
+
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, std::initializer_list<value_type> lst)
+ : QFlatMap(lst.begin(), lst.end())
+ {
+ }
+
+ template <class InputIt, is_compatible_iterator<InputIt> = nullptr>
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, InputIt first, InputIt last)
+ {
+ initWithRange(first, last);
+ }
+
+ explicit QFlatMap(const Compare &compare)
+ : value_compare(compare)
+ {
+ }
+
+ explicit QFlatMap(const key_container_type &keys, const mapped_container_type &values,
+ const Compare &compare)
+ : value_compare(compare), c{keys, values}
+ {
+ ensureOrderedUnique();
+ }
+
+ explicit QFlatMap(key_container_type &&keys, const mapped_container_type &values,
+ const Compare &compare)
+ : value_compare(compare), c{std::move(keys), values}
+ {
+ ensureOrderedUnique();
+ }
+
+ explicit QFlatMap(const key_container_type &keys, mapped_container_type &&values,
+ const Compare &compare)
+ : value_compare(compare), c{keys, std::move(values)}
+ {
+ ensureOrderedUnique();
+ }
+
+ explicit QFlatMap(key_container_type &&keys, mapped_container_type &&values,
+ const Compare &compare)
+ : value_compare(compare), c{std::move(keys), std::move(values)}
+ {
+ ensureOrderedUnique();
+ }
+
+ explicit QFlatMap(std::initializer_list<value_type> lst, const Compare &compare)
+ : QFlatMap(lst.begin(), lst.end(), compare)
+ {
+ }
+
+ template <class InputIt, is_compatible_iterator<InputIt> = nullptr>
+ explicit QFlatMap(InputIt first, InputIt last, const Compare &compare)
+ : value_compare(compare)
+ {
+ initWithRange(first, last);
+ ensureOrderedUnique();
+ }
+
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, const key_container_type &keys,
+ const mapped_container_type &values, const Compare &compare)
+ : value_compare(compare), c{keys, values}
+ {
+ }
+
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, key_container_type &&keys,
+ const mapped_container_type &values, const Compare &compare)
+ : value_compare(compare), c{std::move(keys), values}
+ {
+ }
+
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, const key_container_type &keys,
+ mapped_container_type &&values, const Compare &compare)
+ : value_compare(compare), c{keys, std::move(values)}
+ {
+ }
+
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, key_container_type &&keys,
+ mapped_container_type &&values, const Compare &compare)
+ : value_compare(compare), c{std::move(keys), std::move(values)}
+ {
+ }
+
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, std::initializer_list<value_type> lst,
+ const Compare &compare)
+ : QFlatMap(Qt::OrderedUniqueRange, lst.begin(), lst.end(), compare)
+ {
+ }
+
+ template <class InputIt, is_compatible_iterator<InputIt> = nullptr>
+ explicit QFlatMap(Qt::OrderedUniqueRange_t, InputIt first, InputIt last, const Compare &compare)
+ : value_compare(compare)
+ {
+ initWithRange(first, last);
+ }
+
+ size_type count() const noexcept { return c.keys.size(); }
+ size_type size() const noexcept { return c.keys.size(); }
+ size_type capacity() const noexcept { return c.keys.capacity(); }
+ bool isEmpty() const noexcept { return c.keys.empty(); }
+ bool empty() const noexcept { return c.keys.empty(); }
+ containers extract() && { return std::move(c); }
+ const key_container_type &keys() const noexcept { return c.keys; }
+ const mapped_container_type &values() const noexcept { return c.values; }
+
+ void reserve(size_type s)
+ {
+ c.keys.reserve(s);
+ c.values.reserve(s);
+ }
+
+ void clear()
+ {
+ c.keys.clear();
+ c.values.clear();
+ }
+
+ bool remove(const Key &key)
+ {
+ auto it = binary_find(key);
+ if (it != end()) {
+ c.keys.erase(toKeysIterator(it));
+ c.values.erase(toValuesIterator(it));
+ return true;
+ }
+ return false;
+ }
+
+ iterator erase(iterator it)
+ {
+ c.values.erase(toValuesIterator(it));
+ return fromKeysIterator(c.keys.erase(toKeysIterator(it)));
+ }
+
+ T take(const Key &key)
+ {
+ auto it = binary_find(key);
+ if (it != end()) {
+ T result = std::move(it.value());
+ erase(it);
+ return result;
+ }
+ return {};
+ }
+
+ bool contains(const Key &key) const
+ {
+ return binary_find(key) != end();
+ }
+
+ T value(const Key &key, const T &defaultValue) const
+ {
+ auto it = binary_find(key);
+ return it == end() ? defaultValue : it.value();
+ }
+
+ T value(const Key &key) const
+ {
+ auto it = binary_find(key);
+ return it == end() ? T() : it.value();
+ }
+
+ T &operator[](const Key &key)
+ {
+ auto it = lower_bound(key);
+ if (it == end() || key_compare::operator()(key, it.key())) {
+ c.keys.insert(toKeysIterator(it), key);
+ return *c.values.insert(toValuesIterator(it), T());
+ }
+ return it.value();
+ }
+
+ T &operator[](Key &&key)
+ {
+ auto it = lower_bound(key);
+ if (it == end() || key_compare::operator()(key, it.key())) {
+ c.keys.insert(toKeysIterator(it), key);
+ return *c.values.insert(toValuesIterator(it), T());
+ }
+ return it.value();
+ }
+
+ T operator[](const Key &key) const
+ {
+ return value(key);
+ }
+
+ std::pair<iterator, bool> insert(const Key &key, const T &value)
+ {
+ auto it = lower_bound(key);
+ if (it == end() || key_compare::operator()(key, it.key())) {
+ c.values.insert(toValuesIterator(it), value);
+ auto k = c.keys.insert(toKeysIterator(it), key);
+ return { fromKeysIterator(k), true };
+ } else {
+ it.value() = value;
+ return {it, false};
+ }
+ }
+
+ std::pair<iterator, bool> insert(Key &&key, const T &value)
+ {
+ auto it = lower_bound(key);
+ if (it == end() || key_compare::operator()(key, it.key())) {
+ c.values.insert(toValuesIterator(it), value);
+ return { c.keys.insert(it, std::move(key)), true };
+ } else {
+ *toValuesIterator(it) = value;
+ return {it, false};
+ }
+ }
+
+ std::pair<iterator, bool> insert(const Key &key, T &&value)
+ {
+ auto it = lower_bound(key);
+ if (it == end() || key_compare::operator()(key, it.key())) {
+ c.values.insert(toValuesIterator(it), std::move(value));
+ return { c.keys.insert(it, key), true };
+ } else {
+ *toValuesIterator(it) = std::move(value);
+ return {it, false};
+ }
+ }
+
+ std::pair<iterator, bool> insert(Key &&key, T &&value)
+ {
+ auto it = lower_bound(key);
+ if (it == end() || key_compare::operator()(key, it.key())) {
+ c.values.insert(toValuesIterator(it), std::move(value));
+ return { fromKeysIterator(c.keys.insert(toKeysIterator(it), std::move(key))), true };
+ } else {
+ *toValuesIterator(it) = std::move(value);
+ return {it, false};
+ }
+ }
+
+ template <class InputIt, is_compatible_iterator<InputIt> = nullptr>
+ void insert(InputIt first, InputIt last)
+ {
+ insertRange(first, last);
+ }
+
+ // ### Merge with the templated version above
+ // once we can use std::disjunction in is_compatible_iterator.
+ void insert(const value_type *first, const value_type *last)
+ {
+ insertRange(first, last);
+ }
+
+ template <class InputIt, is_compatible_iterator<InputIt> = nullptr>
+ void insert(Qt::OrderedUniqueRange_t, InputIt first, InputIt last)
+ {
+ insertOrderedUniqueRange(first, last);
+ }
+
+ // ### Merge with the templated version above
+ // once we can use std::disjunction in is_compatible_iterator.
+ void insert(Qt::OrderedUniqueRange_t, const value_type *first, const value_type *last)
+ {
+ insertOrderedUniqueRange(first, last);
+ }
+
+ iterator begin() { return { &c, 0 }; }
+ const_iterator begin() const { return { &c, 0 }; }
+ const_iterator cbegin() const { return begin(); }
+ const_iterator constBegin() const { return cbegin(); }
+ iterator end() { return { &c, c.keys.size() }; }
+ const_iterator end() const { return { &c, c.keys.size() }; }
+ const_iterator cend() const { return end(); }
+ const_iterator constEnd() const { return cend(); }
+ std::reverse_iterator<iterator> rbegin() { return std::reverse_iterator<iterator>(end()); }
+ std::reverse_iterator<const_iterator> rbegin() const
+ {
+ return std::reverse_iterator<const_iterator>(end());
+ }
+ std::reverse_iterator<const_iterator> crbegin() const { return rbegin(); }
+ std::reverse_iterator<iterator> rend() {
+ return std::reverse_iterator<iterator>(begin());
+ }
+ std::reverse_iterator<const_iterator> rend() const
+ {
+ return std::reverse_iterator<const_iterator>(begin());
+ }
+ std::reverse_iterator<const_iterator> crend() const { return rend(); }
+
+ iterator lower_bound(const Key &key)
+ {
+ auto cit = const_cast<const full_map_t *>(this)->lower_bound(key);
+ return { &c, cit.i };
+ }
+
+ template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
+ iterator lower_bound(const X &key)
+ {
+ auto cit = const_cast<const full_map_t *>(this)->lower_bound(key);
+ return { &c, cit.i };
+ }
+
+ const_iterator lower_bound(const Key &key) const
+ {
+ return fromKeysIterator(std::lower_bound(c.keys.begin(), c.keys.end(), key, key_comp()));
+ }
+
+ template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
+ const_iterator lower_bound(const X &key) const
+ {
+ return fromKeysIterator(std::lower_bound(c.keys.begin(), c.keys.end(), key, key_comp()));
+ }
+
+ iterator find(const key_type &k)
+ {
+ return binary_find(k);
+ }
+
+ const_iterator find(const key_type &k) const
+ {
+ return binary_find(k);
+ }
+
+ key_compare key_comp() const noexcept
+ {
+ return static_cast<key_compare>(*this);
+ }
+
+ value_compare value_comp() const noexcept
+ {
+ return static_cast<value_compare>(*this);
+ }
+
+private:
+ template <class InputIt, is_compatible_iterator<InputIt> = nullptr>
+ void initWithRange(InputIt first, InputIt last)
+ {
+ QtPrivate::reserveIfForwardIterator(this, first, last);
+ while (first != last) {
+ c.keys.push_back(first->first);
+ c.values.push_back(first->second);
+ ++first;
+ }
+ }
+
+ iterator fromKeysIterator(typename key_container_type::iterator kit)
+ {
+ return { &c, static_cast<size_type>(std::distance(c.keys.begin(), kit)) };
+ }
+
+ const_iterator fromKeysIterator(typename key_container_type::const_iterator kit) const
+ {
+ return { &c, static_cast<size_type>(std::distance(c.keys.begin(), kit)) };
+ }
+
+ typename key_container_type::iterator toKeysIterator(iterator it)
+ {
+ return c.keys.begin() + it.i;
+ }
+
+ typename mapped_container_type::iterator toValuesIterator(iterator it)
+ {
+ return c.values.begin() + it.i;
+ }
+
+ template <class InputIt>
+ void insertRange(InputIt first, InputIt last)
+ {
+ size_type i = c.keys.size();
+ c.keys.resize(i + std::distance(first, last));
+ c.values.resize(c.keys.size());
+ for (; first != last; ++first, ++i) {
+ c.keys[i] = first->first;
+ c.values[i] = first->second;
+ }
+ ensureOrderedUnique();
+ }
+
+ class IndexedKeyComparator
+ {
+ public:
+ IndexedKeyComparator(const full_map_t *am)
+ : m(am)
+ {
+ }
+
+ bool operator()(size_type i, size_type k) const
+ {
+ return m->key_comp()(m->c.keys[i], m->c.keys[k]);
+ }
+
+ private:
+ const full_map_t *m;
+ };
+
+ template <class InputIt>
+ void insertOrderedUniqueRange(InputIt first, InputIt last)
+ {
+ const size_type s = c.keys.size();
+ c.keys.resize(s + std::distance(first, last));
+ c.values.resize(c.keys.size());
+ for (size_type i = s; first != last; ++first, ++i) {
+ c.keys[i] = first->first;
+ c.values[i] = first->second;
+ }
+
+ std::vector<size_type> p(size_t(c.keys.size()));
+ std::iota(p.begin(), p.end(), 0);
+ std::inplace_merge(p.begin(), p.begin() + s, p.end(), IndexedKeyComparator(this));
+ applyPermutation(p);
+ makeUnique();
+ }
+
+ iterator binary_find(const Key &key)
+ {
+ return { &c, const_cast<const full_map_t *>(this)->binary_find(key).i };
+ }
+
+ const_iterator binary_find(const Key &key) const
+ {
+ auto it = lower_bound(key);
+ if (it != end()) {
+ if (!key_compare::operator()(key, it.key()))
+ return it;
+ it = end();
+ }
+ return it;
+ }
+
+ void ensureOrderedUnique()
+ {
+ std::vector<size_type> p(size_t(c.keys.size()));
+ std::iota(p.begin(), p.end(), 0);
+ std::stable_sort(p.begin(), p.end(), IndexedKeyComparator(this));
+ applyPermutation(p);
+ makeUnique();
+ }
+
+ void applyPermutation(const std::vector<size_type> &p)
+ {
+ const size_type s = c.keys.size();
+ std::vector<bool> done(s);
+ for (size_type i = 0; i < s; ++i) {
+ if (done[i])
+ continue;
+ done[i] = true;
+ size_type j = i;
+ size_type k = p[i];
+ while (i != k) {
+ qSwap(c.keys[j], c.keys[k]);
+ qSwap(c.values[j], c.values[k]);
+ done[k] = true;
+ j = k;
+ k = p[j];
+ }
+ }
+ }
+
+ void makeUnique()
+ {
+ if (c.keys.size() < 2)
+ return;
+ auto k = std::end(c.keys) - 1;
+ auto i = k - 1;
+ for (;;) {
+ if (key_compare::operator()(*i, *k) || key_compare::operator()(*k, *i)) {
+ if (i == std::begin(c.keys))
+ break;
+ --i;
+ --k;
+ } else {
+ c.values.erase(std::begin(c.values) + std::distance(std::begin(c.keys), i));
+ i = c.keys.erase(i);
+ if (i == std::begin(c.keys))
+ break;
+ k = i + 1;
+ }
+ }
+ c.keys.shrink_to_fit();
+ c.values.shrink_to_fit();
+ }
+
+ containers c;
+};
+
+QT_END_NAMESPACE
+
+#endif // QFLATMAP_P_H
diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp
index a53d6db997..15f731a603 100644
--- a/src/corelib/tools/qhash.cpp
+++ b/src/corelib/tools/qhash.cpp
@@ -1120,21 +1120,6 @@ uint qHash(long double key, uint seed) noexcept
\snippet code/src_corelib_tools_qhash.cpp 9
- However, you can store multiple values per key by using
- insertMulti() instead of insert() (or using the convenience
- subclass QMultiHash). If you want to retrieve all
- the values for a single key, you can use values(const Key &key),
- which returns a QList<T>:
-
- \snippet code/src_corelib_tools_qhash.cpp 10
-
- The items that share the same key are available from most
- recently to least recently inserted. A more efficient approach is
- to call find() to get the iterator for the first item with a key
- and iterate from there:
-
- \snippet code/src_corelib_tools_qhash.cpp 11
-
If you only need to extract the values from a hash (not the keys),
you can also use \l{foreach}:
@@ -1435,9 +1420,8 @@ uint qHash(long double key, uint seed) noexcept
/*! \fn template <class Key, class T> int QHash<Key, T>::remove(const Key &key)
Removes all the items that have the \a key from the hash.
- Returns the number of items removed which is usually 1 but will
- be 0 if the key isn't in the hash, or greater than 1 if
- insertMulti() has been used with the \a key.
+ Returns the number of items removed which is 1 if the key exists in the hash,
+ and 0 otherwise.
\sa clear(), take(), QMultiHash::remove()
*/
@@ -1507,27 +1491,25 @@ uint qHash(long double key, uint seed) noexcept
/*! \fn template <class Key, class T> QList<Key> QHash<Key, T>::uniqueKeys() const
\since 4.2
+ \obsolete
Returns a list containing all the keys in the map. Keys that occur multiple
times in the map (because items were inserted with insertMulti(), or
unite() was used) occur only once in the returned list.
- \sa keys(), values()
+ \sa QMultiHash::uniqueKeys()
*/
/*! \fn template <class Key, class T> QList<Key> QHash<Key, T>::keys() const
Returns a list containing all the keys in the hash, in an
arbitrary order. Keys that occur multiple times in the hash
- (because items were inserted with insertMulti(), or unite() was
- used) also occur multiple times in the list.
-
- To obtain a list of unique keys, where each key from the map only
- occurs once, use uniqueKeys().
+ (because the method is operating on a QMultiHash) also occur
+ multiple times in the list.
The order is guaranteed to be the same as that used by values().
- \sa uniqueKeys(), values(), key()
+ \sa QMultiMap::uniqueKeys(), values(), key()
*/
/*! \fn template <class Key, class T> QList<Key> QHash<Key, T>::keys(const T &value) const
@@ -1555,7 +1537,7 @@ uint qHash(long double key, uint seed) noexcept
*/
/*! \fn template <class Key, class T> QList<T> QHash<Key, T>::values(const Key &key) const
-
+ \obsolete
\overload
Returns a list of all the values associated with the \a key,
@@ -1592,6 +1574,7 @@ uint qHash(long double key, uint seed) noexcept
*/
/*! \fn template <class Key, class T> int QHash<Key, T>::count(const Key &key) const
+ \obsolete
Returns the number of items associated with the \a key.
@@ -1803,11 +1786,22 @@ uint qHash(long double key, uint seed) noexcept
If there are multiple items with the \a key, the most
recently inserted item's value is replaced with \a value.
+*/
+
+/*! \fn template <class Key, class T> void QHash<Key, T>::insert(const QHash &other)
+ \since 5.15
+
+ Inserts all the items in the \a other hash into this hash.
+
+ If a key is common to both hashes, its value will be replaced with the
+ value stored in \a other.
- \sa insertMulti()
+ \note If \a other contains multiple entries with the same key then the
+ final value of the key is undefined.
*/
/*! \fn template <class Key, class T> QHash<Key, T>::iterator QHash<Key, T>::insertMulti(const Key &key, const T &value)
+ \obsolete
Inserts a new item with the \a key and a value of \a value.
@@ -1816,16 +1810,17 @@ uint qHash(long double key, uint seed) noexcept
different from insert(), which overwrites the value of an
existing item.)
+ This function is obsolete. Use QMultiHash or QMultiMap instead.
+
\sa insert(), values()
*/
/*! \fn template <class Key, class T> QHash &QHash<Key, T>::unite(const QHash &other)
+ \obsolete
Inserts all the items in the \a other hash into this hash. If a
key is common to both hashes, the resulting hash will contain the
key multiple times.
-
- \sa insertMulti()
*/
/*! \fn template <class Key, class T> bool QHash<Key, T>::empty() const
@@ -1966,10 +1961,7 @@ uint qHash(long double key, uint seed) noexcept
\snippet code/src_corelib_tools_qhash.cpp 17
Unlike QMap, which orders its items by key, QHash stores its
- items in an arbitrary order. The only guarantee is that items that
- share the same key (because they were inserted using
- QHash::insertMulti()) will appear consecutively, from the most
- recently to the least recently inserted value.
+ items in an arbitrary order.
Let's see a few examples of things we can do with a
QHash::iterator that we cannot do with a QHash::const_iterator.
@@ -2036,7 +2028,7 @@ uint qHash(long double key, uint seed) noexcept
There is no direct way of changing an item's key through an
iterator, although it can be done by calling QHash::erase()
- followed by QHash::insert() or QHash::insertMulti().
+ followed by QHash::insert().
\sa value()
*/
@@ -2198,7 +2190,7 @@ uint qHash(long double key, uint seed) noexcept
Unlike QMap, which orders its items by key, QHash stores its
items in an arbitrary order. The only guarantee is that items that
share the same key (because they were inserted using
- QHash::insertMulti()) will appear consecutively, from the most
+ a QMultiHash) will appear consecutively, from the most
recently to the least recently inserted value.
Multiple iterators can be used on the same hash. However, be aware
@@ -2531,18 +2523,22 @@ uint qHash(long double key, uint seed) noexcept
It inherits QHash and extends it with a few convenience functions
that make it more suitable than QHash for storing multi-valued
hashes. A multi-valued hash is a hash that allows multiple values
- with the same key; QHash normally doesn't allow that, unless you
- call QHash::insertMulti().
+ with the same key.
Because QMultiHash inherits QHash, all of QHash's functionality also
applies to QMultiHash. For example, you can use isEmpty() to test
whether the hash is empty, and you can traverse a QMultiHash using
- QHash's iterator classes (for example, QHashIterator). But in
- addition, it provides an insert() function that corresponds to
- QHash::insertMulti(), and a replace() function that corresponds to
+ QHash's iterator classes (for example, QHashIterator). But opposed to
+ QHash, it provides an insert() function will allow the insertion of
+ multiple items with the same key. The replace() function corresponds to
QHash::insert(). It also provides convenient operator+() and
operator+=().
+ Unlike QMultiMap, QMultiHash does not provide and ordering of the
+ inserted items. The only guarantee is that items that
+ share the same key will appear consecutively, from the most
+ recently to the least recently inserted value.
+
Example:
\snippet code/src_corelib_tools_qhash.cpp 24
@@ -2597,7 +2593,7 @@ uint qHash(long double key, uint seed) noexcept
\sa operator=()
*/
-/*! \fn template <class Key, class T> template <class InputIterator> QMultiHash::QMultiHash(InputIterator begin, InputIterator end)
+/*! \fn template <class Key, class T> template <class InputIterator> QMultiHash<Key, T>::QMultiHash(InputIterator begin, InputIterator end)
\since 5.14
Constructs a multi-hash with a copy of each of the elements in the iterator range
@@ -2633,7 +2629,8 @@ uint qHash(long double key, uint seed) noexcept
\sa replace()
*/
-/*! \fn template <class Key, class T> QMultiHash &QMultiHash<Key, T>::operator+=(const QMultiHash &other)
+/*! \fn template <class Key, class T> QMultiHash &QMultiHash<Key, T>::unite(const QMultiHash &other)
+ \since 5.13
Inserts all the items in the \a other hash into this hash
and returns a reference to this hash.
@@ -2641,6 +2638,32 @@ uint qHash(long double key, uint seed) noexcept
\sa insert()
*/
+/*! \fn template <class Key, class T> QList<Key> QMultiHash<Key, T>::uniqueKeys() const
+ \since 5.13
+
+ Returns a list containing all the keys in the map. Keys that occur multiple
+ times in the map occur only once in the returned list.
+
+ \sa keys(), values()
+*/
+
+/*! \fn template <class Key, class T> QList<T> QMultiHash<Key, T>::values(const Key &key) const
+ \overload
+
+ Returns a list of all the values associated with the \a key,
+ from the most recently inserted to the least recently inserted.
+
+ \sa count(), insert()
+*/
+
+/*! \fn template <class Key, class T> QMultiHash &QMultiHash<Key, T>::operator+=(const QMultiHash &other)
+
+ Inserts all the items in the \a other hash into this hash
+ and returns a reference to this hash.
+
+ \sa unite(), insert()
+*/
+
/*! \fn template <class Key, class T> QMultiHash QMultiHash<Key, T>::operator+(const QMultiHash &other) const
Returns a hash that contains all the items in this hash in
@@ -2670,6 +2693,13 @@ uint qHash(long double key, uint seed) noexcept
\sa QHash::remove()
*/
+/*! \fn template <class Key, class T> int QMultiHash<Key, T>::count(const Key &key) const
+
+ Returns the number of items associated with the \a key.
+
+ \sa contains(), insert()
+*/
+
/*!
\fn template <class Key, class T> int QMultiHash<Key, T>::count(const Key &key, const T &value) const
\since 4.3
diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h
index 9fd96686f5..c56324eef7 100644
--- a/src/corelib/tools/qhash.h
+++ b/src/corelib/tools/qhash.h
@@ -305,12 +305,14 @@ public:
T &operator[](const Key &key);
const T operator[](const Key &key) const;
- QList<Key> uniqueKeys() const;
QList<Key> keys() const;
QList<Key> keys(const T &value) const;
QList<T> values() const;
- QList<T> values(const Key &key) const;
- int count(const Key &key) const;
+#if QT_DEPRECATED_SINCE(5, 15)
+ QT_DEPRECATED_X("Use QMultiHash for hashes storing multiple values with the same key.") QList<Key> uniqueKeys() const;
+ QT_DEPRECATED_X("Use QMultiHash for hashes storing multiple values with the same key.") QList<T> values(const Key &key) const;
+ QT_DEPRECATED_X("Use QMultiHash for hashes storing multiple values with the same key.") int count(const Key &key) const;
+#endif
class const_iterator;
@@ -322,7 +324,11 @@ public:
QHashData::Node *i;
public:
+#if QT_DEPRECATED_SINCE(5, 15)
typedef std::bidirectional_iterator_tag iterator_category;
+#else
+ typedef std::forward_iterator_tag iterator_category;
+#endif
typedef qptrdiff difference_type;
typedef T value_type;
typedef T *pointer;
@@ -347,21 +353,25 @@ public:
i = QHashData::nextNode(i);
return r;
}
- inline iterator &operator--() {
+#if QT_DEPRECATED_SINCE(5, 15)
+ inline QT_DEPRECATED iterator &operator--()
+ {
i = QHashData::previousNode(i);
return *this;
}
- inline iterator operator--(int) {
+ inline QT_DEPRECATED iterator operator--(int)
+ {
iterator r = *this;
i = QHashData::previousNode(i);
return r;
}
- inline iterator operator+(int j) const
+ inline QT_DEPRECATED iterator operator+(int j) const
{ iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; }
- inline iterator operator-(int j) const { return operator+(-j); }
- inline iterator &operator+=(int j) { return *this = *this + j; }
- inline iterator &operator-=(int j) { return *this = *this - j; }
- friend inline iterator operator+(int j, iterator k) { return k + j; }
+ inline QT_DEPRECATED iterator operator-(int j) const { return operator+(-j); }
+ inline QT_DEPRECATED iterator &operator+=(int j) { return *this = *this + j; }
+ inline QT_DEPRECATED iterator &operator-=(int j) { return *this = *this - j; }
+ friend inline QT_DEPRECATED iterator operator+(int j, iterator k) { return k + j; }
+#endif
inline bool operator==(const const_iterator &o) const { return i == o.i; }
inline bool operator!=(const const_iterator &o) const { return i != o.i; }
@@ -372,11 +382,16 @@ public:
{
friend class iterator;
friend class QHash<Key, T>;
+ friend class QMultiHash<Key, T>;
friend class QSet<Key>;
QHashData::Node *i;
public:
+#if QT_DEPRECATED_SINCE(5, 15)
typedef std::bidirectional_iterator_tag iterator_category;
+#else
+ typedef std::forward_iterator_tag iterator_category;
+#endif
typedef qptrdiff difference_type;
typedef T value_type;
typedef const T *pointer;
@@ -404,21 +419,28 @@ public:
i = QHashData::nextNode(i);
return r;
}
- inline const_iterator &operator--() {
+#if QT_DEPRECATED_SINCE(5, 15)
+ inline QT_DEPRECATED const_iterator &operator--()
+ {
i = QHashData::previousNode(i);
return *this;
}
- inline const_iterator operator--(int) {
+ inline QT_DEPRECATED const_iterator operator--(int)
+ {
const_iterator r = *this;
i = QHashData::previousNode(i);
return r;
}
- inline const_iterator operator+(int j) const
+ inline QT_DEPRECATED const_iterator operator+(int j) const
{ const_iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; }
- inline const_iterator operator-(int j) const { return operator+(-j); }
- inline const_iterator &operator+=(int j) { return *this = *this + j; }
- inline const_iterator &operator-=(int j) { return *this = *this - j; }
- friend inline const_iterator operator+(int j, const_iterator k) { return k + j; }
+ inline QT_DEPRECATED const_iterator operator-(int j) const { return operator+(-j); }
+ inline QT_DEPRECATED const_iterator &operator+=(int j) { return *this = *this + j; }
+ inline QT_DEPRECATED const_iterator &operator-=(int j) { return *this = *this - j; }
+ friend inline QT_DEPRECATED const_iterator operator+(int j, const_iterator k)
+ {
+ return k + j;
+ }
+#endif
};
friend class const_iterator;
@@ -443,8 +465,14 @@ public:
inline key_iterator &operator++() { ++i; return *this; }
inline key_iterator operator++(int) { return key_iterator(i++);}
- inline key_iterator &operator--() { --i; return *this; }
- inline key_iterator operator--(int) { return key_iterator(i--); }
+#if QT_DEPRECATED_SINCE(5, 15)
+ inline QT_DEPRECATED key_iterator &operator--()
+ {
+ --i;
+ return *this;
+ }
+ inline QT_DEPRECATED key_iterator operator--(int) { return key_iterator(i--); }
+#endif
const_iterator base() const { return i; }
};
@@ -482,8 +510,11 @@ public:
const_iterator find(const Key &key) const;
const_iterator constFind(const Key &key) const;
iterator insert(const Key &key, const T &value);
- iterator insertMulti(const Key &key, const T &value);
- QHash &unite(const QHash &other);
+ void insert(const QHash &hash);
+#if QT_DEPRECATED_SINCE(5, 15)
+ QT_DEPRECATED_X("Use QMultiHash for hashes storing multiple values with the same key.") iterator insertMulti(const Key &key, const T &value);
+ QT_DEPRECATED_X("Use QMultiHash for hashes storing multiple values with the same key.") QHash &unite(const QHash &other);
+#endif
// STL compatibility
typedef T mapped_type;
@@ -525,6 +556,7 @@ private:
#endif
}
friend class QSet<Key>;
+ friend class QMultiHash<Key, T>;
};
@@ -563,22 +595,6 @@ QHash<Key, T>::createNode(uint ah, const Key &akey, const T &avalue, Node **anex
}
template <class Key, class T>
-Q_INLINE_TEMPLATE QHash<Key, T> &QHash<Key, T>::unite(const QHash &other)
-{
- if (d == &QHashData::shared_null) {
- *this = other;
- } else {
- QHash copy(other);
- const_iterator it = copy.constEnd();
- while (it != copy.constBegin()) {
- --it;
- insertMulti(it.key(), it.value());
- }
- }
- return *this;
-}
-
-template <class Key, class T>
Q_OUTOFLINE_TEMPLATE void QHash<Key, T>::freeData(QHashData *x)
{
x->free_helper(deleteNode2);
@@ -637,26 +653,6 @@ Q_INLINE_TEMPLATE const T QHash<Key, T>::value(const Key &akey, const T &adefaul
}
template <class Key, class T>
-Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::uniqueKeys() const
-{
- QList<Key> res;
- res.reserve(size()); // May be too much, but assume short lifetime
- const_iterator i = begin();
- if (i != end()) {
- for (;;) {
- const Key &aKey = i.key();
- res.append(aKey);
- do {
- if (++i == end())
- goto break_out_of_outer_loop;
- } while (aKey == i.key());
- }
- }
-break_out_of_outer_loop:
- return res;
-}
-
-template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::keys() const
{
QList<Key> res;
@@ -715,32 +711,6 @@ Q_OUTOFLINE_TEMPLATE QList<T> QHash<Key, T>::values() const
}
template <class Key, class T>
-Q_OUTOFLINE_TEMPLATE QList<T> QHash<Key, T>::values(const Key &akey) const
-{
- QList<T> res;
- Node *node = *findNode(akey);
- if (node != e) {
- do {
- res.append(node->value);
- } while ((node = node->next) != e && node->key == akey);
- }
- return res;
-}
-
-template <class Key, class T>
-Q_OUTOFLINE_TEMPLATE int QHash<Key, T>::count(const Key &akey) const
-{
- int cnt = 0;
- Node *node = *findNode(akey);
- if (node != e) {
- do {
- ++cnt;
- } while ((node = node->next) != e && node->key == akey);
- }
- return cnt;
-}
-
-template <class Key, class T>
Q_INLINE_TEMPLATE const T QHash<Key, T>::operator[](const Key &akey) const
{
return value(akey);
@@ -781,15 +751,28 @@ Q_INLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::insert(const K
}
template <class Key, class T>
-Q_INLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::insertMulti(const Key &akey,
- const T &avalue)
+Q_INLINE_TEMPLATE void QHash<Key, T>::insert(const QHash &hash)
{
+ if (d == hash.d)
+ return;
+
detach();
- d->willGrow();
- uint h;
- Node **nextNode = findNode(akey, &h);
- return iterator(createNode(h, akey, avalue, nextNode));
+ QHashData::Node *i = hash.d->firstNode();
+ QHashData::Node *end = reinterpret_cast<QHashData::Node *>(hash.e);
+ while (i != end) {
+ Node *n = concrete(i);
+ Node **node = findNode(n->key, n->h);
+ if (*node == e) {
+ if (d->willGrow())
+ node = findNode(n->key, n->h);
+ createNode(n->h, n->key, n->value, node);
+ } else {
+ if (!std::is_same<T, QHashDummyValue>::value)
+ (*node)->value = n->value;
+ }
+ i = QHashData::nextNode(i);
+ }
}
template <class Key, class T>
@@ -1042,11 +1025,12 @@ public:
inline typename QHash<Key, T>::iterator replace(const Key &key, const T &value)
{ return QHash<Key, T>::insert(key, value); }
- inline typename QHash<Key, T>::iterator insert(const Key &key, const T &value)
- { return QHash<Key, T>::insertMulti(key, value); }
+ typename QHash<Key, T>::iterator insert(const Key &key, const T &value);
+
+ inline QMultiHash &unite(const QMultiHash &other);
inline QMultiHash &operator+=(const QMultiHash &other)
- { this->unite(other); return *this; }
+ { return unite(other); }
inline QMultiHash operator+(const QMultiHash &other) const
{ QMultiHash result = *this; result += other; return result; }
@@ -1055,13 +1039,27 @@ public:
using QHash<Key, T>::count;
using QHash<Key, T>::find;
using QHash<Key, T>::constFind;
+ using QHash<Key, T>::values;
+ using QHash<Key, T>::findNode;
+ using QHash<Key, T>::createNode;
+ using QHash<Key, T>::concrete;
+ using QHash<Key, T>::detach;
+
+ using typename QHash<Key, T>::Node;
+ using typename QHash<Key, T>::iterator;
+ using typename QHash<Key, T>::const_iterator;
bool contains(const Key &key, const T &value) const;
int remove(const Key &key, const T &value);
+ int count(const Key &key) const;
int count(const Key &key, const T &value) const;
+ QList<Key> uniqueKeys() const;
+
+ QList<T> values(const Key &akey) const;
+
typename QHash<Key, T>::iterator find(const Key &key, const T &value) {
typename QHash<Key, T>::iterator i(find(key));
typename QHash<Key, T>::iterator end(this->end());
@@ -1090,6 +1088,50 @@ private:
};
template <class Key, class T>
+Q_INLINE_TEMPLATE typename QHash<Key, T>::iterator QMultiHash<Key, T>::insert(const Key &akey, const T &avalue)
+{
+ detach();
+ this->d->willGrow();
+
+ uint h;
+ Node **nextNode = findNode(akey, &h);
+ return iterator(createNode(h, akey, avalue, nextNode));
+}
+
+template <class Key, class T>
+Q_INLINE_TEMPLATE QMultiHash<Key, T> &QMultiHash<Key, T>::unite(const QMultiHash &other)
+{
+ if (this->d == &QHashData::shared_null) {
+ *this = other;
+ } else {
+#if QT_DEPRECATED_SINCE(5, 15)
+ QMultiHash copy(other);
+ const_iterator it = copy.constEnd();
+ while (it != copy.constBegin()) {
+ it.i = QHashData::previousNode(it.i);
+ insert(it.key(), it.value());
+ }
+#else
+ const QMultiHash copy(other);
+ const_iterator it = copy.cbegin();
+ const const_iterator end = copy.cend();
+ while (it != end) {
+ const auto rangeStart = it++;
+ while (it != end && rangeStart.key() == it.key())
+ ++it;
+ const qint64 last = std::distance(rangeStart, it) - 1;
+ for (qint64 i = last; i >= 0; --i) {
+ auto next = std::next(rangeStart, i);
+ insert(next.key(), next.value());
+ }
+ }
+#endif
+ }
+ return *this;
+}
+
+
+template <class Key, class T>
Q_INLINE_TEMPLATE bool QMultiHash<Key, T>::contains(const Key &key, const T &value) const
{
return constFind(key, value) != QHash<Key, T>::constEnd();
@@ -1126,8 +1168,257 @@ Q_INLINE_TEMPLATE int QMultiHash<Key, T>::count(const Key &key, const T &value)
return n;
}
-Q_DECLARE_ASSOCIATIVE_ITERATOR(Hash)
-Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(Hash)
+template <class Key, class T>
+Q_OUTOFLINE_TEMPLATE QList<Key> QMultiHash<Key, T>::uniqueKeys() const
+{
+ QList<Key> res;
+ res.reserve(QHash<Key, T>::size()); // May be too much, but assume short lifetime
+ typename QHash<Key, T>::const_iterator i = QHash<Key, T>::begin();
+ if (i != QHash<Key, T>::end()) {
+ for (;;) {
+ const Key &aKey = i.key();
+ res.append(aKey);
+ do {
+ if (++i == QHash<Key, T>::end())
+ goto break_out_of_outer_loop;
+ } while (aKey == i.key());
+ }
+ }
+break_out_of_outer_loop:
+ return res;
+}
+
+#if QT_DEPRECATED_SINCE(5, 15)
+
+template <class Key, class T>
+Q_OUTOFLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::insertMulti(const Key &key, const T &value) {
+ return static_cast<QMultiHash<Key, T> *>(this)->insert(key, value);
+}
+
+template <class Key, class T>
+Q_OUTOFLINE_TEMPLATE QHash<Key, T> &QHash<Key, T>::unite(const QHash &other) {
+ return static_cast<QMultiHash<Key, T> *>(this)->unite(other);
+}
+
+template <class Key, class T>
+Q_OUTOFLINE_TEMPLATE QList<T> QHash<Key, T>::values(const Key &akey) const
+{
+ return static_cast<const QMultiHash<Key, T> *>(this)->values(akey);
+}
+
+template <class Key, class T>
+Q_OUTOFLINE_TEMPLATE int QHash<Key, T>::count(const Key &akey) const
+{
+ return static_cast<const QMultiHash<Key, T> *>(this)->count(akey);
+}
+
+template <class Key, class T>
+Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::uniqueKeys() const
+{
+ return static_cast<const QMultiHash<Key, T> *>(this)->uniqueKeys();
+}
+#endif
+
+template <class Key, class T>
+Q_OUTOFLINE_TEMPLATE QList<T> QMultiHash<Key, T>::values(const Key &akey) const
+{
+ QList<T> res;
+ Node *node = *findNode(akey);
+ if (node != this->e) {
+ do {
+ res.append(node->value);
+ } while ((node = node->next) != this->e && node->key == akey);
+ }
+ return res;
+}
+
+template <class Key, class T>
+Q_OUTOFLINE_TEMPLATE int QMultiHash<Key, T>::count(const Key &akey) const
+{
+ int cnt = 0;
+ Node *node = *findNode(akey);
+ if (node != this->e) {
+ do {
+ ++cnt;
+ } while ((node = node->next) != this->e && node->key == akey);
+ }
+ return cnt;
+}
+
+template <class Key, class T>
+class QHashIterator
+{
+ typedef typename QHash<Key, T>::const_iterator const_iterator;
+ typedef const_iterator Item;
+ QHash<Key, T> c;
+ const_iterator i, n;
+ inline bool item_exists() const { return n != c.constEnd(); }
+
+public:
+ inline QHashIterator(const QHash<Key, T> &container)
+ : c(container), i(c.constBegin()), n(c.constEnd())
+ {
+ }
+ inline QHashIterator &operator=(const QHash<Key, T> &container)
+ {
+ c = container;
+ i = c.constBegin();
+ n = c.constEnd();
+ return *this;
+ }
+ inline void toFront()
+ {
+ i = c.constBegin();
+ n = c.constEnd();
+ }
+ inline void toBack()
+ {
+ i = c.constEnd();
+ n = c.constEnd();
+ }
+ inline bool hasNext() const { return i != c.constEnd(); }
+ inline Item next()
+ {
+ n = i++;
+ return n;
+ }
+ inline Item peekNext() const { return i; }
+ inline const T &value() const
+ {
+ Q_ASSERT(item_exists());
+ return *n;
+ }
+ inline const Key &key() const
+ {
+ Q_ASSERT(item_exists());
+ return n.key();
+ }
+ inline bool findNext(const T &t)
+ {
+ while ((n = i) != c.constEnd())
+ if (*i++ == t)
+ return true;
+ return false;
+ }
+#if QT_DEPRECATED_SINCE(5, 15)
+ inline QT_DEPRECATED bool hasPrevious() const { return i != c.constBegin(); }
+ inline QT_DEPRECATED Item previous()
+ {
+ n = --i;
+ return n;
+ }
+ inline QT_DEPRECATED Item peekPrevious() const
+ {
+ const_iterator p = i;
+ return --p;
+ }
+ inline bool QT_DEPRECATED findPrevious(const T &t)
+ {
+ while (i != c.constBegin())
+ if (*(n = --i) == t)
+ return true;
+ n = c.constEnd();
+ return false;
+ }
+#endif
+};
+
+template<class Key, class T>
+class QMutableHashIterator
+{
+ typedef typename QHash<Key, T>::iterator iterator;
+ typedef typename QHash<Key, T>::const_iterator const_iterator;
+ typedef iterator Item;
+ QHash<Key, T> *c;
+ iterator i, n;
+ inline bool item_exists() const { return const_iterator(n) != c->constEnd(); }
+
+public:
+ inline QMutableHashIterator(QHash<Key, T> &container) : c(&container)
+ {
+ i = c->begin();
+ n = c->end();
+ }
+ inline QMutableHashIterator &operator=(QHash<Key, T> &container)
+ {
+ c = &container;
+ i = c->begin();
+ n = c->end();
+ return *this;
+ }
+ inline void toFront()
+ {
+ i = c->begin();
+ n = c->end();
+ }
+ inline void toBack()
+ {
+ i = c->end();
+ n = c->end();
+ }
+ inline bool hasNext() const { return const_iterator(i) != c->constEnd(); }
+ inline Item next()
+ {
+ n = i++;
+ return n;
+ }
+ inline Item peekNext() const { return i; }
+ inline void remove()
+ {
+ if (const_iterator(n) != c->constEnd()) {
+ i = c->erase(n);
+ n = c->end();
+ }
+ }
+ inline void setValue(const T &t)
+ {
+ if (const_iterator(n) != c->constEnd())
+ *n = t;
+ }
+ inline T &value()
+ {
+ Q_ASSERT(item_exists());
+ return *n;
+ }
+ inline const T &value() const
+ {
+ Q_ASSERT(item_exists());
+ return *n;
+ }
+ inline const Key &key() const
+ {
+ Q_ASSERT(item_exists());
+ return n.key();
+ }
+ inline bool findNext(const T &t)
+ {
+ while (const_iterator(n = i) != c->constEnd())
+ if (*i++ == t)
+ return true;
+ return false;
+ }
+#if QT_DEPRECATED_SINCE(5, 15)
+ inline QT_DEPRECATED bool hasPrevious() const { return const_iterator(i) != c->constBegin(); }
+ inline QT_DEPRECATED Item previous()
+ {
+ n = --i;
+ return n;
+ }
+ inline QT_DEPRECATED Item peekPrevious() const
+ {
+ iterator p = i;
+ return --p;
+ }
+ inline QT_DEPRECATED bool findPrevious(const T &t)
+ {
+ while (const_iterator(i) != c->constBegin())
+ if (*(n = --i) == t)
+ return true;
+ n = c->end();
+ return false;
+ }
+#endif
+};
template <class Key, class T>
uint qHash(const QHash<Key, T> &key, uint seed = 0)
diff --git a/src/corelib/tools/qline.cpp b/src/corelib/tools/qline.cpp
index 3afd23d76b..dde66ed093 100644
--- a/src/corelib/tools/qline.cpp
+++ b/src/corelib/tools/qline.cpp
@@ -374,7 +374,7 @@ QDataStream &operator>>(QDataStream &stream, QLine &line)
*/
/*!
- \enum QLineF::IntersectionType
+ \enum QLineF::IntersectType
Describes the intersection between two lines.
diff --git a/src/corelib/tools/qmap.cpp b/src/corelib/tools/qmap.cpp
index a0ec372f9a..d747a8cda4 100644
--- a/src/corelib/tools/qmap.cpp
+++ b/src/corelib/tools/qmap.cpp
@@ -468,10 +468,9 @@ void QMapDataBase::freeData(QMapDataBase *d)
\snippet code/src_corelib_tools_qmap.cpp 9
However, you can store multiple values per key by using
- insertMulti() instead of insert() (or using the convenience
- subclass QMultiMap). If you want to retrieve all the values for a
- single key, you can use values(const Key &key), which returns a
- QList<T>:
+ using the subclass QMultiMap. If you want
+ to retrieve all the values for a single key, you can use
+ values(const Key &key), which returns a QList<T>:
\snippet code/src_corelib_tools_qmap.cpp 10
@@ -676,9 +675,8 @@ void QMapDataBase::freeData(QMapDataBase *d)
/*! \fn template <class Key, class T> int QMap<Key, T>::remove(const Key &key)
Removes all the items that have the key \a key from the map.
- Returns the number of items removed which is usually 1 but will be
- 0 if the key isn't in the map, or \> 1 if insertMulti() has been
- used with the \a key.
+ Returns the number of items removed which will be 1 if the key
+ exists in the map, and 0 otherwise.
\sa clear(), take(), QMultiMap::remove()
*/
@@ -742,28 +740,26 @@ void QMapDataBase::freeData(QMapDataBase *d)
/*! \fn template <class Key, class T> QList<Key> QMap<Key, T>::uniqueKeys() const
\since 4.2
+ \obsolete
Returns a list containing all the keys in the map in ascending
order. Keys that occur multiple times in the map (because items
were inserted with insertMulti(), or unite() was used) occur only
once in the returned list.
- \sa keys(), values()
+ \sa QMultiMap::uniqueKeys()
*/
/*! \fn template <class Key, class T> QList<Key> QMap<Key, T>::keys() const
Returns a list containing all the keys in the map in ascending
- order. Keys that occur multiple times in the map (because items
- were inserted with insertMulti(), or unite() was used) also
- occur multiple times in the list.
-
- To obtain a list of unique keys, where each key from the map only
- occurs once, use uniqueKeys().
+ order. Keys that occur multiple times in the map (because the
+ method is operating on a QMultiMap) also occur multiple times
+ in the list.
The order is guaranteed to be the same as that used by values().
- \sa uniqueKeys(), values(), key()
+ \sa QMultiMap::uniqueKeys(), values(), key()
*/
/*! \fn template <class Key, class T> QList<Key> QMap<Key, T>::keys(const T &value) const
@@ -806,6 +802,7 @@ void QMapDataBase::freeData(QMapDataBase *d)
*/
/*! \fn template <class Key, class T> QList<T> QMap<Key, T>::values(const Key &key) const
+ \obsolete
\overload
@@ -813,14 +810,15 @@ void QMapDataBase::freeData(QMapDataBase *d)
\a key, from the most recently inserted to the least recently
inserted one.
- \sa count(), insertMulti()
+ \sa QMultiMap::values()
*/
/*! \fn template <class Key, class T> int QMap<Key, T>::count(const Key &key) const
+ \obsolete
Returns the number of items associated with key \a key.
- \sa contains(), insertMulti(), QMultiMap::count()
+ \sa QMultiMap::count()
*/
/*! \fn template <class Key, class T> int QMap<Key, T>::count() const
@@ -1118,7 +1116,7 @@ void QMapDataBase::freeData(QMapDataBase *d)
If there are multiple items with the key \a key, the most
recently inserted item's value is replaced with \a value.
- \sa insertMulti()
+ \sa QMultiMap::insert()
*/
/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::insert(const_iterator pos, const Key &key, const T &value)
@@ -1147,10 +1145,25 @@ void QMapDataBase::freeData(QMapDataBase *d)
\b {Note:} Be careful with the hint. Providing an iterator from an older shared instance might
crash but there is also a risk that it will silently corrupt both the map and the \a pos map.
- \sa insertMulti()
+ \sa QMultiMap::insert()
+*/
+
+/*! \fn template <class Key, class T> void QMap<Key, T>::insert(const QMap<Key, T> &map)
+ \since 5.15
+
+ Inserts all the items in \a map into this map.
+
+ If a key is common to both maps, its value will be replaced with
+ the value stored in \a map.
+
+ \note If \a map contains multiple entries with the same key then the
+ final value of the key is undefined.
+
+ \sa QMultiMap::insert()
*/
/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::insertMulti(const Key &key, const T &value)
+ \obsolete
Inserts a new item with the key \a key and a value of \a value.
@@ -1159,12 +1172,13 @@ void QMapDataBase::freeData(QMapDataBase *d)
different from insert(), which overwrites the value of an
existing item.)
- \sa insert(), values()
+ \sa QMultiMap::insert()
*/
/*! \fn template <class Key, class T> QMap<Key, T>::iterator QMap<Key, T>::insertMulti(const_iterator pos, const Key &key, const T &value)
\overload
\since 5.1
+ \obsolete
Inserts a new item with the key \a key and value \a value and with hint \a pos
suggesting where to do the insert.
@@ -1178,17 +1192,18 @@ void QMapDataBase::freeData(QMapDataBase *d)
\b {Note:} Be careful with the hint. Providing an iterator from an older shared instance might
crash but there is also a risk that it will silently corrupt both the map and the \a pos map.
- \sa insert()
+ \sa QMultiMap::insert()
*/
/*! \fn template <class Key, class T> QMap<Key, T> &QMap<Key, T>::unite(const QMap<Key, T> &other)
+ \obsolete
Inserts all the items in the \a other map into this map. If a
key is common to both maps, the resulting map will contain the
key multiple times.
- \sa insertMulti()
+ \sa QMultiMap::unite()
*/
/*! \typedef QMap::Iterator
@@ -1271,9 +1286,8 @@ void QMapDataBase::freeData(QMapDataBase *d)
Unlike QHash, which stores its items in an arbitrary order, QMap
stores its items ordered by key. Items that share the same key
- (because they were inserted using QMap::insertMulti(), or due to a
- unite()) will appear consecutively, from the most recently to the
- least recently inserted value.
+ (because the map is a QMultiMap) will appear consecutively,
+ from the most recently to the least recently inserted value.
Let's see a few examples of things we can do with a
QMap::iterator that we cannot do with a QMap::const_iterator.
@@ -1519,9 +1533,8 @@ void QMapDataBase::freeData(QMapDataBase *d)
Unlike QHash, which stores its items in an arbitrary order, QMap
stores its items ordered by key. Items that share the same key
- (because they were inserted using QMap::insertMulti()) will
- appear consecutively, from the most recently to the least
- recently inserted value.
+ (because the map is a QMultiMap) will appear consecutively,
+ from the most recently to the least recently inserted value.
Multiple iterators can be used on the same map. If you add items
to the map, existing iterators will remain valid. If you remove
@@ -1893,20 +1906,20 @@ void QMapDataBase::freeData(QMapDataBase *d)
\reentrant
QMultiMap\<Key, T\> is one of Qt's generic \l{container classes}.
- It inherits QMap and extends it with a few convenience functions
- that make it more suitable than QMap for storing multi-valued
- maps. A multi-valued map is a map that allows multiple values
- with the same key; QMap normally doesn't allow that, unless you
- call QMap::insertMulti().
+ It inherits QMap and extends it with a few functions
+ that make it able to store multi-valued maps. A multi-valued map
+ is a map that allows multiple values with the same key; QMap
+ doesn't allow that.
Because QMultiMap inherits QMap, all of QMap's functionality also
applies to QMultiMap. For example, you can use isEmpty() to test
whether the map is empty, and you can traverse a QMultiMap using
QMap's iterator classes (for example, QMapIterator). But in
- addition, it provides an insert() function that corresponds to
- QMap::insertMulti(), and a replace() function that corresponds to
- QMap::insert(). It also provides convenient operator+() and
- operator+=().
+ addition, it provides an insert() function that inserts but does
+ not overwrite any previous value if the key already exists,
+ and a replace() function that corresponds which does overwite
+ an existing value if they key is already in the map.
+ It also provides convenient operator+() and operator+=().
Example:
\snippet code/src_corelib_tools_qmap.cpp 25
@@ -2095,4 +2108,24 @@ void QMapDataBase::freeData(QMapDataBase *d)
\sa QMap::constFind()
*/
+/*! \fn template <class Key, class T> QList<T> QMultiMap<Key, T>::values(const Key &key) const
+
+ Returns a list containing all the values associated with key
+ \a key, from the most recently inserted to the least recently
+ inserted one.
+*/
+
+/*! \fn template <class Key, class T> int QMultiMap<Key, T>::count(const Key &key) const
+
+ Returns the number of items associated with key \a key.
+*/
+
+/*! \fn template <class Key, class T> QList<Key> QMultiMap<Key, T>::uniqueKeys() const
+ \since 4.2
+
+ Returns a list containing all the keys in the map in ascending
+ order. Keys that occur multiple times in the map occur only
+ once in the returned list.
+*/
+
QT_END_NAMESPACE
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h
index f169ed5e49..a70b355c67 100644
--- a/src/corelib/tools/qmap.h
+++ b/src/corelib/tools/qmap.h
@@ -369,12 +369,15 @@ public:
T &operator[](const Key &key);
const T operator[](const Key &key) const;
- QList<Key> uniqueKeys() const;
QList<Key> keys() const;
QList<Key> keys(const T &value) const;
QList<T> values() const;
- QList<T> values(const Key &key) const;
- int count(const Key &key) const;
+#if QT_DEPRECATED_SINCE(5, 15)
+ QT_DEPRECATED_X("Use QMultiMap for maps storing multiple values with the same key.") QList<Key> uniqueKeys() const;
+ QT_DEPRECATED_X("Use QMultiMap for maps storing multiple values with the same key.") QList<T> values(const Key &key) const;
+ QT_DEPRECATED_X("Use QMultiMap for maps storing multiple values with the same key.") int count(const Key &key) const;
+#endif
+
inline const Key &firstKey() const { Q_ASSERT(!isEmpty()); return constBegin().key(); }
inline const Key &lastKey() const { Q_ASSERT(!isEmpty()); return (constEnd() - 1).key(); }
@@ -436,6 +439,7 @@ public:
inline bool operator==(const const_iterator &o) const { return i == o.i; }
inline bool operator!=(const const_iterator &o) const { return i != o.i; }
friend class QMap<Key, T>;
+ friend class QMultiMap<Key, T>;
};
friend class iterator;
@@ -488,6 +492,7 @@ public:
friend inline const_iterator operator+(int j, const_iterator k) { return k + j; }
friend class QMap<Key, T>;
+ friend class QMultiMap<Key, T>;
};
friend class const_iterator;
@@ -552,9 +557,12 @@ public:
const_iterator upperBound(const Key &key) const;
iterator insert(const Key &key, const T &value);
iterator insert(const_iterator pos, const Key &key, const T &value);
- iterator insertMulti(const Key &key, const T &value);
- iterator insertMulti(const_iterator pos, const Key &akey, const T &avalue);
- QMap<Key, T> &unite(const QMap<Key, T> &other);
+ void insert(const QMap<Key, T> &map);
+#if QT_DEPRECATED_SINCE(5, 15)
+ QT_DEPRECATED_X("Use QMultiMap for maps storing multiple values with the same key.") iterator insertMulti(const Key &key, const T &value);
+ QT_DEPRECATED_X("Use QMultiMap for maps storing multiple values with the same key.") iterator insertMulti(const_iterator pos, const Key &akey, const T &avalue);
+ QT_DEPRECATED_X("Use QMultiMap for maps storing multiple values with the same key.") QMap<Key, T> &unite(const QMap<Key, T> &other);
+#endif
// STL compatibility
typedef Key key_type;
@@ -583,6 +591,8 @@ private:
return true;
#endif
}
+
+ friend class QMultiMap<Key, T>;
};
template <class Key, class T>
@@ -645,23 +655,6 @@ Q_INLINE_TEMPLATE T &QMap<Key, T>::operator[](const Key &akey)
}
template <class Key, class T>
-Q_INLINE_TEMPLATE int QMap<Key, T>::count(const Key &akey) const
-{
- Node *firstNode;
- Node *lastNode;
- d->nodeRange(akey, &firstNode, &lastNode);
-
- const_iterator ci_first(firstNode);
- const const_iterator ci_last(lastNode);
- int cnt = 0;
- while (ci_first != ci_last) {
- ++cnt;
- ++ci_first;
- }
- return cnt;
-}
-
-template <class Key, class T>
Q_INLINE_TEMPLATE bool QMap<Key, T>::contains(const Key &akey) const
{
return d->findNode(akey) != nullptr;
@@ -763,70 +756,44 @@ typename QMap<Key, T>::iterator QMap<Key, T>::insert(const_iterator pos, const K
}
template <class Key, class T>
-Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insertMulti(const Key &akey,
- const T &avalue)
+Q_INLINE_TEMPLATE void QMap<Key, T>::insert(const QMap<Key, T> &map)
{
- detach();
- Node* y = d->end();
- Node* x = static_cast<Node *>(d->root());
- bool left = true;
- while (x != nullptr) {
- left = !qMapLessThanKey(x->key, akey);
- y = x;
- x = left ? x->leftNode() : x->rightNode();
- }
- Node *z = d->createNode(akey, avalue, y, left);
- return iterator(z);
-}
+ if (d == map.d)
+ return;
-template <class Key, class T>
-typename QMap<Key, T>::iterator QMap<Key, T>::insertMulti(const_iterator pos, const Key &akey, const T &avalue)
-{
- if (d->ref.isShared())
- return this->insertMulti(akey, avalue);
-
- Q_ASSERT_X(isValidIterator(pos), "QMap::insertMulti", "The specified const_iterator argument 'pos' is invalid");
-
- if (pos == constEnd()) {
- // Hint is that the Node is larger than (or equal to) the largest value.
- Node *n = static_cast<Node *>(pos.i->left);
- if (n) {
- while (n->right)
- n = static_cast<Node *>(n->right);
+ detach();
- if (!qMapLessThanKey(n->key, akey))
- return this->insertMulti(akey, avalue); // ignore hint
- Node *z = d->createNode(akey, avalue, n, false); // insert right most
- return iterator(z);
+ Node *n = d->root();
+ auto it = map.cbegin();
+ const auto e = map.cend();
+ while (it != e) {
+ // Insertion here is based on insert(Key, T)
+ auto parent = d->end();
+ bool left = true;
+ Node *lastNode = nullptr;
+ while (n) {
+ parent = n;
+ if (!qMapLessThanKey(n->key, it.key())) {
+ lastNode = n;
+ n = n->leftNode();
+ left = true;
+ } else {
+ n = n->rightNode();
+ left = false;
+ }
}
- return this->insertMulti(akey, avalue);
- } else {
- // Hint indicates that the node should be less (or equal to) the hint given
- // but larger than the previous value.
- Node *next = const_cast<Node*>(pos.i);
- if (qMapLessThanKey(next->key, akey))
- return this->insertMulti(akey, avalue); // ignore hint
-
- if (pos == constBegin()) {
- // There is no previous value (insert left most)
- Node *z = d->createNode(akey, avalue, begin().i, true);
- return iterator(z);
+ if (lastNode && !qMapLessThanKey(it.key(), lastNode->key)) {
+ lastNode->value = it.value();
+ n = lastNode;
} else {
- Node *prev = const_cast<Node*>(pos.i->previousNode());
- if (!qMapLessThanKey(prev->key, akey))
- return this->insertMulti(akey, avalue); // ignore hint
-
- // Hint is ok - do insert
- if (prev->right == nullptr) {
- Node *z = d->createNode(akey, avalue, prev, false);
- return iterator(z);
- }
- if (next->left == nullptr) {
- Node *z = d->createNode(akey, avalue, next, true);
- return iterator(z);
- }
- Q_ASSERT(false); // We should have prev->right == nullptr or next->left == nullptr.
- return this->insertMulti(akey, avalue);
+ n = d->createNode(it.key(), it.value(), parent, left);
+ }
+ ++it;
+ if (it != e) {
+ // Move back up the tree until we find the next branch or node which is
+ // relevant for the next key.
+ while (n != d->root() && qMapLessThanKey(n->key, it.key()))
+ n = static_cast<Node *>(n->parent());
}
}
}
@@ -854,19 +821,6 @@ Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::find(const Key &
}
template <class Key, class T>
-Q_INLINE_TEMPLATE QMap<Key, T> &QMap<Key, T>::unite(const QMap<Key, T> &other)
-{
- QMap<Key, T> copy(other);
- const_iterator it = copy.constEnd();
- const const_iterator b = copy.constBegin();
- while (it != b) {
- --it;
- insertMulti(it.key(), it.value());
- }
- return *this;
-}
-
-template <class Key, class T>
QPair<typename QMap<Key, T>::iterator, typename QMap<Key, T>::iterator> QMap<Key, T>::equal_range(const Key &akey)
{
detach();
@@ -982,26 +936,6 @@ Q_OUTOFLINE_TEMPLATE void QMap<Key, T>::detach_helper()
}
template <class Key, class T>
-Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::uniqueKeys() const
-{
- QList<Key> res;
- res.reserve(size()); // May be too much, but assume short lifetime
- const_iterator i = begin();
- if (i != end()) {
- for (;;) {
- const Key &aKey = i.key();
- res.append(aKey);
- do {
- if (++i == end())
- goto break_out_of_outer_loop;
- } while (!qMapLessThanKey(aKey, i.key())); // loop while (key == i.key())
- }
- }
-break_out_of_outer_loop:
- return res;
-}
-
-template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys() const
{
QList<Key> res;
@@ -1054,21 +988,6 @@ Q_OUTOFLINE_TEMPLATE QList<T> QMap<Key, T>::values() const
}
template <class Key, class T>
-Q_OUTOFLINE_TEMPLATE QList<T> QMap<Key, T>::values(const Key &akey) const
-{
- QList<T> res;
- Node *n = d->findNode(akey);
- if (n) {
- const_iterator it(n);
- do {
- res.append(*it);
- ++it;
- } while (it != constEnd() && !qMapLessThanKey<Key>(akey, it.key()));
- }
- return res;
-}
-
-template <class Key, class T>
Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator QMap<Key, T>::lowerBound(const Key &akey) const
{
Node *lb = d->root() ? d->root()->lowerBound(akey) : nullptr;
@@ -1164,15 +1083,20 @@ public:
QMultiMap(QMap<Key, T> &&other) noexcept : QMap<Key, T>(std::move(other)) {}
void swap(QMultiMap<Key, T> &other) noexcept { QMap<Key, T>::swap(other); }
+ QList<Key> uniqueKeys() const;
+ QList<T> values(const Key &key) const;
+
+ using typename QMap<Key, T>::iterator;
+ using typename QMap<Key, T>::const_iterator;
+
inline typename QMap<Key, T>::iterator replace(const Key &key, const T &value)
{ return QMap<Key, T>::insert(key, value); }
- inline typename QMap<Key, T>::iterator insert(const Key &key, const T &value)
- { return QMap<Key, T>::insertMulti(key, value); }
- inline typename QMap<Key, T>::iterator insert(typename QMap<Key, T>::const_iterator pos, const Key &key, const T &value)
- { return QMap<Key, T>::insertMulti(pos, key, value); }
+ iterator insert(const Key &key, const T &value);
+ iterator insert(const_iterator pos, const Key &key, const T &value);
+ QMultiMap &unite(const QMultiMap &other);
inline QMultiMap &operator+=(const QMultiMap &other)
- { this->unite(other); return *this; }
+ { return unite(other); }
inline QMultiMap operator+(const QMultiMap &other) const
{ QMultiMap result = *this; result += other; return result; }
@@ -1181,11 +1105,18 @@ public:
using QMap<Key, T>::count;
using QMap<Key, T>::find;
using QMap<Key, T>::constFind;
+ using QMap<Key, T>::values;
+ using QMap<Key, T>::size;
+ using QMap<Key, T>::detach;
+ using QMap<Key, T>::erase;
+ using QMap<Key, T>::isValidIterator;
+ using typename QMap<Key, T>::Node;
bool contains(const Key &key, const T &value) const;
int remove(const Key &key, const T &value);
+ int count(const Key &key) const;
int count(const Key &key, const T &value) const;
typename QMap<Key, T>::iterator find(const Key &key, const T &value) {
@@ -1216,6 +1147,123 @@ private:
};
template <class Key, class T>
+Q_OUTOFLINE_TEMPLATE QList<Key> QMultiMap<Key, T>::uniqueKeys() const
+{
+ QList<Key> res;
+ res.reserve(size()); // May be too much, but assume short lifetime
+ const_iterator i = this->begin();
+ if (i != this->end()) {
+ for (;;) {
+ const Key &aKey = i.key();
+ res.append(aKey);
+ do {
+ if (++i == this->end())
+ goto break_out_of_outer_loop;
+ } while (!qMapLessThanKey(aKey, i.key())); // loop while (key == i.key())
+ }
+ }
+break_out_of_outer_loop:
+ return res;
+}
+
+template <class Key, class T>
+Q_OUTOFLINE_TEMPLATE QList<T> QMultiMap<Key, T>::values(const Key &akey) const
+{
+ QList<T> res;
+ Node *n = this->d->findNode(akey);
+ if (n) {
+ const_iterator it(n);
+ do {
+ res.append(*it);
+ ++it;
+ } while (it != this->constEnd() && !qMapLessThanKey<Key>(akey, it.key()));
+ }
+ return res;
+}
+
+template <class Key, class T>
+Q_INLINE_TEMPLATE typename QMultiMap<Key, T>::iterator QMultiMap<Key, T>::insert(const Key &akey,
+ const T &avalue)
+{
+ detach();
+ Node* y = this->d->end();
+ Node* x = static_cast<Node *>(this->d->root());
+ bool left = true;
+ while (x != nullptr) {
+ left = !qMapLessThanKey(x->key, akey);
+ y = x;
+ x = left ? x->leftNode() : x->rightNode();
+ }
+ Node *z = this->d->createNode(akey, avalue, y, left);
+ return iterator(z);
+}
+
+template <class Key, class T>
+typename QMultiMap<Key, T>::iterator QMultiMap<Key, T>::insert(const_iterator pos, const Key &akey, const T &avalue)
+{
+ if (this->d->ref.isShared())
+ return insert(akey, avalue);
+
+ Q_ASSERT_X(isValidIterator(pos), "QMap::insert", "The specified const_iterator argument 'pos' is invalid");
+
+ if (pos == this->constEnd()) {
+ // Hint is that the Node is larger than (or equal to) the largest value.
+ Node *n = static_cast<Node *>(pos.i->left);
+ if (n) {
+ while (n->right)
+ n = static_cast<Node *>(n->right);
+
+ if (!qMapLessThanKey(n->key, akey))
+ return insert(akey, avalue); // ignore hint
+ Node *z = this->d->createNode(akey, avalue, n, false); // insert right most
+ return iterator(z);
+ }
+ return insert(akey, avalue);
+ } else {
+ // Hint indicates that the node should be less (or equal to) the hint given
+ // but larger than the previous value.
+ Node *next = const_cast<Node*>(pos.i);
+ if (qMapLessThanKey(next->key, akey))
+ return insert(akey, avalue); // ignore hint
+
+ if (pos == this->constBegin()) {
+ // There is no previous value (insert left most)
+ Node *z = this->d->createNode(akey, avalue, this->begin().i, true);
+ return iterator(z);
+ } else {
+ Node *prev = const_cast<Node*>(pos.i->previousNode());
+ if (!qMapLessThanKey(prev->key, akey))
+ return insert(akey, avalue); // ignore hint
+
+ // Hint is ok - do insert
+ if (prev->right == nullptr) {
+ Node *z = this->d->createNode(akey, avalue, prev, false);
+ return iterator(z);
+ }
+ if (next->left == nullptr) {
+ Node *z = this->d->createNode(akey, avalue, next, true);
+ return iterator(z);
+ }
+ Q_ASSERT(false); // We should have prev->right == nullptr or next->left == nullptr.
+ return insert(akey, avalue);
+ }
+ }
+}
+
+template <class Key, class T>
+Q_INLINE_TEMPLATE QMultiMap<Key, T> &QMultiMap<Key, T>::unite(const QMultiMap<Key, T> &other)
+{
+ QMultiMap<Key, T> copy(other);
+ const_iterator it = copy.constEnd();
+ const const_iterator b = copy.constBegin();
+ while (it != b) {
+ --it;
+ insert(it.key(), it.value());
+ }
+ return *this;
+}
+
+template <class Key, class T>
Q_INLINE_TEMPLATE bool QMultiMap<Key, T>::contains(const Key &key, const T &value) const
{
return constFind(key, value) != QMap<Key, T>::constEnd();
@@ -1229,7 +1277,7 @@ Q_INLINE_TEMPLATE int QMultiMap<Key, T>::remove(const Key &key, const T &value)
typename QMap<Key, T>::iterator end(QMap<Key, T>::end());
while (i != end && !qMapLessThanKey<Key>(key, i.key())) {
if (i.value() == value) {
- i = this->erase(i);
+ i = erase(i);
++n;
} else {
++i;
@@ -1239,6 +1287,23 @@ Q_INLINE_TEMPLATE int QMultiMap<Key, T>::remove(const Key &key, const T &value)
}
template <class Key, class T>
+Q_INLINE_TEMPLATE int QMultiMap<Key, T>::count(const Key &akey) const
+{
+ QMultiMap::Node *firstNode;
+ QMultiMap::Node *lastNode;
+ this->d->nodeRange(akey, &firstNode, &lastNode);
+
+ const_iterator ci_first(firstNode);
+ const const_iterator ci_last(lastNode);
+ int cnt = 0;
+ while (ci_first != ci_last) {
+ ++cnt;
+ ++ci_first;
+ }
+ return cnt;
+}
+
+template <class Key, class T>
Q_INLINE_TEMPLATE int QMultiMap<Key, T>::count(const Key &key, const T &value) const
{
int n = 0;
@@ -1252,6 +1317,44 @@ Q_INLINE_TEMPLATE int QMultiMap<Key, T>::count(const Key &key, const T &value) c
return n;
}
+#if QT_DEPRECATED_SINCE(5, 15)
+template<class Key, class T>
+QList<Key> QMap<Key, T>::uniqueKeys() const
+{
+ return static_cast<const QMultiMap<Key, T> *>(this)->uniqueKeys();
+}
+
+template<class Key, class T>
+QList<T> QMap<Key, T>::values(const Key &key) const
+{
+ return static_cast<const QMultiMap<Key, T> *>(this)->values(key);
+}
+
+template<class Key, class T>
+int QMap<Key, T>::count(const Key &key) const
+{
+ return static_cast<const QMultiMap<Key, T> *>(this)->count(key);
+}
+
+template<class Key, class T>
+typename QMap<Key, T>::iterator QMap<Key, T>::insertMulti(const Key &key, const T &value)
+{
+ return static_cast<QMultiMap<Key, T> *>(this)->insert(key, value);
+}
+
+template<class Key, class T>
+typename QMap<Key, T>::iterator QMap<Key, T>::insertMulti(const_iterator pos, const Key &akey, const T &avalue)
+{
+ return static_cast<QMultiMap<Key, T> *>(this)->insert(pos, akey, avalue);
+}
+
+template<class Key, class T>
+QMap<Key, T> &QMap<Key, T>::unite(const QMap<Key, T> &other)
+{
+ return static_cast<QMultiMap<Key, T> *>(this)->unite(other);
+}
+#endif
+
Q_DECLARE_ASSOCIATIVE_ITERATOR(Map)
Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(Map)
diff --git a/src/corelib/tools/qpoint.h b/src/corelib/tools/qpoint.h
index fe952f95da..f0a91c4ff8 100644
--- a/src/corelib/tools/qpoint.h
+++ b/src/corelib/tools/qpoint.h
@@ -352,6 +352,7 @@ Q_DECL_RELAXED_CONSTEXPR inline QPointF &QPointF::operator*=(qreal c)
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
QT_WARNING_DISABLE_GCC("-Wfloat-equal")
+QT_WARNING_DISABLE_INTEL(1572)
Q_DECL_CONSTEXPR inline bool operator==(const QPointF &p1, const QPointF &p2)
{
diff --git a/src/corelib/tools/qrect.h b/src/corelib/tools/qrect.h
index 7aa2312f38..c6bfc1a50d 100644
--- a/src/corelib/tools/qrect.h
+++ b/src/corelib/tools/qrect.h
@@ -665,6 +665,7 @@ Q_DECL_CONSTEXPR inline QRectF::QRectF(const QRect &r) noexcept
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
QT_WARNING_DISABLE_GCC("-Wfloat-equal")
+QT_WARNING_DISABLE_INTEL(1572)
Q_DECL_CONSTEXPR inline bool QRectF::isNull() const noexcept
{ return w == 0. && h == 0.; }
diff --git a/src/corelib/tools/qscopedvaluerollback.h b/src/corelib/tools/qscopedvaluerollback.h
index f904b8dfcb..b8ceff6665 100644
--- a/src/corelib/tools/qscopedvaluerollback.h
+++ b/src/corelib/tools/qscopedvaluerollback.h
@@ -45,7 +45,11 @@
QT_BEGIN_NAMESPACE
template <typename T>
-class QScopedValueRollback
+class
+#if QT_HAS_CPP_ATTRIBUTE(nodiscard) && __cplusplus >= 201703L
+[[nodiscard]]
+#endif
+QScopedValueRollback
{
public:
explicit QScopedValueRollback(T &var)
diff --git a/src/corelib/tools/qscopeguard.h b/src/corelib/tools/qscopeguard.h
index 45c3f93da4..40d2747b1d 100644
--- a/src/corelib/tools/qscopeguard.h
+++ b/src/corelib/tools/qscopeguard.h
@@ -51,7 +51,7 @@ template <typename F> QScopeGuard<F> qScopeGuard(F f);
template <typename F>
class
-#if QT_HAS_CPP_ATTRIBUTE(nodiscard)
+#if __has_cpp_attribute(nodiscard)
// Q_REQUIRED_RESULT can be defined as __warn_unused_result__ or as [[nodiscard]]
// but the 1st one has some limitations for example can be placed only on functions.
Q_REQUIRED_RESULT
@@ -91,7 +91,7 @@ private:
template <typename F>
-#if QT_HAS_CPP_ATTRIBUTE(nodiscard)
+#if __has_cpp_attribute(nodiscard)
Q_REQUIRED_RESULT
#endif
QScopeGuard<F> qScopeGuard(F f)
diff --git a/src/corelib/tools/qscopeguard.qdoc b/src/corelib/tools/qscopeguard.qdoc
index 5a9b7fd210..6b3c942e84 100644
--- a/src/corelib/tools/qscopeguard.qdoc
+++ b/src/corelib/tools/qscopeguard.qdoc
@@ -51,10 +51,10 @@ QT_BEGIN_NAMESPACE
of the scope.
\ingroup misc
- QScopeGuard<F> is a class which sole purpose is to run a function \e F in
- its destructor. This is useful for guaranteeing your cleanup code is
- executed, whether the function is exited normally, exited early by a return
- statement, or exited by an exception.
+ QScopeGuard<F> is a class of which the sole purpose is to run the function
+ \a f in its destructor. This is useful for guaranteeing
+ your cleanup code is executed, whether the function is exited normally,
+ exited early by a return statement, or exited by an exception.
If \e F is a lambda then you cannot instantiate the template directly,
therefore the qScopeGuard() helper is provided and QScopeGuard<F> is made a
diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp
index f185d2f23f..0576fb2bd0 100644
--- a/src/corelib/tools/qsharedpointer.cpp
+++ b/src/corelib/tools/qsharedpointer.cpp
@@ -1285,6 +1285,8 @@
\relates QSharedPointer
\since 5.14
+ Returns a shared pointer to the pointer held by \a src.
+
Same as qSharedPointerObjectCast(). This function is provided for STL
compatibility.
*/
diff --git a/src/corelib/tools/qsimd.cpp b/src/corelib/tools/qsimd.cpp
index d7c1d8c4a9..75c380ee8a 100644
--- a/src/corelib/tools/qsimd.cpp
+++ b/src/corelib/tools/qsimd.cpp
@@ -685,7 +685,7 @@ static QT_FUNCTION_TARGET(RDRND) Q_DECL_COLD_FUNCTION bool checkRdrndWorks() noe
// Check the results for equality
if (testBuffer[0] == testBuffer[1]
&& testBuffer[0] == testBuffer[2]
- && end == testBuffer + TestBufferSize && testBuffer[0] == testBuffer[3]) {
+ && (end < testBuffer + TestBufferSize || testBuffer[0] == testBuffer[3])) {
fprintf(stderr, "WARNING: CPU random generator seem to be failing, "
"disabling hardware random number generation\n"
"WARNING: RDRND generated:");
diff --git a/src/corelib/tools/qsimd_p.h b/src/corelib/tools/qsimd_p.h
index c28624a25e..26e98c4542 100644
--- a/src/corelib/tools/qsimd_p.h
+++ b/src/corelib/tools/qsimd_p.h
@@ -346,7 +346,7 @@ extern Q_CORE_EXPORT QBasicAtomicInteger<unsigned> qt_cpu_features[2];
#endif
Q_CORE_EXPORT quint64 qDetectCpuFeatures();
-#if defined(Q_PROCESSOR_X86) && QT_COMPILER_SUPPORTS_HERE(RDRND)
+#if defined(Q_PROCESSOR_X86) && QT_COMPILER_SUPPORTS_HERE(RDRND) && !defined(QT_BOOTSTRAPPED)
Q_CORE_EXPORT qsizetype qRandomCpu(void *, qsizetype) noexcept;
#else
static inline qsizetype qRandomCpu(void *, qsizetype) noexcept
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h
index 5def2eceb2..3e98de41f4 100644
--- a/src/corelib/tools/qvector.h
+++ b/src/corelib/tools/qvector.h
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2019 Intel Corporation
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -40,22 +41,15 @@
#ifndef QVECTOR_H
#define QVECTOR_H
-#include <QtCore/qalgorithms.h>
-#include <QtCore/qiterator.h>
-#include <QtCore/qrefcount.h>
-#include <QtCore/qarraydata.h>
+#include <QtCore/qarraydatapointer.h>
+#include <QtCore/qnamespace.h>
#include <QtCore/qhashfunctions.h>
-#include <QtCore/qcontainertools_impl.h>
+#include <QtCore/qiterator.h>
-#include <iterator>
+#include <functional>
+#include <limits>
#include <initializer_list>
-#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
-#include <vector>
-#endif
-#include <stdlib.h>
-#include <string.h>
-
-#include <algorithm>
+#include <type_traits>
QT_BEGIN_NAMESPACE
@@ -79,90 +73,234 @@ class QVector
#endif
{
typedef QTypedArrayData<T> Data;
- Data *d;
+ typedef QArrayDataOps<T> DataOps;
+ typedef QArrayDataPointer<T> DataPointer;
+ class DisableRValueRefs {};
+
+ DataPointer d;
template <typename V, typename U> friend int QtPrivate::indexOf(const QVector<V> &list, const U &u, int from);
template <typename V, typename U> friend int QtPrivate::lastIndexOf(const QVector<V> &list, const U &u, int from);
public:
- inline QVector() noexcept : d(Data::sharedNull()) { }
- explicit QVector(int size);
- QVector(int size, const T &t);
- inline QVector(const QVector<T> &v);
- inline ~QVector() { if (!d->ref.deref()) freeData(d); }
- QVector<T> &operator=(const QVector<T> &v);
- QVector(QVector<T> &&other) noexcept : d(other.d) { other.d = Data::sharedNull(); }
- QVector<T> &operator=(QVector<T> &&other) noexcept
- { QVector moved(std::move(other)); swap(moved); return *this; }
+ typedef T Type;
+ typedef T value_type;
+ typedef value_type *pointer;
+ typedef const value_type *const_pointer;
+ typedef value_type &reference;
+ typedef const value_type &const_reference;
+ typedef int size_type;
+ typedef qptrdiff difference_type;
+ typedef typename Data::iterator iterator;
+ typedef typename Data::const_iterator const_iterator;
+ typedef iterator Iterator;
+ typedef const_iterator ConstIterator;
+ typedef std::reverse_iterator<iterator> reverse_iterator;
+ typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
+ typedef typename DataPointer::parameter_type parameter_type;
+ using rvalue_ref = typename std::conditional<DataPointer::pass_parameter_by_value, DisableRValueRefs, T &&>::type;
+
+private:
+ void resize_internal(int i, Qt::Initialization);
+ bool isValidIterator(const_iterator i) const
+ {
+ const std::less<const T*> less = {};
+ return !less(d->end(), i) && !less(i, d->begin());
+ }
+public:
+ QVector(DataPointer dd) noexcept
+ : d(dd)
+ {
+ }
+
+public:
+ inline QVector() noexcept { }
+ explicit QVector(int size)
+ : d(Data::allocate(size))
+ {
+ if (size)
+ d->appendInitialize(size);
+ }
+ QVector(int size, const T &t)
+ : d(Data::allocate(size))
+ {
+ if (size)
+ d->copyAppend(size, t);
+ }
+
+ inline QVector(const QVector<T> &other) noexcept : d(other.d) {}
+ QVector(QVector<T> &&other) noexcept : d(std::move(other.d)) {}
+ inline QVector(std::initializer_list<T> args)
+ : d(Data::allocate(args.size()))
+ {
+ if (args.size())
+ d->copyAppend(args.begin(), args.end());
+ }
+
+ ~QVector() /*noexcept(std::is_nothrow_destructible<T>::value)*/ {}
+ QVector<T> &operator=(const QVector<T> &other) { d = other.d; return *this; }
+ QVector &operator=(QVector &&other) noexcept(std::is_nothrow_destructible<T>::value)
+ {
+ d = std::move(other.d);
+ return *this;
+ }
+ QVector<T> &operator=(std::initializer_list<T> args)
+ {
+ d = DataPointer(Data::allocate(args.size()));
+ if (args.size())
+ d->copyAppend(args.begin(), args.end());
+ return *this;
+ }
+ template <typename InputIterator, QtPrivate::IfIsForwardIterator<InputIterator> = true>
+ QVector(InputIterator i1, InputIterator i2)
+ : d(Data::allocate(std::distance(i1, i2)))
+ {
+ if (std::distance(i1, i2))
+ d->copyAppend(i1, i2);
+ }
+
+ template <typename InputIterator, QtPrivate::IfIsNotForwardIterator<InputIterator> = true>
+ QVector(InputIterator i1, InputIterator i2)
+ : QVector()
+ {
+ QtPrivate::reserveIfForwardIterator(this, i1, i2);
+ std::copy(i1, i2, std::back_inserter(*this));
+ }
+
void swap(QVector<T> &other) noexcept { qSwap(d, other.d); }
- inline QVector(std::initializer_list<T> args);
- QVector<T> &operator=(std::initializer_list<T> args);
- template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true>
- inline QVector(InputIterator first, InputIterator last);
- explicit QVector(QArrayDataPointerRef<T> ref) noexcept : d(ref.ptr) {}
- bool operator==(const QVector<T> &v) const;
- inline bool operator!=(const QVector<T> &v) const { return !(*this == v); }
+ friend bool operator==(const QVector &l, const QVector &r)
+ {
+ if (l.size() != r.size())
+ return false;
+ if (l.begin() == r.begin())
+ return true;
- inline int size() const { return d->size; }
+ // do element-by-element comparison
+ return l.d->compare(l.begin(), r.begin(), l.size());
+ }
+ friend bool operator!=(const QVector &l, const QVector &r)
+ {
+ return !(l == r);
+ }
- inline bool isEmpty() const { return d->size == 0; }
+ int size() const noexcept { return int(d->size); }
+ int count() const noexcept { return size(); }
+ int length() const noexcept { return size(); }
- void resize(int size);
+ inline bool isEmpty() const noexcept { return d->size == 0; }
- inline int capacity() const { return int(d->alloc); }
- void reserve(int size);
- inline void squeeze()
+ void resize(int size)
{
- if (d->size < int(d->alloc)) {
- if (!d->size) {
- *this = QVector<T>();
- return;
- }
- realloc(d->size);
- }
- if (d->capacityReserved) {
- // capacity reserved in a read only memory would be useless
- // this checks avoid writing to such memory.
- d->capacityReserved = 0;
- }
+ resize_internal(size, Qt::Uninitialized);
+ if (size > this->size())
+ d->appendInitialize(size);
+ }
+ void resize(int size, parameter_type c)
+ {
+ resize_internal(size, Qt::Uninitialized);
+ if (size > this->size())
+ d->copyAppend(size - this->size(), c);
}
- inline void detach();
- inline bool isDetached() const { return !d->ref.isShared(); }
+ inline int capacity() const { return int(d->constAllocatedCapacity()); }
+ void reserve(int size);
+ inline void squeeze();
+
+ void detach() { d.detach(); }
+ bool isDetached() const noexcept { return !d->isShared(); }
inline bool isSharedWith(const QVector<T> &other) const { return d == other.d; }
- inline T *data() { detach(); return d->begin(); }
- inline const T *data() const { return d->begin(); }
- inline const T *constData() const { return d->begin(); }
- void clear();
-
- const T &at(int i) const;
- T &operator[](int i);
- const T &operator[](int i) const;
- void append(const T &t);
- void append(T &&t);
- inline void append(const QVector<T> &l) { *this += l; }
- void prepend(T &&t);
+ pointer data() { detach(); return d->data(); }
+ const_pointer data() const noexcept { return d->data(); }
+ const_pointer constData() const noexcept { return d->data(); }
+ void clear() {
+ if (!size())
+ return;
+ if (d->needsDetach()) {
+ // must allocate memory
+ DataPointer detached(Data::allocate(d.allocatedCapacity(), d->detachFlags()));
+ d.swap(detached);
+ } else {
+ d->truncate(0);
+ }
+ }
+
+ const_reference at(int i) const noexcept
+ {
+ Q_ASSERT_X(size_t(i) < size_t(d->size), "QVector::at", "index out of range");
+ return data()[i];
+ }
+ reference operator[](int i)
+ {
+ Q_ASSERT_X(size_t(i) < size_t(d->size), "QVector::operator[]", "index out of range");
+ detach();
+ return data()[i];
+ }
+ const_reference operator[](int i) const noexcept { return at(i); }
+ void append(const_reference t)
+ { append(const_iterator(std::addressof(t)), const_iterator(std::addressof(t)) + 1); }
+ void append(const_iterator i1, const_iterator i2);
+ void append(value_type &&t);
+ void append(const QVector<T> &l) { append(l.constBegin(), l.constEnd()); }
+ void prepend(rvalue_ref t);
void prepend(const T &t);
- void insert(int i, T &&t);
- void insert(int i, const T &t);
- void insert(int i, int n, const T &t);
- void replace(int i, const T &t);
- void remove(int i);
- void remove(int i, int n);
- inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(d->begin()); }
- inline void removeLast();
- T takeFirst() { Q_ASSERT(!isEmpty()); T r = std::move(first()); removeFirst(); return r; }
- T takeLast() { Q_ASSERT(!isEmpty()); T r = std::move(last()); removeLast(); return r; }
-
- QVector<T> &fill(const T &t, int size = -1);
-
- int indexOf(const T &t, int from = 0) const;
- int lastIndexOf(const T &t, int from = -1) const;
- bool contains(const T &t) const;
- int count(const T &t) const;
+ iterator insert(int i, parameter_type t)
+ { return insert(i, 1, t); }
+ iterator insert(int i, int n, parameter_type t);
+ iterator insert(const_iterator before, parameter_type t)
+ {
+ Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid");
+ return insert(before, 1, t);
+ }
+ iterator insert(const_iterator before, int n, parameter_type t)
+ {
+ Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid");
+ return insert(std::distance(constBegin(), before), n, t);
+ }
+ iterator insert(const_iterator before, rvalue_ref t)
+ {
+ Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid");
+ return insert(std::distance(constBegin(), before), std::move(t));
+ }
+ iterator insert(int i, rvalue_ref t);
+#if 0
+ template< class InputIt >
+ iterator insert( const_iterator pos, InputIt first, InputIt last );
+ iterator insert( const_iterator pos, std::initializer_list<T> ilist );
+#endif
+ void replace(int i, const T &t)
+ {
+ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
+ const T copy(t);
+ data()[i] = copy;
+ }
+ void replace(int i, rvalue_ref t)
+ {
+ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
+ const T copy(std::move(t));
+ data()[i] = std::move(copy);
+ }
+
+ void remove(int i, int n = 1);
+ void removeFirst() { Q_ASSERT(!isEmpty()); remove(0); }
+ void removeLast() { Q_ASSERT(!isEmpty()); remove(size() - 1); }
+ value_type takeFirst() { Q_ASSERT(!isEmpty()); value_type v = std::move(first()); remove(0); return v; }
+ value_type takeLast() { Q_ASSERT(!isEmpty()); value_type v = std::move(last()); remove(size() - 1); return v; }
+
+ QVector<T> &fill(parameter_type t, int size = -1);
+
+ int indexOf(const T &t, int from = 0) const noexcept;
+ int lastIndexOf(const T &t, int from = -1) const noexcept;
+ bool contains(const T &t) const noexcept
+ {
+ return indexOf(t) != -1;
+ }
+ int count(const T &t) const noexcept
+ {
+ return int(std::count(&*cbegin(), &*cend(), t));
+ }
// QList compatibility
void removeAt(int i) { remove(i); }
@@ -171,10 +309,10 @@ public:
const const_iterator ce = this->cend(), cit = std::find(this->cbegin(), ce, t);
if (cit == ce)
return 0;
+ int index = cit - this->cbegin();
// next operation detaches, so ce, cit, t may become invalidated:
const T tCopy = t;
- const int firstFoundIdx = std::distance(this->cbegin(), cit);
- const iterator e = end(), it = std::remove(begin() + firstFoundIdx, e, tCopy);
+ const iterator e = end(), it = std::remove(begin() + index, e, tCopy);
const int result = std::distance(it, e);
erase(it, e);
return result;
@@ -187,7 +325,6 @@ public:
remove(i);
return true;
}
- int length() const { return size(); }
T takeAt(int i) { T t = std::move((*this)[i]); remove(i); return t; }
void move(int from, int to)
{
@@ -204,32 +341,26 @@ public:
}
// STL-style
- typedef typename Data::iterator iterator;
- typedef typename Data::const_iterator const_iterator;
- typedef std::reverse_iterator<iterator> reverse_iterator;
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
- inline iterator begin() { detach(); return d->begin(); }
- inline const_iterator begin() const noexcept { return d->constBegin(); }
- inline const_iterator cbegin() const noexcept { return d->constBegin(); }
- inline const_iterator constBegin() const noexcept { return d->constBegin(); }
- inline iterator end() { detach(); return d->end(); }
- inline const_iterator end() const noexcept { return d->constEnd(); }
- inline const_iterator cend() const noexcept { return d->constEnd(); }
- inline const_iterator constEnd() const noexcept { return d->constEnd(); }
+ iterator begin() { detach(); return d->begin(); }
+ iterator end() { detach(); return d->end(); }
+
+ const_iterator begin() const noexcept { return d->constBegin(); }
+ const_iterator end() const noexcept { return d->constEnd(); }
+ const_iterator cbegin() const noexcept { return d->constBegin(); }
+ const_iterator cend() const noexcept { return d->constEnd(); }
+ const_iterator constBegin() const noexcept { return d->constBegin(); }
+ const_iterator constEnd() const noexcept { return d->constEnd(); }
reverse_iterator rbegin() { return reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
- iterator insert(iterator before, int n, const T &x);
- inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }
- inline iterator insert(iterator before, T &&x);
+
iterator erase(iterator begin, iterator end);
inline iterator erase(iterator pos) { return erase(pos, pos+1); }
// more Qt
- inline int count() const { return d->size; }
inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
inline const T &first() const { Q_ASSERT(!isEmpty()); return *begin(); }
inline const T &constFirst() const { Q_ASSERT(!isEmpty()); return *begin(); }
@@ -240,7 +371,7 @@ public:
inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
QVector<T> mid(int pos, int len = -1) const;
- T value(int i) const;
+ T value(int i) const { return value(i, T()); }
T value(int i, const T &defaultValue) const;
void swapItemsAt(int i, int j) {
@@ -251,31 +382,22 @@ public:
}
// STL compatibility
- typedef T value_type;
- typedef value_type* pointer;
- typedef const value_type* const_pointer;
- typedef value_type& reference;
- typedef const value_type& const_reference;
- typedef qptrdiff difference_type;
- typedef iterator Iterator;
- typedef const_iterator ConstIterator;
- typedef int size_type;
inline void push_back(const T &t) { append(t); }
- void push_back(T &&t) { append(std::move(t)); }
- void push_front(T &&t) { prepend(std::move(t)); }
+ void push_back(rvalue_ref t) { append(std::move(t)); }
+ void push_front(rvalue_ref t) { prepend(std::move(t)); }
inline void push_front(const T &t) { prepend(t); }
void pop_back() { removeLast(); }
void pop_front() { removeFirst(); }
inline bool empty() const
{ return d->size == 0; }
- inline T& front() { return first(); }
+ inline reference front() { return first(); }
inline const_reference front() const { return first(); }
inline reference back() { return last(); }
inline const_reference back() const { return last(); }
void shrink_to_fit() { squeeze(); }
// comfort
- QVector<T> &operator+=(const QVector<T> &l);
+ QVector<T> &operator+=(const QVector<T> &l) { append(l.cbegin(), l.cend()); return *this; }
inline QVector<T> operator+(const QVector<T> &l) const
{ QVector n = *this; n += l; return n; }
inline QVector<T> &operator+=(const T &t)
@@ -284,42 +406,17 @@ public:
{ append(t); return *this; }
inline QVector<T> &operator<<(const QVector<T> &l)
{ *this += l; return *this; }
- inline QVector<T> &operator+=(T &&t)
+ inline QVector<T> &operator+=(rvalue_ref t)
{ append(std::move(t)); return *this; }
- inline QVector<T> &operator<<(T &&t)
+ inline QVector<T> &operator<<(rvalue_ref t)
{ append(std::move(t)); return *this; }
-#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
- Q_DECL_DEPRECATED_X("Use QVector<T>(vector.begin(), vector.end()) instead.")
- static inline QVector<T> fromStdVector(const std::vector<T> &vector)
- { return QVector<T>(vector.begin(), vector.end()); }
- Q_DECL_DEPRECATED_X("Use std::vector<T>(vector.begin(), vector.end()) instead.")
- inline std::vector<T> toStdVector() const
- { return std::vector<T>(d->begin(), d->end()); }
-#endif
-
// Consider deprecating in 6.4 or later
static QVector<T> fromList(const QVector<T> &list) { return list; }
QVector<T> toList() const { return *this; }
static inline QVector<T> fromVector(const QVector<T> &vector) { return vector; }
inline QVector<T> toVector() const { return *this; }
-
-private:
- // ### Qt6: remove methods, they are unused
- void reallocData(const int size, const int alloc, QArrayData::AllocationOptions options = QArrayData::Default);
- void reallocData(const int sz) { reallocData(sz, d->alloc); }
- void realloc(int alloc, QArrayData::AllocationOptions options = QArrayData::Default);
- void freeData(Data *d);
- void defaultConstruct(T *from, T *to);
- void copyConstruct(const T *srcFrom, const T *srcTo, T *dstFrom);
- void destruct(T *from, T *to);
- bool isValidIterator(const iterator &i) const
- {
- const std::less<const T*> less = {};
- return !less(d->end(), i) && !less(i, d->begin());
- }
- class AlignmentDummy { Data header; T array[1]; };
};
#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606
@@ -329,654 +426,227 @@ template <typename InputIterator,
QVector(InputIterator, InputIterator) -> QVector<ValueType>;
#endif
-#ifdef Q_CC_MSVC
-// behavior change: an object of POD type constructed with an initializer of the form ()
-// will be default-initialized
-# pragma warning ( push )
-# pragma warning ( disable : 4345 )
-# pragma warning(disable : 4127) // conditional expression is constant
-#endif
-
template <typename T>
-void QVector<T>::defaultConstruct(T *from, T *to)
+inline void QVector<T>::resize_internal(int newSize, Qt::Initialization)
{
- if (QTypeInfo<T>::isComplex) {
- while (from != to) {
- new (from++) T();
+ Q_ASSERT(newSize >= 0);
+
+ if (d->needsDetach() || newSize > capacity()) {
+ // must allocate memory
+ DataPointer detached(Data::allocate(d->detachCapacity(newSize),
+ d->detachFlags()));
+ if (size() && newSize) {
+ detached->copyAppend(constBegin(), constBegin() + qMin(newSize, size()));
}
- } else {
- ::memset(static_cast<void *>(from), 0, (to - from) * sizeof(T));
+ d.swap(detached);
}
-}
-template <typename T>
-void QVector<T>::copyConstruct(const T *srcFrom, const T *srcTo, T *dstFrom)
-{
- if (QTypeInfo<T>::isComplex) {
- while (srcFrom != srcTo)
- new (dstFrom++) T(*srcFrom++);
- } else {
- ::memcpy(static_cast<void *>(dstFrom), static_cast<const void *>(srcFrom), (srcTo - srcFrom) * sizeof(T));
- }
+ if (newSize < size())
+ d->truncate(newSize);
}
template <typename T>
-void QVector<T>::destruct(T *from, T *to)
+void QVector<T>::reserve(int asize)
{
- if (QTypeInfo<T>::isComplex) {
- while (from != to) {
- from++->~T();
+ // capacity() == 0 for immutable data, so this will force a detaching below
+ if (asize <= capacity()) {
+ if (d->flags() & Data::CapacityReserved)
+ return; // already reserved, don't shrink
+ if (!d->isShared()) {
+ // accept current allocation, don't shrink
+ d->flags() |= Data::CapacityReserved;
+ return;
}
}
+
+ DataPointer detached(Data::allocate(qMax(asize, size()),
+ d->detachFlags() | Data::CapacityReserved));
+ detached->copyAppend(constBegin(), constEnd());
+ d.swap(detached);
}
template <typename T>
-inline QVector<T>::QVector(const QVector<T> &v)
+inline void QVector<T>::squeeze()
{
- if (v.d->ref.ref()) {
- d = v.d;
- } else {
- if (v.d->capacityReserved) {
- d = Data::allocate(v.d->alloc);
- Q_CHECK_PTR(d);
- d->capacityReserved = true;
- } else {
- d = Data::allocate(v.d->size);
- Q_CHECK_PTR(d);
- }
- if (d->alloc) {
- copyConstruct(v.d->begin(), v.d->end(), d->begin());
- d->size = v.d->size;
+ if (d->needsDetach() || size() != capacity()) {
+ // must allocate memory
+ DataPointer detached(Data::allocate(size(), d->detachFlags() & ~Data::CapacityReserved));
+ if (size()) {
+ detached->copyAppend(constBegin(), constEnd());
}
+ d.swap(detached);
}
}
-#if defined(Q_CC_MSVC)
-#pragma warning( pop )
-#endif
-
template <typename T>
-void QVector<T>::detach()
+inline void QVector<T>::remove(int i, int n)
{
- if (d->ref.isStatic())
- return;
-
- if (!isDetached())
- realloc(int(d->alloc));
- Q_ASSERT(isDetached());
-}
+ Q_ASSERT_X(size_t(i) + size_t(n) <= size_t(d->size), "QVector::remove", "index out of range");
+ Q_ASSERT_X(n >= 0, "QVector::remove", "invalid count");
-template <typename T>
-void QVector<T>::reserve(int asize)
-{
- if (asize > int(d->alloc))
- realloc(asize);
- if (isDetached())
- d->capacityReserved = 1;
- Q_ASSERT(capacity() >= asize);
-}
+ if (n == 0)
+ return;
-template <typename T>
-void QVector<T>::resize(int asize)
-{
- if (asize == d->size)
- return detach();
- if (asize > int(d->alloc) || !isDetached()) { // there is not enough space
- QArrayData::AllocationOptions opt = asize > int(d->alloc) ? QArrayData::Grow : QArrayData::Default;
- realloc(qMax(int(d->alloc), asize), opt);
+ const size_t newSize = size() - n;
+ if (d->needsDetach() ||
+ ((d->flags() & Data::CapacityReserved) == 0
+ && newSize < d->allocatedCapacity()/2)) {
+ // allocate memory
+ DataPointer detached(Data::allocate(d->detachCapacity(newSize),
+ d->detachFlags() & ~(Data::GrowsBackwards | Data::GrowsForward)));
+ const_iterator where = constBegin() + i;
+ if (newSize) {
+ detached->copyAppend(constBegin(), where);
+ detached->copyAppend(where + n, constEnd());
+ }
+ d.swap(detached);
+ } else {
+ // we're detached and we can just move data around
+ d->erase(d->begin() + i, d->begin() + i + n);
}
- if (asize < d->size)
- destruct(begin() + asize, end());
- else
- defaultConstruct(end(), begin() + asize);
- d->size = asize;
-}
-template <typename T>
-inline void QVector<T>::clear()
-{
- if (!d->size)
- return;
- destruct(begin(), end());
- d->size = 0;
}
-template <typename T>
-inline const T &QVector<T>::at(int i) const
-{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::at", "index out of range");
- return d->begin()[i]; }
-template <typename T>
-inline const T &QVector<T>::operator[](int i) const
-{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
- return d->begin()[i]; }
-template <typename T>
-inline T &QVector<T>::operator[](int i)
-{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
- return data()[i]; }
-template <typename T>
-inline void QVector<T>::insert(int i, const T &t)
-{ Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
- insert(begin() + i, 1, t); }
-template <typename T>
-inline void QVector<T>::insert(int i, int n, const T &t)
-{ Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
- insert(begin() + i, n, t); }
-template <typename T>
-inline void QVector<T>::insert(int i, T &&t)
-{ Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
- insert(begin() + i, std::move(t)); }
-template <typename T>
-inline void QVector<T>::remove(int i, int n)
-{ Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= d->size, "QVector<T>::remove", "index out of range");
- erase(d->begin() + i, d->begin() + i + n); }
-template <typename T>
-inline void QVector<T>::remove(int i)
-{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::remove", "index out of range");
- erase(d->begin() + i, d->begin() + i + 1); }
+
template <typename T>
inline void QVector<T>::prepend(const T &t)
-{ insert(begin(), 1, t); }
-template <typename T>
-inline void QVector<T>::prepend(T &&t)
-{ insert(begin(), std::move(t)); }
-
+{ insert(0, 1, t); }
template <typename T>
-inline void QVector<T>::replace(int i, const T &t)
-{
- Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
- const T copy(t);
- data()[i] = copy;
-}
+void QVector<T>::prepend(rvalue_ref t)
+{ insert(0, std::move(t)); }
-template <typename T>
-QVector<T> &QVector<T>::operator=(const QVector<T> &v)
+template<typename T>
+inline T QVector<T>::value(int i, const T &defaultValue) const
{
- if (v.d != d) {
- QVector<T> tmp(v);
- tmp.swap(*this);
- }
- return *this;
+ return size_t(i) < size_t(d->size) ? at(i) : defaultValue;
}
template <typename T>
-QVector<T>::QVector(int asize)
+inline void QVector<T>::append(const_iterator i1, const_iterator i2)
{
- Q_ASSERT_X(asize >= 0, "QVector::QVector", "Size must be greater than or equal to 0.");
- if (Q_LIKELY(asize > 0)) {
- d = Data::allocate(asize);
- Q_CHECK_PTR(d);
- d->size = asize;
- defaultConstruct(d->begin(), d->end());
+ if (i1 == i2)
+ return;
+ const size_t newSize = size() + std::distance(i1, i2);
+ if (d->needsDetach() || newSize > d->allocatedCapacity()) {
+ DataPointer detached(Data::allocate(d->detachCapacity(newSize),
+ d->detachFlags() | Data::GrowsForward));
+ detached->copyAppend(constBegin(), constEnd());
+ detached->copyAppend(i1, i2);
+ d.swap(detached);
} else {
- d = Data::sharedNull();
+ // we're detached and we can just move data around
+ d->copyAppend(i1, i2);
}
}
template <typename T>
-QVector<T>::QVector(int asize, const T &t)
+inline void QVector<T>::append(value_type &&t)
{
- Q_ASSERT_X(asize >= 0, "QVector::QVector", "Size must be greater than or equal to 0.");
- if (asize > 0) {
- d = Data::allocate(asize);
- Q_CHECK_PTR(d);
- d->size = asize;
- T* i = d->end();
- while (i != d->begin())
- new (--i) T(t);
+ const size_t newSize = size() + 1;
+ const bool isTooSmall = newSize > d->allocatedCapacity();
+ const bool isOverlapping = std::addressof(*d->begin()) <= std::addressof(t)
+ && std::addressof(t) < std::addressof(*d->end());
+ if (isTooSmall || d->needsDetach() || Q_UNLIKELY(isOverlapping)) {
+ typename Data::ArrayOptions flags = d->detachFlags();
+ if (isTooSmall)
+ flags |= Data::GrowsForward;
+ DataPointer detached(Data::allocate(d->detachCapacity(newSize), flags));
+ detached->copyAppend(constBegin(), constEnd());
+ detached->moveAppend(std::addressof(t), std::addressof(t) + 1);
+ d.swap(detached);
} else {
- d = Data::sharedNull();
+ // we're detached and we can just move data around
+ d->moveAppend(std::addressof(t), std::addressof(t) + 1);
}
}
-#if defined(Q_CC_MSVC)
-QT_WARNING_PUSH
-QT_WARNING_DISABLE_MSVC(4127) // conditional expression is constant
-#endif // Q_CC_MSVC
-
template <typename T>
-QVector<T>::QVector(std::initializer_list<T> args)
+inline typename QVector<T>::iterator
+QVector<T>::insert(int i, int n, parameter_type t)
{
- if (args.size() > 0) {
- d = Data::allocate(args.size());
- Q_CHECK_PTR(d);
- // std::initializer_list<T>::iterator is guaranteed to be
- // const T* ([support.initlist]/1), so can be memcpy'ed away from by copyConstruct
- copyConstruct(args.begin(), args.end(), d->begin());
- d->size = int(args.size());
- } else {
- d = Data::sharedNull();
- }
-}
+ Q_ASSERT_X(size_t(i) <= size_t(d->size), "QVector<T>::insert", "index out of range");
-template <typename T>
-QVector<T> &QVector<T>::operator=(std::initializer_list<T> args)
-{
- QVector<T> tmp(args);
- tmp.swap(*this);
- return *this;
-}
+ // we don't have a quick exit for n == 0
+ // it's not worth wasting CPU cycles for that
-#if defined(Q_CC_MSVC)
-QT_WARNING_POP
-#endif // Q_CC_MSVC
+ const size_t newSize = size() + n;
+ if (d->needsDetach() || newSize > d->allocatedCapacity()) {
+ typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward;
+ if (size_t(i) <= newSize / 4)
+ flags |= Data::GrowsBackwards;
-template <typename T>
-template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator>>
-QVector<T>::QVector(InputIterator first, InputIterator last)
- : QVector()
-{
- QtPrivate::reserveIfForwardIterator(this, first, last);
- std::copy(first, last, std::back_inserter(*this));
-}
-
-template <typename T>
-void QVector<T>::freeData(Data *x)
-{
- destruct(x->begin(), x->end());
- Data::deallocate(x);
-}
-
-#if defined(Q_CC_MSVC)
-QT_WARNING_PUSH
-QT_WARNING_DISABLE_MSVC(4127) // conditional expression is constant
-#endif
-
-template <typename T>
-void QVector<T>::reallocData(const int asize, const int aalloc, QArrayData::AllocationOptions options)
-{
- Q_ASSERT(asize >= 0 && asize <= aalloc);
- Data *x = d;
-
- const bool isShared = d->ref.isShared();
-
- if (aalloc != 0) {
- if (aalloc != int(d->alloc) || isShared) {
- QT_TRY {
- // allocate memory
- x = Data::allocate(aalloc, options);
- Q_CHECK_PTR(x);
- // aalloc is bigger then 0 so it is not [un]sharedEmpty
- Q_ASSERT(!x->ref.isStatic());
- x->size = asize;
-
- T *srcBegin = d->begin();
- T *srcEnd = asize > d->size ? d->end() : d->begin() + asize;
- T *dst = x->begin();
-
- if (!QTypeInfoQuery<T>::isRelocatable || (isShared && QTypeInfo<T>::isComplex)) {
- QT_TRY {
- if (isShared || !std::is_nothrow_move_constructible<T>::value) {
- // we can not move the data, we need to copy construct it
- while (srcBegin != srcEnd)
- new (dst++) T(*srcBegin++);
- } else {
- while (srcBegin != srcEnd)
- new (dst++) T(std::move(*srcBegin++));
- }
- } QT_CATCH (...) {
- // destruct already copied objects
- destruct(x->begin(), dst);
- QT_RETHROW;
- }
- } else {
- ::memcpy(static_cast<void *>(dst), static_cast<void *>(srcBegin), (srcEnd - srcBegin) * sizeof(T));
- dst += srcEnd - srcBegin;
-
- // destruct unused / not moved data
- if (asize < d->size)
- destruct(d->begin() + asize, d->end());
- }
-
- if (asize > d->size) {
- // construct all new objects when growing
- if (!QTypeInfo<T>::isComplex) {
- ::memset(static_cast<void *>(dst), 0, (static_cast<T *>(x->end()) - dst) * sizeof(T));
- } else {
- QT_TRY {
- while (dst != x->end())
- new (dst++) T();
- } QT_CATCH (...) {
- // destruct already copied objects
- destruct(x->begin(), dst);
- QT_RETHROW;
- }
- }
- }
- } QT_CATCH (...) {
- Data::deallocate(x);
- QT_RETHROW;
- }
- x->capacityReserved = d->capacityReserved;
- } else {
- Q_ASSERT(int(d->alloc) == aalloc); // resize, without changing allocation size
- Q_ASSERT(isDetached()); // can be done only on detached d
- Q_ASSERT(x == d); // in this case we do not need to allocate anything
- if (asize <= d->size) {
- destruct(x->begin() + asize, x->end()); // from future end to current end
- } else {
- defaultConstruct(x->end(), x->begin() + asize); // from current end to future end
- }
- x->size = asize;
- }
+ DataPointer detached(Data::allocate(d->detachCapacity(newSize), flags));
+ const_iterator where = constBegin() + i;
+ detached->copyAppend(constBegin(), where);
+ detached->copyAppend(n, t);
+ detached->copyAppend(where, constEnd());
+ d.swap(detached);
} else {
- x = Data::sharedNull();
- }
- if (d != x) {
- if (!d->ref.deref()) {
- if (!QTypeInfoQuery<T>::isRelocatable || !aalloc || (isShared && QTypeInfo<T>::isComplex)) {
- // data was copy constructed, we need to call destructors
- // or if !alloc we did nothing to the old 'd'.
- freeData(d);
- } else {
- Data::deallocate(d);
- }
- }
- d = x;
- }
-
- Q_ASSERT(d->data());
- Q_ASSERT(uint(d->size) <= d->alloc);
- Q_ASSERT(aalloc ? d != Data::sharedNull() : d == Data::sharedNull());
- Q_ASSERT(d->alloc >= uint(aalloc));
- Q_ASSERT(d->size == asize);
-}
-
-template<typename T>
-void QVector<T>::realloc(int aalloc, QArrayData::AllocationOptions options)
-{
- Q_ASSERT(aalloc >= d->size);
- Data *x = d;
-
- const bool isShared = d->ref.isShared();
-
- QT_TRY {
- // allocate memory
- x = Data::allocate(aalloc, options);
- Q_CHECK_PTR(x);
- // aalloc is bigger then 0 so it is not [un]sharedEmpty
- Q_ASSERT(!x->ref.isStatic());
- x->size = d->size;
-
- T *srcBegin = d->begin();
- T *srcEnd = d->end();
- T *dst = x->begin();
-
- if (!QTypeInfoQuery<T>::isRelocatable || (isShared && QTypeInfo<T>::isComplex)) {
- QT_TRY {
- if (isShared || !std::is_nothrow_move_constructible<T>::value) {
- // we can not move the data, we need to copy construct it
- while (srcBegin != srcEnd)
- new (dst++) T(*srcBegin++);
- } else {
- while (srcBegin != srcEnd)
- new (dst++) T(std::move(*srcBegin++));
- }
- } QT_CATCH (...) {
- // destruct already copied objects
- destruct(x->begin(), dst);
- QT_RETHROW;
- }
- } else {
- ::memcpy(static_cast<void *>(dst), static_cast<void *>(srcBegin), (srcEnd - srcBegin) * sizeof(T));
- dst += srcEnd - srcBegin;
- }
-
- } QT_CATCH (...) {
- Data::deallocate(x);
- QT_RETHROW;
- }
- x->capacityReserved = d->capacityReserved;
-
- Q_ASSERT(d != x);
- if (!d->ref.deref()) {
- if (!QTypeInfoQuery<T>::isRelocatable || !aalloc || (isShared && QTypeInfo<T>::isComplex)) {
- // data was copy constructed, we need to call destructors
- // or if !alloc we did nothing to the old 'd'.
- freeData(d);
+ // we're detached and we can just move data around
+ if (i == size()) {
+ d->copyAppend(n, t);
} else {
- Data::deallocate(d);
+ T copy(t);
+ d->insert(d.begin() + i, n, copy);
}
}
- d = x;
-
- Q_ASSERT(d->data());
- Q_ASSERT(uint(d->size) <= d->alloc);
- Q_ASSERT(d != Data::sharedNull());
- Q_ASSERT(d->alloc >= uint(aalloc));
-}
-
-#if defined(Q_CC_MSVC)
-QT_WARNING_POP
-#endif
-
-template<typename T>
-Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
-{
- if (uint(i) >= uint(d->size)) {
- return T();
- }
- return d->begin()[i];
-}
-template<typename T>
-Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const
-{
- return uint(i) >= uint(d->size) ? defaultValue : d->begin()[i];
+ return d.begin() + i;
}
template <typename T>
-void QVector<T>::append(const T &t)
+typename QVector<T>::iterator
+QVector<T>::insert(int i, rvalue_ref t)
{
- const bool isTooSmall = uint(d->size + 1) > d->alloc;
- if (!isDetached() || isTooSmall) {
- T copy(t);
- QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default);
- realloc(isTooSmall ? d->size + 1 : d->alloc, opt);
-
- if (QTypeInfo<T>::isComplex)
- new (d->end()) T(std::move(copy));
- else
- *d->end() = std::move(copy);
+ Q_ASSERT_X(size_t(i) <= size_t(d->size), "QVector<T>::insert", "index out of range");
- } else {
- if (QTypeInfo<T>::isComplex)
- new (d->end()) T(t);
- else
- *d->end() = t;
- }
- ++d->size;
-}
-
-template <typename T>
-void QVector<T>::append(T &&t)
-{
- const bool isTooSmall = uint(d->size + 1) > d->alloc;
- if (!isDetached() || isTooSmall) {
- QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default);
- realloc(isTooSmall ? d->size + 1 : d->alloc, opt);
- }
-
- new (d->end()) T(std::move(t));
-
- ++d->size;
-}
+ const size_t newSize = size() + 1;
+ if (d->needsDetach() || newSize > d->allocatedCapacity()) {
+ typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward;
+ if (size_t(i) <= newSize / 4)
+ flags |= Data::GrowsBackwards;
-template <typename T>
-void QVector<T>::removeLast()
-{
- Q_ASSERT(!isEmpty());
- Q_ASSERT(d->alloc);
-
- if (d->ref.isShared())
- detach();
- --d->size;
- if (QTypeInfo<T>::isComplex)
- (d->data() + d->size)->~T();
-}
-
-template <typename T>
-typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t)
-{
- Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid");
-
- const auto offset = std::distance(d->begin(), before);
- if (n != 0) {
- const T copy(t);
- if (!isDetached() || d->size + n > int(d->alloc))
- realloc(d->size + n, QArrayData::Grow);
- if (!QTypeInfoQuery<T>::isRelocatable) {
- T *b = d->end();
- T *i = d->end() + n;
- while (i != b)
- new (--i) T;
- i = d->end();
- T *j = i + n;
- b = d->begin() + offset;
- while (i != b)
- *--j = *--i;
- i = b+n;
- while (i != b)
- *--i = copy;
- } else {
- T *b = d->begin() + offset;
- T *i = b + n;
- memmove(static_cast<void *>(i), static_cast<const void *>(b), (d->size - offset) * sizeof(T));
- while (i != b)
- new (--i) T(copy);
- }
- d->size += n;
- }
- return d->begin() + offset;
-}
-
-template <typename T>
-typename QVector<T>::iterator QVector<T>::insert(iterator before, T &&t)
-{
- Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid");
-
- const auto offset = std::distance(d->begin(), before);
- if (!isDetached() || d->size + 1 > int(d->alloc))
- realloc(d->size + 1, QArrayData::Grow);
- if (!QTypeInfoQuery<T>::isRelocatable) {
- T *i = d->end();
- T *j = i + 1;
- T *b = d->begin() + offset;
- // The new end-element needs to be constructed, the rest must be move assigned
- if (i != b) {
- new (--j) T(std::move(*--i));
- while (i != b)
- *--j = std::move(*--i);
- *b = std::move(t);
- } else {
- new (b) T(std::move(t));
- }
+ DataPointer detached(Data::allocate(d->detachCapacity(newSize), flags));
+ const_iterator where = constBegin() + i;
+ detached->copyAppend(constBegin(), where);
+ detached->moveAppend(std::addressof(t), std::addressof(t) + 1);
+ detached->copyAppend(where, constEnd());
+ d.swap(detached);
} else {
- T *b = d->begin() + offset;
- memmove(static_cast<void *>(b + 1), static_cast<const void *>(b), (d->size - offset) * sizeof(T));
- new (b) T(std::move(t));
+ d->insert(d.begin() + i, std::move(t));
}
- d->size += 1;
- return d->begin() + offset;
+ return d.begin() + i;
}
template <typename T>
typename QVector<T>::iterator QVector<T>::erase(iterator abegin, iterator aend)
{
- Q_ASSERT_X(isValidIterator(abegin), "QVector::erase", "The specified iterator argument 'abegin' is invalid");
- Q_ASSERT_X(isValidIterator(aend), "QVector::erase", "The specified iterator argument 'aend' is invalid");
-
- const auto itemsToErase = aend - abegin;
-
- if (!itemsToErase)
- return abegin;
+ Q_ASSERT_X(isValidIterator(const_iterator(abegin)), "QVector::erase", "The specified iterator argument 'abegin' is invalid");
+ Q_ASSERT_X(isValidIterator(const_iterator(aend)), "QVector::erase", "The specified iterator argument 'aend' is invalid");
+ Q_ASSERT(aend >= abegin);
- Q_ASSERT(abegin >= d->begin());
- Q_ASSERT(aend <= d->end());
- Q_ASSERT(abegin <= aend);
+ // d.begin() so we don't detach just yet
+ int i = std::distance(d.begin(), abegin);
+ int n = std::distance(abegin, aend);
+ remove(i, n);
- const auto itemsUntouched = abegin - d->begin();
-
- // FIXME we could do a proper realloc, which copy constructs only needed data.
- // FIXME we are about to delete data - maybe it is good time to shrink?
- // FIXME the shrink is also an issue in removeLast, that is just a copy + reduce of this.
- if (d->alloc) {
- detach();
- abegin = d->begin() + itemsUntouched;
- aend = abegin + itemsToErase;
- if (!QTypeInfoQuery<T>::isRelocatable) {
- iterator moveBegin = abegin + itemsToErase;
- iterator moveEnd = d->end();
- while (moveBegin != moveEnd) {
- if (QTypeInfo<T>::isComplex)
- static_cast<T *>(abegin)->~T();
- new (abegin++) T(*moveBegin++);
- }
- if (abegin < d->end()) {
- // destroy rest of instances
- destruct(abegin, d->end());
- }
- } else {
- destruct(abegin, aend);
- // QTBUG-53605: static_cast<void *> masks clang errors of the form
- // error: destination for this 'memmove' call is a pointer to class containing a dynamic class
- // FIXME maybe use std::is_polymorphic (as soon as allowed) to avoid the memmove
- memmove(static_cast<void *>(abegin), static_cast<void *>(aend),
- (d->size - itemsToErase - itemsUntouched) * sizeof(T));
- }
- d->size -= int(itemsToErase);
- }
- return d->begin() + itemsUntouched;
-}
-
-template <typename T>
-bool QVector<T>::operator==(const QVector<T> &v) const
-{
- if (d == v.d)
- return true;
- if (d->size != v.d->size)
- return false;
- const T *vb = v.d->begin();
- const T *b = d->begin();
- const T *e = d->end();
- return std::equal(b, e, QT_MAKE_CHECKED_ARRAY_ITERATOR(vb, v.d->size));
+ return d.begin() + i;
}
template <typename T>
-QVector<T> &QVector<T>::fill(const T &from, int asize)
+inline QVector<T> &QVector<T>::fill(parameter_type t, int newSize)
{
- const T copy(from);
- resize(asize < 0 ? d->size : asize);
- if (d->size) {
- T *i = d->end();
- T *b = d->begin();
- while (i != b)
- *--i = copy;
- }
- return *this;
-}
-
-template <typename T>
-QVector<T> &QVector<T>::operator+=(const QVector &l)
-{
- if (d->size == 0) {
- *this = l;
+ if (newSize == -1)
+ newSize = size();
+ if (d->needsDetach() || newSize > capacity()) {
+ // must allocate memory
+ DataPointer detached(Data::allocate(d->detachCapacity(newSize),
+ d->detachFlags()));
+ detached->copyAppend(newSize, t);
+ d.swap(detached);
} else {
- uint newSize = d->size + l.d->size;
- const bool isTooSmall = newSize > d->alloc;
- if (!isDetached() || isTooSmall) {
- QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default);
- realloc(isTooSmall ? newSize : d->alloc, opt);
- }
-
- if (d->alloc) {
- T *w = d->begin() + newSize;
- T *i = l.d->end();
- T *b = l.d->begin();
- while (i != b) {
- if (QTypeInfo<T>::isComplex)
- new (--w) T(*--i);
- else
- *--w = *--i;
- }
- d->size = newSize;
- }
+ // we're detached
+ const T copy(t);
+ d->assign(d.begin(), d.begin() + qMin(size(), newSize), t);
+ if (newSize > size())
+ d->copyAppend(newSize - size(), copy);
}
return *this;
}
@@ -1017,54 +687,35 @@ int lastIndexOf(const QVector<T> &vector, const U &u, int from)
}
template <typename T>
-int QVector<T>::indexOf(const T &t, int from) const
+int QVector<T>::indexOf(const T &t, int from) const noexcept
{
return QtPrivate::indexOf<T, T>(*this, t, from);
}
template <typename T>
-int QVector<T>::lastIndexOf(const T &t, int from) const
+int QVector<T>::lastIndexOf(const T &t, int from) const noexcept
{
return QtPrivate::lastIndexOf(*this, t, from);
}
template <typename T>
-bool QVector<T>::contains(const T &t) const
-{
- const T *b = d->begin();
- const T *e = d->end();
- return std::find(b, e, t) != e;
-}
-
-template <typename T>
-int QVector<T>::count(const T &t) const
-{
- const T *b = d->begin();
- const T *e = d->end();
- return int(std::count(b, e, t));
-}
-
-template <typename T>
-Q_OUTOFLINE_TEMPLATE QVector<T> QVector<T>::mid(int pos, int len) const
+inline QVector<T> QVector<T>::mid(int pos, int len) const
{
using namespace QtPrivate;
- switch (QContainerImplHelper::mid(d->size, &pos, &len)) {
+ switch (QContainerImplHelper::mid(d.size, &pos, &len)) {
case QContainerImplHelper::Null:
case QContainerImplHelper::Empty:
- return QVector<T>();
+ return QVector();
case QContainerImplHelper::Full:
return *this;
case QContainerImplHelper::Subset:
break;
}
- QVector<T> midResult;
- midResult.realloc(len);
- T *srcFrom = d->begin() + pos;
- T *srcTo = d->begin() + pos + len;
- midResult.copyConstruct(srcFrom, srcTo, midResult.data());
- midResult.d->size = len;
- return midResult;
+ // Allocate memory
+ DataPointer copied(Data::allocate(len));
+ copied->copyAppend(constBegin() + pos, constBegin() + pos + len);
+ return copied;
}
Q_DECLARE_SEQUENTIAL_ITERATOR(Vector)
diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri
index 40c84157cd..230456bef9 100644
--- a/src/corelib/tools/tools.pri
+++ b/src/corelib/tools/tools.pri
@@ -12,6 +12,7 @@ HEADERS += \
tools/qcontainerfwd.h \
tools/qcontainertools_impl.h \
tools/qcryptographichash.h \
+ tools/qflatmap_p.h \
tools/qfreelist_p.h \
tools/qhash.h \
tools/qhashfunctions.h \
@@ -47,7 +48,6 @@ HEADERS += \
tools/qvector.h \
tools/qversionnumber.h
-
SOURCES += \
tools/qarraydata.cpp \
tools/qbitarray.cpp \