Program Listing for File LiteralID.hpp

Return to documentation for file (src/rdf4cpp/storage/identifier/LiteralID.hpp)

#ifndef RDF4CPP_LITERALID_HPP
#define RDF4CPP_LITERALID_HPP

#include <cstddef>
#include <cassert>
#include <compare>
#include <cstdint>
#include <ostream>
#include <rdf4cpp/Assert.hpp>

namespace rdf4cpp::storage::identifier {
struct __attribute__((__packed__)) LiteralID {
    static constexpr size_t width = 42;
    using underlying_type = uint64_t;

private:
    underlying_type underlying: width;

public:
    constexpr LiteralID() noexcept = default;

    explicit constexpr LiteralID(underlying_type const underlying) noexcept : underlying{underlying} {
        RDF4CPP_ASSERT(underlying < (1UL << 42));
    }

    constexpr LiteralID &operator++() noexcept {
        ++underlying;
        return *this;
    }

    [[nodiscard]] constexpr underlying_type to_underlying() const noexcept {
        return underlying;
    }

    explicit operator underlying_type() const noexcept {
        return underlying;
    }

    constexpr LiteralID operator++(int) noexcept {
        LiteralID new_literal_id{*this};
        ++underlying;
        return new_literal_id;
    }

    constexpr LiteralID &operator--() noexcept {
        --underlying;
        return *this;
    }

    constexpr LiteralID operator--(int) noexcept {
        LiteralID new_literal_id{*this};
        --underlying;
        return new_literal_id;
    }

    constexpr std::strong_ordering operator<=>(LiteralID const &) const noexcept = default;
};

inline std::ostream &operator<<(std::ostream &os, LiteralID id) {
    os << "{ .underlying = " << id.to_underlying() << " }";
    return os;
}

}  // namespace rdf4cpp::storage::identifier
#endif  //RDF4CPP_LITERALID_HPP