tUnits.hh - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
HTML git clone git://src.adamsgaard.dk/pism
DIR Log
DIR Files
DIR Refs
DIR LICENSE
---
tUnits.hh (3262B)
---
1 /* Copyright (C) 2013, 2014, 2015, 2016, 2017 PISM Authors
2 *
3 * This file is part of PISM.
4 *
5 * PISM is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
8 * version.
9 *
10 * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with PISM; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef _PISMUNITS_H_
21 #define _PISMUNITS_H_
22
23 #include <string>
24 #include <memory>
25
26 #include <udunits2.h>
27
28 namespace pism {
29
30 namespace units {
31
32 /** @file Units.hh This file contains thin wrappers around
33 * UDUNITS-2 objects. Nothing fancy. The only purpose is to simplify
34 * memory management for objects that are stored as data members of
35 * other C++ classes.
36 *
37 * One thing is worth mentioning, though: in UDUNITS-2, every ut_unit
38 * object contains a pointer to the unit system that was used to create it.
39 *
40 * We use C++ shared pointers to make sure that the system a
41 * Unit instance needs is allocated during the whole life span of
42 * this instance. (De-allocating the unit system too early results in
43 * having a "dangling" pointer.)
44 */
45
46 class System {
47 public:
48 System(const std::string &path = "");
49 typedef std::shared_ptr<System> Ptr;
50 private:
51 friend class Unit;
52 std::shared_ptr<ut_system> m_system;
53 System(const System &);
54 System& operator=(System const &);
55 };
56
57 double convert(System::Ptr system, double input,
58 const std::string &spec1, const std::string &spec2);
59
60 class Unit {
61 public:
62 Unit(System::Ptr system, const std::string &spec);
63 Unit(const Unit &other);
64 ~Unit();
65
66 Unit& operator=(const Unit& other);
67 std::string format() const;
68
69 ut_unit* get() const;
70 System::Ptr get_system() const;
71 private:
72 void reset();
73 ut_unit *m_unit;
74 System::Ptr m_system;
75 std::string m_unit_string;
76 };
77
78 /** Check if units are convertible without creating a converter.
79 *
80 * @param[in] u1 first Unit instance
81 * @param[in] u2 second Unit instance
82 *
83 * @return true if units are convertible, false otherwise
84 */
85 bool are_convertible(const Unit &u1, const Unit &u2);
86
87 /** Unit converter.
88 *
89 * Throws pism::RuntimeError() if the conversion is not possible.
90 *
91 */
92 class Converter {
93 public:
94 Converter();
95 Converter(const Unit &u1, const Unit &u2);
96 Converter(System::Ptr sys, const std::string &u1, const std::string &u2);
97 ~Converter();
98 /** Convert an array of doubles in place
99 *
100 * @param[in,out] data array to process
101 * @param length length of the array
102 */
103 void convert_doubles(double *data, size_t length) const;
104 double operator()(double input) const;
105 private:
106 cv_converter *m_converter;
107 // hide copy constructor and the assignment operator
108 Converter(const Converter &);
109 Converter& operator=(Converter const &);
110 };
111
112 } // end of namespace units
113
114 } // end of namespace pism
115
116 #endif /* _PISMUNITS_H_ */