Program Listing for File IRI.hpp

Return to documentation for file (src/rdf4cpp/IRI.hpp)

#ifndef RDF4CPP_IRI_HPP
#define RDF4CPP_IRI_HPP

#include <ostream>
#include <rdf4cpp/Node.hpp>
#include <rdf4cpp/datatypes/registry/DatatypeID.hpp>

namespace rdf4cpp {

struct IRI : Node {
private:
    IRI(datatypes::registry::DatatypeIDView id, storage::DynNodeStoragePtr node_storage) noexcept;

public:
    operator datatypes::registry::DatatypeIDView() const noexcept;

    explicit IRI(storage::identifier::NodeBackendHandle handle) noexcept;

    IRI() noexcept;

    explicit IRI(std::string_view iri, storage::DynNodeStoragePtr node_storage = storage::default_node_storage);

    [[nodiscard]] static IRI make_null() noexcept;

    [[nodiscard]] static IRI make(std::string_view iri, storage::DynNodeStoragePtr node_storage = storage::default_node_storage);

    [[nodiscard]] static IRI make_unchecked(std::string_view iri, storage::DynNodeStoragePtr node_storage = storage::default_node_storage);

    [[nodiscard]] static IRI make_uuid(storage::DynNodeStoragePtr node_storage = storage::default_node_storage);

    IRI to_node_storage(storage::DynNodeStoragePtr node_storage) const;
    [[nodiscard]] IRI try_get_in_node_storage(storage::DynNodeStoragePtr node_storage) const noexcept;

    [[nodiscard]] static IRI find(std::string_view iri, storage::DynNodeStoragePtr node_storage = storage::default_node_storage) noexcept;

    static void validate(std::string_view iri);

private:
    [[nodiscard]] static IRI find(datatypes::registry::DatatypeIDView id, storage::DynNodeStoragePtr node_storage) noexcept;

public:
    [[nodiscard]] std::string_view identifier() const noexcept;

    [[nodiscard]] FetchOrSerializeResult fetch_or_serialize_identifier(std::string_view &out, writer::BufWriterParts writer) const noexcept;

    bool serialize(writer::BufWriterParts writer) const noexcept;

    [[nodiscard]] explicit operator std::string() const noexcept;
    friend std::ostream &operator<<(std::ostream &os, const IRI &iri);

    bool is_literal() const noexcept = delete;
    bool is_variable() const noexcept = delete;
    bool is_blank_node() const noexcept = delete;
    bool is_iri() const noexcept = delete;

    friend struct Node;
    friend struct Literal;

    [[nodiscard]] static IRI default_graph(storage::DynNodeStoragePtr node_storage = storage::default_node_storage);

    [[nodiscard]] TriBool is_default_graph() const noexcept;

    [[nodiscard]] static IRI rdf_type(storage::DynNodeStoragePtr node_storage = storage::default_node_storage);

    [[nodiscard]] TriBool is_rdf_type() const noexcept;

    template<datatypes::LiteralDatatype T>
    [[nodiscard]] static IRI datatype(storage::DynNodeStoragePtr node_storage = storage::default_node_storage) {
        return IRI{T::datatype_id, node_storage};
    }

    template<datatypes::LiteralDatatype T>
    [[nodiscard]] TriBool is_datatype() const noexcept {
        if (null()) {
            return TriBool::Err;
        }

        if constexpr (datatypes::HasFixedId<T>) {
            auto const type = storage::identifier::iri_node_id_to_literal_type(handle_.id());
            return type == T::fixed_id;
        }

        return identifier() == T::identifier;
    }
};

inline namespace shorthands {

IRI operator""_iri(char const *str, size_t len);

}  // namespace shorthands
}  // namespace rdf4cpp

template<>
struct std::hash<rdf4cpp::IRI> {
    inline size_t operator()(rdf4cpp::IRI const &v) const noexcept {
        return std::hash<rdf4cpp::Node>()(v);
    }
};

template<>
struct std::formatter<rdf4cpp::IRI> : std::formatter<rdf4cpp::Node> {
    auto format(rdf4cpp::IRI n, format_context &ctx) const -> decltype(ctx.out());
};

#endif  //RDF4CPP_IRI_HPP