Skip to content

Commit

Permalink
Codechange: Give StrongType an implicit/explict constructor template …
Browse files Browse the repository at this point in the history
…parameter.

This allows a StrongType to be even stronger as implicit conversions are prevented.
  • Loading branch information
PeterN committed Jan 28, 2024
1 parent 2af5822 commit 3b92290
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/core/strong_typedef_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,18 @@ namespace StrongType {
*
* @tparam TBaseType Type of the derived class (i.e. the concrete usage of this class).
* @tparam TTag An unique struct to keep types of the same TBaseType distinct.
* @tparam TExplicitConstructor Enable explicit constructor instead of implicit constructor.
* @tparam TProperties A list of mixins to add to the class.
*/
template <typename TBaseType, typename TTag, typename... TProperties>
struct EMPTY_BASES Typedef : public StrongTypedefBase, public TProperties::template mixin<Typedef<TBaseType, TTag, TProperties...>, TBaseType>... {
template <typename TBaseType, typename TTag, bool TExplicitConstructor, typename... TProperties>
struct EMPTY_BASES Typedef : public StrongTypedefBase, public TProperties::template mixin<Typedef<TBaseType, TTag, TExplicitConstructor, TProperties...>, TBaseType>... {
using BaseType = TBaseType;

constexpr Typedef() = default;
constexpr Typedef(const Typedef &) = default;
constexpr Typedef(Typedef &&) = default;

constexpr Typedef(const TBaseType &value) : value(value) {}
constexpr explicit(TExplicitConstructor) Typedef(const TBaseType &value) : value(value) {}

constexpr Typedef &operator =(const Typedef &rhs) { this->value = rhs.value; return *this; }
constexpr Typedef &operator =(Typedef &&rhs) { this->value = std::move(rhs.value); return *this; }
Expand Down
2 changes: 1 addition & 1 deletion src/tile_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ enum TropicZone {
*
* It is compatible with int32 / int64 for easy math throughout the code.
*/
using TileIndex = StrongType::Typedef<uint32_t, struct TileIndexTag, StrongType::CompareSelf, StrongType::CompareBase, StrongType::Integer, StrongType::Compatible<int32_t>, StrongType::Compatible<int64_t>>;
using TileIndex = StrongType::Typedef<uint32_t, struct TileIndexTag, false, StrongType::CompareSelf, StrongType::CompareBase, StrongType::Integer, StrongType::Compatible<int32_t>, StrongType::Compatible<int64_t>>;

/* Make sure the size is as expected. */
static_assert(sizeof(TileIndex) == 4);
Expand Down
4 changes: 2 additions & 2 deletions src/timer/timer_game_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class TimerGame {
public:
/** The type to store our dates in. */
template <class ST> struct DateTag;
using Date = StrongType::Typedef<int32_t, DateTag<T>, StrongType::CompareSelf, StrongType::CompareBase, StrongType::Integer>;
using Date = StrongType::Typedef<int32_t, DateTag<T>, false, StrongType::CompareSelf, StrongType::CompareBase, StrongType::Integer>;

/** The fraction of a date we're in, i.e. the number of ticks since the last date changeover. */
using DateFract = uint16_t;

/** Type for the year, note: 0 based, i.e. starts at the year 0. */
template <class ST> struct YearTag;
using Year = StrongType::Typedef<int32_t, struct YearTag<T>, StrongType::CompareSelf, StrongType::CompareBase, StrongType::Integer>;
using Year = StrongType::Typedef<int32_t, struct YearTag<T>, false, StrongType::CompareSelf, StrongType::CompareBase, StrongType::Integer>;
/** Type for the month, note: 0 based, i.e. 0 = January, 11 = December. */
using Month = uint8_t;
/** Type for the day of the month, note: 1 based, first day of a month is 1. */
Expand Down

0 comments on commit 3b92290

Please sign in to comment.