Statistics
| Branch: | Revision:

root / src / common / unitconversion.h @ e1750c09

History | View | Annotate | Download (3.65 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  UNITCONVERSION.H - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//=========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#ifndef _UNITCONVERSION_H_
18
#define _UNITCONVERSION_H_
19
20
#include <vector>
21
#include "commondefs.h"
22
#include "exception.h"
23
24
NAMESPACE_BEGIN
25
26
/**
27
 * Unit conversion utilities. This class has built-in knowledge of some
28
 * physical units (seconds, watts, etc); see internal unitTable[].
29
 */
30
class COMMON_API UnitConversion
31
{
32
  protected:
33
    struct UnitDesc { const char *unit; double mult; const char *baseUnit; const char *longName; };
34
    static UnitDesc unitTable[];
35
36
  protected:
37
    static UnitDesc *lookupUnit(const char *unit);
38
    static bool readNumber(const char *&s, double& number);
39
    static bool readUnit(const char *&s, std::string& unit);
40
41
  private:
42
    // all methods are static, no reason to instantiate
43
    UnitConversion() {}
44
45
  public:
46
    /**
47
     * Invokes parseQuantity(), and converts the result into the given unit.
48
     * If conversion is not possible (unrelated or unknown units), and error
49
     * is thrown.
50
     */
51
    static double parseQuantity(const char *str, const char *expectedUnit=NULL);
52
53
    /**
54
     * Converts a quantity given as string to a double, and returns it, together
55
     * with the unit it was given in. If there are several numbers and units
56
     * (see syntax), everything is converted into the last unit.
57
     *
58
     * Syntax: <number> | (<number> <unit>)+
59
     *
60
     * If there is a syntax error, or if unit mismatch is found (i.e. distance
61
     * is given instead of time), the method throws an exception.
62
     */
63
    static double parseQuantity(const char *str, std::string& outActualUnit);
64
65
    /**
66
     * Parse a quantity and return its unit. This method simply delegates to
67
     * parseQuantity(), and it only exists to make it possible to get the unit
68
     * from Java code.
69
     */
70
    static std::string parseQuantityForUnit(const char *str) {std::string u; parseQuantity(str, u); return u;}
71
72
    /**
73
     * Returns a concatenation of the number and the unit.
74
     */
75
    static std::string formatQuantity(double d, const char *unit=NULL);
76
77
    /**
78
     * Returns a descriptive name of the given unit; in the worst case
79
     * it returns the input string itself in quotes.
80
     */
81
    static std::string getUnitDescription(const char *unit);
82
83
    /**
84
     * Returns 0.0 if conversion is not possible (unrelated or unrecognized units).
85
     */
86
    static double getConversionFactor(const char *sourceUnit, const char *targetUnit);
87
88
    /**
89
     * Converts the given value with unit into the given target unit.
90
     * Throws an error if the conversion is not possible.
91
     */
92
    static double convertUnit(double d, const char *unit, const char *targetUnit);
93
94
    /**
95
     * Returns the long name for the given unit, or NULL if it is unrecognized.
96
     * See getAllUnits().
97
     */
98
    static const char *getLongName(const char *unit);
99
100
    /**
101
     * Returns the base unit for the given unit, or NULL if the unit is unrecognized.
102
     * See getAllUnits().
103
     */
104
    static const char *getBaseUnit(const char *unit);
105
106
    /**
107
     * Produces the list of all recognized units, with their short names.
108
     */
109
    static std::vector<const char *> getAllUnits();
110
};
111
112
NAMESPACE_END
113
114
#endif