Skip to content

Commit 7afcb81

Browse files
committed
WIP
1 parent ccd7a9b commit 7afcb81

19 files changed

+109
-60
lines changed

src/rdf4cpp/datatypes/LiteralDatatype.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ concept PromotableLiteralDatatype = LiteralDatatype<LiteralDatatypeImpl> && requ
156156
typename LiteralDatatypeImpl::template promoted_cpp_type<0>;
157157
{ LiteralDatatypeImpl::promotion_rank } -> std::convertible_to<size_t>;
158158
{ LiteralDatatypeImpl::max_promotion_specialization_ix } -> std::convertible_to<size_t>;
159-
{ LiteralDatatypeImpl::template promote<0>(value) } -> std::convertible_to<typename LiteralDatatypeImpl::template promoted_cpp_type<0>>;
159+
{ LiteralDatatypeImpl::template promote<0>(value) } -> std::convertible_to<nonstd::expected<typename LiteralDatatypeImpl::template promoted_cpp_type<0>, DynamicError>>;
160160
{ LiteralDatatypeImpl::template demote<0>(promoted_value) } -> std::convertible_to<nonstd::expected<typename LiteralDatatypeImpl::cpp_type, DynamicError>>;
161161
};
162162

src/rdf4cpp/datatypes/registry/DatatypeConversion.hpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ consteval ConversionLayer auto make_conversion_layer_impl() {
105105
using source_type = Type;
106106
using target_type = typename next::template converted<ix>;
107107

108-
inline static typename target_type::cpp_type convert(typename source_type::cpp_type const &value) noexcept {
108+
inline static nonstd::expected<typename target_type::cpp_type, DynamicError> convert(typename source_type::cpp_type const &value) noexcept {
109109
return next::template convert<ix>(value);
110110
}
111111

@@ -128,8 +128,13 @@ consteval ConversionLayer auto make_conversion_layer_impl() {
128128
using source_type = typename prev_promotion_t::source_type;
129129
using target_type = typename next::template converted<ix>;
130130

131-
inline static typename target_type::cpp_type convert(typename source_type::cpp_type const &value) noexcept {
132-
return next::template convert<ix>(prev_promotion_t::convert(value));
131+
inline static nonstd::expected<typename target_type::cpp_type, DynamicError> convert(typename source_type::cpp_type const &value) noexcept {
132+
auto const promoted = prev_promotion_t::convert(value);
133+
if (!promoted.has_value()) {
134+
return nonstd::make_unexpected(promoted.error());
135+
}
136+
137+
return next::template convert<ix>(*promoted);
133138
}
134139

135140
inline static nonstd::expected<typename source_type::cpp_type, DynamicError> inverse_convert(typename target_type::cpp_type const &value) noexcept {
@@ -174,7 +179,7 @@ struct PromoteConversion<LiteralDatatypeImpl> {
174179
using converted_cpp_type = typename LiteralDatatypeImpl::template promoted_cpp_type<ix>;
175180

176181
template<size_t ix>
177-
inline static converted_cpp_type<ix> convert(cpp_type const &value) noexcept {
182+
inline static nonstd::expected<converted_cpp_type<ix>, DynamicError> convert(cpp_type const &value) noexcept {
178183
return LiteralDatatypeImpl::template promote<ix>(value);
179184
}
180185

@@ -340,7 +345,7 @@ consteval ConversionTable auto make_conversion_table() {
340345
using source_type = Type;
341346
using target_type = Type;
342347

343-
inline static typename target_type::cpp_type convert(typename source_type::cpp_type const &value) noexcept {
348+
inline static nonstd::expected<typename target_type::cpp_type, DynamicError> convert(typename source_type::cpp_type const &value) noexcept {
344349
return value;
345350
}
346351

@@ -367,8 +372,13 @@ consteval ConversionTable auto make_conversion_table() {
367372
using source_type = typename ToSuper::source_type;
368373
using target_type = typename PromoteSuper::target_type;
369374

370-
inline static typename target_type::cpp_type convert(typename source_type::cpp_type const &value) noexcept {
371-
return PromoteSuper::convert(ToSuper::convert(value));
375+
inline static nonstd::expected<typename target_type::cpp_type, DynamicError> convert(typename source_type::cpp_type const &value) noexcept {
376+
auto const super = ToSuper::convert(value);
377+
if (!super.has_value()) {
378+
return nonstd::make_unexpected(super.error());
379+
}
380+
381+
return PromoteSuper::convert(*super);
372382
}
373383

374384
inline static nonstd::expected<typename source_type::cpp_type, DynamicError> inverse_convert(typename target_type::cpp_type const &value) noexcept {

src/rdf4cpp/datatypes/registry/DatatypeConversionTyping.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ template<typename T>
1919
concept ConversionEntry = requires(typename T::source_type::cpp_type value) {
2020
requires LiteralDatatype<typename T::source_type>;
2121
requires LiteralDatatype<typename T::target_type>;
22-
{ T::convert(value) } -> std::same_as<typename T::target_type::cpp_type>;
22+
{ T::convert(value) } -> std::same_as<nonstd::expected<typename T::target_type::cpp_type, DynamicError>>;
2323
};
2424

2525

@@ -72,7 +72,7 @@ concept ConversionTable = conversion_typing_detail::IsConversionTable<T>::value;
7272
* A type erased version of a ConversionEntry.
7373
*/
7474
struct RuntimeConversionEntry {
75-
using convert_fptr_t = std::any (*)(std::any const &) noexcept;
75+
using convert_fptr_t = nonstd::expected<std::any, DynamicError> (*)(std::any const &) noexcept;
7676
using inverted_convert_fptr_t = nonstd::expected<std::any, DynamicError> (*)(std::any const &) noexcept;
7777

7878
DatatypeID target_type_id;
@@ -91,7 +91,7 @@ struct RuntimeConversionEntry {
9191

9292
return RuntimeConversionEntry{
9393
.target_type_id = std::move(target_type_iri),
94-
.convert = [](std::any const &value) noexcept -> std::any {
94+
.convert = [](std::any const &value) noexcept -> nonstd::expected<std::any, DynamicError> {
9595
auto const actual_value = std::any_cast<typename Entry::source_type::cpp_type>(value);
9696
return std::any{Entry::convert(actual_value)};
9797
},

src/rdf4cpp/datatypes/registry/LiteralDatatypeImpl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ struct Promotable {
105105
using promoted_cpp_type = typename DatatypeMapping<promoted<ix>::identifier>::cpp_datatype;
106106

107107
template<size_t ix = 0>
108-
inline static promoted_cpp_type<ix> promote(cpp_type const &value) noexcept {
108+
static nonstd::expected<promoted_cpp_type<ix>, DynamicError> promote(cpp_type const &value) noexcept {
109109
return static_cast<promoted_cpp_type<ix>>(value);
110110
}
111111

112112
template<size_t ix = 0>
113-
inline static nonstd::expected<cpp_type, DynamicError> demote(promoted_cpp_type<ix> const &value) noexcept {
113+
static nonstd::expected<cpp_type, DynamicError> demote(promoted_cpp_type<ix> const &value) noexcept {
114114
if constexpr (std::is_integral_v<cpp_type> && std::is_integral_v<promoted_cpp_type<ix>>) {
115115
if (!std::in_range<cpp_type>(value)) {
116116
return nonstd::make_unexpected(DynamicError::InvalidValueForCast);

src/rdf4cpp/datatypes/registry/util/DateTimeUtils.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ inline nonstd::expected<std::chrono::nanoseconds, DynamicError> timepoint_sub(st
218218
}
219219
}
220220

221-
static inline std::partial_ordering compare_time_points(const rdf4cpp::TimePoint& a, std::optional<rdf4cpp::Timezone> atz,
221+
inline std::partial_ordering compare_time_points(const rdf4cpp::TimePoint& a, std::optional<rdf4cpp::Timezone> atz,
222222
const rdf4cpp::TimePoint& b, std::optional<rdf4cpp::Timezone> btz) noexcept {
223223
auto apply_timezone = [](const rdf4cpp::TimePoint& t, rdf4cpp::Timezone tz) noexcept -> std::optional<rdf4cpp::TimePointSys> {
224224
try {
@@ -253,6 +253,12 @@ static inline std::partial_ordering compare_time_points(const rdf4cpp::TimePoint
253253
auto a_sys = apply_timezone(a, *atz);
254254
return cmp_opt(a_sys, apply_timezone(b, *btz));
255255
}
256+
257+
inline std::partial_ordering compare_time_points(std::pair<rdf4cpp::TimePoint, rdf4cpp::OptionalTimezone> const &lhs,
258+
std::pair<rdf4cpp::TimePoint, rdf4cpp::OptionalTimezone> const &rhs) {
259+
return compare_time_points(lhs.first, lhs.second, rhs.first, rhs.second);
260+
}
261+
256262
template<std::unsigned_integral T>
257263
constexpr T number_of_bits(T x) noexcept {
258264
return x < 2 ? x : 1 + number_of_bits(x >> 1);

src/rdf4cpp/datatypes/xsd/time/Date.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,53 +63,60 @@ capabilities::Inlineable<xsd_date>::cpp_type capabilities::Inlineable<xsd_date>:
6363
return capabilities::Inlineable<xsd_date>::cpp_type{YearMonthDay::time_point<int64_t>{YearMonthDay::time_point<int64_t>::duration{i}}, std::nullopt};
6464
}
6565

66-
rdf4cpp::TimePoint date_to_tp(YearMonthDay const &d) noexcept {
67-
return rdf4cpp::util::construct_timepoint(d, rdf4cpp::util::time_point_replacement_time_of_day);
66+
inline capabilities::Promotable<xsd_date>::promoted_cpp_type<0> date_to_tp(capabilities::Default<xsd_date>::cpp_type const &d) noexcept {
67+
return std::make_pair(rdf4cpp::util::construct_timepoint(d.first, rdf4cpp::util::time_point_replacement_time_of_day), d.second);
6868
}
6969

7070
template<>
7171
template<>
72-
capabilities::Promotable<xsd_date>::promoted_cpp_type<0> capabilities::Promotable<xsd_date>::promote<0>(cpp_type const &value) noexcept {
73-
return std::make_pair(date_to_tp(value.first), value.second);
72+
nonstd::expected<capabilities::Promotable<xsd_date>::promoted_cpp_type<0>, DynamicError>
73+
capabilities::Promotable<xsd_date>::promote<0>(cpp_type const &value) noexcept {
74+
return date_to_tp(value);
7475
}
7576

7677
template<>
7778
template<>
78-
nonstd::expected<capabilities::Promotable<xsd_date>::cpp_type, DynamicError> capabilities::Promotable<xsd_date>::demote<0>(promoted_cpp_type<0> const &value) noexcept {
79+
nonstd::expected<capabilities::Promotable<xsd_date>::cpp_type, DynamicError>
80+
capabilities::Promotable<xsd_date>::demote<0>(promoted_cpp_type<0> const &value) noexcept {
7981
return std::make_pair(YearMonthDay{std::chrono::floor<std::chrono::days>(value.first)}, value.second);
8082
}
8183

8284
template<>
8385
std::partial_ordering capabilities::Comparable<xsd_date>::compare(cpp_type const &lhs, cpp_type const &rhs) noexcept {
84-
return rdf4cpp::datatypes::registry::util::compare_time_points(date_to_tp(lhs.first), lhs.second, date_to_tp(rhs.first), rhs.second);
86+
return rdf4cpp::datatypes::registry::util::compare_time_points(date_to_tp(lhs), date_to_tp(rhs));
8587
}
8688

8789
template<>
8890
nonstd::expected<capabilities::Timepoint<xsd_date>::timepoint_sub_result_cpp_type, DynamicError>
8991
capabilities::Timepoint<xsd_date>::timepoint_sub(cpp_type const &lhs, cpp_type const &rhs) noexcept {
90-
auto const super_lhs = Promotable<xsd_date>::promote(lhs);
91-
auto const super_rhs = Promotable<xsd_date>::promote(rhs);
92-
return util::timepoint_sub(super_lhs, super_rhs);
92+
auto const lhs_tp = date_to_tp(lhs);
93+
auto const rhs_tp = date_to_tp(rhs);
94+
95+
return util::timepoint_sub(lhs_tp, rhs_tp);
9396
}
9497

9598
template<>
9699
nonstd::expected<capabilities::Timepoint<xsd_date>::cpp_type, DynamicError>
97100
capabilities::Timepoint<xsd_date>::timepoint_duration_add(cpp_type const &tp, timepoint_duration_operand_cpp_type const &dur) noexcept {
98101
auto const super_tp = Promotable<xsd_date>::promote(tp);
99-
auto res_tp = util::add_duration_to_date_time(super_tp.first, dur);
102+
assert(super_tp.has_value());
103+
104+
auto res_tp = util::add_duration_to_date_time(super_tp->first, dur);
100105

101106
auto [date, _] = rdf4cpp::util::deconstruct_timepoint(res_tp);
102-
return std::make_pair(date, super_tp.second);
107+
return std::make_pair(date, super_tp->second);
103108
}
104109

105110
template<>
106111
nonstd::expected<capabilities::Timepoint<xsd_date>::cpp_type, DynamicError>
107112
capabilities::Timepoint<xsd_date>::timepoint_duration_sub(cpp_type const &tp, timepoint_duration_operand_cpp_type const &dur) noexcept {
108113
auto const super_tp = Promotable<xsd_date>::promote(tp);
109-
auto res_tp = util::add_duration_to_date_time(super_tp.first, std::make_pair(-dur.first, -dur.second));
114+
assert(super_tp.has_value());
115+
116+
auto res_tp = util::add_duration_to_date_time(super_tp->first, std::make_pair(-dur.first, -dur.second));
110117

111118
auto [date, _] = rdf4cpp::util::deconstruct_timepoint(res_tp);
112-
return std::make_pair(date, super_tp.second);
119+
return std::make_pair(date, super_tp->second);
113120
}
114121

115122
#endif

src/rdf4cpp/datatypes/xsd/time/Date.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ capabilities::Inlineable<xsd_date>::cpp_type capabilities::Inlineable<xsd_date>:
4949

5050
template<>
5151
template<>
52-
capabilities::Promotable<xsd_date>::promoted_cpp_type<0> capabilities::Promotable<xsd_date>::promote<0>(cpp_type const &value) noexcept;
52+
nonstd::expected<capabilities::Promotable<xsd_date>::promoted_cpp_type<0>, DynamicError>
53+
capabilities::Promotable<xsd_date>::promote<0>(cpp_type const &value) noexcept;
5354

5455
template<>
5556
template<>

src/rdf4cpp/datatypes/xsd/time/Day.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ capabilities::Inlineable<xsd_gDay>::cpp_type capabilities::Inlineable<xsd_gDay>:
6363

6464
template<>
6565
template<>
66-
capabilities::Promotable<xsd_gDay>::promoted_cpp_type<0> capabilities::Promotable<xsd_gDay>::promote<0>(cpp_type const &value) noexcept {
67-
return std::make_pair(YearMonthDay{rdf4cpp::util::time_point_replacement_date.year(), rdf4cpp::util::time_point_replacement_date.month(), value.first}, value.second);
66+
nonstd::expected<capabilities::Promotable<xsd_gDay>::promoted_cpp_type<0>, DynamicError>
67+
capabilities::Promotable<xsd_gDay>::promote<0>([[maybe_unused]] cpp_type const &value) noexcept {
68+
return nonstd::make_unexpected(DynamicError::Unsupported);
6869
}
6970

7071
template<>
7172
template<>
72-
nonstd::expected<capabilities::Promotable<xsd_gDay>::cpp_type, DynamicError> capabilities::Promotable<xsd_gDay>::demote<0>(promoted_cpp_type<0> const &value) noexcept {
73+
nonstd::expected<capabilities::Promotable<xsd_gDay>::cpp_type, DynamicError>
74+
capabilities::Promotable<xsd_gDay>::demote<0>(promoted_cpp_type<0> const &value) noexcept {
7375
return std::make_pair(value.first.day(), value.second);
7476
}
7577
#endif

src/rdf4cpp/datatypes/xsd/time/Day.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ capabilities::Inlineable<xsd_gDay>::cpp_type capabilities::Inlineable<xsd_gDay>:
4040

4141
template<>
4242
template<>
43-
capabilities::Promotable<xsd_gDay>::promoted_cpp_type<0> capabilities::Promotable<xsd_gDay>::promote<0>(cpp_type const &value) noexcept;
43+
nonstd::expected<capabilities::Promotable<xsd_gDay>::promoted_cpp_type<0>, DynamicError>
44+
capabilities::Promotable<xsd_gDay>::promote<0>(cpp_type const &value) noexcept;
4445

4546
template<>
4647
template<>

src/rdf4cpp/datatypes/xsd/time/Month.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ capabilities::Inlineable<xsd_gMonth>::cpp_type capabilities::Inlineable<xsd_gMon
6363

6464
template<>
6565
template<>
66-
capabilities::Promotable<xsd_gMonth>::promoted_cpp_type<0> capabilities::Promotable<xsd_gMonth>::promote<0>(cpp_type const &value) noexcept {
67-
return std::make_pair(YearMonthDay{rdf4cpp::util::time_point_replacement_date.year(), value.first, std::chrono::last}, value.second);
66+
nonstd::expected<capabilities::Promotable<xsd_gMonth>::promoted_cpp_type<0>, DynamicError>
67+
capabilities::Promotable<xsd_gMonth>::promote<0>([[maybe_unused]] cpp_type const &value) noexcept {
68+
return nonstd::make_unexpected(DynamicError::Unsupported);
6869
}
6970

7071
template<>
7172
template<>
72-
nonstd::expected<capabilities::Promotable<xsd_gMonth>::cpp_type, DynamicError> capabilities::Promotable<xsd_gMonth>::demote<0>(promoted_cpp_type<0> const &value) noexcept {
73+
nonstd::expected<capabilities::Promotable<xsd_gMonth>::cpp_type, DynamicError>
74+
capabilities::Promotable<xsd_gMonth>::demote<0>(promoted_cpp_type<0> const &value) noexcept {
7375
return std::make_pair(value.first.month(), value.second);
7476
}
7577
#endif

0 commit comments

Comments
 (0)