Skip to content

Commit

Permalink
Backport parser module concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
gpicciuca committed Feb 12, 2025
1 parent b8dfeaa commit 64829a0
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/parser/GeoPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class GeoPoint {
public:
using T = uint64_t;

template <typename H>
friend H AbslHashValue(H h, const std::same_as<GeoPoint> auto& g) {
QL_TEMPLATE_NO_DEFAULT(typename H, typename G)
(requires std::same_as<G, GeoPoint>)friend H AbslHashValue(H h, const G& g) {
return H::combine(std::move(h), g.lat_, g.lng_);
}

Expand Down
5 changes: 3 additions & 2 deletions src/parser/Iri.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <string_view>

#include "backports/concepts.h"
#include "parser/NormalizedString.h"

namespace ad_utility::triple_component {
Expand All @@ -30,8 +31,8 @@ class Iri {
public:
// A default constructed IRI is empty.
Iri() = default;
template <typename H>
friend H AbslHashValue(H h, const std::same_as<Iri> auto& iri) {
QL_TEMPLATE_NO_DEFAULT(typename H, typename I)
(requires std::same_as<I, Iri>)friend H AbslHashValue(H h, const I& iri) {
return H::combine(std::move(h), iri.iri_);
}
bool operator==(const Iri&) const = default;
Expand Down
6 changes: 4 additions & 2 deletions src/parser/Literal.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <optional>
#include <variant>

#include "backports/concepts.h"
#include "parser/Iri.h"
#include "parser/NormalizedString.h"

Expand Down Expand Up @@ -41,8 +42,9 @@ class Literal {
}

public:
template <typename H>
friend H AbslHashValue(H h, const std::same_as<Literal> auto& literal) {
QL_TEMPLATE_NO_DEFAULT(typename H, typename L)
(requires std::same_as<L, Literal>)friend H
AbslHashValue(H h, const L& literal) {
return H::combine(std::move(h), literal.content_);
}
bool operator==(const Literal&) const = default;
Expand Down
6 changes: 3 additions & 3 deletions src/parser/LiteralOrIri.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class alignas(16) LiteralOrIri {
return LiteralOrIri{Iri::fromStringRepresentation(std::move(internal))};
}
}
template <typename H>
friend H AbslHashValue(H h,
const std::same_as<LiteralOrIri> auto& literalOrIri) {
QL_TEMPLATE_NO_DEFAULT(typename H, typename L)
(requires std::same_as<L, LiteralOrIri>)friend H
AbslHashValue(H h, const L& literalOrIri) {
return H::combine(std::move(h), literalOrIri.data_);
}
bool operator==(const LiteralOrIri&) const = default;
Expand Down
5 changes: 3 additions & 2 deletions src/parser/RdfParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,9 @@ class NQuadParser : public TurtleParser<Tokenizer_T> {
* Parses turtle from std::string. Used to perform unit tests for
* the different parser rules
*/
template <std::derived_from<RdfParserBase> Parser>
class RdfStringParser : public Parser {
CPP_template(typename Parser)(
requires std::derived_from<Parser, RdfParserBase>) class RdfStringParser
: public Parser {
public:
using Parser::getLine;
using Parser::prefixMap_;
Expand Down
1 change: 1 addition & 0 deletions src/parser/TokenizerCtre.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ class TokenizerCtre : public SkipWhitespaceAndCommentsMixin<TokenizerCtre> {
* <true, matchContent> on success and <false, emptyStringView> on failure
*/
struct Matcher {
// TODO<joka921>: Template-value feature not available in C++17
template <auto& regex>
static std::pair<bool, std::string_view> process(
std::string_view data) noexcept {
Expand Down
38 changes: 26 additions & 12 deletions src/parser/TripleComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
#include "util/Exception.h"
#include "util/Forward.h"

namespace ad_utility::detail {

template <typename T, typename U>
CPP_requires(MoveAssignableWithRequires, requires(T t, U&& u)(t = u));

template <typename T, typename U>
CPP_concept MoveAssignableWith =
CPP_requires_ref(MoveAssignableWithRequires, T, U);

} // namespace ad_utility::detail

/// A wrapper around a `std::variant` that can hold the different types that the
/// subject, predicate, or object of a triple can have in the Turtle Parser.
/// Those currently are `double` (xsd:double and xsd:decimal), `int64_t`
Expand Down Expand Up @@ -56,10 +67,11 @@ class TripleComponent {
TripleComponent() = default;
/// Construct from anything that is able to construct the underlying
/// `Variant`.
template <typename FirstArg, typename... Args>
requires(!std::same_as<std::remove_cvref_t<FirstArg>, TripleComponent> &&
std::is_constructible_v<Variant, FirstArg &&, Args && ...>)
TripleComponent(FirstArg&& firstArg, Args&&... args)
QL_TEMPLATE_NO_DEFAULT(typename FirstArg, typename... Args)
(requires CPP_NOT(
std::same_as<std::remove_cvref_t<FirstArg>, TripleComponent>) &&
std::is_constructible_v<Variant, FirstArg&&, Args&&...>)
TripleComponent(FirstArg&& firstArg, Args&&... args)
: _variant(AD_FWD(firstArg), AD_FWD(args)...) {
if (isString()) {
// Storing variables and literals as strings is deprecated. The following
Expand All @@ -83,9 +95,10 @@ class TripleComponent {

/// Assignment for types that can be directly assigned to the underlying
/// variant.
template <typename T>
requires requires(Variant v, T&& t) { _variant = t; }
TripleComponent& operator=(T&& value) {
CPP_template(typename T)(
requires ad_utility::detail::MoveAssignableWith<Variant, T>)
TripleComponent&
operator=(T&& value) {
_variant = AD_FWD(value);
checkThatStringIsValid();
return *this;
Expand All @@ -103,9 +116,9 @@ class TripleComponent {
TripleComponent& operator=(TripleComponent&&) = default;

/// Make a `TripleComponent` directly comparable to the underlying types.
template <typename T>
requires requires(T&& t) { _variant == t; }
bool operator==(const T& other) const {
CPP_template(typename T)(
requires ql::concepts::equality_comparable_with<Variant, T>) bool
operator==(const T& other) const {
return _variant == other;
}

Expand All @@ -117,8 +130,9 @@ class TripleComponent {
/// overload would also be eligible for the contained types that are
/// implicitly convertible to `TripleComponent` which would lead to strange
/// bugs.
template <typename H>
friend H AbslHashValue(H h, const std::same_as<TripleComponent> auto& tc) {
CPP_template(typename H,
typename TC)(requires std::same_as<TC, TripleComponent>) friend H
AbslHashValue(H h, const TC& tc) {
return H::combine(std::move(h), tc._variant);
}

Expand Down

0 comments on commit 64829a0

Please sign in to comment.