QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
unitofmeasure.hpp
Go to the documentation of this file.
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 <http://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
34namespace QuantLib {
35
36 //! %Unit of measure specification
38 public:
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 UnitOfMeasure() = default;
47 UnitOfMeasure(const std::string& name,
48 const std::string& code,
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
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> >
74 };
75
77 std::string name, code;
81
82 Data(std::string name,
83 std::string code,
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 inline const std::string& UnitOfMeasure::name() const {
106 return data_->name;
107 }
108
109 inline const std::string& UnitOfMeasure::code() const {
110 return data_->code;
111 }
112
114 return data_->unitType;
115 }
116
117 inline const Rounding& UnitOfMeasure::rounding() const {
118 return data_->rounding;
119 }
120
121 inline bool UnitOfMeasure::empty() const {
122 return !data_;
123 }
124
125 inline const UnitOfMeasure&
127 return data_->triangulationUnitOfMeasure;
128 }
129
130 inline bool operator==(const UnitOfMeasure& c1, const UnitOfMeasure& c2) {
131 return c1.code() == c2.code();
132 }
133
134 inline bool operator!=(const UnitOfMeasure& c1, const UnitOfMeasure& c2) {
135 return !(c1 == c2);
136 }
137
139 public:
141 static ext::shared_ptr<Data> data(
142 new Data("Lot", "Lot", UnitOfMeasure::Quantity));
143 data_ = data;
144 }
145 };
146
147}
148
149
150#endif
Amount of a commodity.
Definition: quantity.hpp:34
basic rounding class
Definition: rounding.hpp:35
Unit of measure specification
const UnitOfMeasure & triangulationUnitOfMeasure() const
unit used for triangulation when required
const Rounding & rounding() const
const std::string & code() const
code, e.g, "BBL", "MT"
const std::string & name() const
name, e.g, "Barrels"
bool empty() const
is this a usable instance?
static std::map< std::string, ext::shared_ptr< UnitOfMeasure::Data > > unitsOfMeasure_
Type unitType() const
unit type (mass, volume...)
ext::shared_ptr< Data > data_
UnitOfMeasure()=default
default constructor
Definition: any.hpp:35
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:231
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
bool operator!=(const Currency &c1, const Currency &c2)
Definition: currency.hpp:236
Rounding implementation.
Maps shared_ptr to either the boost or std implementation.
UnitOfMeasure::Type unitType
UnitOfMeasure triangulationUnitOfMeasure
Custom types.