116 inline T fromVariant(
const QVariant &value)
120 QT_WARNING_DISABLE_MSVC(4702)
122 if constexpr (std::is_same_v<T, QVariant>)
125 const QMetaType sourceType = value.metaType();
126 const QMetaType targetType = QMetaType::fromType<T>();
127 if (sourceType == targetType)
128 return *
reinterpret_cast<
const T *>(value.constData());
130 if constexpr (std::is_same_v<T,std::remove_const_t<std::remove_pointer_t<T>>
const *>) {
131 using nonConstT = std::remove_const_t<std::remove_pointer_t<T>> *;
132 const QMetaType nonConstTargetType = QMetaType::fromType<nonConstT>();
133 if (value.metaType() == nonConstTargetType)
134 return *
reinterpret_cast<
const nonConstT *>(value.constData());
137 if constexpr (std::is_same_v<T, QJSValue>)
138 return toScriptValue(value);
140 if constexpr (std::is_same_v<T, QJSManagedValue>)
141 return toManagedValue(value);
143 if constexpr (std::is_same_v<T, QJSPrimitiveValue>)
144 return toPrimitiveValue(value);
146 if constexpr (std::is_same_v<T, QString>) {
147 if (!sourceType.isValid())
148 return QStringLiteral(
"undefined");
150 if (sourceType.flags() & QMetaType::PointerToQObject) {
151 return convertQObjectToString(
152 *
reinterpret_cast<QObject *
const *>(value.constData()));
156 if constexpr (std::is_same_v<T,
double> || std::is_same_v<T,
float>) {
157 if (!sourceType.isValid())
158 return std::numeric_limits<T>::quiet_NaN();
161 if constexpr (std::is_same_v<T,
double>) {
162 if (sourceType == QMetaType::fromType<
int>())
163 return double(*
static_cast<
const int *>(value.constData()));
166 if constexpr (std::is_same_v<T,
int>) {
167 if (sourceType == QMetaType::fromType<
double>()) {
168 return QJSNumberCoercion::toInteger(
169 *
static_cast<
const double *>(value.constData()));
173 if constexpr (std::is_same_v<QObject, std::remove_const_t<std::remove_pointer_t<T>>>) {
174 if (sourceType.flags() & QMetaType::PointerToQObject) {
175 return *
static_cast<QObject *
const *>(value.constData());
182 if (sourceType == QMetaType::fromType<QJSValue>())
183 return fromScriptValue<T>(*
reinterpret_cast<
const QJSValue *>(value.constData()));
185 if (sourceType == QMetaType::fromType<QJSManagedValue>()) {
186 return fromManagedValue<T>(
187 *
reinterpret_cast<
const QJSManagedValue *>(value.constData()));
190 if (sourceType == QMetaType::fromType<QJSPrimitiveValue>()) {
191 return fromPrimitiveValue<T>(
192 *
reinterpret_cast<
const QJSPrimitiveValue *>(value.constData()));
197 if (value.metaType() == QMetaType::fromType<QString>()) {
198 if (convertString(value.toString(), targetType, &t))
200 }
else if (convertVariant(value, targetType, &t)) {
204 QMetaType::convert(value.metaType(), value.constData(), targetType, &t);
212 inline To coerceValue(
const From &from)
214 if constexpr (std::is_base_of_v<To, From>)
217 if constexpr (std::is_same_v<To, QJSValue>)
218 return toScriptValue(from);
220 if constexpr (std::is_same_v<From, QJSValue>)
221 return fromScriptValue<To>(from);
223 if constexpr (std::is_same_v<To, QJSManagedValue>)
224 return toManagedValue(from);
226 if constexpr (std::is_same_v<From, QJSManagedValue>)
227 return fromManagedValue<To>(from);
229 if constexpr (std::is_same_v<To, QJSPrimitiveValue>)
230 return toPrimitiveValue(from);
232 if constexpr (std::is_same_v<From, QJSPrimitiveValue>)
233 return fromPrimitiveValue<To>(from);
235 if constexpr (std::is_same_v<From, QVariant>)
236 return fromVariant<To>(from);
238 if constexpr (std::is_same_v<To, QVariant>)
239 return QVariant::fromValue(from);
241 if constexpr (std::is_same_v<To, QString>) {
242 if constexpr (std::is_base_of_v<QObject, std::remove_const_t<std::remove_pointer_t<From>>>)
243 return convertQObjectToString(from);
246 if constexpr (std::is_same_v<From, QDateTime>) {
247 if constexpr (std::is_same_v<To, QDate>)
248 return convertDateTimeToDate(from.toLocalTime());
249 if constexpr (std::is_same_v<To, QTime>)
250 return from.toLocalTime().time();
251 if constexpr (std::is_same_v<To, QString>)
252 return convertDateTimeToString(from.toLocalTime());
253 if constexpr (std::is_same_v<To,
double>)
254 return convertDateTimeToNumber(from.toLocalTime());
257 if constexpr (std::is_same_v<From, QDate>) {
258 if constexpr (std::is_same_v<To, QDateTime>)
259 return from.startOfDay(QTimeZone::UTC);
260 if constexpr (std::is_same_v<To, QTime>) {
262 return coerceValue<QDateTime, QTime>(coerceValue<QDate, QDateTime>(from));
264 if constexpr (std::is_same_v<To, QString>)
265 return convertDateTimeToString(coerceValue<QDate, QDateTime>(from));
266 if constexpr (std::is_same_v<To,
double>)
267 return convertDateTimeToNumber(coerceValue<QDate, QDateTime>(from));
270 if constexpr (std::is_same_v<From, QTime>) {
271 if constexpr (std::is_same_v<To, QDate>) {
273 return from.isValid() ? QDate(1971, 4, 1) : QDate();
276 if constexpr (std::is_same_v<To, QDateTime>)
277 return QDateTime(coerceValue<QTime, QDate>(from), from, QTimeZone::LocalTime);
278 if constexpr (std::is_same_v<To, QString>)
279 return convertDateTimeToString(coerceValue<QTime, QDateTime>(from));
280 if constexpr (std::is_same_v<To,
double>)
281 return convertDateTimeToNumber(coerceValue<QTime, QDateTime>(from));
284 if constexpr (std::is_same_v<To, std::remove_const_t<std::remove_pointer_t<To>>
const *>) {
285 using nonConstTo = std::remove_const_t<std::remove_pointer_t<To>> *;
286 if constexpr (std::is_same_v<From, nonConstTo>)
290 if constexpr (std::is_same_v<To,
double>) {
291 if constexpr (std::is_same_v<From,
int>)
295 if constexpr (std::is_same_v<To,
int>) {
296 if constexpr (std::is_same_v<From,
double>)
297 return QJSNumberCoercion::toInteger(from);
301 const QMetaType sourceType = QMetaType::fromType<From>();
302 const QMetaType targetType = QMetaType::fromType<To>();
304 if constexpr (std::is_same_v<From, QString>) {
305 if (convertString(from, targetType, &to))
307 }
else if (convertMetaType(sourceType, &from, targetType, &to)) {
311 QMetaType::convert(sourceType, &from, targetType, &to);