Base: Units: refactor

Refactor Units making it constexpr, immutable, with repetition reduced.
Separate data from code.

Constexpr constructed units allow constructing predefined Quantities
from predefined unit types.
This commit is contained in:
bofdahof
2025-01-01 18:17:00 +10:00
committed by Ladislav Michl
parent f8d2789a43
commit 847e2f5c85
8 changed files with 729 additions and 1155 deletions

View File

@@ -1,178 +1,178 @@
/***************************************************************************
* Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#ifndef BASE_Unit_H
#define BASE_Unit_H
#include <array>
#include <cstdint>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
#include <FCGlobal.h>
#include "Exception.h"
namespace Base
{
/**
* The Unit class.
*/
class BaseExport Unit
constexpr auto unitSymbols =
std::to_array<std::string_view>({"mm", "kg", "s", "A", "K", "mol", "cd", "deg"});
constexpr auto unitNumExponents {unitSymbols.size()};
using UnitExponents = std::array<int8_t, unitNumExponents>;
constexpr auto unitExponentLimit {8};
class BaseExport Unit final
{
public:
/// default constructor
explicit Unit(int8_t Length,
int8_t Mass = 0,
int8_t Time = 0,
int8_t ElectricCurrent = 0,
int8_t ThermodynamicTemperature = 0,
int8_t AmountOfSubstance = 0,
int8_t LuminousIntensity = 0,
int8_t Angle = 0);
Unit();
Unit(const Unit&) = default;
Unit(Unit&&) = default;
explicit Unit(const std::string& expr);
/// Destruction
~Unit() = default;
Unit() = default;
explicit constexpr Unit(const UnitExponents exps, const std::string_view name = "")
: _exps {exps}
, _name {name}
{
checkRange();
}
/// helper constructor to ease Unit construction from Python
explicit Unit(const int length,
const int mass = 0,
const int time = 0,
const int electricCurrent = 0,
const int thermodynamicTemperature = 0,
const int amountOfSubstance = 0,
const int luminousIntensity = 0,
const int angle = 0);
/** Operators. */
//@{
inline Unit& operator*=(const Unit& that);
inline Unit& operator/=(const Unit& that);
int operator[](int index) const;
Unit operator*(const Unit&) const;
Unit operator/(const Unit&) const;
bool operator==(const Unit&) const;
bool operator!=(const Unit& that) const;
Unit& operator=(const Unit&) = default;
Unit& operator=(Unit&&) = default;
Unit pow(double exp) const;
Unit sqrt() const;
Unit cbrt() const;
//@}
int length() const;
int mass() const;
int time() const;
int electricCurrent() const;
int thermodynamicTemperature() const;
int amountOfSubstance() const;
int luminousIntensity() const;
int angle() const;
Unit& operator*=(const Unit& that);
Unit& operator/=(const Unit& that);
Unit operator*(const Unit&) const;
Unit operator/(const Unit&) const;
std::string getString() const;
/// get the type as an string such as "Area", "Length" or "Pressure".
std::string getTypeString() const;
[[nodiscard]] Unit pow(const double exp) const;
[[nodiscard]] Unit root(const uint8_t num) const;
/** Predefined Unit types. */
//@{
static const Unit One;
/// Length unit
static const Unit Length;
/// Mass unit
static const Unit Mass;
[[nodiscard]] UnitExponents exponents() const;
[[nodiscard]] int length() const;
/// Angle
[[nodiscard]] std::string getString() const; // E.g. kg, mm^2, mm*kg/s^2
[[nodiscard]] std::string getTypeString() const; // E.g. "Area", "Length", "Pressure"
[[nodiscard]] std::string representation() const; // E.g. "Unit: mm (1,0,0,0,0,0,0,0) [Length]"
Unit sqrt() const
{
return root(2);
}
Unit cbrt() const
{
return root(3);
}
private:
UnitExponents _exps {};
std::string_view _name;
constexpr void checkRange()
{
for (const auto exp : _exps) {
if (exp >= unitExponentLimit) {
throw OverflowError("Unit exponent overflow");
}
if (exp < -unitExponentLimit) {
throw UnderflowError("Unit exponent underflow");
}
}
}
/** Returns posIndexes, negIndexes*/
std::pair<std::vector<size_t>, std::vector<size_t>> nonZeroValsIndexes() const;
public:
static const Unit Acceleration;
static const Unit AmountOfSubstance;
static const Unit Angle;
static const Unit AngleOfFriction;
static const Unit Density;
static const Unit Area;
static const Unit Volume;
static const Unit TimeSpan;
static const Unit Frequency;
static const Unit Velocity;
static const Unit Acceleration;
static const Unit Temperature;
static const Unit CompressiveStrength;
static const Unit CurrentDensity;
static const Unit Density;
static const Unit DissipationRate;
static const Unit DynamicViscosity;
static const Unit ElectricalCapacitance;
static const Unit ElectricalConductance;
static const Unit ElectricalConductivity;
static const Unit ElectricalInductance;
static const Unit ElectricalResistance;
static const Unit ElectricCharge;
static const Unit ElectricCurrent;
static const Unit ElectricPotential;
static const Unit ElectricCharge;
static const Unit SurfaceChargeDensity;
static const Unit VolumeChargeDensity;
static const Unit ElectromagneticPotential;
static const Unit Force;
static const Unit Frequency;
static const Unit HeatFlux;
static const Unit InverseArea;
static const Unit InverseLength;
static const Unit InverseVolume;
static const Unit KinematicViscosity;
static const Unit Length;
static const Unit LuminousIntensity;
static const Unit MagneticFieldStrength;
static const Unit MagneticFlux;
static const Unit MagneticFluxDensity;
static const Unit Magnetization;
static const Unit ElectricalCapacitance;
static const Unit ElectricalInductance;
static const Unit ElectricalConductance;
static const Unit ElectricalResistance;
static const Unit ElectricalConductivity;
static const Unit ElectromagneticPotential;
static const Unit AmountOfSubstance;
static const Unit LuminousIntensity;
// Pressure
static const Unit CompressiveStrength;
static const Unit Mass;
static const Unit Moment;
static const Unit One;
static const Unit Pressure;
static const Unit Power;
static const Unit ShearModulus;
static const Unit Stress;
static const Unit UltimateTensileStrength;
static const Unit YieldStrength;
static const Unit YoungsModulus;
static const Unit SpecificEnergy;
static const Unit SpecificHeat;
static const Unit Stiffness;
static const Unit StiffnessDensity;
static const Unit Force;
static const Unit Work;
static const Unit Power;
static const Unit Moment;
static const Unit SpecificEnergy;
static const Unit Stress;
static const Unit SurfaceChargeDensity;
static const Unit Temperature;
static const Unit TimeSpan;
static const Unit ThermalConductivity;
static const Unit ThermalExpansionCoefficient;
static const Unit VolumetricThermalExpansionCoefficient;
static const Unit SpecificHeat;
static const Unit ThermalTransferCoefficient;
static const Unit HeatFlux;
static const Unit DynamicViscosity;
static const Unit KinematicViscosity;
static const Unit UltimateTensileStrength;
static const Unit VacuumPermittivity;
static const Unit Velocity;
static const Unit Volume;
static const Unit VolumeChargeDensity;
static const Unit VolumeFlowRate;
static const Unit DissipationRate;
static const Unit InverseLength;
static const Unit InverseArea;
static const Unit InverseVolume;
//@}
private:
uint32_t Val;
static const Unit VolumetricThermalExpansionCoefficient;
static const Unit Work;
static const Unit YieldStrength;
static const Unit YoungsModulus;
};
inline Unit& Unit::operator*=(const Unit& that)
{
*this = *this * that;
return *this;
}
inline Unit& Unit::operator/=(const Unit& that)
{
*this = *this / that;
return *this;
}
} // namespace Base
#endif // BASE_Unit_H