From 0d4e6c46ff4af97205823014e0b4300c7a2c3044 Mon Sep 17 00:00:00 2001 From: frosch Date: Sun, 11 Apr 2010 15:44:16 +0000 Subject: (svn r19605) -Codechange: Merge ExtractBits into EnumPropsT. --- src/cmd_helper.h | 19 ++++--------------- src/core/enum_type.hpp | 4 +++- src/direction_type.h | 5 +++-- src/road_type.h | 2 ++ src/track_type.h | 4 ++-- 5 files changed, 14 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/cmd_helper.h b/src/cmd_helper.h index 8c9c789ca..a034f1217 100644 --- 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 static inline void ExtractValid(); -template<> inline void ExtractValid<1>() {} - - -template struct ExtractBits; -template<> struct ExtractBits { static const uint Count = 1; }; -template<> struct ExtractBits { static const uint Count = 2; }; -template<> struct ExtractBits { static const uint Count = 4; }; - +#include "core/enum_type.hpp" template static inline T Extract(U v) { /* Check if there are enough bits in v */ - ExtractValid::Count <= sizeof(U) * 8>(); - return (T)GB(v, N, ExtractBits::Count); + assert_tcompile(N + EnumPropsT::num_bits <= sizeof(U) * 8); + assert_tcompile(EnumPropsT::end <= (1 << EnumPropsT::num_bits)); + return (T)GB(v, N, EnumPropsT::num_bits); } #endif diff --git a/src/core/enum_type.hpp b/src/core/enum_type.hpp index f6c111aae..ad157f30b 100644 --- a/src/core/enum_type.hpp +++ b/src/core/enum_type.hpp @@ -58,14 +58,16 @@ template struct EnumPropsT; * @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 +template 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 }; diff --git a/src/direction_type.h b/src/direction_type.h index 1a6ab996c..4cc6d1b24 100644 --- a/src/direction_type.h +++ b/src/direction_type.h @@ -41,7 +41,7 @@ enum Direction { DECLARE_POSTFIX_INCREMENT(Direction) /** Define basic enum properties */ -template <> struct EnumPropsT : MakeEnumPropsT {}; +template <> struct EnumPropsT : MakeEnumPropsT {}; typedef TinyEnumT DirectionByte; // typedefing-enumification of Direction @@ -91,7 +91,7 @@ enum DiagDirection { DECLARE_POSTFIX_INCREMENT(DiagDirection) /** Define basic enum properties */ -template <> struct EnumPropsT : MakeEnumPropsT {}; +template <> struct EnumPropsT : MakeEnumPropsT {}; typedef TinyEnumT DiagDirectionByte; // typedefing-enumification of DiagDirection @@ -130,5 +130,6 @@ enum Axis { AXIS_END, ///< Used for iterations INVALID_AXIS = 0xFF, ///< Flag for an invalid Axis }; +template <> struct EnumPropsT : MakeEnumPropsT {}; #endif /* DIRECTION_TYPE_H */ diff --git a/src/road_type.h b/src/road_type.h index 86ca4de78..be16c2315 100644 --- a/src/road_type.h +++ b/src/road_type.h @@ -27,6 +27,7 @@ enum RoadType { INVALID_ROADTYPE = 0xFF ///< flag for invalid roadtype }; DECLARE_POSTFIX_INCREMENT(RoadType) +template <> struct EnumPropsT : MakeEnumPropsT {}; /** * The different roadtypes we support, but then a bitmask of them @@ -67,5 +68,6 @@ enum RoadBits { ROAD_ALL = ROAD_X | ROAD_Y ///< Full 4-way crossing }; DECLARE_ENUM_AS_BIT_SET(RoadBits) +template <> struct EnumPropsT : MakeEnumPropsT {}; #endif /* ROAD_TYPE_H */ diff --git a/src/track_type.h b/src/track_type.h index b5c95d575..a17c9773a 100644 --- a/src/track_type.h +++ b/src/track_type.h @@ -33,7 +33,7 @@ enum Track { /** Allow incrementing of Track variables */ DECLARE_POSTFIX_INCREMENT(Track) /** Define basic enum properties */ -template <> struct EnumPropsT : MakeEnumPropsT {}; +template <> struct EnumPropsT : MakeEnumPropsT {}; typedef TinyEnumT TrackByte; @@ -94,7 +94,7 @@ enum Trackdir { }; /** Define basic enum properties */ -template <> struct EnumPropsT : MakeEnumPropsT {}; +template <> struct EnumPropsT : MakeEnumPropsT {}; typedef TinyEnumT TrackdirByte; /** -- cgit v1.2.3-54-g00ecf