Changeset - r23540:8ae17b2213ea
[Not reviewed]
master
0 2 0
Henry Wilson - 6 years ago 2019-03-13 20:55:31
m3henry@googlemail.com
Codechange: Use range-based for-loop in Auto[Free|Delete]SmallVector
2 files changed with 5 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/core/smallvec_type.hpp
Show inline comments
 
/* $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 smallvec_type.hpp Simple vector class that allows allocating an item without the need to copy this->data needlessly. */
 

	
 
#ifndef SMALLVEC_TYPE_HPP
 
#define SMALLVEC_TYPE_HPP
 

	
 
#include "alloc_func.hpp"
 
#include "mem_func.hpp"
 
#include <vector>
 
#include <algorithm>
 

	
 
/**
 
 * Helper function to append an item to a vector if it is not already contained
 
 * Consider using std::set, std::unordered_set or std::flat_set in new code
 
 *
 
 * @param vec A reference to the vector to be extended
 
 * @param item Reference to the item to be copy-constructed if not found
 
 *
 
 * @return Whether the item was already present
 
 */
 
template <typename T>
 
inline bool include(std::vector<T>& vec, const T &item)
 
{
 
	const bool is_member = std::find(vec.begin(), vec.end(), item) != vec.end();
 
	if (!is_member) vec.emplace_back(item);
 
	return is_member;
 
}
 

	
 
/**
 
 * Helper function to get the index of an item
 
 * Consider using std::set, std::unordered_set or std::flat_set in new code
 
 *
 
 * @param vec A reference to the vector to be extended
 
 * @param item Reference to the item to be search for
 
 *
 
 * @return Index of element if found, otherwise -1
 
 */
 
template <typename T>
 
int find_index(std::vector<T> const& vec, T const& item)
 
{
 
	auto const it = std::find(vec.begin(), vec.end(), item);
 
	if (it != vec.end()) return it - vec.begin();
 

	
 
	return -1;
 
}
 

	
 
/**
 
 * Helper function to append N default-constructed elements and get a pointer to the first new element
 
 * Consider using std::back_inserter in new code
 
 *
 
 * @param vec A reference to the vector to be extended
 
 * @param num Number of elements to be default-constructed
 
 *
 
 * @return Pointer to the first new element
 
 */
 
template <typename T>
 
T* grow(std::vector<T>& vec, std::size_t num)
 
{
 
	std::size_t const pos = vec.size();
 
	vec.resize(pos + num);
 
	return vec.data() + pos;
 
}
 

	
 
/**
 
 * Simple vector template class, with automatic free.
 
 *
 
 * @note There are no asserts in the class so you have
 
 *       to care about that you grab an item which is
 
 *       inside the list.
 
 *
 
 * @param T The type of the items stored, must be a pointer
 
 */
 
template <typename T>
 
class AutoFreeSmallVector : public std::vector<T> {
 
public:
 
	~AutoFreeSmallVector()
 
	{
 
		this->Clear();
 
	}
 

	
 
	/**
 
	 * Remove all items from the list.
 
	 */
 
	inline void Clear()
 
	{
 
		for (uint i = 0; i < std::vector<T>::size(); i++) {
 
			free(std::vector<T>::operator[](i));
 
		for (T p : *this) {
 
			free(p);
 
		}
 

	
 
		std::vector<T>::clear();
 
	}
 
};
 

	
 
/**
 
 * Simple vector template class, with automatic delete.
 
 *
 
 * @note There are no asserts in the class so you have
 
 *       to care about that you grab an item which is
 
 *       inside the list.
 
 *
 
 * @param T The type of the items stored, must be a pointer
 
 */
 
template <typename T>
 
class AutoDeleteSmallVector : public std::vector<T> {
 
public:
 
	~AutoDeleteSmallVector()
 
	{
 
		this->Clear();
 
	}
 

	
 
	/**
 
	 * Remove all items from the list.
 
	 */
 
	inline void Clear()
 
	{
 
		for (uint i = 0; i < std::vector<T>::size(); i++) {
 
			delete std::vector<T>::operator[](i);
 
		for (T p : *this) {
 
			delete p;
 
		}
 

	
 
		std::vector<T>::clear();
 
	}
 
};
 

	
 
typedef AutoFreeSmallVector<char*> StringList; ///< Type for a list of strings.
 

	
 
#endif /* SMALLVEC_TYPE_HPP */
src/script/squirrel_helper.hpp
Show inline comments
 
/* $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 squirrel_helper.hpp declarations and parts of the implementation of the class for convert code */
 

	
 
#ifndef SQUIRREL_HELPER_HPP
 
#define SQUIRREL_HELPER_HPP
 

	
 
#include "squirrel.hpp"
 
#include "../core/smallvec_type.hpp"
 
#include "../economy_type.h"
 
#include "../string_func.h"
 
#include "squirrel_helper_type.hpp"
 

	
 
template <class CL, ScriptType ST> const char *GetClassName();
 

	
 
/**
 
 * The Squirrel convert routines
 
 */
 
namespace SQConvert {
 
	/**
 
	 * Pointers assigned to this class will be free'd when this instance
 
	 *  comes out of scope. Useful to make sure you can use stredup(),
 
	 *  without leaking memory.
 
	 */
 
	struct SQAutoFreePointers : std::vector<void *> {
 
		~SQAutoFreePointers()
 
		{
 
			for (uint i = 0; i < std::vector<void *>::size(); i++) free(std::vector<void *>::operator[](i));
 
			for (void * p : *this) free(p);
 
		}
 
	};
 

	
 
	template <bool Y> struct YesT {
 
		static const bool Yes = Y;
 
		static const bool No = !Y;
 
	};
 

	
 
	/**
 
	 * Helper class to recognize if the given type is void. Usage: 'IsVoidT<T>::Yes'
 
	 */
 
	template <typename T> struct IsVoidT : YesT<false> {};
 
	template <> struct IsVoidT<void> : YesT<true> {};
 

	
 
	/**
 
	 * Helper class to recognize if the function/method return type is void.
 
	 */
 
	template <typename Tfunc> struct HasVoidReturnT;
 
	/* functions */
 
	template <typename Tretval> struct HasVoidReturnT<Tretval (*)()> : IsVoidT<Tretval> {};
 
	template <typename Tretval, typename Targ1> struct HasVoidReturnT<Tretval (*)(Targ1)> : IsVoidT<Tretval> {};
 
	template <typename Tretval, typename Targ1, typename Targ2> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2)> : IsVoidT<Tretval> {};
 
	template <typename Tretval, typename Targ1, typename Targ2, typename Targ3> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3)> : IsVoidT<Tretval> {};
 
	template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3, Targ4)> : IsVoidT<Tretval> {};
 
	template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5)> : IsVoidT<Tretval> {};
 
	template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10)> : IsVoidT<Tretval> {};
 
	/* methods */
 
	template <class Tcls, typename Tretval> struct HasVoidReturnT<Tretval (Tcls::*)()> : IsVoidT<Tretval> {};
 
	template <class Tcls, typename Tretval, typename Targ1> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1)> : IsVoidT<Tretval> {};
 
	template <class Tcls, typename Tretval, typename Targ1, typename Targ2> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2)> : IsVoidT<Tretval> {};
 
	template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3)> : IsVoidT<Tretval> {};
 
	template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4)> : IsVoidT<Tretval> {};
 
	template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5)> : IsVoidT<Tretval> {};
 
	template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10)> : IsVoidT<Tretval> {};
 

	
 

	
 
	/**
 
	 * Special class to make it possible for the compiler to pick the correct GetParam().
 
	 */
 
	template <typename T> class ForceType { };
 

	
 
	/**
 
	 * To return a value to squirrel, we call this function. It converts to the right format.
 
	 */
 
	template <typename T> static int Return(HSQUIRRELVM vm, T t);
 

	
 
	template <> inline int Return<uint8>       (HSQUIRRELVM vm, uint8 res)       { sq_pushinteger(vm, (int32)res); return 1; }
 
	template <> inline int Return<uint16>      (HSQUIRRELVM vm, uint16 res)      { sq_pushinteger(vm, (int32)res); return 1; }
 
	template <> inline int Return<uint32>      (HSQUIRRELVM vm, uint32 res)      { sq_pushinteger(vm, (int32)res); return 1; }
 
	template <> inline int Return<int8>        (HSQUIRRELVM vm, int8 res)        { sq_pushinteger(vm, res); return 1; }
 
	template <> inline int Return<int16>       (HSQUIRRELVM vm, int16 res)       { sq_pushinteger(vm, res); return 1; }
 
	template <> inline int Return<int32>       (HSQUIRRELVM vm, int32 res)       { sq_pushinteger(vm, res); return 1; }
 
	template <> inline int Return<int64>       (HSQUIRRELVM vm, int64 res)       { sq_pushinteger(vm, res); return 1; }
 
	template <> inline int Return<Money>       (HSQUIRRELVM vm, Money res)       { sq_pushinteger(vm, res); return 1; }
 
	template <> inline int Return<bool>        (HSQUIRRELVM vm, bool res)        { sq_pushbool   (vm, res); return 1; }
 
	template <> inline int Return<char *>      (HSQUIRRELVM vm, char *res)       { if (res == NULL) sq_pushnull(vm); else { sq_pushstring(vm, res, -1); free(res); } return 1; }
 
	template <> inline int Return<const char *>(HSQUIRRELVM vm, const char *res) { if (res == NULL) sq_pushnull(vm); else { sq_pushstring(vm, res, -1); } return 1; }
 
	template <> inline int Return<void *>      (HSQUIRRELVM vm, void *res)       { sq_pushuserpointer(vm, res); return 1; }
 
	template <> inline int Return<HSQOBJECT>   (HSQUIRRELVM vm, HSQOBJECT res)   { sq_pushobject(vm, res); return 1; }
 

	
 
	/**
 
	 * To get a param from squirrel, we call this function. It converts to the right format.
 
	 */
 
	template <typename T> static T GetParam(ForceType<T>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr);
 

	
 
	template <> inline uint8       GetParam(ForceType<uint8>       , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger     tmp; sq_getinteger    (vm, index, &tmp); return tmp; }
 
	template <> inline uint16      GetParam(ForceType<uint16>      , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger     tmp; sq_getinteger    (vm, index, &tmp); return tmp; }
 
	template <> inline uint32      GetParam(ForceType<uint32>      , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger     tmp; sq_getinteger    (vm, index, &tmp); return tmp; }
 
	template <> inline int8        GetParam(ForceType<int8>        , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger     tmp; sq_getinteger    (vm, index, &tmp); return tmp; }
 
	template <> inline int16       GetParam(ForceType<int16>       , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger     tmp; sq_getinteger    (vm, index, &tmp); return tmp; }
 
	template <> inline int32       GetParam(ForceType<int32>       , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger     tmp; sq_getinteger    (vm, index, &tmp); return tmp; }
 
	template <> inline int64       GetParam(ForceType<int64>       , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger     tmp; sq_getinteger    (vm, index, &tmp); return tmp; }
 
	template <> inline Money       GetParam(ForceType<Money>       , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger     tmp; sq_getinteger    (vm, index, &tmp); return tmp; }
 
	template <> inline bool        GetParam(ForceType<bool>        , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQBool        tmp; sq_getbool       (vm, index, &tmp); return tmp != 0; }
 
	template <> inline void       *GetParam(ForceType<void *>      , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; }
 
	template <> inline const char *GetParam(ForceType<const char *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)
 
	{
 
		/* Convert what-ever there is as parameter to a string */
 
		sq_tostring(vm, index);
 

	
 
		const SQChar *tmp;
 
		sq_getstring(vm, -1, &tmp);
 
		char *tmp_str = stredup(tmp);
 
		sq_poptop(vm);
 
		ptr->push_back((void *)tmp_str);
 
		str_validate(tmp_str, tmp_str + strlen(tmp_str));
 
		return tmp_str;
 
	}
 

	
 
	template <> inline Array      *GetParam(ForceType<Array *>,      HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)
 
	{
 
		/* Sanity check of the size. */
 
		if (sq_getsize(vm, index) > UINT16_MAX) throw sq_throwerror(vm, "an array used as parameter to a function is too large");
 

	
 
		SQObject obj;
 
		sq_getstackobj(vm, index, &obj);
0 comments (0 inline, 0 general)