3#include <Cyqlops/Crypto/Hash.h>
5#include "QuantityValue.h"
9 enum class RangeBoundType
17 template<
typename _T_>
22 using ReturnType = cres_qresult<bool>;
23 static ReturnType isEqual(
const Value& _lhs,
const Value& _rhs)
25 return _lhs.
compare(_rhs, knowCore::ComparisonOperator::Equal);
27 static ReturnType isStrictlyInferior(
const Value& _lhs,
const Value& _rhs)
29 return _lhs.
compare(_rhs, knowCore::ComparisonOperator::Inferior);
32 template<
typename _T_>
35 using ReturnType = cres_qresult<bool>;
49 using ReturnType = bool;
64 template<
typename _T_>
68 Range() : m_lowerType(RangeBoundType::Unset), m_upperType(RangeBoundType::Unset) {}
69 Range(RangeBoundType _lower_type,
const _T_& _lower,
const _T_& _upper,
70 RangeBoundType _upper_type)
71 : m_lower(_lower), m_lowerType(_lower_type), m_upper(_upper), m_upperType(_upper_type)
76 bool operator==(
const Range& _rhs)
const;
77 _T_ lower()
const {
return m_lower; }
78 RangeBoundType lowerBoundType()
const {
return m_lowerType; }
79 _T_ upper()
const {
return m_upper; }
80 RangeBoundType upperBoundType()
const {
return m_upperType; }
86 requires(std::is_same_v<typename details::RangeComparator<_T_>::ReturnType,
bool>);
92 std::is_same_v<typename details::RangeComparator<_T_>::ReturnType, cres_qresult<bool>>);
97 cres_qresult<QByteArray>
md5()
const;
102 = defaultSerialisationContext())
const;
106 static cres_qresult<Range>
fromJsonValue(
const QJsonValue& _value,
108 = defaultDeserialisationContext());
113 = defaultSerialisationContext())
const;
117 static cres_qresult<Range>
fromCborValue(
const QCborValue& _value,
119 = defaultDeserialisationContext());
124 = defaultSerialisationContext())
const;
130 = defaultDeserialisationContext());
137 RangeBoundType m_lowerType;
139 RangeBoundType m_upperType;
142 template<
typename _T_>
145 return m_lowerType == _rhs.m_lowerType and m_upperType == _rhs.m_upperType
146 and m_lower == _rhs.m_lower and m_upper == _rhs.m_upper;
149#define __KNOWCORE_DATATYPE_KEY QString("datatype")
150#define __KNOWCORE_VALUE_KEY QString("value")
154 template<
typename _T_>
161 static auto md5(
const Value& _value) {
return _value.md5(); }
162 static cres_qresult<QJsonValue> toJsonValue(
const Value& _value,
166 obj[__KNOWCORE_DATATYPE_KEY] = QString(_value.
datatype());
167 cres_try(QJsonValue value, _value.toJsonValue(_contexts));
168 obj[__KNOWCORE_VALUE_KEY] = value;
169 return cres_success(obj);
171 static cres_qresult<Value> fromJsonValue(
const QJsonValue& _value,
174 QJsonObject obj = _value.toObject();
175 cres_try(
Value value, Value::fromJsonValue(obj[__KNOWCORE_DATATYPE_KEY].toString(),
176 obj[__KNOWCORE_VALUE_KEY], _contexts));
177 return cres_success(value);
179 static cres_qresult<QCborValue> toCborValue(
const Value& _value,
183 obj[__KNOWCORE_DATATYPE_KEY] = QString(_value.
datatype());
184 cres_try(QCborValue value, _value.toCborValue(_contexts));
185 obj[__KNOWCORE_VALUE_KEY] = value;
186 return cres_success(obj);
188 static cres_qresult<Value> fromCborValue(
const QCborValue& _value,
191 QCborMap obj = _value.toMap();
192 cres_try(
Value value, Value::fromCborValue(obj[__KNOWCORE_DATATYPE_KEY].toString(),
193 obj[__KNOWCORE_VALUE_KEY], _contexts));
194 return cres_success(value);
196 static cres_qresult<QString> toRdfLiteral(
const Value& _value,
199 cres_try(QString lit, _value.toRdfLiteral(_contexts));
200 return cres_success(clog_qt::qformat(
"<{}>({})", _value.
datatype(), lit));
202 static cres_qresult<Value> fromRdfLiteral(
const QString& _value,
205 QRegularExpressionMatch match = QRegularExpression(
"<(.*)>(.*)").match(_value);
208 return Value::fromRdfLiteral(match.captured(1), match.captured(2), _contexts);
212 return cres_failure(
"Invalid range value literal.");
218 template<
typename _T_>
221 QCryptographicHash hash(QCryptographicHash::Md5);
224 hash.addData(value_lower);
225 Cyqlops::Crypto::Hash::compute(hash, lowerBoundType());
226 hash.addData(value_upper);
227 Cyqlops::Crypto::Hash::compute(hash, upperBoundType());
229 return cres_success(hash.result());
232 template<
typename _T_>
234 requires(std::is_same_v<typename details::RangeComparator<_T_>::ReturnType, cres_qresult<bool>>)
236 switch(lowerBoundType())
238 case RangeBoundType::Inclusive:
247 case RangeBoundType::Exclusive:
252 return cres_success(
false);
255 case RangeBoundType::Unset:
258 switch(upperBoundType())
260 case RangeBoundType::Inclusive:
269 case RangeBoundType::Exclusive:
274 return cres_success(
false);
277 case RangeBoundType::Unset:
280 return cres_success(
true);
283 template<
typename _T_>
285 requires(std::is_same_v<typename details::RangeComparator<_T_>::ReturnType,
bool>)
287 switch(lowerBoundType())
289 case RangeBoundType::Inclusive:
297 case RangeBoundType::Exclusive:
299 if(details::RangeComparator<_T_>::isStrictlyInferior(_v, lower()))
301 return cres_success(
false);
304 case RangeBoundType::Unset:
307 switch(upperBoundType())
309 case RangeBoundType::Inclusive:
311 if(details::RangeComparator<_T_>::isEqual(_v, upper()))
317 case RangeBoundType::Exclusive:
319 if(details::RangeComparator<_T_>::isStrictlyInferior(upper(), _v))
321 return cres_success(
false);
324 case RangeBoundType::Unset:
330#define __KNOWCORE_LOWER_VALUE_KEY QString("lower_value")
331#define __KNOWCORE_LOWER_TYPE_KEY QString("lower_type")
332#define __KNOWCORE_UPPER_VALUE_KEY QString("upper_value")
333#define __KNOWCORE_UPPER_TYPE_KEY QString("upper_value")
335 template<
typename _T_>
336 inline cres_qresult<QJsonValue>
340 cres_try(
object[__KNOWCORE_LOWER_VALUE_KEY],
342 cres_try(
object[__KNOWCORE_UPPER_VALUE_KEY],
344 object[__KNOWCORE_LOWER_TYPE_KEY] = (int)lowerBoundType();
345 object[__KNOWCORE_UPPER_TYPE_KEY] = (int)upperBoundType();
346 return cres_success(
object);
349 template<
typename _T_>
353 if(_value.isObject())
355 QJsonObject
object = _value.toObject();
357 object.value(__KNOWCORE_LOWER_VALUE_KEY), _context));
359 object.value(__KNOWCORE_UPPER_VALUE_KEY), _context));
360 RangeBoundType lowerType = RangeBoundType(
object.value(__KNOWCORE_LOWER_TYPE_KEY).toInt());
361 RangeBoundType upperType = RangeBoundType(
object.value(__KNOWCORE_UPPER_TYPE_KEY).toInt());
362 return cres_success<Range<_T_>>({lowerType, lower, upper, upperType});
366 return cres_failure(
"Expected object got {}", _value);
370 template<
typename _T_>
371 inline cres_qresult<QCborValue>
375 cres_try(
object[__KNOWCORE_LOWER_VALUE_KEY],
377 cres_try(
object[__KNOWCORE_UPPER_VALUE_KEY],
379 object[__KNOWCORE_LOWER_TYPE_KEY] = (int)lowerBoundType();
380 object[__KNOWCORE_UPPER_TYPE_KEY] = (int)upperBoundType();
381 return cres_success(
object);
384 template<
typename _T_>
390 QCborMap
object = _value.toMap();
392 object.value(__KNOWCORE_LOWER_VALUE_KEY), _context));
394 object.value(__KNOWCORE_UPPER_VALUE_KEY), _context));
395 RangeBoundType lowerType
396 = RangeBoundType(
object.value(__KNOWCORE_LOWER_TYPE_KEY).toInteger());
397 RangeBoundType upperType
398 = RangeBoundType(
object.value(__KNOWCORE_UPPER_TYPE_KEY).toInteger());
399 return cres_success<Range<_T_>>({lowerType, lower, upper, upperType});
403 return cres_failure(
"Expected object got {}", _value);
407 template<
typename _T_>
408 inline cres_qresult<QString>
412 switch(lowerBoundType())
414 case RangeBoundType::Exclusive:
415 case RangeBoundType::Unset:
418 case RangeBoundType::Inclusive:
422 QString str_lower, str_upper;
423 if(lowerBoundType() == RangeBoundType::Unset)
431 if(upperBoundType() == RangeBoundType::Unset)
440 v += str_lower +
", " + str_upper;
442 switch(upperBoundType())
444 case RangeBoundType::Exclusive:
445 case RangeBoundType::Unset:
448 case RangeBoundType::Inclusive:
453 return cres_success(v);
456 template<
typename _T_>
457 inline cres_qresult<Range<_T_>>
460 if(_value.size() < 5)
461 return cres_failure(
"Invalid range RDF Literal: '{}'", _value);
463 QString lowerBoundString = _value.left(2);
464 QString upperBoundString = _value.right(2);
465 QStringList values = _value.mid(2, _value.size() - 4).split(
",", KNOWCORE_QT_SKIP_EMPTY_PART);
467 RangeBoundType lowerBoundType;
468 if(lowerBoundString ==
"]]")
469 lowerBoundType = RangeBoundType::Exclusive;
470 else if(lowerBoundString ==
"[[")
471 lowerBoundType = RangeBoundType::Inclusive;
473 return cres_failure(
"Invalid lower bound symbol: '{}'", lowerBoundString);
475 RangeBoundType upperBoundType;
476 if(upperBoundString ==
"[[")
477 upperBoundType = RangeBoundType::Exclusive;
478 else if(upperBoundString ==
"]]")
479 upperBoundType = RangeBoundType::Inclusive;
481 return cres_failure(
"Invalid lower bound symbol: '{}'", lowerBoundString);
483 if(values.size() != 2)
484 return cres_failure(
"Got wrong amount of values in: '{}' got {} expected 2", _value,
488 if(values[0] ==
"inf")
490 lowerBoundType = RangeBoundType::Unset;
496 if(values[1] ==
"inf")
498 upperBoundType = RangeBoundType::Unset;
505 return cres_success<Range<_T_>>({lowerBoundType, lower, upper, upperBoundType});
508 template<
typename _T_>
511 return toRdfLiteral();
514#undef __KNOWCORE_LOWER_VALUE_KEY
515#undef __KNOWCORE_LOWER_TYPE_KEY
516#undef __KNOWCORE_UPPER_VALUE_KEY
517#undef __KNOWCORE_UPPER_TYPE_KEY
522KNOWCORE_DECLARE_FULL_METATYPE(knowCore, QuantityNumberRange);
523KNOWCORE_DECLARE_FULL_METATYPE(knowCore, NumberRange);
524KNOWCORE_DECLARE_FULL_METATYPE(knowCore, ValueRange);
526#include "Formatter.h"
527clog_format_declare_enum_formatter(knowCore::RangeBoundType, Exclusive, Inclusive, Unset);
529template<
typename _T_>
530struct std::formatter<knowCore::Range<_T_>>
Class that can contains large numeric value.
Definition BigNumber.h:51
Definition MetaType.h:126
Definition QuantityValue.h:22
cres_qresult< QString > printable() const
Definition Range.h:509
details::RangeComparator< _T_ >::ReturnType contains(const _T_ &_v) const
Definition Range.h:233
details::RangeComparator< _T_ >::ReturnType contains(const _T_ &_v) const
static cres_qresult< Range > fromRdfLiteral(const QString &_value, const DeserialisationContexts &_context=defaultDeserialisationContext())
Definition Range.h:458
static cres_qresult< Range > fromJsonValue(const QJsonValue &_value, const DeserialisationContexts &_context=defaultDeserialisationContext())
Definition Range.h:350
cres_qresult< QString > toRdfLiteral(const SerialisationContexts &_contexts=defaultSerialisationContext()) const
Definition Range.h:409
cres_qresult< QByteArray > md5() const
Definition Range.h:219
cres_qresult< QJsonValue > toJsonValue(const SerialisationContexts &_contexts=defaultSerialisationContext()) const
Definition Range.h:337
static cres_qresult< Range > fromCborValue(const QCborValue &_value, const DeserialisationContexts &_context=defaultDeserialisationContext())
Definition Range.h:385
cres_qresult< QCborValue > toCborValue(const SerialisationContexts &_contexts=defaultSerialisationContext()) const
Definition Range.h:372
Definition MetaType.h:159
Uri datatype() const
Definition Value.cpp:230
cres_qresult< bool > compare(const Value &_value, ComparisonOperators _operators) const
Definition Value.cpp:316