(svn r19605) -Codechange: Merge ExtractBits into EnumPropsT.

This commit is contained in:
frosch 2010-04-11 15:44:16 +00:00
parent 03c609f019
commit 0d4e6c46ff
5 changed files with 14 additions and 20 deletions

View File

@ -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

View File

@ -58,14 +58,16 @@ template <typename Tenum_t> 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 <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
};

View File

@ -41,7 +41,7 @@ enum Direction {
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 @@ enum DiagDirection {
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 @@ enum Axis {
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 */

View File

@ -27,6 +27,7 @@ enum RoadType {
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 @@ enum RoadBits {
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 */

View File

@ -33,7 +33,7 @@ enum Track {
/** 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 @@ enum Trackdir {
};
/** 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;
/**