| 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 commoditytype.hpp |
| 21 | \brief commodity type |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_commodity_type_hpp |
| 25 | #define quantlib_commodity_type_hpp |
| 26 | |
| 27 | #include <ql/qldefines.hpp> |
| 28 | #include <ql/shared_ptr.hpp> |
| 29 | #include <iosfwd> |
| 30 | #include <map> |
| 31 | #include <string> |
| 32 | #include <utility> |
| 33 | |
| 34 | namespace QuantLib { |
| 35 | |
| 36 | //! commodity type |
| 37 | class CommodityType { |
| 38 | public: |
| 39 | //! default constructor |
| 40 | /*! Instances built via this constructor have undefined |
| 41 | behavior. Such instances can only act as placeholders |
| 42 | and must be reassigned to a valid currency before being |
| 43 | used. |
| 44 | */ |
| 45 | CommodityType() = default; |
| 46 | CommodityType(const std::string& code, const std::string& name); |
| 47 | //! \name Inspectors |
| 48 | //@{ |
| 49 | //! commodity code, e.g, "HO" |
| 50 | const std::string& code() const; |
| 51 | //! name, e.g, "Heating Oil" |
| 52 | const std::string& name() const; |
| 53 | // commodity code |
| 54 | //@} |
| 55 | //! \name Other information |
| 56 | //@{ |
| 57 | //! is this a usable instance? |
| 58 | bool empty() const; |
| 59 | //@} |
| 60 | |
| 61 | protected: |
| 62 | struct Data; |
| 63 | ext::shared_ptr<Data> data_; |
| 64 | |
| 65 | struct Data { |
| 66 | std::string name, code; |
| 67 | |
| 68 | Data(std::string name, std::string code) |
| 69 | : name(std::move(name)), code(std::move(code)) {} |
| 70 | }; |
| 71 | |
| 72 | static std::map<std::string, ext::shared_ptr<Data> > commodityTypes_; |
| 73 | }; |
| 74 | |
| 75 | /*! \relates CommodityType */ |
| 76 | bool operator==(const CommodityType&, |
| 77 | const CommodityType&); |
| 78 | |
| 79 | /*! \relates CommodityType */ |
| 80 | bool operator!=(const CommodityType&, |
| 81 | const CommodityType&); |
| 82 | |
| 83 | /*! \relates CommodityType */ |
| 84 | std::ostream& operator<<(std::ostream&, |
| 85 | const CommodityType&); |
| 86 | |
| 87 | |
| 88 | class NullCommodityType : public CommodityType { |
| 89 | public: |
| 90 | NullCommodityType() : |
| 91 | CommodityType("<NULL>" , "<NULL>" ) {} |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | inline const std::string& CommodityType::code() const { |
| 96 | return data_->code; |
| 97 | } |
| 98 | |
| 99 | inline const std::string& CommodityType::name() const { |
| 100 | return data_->name; |
| 101 | } |
| 102 | |
| 103 | inline bool CommodityType::empty() const { |
| 104 | return !data_; |
| 105 | } |
| 106 | |
| 107 | inline bool operator==(const CommodityType& c1, const CommodityType& c2) { |
| 108 | return c1.code() == c2.code(); |
| 109 | } |
| 110 | |
| 111 | inline bool operator!=(const CommodityType& c1, const CommodityType& c2) { |
| 112 | return !(c1 == c2); |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | |
| 117 | |
| 118 | #endif |
| 119 | |