3#include <QCryptographicHash>
19 template<
typename _T_>
22 template<
typename _TOther_,
typename _Enabled_ =
void>
28 template<
typename _TOther_,
typename _TUri_, std::enable_if_t<not std::is_base_of_v<Value, _TOther_>,
bool> = true>
29 static std::enable_if_t<knowCore::Uris::IsUriDefinitionV<_TUri_>,
ReturnValue<QuantityValue>> create(
const _TOther_& _value,
const _TUri_& _unit_uri) {
return create(_value,
knowCore::Uri(_unit_uri)); }
30 template<
typename _TOther_, std::enable_if_t<not std::is_base_of_v<Value, _TOther_>,
bool> = true>
33 template<
typename _TOther_, std::enable_if_t<not std::is_base_of_v<Value, _TOther_>,
bool> = true>
36 template<
typename _TOther_, std::enable_if_t<not std::is_base_of_v<Value, _TOther_>,
bool> = true>
47 _T_
value()
const {
return m_value; }
123 template<
typename _T_>
124 QuantityValue<_T_> operator*(
const _T_& _lhs,
const QuantityValue<_T_>& _rhs);
128 template<
typename _TOther_>
142 template<
typename _T_>
147 template<
typename _T_>
148 template<
typename _TOther_, std::enable_if_t<not std::is_base_of_v<Value, _TOther_>,
bool>>
151 KNOWCORE_RETURN_VALUE_TRY(unit,
Unit::byUri(_unit_uri));
155 template<
typename _T_>
158 KNOWCORE_RETURN_VALUE_TRY(
value, _value.
value<_T_>());
159 return create(
value, _unit_uri);
162 template<
typename _T_>
163 template<
typename _TOther_, std::enable_if_t<not std::is_base_of_v<Value, _TOther_>,
bool>>
164 inline ReturnValue<QuantityValue<_T_>> QuantityValue<_T_>::create(
const _TOther_& _value,
const Symbol& _unit_symbol)
167 KNOWCORE_RETURN_VALUE_TRY(
value, Constructor<_TOther_>::construct(_value));
168 return kCrvSuccess(QuantityValue(
value, unit));
171 template<
typename _T_>
172 inline ReturnValue<QuantityValue<_T_>> QuantityValue<_T_>::create(
const Value& _value,
const Symbol& _unit_symbol)
174 KNOWCORE_RETURN_VALUE_TRY(
value, _value.value<_T_>());
175 return create(
value, _unit_symbol);
178 template<
typename _T_>
179 inline ReturnValue<_T_> QuantityValue<_T_>::cast_rhs(
const QuantityValue& _rhs)
const
181 if(_rhs.unit().base() == unit().base())
183 return kCrvSuccess(_rhs.value() * _rhs.unit().scale() / unit().scale());
185 return kCrvError(
"Cannot operate on incompatible units: {} is not compatible with {}",
unit(), _rhs.unit());
189 template<
typename _T_>
192 KNOWCORE_RETURN_VALUE_TRY(rhsv, cast_rhs(_rhs));
193 return kCrvSuccess<QuantityValue<_T_>>({
value() + rhsv,
unit()});
195 template<
typename _T_>
198 KNOWCORE_RETURN_VALUE_TRY(rhsv, cast_rhs(_rhs));
199 return kCrvSuccess<QuantityValue<_T_>>({
value() - rhsv,
unit()});
201 template<
typename _T_>
206 KNOWCORE_RETURN_VALUE_TRY(rhsv, cast_rhs(_rhs));
207 return kCrvSuccess<QuantityValue<_T_>>({
value() / rhsv, Unit::empty()});
209 KNOWCORE_RETURN_VALUE_TRY(runit,
unit() / _rhs.
unit());
210 return kCrvSuccess<QuantityValue<_T_>>({
value() / _rhs.
value(), runit});
213 template<
typename _T_>
218 KNOWCORE_RETURN_VALUE_TRY(rhsv, cast_rhs(_rhs));
219 KNOWCORE_RETURN_VALUE_TRY(runit,
unit() *
unit());
220 return kCrvSuccess<QuantityValue<_T_>>({
value() * rhsv, runit});
222 KNOWCORE_RETURN_VALUE_TRY(runit,
unit() * _rhs.
unit());
223 return kCrvSuccess<QuantityValue<_T_>>({
value() * _rhs.
value(), runit});
227 template<
typename _T_>
233 template<
typename _T_>
239 template<
typename _T_>
246 template<
typename _T_>
252 template<
typename _T_>
255 KNOWCORE_RETURN_VALUE_TRY(rhsv, cast_rhs(_rhs));
256 return kCrvSuccess(
value() < rhsv);
259 template<
typename _T_>
262 QCryptographicHash hash(QCryptographicHash::Md5);
264 hash.addData(value_md5);
265 hash.addData(
unit().symbol().toUtf8());
266 return kCrvSuccess(hash.result());
269#define __KNOWCORE_VALUE_KEY QString("value")
270#define __KNOWCORE_UNIT_KEY QString("unit")
272 template<
typename _T_>
277 object[__KNOWCORE_UNIT_KEY] =
unit().
symbol();
278 return kCrvSuccess(
object);
281 template<
typename _T_>
284 if(_value.isObject())
286 QJsonObject
object = _value.toObject();
288 KNOWCORE_RETURN_VALUE_TRY(unit,
Unit::bySymbol(
object.
value(__KNOWCORE_UNIT_KEY).toString()));
289 return kCrvSuccess<QuantityValue<_T_>>({
value, unit});
291 return kCrvError(
"Expected object got {}", _value);
295 template<
typename _T_>
300 object[__KNOWCORE_UNIT_KEY] =
unit().
symbol();
301 return kCrvSuccess(
object);
304 template<
typename _T_>
309 QCborMap
object = _value.toMap();
311 KNOWCORE_RETURN_VALUE_TRY(unit,
Unit::bySymbol(
object.
value(__KNOWCORE_UNIT_KEY).toString()));
312 return kCrvSuccess<QuantityValue<_T_>>({
value, unit});
314 return kCrvError(
"Expected object got {}", _value);
318 template<
typename _T_>
322 return kCrvSuccess(str +
" " + m_unit.
symbol());
325 template<
typename _T_>
328 QStringList splited = _value.split(
" ", KNOWCORE_QT_SKIP_EMPTY_PART);
329 if(splited.size() == 0)
331 return kCrvError(
"Empty literal");
333 Unit u = Unit::empty();
334 if(splited.size() == 2)
339 return kCrvSuccess<QuantityValue<_T_>>({
value, u});
342 template<
typename _T_>
348#undef __KNOWCORE_UNIT_KEY
349#undef __KNOWCORE_VALUE_KEY
357template<
typename _T_>
358struct fmt::formatter<knowCore::QuantityValue<_T_>> :
public clog_fmt::base_formatter {
359 template <
typename FormatContext>
362 return format_to(ctx.out(),
"({} {})", p.
value(), p.
unit());
Class that can contains large numeric value.
Definition BigNumber.h:46
Definition MetaType.h:114
Definition NamedType.h:40
Definition QuantityValue.h:21
Unit unit() const
Definition QuantityValue.h:43
ReturnValue< QuantityValue > operator-(const QuantityValue &_rhs) const
Definition QuantityValue.h:196
ReturnValue< QString > toRdfLiteral(const SerialisationContexts &_contexts=defaultSerialisationContext()) const
Definition QuantityValue.h:319
bool operator==(const QuantityValue &_rhs) const
Definition QuantityValue.h:247
ReturnValue< QuantityValue > operator+(const QuantityValue &_rhs) const
Definition QuantityValue.h:190
ReturnValue< QString > printable() const
Definition QuantityValue.h:343
_T_ value() const
Definition QuantityValue.h:47
ReturnValue< bool > operator<(const QuantityValue &_rhs) const
Definition QuantityValue.h:253
static ReturnValue< QuantityValue > fromJsonValue(const QJsonValue &_value, const DeserialisationContexts &_context=defaultDeserialisationContext())
Definition QuantityValue.h:282
static ReturnValue< QuantityValue > fromCborValue(const QCborValue &_value, const DeserialisationContexts &_context=defaultDeserialisationContext())
Definition QuantityValue.h:305
ReturnValue< QuantityValue > operator/(const QuantityValue &_rhs) const
Definition QuantityValue.h:202
ReturnValue< QuantityValue > operator*(const QuantityValue &_rhs) const
Definition QuantityValue.h:214
static ReturnValue< QuantityValue > fromRdfLiteral(const QString &_value, const DeserialisationContexts &_context=defaultDeserialisationContext())
Definition QuantityValue.h:326
ReturnValue< QByteArray > md5() const
Definition QuantityValue.h:260
ReturnValue< QJsonValue > toJsonValue(const SerialisationContexts &_contexts=defaultSerialisationContext()) const
Definition QuantityValue.h:273
ReturnValue< QCborValue > toCborValue(const SerialisationContexts &_contexts=defaultSerialisationContext()) const
Definition QuantityValue.h:296
Definition ReturnValue.h:29
Definition MetaType.h:145
static ReturnValue< Unit > bySymbol(const QString &_symbol)
Definition Unit.cpp:404
static ReturnValue< Unit > byUri(const knowCore::Uri &_symbol)
Definition Unit.cpp:424
Unit base() const
Definition Unit.cpp:259
QString symbol() const
Definition Unit.cpp:244
ReturnValue< _T_ > value(TypeCheckingMode _conversion=TypeCheckingMode::Safe) const
Definition Value.h:229