changeset 15880:383c02988b9d draft

(svn r20562) -Change: [NoAI] Move all functions from AIList to AIAbstractList
author yexo <yexo@openttd.org>
date Thu, 19 Aug 2010 15:19:40 +0000
parents 17cb2ee71a84
children 75df4bf4f199
files projects/openttd_vs100.vcxproj projects/openttd_vs100.vcxproj.filters projects/openttd_vs80.vcproj projects/openttd_vs90.vcproj source.list src/ai/ai_instance.cpp src/ai/api/ai_abstractlist.cpp src/ai/api/ai_abstractlist.hpp src/ai/api/ai_abstractlist.hpp.sq src/ai/api/ai_industrytype.cpp src/ai/api/ai_industrytype.hpp src/ai/api/ai_list.cpp src/ai/api/ai_list.hpp src/ai/api/ai_list.hpp.sq
diffstat 14 files changed, 49 insertions(+), 187 deletions(-) [+]
line wrap: on
line diff
--- a/projects/openttd_vs100.vcxproj
+++ b/projects/openttd_vs100.vcxproj
@@ -873,7 +873,6 @@
     <ClInclude Include="..\src\ai\api\ai_industrytype.hpp" />
     <ClInclude Include="..\src\ai\api\ai_industrytypelist.hpp" />
     <ClInclude Include="..\src\ai\api\ai_info_docs.hpp" />
-    <ClInclude Include="..\src\ai\api\ai_list.hpp" />
     <ClInclude Include="..\src\ai\api\ai_log.hpp" />
     <ClInclude Include="..\src\ai\api\ai_map.hpp" />
     <ClInclude Include="..\src\ai\api\ai_marine.hpp" />
@@ -925,7 +924,6 @@
     <ClCompile Include="..\src\ai\api\ai_industrylist.cpp" />
     <ClCompile Include="..\src\ai\api\ai_industrytype.cpp" />
     <ClCompile Include="..\src\ai\api\ai_industrytypelist.cpp" />
-    <ClCompile Include="..\src\ai\api\ai_list.cpp" />
     <ClCompile Include="..\src\ai\api\ai_log.cpp" />
     <ClCompile Include="..\src\ai\api\ai_map.cpp" />
     <ClCompile Include="..\src\ai\api\ai_marine.cpp" />
--- a/projects/openttd_vs100.vcxproj.filters
+++ b/projects/openttd_vs100.vcxproj.filters
@@ -1822,9 +1822,6 @@
     <ClInclude Include="..\src\ai\api\ai_info_docs.hpp">
       <Filter>AI API</Filter>
     </ClInclude>
-    <ClInclude Include="..\src\ai\api\ai_list.hpp">
-      <Filter>AI API</Filter>
-    </ClInclude>
     <ClInclude Include="..\src\ai\api\ai_log.hpp">
       <Filter>AI API</Filter>
     </ClInclude>
@@ -1978,9 +1975,6 @@
     <ClCompile Include="..\src\ai\api\ai_industrytypelist.cpp">
       <Filter>AI API Implementation</Filter>
     </ClCompile>
-    <ClCompile Include="..\src\ai\api\ai_list.cpp">
-      <Filter>AI API Implementation</Filter>
-    </ClCompile>
     <ClCompile Include="..\src\ai\api\ai_log.cpp">
       <Filter>AI API Implementation</Filter>
     </ClCompile>
--- a/projects/openttd_vs80.vcproj
+++ b/projects/openttd_vs80.vcproj
@@ -2808,10 +2808,6 @@
 				>
 			</File>
 			<File
-				RelativePath=".\..\src\ai\api\ai_list.hpp"
-				>
-			</File>
-			<File
 				RelativePath=".\..\src\ai\api\ai_log.hpp"
 				>
 			</File>
@@ -3020,10 +3016,6 @@
 				>
 			</File>
 			<File
-				RelativePath=".\..\src\ai\api\ai_list.cpp"
-				>
-			</File>
-			<File
 				RelativePath=".\..\src\ai\api\ai_log.cpp"
 				>
 			</File>
--- a/projects/openttd_vs90.vcproj
+++ b/projects/openttd_vs90.vcproj
@@ -2805,10 +2805,6 @@
 				>
 			</File>
 			<File
-				RelativePath=".\..\src\ai\api\ai_list.hpp"
-				>
-			</File>
-			<File
 				RelativePath=".\..\src\ai\api\ai_log.hpp"
 				>
 			</File>
@@ -3017,10 +3013,6 @@
 				>
 			</File>
 			<File
-				RelativePath=".\..\src\ai\api\ai_list.cpp"
-				>
-			</File>
-			<File
 				RelativePath=".\..\src\ai\api\ai_log.cpp"
 				>
 			</File>
--- a/source.list
+++ b/source.list
@@ -639,7 +639,6 @@
 ai/api/ai_industrytype.hpp
 ai/api/ai_industrytypelist.hpp
 ai/api/ai_info_docs.hpp
-ai/api/ai_list.hpp
 ai/api/ai_log.hpp
 ai/api/ai_map.hpp
 ai/api/ai_marine.hpp
@@ -693,7 +692,6 @@
 ai/api/ai_industrylist.cpp
 ai/api/ai_industrytype.cpp
 ai/api/ai_industrytypelist.cpp
-ai/api/ai_list.cpp
 ai/api/ai_log.cpp
 ai/api/ai_map.cpp
 ai/api/ai_marine.cpp
--- a/src/ai/ai_instance.cpp
+++ b/src/ai/ai_instance.cpp
@@ -52,7 +52,6 @@
 #include "api/ai_industrylist.hpp.sq"
 #include "api/ai_industrytype.hpp.sq"
 #include "api/ai_industrytypelist.hpp.sq"
-#include "api/ai_list.hpp.sq"
 #include "api/ai_log.hpp.sq"
 #include "api/ai_map.hpp.sq"
 #include "api/ai_marine.hpp.sq"
@@ -222,7 +221,6 @@
 	SQAIIndustryList_CargoProducing_Register(this->engine);
 	SQAIIndustryType_Register(this->engine);
 	SQAIIndustryTypeList_Register(this->engine);
-	SQAIList_Register(this->engine);
 	SQAILog_Register(this->engine);
 	SQAIMap_Register(this->engine);
 	SQAIMarine_Register(this->engine);
--- a/src/ai/api/ai_abstractlist.cpp
+++ b/src/ai/api/ai_abstractlist.cpp
@@ -404,7 +404,7 @@
 	this->sorter->End();
 }
 
-void AIAbstractList::AddItem(int32 item)
+void AIAbstractList::AddItem(int32 item, int32 value)
 {
 	this->modifications++;
 
@@ -412,6 +412,8 @@
 
 	this->items[item] = 0;
 	this->buckets[0].insert(item);
+
+	this->SetValue(item, value);
 }
 
 void AIAbstractList::RemoveItem(int32 item)
@@ -734,6 +736,30 @@
 	return 1;
 }
 
+SQInteger AIAbstractList::_set(HSQUIRRELVM vm)
+{
+	if (sq_gettype(vm, 2) != OT_INTEGER) return SQ_ERROR;
+	if (sq_gettype(vm, 3) != OT_INTEGER || sq_gettype(vm, 3) == OT_NULL) {
+		return sq_throwerror(vm, _SC("you can only assign integers to this list"));
+	}
+
+	SQInteger idx, val;
+	sq_getinteger(vm, 2, &idx);
+	if (sq_gettype(vm, 3) == OT_NULL) {
+		this->RemoveItem(idx);
+		return 0;
+	}
+
+	sq_getinteger(vm, 3, &val);
+	if (!this->HasItem(idx)) {
+		this->AddItem(idx, val);
+		return 0;
+	}
+
+	this->SetValue(idx, val);
+	return 0;
+}
+
 SQInteger AIAbstractList::_nexti(HSQUIRRELVM vm)
 {
 	if (sq_gettype(vm, 2) == OT_NULL) {
--- a/src/ai/api/ai_abstractlist.hpp
+++ b/src/ai/api/ai_abstractlist.hpp
@@ -53,25 +53,23 @@
 	AIAbstractListMap items;           ///< The items in the list
 	AIAbstractListBucket buckets;      ///< The items in the list, sorted by value
 
-protected:
+	AIAbstractList();
+	~AIAbstractList();
+
 	/**
 	 * Add a single item to the list.
 	 * @param item the item to add. Should be unique, otherwise it is ignored.
+	 * @param value the value to assign.
 	 * @note the value is set to 0 by default.
 	 */
-	void AddItem(int32 item);
+	void AddItem(int32 item, int32 value = 0);
 
 	/**
 	 * Remove a single item from the list.
 	 * @param item the item to remove. If not existing, it is ignored.
-	 * @note Always use this function for removing items. It keeps the iterator valid!
 	 */
 	void RemoveItem(int32 item);
 
-public:
-	AIAbstractList();
-	~AIAbstractList();
-
 	/**
 	 * Clear the list, making Count() returning 0 and IsEmpty() returning true.
 	 */
@@ -248,6 +246,11 @@
 	SQInteger _get(HSQUIRRELVM vm);
 
 	/**
+	 * Used for [] set from Squirrel.
+	 */
+	SQInteger _set(HSQUIRRELVM vm);
+
+	/**
 	 * Used for 'foreach()' from Squirrel.
 	 */
 	SQInteger _nexti(HSQUIRRELVM vm);
--- a/src/ai/api/ai_abstractlist.hpp.sq
+++ b/src/ai/api/ai_abstractlist.hpp.sq
@@ -36,6 +36,8 @@
 	SQAIAbstractList.DefSQConst(engine, AIAbstractList::SORT_ASCENDING,  "SORT_ASCENDING");
 	SQAIAbstractList.DefSQConst(engine, AIAbstractList::SORT_DESCENDING, "SORT_DESCENDING");
 
+	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::AddItem,            "AddItem",            3, "xii");
+	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::RemoveItem,         "RemoveItem",         2, "xi");
 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Clear,              "Clear",              1, "x");
 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::HasItem,            "HasItem",            2, "xi");
 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Begin,              "Begin",              1, "x");
@@ -62,6 +64,7 @@
 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::KeepBottom,         "KeepBottom",         2, "xi");
 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::KeepList,           "KeepList",           2, "xx");
 	SQAIAbstractList.DefSQAdvancedMethod(engine, &AIAbstractList::_get,       "_get");
+	SQAIAbstractList.DefSQAdvancedMethod(engine, &AIAbstractList::_set,       "_set");
 	SQAIAbstractList.DefSQAdvancedMethod(engine, &AIAbstractList::_nexti,     "_nexti");
 	SQAIAbstractList.DefSQAdvancedMethod(engine, &AIAbstractList::Valuate,    "Valuate");
 
--- a/src/ai/api/ai_industrytype.cpp
+++ b/src/ai/api/ai_industrytype.cpp
@@ -58,29 +58,29 @@
 	return industrytype_name;
 }
 
-/* static */ AIList *AIIndustryType::GetProducedCargo(IndustryType industry_type)
+/* static */ AIAbstractList *AIIndustryType::GetProducedCargo(IndustryType industry_type)
 {
 	if (!IsValidIndustryType(industry_type)) return NULL;
 
 	const IndustrySpec *ins = ::GetIndustrySpec(industry_type);
 
-	AIList *list = new AIList();
+	AIAbstractList *list = new AIAbstractList();
 	for (size_t i = 0; i < lengthof(ins->produced_cargo); i++) {
-		if (ins->produced_cargo[i] != CT_INVALID) list->AddItem(ins->produced_cargo[i], 0);
+		if (ins->produced_cargo[i] != CT_INVALID) list->AddItem(ins->produced_cargo[i]);
 	}
 
 	return list;
 }
 
-/* static */ AIList *AIIndustryType::GetAcceptedCargo(IndustryType industry_type)
+/* static */ AIAbstractList *AIIndustryType::GetAcceptedCargo(IndustryType industry_type)
 {
 	if (!IsValidIndustryType(industry_type)) return NULL;
 
 	const IndustrySpec *ins = ::GetIndustrySpec(industry_type);
 
-	AIList *list = new AIList();
+	AIAbstractList *list = new AIAbstractList();
 	for (size_t i = 0; i < lengthof(ins->accepts_cargo); i++) {
-		if (ins->accepts_cargo[i] != CT_INVALID) list->AddItem(ins->accepts_cargo[i], 0);
+		if (ins->accepts_cargo[i] != CT_INVALID) list->AddItem(ins->accepts_cargo[i]);
 	}
 
 	return list;
--- a/src/ai/api/ai_industrytype.hpp
+++ b/src/ai/api/ai_industrytype.hpp
@@ -14,7 +14,7 @@
 
 #include "ai_object.hpp"
 #include "ai_error.hpp"
-#include "ai_list.hpp"
+#include "ai_abstractlist.hpp"
 
 /**
  * Class that handles all industry-type related functions.
@@ -55,7 +55,7 @@
 	 * @pre IsValidIndustryType(industry_type).
 	 * @return The CargoIDs of all cargotypes this industry could produce.
 	 */
-	static AIList *GetProducedCargo(IndustryType industry_type);
+	static AIAbstractList *GetProducedCargo(IndustryType industry_type);
 
 	/**
 	 * Get a list of CargoID accepted by this industry-type.
@@ -65,7 +65,7 @@
 	 * @pre IsValidIndustryType(industry_type).
 	 * @return The CargoIDs of all cargotypes this industry accepts.
 	 */
-	static AIList *GetAcceptedCargo(IndustryType industry_type);
+	static AIAbstractList *GetAcceptedCargo(IndustryType industry_type);
 
 	/**
 	 * Is this industry type a raw industry?
deleted file mode 100644
--- a/src/ai/api/ai_list.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-/* $Id$ */
-
-/*
- * This file is part of OpenTTD.
- * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
- * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/** @file ai_list.cpp Implementation of AIList. */
-
-#include "ai_list.hpp"
-
-void AIList::AddItem(int32 item, int32 value)
-{
-	AIAbstractList::AddItem(item);
-	this->SetValue(item, value);
-}
-
-void AIList::ChangeItem(int32 item, int32 value)
-{
-	this->SetValue(item, value);
-}
-
-void AIList::RemoveItem(int32 item)
-{
-	AIAbstractList::RemoveItem(item);
-}
-
-SQInteger AIList::_set(HSQUIRRELVM vm)
-{
-	if (sq_gettype(vm, 2) != OT_INTEGER) return SQ_ERROR;
-	if (sq_gettype(vm, 3) != OT_INTEGER || sq_gettype(vm, 3) == OT_NULL) {
-		return sq_throwerror(vm, _SC("you can only assign integers to this list"));
-	}
-
-	SQInteger idx, val;
-	sq_getinteger(vm, 2, &idx);
-	if (sq_gettype(vm, 3) == OT_NULL) {
-		this->RemoveItem(idx);
-		return 0;
-	}
-
-	sq_getinteger(vm, 3, &val);
-	if (!this->HasItem(idx)) {
-		this->AddItem(idx, val);
-		return 0;
-	}
-
-	this->ChangeItem(idx, val);
-	return 0;
-}
deleted file mode 100644
--- a/src/ai/api/ai_list.hpp
+++ /dev/null
@@ -1,55 +0,0 @@
-/* $Id$ */
-
-/*
- * This file is part of OpenTTD.
- * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
- * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/** @file ai_list.hpp List custom entries. */
-
-#ifndef AI_LIST_HPP
-#define AI_LIST_HPP
-
-#include "ai_abstractlist.hpp"
-
-/**
- * Creates an empty list, in which you can add integers.
- * @ingroup AIList
- */
-class AIList : public AIAbstractList {
-public:
-	/** Get the name of this class to identify it towards squirrel. */
-	static const char *GetClassName() { return "AIList"; }
-
-public:
-	/**
-	 * Add an item to the list.
-	 * @param item the item to add.
-	 * @param value the value to assign.
-	 */
-	void AddItem(int32 item, int32 value);
-
-	/**
-	 * Change the value of an item in the list.
-	 * @param item the item to change
-	 * @param value the value to assign.
-	 */
-	void ChangeItem(int32 item, int32 value);
-
-	/**
-	 * Remove the item from the list.
-	 * @param item the item to remove.
-	 */
-	void RemoveItem(int32 item);
-
-#ifndef DOXYGEN_SKIP
-	/**
-	 * Used for [] set from Squirrel.
-	 */
-	SQInteger _set(HSQUIRRELVM vm);
-#endif /* DOXYGEN_SKIP */
-};
-
-#endif /* AI_LIST_HPP */
deleted file mode 100644
--- a/src/ai/api/ai_list.hpp.sq
+++ /dev/null
@@ -1,35 +0,0 @@
-/* $Id$ */
-
-/*
- * This file is part of OpenTTD.
- * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
- * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/* THIS FILE IS AUTO-GENERATED; PLEASE DO NOT ALTER MANUALLY */
-
-#include "ai_list.hpp"
-
-namespace SQConvert {
-	/* Allow AIList to be used as Squirrel parameter */
-	template <> AIList *GetParam(ForceType<AIList *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIList *)instance; }
-	template <> AIList &GetParam(ForceType<AIList &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIList *)instance; }
-	template <> const AIList *GetParam(ForceType<const AIList *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIList *)instance; }
-	template <> const AIList &GetParam(ForceType<const AIList &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIList *)instance; }
-	template <> int Return<AIList *>(HSQUIRRELVM vm, AIList *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIList", res, NULL, DefSQDestructorCallback<AIList>); return 1; }
-} // namespace SQConvert
-
-void SQAIList_Register(Squirrel *engine)
-{
-	DefSQClass <AIList> SQAIList("AIList");
-	SQAIList.PreRegister(engine, "AIAbstractList");
-	SQAIList.AddConstructor<void (AIList::*)(), 1>(engine, "x");
-
-	SQAIList.DefSQMethod(engine, &AIList::AddItem,    "AddItem",    3, "xii");
-	SQAIList.DefSQMethod(engine, &AIList::ChangeItem, "ChangeItem", 3, "xii");
-	SQAIList.DefSQMethod(engine, &AIList::RemoveItem, "RemoveItem", 2, "xi");
-	SQAIList.DefSQAdvancedMethod(engine, &AIList::_set, "_set");
-
-	SQAIList.PostRegister(engine);
-}