1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file cmd_helper.h Helper functions to extract data from command parameters. */
9 
10 #ifndef CMD_HELPER_H
11 #define CMD_HELPER_H
12 
13 #include "core/enum_type.hpp"
14 #include "core/bitmath_func.hpp"
15 
16 /**
17  * Extracts a given type from a value.
18  * @tparam T The type of data we're looking for.
19  * @tparam S The offset in the data.
20  * @tparam N The amount of bits to read.
21  * @tparam U The type of data passed to us.
22  * @param v The data to extract the value from.
23  */
Extract(U v)24 template<typename T, uint S, uint N, typename U> static inline T Extract(U v)
25 {
26 	/* Check if there are enough bits in v */
27 	static_assert(N == EnumPropsT<T>::num_bits);
28 	static_assert(S + N <= sizeof(U) * 8);
29 	static_assert(EnumPropsT<T>::end <= (1 << N));
30 	U masked = GB(v, S, N);
31 	return IsInsideMM(masked, EnumPropsT<T>::begin, EnumPropsT<T>::end) ? (T)masked : EnumPropsT<T>::invalid;
32 }
33 
34 #endif /* CMD_HELPER_H */
35