mercredi 3 octobre 2018

Union for storing Timestamp

While trying to wrap my brain around a custom game engine, I stumbled across a piece of code that looks rather odd to me:

union TimeStamp
{
    f32 asFloat;
    u32 asUInt;

    TimeStamp() : asUInt(0U)
    {}

    TimeStamp(f32 floatValue) : asFloat(floatValue)
    {}

    operator u32() const { return this->asUInt; }

    inline const bool operator==(const TimeStamp& other) const { return this->asUInt == other.asUInt; }
    inline const bool operator!=(const TimeStamp& other) const { return this->asUInt != other.asUInt; }

    inline const bool operator<(const TimeStamp& other) const { return this->asFloat < other.asFloat; }
    inline const bool operator>(const TimeStamp& other) const { return this->asFloat > other.asFloat; }

};

My c++ experience(which is quite limited) tells me that the only meaningful reason to have this union is to make equality operators to treat compared timestamps(otherwise float) as integers? If yes, then why would someone want that?

I also don't understand what is the point of an integer type cast overload here, since we can't event init Timestamp with int?

Note: f32 and u32 are defined as float_t and uint32_t

Just in case, here is a link containing the definition: https://github.com/tobias-stein/EntityComponentSystem/blob/master/EntityComponentSystem/include/ECS/Platform.h

Aucun commentaire:

Enregistrer un commentaire