| 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 | #include <ql/experimental/commodities/unitofmeasure.hpp> |
| 21 | #include <ostream> |
| 22 | #include <utility> |
| 23 | |
| 24 | namespace QuantLib { |
| 25 | |
| 26 | std::ostream& operator<<(std::ostream& out, const UnitOfMeasure& c) { |
| 27 | if (!c.empty()) |
| 28 | return out << c.code(); |
| 29 | else |
| 30 | return out << "null unit of measure" ; |
| 31 | } |
| 32 | |
| 33 | std::map<std::string, |
| 34 | ext::shared_ptr<UnitOfMeasure::Data> > |
| 35 | UnitOfMeasure::unitsOfMeasure_; |
| 36 | |
| 37 | UnitOfMeasure::UnitOfMeasure(const std::string& name, |
| 38 | const std::string& code, |
| 39 | UnitOfMeasure::Type unitType) { |
| 40 | std::map<std::string, |
| 41 | ext::shared_ptr<UnitOfMeasure::Data> >::const_iterator i = |
| 42 | unitsOfMeasure_.find(x: name); |
| 43 | if (i != unitsOfMeasure_.end()) { |
| 44 | data_ = i->second; |
| 45 | } else { |
| 46 | data_ = ext::make_shared<UnitOfMeasure::Data>( |
| 47 | args: name, args: code, args&: unitType); |
| 48 | unitsOfMeasure_[name] = data_; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | UnitOfMeasure::Data::Data(std::string name, |
| 53 | std::string code, |
| 54 | UnitOfMeasure::Type unitType, |
| 55 | UnitOfMeasure triangulationUnitOfMeasure, |
| 56 | const Rounding& rounding) |
| 57 | : name(std::move(name)), code(std::move(code)), unitType(unitType), |
| 58 | triangulationUnitOfMeasure(std::move(triangulationUnitOfMeasure)), rounding(rounding) {} |
| 59 | } |
| 60 | |
| 61 | |