/**************************************************************************** ** Copyright (c) 2021, Fougue Ltd. ** All rights reserved. ** See license at https://github.com/fougue/mayo/blob/master/LICENSE.txt ****************************************************************************/ #pragma once #include "global.h" #include "property.h" #include "filepath.h" #include "quantity.h" #include #include #include #include #include namespace Mayo { template class GenericProperty : public Property { public: using ValueType = T; // TODO Add value_type traits for T GenericProperty(PropertyGroup* grp, const TextId& name); const T& value() const { return m_value; } bool setValue(const T& val); operator const T&() const { return this->value(); } const char* dynTypeName() const override { return TypeName; } static const char TypeName[]; protected: T m_value = {}; }; template class PropertyScalarConstraints { static_assert(std::is_scalar_v, "Requires scalar type"); public: using ValueType = T; PropertyScalarConstraints() = default; PropertyScalarConstraints(T minimum, T maximum, T singleStep); bool constraintsEnabled() const { return m_constraintsEnabled; } void setConstraintsEnabled(bool on) { m_constraintsEnabled = on; } T minimum() const { return m_minimum; } void setMinimum(T val) { m_minimum = val; } T maximum() const { return m_maximum; } void setMaximum(T val) { m_maximum = val; } void setRange(T minVal, T maxVal); T singleStep() const { return m_singleStep; } void setSingleStep(T step) { m_singleStep = step; } private: T m_minimum; T m_maximum; T m_singleStep; bool m_constraintsEnabled = false; }; template class GenericScalarProperty : public GenericProperty, public PropertyScalarConstraints { public: using ValueType = T; GenericScalarProperty(PropertyGroup* grp, const TextId& name); GenericScalarProperty( PropertyGroup* grp, const TextId& name, T minimum, T maximum, T singleStep); }; class BasePropertyQuantity : public Property, public PropertyScalarConstraints { public: virtual Unit quantityUnit() const = 0; virtual double quantityValue() const = 0; virtual bool setQuantityValue(double v) = 0; inline static const char TypeName[] = "Mayo::BasePropertyQuantity"; const char* dynTypeName() const override { return BasePropertyQuantity::TypeName; } protected: BasePropertyQuantity(PropertyGroup* grp, const TextId& name); }; template class GenericPropertyQuantity : public BasePropertyQuantity { public: using QuantityType = Quantity; GenericPropertyQuantity(PropertyGroup* grp, const TextId& name); Unit quantityUnit() const override { return U; } double quantityValue() const override { return this->quantity().value(); } bool setQuantityValue(double v) override; QuantityType quantity() const { return m_quantity; } bool setQuantity(QuantityType qty); private: QuantityType m_quantity = {}; }; using PropertyBool = GenericProperty; using PropertyInt = GenericScalarProperty; using PropertyDouble = GenericScalarProperty; using PropertyString = GenericProperty; using PropertyCheckState = GenericProperty; using PropertyOccPnt = GenericProperty; using PropertyOccVec = GenericProperty; using PropertyOccTrsf = GenericProperty; using PropertyOccColor = GenericProperty; using PropertyFilePath = GenericProperty; using PropertyLength = GenericPropertyQuantity; using PropertyArea = GenericPropertyQuantity; using PropertyVolume = GenericPropertyQuantity; using PropertyMass = GenericPropertyQuantity; using PropertyTime = GenericPropertyQuantity; using PropertyAngle = GenericPropertyQuantity; using PropertyVelocity = GenericPropertyQuantity; using PropertyDensity = GenericPropertyQuantity; // -- // -- Implementation // -- // GenericProperty<> template GenericProperty::GenericProperty(PropertyGroup* grp, const TextId& name) : Property(grp, name) { } template bool GenericProperty::setValue(const T& val) { return Property::setValueHelper(this, &m_value, val); } // PropertyScalarConstraints<> template PropertyScalarConstraints::PropertyScalarConstraints(T minimum, T maximum, T singleStep) : m_minimum(minimum), m_maximum(maximum), m_singleStep(singleStep), m_constraintsEnabled(true) { } template void PropertyScalarConstraints::setRange(T minVal, T maxVal) { this->setMinimum(minVal); this->setMaximum(maxVal); } // GenericScalarProperty<> template GenericScalarProperty::GenericScalarProperty(PropertyGroup* grp, const TextId& name) : GenericProperty(grp, name) { } template GenericScalarProperty::GenericScalarProperty( PropertyGroup* grp, const TextId& name, T minimum, T maximum, T singleStep) : GenericProperty(grp, name), PropertyScalarConstraints(minimum, maximum, singleStep) { } // GenericPropertyQuantity<> template GenericPropertyQuantity::GenericPropertyQuantity(PropertyGroup* grp, const TextId& name) : BasePropertyQuantity(grp, name) { } template bool GenericPropertyQuantity::setQuantityValue(double v) { return this->setQuantity(QuantityType(v)); } template bool GenericPropertyQuantity::setQuantity(Quantity qty) { return Property::setValueHelper(this, &m_quantity, qty); } } // namespace Mayo