QVariant & value_unsafe(const QStringList & keys, QVariantMap & context){ QVariantMap* map = &context; int i = 0; while(i < keys.size()) { const QString & key = keys.at(i++); QVariantMap::iterator it = map->find(key); if(it == map->constEnd()) return nullVariant; QVariant & value = *it; if(i >= keys.size()) return value; if(value.type() == QVariant::Map) map = &asMap(value); else if(i == keys.size() -1 && value.type() == QVariant::List) { const QString & idxStr = keys.at(i); bool ok; int idx = idxStr.toInt(&ok); if(ok) { QVariantList & lst = asList(value); if(idx < lst.size()) return lst[idx]; } } else return nullVariant; } return nullVariant; }
QVariant value(const QStringList & keys, const QVariantMap & context, const QVariant & defaultValue = QVariant()){ const QVariantMap* map = &context; int i = 0; while(i < keys.size()) { const QString & key = keys.at(i++); QVariantMap::const_iterator it = map->find(key); if(it == map->constEnd()) return defaultValue; const QVariant & value = *it; if(i >= keys.size()) { return detached(value); } if(value.type() == QVariant::Map) map = &asMap(value); else if(i == keys.size() -1 && value.type() == QVariant::List) { const QString & idxStr = keys.at(i); bool ok; int idx = idxStr.toInt(&ok); if(ok) { const QVariantList & lst = asList(value); if(idx < lst.size()) return detached(lst[idx]); } } else return defaultValue; } return defaultValue; }
std::string FileProperties::asJson() const { json::JsonObjectStreamWriter writer; PropertyMap properties = asMap(); for(PropertyMap::iterator it = properties.begin(); it != properties.end(); ++it) writer << std::make_pair(it->first.c_str(), it->second.c_str()); return writer.build(); }
void expand_map(QStringList & keys, const QString & k, const QVariant & v){ if(v.type() == QVariant::Map){ const QVariantMap & map = asMap(v); for(QVariantMap::const_iterator it = map.begin(); it != map.end(); ++it){ expand_map(keys, k.isEmpty() ? it.key() : k +"." + it.key(), it.value()); } } if(!k.isEmpty()){ keys.append(k); } }
bool contains(const QStringList & keys, const QVariantMap & context){ if(keys.size() == 1){ return context.contains(keys.first()); } int i = 0; QVariant variant = context; do{ const QString & key = keys.at(i); if(variant.type() != QVariant::Map) return false; const QVariantMap & map = asMap(variant); QVariantMap::const_iterator it = map.find(key); if(it == map.constEnd()) return false; variant = *it; }while(++i < keys.size()); return true; }
bool set_value(QStringList & keys, const QVariant & value, QVariantMap & context) { if(keys.isEmpty()) return true; QString key = keys.takeFirst(); if(keys.isEmpty()) { context.insert(key, value); } else if(!context.contains(key) || context.value(key).type() != QVariant::Map){ QVariantMap subcontext; set_value(keys, value, subcontext); context.insert(key, subcontext); } else { QVariant & subcontextV = context[key]; if(!(subcontextV.type() == QVariant::Map)) return false; QVariantMap & subcontext = asMap(subcontextV); if(!set_value(keys, value, subcontext)) return false; } return true; }
std::string VariantImpl::asString() const { switch(type) { case VAR_VOID: return EMPTY; case VAR_BOOL: return value.b ? TRUE : FALSE; case VAR_UINT8: return boost::lexical_cast<std::string>((int) value.ui8); case VAR_UINT16: return boost::lexical_cast<std::string>(value.ui16); case VAR_UINT32: return boost::lexical_cast<std::string>(value.ui32); case VAR_UINT64: return boost::lexical_cast<std::string>(value.ui64); case VAR_INT8: return boost::lexical_cast<std::string>((int) value.i8); case VAR_INT16: return boost::lexical_cast<std::string>(value.i16); case VAR_INT32: return boost::lexical_cast<std::string>(value.i32); case VAR_INT64: return boost::lexical_cast<std::string>(value.i64); case VAR_DOUBLE: return boost::lexical_cast<std::string>(value.d); case VAR_FLOAT: return boost::lexical_cast<std::string>(value.f); case VAR_STRING: return *reinterpret_cast<std::string*>(value.v); case VAR_LIST: return toString(asList()); case VAR_MAP: return toString(asMap()); default: throw InvalidConversion(QPID_MSG("Cannot convert from " << getTypeName(type) << " to " << getTypeName(VAR_STRING))); } }
bool unset(const QStringList & keys, QVariantMap & context){ if(keys.size() == 1){ return context.remove(keys.first()) > 0; } QVariant v = value_unsafe(keys.mid(0, keys.size() - 1), context); if(v.isNull()) return false; if(v.type() == QVariant::Map) { QVariantMap & map = asMap(v); return map.remove(keys.last()) != 0; } if(v.type() == QVariant::List) { QVariantList & lst = asList(v); bool ok; int idx = keys.last().toInt(&ok); if(!ok || idx >= lst.size()) return false; lst.removeAt(idx); return true; } return false; }
bool VariantImpl::isEqualTo(VariantImpl& other) const { if (type == other.type) { switch(type) { case VAR_VOID: return true; case VAR_BOOL: return value.b == other.value.b; case VAR_UINT8: return value.ui8 == other.value.ui8; case VAR_UINT16: return value.ui16 == other.value.ui16; case VAR_UINT32: return value.ui32 == other.value.ui32; case VAR_UINT64: return value.ui64 == other.value.ui64; case VAR_INT8: return value.i8 == other.value.i8; case VAR_INT16: return value.i16 == other.value.i16; case VAR_INT32: return value.i32 == other.value.i32; case VAR_INT64: return value.i64 == other.value.i64; case VAR_DOUBLE: return value.d == other.value.d; case VAR_FLOAT: return value.f == other.value.f; case VAR_STRING: return *reinterpret_cast<std::string*>(value.v) == *reinterpret_cast<std::string*>(other.value.v); case VAR_LIST: return equal(asList(), other.asList()); case VAR_MAP: return equal(asMap(), other.asMap()); } } return false; }