Exemple #1
0
void testCodec()
{
    MapType map;

    ListType list = { "foo", 1.5, 5 };
    map["foo1"] = "foo";
    map["foo2"] = 1;
    map["foo3"] = 2.5;
    map["foo4"] = list;
    map["<"] = "<";
    map[">"] = ">";
    map["foo6&"] = "&;";
    map["foo6:"] = ":";
    map["\""] = "\"";
    map["{"] = "{";
    map["}"] = "}";
    map["["] = "[";
    map["]"] = "]";
    map["@"] = "@";
    map["$"] = "$";

    std::stringstream ss;

    {
        Atlas::Message::QueuedDecoder decoder;

        T codec(ss, ss, decoder);
        Atlas::Message::Encoder encoder(codec);
        encoder.streamBegin();
        encoder.streamMessageElement(map);
        encoder.streamEnd();
    }

    std::string atlas_data = ss.str();

    std::stringstream ss2(atlas_data, std::ios::in);

    Atlas::Message::QueuedDecoder decoder;
    {

        T codec(ss2, ss2, decoder);
        Atlas::Message::Encoder enc(codec);

        decoder.streamBegin();
        codec.poll();
        decoder.streamEnd();

    }
//    assert(decoder.queueSize() == 1);
    MapType map2 = decoder.popMessage();

    assert(map2["foo1"].asString() == "foo");
    assert(map2["foo2"].asInt() == 1);
    assert(map2["foo3"].asFloat() == 2.5);
    assert(map2["foo4"].asList()[0].asString() == "foo");
    assert(map2["foo4"].asList()[1].asFloat() == 1.5);
    assert(map2["foo4"].asList()[2].asInt() == 5);
    assert(map2["foo4"].asList()[2].asInt() == 5);
    assert(map2["<"].asString() == "<");
    assert(map2[">"].asString() == ">");
    assert(map2["foo6&"].asString() == "&;");

    assert(map2["foo6:"].asString() == ":");
    assert(map2["\""].asString() == "\"");
    assert(map2["{"].asString() == "{");
    assert(map2["}"].asString() == "}");
    assert(map2["["].asString() == "[");
    assert(map2["]"].asString() == "]");
    assert(map2["@"].asString() == "@");
    assert(map2["$"].asString() == "$");

}
Exemple #2
0
Atlas::Message::MapType EntityRecipe::createEntity(Eris::TypeService& typeService)
{
	S_LOG_VERBOSE("Creating entity.");

	ScriptingService& scriptingService = EmberServices::getSingleton().getScriptingService();
	// Loading script code
	scriptingService.executeCode(mScript, "LuaScriptingProvider");

	// Walking through adapter bindings
	for (BindingsStore::iterator I = mBindings.begin(); I != mBindings.end(); ++I) {
		const std::string& func = I->second->getFunc();

		S_LOG_VERBOSE(" binding: " << I->first << " to func " << func);

		if (func.empty()) {
			std::vector<std::string>& adapters = I->second->getAdapters();

			if (adapters.size() == 1) {
				std::string adapterName = adapters[0];
				Atlas::Message::Element val = mGUIAdapters[adapterName]->getValue();
				I->second->setValue(val);
			} else {
				S_LOG_WARNING("Should be only one adapter without calling function.");
			}
		} else {
			Lua::LuaScriptingCallContext callContext;

			lua_State* L = static_cast<Lua::LuaScriptingProvider*> (scriptingService.getProviderFor("LuaScriptingProvider"))->getLuaState();

			// Pushing function params
			std::vector<std::string>& adapters = I->second->getAdapters();
			for (std::vector<std::string>::iterator J = adapters.begin(); J != adapters.end(); J++) {
				std::string adapterName = *J;
				Atlas::Message::Element* val = new Atlas::Message::Element(mGUIAdapters[adapterName]->getValue());
				tolua_pushusertype_and_takeownership(L, val, "Atlas::Message::Element");
			}

			// Calling test function
			scriptingService.callFunction(func, adapters.size(), "LuaScriptingProvider", &callContext);

			LuaRef returnValue(callContext.getReturnValue());

			Atlas::Message::Element returnObj;
			returnObj = returnValue.asObject<Atlas::Message::Element> ("Atlas::Message::Element");
			I->second->setValue(returnObj);
		}
	}
	//Inject all default attributes that aren't yet added.
	// 	TiXmlElement *elem = mEntitySpec->FirstChildElement("atlas");
	// 	if (elem)
	// 	{
	// 		Eris::TypeInfo* erisType = mConn->getTypeService()->getTypeByName(getEntityType());
	// 		if (erisType) {
	// 			const Atlas::Message::MapType& defaultAttributes = erisType->getAttributes();
	// 			for (Atlas::Message::MapType::const_iterator I = defaultAttributes.begin(); I != defaultAttributes.end(); ++I) {
	// 				bool hasAttribute = false;
	// 				TiXmlNode* child(0);
	// 				while(child = elem->IterateChildren(child)) {
	// 					if (child->ToElement()) {
	// 						if (std::string(child->ToElement()->Attribute("name")) == I->first) {
	// 							hasAttribute = true;
	// 							break;
	// 						}
	// 					}
	// 				}
	//
	// 				if (!hasAttribute) {
	// 					//The attribute isn't present, we'll inject it
	// 					//This a bit contrived, since we'll now first convert the atlas into xml and inject it into the TiXmlElement (which will convert the xml strings into TiXml structures). And then later on we'll parse the xml again and create the final atlas data from it. However, the main reason for doing it this way is that in the future we would want to have nested child elements, which could be repeated. And in those cases we'll want to work directly with xml.
	// 				}
	// 			}
	// 		}
	// 	}
	/*
	 std::stringstream str;

	 Atlas::Message::Element element(message);

	 Atlas::Message::QueuedDecoder decoder;

	 Atlas::Codecs::XML codec(str, decoder);
	 Atlas::Formatter formatter(str, codec);
	 Atlas::Message::Encoder encoder(formatter);
	 formatter.streamBegin();
	 encoder.streamMessageElement(message);
	 formatter.streamEnd();
	 */
	if (mEntitySpec) {
		// Print entity into string
		TiXmlPrinter printer;
		printer.SetStreamPrinting();
		mEntitySpec->Accept(&printer);

		S_LOG_VERBOSE("Composed entity: " << printer.Str());

		std::stringstream strStream(printer.CStr(), std::ios::in);

		// Create objects
		Atlas::Message::QueuedDecoder decoder;
		Atlas::Codecs::XML codec(strStream, decoder);

		// Read whole stream into decoder queue
		while (!strStream.eof()) {
			codec.poll();
		}

		// Read decoder queue
		while (decoder.queueSize() > 0) {
			Atlas::Message::MapType m = decoder.popMessage();
			Eris::TypeInfo* erisType = typeService.getTypeByName(getEntityType());
			if (erisType) {
				const Atlas::Message::MapType& defaultAttributes = erisType->getAttributes();
				for (Atlas::Message::MapType::const_iterator I = defaultAttributes.begin(); I != defaultAttributes.end(); ++I) {
					if (m.find(I->first) == m.end()) {
						m.insert(Atlas::Message::MapType::value_type(I->first, I->second));
					}
				}
			}
			return m;
		}
	} else {
		Atlas::Message::MapType msg;
		msg["parents"] = Atlas::Message::ListType(1, mEntityType);
		msg["name"] = getName();
		return msg;
	}
	S_LOG_WARNING("No entity composed");
	return Atlas::Message::MapType();
}
int convert(const std::string & file_in, const std::string & codec_in,
            const std::string & file_out, const std::string & codec_out)
{
    std::cout << "Convert " << codec_in << " to " << codec_out << std::endl;

    std::fstream in, out;

    in.open( file_in.c_str(), std::ios::in );

    if (!in.is_open()) {
        std::cerr << "Unable to open " << file_in << " for input"
                  << std::endl << std::flush;
        return 1;
    }

    out.open( file_out.c_str(), std::ios::out );

    if (!out.is_open()) {
        std::cerr << "Unable to open " << file_out << " for output"
                  << std::endl << std::flush;
        return 1;
    }

    std::cout << "Reading... ";

    Atlas::Message::QueuedDecoder decoder;
    Atlas::Codec *inCodec = getCodec(codec_in, in, decoder);
    while (!in.eof()) {
        inCodec->poll(true);
    }

    std::cout << "done." << std::endl;
    std::cout << "Writing... ";

    Atlas::Codec * outCodec = getCodec(codec_out, out, decoder);
    Atlas::Bridge * bridge;

    if (option_format) {
        Atlas::Formatter * format;
        bridge = format = new Atlas::Formatter(out, *outCodec);
        if (option_spacing != -1) {
            format->setSpacing(option_spacing);
        }
    } else {
        bridge = outCodec;
    }

    Atlas::Message::Encoder encoder(*bridge);
    encoder.streamBegin();
    while (decoder.queueSize() > 0 ) {
        Atlas::Message::MapType msg(decoder.popMessage());
        encoder.streamMessageElement(msg);
    }
    encoder.streamEnd();

    std::cout << "done." << std::endl;

    out.close();
    in.close();

    return 0;
}