14#include "arcane/utils/ArcanePrecomp.h"
16#include "arcane/utils/PlatformUtils.h"
17#include "arcane/utils/NotImplementedException.h"
18#include "arcane/utils/FatalErrorException.h"
19#include "arcane/utils/ArgumentException.h"
20#include "arcane/utils/Array.h"
21#include "arcane/utils/TraceInfo.h"
23#include "arcane/core/FactoryService.h"
24#include "arcane/core/AbstractService.h"
25#include "arcane/core/IPhysicalUnitSystemService.h"
26#include "arcane/core/IPhysicalUnitSystem.h"
27#include "arcane/core/IPhysicalUnitConverter.h"
28#include "arcane/core/IPhysicalUnit.h"
48class UdunitsPhysicalUnit
53 friend class UdunitsPhysicalUnitSystem;
57 UdunitsPhysicalUnit(UdunitsPhysicalUnitSystem*, ::ut_unit* unit,
62 m_name = String::fromUtf8(
name.utf8());
65 UdunitsPhysicalUnit(::ut_unit* unit)
70 virtual ~UdunitsPhysicalUnit()
90class UdunitsPhysicalUnitConverter
98 ::cv_converter* converter)
99 : m_from_unit(from_unit)
101 , m_is_from_owned(
true)
102 , m_is_to_owned(
true)
103 , m_converter(converter)
107 ~UdunitsPhysicalUnitConverter()
119 double new_value = cv_convert_double(m_converter, value);
129 throw ArgumentException(A_FUNCINFO,
"input and ouput arrays do not have the same number of elements");
130 cv_convert_doubles(m_converter, input_values.
data(), nb, output_values.
data());
147 bool m_is_from_owned;
149 ::cv_converter* m_converter;
155class UdunitsPhysicalUnitSystem
167 virtual ~UdunitsPhysicalUnitSystem()
170 ut_free_system(m_unit_system);
176 _checkCreateUnitSystem();
179 if (!from_unit || !to_unit)
180 throw ArgumentException(A_FUNCINFO,
"can not convert units to this system unit");
181 return _createConverter(from_unit, to_unit);
186 info() <<
"Create unit converter from='" << from <<
"' to='" << to <<
"'";
187 _checkCreateUnitSystem();
191 return _createConverter(from_unit, to_unit);
198 ::cv_converter* cv_cvt = ::ut_get_converter(from_unit->m_unit, to_unit->m_unit);
201 String::format(
"Can not convert from '{0}' to '{1}' because units are not convertible",
202 from_unit->
name(), to_unit->
name()));
207 void _checkCreateUnitSystem()
211 m_unit_system = ut_read_xml(0);
213 throw FatalErrorException(A_FUNCINFO,
"Can not load unit system");
216 UdunitsPhysicalUnit* _createUnit(
const String& name)
218 ::ut_unit* unit = ut_parse(m_unit_system, (
const char*)name.utf8().data(), UT_UTF8);
220 throw FatalErrorException(A_FUNCINFO, String::format(
"Can not create unit from string '{0}'", name));
221 return new UdunitsPhysicalUnit(
this, unit, name);
226 ::ut_system* m_unit_system;
235class UdunitsUnitSystemService
242 virtual ~UdunitsUnitSystemService() {}
246 virtual void build();
266UdunitsUnitSystemService::
267UdunitsUnitSystemService(
const ServiceBuildInfo& sbi)
268: AbstractService(sbi)
#define ARCANE_REGISTER_APPLICATION_FACTORY(aclass, ainterface, aname)
Registers a factory service for the class aclass.
AbstractService(const ServiceBuildInfo &)
Constructor from a ServiceBuildInfo.
Exception when an argument is invalid.
constexpr const_pointer data() const noexcept
Pointer to the start of the view.
constexpr Integer size() const noexcept
Returns the size of the array.
constexpr const_pointer data() const noexcept
Pointer to the allocated memory.
constexpr Integer size() const noexcept
Number of elements in the array.
Exception when a fatal error has occurred.
Interface of a unit converter.
Interface of a service managing a unit system.
Interface of a unit system.
Structure containing the information to create a service.
Unicode character string.
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
TraceMessage info() const
Flow for an information message.
ITraceMng * traceMng() const
Trace manager.
virtual IPhysicalUnit * fromUnit()
Starting unit.
virtual void convert(RealConstArrayView input_values, RealArrayView output_values)
Returns the converted values of input_values in output_values.
virtual Real convert(Real value)
Returns the converted value of value.
virtual IPhysicalUnit * toUnit()
Target unit.
virtual IPhysicalUnitConverter * createConverter(const String &from, const String &to)
Creates a converter between two units. The caller must destroy the returned converter.
virtual IPhysicalUnitConverter * createConverter(IPhysicalUnit *from, IPhysicalUnit *to)
Creates a converter between two units. The caller must destroy the returned converter....
virtual const String & name() const
Unit name.
Service for managing physical unit system using 'udunits2'.
virtual void build()
Build-level construction of the service.
virtual IPhysicalUnitSystem * createStandardUnitSystem()
Creates a unit system for the International System SI.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
ArrayView< Real > RealArrayView
C equivalent of a 1D array of reals.
ConstArrayView< Real > RealConstArrayView
C equivalent of a 1D array of reals.