Bayeux  3.4.1
Core Foundation library for SuperNEMO
units.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date: 2010-03-18
4  * Last modified: 2015-10-18
5  *
6  * License: GPL3
7  *
8  * Description:
9  *
10  * Utilities for units based on the CLHEP library's implementation
11  *
12  */
13 #ifndef DATATOOLS_UNITS_H
14 #define DATATOOLS_UNITS_H
15 
16 // Standard Library:
17 #include <string>
18 #include <vector>
19 #include <set>
20 #include <map>
21 #include <limits>
22 #include <list>
23 
24 // Third party:
25 // - Boost:
26 #include <boost/cstdint.hpp>
27 
28 // This Project:
29 #include <datatools/exception.h>
30 #include <datatools/i_tree_dump.h>
31 
32 #ifndef Q_MOC_RUN
34 #endif // Q_MOC_RUN
35 
36 namespace datatools {
37 
47  namespace units {
48 
50  enum power_of_ten {
51  YOCTO = -24,
52  ZEPTO = -21,
53  ATTO = -18,
54  FEMTO = -15,
55  PICO = -12,
56  NANO = -9,
57  MICRO = -6,
58  MILLI = -3,
59  CENTI = -2,
60  DECI = -1,
62  DECA = 1,
63  HECTO = 2,
64  KILO = 3,
65  MEGA = 6,
66  GIGA = 9,
67  TERA = 12,
68  PETA = 15,
69  EXA = 18,
70  ZETTA = 21,
71  YOTTA = 24
72  };
73 
74  double yocto();
75  double zepto();
76  double atto();
77  double femto();
78  double pico();
79  double nano();
80  double micro();
81  double milli();
82  double centi();
83  double deci();
84  double deca();
85  double hecto();
86  double kilo();
87  double mega();
88  double giga();
89  double tera();
90  double peta();
91  double exa();
92  double zetta();
93  double yotta();
94 
97 
100 
103 
107  KIBI = 10,
108  MEBI = 20,
109  GIBI = 30,
110  TEBI = 40,
111  PEBI = 50,
112  EXBI = 60,
113  ZEBI = 70,
114  YOBI = 80
115  };
116 
118  double byte_to_bit_factor();
119 
120  static const double bit = 1.0;
121  static const double octet = 8.0 * bit;
122  static const double byte = octet;
123 
126 
128  std::string power_of_two_to_name(power_of_two);
129 
132 
136  size_t registered_unit_dimension_labels(std::vector<std::string> & dimension_labels_);
137 
141  size_t registered_unit_names(std::vector<std::string> & unit_names_);
142 
146  size_t registered_unit_symbols(std::vector<std::string> & unit_symbols_);
147 
157  bool is_unit_in_dimension_from(const std::string& unit_id_,
158  const std::string& unit_dimension_label_);
159 
169  double get_unit_in_dimension_from(const std::string& unit_id_,
170  const std::string& unit_dimension_label_);
171 
180  double get_length_unit_from(const std::string& unit_id_);
181 
183  double get_surface_unit_from(const std::string&);
184 
186  double get_volume_unit_from(const std::string&);
187 
189  double get_time_unit_from(const std::string&);
190 
192  double get_angle_unit_from(const std::string&);
193 
195  double get_solid_angle_unit_from(const std::string&);
196 
198  double get_energy_unit_from(const std::string&);
199 
201  double get_mass_unit_from(const std::string&);
202 
204  double get_pressure_unit_from(const std::string&);
205 
207  double get_magnetic_flux_density_unit_from(const std::string&);
208 
210  double get_magnetic_field_unit_from(const std::string&);
211 
213  double get_electric_field_unit_from(const std::string&);
214 
216  double get_electric_potential_unit_from(const std::string&);
217 
219  double get_electric_tension_unit_from(const std::string&);
220 
222  double get_temperature_unit_from(const std::string&);
223 
225  double get_density_unit_from(const std::string&);
226 
228  double get_activity_unit_from(const std::string&);
229 
231  double get_surface_activity_unit_from(const std::string&);
232 
234  double get_volume_activity_unit_from(const std::string&);
235 
237  double get_mass_activity_unit_from(const std::string&);
238 
240  double get_frequency_unit_from(const std::string&);
241 
243  double get_electric_charge_unit_from(const std::string&);
244 
246  double get_electric_current_unit_from(const std::string&);
247 
249  double get_velocity_unit_from(const std::string&);
250 
252  double get_speed_unit_from(const std::string&);
253 
254 
256 
263  double get_unit_from(const std::string& unit_type_,
264  const std::string& unit_str_);
265 
267 
274  double get_unit(const std::string& unit_str_, bool throw_ = false);
275 
277  // const std::vector<std::string>& get_unit_labels_registry();
278 
280 
294  bool is_unit_label_valid(const std::string & unit_label_);
295 
297  std::string get_default_unit_symbol_from_label(const std::string & unit_label_);
298 
300 
317  bool find_unit(const std::string & unit_str_,
318  double & unit_value_,
319  std::string & unit_label_);
320 
322 
329  double get_value_with_unit(const std::string& word_);
330 
332 
341  bool find_value_with_unit(const std::string & word_,
342  double & value_,
343  std::string & unit_label_,
344  double default_unit_ = std::numeric_limits<double>::quiet_NaN());
345 
347 
361  bool parse_value_with_unit(const std::string& word,
362  double& value,
363  std::string& unit_symbol,
364  std::string& unit_label,
365  uint32_t flags = 0);
366 
368  class unit
369  : public i_tree_dumpable
370  {
371 
372  public:
373 
375  unit();
376 
378  unit(const std::string & name_,
379  const std::string & dimension_desc_,
380  double value_,
381  bool SI_main_ = false);
382 
384  unit(const std::string & name_,
385  const std::string & symbol_,
386  const std::string & dimension_desc_,
387  double value_,
388  bool SI_main_ = false);
389 
391  unit(const std::string & name_,
392  const std::string & main_symbol_,
393  const std::string & alt_symbols_,
394  const std::string & dimension_desc_,
395  double value_,
396  bool SI_main_ = false);
397 
399  virtual ~unit();
400 
402  bool is_valid() const;
403 
405  void reset();
406 
408  bool has_name() const;
409 
411  const std::string & get_name() const;
412 
414  unit & set_name(const std::string &);
415 
417  unit & add_symbol(const std::string & symbol_, bool main_ = false);
418 
420  bool has_symbol(const std::string & symbol_) const;
421 
423  bool match(const std::string & id_) const;
424 
426  const std::set<std::string> & get_symbols() const;
427 
429  bool has_main_symbol() const;
430 
432  const std::string & get_main_symbol() const;
433 
435  unit & set_main_symbol(const std::string &);
436 
438  bool has_dimension_label() const;
439 
441  const std::string & get_dimension_label() const;
442 
444  unit & set_dimension_label(const std::string &);
445 
447  bool has_dimension_meta() const;
448 
450  const std::string & get_dimension_meta() const;
451 
453  unit & set_dimension_meta(const std::string &);
454 
456  unit & set_dimension_infos(const std::string &);
457 
459  bool is_dimension(const std::string &) const;
460 
462  bool is_SI_main() const;
463 
465  unit & set_SI_main(bool);
466 
468  double get_value() const;
469 
471  unit & set_value(double);
472 
474  operator double() const;
475 
477  const std::string & str() const;
478 
479  // Implement a manipulator for "cout << meter(23.1) << endl;" printing "0.0231" ???
480  // std::ostream & operator()(std::ostream & out_, double x_) const;
481 
483  virtual void tree_dump(std::ostream & out_ = std::clog,
484  const std::string & title_ = "",
485  const std::string & indent_ = "",
486  bool inherit_ = false) const;
487 
488  protected:
489 
491  void _set_default();
492 
493  private:
494 
495  std::string _name_;
496  std::set<std::string> _symbols_;
497  std::string _main_symbol_;
498  std::string _dimension_label_;
499  std::string _dimension_meta_;
500  bool _SI_main_;
501  double _value_;
502 
503  };
504 
507  : public i_tree_dumpable
508  {
509 
510  public:
511 
515  BD_MASS = 1, // [M]
516  BD_LENGTH = 2, // [L]
517  BD_TIME = 3, // [T]
519  BD_TEMPERATURE = 5, // [theta]
520  BD_ABSOLUTE_TEMPERATURE = 5, // [theta]
521  BD_AMOUNT = 6, // [N]
524  BD_PROCEDURE_DEFINED = 8, // [?] For "arbitrary/procedure defined" units
526  };
527 
528  static std::string base_dimension_to_symbol(base_dimension_type);
529 
530  static base_dimension_type symbol_to_base_dimension(const std::string &);
531 
532  static std::string base_dimension_to_label(base_dimension_type);
533 
534  static base_dimension_type label_to_base_dimension(const std::string &);
535 
537  static bool validate_dimension_label(const std::string & name_);
538 
540  unit_dimension();
541 
543  unit_dimension(const std::string & label_,
544  const std::string & default_unit_symbol_);
545 
547  virtual ~unit_dimension();
548 
550  bool is_valid() const;
551 
553  void reset();
554 
556  bool has_label() const;
557 
559  unit_dimension & set_label(const std::string &);
560 
562  const std::string & get_label() const;
563 
565  bool has_unit(const std::string & unit_name_) const;
566 
568  unit_dimension & add_unit(const std::string & unit_name_,
569  bool default_unit_ = false);
570 
572  void remove_unit(const std::string & unit_name_);
573 
575  const std::set<std::string> & get_unit_names() const;
576 
578  bool has_default_unit() const;
579 
581  unit_dimension & set_default_unit_name(const std::string & unit_name_);
582 
584  const std::string & get_default_unit_name() const;
585 
587  bool has_dimensional_powers() const;
588 
591 
593  bool decode_dimensional_powers(const std::string & word_);
594 
596  void encode_dimensional_powers(std::string & word_) const;
597 
599  bool is_dimensionless() const;
600 
602  bool match_dimensional_powers(const unit_dimension & ud_) const;
603 
605  virtual void tree_dump(std::ostream & out_ = std::clog,
606  const std::string & title_ = "",
607  const std::string & indent_ = "",
608  bool inherit_ = false) const;
609 
610  protected:
611 
613  void _set_default();
614 
615  private:
616 
617  std::string _label_;
618  std::set<std::string> _unit_names_;
619  std::string _default_unit_name_;
620  int _dimensional_powers_[9];
621 
622  };
623 
625  class registry
626  : public i_tree_dumpable
627  {
628 
629  public:
630 
633  };
634 
636  typedef std::map<std::string, unit> unit_dict_type;
637 
639  typedef std::map<std::string, const unit *> symbol_dict_type;
640 
642  typedef std::map<std::string, unit_dimension> dimension_dict_type;
643 
645  registry(uint32_t flags_ = 0);
646 
648  ~registry();
649 
651  void registration(const unit & unit_, bool default_in_dimension_ = false);
652 
654  void unregistration(const std::string & unit_name_);
655 
657  bool has_unit(const std::string & unit_name_) const;
658 
660  const unit & get_unit(const std::string & unit_name_) const;
661 
663  bool has_symbol(const std::string & unit_symbol_) const;
664 
666  bool has_unit_from_any(const std::string & unit_label_) const;
667 
669  const unit & get_unit_from_symbol(const std::string & unit_symbol_) const;
670 
672  const unit & get_unit_from_any(const std::string & unit_id_) const;
673 
675  bool has_dimension(const std::string & dimension_label_) const;
676 
678  const unit_dimension & get_dimension(const std::string & dimension_label_) const;
679 
681  void clear();
682 
684  const unit_dict_type & get_units() const;
685 
687  const symbol_dict_type & get_symbols() const;
688 
690  const dimension_dict_type & get_dimensions() const;
691 
693  bool build_ordered_unit_symbols(const std::string & dimension_label_,
694  std::list<std::string> & symbols_) const;
695 
698 
700  virtual void tree_dump(std::ostream & out_ = std::clog,
701  const std::string & title_ = "",
702  const std::string & indent_ = "",
703  bool inherit_ = false) const;
704 
705  protected:
706 
708  unit & _grab_unit(const std::string & unit_name_);
709 
711  unit_dimension & _grab_dimension(const std::string & dimension_label_);
712 
713  private:
714 
715  unit_dict_type _units_;
716  symbol_dict_type _symbols_;
717  dimension_dict_type _dimensions_;
718 
719  public:
720 
722  static registry & system_registry();
723 
725  static const registry & const_system_registry();
726 
727  private:
728 
730  static registry & _access_system_registry_();
731 
732  };
733 
734  } // end of namespace units
735 
736 } // end of namespace datatools
737 
738 #ifndef Q_MOC_RUN
739 // Activate reflection layer:
741 #endif // Q_MOC_RUN
742 
745 #define DT_THROW_BAD_UNIT(UnitType,UnitSymbol) \
746  DT_THROW_IF(true,std::logic_error, \
747  "Invalid '" << UnitType << "' unit for symbol '" << UnitSymbol << "' !")
748 
749 
750 #endif // DATATOOLS_UNITS_H
751 
752 // Local Variables: --
753 // mode: c++ --
754 // c-file-style: "gnu" --
755 // tab-width: 2 --
756 // End: --
std::map< std::string, const unit * > symbol_dict_type
Type of dictionary of unit.
Definition: units.h:639
const std::string & str() const
Shortcut for the symbol.
unit_dimension & set_label(const std::string &)
Set the label of the unit dimension.
unit_dimension()
Default constructor.
const std::string & get_dimension_meta() const
Return the dimension meta.
std::map< std::string, unit > unit_dict_type
Type of dictionary of unit.
Definition: units.h:636
double get_activity_unit_from(const std::string &)
Get the activity unit value from a string.
The description of an unit dimension.
Definition: units.h:506
power_of_two
IEC powers of two (binary prefixes)
Definition: units.h:105
const symbol_dict_type & get_symbols() const
Return the non mutable dictionary of registered unit symbols.
bool is_unit_in_dimension_from(const std::string &unit_id_, const std::string &unit_dimension_label_)
bool has_default_unit() const
Check if a default unit is defined.
double get_volume_activity_unit_from(const std::string &)
Get the volume activity unit value from a string.
const unit & get_unit_from_any(const std::string &unit_id_) const
Return a registered unit by name or symbol.
Definition: units.h:64
double get_density_unit_from(const std::string &)
Get the density unit value from a string.
void clear()
Clear the registry.
double get_angle_unit_from(const std::string &)
Get the angle unit value from a string.
Definition: units.h:62
unit & set_dimension_infos(const std::string &)
Set the dimension infos.
void _set_default()
Set default attributes.
Definition: units.h:59
void register_standard_units()
Register standard units.
size_t registered_unit_dimension_labels(std::vector< std::string > &dimension_labels_)
unit & set_dimension_meta(const std::string &)
Set the dimension meta.
const std::string & get_main_symbol() const
Return the symbol.
Definition: units.h:52
unit & add_symbol(const std::string &symbol_, bool main_=false)
Add a symbol.
std::string power_of_two_to_symbol(power_of_two)
Return the IEC binary prefix symbol associated to a given IEC power of two.
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
double get_value() const
Return the value.
unit & set_main_symbol(const std::string &)
Set the symbol.
const std::string & get_name() const
Return the name.
double get_value_with_unit(const std::string &word_)
Get a value from a string taking into account the unit symbol.
bool has_main_symbol() const
Check main symbol.
double get_mass_unit_from(const std::string &)
Get the mass unitvalue from a string.
void registration(const unit &unit_, bool default_in_dimension_=false)
Registration of an unit.
Definition: units.h:112
bool match(const std::string &id_) const
Check if a given id matches the name or one of the symbol.
bool has_dimension_meta() const
Check dimension meta.
double get_frequency_unit_from(const std::string &)
Get the frequency unit value from a string.
const unit_dict_type & get_units() const
Return the non mutable dictionary of registered units.
unit & set_value(double)
Set the value.
Definition: units.h:67
const std::set< std::string > & get_unit_names() const
Return the set of unit names.
double get_electric_charge_unit_from(const std::string &)
Get the electric charge unit value from a string.
static base_dimension_type label_to_base_dimension(const std::string &)
static registry & system_registry()
Access to the mutable system singleton registry.
Definition: units.h:56
const unit & get_unit(const std::string &unit_name_) const
Return a registered unit.
double power_of_two_to_factor(power_of_two)
Return the IEC binary prefix approximated factor associated to a given IEC power of two.
bool has_label() const
Check the label.
bool parse_value_with_unit(const std::string &word, double &value, std::string &unit_symbol, std::string &unit_label, uint32_t flags=0)
Parse a value from a string taking into account the unit symbol, set the value, the unit symbol and u...
const std::set< std::string > & get_symbols() const
Build the set of associated symbols.
double get_time_unit_from(const std::string &)
Get the time unit value from a string.
bool decode_dimensional_powers(const std::string &word_)
Decode and set the dimensional powers from a character string.
static const registry & const_system_registry()
Access to the non mutable system singleton registry.
bool is_dimension(const std::string &) const
Check if the dimension matches a given label.
Utility macros for exception handling.
bool build_ordered_unit_symbols(const std::string &dimension_label_, std::list< std::string > &symbols_) const
Build the list of unit symbols associated to a given dimension with units ordered by values (i....
bool match_dimensional_powers(const unit_dimension &ud_) const
Check if the dimensional powers match another unit dimension.
virtual ~unit_dimension()
Destructor.
Definition: units.h:108
static std::string base_dimension_to_label(base_dimension_type)
bool has_symbol(const std::string &unit_symbol_) const
Check unit symbol.
double get_electric_tension_unit_from(const std::string &)
static std::string base_dimension_to_symbol(base_dimension_type)
double get_length_unit_from(const std::string &unit_id_)
bool find_unit(const std::string &unit_str_, double &unit_value_, std::string &unit_label_)
Find the specified unit and the associated unit label from a string.
unit & set_name(const std::string &)
Set the name.
bool has_unit(const std::string &unit_name_) const
Check unit.
Definition: units.h:51
unit_dimension & set_default_unit_name(const std::string &unit_name_)
Set the name of the default unit.
std::string get_default_unit_symbol_from_label(const std::string &unit_label_)
Return the symbol of the default unit associated to a unit label supported by the datatools::units cl...
size_t registered_unit_names(std::vector< std::string > &unit_names_)
const unit & get_unit_from_symbol(const std::string &unit_symbol_) const
Return a registered unit per symbol.
bool is_valid() const
Check validity.
base_dimension_type
Base dimension index.
Definition: units.h:513
bool find_value_with_unit(const std::string &word_, double &value_, std::string &unit_label_, double default_unit_=std::numeric_limits< double >::quiet_NaN())
Get a value from a string taking into account the unit symbol.
Definition: units.h:53
double get_speed_unit_from(const std::string &)
bool has_unit_from_any(const std::string &unit_label_) const
Check unit name or symbol.
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
const std::string & get_label() const
Return the label of the unit dimension.
const unit_dimension & get_dimension(const std::string &dimension_label_) const
Return a registered unit dimension.
double get_volume_unit_from(const std::string &)
Get the volume unit value from a string.
double get_magnetic_flux_density_unit_from(const std::string &)
Get the magnetic field unit value from a string.
bool has_unit(const std::string &unit_name_) const
Check if a unit is known by name.
double get_pressure_unit_from(const std::string &)
Get the pressure unit value from a string.
Definition: units.h:65
double get_unit(const std::string &unit_str_, bool throw_=false)
Get the CLHEP unspecified unit value from a string.
void reset_dimensional_powers()
Reset the the dimensional analysis information.
bool has_dimension_label() const
Check dimension label.
void encode_dimensional_powers(std::string &word_) const
Encode the set of dimensional powers.
double get_unit_from(const std::string &unit_type_, const std::string &unit_str_)
Get the CLHEP unit value from a string specifiying the unit type.
Definition: units.h:58
double get_mass_activity_unit_from(const std::string &)
Get the mass activity unit value from a string.
unit()
Default constructor.
double get_unit_in_dimension_from(const std::string &unit_id_, const std::string &unit_dimension_label_)
void _set_default()
Set default attributes.
Definition: units.h:71
static base_dimension_type symbol_to_base_dimension(const std::string &)
Definition: units.h:54
double byte_to_bit_factor()
Return the IEC 80000-13 byte (octet 8 bits) to bit factor.
Definition: units.h:113
bool is_valid() const
Check validity.
bool has_name() const
Check the name.
A registry for units.
Definition: units.h:625
bool has_dimension(const std::string &dimension_label_) const
Check dimension.
std::map< std::string, unit_dimension > dimension_dict_type
Type of dictionary of dimension.
Definition: units.h:642
std::string power_of_two_to_name(power_of_two)
Return the IEC binary prefix name associated to a given IEC power of two.
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
Definition: units.h:57
void unregistration(const std::string &unit_name_)
Unregistration of an unit.
bool is_unit_label_valid(const std::string &unit_label_)
Return an array containing the labels associated to all type of units supported by the datatools::uni...
size_t registered_unit_symbols(std::vector< std::string > &unit_symbols_)
unit & set_SI_main(bool)
Set the main SI unit flag.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
const dimension_dict_type & get_dimensions() const
Return the non mutable dictionary of registered unit dimensions.
double get_electric_current_unit_from(const std::string &)
Get the electric current unit value from a string.
bool has_symbol(const std::string &symbol_) const
Check if a given symbol is defined.
double get_surface_unit_from(const std::string &)
Get the surface unit value from a string.
init_flag_type
Definition: units.h:631
Definition: units.h:60
double get_surface_activity_unit_from(const std::string &)
Get the surface activity unit value from a string.
double power_of_ten_to_factor(power_of_ten)
Return the SI prefix factor associated to a given SI power of ten.
Definition: units.h:63
double get_electric_field_unit_from(const std::string &)
Get the electric field unit value from a string.
std::string power_of_ten_to_symbol(power_of_ten)
Return the SI symbol associated to a given SI power of ten.
bool is_dimensionless() const
Check if unit dimension is dimensionless.
#define DR_TYPE_INIT(Introspectable)
Definition: reflection_interface.h:165
power_of_ten
SI powers of ten.
Definition: units.h:50
unit & set_dimension_label(const std::string &)
Set the dimension label.
Definition: units.h:70
Definition: units.h:110
double get_solid_angle_unit_from(const std::string &)
Get the solid angle unit value from a string.
Definition: units.h:114
Definition: units.h:109
double get_electric_potential_unit_from(const std::string &)
Get the electric potential unit value from a string.
Definition: units.h:107
registry(uint32_t flags_=0)
Default constructor.
Definition: units.h:69
unit_dimension & add_unit(const std::string &unit_name_, bool default_unit_=false)
Add an unit by name.
const std::string & get_default_unit_name() const
Return the name of the default unit.
void remove_unit(const std::string &unit_name_)
Remove an unit by name.
static bool validate_dimension_label(const std::string &name_)
Validate a unit dimension label.
Definition: units.h:66
double get_energy_unit_from(const std::string &)
Get the energy unit from a string.
Definition: units.h:55
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
Definition: units.h:111
virtual ~unit()
Destructor.
unit_dimension & _grab_dimension(const std::string &dimension_label_)
Return a mutable registered unit dimension.
double get_magnetic_field_unit_from(const std::string &)
double get_temperature_unit_from(const std::string &)
Get the temperature unit value from a string.
bool has_dimensional_powers() const
Check if the dimensional analysis information is available.
bool is_SI_main() const
Check if the unit is the main SI unit for its unit dimension.
Definition: units.h:68
const std::string & get_dimension_label() const
Return the dimension label.
double get_velocity_unit_from(const std::string &)
Get the velocity unit value from a string.
std::string power_of_ten_to_name(power_of_ten)
Return the SI name associated to a given SI power of ten.
The description of an unit.
Definition: units.h:368
unit & _grab_unit(const std::string &unit_name_)
Return a mutable registered unit.