// Convert a QVariant to a Python object with an optional specific type. PyObject *qpycore_qvariant_value(QVariant &value, PyObject *type) { PyObject *value_obj; if (type) { const Chimera *ct = Chimera::parse(type); if (!ct) return 0; // Get QVariant to do a conversion if there is one to do. if (ct->metatype() < static_cast<int>(QVariant::UserType)) { QVariant::Type wanted = static_cast<QVariant::Type>(ct->metatype()); // If we have a QStringList but are not wanting one then convert it // to a QVariantList. if (wanted != QVariant::StringList && value.type() == QVariant::StringList) value.convert(QVariant::List); // If we have a container but are not wanting one then assume we // want a container with elements of the wanted type. if (wanted != QVariant::List && value.type() == QVariant::List) value_obj = convert_list(ct, value.toList()); else if (wanted != QVariant::Map && value.type() == QVariant::Map) value_obj = convert_map(ct, value.toMap()); #if QT_VERSION >= 0x040500 else if (wanted != QVariant::Hash && value.type() == QVariant::Hash) value_obj = convert_hash(ct, value.toHash()); #endif else value_obj = convert(ct, value); } else if (!value.isValid() && ct->py_type()) { // Convert an invalid value to the default value of the requested // type. This is most useful when the requested type is a list or // dict. value_obj = PyObject_CallObject(ct->py_type(), NULL); } else { // This is likely to fail and the exception will say why. value_obj = ct->toPyObject(value); } delete ct; } else { QVariant *heap = new QVariant(value); value_obj = sipConvertFromNewType(heap, sipType_QVariant, 0); if (!value_obj) delete heap; } return value_obj; }
mustache_value_t* convert_type( char* key, void* context, mustache_context_t* m_lookup_context, mustache_context_t* m_exection_context) { if (strcmp(key, ".") == 0) { return convert_value((VALUE)context, m_lookup_context); } VALUE rb_context = (VALUE) context; switch (TYPE(rb_context)) { case T_OBJECT: return convert_object(rb_context, key, m_lookup_context, m_exection_context); case T_HASH: return convert_hash(rb_context, key, m_lookup_context, m_exection_context); default: return NULL; } }