Bläddra i källkod

Lua bindings building

Michael Ragazzon 6 år sedan
förälder
incheckning
eb26dd47f2

+ 3 - 3
Include/Rocket/Core/Lua/LuaType.inl

@@ -242,7 +242,7 @@ int LuaType<T>::index(lua_State* L)
             {
                 lua_pushvalue(L,1); //push the userdata to the stack [6]
                 if(lua_pcall(L,1,1,0) != 0) //remove one, result is at [6]
-                    Report(L, String(GetTClassName<T>()).Append(".__index for ").Append(lua_tostring(L,2)).Append(": "));
+                    Report(L, String(GetTClassName<T>()).append(".__index for ").append(lua_tostring(L,2)).append(": "));
             }
             else
             {
@@ -256,7 +256,7 @@ int LuaType<T>::index(lua_State* L)
                         lua_pushvalue(L,1); //[1] = object -> [7] = object
                         lua_pushvalue(L,2); //[2] = key -> [8] = key
                         if(lua_pcall(L,2,1,0) != 0) //call function at top of stack (__index) -> pop top 2 as args; [7] = return value
-                            Report(L, String(GetTClassName<T>()).Append(".__index for ").Append(lua_tostring(L,2)).Append(": "));
+                            Report(L, String(GetTClassName<T>()).append(".__index for ").append(lua_tostring(L,2)).append(": "));
                     }
                     else if(lua_istable(L,-1) )
                         lua_getfield(L,-1,key); //shorthand version of above -> [7] = return value
@@ -298,7 +298,7 @@ int LuaType<T>::newindex(lua_State* L)
         lua_pushvalue(L,1); //userdata at [7]
         lua_pushvalue(L,3); //[8] = copy of [3]
         if(lua_pcall(L,2,0,0) != 0) //call function, pop 2 off push 0 on
-            Report(L, String(GetTClassName<T>()).Append(".__newindex for ").Append(lua_tostring(L,2)).Append(": ")); 
+            Report(L, String(GetTClassName<T>()).append(".__newindex for ").append(lua_tostring(L,2)).append(": ")); 
     }
     else
         lua_pop(L,1); //not a setter function.

+ 1 - 1
Include/Rocket/Core/Lua/Utilities.h

@@ -41,7 +41,7 @@ namespace Lua {
 
 /** casts the variant to its specific type before pushing it to the stack 
 @relates Rocket::Core::Lua::LuaType */
-void ROCKETLUA_API PushVariant(lua_State* L, Variant* var);
+void ROCKETLUA_API PushVariant(lua_State* L, const Variant* var);
 
 /** If there are errors on the top of the stack, this will print those out to the log.
 @param L A Lua state, and if not passed in, will use the Interpreter's state

+ 1 - 1
Samples/basic/treeview/src/FileSystem.cpp

@@ -89,7 +89,7 @@ struct FileSystemNode
 #else
 			struct dirent** file_list = NULL;
 			int file_count = -1;
-			file_count = scandir((root + name).CString(), &file_list, 0, alphasort);
+			file_count = scandir((root + name).c_str(), &file_list, 0, alphasort);
 			if (file_count == -1)
 				return;
 

+ 4 - 4
Samples/luainvaders/src/Game.cpp

@@ -225,7 +225,7 @@ void Game::SetScore(int score)
 
 	Rocket::Core::Element* score_element = context->GetDocument("game_window")->GetElementById("score");
 	if (score_element != NULL)
-		score_element->SetInnerRML(Rocket::Core::String(128, "%d", score).CString());
+		score_element->SetInnerRML(Rocket::Core::CreateString(128, "%d", score).c_str());
 
 	// Update the high score if we've beaten it.
 	if (score > HighScores::GetHighScore())
@@ -237,7 +237,7 @@ void Game::SetHighScore(int score)
 {
 	Rocket::Core::Element* high_score_element = context->GetDocument("game_window")->GetElementById("hiscore");
 	if (high_score_element != NULL)
-		high_score_element->SetInnerRML(Rocket::Core::String(128, "%d", score).CString());
+		high_score_element->SetInnerRML(Rocket::Core::CreateString(128, "%d", score).c_str());
 }
 
 void Game::SetLives(int lives)
@@ -246,7 +246,7 @@ void Game::SetLives(int lives)
 
 	Rocket::Core::Element* score_element = context->GetDocument("game_window")->GetElementById("lives");
 	if (score_element != NULL)
-		score_element->SetInnerRML(Rocket::Core::String(128, "%d", defender_lives).CString());
+		score_element->SetInnerRML(Rocket::Core::CreateString(128, "%d", defender_lives).c_str());
 }
 
 void Game::SetWave(int wave)
@@ -255,7 +255,7 @@ void Game::SetWave(int wave)
 
 	Rocket::Core::Element* waves_element = context->GetDocument("game_window")->GetElementById("waves");
 	if (waves_element != NULL)
-		waves_element->SetInnerRML(Rocket::Core::String(128, "%d", wave).CString());
+		waves_element->SetInnerRML(Rocket::Core::CreateString(128, "%d", wave).c_str());
 }
 
 void Game::RemoveLife()

+ 5 - 5
Samples/luainvaders/src/HighScores.cpp

@@ -75,11 +75,11 @@ void HighScores::GetRow(Rocket::Core::StringList& row, const Rocket::Core::Strin
 			}
 			else if (columns[i] == "name_required")
 			{
-				row.push_back(Rocket::Core::String(4, "%d", scores[row_index].name_required));
+				row.push_back(Rocket::Core::CreateString(4, "%d", scores[row_index].name_required));
 			}
 			else if (columns[i] == "score")
 			{
-				row.push_back(Rocket::Core::String(32, "%d", scores[row_index].score));
+				row.push_back(Rocket::Core::CreateString(32, "%d", scores[row_index].score));
 			}
 			else if (columns[i] == "colour")
 			{
@@ -89,7 +89,7 @@ void HighScores::GetRow(Rocket::Core::StringList& row, const Rocket::Core::Strin
 			}
 			else if (columns[i] == "wave")
 			{
-				row.push_back(Rocket::Core::String(8, "%d", scores[row_index].wave));
+				row.push_back(Rocket::Core::CreateString(8, "%d", scores[row_index].wave));
 			}
 		}
 	}
@@ -227,8 +227,8 @@ void HighScores::SaveScores()
 			Rocket::Core::String colour_string;
 			Rocket::Core::TypeConverter< Rocket::Core::Colourb, Rocket::Core::String >::Convert(scores[i].colour, colour_string);
 
-			Rocket::Core::String score(1024, "%s\t%s\t%d\t%d\n", scores[i].name.CString(), colour_string.CString(), scores[i].wave, scores[i].score);
-			fputs(score.CString(), scores_file);		
+			Rocket::Core::String score = Rocket::Core::CreateString(1024, "%s\t%s\t%d\t%d\n", scores[i].name.c_str(), colour_string.c_str(), scores[i].wave, scores[i].score);
+			fputs(score.c_str(), scores_file);		
 		}
 
 		fclose(scores_file);

+ 1 - 1
Source/Controls/ElementTabSet.cpp

@@ -73,7 +73,7 @@ void ElementTabSet::SetTab(int tab_index, Core::Element* element)
 // Set the specified tab index's body element.
 void ElementTabSet::SetPanel(int tab_index, Core::Element* element)
 {	
-	// Append the window
+	// append the window
 	Core::Element* windows = GetChildByTag("panels");
 	if (tab_index >= 0 &&
 		tab_index < windows->GetNumChildren())

+ 2 - 2
Source/Controls/Lua/ElementFormControl.cpp

@@ -50,7 +50,7 @@ int ElementFormControlGetAttrname(lua_State* L)
 {
     ElementFormControl* efc = LuaType<ElementFormControl>::check(L,1);
     LUACHECKOBJ(efc);
-    lua_pushstring(L,efc->GetName().CString());
+    lua_pushstring(L,efc->GetName().c_str());
     return 1;
 }
 
@@ -58,7 +58,7 @@ int ElementFormControlGetAttrvalue(lua_State* L)
 {
     ElementFormControl* efc = LuaType<ElementFormControl>::check(L,1);
     LUACHECKOBJ(efc);
-    lua_pushstring(L,efc->GetValue().CString());
+    lua_pushstring(L,efc->GetValue().c_str());
     return 1;
 }
 

+ 1 - 1
Source/Controls/Lua/LuaDataFormatter.cpp

@@ -67,7 +67,7 @@ void LuaDataFormatter::FormatData(Rocket::Core::String& formatted_data, const Ro
     int tbl = lua_gettop(L);
     for(unsigned int i = 0; i < raw_data.size(); i++)
     {
-        lua_pushstring(L,raw_data[i].CString());
+        lua_pushstring(L,raw_data[i].c_str());
         lua_rawseti(L,tbl,i);
     }
     Interpreter::ExecuteCall(1,1); //1 parameter (the table), 1 result (a string)

+ 3 - 3
Source/Controls/Lua/LuaDataSource.cpp

@@ -54,13 +54,13 @@ void LuaDataSource::GetRow(Rocket::Core::StringList& row, const Rocket::Core::St
     //setup the call
     Interpreter::BeginCall(getRowRef);
     lua_State* L = Interpreter::GetLuaState();
-    lua_pushstring(L,table.CString());
+    lua_pushstring(L,table.c_str());
     lua_pushinteger(L,row_index);
     lua_newtable(L);
     int index = 0;
     for(Rocket::Core::StringList::const_iterator itr = columns.begin(); itr != columns.end(); ++itr)
     {
-        lua_pushstring(L,itr->CString());
+        lua_pushstring(L,itr->c_str());
         lua_rawseti(L,-2,index++);
     }
     Interpreter::ExecuteCall(3,1); //3 parameters, 1 return. After here, the top of the stack contains the return value
@@ -92,7 +92,7 @@ int LuaDataSource::GetNumRows(const Rocket::Core::String& table)
 
     lua_State* L = Interpreter::GetLuaState();
     Interpreter::BeginCall(getNumRowsRef);
-    lua_pushstring(L,table.CString());
+    lua_pushstring(L,table.c_str());
     Interpreter::ExecuteCall(1,1); //1 parameter, 1 return. After this, the top of the stack contains the return value
 
     int res = lua_gettop(L);

+ 2 - 2
Source/Controls/Lua/SelectOptionsProxy.cpp

@@ -49,7 +49,7 @@ int SelectOptionsProxy__index(lua_State* L)
         lua_newtable(L);
         LuaType<Rocket::Core::Element>::push(L,opt->GetElement(),false);
         lua_setfield(L,-2,"element");
-        lua_pushstring(L,opt->GetValue().CString());
+        lua_pushstring(L,opt->GetValue().c_str());
         lua_setfield(L,-2,"value");
         return 1;
     }
@@ -91,7 +91,7 @@ int SelectOptionsProxy__ipairs(lua_State* L)
         //fill the value
         LuaType<Rocket::Core::Element>::push(L,opt->GetElement());
         lua_setfield(L,-2,"element");
-        lua_pushstring(L,opt->GetValue().CString());
+        lua_pushstring(L,opt->GetValue().c_str());
         lua_setfield(L,-2,"value");
     }
     return 2;

+ 1 - 1
Source/Core/Lua/Context.cpp

@@ -166,7 +166,7 @@ int ContextGetAttrname(lua_State* L)
 {
     Context* cont = LuaType<Context>::check(L,1);
     LUACHECKOBJ(cont);
-    lua_pushstring(L,cont->GetName().CString());
+    lua_pushstring(L,cont->GetName().c_str());
     return 1;
 }
 

+ 1 - 1
Source/Core/Lua/ContextDocumentsProxy.cpp

@@ -86,7 +86,7 @@ int ContextDocumentsProxy__pairs(lua_State* L)
     //If we found a document 
     if(doc != NULL)
     {
-        lua_pushstring(L,doc->GetId().CString());
+        lua_pushstring(L,doc->GetId().c_str());
         LuaType<Document>::push(L,doc);
     }
     else //if we were at the end and didn't find a document

+ 1 - 1
Source/Core/Lua/Document.cpp

@@ -123,7 +123,7 @@ int DocumentGetAttrtitle(lua_State* L)
 {
     Document* doc = LuaType<Document>::check(L,1);
     LUACHECKOBJ(doc);
-    lua_pushstring(L,doc->GetTitle().CString());
+    lua_pushstring(L,doc->GetTitle().c_str());
     return 1;
 }
 

+ 8 - 8
Source/Core/Lua/Element.cpp

@@ -130,17 +130,17 @@ int ElementDispatchEvent(lua_State* L, Element* obj)
         switch(type)
         {
 		case LUA_TNUMBER:
-            params.Set(key,(float)lua_tonumber(L,-1));
+            params[key] = (float)lua_tonumber(L,-1);
             break;
 		case LUA_TBOOLEAN: 
-            params.Set(key,CHECK_BOOL(L,-1));
+            params[key] = CHECK_BOOL(L,-1);
             break;
 		case LUA_TSTRING:
-            params.Set(key,luaL_checkstring(L,-1));
+            params[key] = luaL_checkstring(L,-1);
             break;
         case LUA_TUSERDATA:
         case LUA_TLIGHTUSERDATA:
-            params.Set(key,lua_touserdata(L,-1));
+            params[key] = lua_touserdata(L,-1);
             break;
         default:
             break;
@@ -285,7 +285,7 @@ int ElementGetAttrclass_name(lua_State* L)
 {
     Element* ele = LuaType<Element>::check(L,1);
     LUACHECKOBJ(ele);
-    const char* classnames = ele->GetClassNames().CString();
+    const char* classnames = ele->GetClassNames().c_str();
     lua_pushstring(L,classnames);
     return 1;
 }
@@ -338,7 +338,7 @@ int ElementGetAttrid(lua_State* L)
 {
     Element* ele = LuaType<Element>::check(L,1);
     LUACHECKOBJ(ele);
-    lua_pushstring(L,ele->GetId().CString());
+    lua_pushstring(L,ele->GetId().c_str());
     return 1;
 }
 
@@ -346,7 +346,7 @@ int ElementGetAttrinner_rml(lua_State* L)
 {
     Element* ele = LuaType<Element>::check(L,1);
     LUACHECKOBJ(ele);
-    lua_pushstring(L,ele->GetInnerRML().CString());
+    lua_pushstring(L,ele->GetInnerRML().c_str());
     return 1;
 }
 
@@ -494,7 +494,7 @@ int ElementGetAttrtag_name(lua_State* L)
 {
     Element* ele = LuaType<Element>::check(L,1);
     LUACHECKOBJ(ele);
-    lua_pushstring(L,ele->GetTagName().CString());
+    lua_pushstring(L,ele->GetTagName().c_str());
     return 1;
 }
 

+ 7 - 5
Source/Core/Lua/ElementAttributesProxy.cpp

@@ -68,12 +68,14 @@ int ElementAttributesProxy__pairs(lua_State* L)
     int* pindex = (int*)lua_touserdata(L,3);
     if((*pindex) == -1) 
         *pindex = 0;
-    String key = "";
-    Variant* val;
-    if(obj->owner->IterateAttributes((*pindex),key,val))
+	const ElementAttributes& attributes = obj->owner->GetAttributes();
+
+    if(*pindex >= 0 && *pindex < (int)attributes.size())
     {
-        lua_pushstring(L,key.CString());
-        PushVariant(L,val);
+		const String& key = attributes.container()[*pindex].first;
+		const Variant* value = &attributes.container()[*pindex].second;
+        lua_pushstring(L,key.c_str());
+        PushVariant(L,value);
     }
     else
     {

+ 3 - 3
Source/Core/Lua/ElementStyleProxy.cpp

@@ -58,7 +58,7 @@ int ElementStyleProxy__index(lua_State* L)
         LUACHECKOBJ(es);
         const Property* prop = es->owner->GetProperty(lua_tostring(L,2));
         LUACHECKOBJ(prop)
-        lua_pushstring(L,prop->ToString().CString());
+        lua_pushstring(L,prop->ToString().c_str());
         return 1;
     }
     else //if it wasn't trying to get a string
@@ -112,8 +112,8 @@ int ElementStyleProxy__pairs(lua_State* L)
     if(obj->owner->IterateProperties((*pindex),key,prop))
     {
         prop->definition->GetValue(val,*prop);
-        lua_pushstring(L,key.CString());
-        lua_pushstring(L,val.CString());
+        lua_pushstring(L,key.c_str());
+        lua_pushstring(L,val.c_str());
     }
     else
     {

+ 2 - 3
Source/Core/Lua/ElementText.cpp

@@ -45,8 +45,7 @@ int ElementTextGetAttrtext(lua_State* L)
 {
     ElementText* obj = LuaType<ElementText>::check(L, 1);
     LUACHECKOBJ(obj);
-    String temp;
-    lua_pushstring(L,obj->GetText().ToUTF8(temp).CString());
+    lua_pushstring(L, ToUTF8(obj->GetText()).c_str());
     return 1;
 }
 
@@ -55,7 +54,7 @@ int ElementTextSetAttrtext(lua_State* L)
     ElementText* obj = LuaType<ElementText>::check(L, 1);
     LUACHECKOBJ(obj);
     const char* text = luaL_checkstring(L,2);
-    obj->SetText(text);
+    obj->SetText(ToWideString(text));
     return 0;
 }
 

+ 1 - 1
Source/Core/Lua/Event.cpp

@@ -61,7 +61,7 @@ int EventGetAttrtype(lua_State* L)
     Event* evt = LuaType<Event>::check(L,1);
     LUACHECKOBJ(evt);
     String type = evt->GetType();
-    lua_pushstring(L,type.CString());
+    lua_pushstring(L,type.c_str());
     return 1;
 }
 

+ 7 - 5
Source/Core/Lua/EventParametersProxy.cpp

@@ -55,7 +55,8 @@ int EventParametersProxy__index(lua_State* L)
         EventParametersProxy* obj = LuaType<EventParametersProxy>::check(L,1);
         LUACHECKOBJ(obj);
         const char* key = lua_tostring(L,2);
-        Variant* param = obj->owner->GetParameters()->Get(key);
+		auto it = obj->owner->GetParameters()->find(key);
+		const Variant* param = (it == obj->owner->GetParameters()->end() ? nullptr : &it->second);
         PushVariant(L,param);
         return 1;
     }
@@ -72,11 +73,12 @@ int EventParametersProxy__pairs(lua_State* L)
     int* pindex = (int*)lua_touserdata(L,3);
     if((*pindex) == -1)
         *pindex = 0;
-    String key = "";
-    Variant* value = NULL;
-    if(obj->owner->GetParameters()->Iterate((*pindex),key,value))
+	const Dictionary& attributes = *obj->owner->GetParameters();
+    if(pindex >= 0 && *pindex < (int)attributes.size())
     {
-        lua_pushstring(L,key.CString());
+		const String& key = attributes.container()[*pindex].first;
+		const Variant* value = &attributes.container()[*pindex].second;
+        lua_pushstring(L,key.c_str());
         PushVariant(L,value);
     }
     else

+ 1 - 1
Source/Core/Lua/GlobalLuaFunctions.cpp

@@ -138,7 +138,7 @@ int LuaPrint(lua_State* L)
         lua_pop(L, 1);  /* pop result */
     }
     output += "\n";
-    Log::Message(Log::LT_INFO, output.CString());
+    Log::Message(Log::LT_INFO, output.c_str());
     return 0;
 }
 

+ 5 - 5
Source/Core/Lua/Interpreter.cpp

@@ -110,14 +110,14 @@ void Interpreter::LoadFile(const String& file)
     Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
     Rocket::Core::FileHandle handle = file_interface->Open(file);
     if(handle == 0) {
-        lua_pushfstring(_L, "LoadFile: Unable to open file: %s", file.CString());
+        lua_pushfstring(_L, "LoadFile: Unable to open file: %s", file.c_str());
         Report(_L);
         return;
     }
 
     size_t size = file_interface->Length(handle);
     if(size == 0) {
-        lua_pushfstring(_L, "LoadFile: File is 0 bytes in size: %s", file.CString());
+        lua_pushfstring(_L, "LoadFile: File is 0 bytes in size: %s", file.c_str());
         Report(_L);
         return;
     }
@@ -125,7 +125,7 @@ void Interpreter::LoadFile(const String& file)
     file_interface->Read(file_contents,size,handle);
     file_interface->Close(handle);
 
-    if(luaL_loadbuffer(_L,file_contents,size,file.CString()) != 0)
+    if(luaL_loadbuffer(_L,file_contents,size,file.c_str()) != 0)
         Report(_L); 
     else //if there were no errors loading, then the compiled function is on the top of the stack
     {
@@ -139,7 +139,7 @@ void Interpreter::LoadFile(const String& file)
 
 void Interpreter::DoString(const Rocket::Core::String& code, const Rocket::Core::String& name)
 {
-    if(luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString()) != 0)
+    if(luaL_loadbuffer(_L,code.c_str(),code.length(), name.c_str()) != 0)
         Report(_L);
     else
     {
@@ -150,7 +150,7 @@ void Interpreter::DoString(const Rocket::Core::String& code, const Rocket::Core:
 
 void Interpreter::LoadString(const Rocket::Core::String& code, const Rocket::Core::String& name)
 {
-    if(luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString()) != 0)
+    if(luaL_loadbuffer(_L,code.c_str(),code.length(), name.c_str()) != 0)
         Report(_L);
 }
 

+ 1 - 1
Source/Core/Lua/LuaElementInstancer.cpp

@@ -61,7 +61,7 @@ Element* LuaElementInstancer::InstanceElement(Element* ROCKET_UNUSED_PARAMETER(p
     {
         PushFunctionsTable(L);
         lua_rawgeti(L,-1,ref_InstanceElement); //push the function
-        lua_pushstring(L,tag.CString()); //push the tag
+        lua_pushstring(L,tag.c_str()); //push the tag
         Interpreter::ExecuteCall(1,1); //we pass in a string, and we want to get an Element back
         ret = LuaType<Element>::check(L,-1);
     }

+ 3 - 3
Source/Core/Lua/LuaEventListener.cpp

@@ -40,8 +40,8 @@ LuaEventListener::LuaEventListener(const String& code, Element* element) : Event
 {
     //compose function
     String function = "return function (event,element,document) ";
-    function.Append(code);
-    function.Append(" end");
+    function.append(code);
+    function.append(" end");
 
     //make sure there is an area to save the function
     lua_State* L = Interpreter::GetLuaState();
@@ -57,7 +57,7 @@ LuaEventListener::LuaEventListener(const String& code, Element* element) : Event
     int tbl = lua_gettop(L);
 
     //compile,execute,and save the function
-    if(luaL_loadstring(L,function.CString()) != 0)
+    if(luaL_loadstring(L,function.c_str()) != 0)
     {
         Report(L);
         return;

+ 1 - 1
Source/Core/Lua/RocketContextsProxy.cpp

@@ -90,7 +90,7 @@ int RocketContextsProxy__pairs(lua_State* L)
     }
     else
     {
-        lua_pushstring(L,value->GetName().CString());
+        lua_pushstring(L,value->GetName().c_str());
         LuaType<Context>::push(L,value);
     }
     return 2;

+ 4 - 4
Source/Core/Lua/Utilities.cpp

@@ -32,7 +32,7 @@ namespace Rocket {
 namespace Core {
 namespace Lua {
 
-void PushVariant(lua_State* L, Variant* var)
+void PushVariant(lua_State* L, const Variant* var)
 {
     if(var == NULL)
     {
@@ -57,7 +57,7 @@ void PushVariant(lua_State* L, Variant* var)
         LuaType<Colourf>::push(L,new Colourf(var->Get<Colourf>()),true);
         break;
     case Variant::STRING:
-        lua_pushstring(L,var->Get<String>().CString());
+        lua_pushstring(L,var->Get<String>().c_str());
         break;
     case Variant::VECTOR2:
         //according to Variant.inl, it is going to be a Vector2f
@@ -83,8 +83,8 @@ void Report(lua_State* L, const Rocket::Core::String& place)
         if(place == "")
             strmsg = msg;
         else
-            strmsg = String(place).Append(" ").Append(msg);
-        Log::Message(Log::LT_WARNING, strmsg.CString());
+            strmsg = String(place).append(" ").append(msg);
+        Log::Message(Log::LT_WARNING, strmsg.c_str());
         msg=lua_tostring(L,-1);
     }
 }