template struct utils::StrongId

Overview

Template class implementation of a Named Strong Id (a Strong Type with an underlying unsigned integer type (uint32_t)). More…

#include <StrongID.h>

template <typename PhantomType>
struct StrongId: public utils::StrongType {
    // typedefs

    typedef uint32_t ValueType;

    // construction

    StrongId(size_t value);
    StrongId(int value);

    // methods

    StrongId Next() const;
    std::string ToString() const;
    static std::vector<StrongId<PhantomType>> CreateIds(const std::vector<unsigned>& ids);
};

Inherited Members

public:
    // methods

    template <typename RT>
    std::enable_if_t<std::is_arithmetic_v<RT>, RT> As() const;

    bool operator==(const StrongType& other) const;
    bool operator!=(const StrongType& other) const;
    bool operator<(const StrongType& other) const;
    bool operator>(const StrongType& other) const;
    const ValueType& operator*() const;

protected:
    // fields

    ValueType value_;

Detailed Documentation

Template class implementation of a Named Strong Id (a Strong Type with an underlying unsigned integer type (uint32_t)).

https://www.fluentcpp.com/2016/12/08/strong-types-for-strong-interfaces/ Strong Id is an integral type (uint32_t) specific for the given usage (usually as an id of a class). It prevents implicit conversions to and from other types, as well as comparisons to other types.

Parameters:

PhantomType

An unused type specific for each Strong Type to ensure the Strong Type is different from every other type.

Construction

StrongId(size_t value)

Construct from size_t.

StrongId(int value)

Construct from int.

Methods

StrongId Next() const

An id following this one. Incremented by 1.

std::string ToString() const

Convert the id to string.

static std::vector<StrongId<PhantomType>> CreateIds(const std::vector<unsigned>& ids)

Creates vector of specific strong ids values from the vector of unsigned values.