summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cmd_helper.h19
-rw-r--r--src/core/enum_type.hpp4
-rw-r--r--src/direction_type.h5
-rw-r--r--src/road_type.h2
-rw-r--r--src/track_type.h4
5 files changed, 14 insertions, 20 deletions
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<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
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 <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
};
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<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 */
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<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 */
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<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;
/**