Skip to content

Commit cf799b1

Browse files
authored
Optimize: templated literal cast (#418)
1 parent 0f03c1a commit cf799b1

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/rdf4cpp/Literal.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ std::any Literal::value() const noexcept {
890890
});
891891
}
892892

893-
Literal Literal::cast(IRI const &target, storage::DynNodeStoragePtr node_storage) const {
893+
Literal Literal::cast_impl(datatypes::registry::DatatypeIDView target_dtid, storage::DynNodeStoragePtr node_storage) const {
894894
using namespace datatypes::registry;
895895
using namespace datatypes::xsd;
896896

@@ -901,7 +901,6 @@ Literal Literal::cast(IRI const &target, storage::DynNodeStoragePtr node_storage
901901
node_storage = select_node_storage(node_storage);
902902

903903
auto const this_dtid = this->datatype_id();
904-
DatatypeIDView const target_dtid{target};
905904

906905
if (this_dtid == target_dtid) {
907906
return this->to_node_storage(node_storage);
@@ -911,7 +910,7 @@ Literal Literal::cast(IRI const &target, storage::DynNodeStoragePtr node_storage
911910
// string -> any
912911
try {
913912
auto const lex = this->lexical_form();
914-
return Literal::make_typed(lex, target, node_storage);
913+
return Literal::make_typed(lex, IRI{target_dtid, node_storage}, node_storage);
915914
} catch (...) {
916915
return Literal{};
917916
}
@@ -984,6 +983,10 @@ Literal Literal::cast(IRI const &target, storage::DynNodeStoragePtr node_storage
984983
return Literal{};
985984
}
986985

986+
Literal Literal::cast(IRI const &target, storage::DynNodeStoragePtr node_storage) const {
987+
return this->cast_impl(static_cast<datatypes::registry::DatatypeIDView>(target), node_storage);
988+
}
989+
987990
template<typename OpSelect>
988991
requires std::is_nothrow_invocable_r_v<datatypes::registry::DatatypeRegistry::binop_fptr_t, OpSelect, datatypes::registry::DatatypeRegistry::NumericOpsImpl const &>
989992
Literal Literal::numeric_binop_impl(OpSelect op_select, Literal const &other, storage::DynNodeStoragePtr node_storage) const {

src/rdf4cpp/Literal.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ struct Literal : Node {
296296
template<bool simplified, typename C>
297297
auto serialize_lexical_form_impl(C &&consume) const noexcept;
298298

299+
[[nodiscard]] Literal cast_impl(datatypes::registry::DatatypeIDView target_dtid, storage::DynNodeStoragePtr node_storage) const;
300+
299301
explicit Literal(storage::identifier::NodeBackendHandle handle) noexcept;
300302

301303
public:
@@ -715,8 +717,7 @@ struct Literal : Node {
715717
*/
716718
template<datatypes::LiteralDatatype T>
717719
[[nodiscard]] Literal cast(storage::DynNodeStoragePtr node_storage = keep_node_storage) const {
718-
auto const ns = select_node_storage(node_storage);
719-
return this->cast(IRI{T::datatype_id, ns}, ns);
720+
return this->cast_impl(T::datatype_id, node_storage);
720721
}
721722

722723
/**

0 commit comments

Comments
 (0)