/src/quantlib/ql/experimental/commodities/unitofmeasure.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2008 J. Erik Radmall |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <https://www.quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | /*! \file unitofmeasure.hpp |
21 | | \brief Unit of measure |
22 | | */ |
23 | | |
24 | | #ifndef quantlib_unit_of_measure_hpp |
25 | | #define quantlib_unit_of_measure_hpp |
26 | | |
27 | | #include <ql/types.hpp> |
28 | | #include <ql/math/rounding.hpp> |
29 | | #include <ql/shared_ptr.hpp> |
30 | | #include <string> |
31 | | #include <map> |
32 | | #include <iosfwd> |
33 | | |
34 | | namespace QuantLib { |
35 | | |
36 | | //! %Unit of measure specification |
37 | | class UnitOfMeasure { |
38 | | public: |
39 | | enum Type { Mass, Volume, Energy, Quantity }; |
40 | | //! default constructor |
41 | | /*! Instances built via this constructor have undefined |
42 | | behavior. Such instances can only act as placeholders |
43 | | and must be reassigned to a valid currency before being |
44 | | used. |
45 | | */ |
46 | 0 | UnitOfMeasure() = default; |
47 | | UnitOfMeasure(const std::string& name, |
48 | | const std::string& code, |
49 | | Type unitType); |
50 | | //! \name Inspectors |
51 | | //@{ |
52 | | //! name, e.g, "Barrels" |
53 | | const std::string& name() const; |
54 | | //! code, e.g, "BBL", "MT" |
55 | | const std::string& code() const; |
56 | | //! unit type (mass, volume...) |
57 | | Type unitType() const; |
58 | | //@} |
59 | | //! \name Other information |
60 | | //@{ |
61 | | //! is this a usable instance? |
62 | | bool empty() const; |
63 | | // rounding |
64 | | const Rounding& rounding() const; |
65 | | //! unit used for triangulation when required |
66 | | const UnitOfMeasure& triangulationUnitOfMeasure() const; |
67 | | //@} |
68 | | protected: |
69 | | struct Data; |
70 | | ext::shared_ptr<Data> data_; |
71 | | private: |
72 | | static std::map<std::string, ext::shared_ptr<UnitOfMeasure::Data> > |
73 | | unitsOfMeasure_; |
74 | | }; |
75 | | |
76 | | struct UnitOfMeasure::Data { |
77 | | std::string name, code; |
78 | | UnitOfMeasure::Type unitType; |
79 | | UnitOfMeasure triangulationUnitOfMeasure; |
80 | | Rounding rounding; |
81 | | |
82 | | Data(std::string name, |
83 | | std::string code, |
84 | | UnitOfMeasure::Type unitType, |
85 | | UnitOfMeasure triangulationUnitOfMeasure = UnitOfMeasure(), |
86 | | const Rounding& rounding = Rounding(0)); |
87 | | }; |
88 | | |
89 | | |
90 | | /*! \relates UnitOfMeasure */ |
91 | | bool operator==(const UnitOfMeasure&, |
92 | | const UnitOfMeasure&); |
93 | | |
94 | | /*! \relates UnitOfMeasure */ |
95 | | bool operator!=(const UnitOfMeasure&, |
96 | | const UnitOfMeasure&); |
97 | | |
98 | | /*! \relates UnitOfMeasure */ |
99 | | std::ostream& operator<<(std::ostream&, |
100 | | const UnitOfMeasure&); |
101 | | |
102 | | |
103 | | // inline definitions |
104 | | |
105 | 0 | inline const std::string& UnitOfMeasure::name() const { |
106 | 0 | return data_->name; |
107 | 0 | } |
108 | | |
109 | 0 | inline const std::string& UnitOfMeasure::code() const { |
110 | 0 | return data_->code; |
111 | 0 | } |
112 | | |
113 | 0 | inline UnitOfMeasure::Type UnitOfMeasure::unitType() const { |
114 | 0 | return data_->unitType; |
115 | 0 | } |
116 | | |
117 | 0 | inline const Rounding& UnitOfMeasure::rounding() const { |
118 | 0 | return data_->rounding; |
119 | 0 | } |
120 | | |
121 | 0 | inline bool UnitOfMeasure::empty() const { |
122 | 0 | return !data_; |
123 | 0 | } |
124 | | |
125 | | inline const UnitOfMeasure& |
126 | 0 | UnitOfMeasure::triangulationUnitOfMeasure() const { |
127 | 0 | return data_->triangulationUnitOfMeasure; |
128 | 0 | } |
129 | | |
130 | 0 | inline bool operator==(const UnitOfMeasure& c1, const UnitOfMeasure& c2) { |
131 | 0 | return c1.code() == c2.code(); |
132 | 0 | } |
133 | | |
134 | 0 | inline bool operator!=(const UnitOfMeasure& c1, const UnitOfMeasure& c2) { |
135 | 0 | return !(c1 == c2); |
136 | 0 | } |
137 | | |
138 | | class LotUnitOfMeasure : public UnitOfMeasure { |
139 | | public: |
140 | 0 | LotUnitOfMeasure() { |
141 | 0 | static ext::shared_ptr<Data> data( |
142 | 0 | new Data("Lot", "Lot", UnitOfMeasure::Quantity)); |
143 | 0 | data_ = data; |
144 | 0 | } |
145 | | }; |
146 | | |
147 | | } |
148 | | |
149 | | |
150 | | #endif |