Merge branch 'development' into master_joker
[openttd-joker.git] / src / cmd_helper.h
blobd17b59221bfae967d5f64bd6a0e32a0c5b756b4d
1 /* $Id: cmd_helper.h 23742 2012-01-03 21:58:26Z rubidium $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file cmd_helper.h Helper functions to extract data from command parameters. */
12 #ifndef CMD_HELPER_H
13 #define CMD_HELPER_H
15 #include "core/enum_type.hpp"
16 #include "core/bitmath_func.hpp"
18 /**
19 * Extracts a given type from a value.
20 * @tparam T The type of data we're looking for.
21 * @tparam S The offset in the data.
22 * @tparam N The amount of bits to read.
23 * @tparam U The type of data passed to us.
24 * @param v The data to extract the value from.
26 template<typename T, uint S, uint N, typename U> static inline T Extract(U v)
28 /* Check if there are enough bits in v */
29 assert_tcompile(N == EnumPropsT<T>::num_bits);
30 assert_tcompile(S + N <= sizeof(U) * 8);
31 assert_tcompile(EnumPropsT<T>::end <= (1 << N));
32 U masked = GB(v, S, N);
33 return IsInsideMM(masked, EnumPropsT<T>::begin, EnumPropsT<T>::end) ? (T)masked : EnumPropsT<T>::invalid;
36 #endif /* CMD_HELPER_H */