Mercurial > hg > openttd
changeset 14993:f3e5df0b5727 draft
(svn r19605) -Codechange: Merge ExtractBits into EnumPropsT.
author | frosch <frosch@openttd.org> |
---|---|
date | Sun, 11 Apr 2010 15:44:16 +0000 |
parents | 67870be3d58f |
children | 7a1b8f9042a1 |
files | src/cmd_helper.h src/core/enum_type.hpp src/direction_type.h src/road_type.h src/track_type.h |
diffstat | 5 files changed, 14 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/src/cmd_helper.h +++ b/src/cmd_helper.h @@ -12,25 +12,14 @@ #ifndef CMD_HELPER_H #define CMD_HELPER_H -#include "direction_type.h" -#include "road_type.h" - - -template<uint N> static inline void ExtractValid(); -template<> inline void ExtractValid<1>() {} - - -template<typename T> struct ExtractBits; -template<> struct ExtractBits<Axis> { static const uint Count = 1; }; -template<> struct ExtractBits<DiagDirection> { static const uint Count = 2; }; -template<> struct ExtractBits<RoadBits> { static const uint Count = 4; }; - +#include "core/enum_type.hpp" template<typename T, uint N, typename U> static inline T Extract(U v) { /* Check if there are enough bits in v */ - ExtractValid<N + ExtractBits<T>::Count <= sizeof(U) * 8>(); - return (T)GB(v, N, ExtractBits<T>::Count); + assert_tcompile(N + EnumPropsT<T>::num_bits <= sizeof(U) * 8); + assert_tcompile(EnumPropsT<T>::end <= (1 << EnumPropsT<T>::num_bits)); + return (T)GB(v, N, EnumPropsT<T>::num_bits); } #endif
--- a/src/core/enum_type.hpp +++ b/src/core/enum_type.hpp @@ -58,14 +58,16 @@ * @param Tbegin first valid value from the contiguous range (i.e. TRACK_BEGIN) * @param Tend one past the last valid value from the contiguous range (i.e. TRACK_END) * @param Tinvalid value used as invalid value marker (i.e. INVALID_TRACK) + * @param Tnum_bits Number of bits for storing the enum in command parameters */ -template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid> +template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid, uint Tnum_bits = 8 * sizeof(Tstorage_t)> struct MakeEnumPropsT { typedef Tenum_t type; ///< enum type (i.e. Trackdir) typedef Tstorage_t storage; ///< storage type (i.e. byte) static const Tenum_t begin = Tbegin; ///< lowest valid value (i.e. TRACKDIR_BEGIN) static const Tenum_t end = Tend; ///< one after the last valid value (i.e. TRACKDIR_END) static const Tenum_t invalid = Tinvalid; ///< what value is used as invalid value (i.e. INVALID_TRACKDIR) + static const uint num_bits = Tnum_bits; ///< Number of bits for storing the enum in command parameters };
--- a/src/direction_type.h +++ b/src/direction_type.h @@ -41,7 +41,7 @@ DECLARE_POSTFIX_INCREMENT(Direction) /** Define basic enum properties */ -template <> struct EnumPropsT<Direction> : MakeEnumPropsT<Direction, byte, DIR_BEGIN, DIR_END, INVALID_DIR> {}; +template <> struct EnumPropsT<Direction> : MakeEnumPropsT<Direction, byte, DIR_BEGIN, DIR_END, INVALID_DIR, 3> {}; typedef TinyEnumT<Direction> DirectionByte; // typedefing-enumification of Direction @@ -91,7 +91,7 @@ DECLARE_POSTFIX_INCREMENT(DiagDirection) /** Define basic enum properties */ -template <> struct EnumPropsT<DiagDirection> : MakeEnumPropsT<DiagDirection, byte, DIAGDIR_BEGIN, DIAGDIR_END, INVALID_DIAGDIR> {}; +template <> struct EnumPropsT<DiagDirection> : MakeEnumPropsT<DiagDirection, byte, DIAGDIR_BEGIN, DIAGDIR_END, INVALID_DIAGDIR, 2> {}; typedef TinyEnumT<DiagDirection> DiagDirectionByte; // typedefing-enumification of DiagDirection @@ -130,5 +130,6 @@ AXIS_END, ///< Used for iterations INVALID_AXIS = 0xFF, ///< Flag for an invalid Axis }; +template <> struct EnumPropsT<Axis> : MakeEnumPropsT<Axis, byte, AXIS_X, AXIS_END, INVALID_AXIS, 1> {}; #endif /* DIRECTION_TYPE_H */
--- a/src/road_type.h +++ b/src/road_type.h @@ -27,6 +27,7 @@ INVALID_ROADTYPE = 0xFF ///< flag for invalid roadtype }; DECLARE_POSTFIX_INCREMENT(RoadType) +template <> struct EnumPropsT<RoadType> : MakeEnumPropsT<RoadType, byte, ROADTYPE_BEGIN, ROADTYPE_END, INVALID_ROADTYPE> {}; /** * The different roadtypes we support, but then a bitmask of them @@ -67,5 +68,6 @@ ROAD_ALL = ROAD_X | ROAD_Y ///< Full 4-way crossing }; DECLARE_ENUM_AS_BIT_SET(RoadBits) +template <> struct EnumPropsT<RoadBits> : MakeEnumPropsT<RoadBits, byte, ROAD_NONE, ROAD_ALL, ROAD_NONE, 4> {}; #endif /* ROAD_TYPE_H */
--- a/src/track_type.h +++ b/src/track_type.h @@ -33,7 +33,7 @@ /** Allow incrementing of Track variables */ DECLARE_POSTFIX_INCREMENT(Track) /** Define basic enum properties */ -template <> struct EnumPropsT<Track> : MakeEnumPropsT<Track, byte, TRACK_BEGIN, TRACK_END, INVALID_TRACK> {}; +template <> struct EnumPropsT<Track> : MakeEnumPropsT<Track, byte, TRACK_BEGIN, TRACK_END, INVALID_TRACK, 3> {}; typedef TinyEnumT<Track> TrackByte; @@ -94,7 +94,7 @@ }; /** Define basic enum properties */ -template <> struct EnumPropsT<Trackdir> : MakeEnumPropsT<Trackdir, byte, TRACKDIR_BEGIN, TRACKDIR_END, INVALID_TRACKDIR> {}; +template <> struct EnumPropsT<Trackdir> : MakeEnumPropsT<Trackdir, byte, TRACKDIR_BEGIN, TRACKDIR_END, INVALID_TRACKDIR, 4> {}; typedef TinyEnumT<Trackdir> TrackdirByte; /**