4#include <QCryptographicHash>
20 template<
typename _T_>
23 template<
typename _TOther_>
29 template<
typename _TOther_,
typename _TUri_>
30 requires(not std::is_base_of_v<Value, _TOther_> and knowCore::Uris::IsUriDefinitionV<_TUri_>)
31 static cres_qresult<QuantityValue> create(
const _TOther_& _value,
const _TUri_& _unit_uri)
35 template<
typename _TOther_>
36 requires(not std::is_base_of_v<Value, _TOther_>)
37 static cres_qresult<QuantityValue> create(
const _TOther_& _value,
39 static cres_qresult<QuantityValue> create(
const Value& _value,
const knowCore::Uri& _unit_uri);
40 template<
typename _TOther_>
41 requires(not std::is_base_of_v<Value, _TOther_>)
42 static cres_qresult<QuantityValue> create(
const _TOther_& _value,
const QString& _unit_uri)
44 static cres_qresult<QuantityValue> create(
const Value& _value,
const QString& _unit_uri)
46 template<
typename _TOther_>
47 requires(not std::is_base_of_v<Value, _TOther_>)
48 static cres_qresult<QuantityValue> create(
const _TOther_& _value,
const Symbol& _unit_symbol);
49 static cres_qresult<QuantityValue> create(
const Value& _value,
const Symbol& _unit_symbol);
58 _T_
value()
const {
return m_value; }
98 cres_qresult<QByteArray>
md5()
const;
103 = defaultSerialisationContext())
const;
107 static cres_qresult<QuantityValue>
fromJsonValue(
const QJsonValue& _value,
109 = defaultDeserialisationContext());
114 = defaultSerialisationContext())
const;
118 static cres_qresult<QuantityValue>
fromCborValue(
const QCborValue& _value,
120 = defaultDeserialisationContext());
125 = defaultSerialisationContext())
const;
129 static cres_qresult<QuantityValue>
fromRdfLiteral(
const QString& _value,
131 = defaultDeserialisationContext());
144 template<
typename _T_>
145 QuantityValue<_T_> operator*(
const _T_& _lhs,
const QuantityValue<_T_>& _rhs);
149 template<
typename _TOther_>
152 static cres_qresult<BigNumber> construct(
const _TOther_& _v)
153 requires(not std::is_same_v<_TOther_, QString>)
157 static cres_qresult<BigNumber> construct(
const QString& _v)
158 requires(std::is_same_v<_TOther_, QString>)
160 return BigNumber::fromString(_v);
166 template<
typename _T_>
168 : m_value(_value), m_unit(_unit)
172 template<
typename _T_>
173 template<
typename _TOther_>
174 requires(not std::is_base_of_v<Value, _TOther_>)
182 template<
typename _T_>
183 inline cres_qresult<QuantityValue<_T_>> QuantityValue<_T_>::create(
const knowCore::Value& _value,
187 return create(
value, _unit_uri);
190 template<
typename _T_>
191 template<
typename _TOther_>
192 requires(not std::is_base_of_v<Value, _TOther_>)
193 inline cres_qresult<QuantityValue<_T_>> QuantityValue<_T_>::create(
const _TOther_& _value,
194 const Symbol& _unit_symbol)
197 cres_try(_T_
value, Constructor<_TOther_>::construct(_value));
198 return cres_success(QuantityValue(
value, unit));
201 template<
typename _T_>
202 inline cres_qresult<QuantityValue<_T_>> QuantityValue<_T_>::create(
const Value& _value,
203 const Symbol& _unit_symbol)
205 cres_try(_T_
value, _value.value<_T_>());
206 return create(
value, _unit_symbol);
209 template<
typename _T_>
210 inline cres_qresult<_T_> QuantityValue<_T_>::cast_rhs(
const QuantityValue& _rhs)
const
212 if(_rhs.unit().base() == unit().base())
214 return cres_success(_rhs.value() * _rhs.unit().scale() / unit().scale());
218 return cres_failure(
"Cannot operate on incompatible units: {} is not compatible with {}",
219 unit(), _rhs.unit());
223 template<
typename _T_>
224 inline cres_qresult<QuantityValue<_T_>>
227 cres_try(_T_ rhsv, cast_rhs(_rhs));
228 return cres_success<QuantityValue<_T_>>({
value() + rhsv,
unit()});
230 template<
typename _T_>
231 inline cres_qresult<QuantityValue<_T_>>
234 cres_try(_T_ rhsv, cast_rhs(_rhs));
235 return cres_success<QuantityValue<_T_>>({
value() - rhsv,
unit()});
237 template<
typename _T_>
238 inline cres_qresult<QuantityValue<_T_>>
243 cres_try(_T_ rhsv, cast_rhs(_rhs));
244 return cres_success<QuantityValue<_T_>>({
value() / rhsv, Unit::empty()});
249 return cres_success<QuantityValue<_T_>>({
value() / _rhs.
value(), runit});
252 template<
typename _T_>
253 inline cres_qresult<QuantityValue<_T_>>
258 cres_try(_T_ rhsv, cast_rhs(_rhs));
260 return cres_success<QuantityValue<_T_>>({
value() * rhsv, runit});
265 return cres_success<QuantityValue<_T_>>({
value() * _rhs.
value(), runit});
269 template<
typename _T_>
275 template<
typename _T_>
281 template<
typename _T_>
287 template<
typename _T_>
293 template<
typename _T_>
296 cres_try(_T_ rhsv, cast_rhs(_rhs));
297 return cres_success(
value() < rhsv);
300 template<
typename _T_>
303 QCryptographicHash hash(QCryptographicHash::Md5);
305 hash.addData(value_md5);
306 hash.addData(
unit().symbol().toUtf8());
307 return cres_success(hash.result());
310#define __KNOWCORE_VALUE_KEY QString("value")
311#define __KNOWCORE_UNIT_KEY QString("unit")
313 template<
typename _T_>
314 inline cres_qresult<QJsonValue>
318 cres_try(
object[__KNOWCORE_VALUE_KEY],
320 object[__KNOWCORE_UNIT_KEY] =
unit().
symbol();
321 return cres_success(
object);
324 template<
typename _T_>
325 inline cres_qresult<QuantityValue<_T_>>
329 if(_value.isObject())
331 QJsonObject
object = _value.toObject();
333 object.
value(__KNOWCORE_VALUE_KEY), _context));
335 return cres_success<QuantityValue<_T_>>({
value, unit});
339 return cres_failure(
"Expected object got {}", _value);
343 template<
typename _T_>
344 inline cres_qresult<QCborValue>
348 cres_try(
object[__KNOWCORE_VALUE_KEY],
350 object[__KNOWCORE_UNIT_KEY] =
unit().
symbol();
351 return cres_success(
object);
354 template<
typename _T_>
355 inline cres_qresult<QuantityValue<_T_>>
361 QCborMap
object = _value.toMap();
363 object.
value(__KNOWCORE_VALUE_KEY), _context));
365 return cres_success<QuantityValue<_T_>>({
value, unit});
369 return cres_failure(
"Expected object got {}", _value);
373 template<
typename _T_>
374 inline cres_qresult<QString>
378 return cres_success(str +
" " + m_unit.
symbol());
381 template<
typename _T_>
382 inline cres_qresult<QuantityValue<_T_>>
386 QStringList splited = _value.split(
" ", KNOWCORE_QT_SKIP_EMPTY_PART);
387 if(splited.size() == 0)
389 return cres_failure(
"Empty literal");
391 Unit u = Unit::empty();
392 if(splited.size() == 2)
397 return cres_success<QuantityValue<_T_>>({
value, u});
400 template<
typename _T_>
406#undef __KNOWCORE_UNIT_KEY
407#undef __KNOWCORE_VALUE_KEY
415template<
typename _T_>
416struct std::formatter<knowCore::QuantityValue<_T_>> :
public clog_format::base_formatter
418 template<
typename FormatContext>
420 FormatContext& ctx)
const ->
decltype(ctx.out())
422 return format_to(ctx.out(),
"({} {})", p.
value(), p.
unit());
Class that can contains large numeric value.
Definition BigNumber.h:51
Definition MetaType.h:126
Definition QuantityValue.h:22
static cres_qresult< QuantityValue > fromRdfLiteral(const QString &_value, const DeserialisationContexts &_context=defaultDeserialisationContext())
Definition QuantityValue.h:383
cres_qresult< QuantityValue > operator*(const QuantityValue &_rhs) const
Definition QuantityValue.h:254
cres_qresult< QuantityValue > operator+(const QuantityValue &_rhs) const
Definition QuantityValue.h:225
Unit unit() const
Definition QuantityValue.h:54
cres_qresult< QByteArray > md5() const
Definition QuantityValue.h:301
cres_qresult< QString > printable() const
Definition QuantityValue.h:401
bool operator==(const QuantityValue &_rhs) const
Definition QuantityValue.h:288
cres_qresult< QCborValue > toCborValue(const SerialisationContexts &_contexts=defaultSerialisationContext()) const
Definition QuantityValue.h:345
static cres_qresult< QuantityValue > fromJsonValue(const QJsonValue &_value, const DeserialisationContexts &_context=defaultDeserialisationContext())
Definition QuantityValue.h:326
_T_ value() const
Definition QuantityValue.h:58
cres_qresult< QString > toRdfLiteral(const SerialisationContexts &_contexts=defaultSerialisationContext()) const
Definition QuantityValue.h:375
cres_qresult< QuantityValue > operator-(const QuantityValue &_rhs) const
Definition QuantityValue.h:232
cres_qresult< QuantityValue > operator/(const QuantityValue &_rhs) const
Definition QuantityValue.h:239
static cres_qresult< QuantityValue > fromCborValue(const QCborValue &_value, const DeserialisationContexts &_context=defaultDeserialisationContext())
Definition QuantityValue.h:356
cres_qresult< QJsonValue > toJsonValue(const SerialisationContexts &_contexts=defaultSerialisationContext()) const
Definition QuantityValue.h:315
cres_qresult< bool > operator<(const QuantityValue &_rhs) const
Definition QuantityValue.h:294
Definition MetaType.h:159
Unit base() const
Definition Unit.cpp:251
static cres_qresult< Unit > byUri(const knowCore::Uri &_symbol)
Definition Unit.cpp:401
QString symbol() const
Definition Unit.cpp:245
static cres_qresult< Unit > bySymbol(const QString &_symbol)
Definition Unit.cpp:377
cres_qresult< _T_ > value(TypeCheckingMode _conversion=TypeCheckingMode::Safe) const
Definition Value.h:353
Definition QuantityValue.h:151