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"
|
2007-02-24 09:42:39 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|