1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2008 J. Erik Radmall
5  Copyright (C) 2009 StatPro Italia srl
6 
7  This file is part of QuantLib, a free-software/open-source library
8  for financial quantitative analysts and developers - http://quantlib.org/
9 
10  QuantLib is free software: you can redistribute it and/or modify it
11  under the terms of the QuantLib license.  You should have received a
12  copy of the license along with this program; if not, please email
13  <quantlib-dev@lists.sf.net>. The license is also available online at
14  <http://quantlib.org/license.shtml>.
15 
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the license for more details.
19 */
20 
21 /*! \file unitofmeasureconversionmanager.hpp
22     \brief Unit-of-measure conversion manager
23 */
24 
25 #ifndef quantlib_unit_of_measure_conversion_manager_hpp
26 #define quantlib_unit_of_measure_conversion_manager_hpp
27 
28 #include <ql/experimental/commodities/unitofmeasureconversion.hpp>
29 #include <ql/patterns/singleton.hpp>
30 #include <list>
31 
32 namespace QuantLib {
33 
34     //! repository of conversion factors between units of measure
35     /*! \test lookup of direct unit of measure conversion is tested. */
36     class UnitOfMeasureConversionManager
37         : public Singleton<UnitOfMeasureConversionManager> {
38         friend class Singleton<UnitOfMeasureConversionManager>;
39 
40       public:
41         UnitOfMeasureConversion lookup(
42             const CommodityType& commodityType,
43             const UnitOfMeasure&,
44             const UnitOfMeasure&,
45             UnitOfMeasureConversion::Type type =
46                                     UnitOfMeasureConversion::Derived) const;
47         void add(const UnitOfMeasureConversion&);
48         void clear();
49 
50       private:
51         std::list<UnitOfMeasureConversion> data_;
52         UnitOfMeasureConversionManager();
53         void addKnownConversionFactors();
54         UnitOfMeasureConversion directLookup(const CommodityType& commodityType,
55                                              const UnitOfMeasure& source,
56                                              const UnitOfMeasure& target) const;
57         UnitOfMeasureConversion smartLookup(const CommodityType& commodityType,
58                                             const UnitOfMeasure& source,
59                                             const UnitOfMeasure& target,
60                                             std::list<std::string> forbidden =
61                                                 std::list<std::string>()) const;
62     };
63 
64 }
65 
66 #endif
67