void ConnectionSettings::operator<<(const VariantMap &value) { for (auto it = value.begin(); it != value.end(); ++it) { std::string key = it->first; Variant value = it->second; // Set the key-value pair set(key, value); } }
bool XMLElement::SetVariantMap(const VariantMap& value) { if (!RemoveChildren("variant")) return false; for (auto i = value.begin(); i != value.end(); ++i) { XMLElement variantElem = CreateChild("variant"); if (!variantElem) return false; variantElem.SetUInt("hash", i->first.Value()); variantElem.SetVariant(i->second); } return true; }
void CityTradeOptions::load( const VariantMap& stream ) { for( VariantMap::const_iterator it=stream.begin(); it != stream.end(); it++ ) { GoodType gtype = GoodHelper::getType( it->first ); if( gtype == G_NONE ) { StringHelper::debug( 0xff, "%s %s [%s %d]", "Can't convert type from ", it->first.c_str(), __FILE__, __LINE__ ); return; } _d->goods[ gtype ].load( it->second.toList() ); } }
void BuildingDataHolder::initialize( const io::FilePath& filename ) { // populate _mapBuildingByInGood _d->mapBuildingByInGood[G_IRON] = B_WEAPONS_WORKSHOP; _d->mapBuildingByInGood[G_TIMBER] = B_FURNITURE; _d->mapBuildingByInGood[G_CLAY] = B_POTTERY; _d->mapBuildingByInGood[G_OLIVE] = B_OIL_WORKSHOP; _d->mapBuildingByInGood[G_GRAPE] = B_WINE_WORKSHOP; VariantMap constructions = SaveAdapter::load( filename.toString() ); for( VariantMap::iterator it=constructions.begin(); it != constructions.end(); it++ ) { VariantMap options = (*it).second.toMap(); const BuildingType btype = getType( (*it).first ); if( btype == B_NONE ) { StringHelper::debug( 0xff, "!!!Warning: can't associate type with %s", (*it).first.c_str() ); continue; } Impl::BuildingsMap::const_iterator bdataIt = _d->buildings.find( btype ); if( bdataIt != _d->buildings.end() ) { StringHelper::debug( 0xff, "!!!Warning: type %s also initialized", (*it).first.c_str() ); continue; } BuildingData bData( btype, (*it).first, (int)options[ "cost" ] ); const std::string pretty = options[ "pretty" ].toString(); if( !pretty.empty() ) { bData._prettyName = pretty; } bData._baseDesirability = (int)options[ "desirability" ]; bData._desirabilityRange = (int)options[ "desrange" ]; bData._desirabilityStep = (int)options[ "desstep" ]; bData._employers = (int)options[ "employers" ]; bData._buildingClass = getClass( options[ "class" ].toString() ); bData._resourceGroup = options[ "resource" ].toString(); bData._rcIndex = (int)options[ "rcindex" ]; addData( bData ); } }
VariantMap proxyProcessMap( const VariantMap &args, const JSAPIImplPtr& self, const JSAPIImplPtr& proxy ) { VariantMap newMap; for (VariantMap::const_iterator it = args.begin(); it != args.end(); ++it) { if (it->second.is_of_type<JSAPIPtr>() && it->second.convert_cast<JSAPIPtr>() == self) { newMap[it->first] = proxy; } else if (it->second.is_of_type<VariantList>()) { newMap[it->first] = proxyProcessList(it->second.convert_cast<VariantList>(), self, proxy); } else if (it->second.is_of_type<VariantMap>()) { newMap[it->first] = proxyProcessMap(it->second.convert_cast<VariantMap>(), self, proxy); } else { newMap[it->first] = it->second; } } return newMap; }
void Options::load( const VariantMap& stream ) { for( VariantMap::const_iterator it=stream.begin(); it != stream.end(); ++it ) { good::Product gtype = good::Helper::getType( it->first ); if( gtype == good::none ) { Logger::warning( "%s %s [%s %d]", "Can't convert type from ", it->first.c_str(), __FILE__, __LINE__ ); } _d->goods[ gtype ].load( it->second.toList() ); } _d->updateLists(); }
void HouseSpecHelper::loadHouseModel( const std::string& filename ) { VariantMap houses = SaveAdapter::load( filename ); if( houses.empty() ) { StringHelper::debug( 0xff, "Can't load house model from %s", filename.c_str() ); return; } for( VariantMap::iterator it = houses.begin(); it != houses.end(); it++ ) { // this is not a comment (comments start by #) // std::cout << "Line #" << linenum << ":" << line << std::endl; VariantMap hSpec = (*it).second.toMap(); HouseLevelSpec spec; spec._houseLevel = hSpec.get( "level" ).toInt(); spec._levelName = hSpec.get( "title" ).toString(); spec._maxHabitantsByTile = hSpec.get( "habitants" ).toInt(); spec._midDesirability = hSpec.get( "minDesirability" ).toInt(); // min desirability spec._maxDesirability = hSpec.get( "maxDesirability" ).toInt(); // desirability levelUp spec._minEntertainmentLevel = hSpec.get( "entertainment" ).toInt(); spec._minWaterLevel = hSpec.get( "water" ).toInt(); spec._minReligionLevel = hSpec.get( "religion" ).toInt(); spec._minEducationLevel = hSpec.get( "education" ).toInt(); spec._minHealthLevel = hSpec.get( "health" ).toInt(); spec._minFoodLevel = hSpec.get( "food" ).toInt(); spec._requiredGoods[G_WHEAT] = 1; // hard coded ... to be changed! spec._requiredGoods[G_POTTERY] = hSpec.get( "pottery" ).toInt(); // pottery spec._requiredGoods[G_OIL] = hSpec.get( "oil" ).toInt(); // oil spec._requiredGoods[G_FURNITURE] = hSpec.get( "furniture").toInt();// furniture spec._requiredGoods[G_WINE] = hSpec.get( "wine" ).toInt(); // wine spec._crime = hSpec.get( "crime" ).toInt();; // crime spec._prosperity = hSpec.get( "prosperity" ).toInt(); // prosperity spec._taxRate = hSpec.get( "tax" ).toInt();// tax_rate HouseSpecHelper::getInstance().setHouseLevelSpec( spec ); } }
std::string Json::serialize(const Variant &data, bool &success, const std::string& tab) { std::string str; success = true; if( !data.isValid() ) // invalid or null? { str = "null"; } else if( (data.type() == Variant::List) || (data.type() == Variant::NStringArray) ) // variant is a list? { StringArray values; const VariantList rlist = data.toList(); for( VariantList::const_iterator it = rlist.begin(); it != rlist.end(); it++) { std::string serializedValue = serialize( *it, "" ); if( serializedValue.empty() ) { success = false; break; } values.push_back( serializedValue ); } str = "[ " + join( values, ", " ) + " ]"; } // else if(data.type() == Variant::Hash) // variant is a hash? // { // const VariantHash vhash = data.toHash(); // QHashIterator<std::string, Variant> it( vhash ); // str = "{ "; // QList<QByteArray> pairs; // // while(it.hasNext()) // { // it.next(); // QByteArray serializedValue = serialize(it.value(), ""); // // if(serializedValue.isNull()) // { // success = false; // break; // } // // pairs << tab.toAscii() + sanitizeString(it.key()).toUtf8() + " : " + serializedValue; // } // // str += join(pairs, ", "); // str += " }"; // } else if(data.type() == Variant::Map) // variant is a map? { VariantMap vmap = data.toMap(); str = "{ \n"; StringArray pairs; for( VariantMap::iterator it = vmap.begin(); it != vmap.end(); it++ ) { std::string serializedValue = serialize( it->second, tab + " "); if( serializedValue.empty()) { //success = false; pairs.push_back( tab + sanitizeString( it->first ) + std::string( " : \"nonSerializableValue\"" ) ); continue; } pairs.push_back( tab + sanitizeString( it->first ) + " : " + serializedValue ); } str += join(pairs, ",\n"); std::string rtab( tab ); rtab.resize( std::max<int>( 0, tab.size() - 2 ) ); str += std::string( "\n" ) + rtab + "}"; } else if((data.type() == Variant::String) || (data.type() == Variant::NByteArray)) // a string or a byte array? { str = sanitizeString( data.toString() ); } else if(data.type() == Variant::Double || data.type() == Variant::Float) // double? { str = StringHelper::format( 0xff, "\"%f\"", data.toDouble() ); if( str.find(".") == std::string::npos && str.find("e") == std::string::npos ) { str += ".0"; } } else if( data.type() == Variant::NTilePos) { TilePos pos = data.toTilePos(); str = StringHelper::format( 0xff, "[ %d, %d ]", pos.getI(), pos.getJ() ); } else if( data.type() == Variant::NSize) { Size size = data.toSize(); str = StringHelper::format( 0xff, "[ %d, %d ]", size.getWidth(), size.getHeight() ); } else if( data.type() == Variant::NPoint) { Point pos = data.toPoint(); str = StringHelper::format( 0xff, "[ %d, %d ]", pos.getX(), pos.getY() ); } else if( data.type() == Variant::NPointF) { PointF pos = data.toPointF(); str = StringHelper::format( 0xff, "[ \"%f\", \"%f\" ]", pos.getX(), pos.getY() ); } else if (data.type() == Variant::Bool) // boolean value? { str = data.toBool() ? "true" : "false"; } else if (data.type() == Variant::ULongLong) // large unsigned number? { str = StringHelper::format( 0xff, "%u", data.toULongLong() ); } else if ( data.canConvert( Variant::LongLong ) ) // any signed number? { str = StringHelper::format( 0xff, "%d", data.toLongLong() ); } else if (data.canConvert( Variant::Long )) { str = StringHelper::format( 0xff, "%d", data.toLongLong() ); } else if (data.canConvert( Variant::String ) ) // can value be converted to string? { // this will catch Date, DateTime, Url, ... str = sanitizeString( data.toString() ); } else { success = false; } if (success) { return str; } else { return std::string(); } }