2007-02-24 09:42:39 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file cmd_helper.h Helper functions to extract data from command parameters. */
|
|
|
|
|
2007-02-24 09:42:39 +00:00
|
|
|
#ifndef CMD_HELPER_H
|
|
|
|
#define CMD_HELPER_H
|
|
|
|
|
2010-04-11 15:44:16 +00:00
|
|
|
#include "core/enum_type.hpp"
|
2011-05-21 08:40:50 +00:00
|
|
|
#include "core/bitmath_func.hpp"
|
2007-02-24 09:42:39 +00:00
|
|
|
|
2011-05-01 19:14:12 +00:00
|
|
|
/**
|
|
|
|
* Extracts a given type from a value.
|
|
|
|
* @tparam T The type of data we're looking for.
|
|
|
|
* @tparam S The offset in the data.
|
|
|
|
* @tparam N The amount of bits to read.
|
|
|
|
* @tparam U The type of data passed to us.
|
|
|
|
* @param v The data to extract the value from.
|
|
|
|
*/
|
2010-04-13 17:29:19 +00:00
|
|
|
template<typename T, uint S, uint N, typename U> static inline T Extract(U v)
|
2007-02-24 09:42:39 +00:00
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Check if there are enough bits in v */
|
2010-04-13 17:29:19 +00:00
|
|
|
assert_tcompile(N == EnumPropsT<T>::num_bits);
|
|
|
|
assert_tcompile(S + N <= sizeof(U) * 8);
|
|
|
|
assert_tcompile(EnumPropsT<T>::end <= (1 << N));
|
2010-04-17 13:31:41 +00:00
|
|
|
U masked = GB(v, S, N);
|
|
|
|
return IsInsideMM(masked, EnumPropsT<T>::begin, EnumPropsT<T>::end) ? (T)masked : EnumPropsT<T>::invalid;
|
2007-02-24 09:42:39 +00:00
|
|
|
}
|
|
|
|
|
2010-12-22 10:50:32 +00:00
|
|
|
#endif /* CMD_HELPER_H */
|