money.h

Go to the documentation of this file.
00001 // OSB library ********************************************* -*- C++ -*-
00009 /*
00010   AUTHOR(S): Vipul Gupta (vg)
00011              Stephan Broennimann (vb)
00012 
00013   RCS information
00014    $Name: OSB_060808 $
00015    $Revision: 1.46 $
00016 
00017   License
00018    OSB rating and billing library for communication networks
00019    Copyright (C) 2004, 2005, 2006  OSB systems
00020 
00021    This file may be distributed and/or modify under the terms of the
00022    GNU General Public License (GPL) as published by the Free Software
00023    Foundation which is provided in the file LICENSE.GPL included in the
00024    packaging of this file.
00025 
00026    The file is distributed in the hope that it will be useful, but WITHOUT
00027    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00028    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00029    for more details.
00030 
00031    Holders of a OSB Commercial License may use this file under the terms
00032    and conditions of this commercial license.
00033  */
00034 #ifndef _MONEY_H_
00035 #define _MONEY_H_
00036 
00037 // *********************************************************************
00038 // included header files
00039 // + standard includes
00040 #include <string>
00041 #include <utility>
00042 #include <map>
00043 #include <iosfwd>
00044 
00045 // + local headers
00046 #include "decimal.h"
00047 #include "osbid.h"
00048 
00049 // *********************************************************************
00050 // class declarations
00051 namespace OSB_DB {
00052     class CurrencyBookGw;
00053     class Session;
00054 }
00055 
00056 // *********************************************************************
00057 // namespace extensions
00058 namespace OSB_LIB {
00059 
00060 // *********************************************************************
00061 // type definitions
00062 
00073     class Currency
00074     {
00076         friend class OSB_DB::CurrencyBookGw;
00077     public:
00079         typedef std::string Oid;
00080     public:
00086         explicit Currency(const Oid& oid = Oid());
00087 
00089 
00090 
00091         Oid oid() const { return oid_; }
00093         std::string baseName() const { return baseName_; }
00095         std::string fractionName() const { return fractionName_; }
00097         std::string baseSymbol() const { return baseSymbol_; }
00099         std::string fractionSymbol() const { return fractionSymbol_; }
00101 
00102         bool match(const Oid& oid) const { return oid_ == oid; }
00103 
00104     private:
00106         Oid oid_;
00108         std::string baseName_;
00110         std::string baseSymbol_;
00112         std::string fractionName_;
00114         std::string fractionSymbol_;
00115     };                                  // class Currency
00116 
00118     std::ostream& operator<<(std::ostream& os, const Currency& c);
00119 
00126     class CurrencyBook {
00127     public:
00134         typedef std::map<Currency::Oid, Currency> CurrencyList;
00135     public:
00137         CurrencyBook() {}
00138 
00140 
00141 
00142         typedef CurrencyList::iterator Iterator;
00144         typedef CurrencyList::const_iterator ConstIterator;
00146         typedef CurrencyList::size_type SizeType;
00148 
00157 
00158         Iterator begin() { return currencyList_.begin(); }
00160         Iterator end() { return currencyList_.end(); }
00162         ConstIterator begin() const { return currencyList_.begin(); }
00164         ConstIterator end() const { return currencyList_.end(); }
00166         SizeType size() const { return currencyList_.size(); }
00168 
00175         long read(const OSB_DB::Session& session);
00186         void add(const Currency& currency, bool isBase = false);
00188         void print(std::ostream& os);
00195         const Currency* findCurrency(const Currency::Oid& oid) const;
00203         const Currency& getCurrency(const Currency::Oid& oid) const;
00205         void clear();
00207         void swap(CurrencyBook&);
00209         const Currency::Oid& baseCurrency() const;
00211         void setBaseCurrency(const Currency::Oid& newBase);
00212 
00213     private:
00215         CurrencyList currencyList_;
00217         Currency::Oid baseCurrency_;
00218     };
00219 
00220     // *****************************************************************
00221     // Money
00227     class Money
00228     {
00229         friend std::ostream& operator<<(std::ostream&, const Money&);
00230 
00231     public:
00250         enum ConversionMethod { noConversion, baseCurrency, automatic };
00251 
00264         Money() {}
00266         Money(const Currency::Oid& cc, const Decimal& d)
00267             : currency_(cc), amount_(d) {}
00268 
00269         // Copy, destructor and assignment by compiler.
00270 
00272 
00273 
00274         const Currency::Oid& currency() const
00275             { return currency_; }
00277         Decimal amount() const
00278             { return amount_; }
00280         static ConversionMethod conversionMethod()
00281             { return conversionMethod_; }
00283 
00285         static ConversionMethod conversionMethod(ConversionMethod);
00286 
00288 
00289 
00290         Money& operator+=(const Money&);
00292         Money& operator-=(const Money&);
00294         Money& operator*=(double);
00296         Money& operator*=(const Decimal&);
00298         Money& operator/=(double);
00300         Money& operator/=(const Decimal&);
00302         Money& operator-();
00304 
00305     private:
00307         Currency::Oid currency_;
00309         Decimal amount_;
00311         static ConversionMethod conversionMethod_;
00312     };
00313 
00315     typedef std::map<Currency::Oid, Money> MoneyList;
00316 
00318 
00319 
00320     void add(MoneyList& ml, const Money& m);
00322     void add(MoneyList& ml, const MoneyList& m);
00324     void subtract(MoneyList& ml, const Money& m);
00326     void subtract(MoneyList& ml, const MoneyList& m);
00328 
00329     // ToDo: Implement conversion / exchange rate managers, etc.
00330     // See the OMG Currency Specification (formal/00-06-29) and
00331     // Vipul's IA report for details
00332 
00338     void convert(Money&, Money&);
00339 
00341 
00342 
00343     inline Money operator*(const Money&, double);
00345     inline Money operator*(const Money&, const Decimal&);
00347     inline Money operator/(const Money&, double);
00349     inline Money operator/(const Money&, const Decimal&);
00351     inline Money operator+(const Money&, const Money&);
00353     inline Money operator-(const Money&, const Money&);
00355     bool operator==(const Money&, const Money&);
00357     bool operator!=(const Money&, const Money&);
00359     bool operator<(const Money&, const Money&);
00361     bool operator>(const Money&, const Money&);
00363     bool operator<=(const Money&, const Money&);
00365     bool operator>=(const Money&, const Money&);
00367 
00369     std::ostream& operator<<(std::ostream& out, const Money& m);
00370 
00371 // *********************************************************************
00372 // inline definitions
00373 
00374     inline Money operator+(const Money& m1, const Money& m2)
00375     {
00376         Money m3(m1);
00377         m3 += m2;
00378         return m3;
00379     }
00380 
00381     inline Money operator-(const Money& m1, const Money& m2)
00382     {
00383         Money m3(m1);
00384         m3 -= m2;
00385         return m3;
00386     }
00387 
00388     inline Money& Money::operator*=(double d)
00389     {
00390         amount_ *= d;
00391         return *this;
00392     }
00393 
00394     inline Money operator*(const Money& m, double d)
00395     {
00396         Money r(m);
00397         return r *= d;
00398     }
00399 
00400     inline Money& Money::operator*=(const Decimal& d)
00401     {
00402         amount_ *= d;
00403         return *this;
00404     }
00405 
00406     inline Money operator*(const Money& m, const Decimal& d)
00407     {
00408         Money r(m);
00409         return r *= d;
00410     }
00411 
00412     inline Money& Money::operator/=(double d)
00413     {
00414         amount_ /= d;
00415         return *this;
00416     }
00417 
00418     inline Money operator/(const Money& m, double d)
00419     {
00420         Money r(m);
00421         return r /= d;
00422     }
00423 
00424     inline Money& Money::operator/=(const Decimal& d)
00425     {
00426         amount_ /= d;
00427         return *this;
00428     }
00429 
00430     inline Money operator/(const Money& m, const Decimal& d)
00431     {
00432         Money r(m);
00433         return r /= d;
00434     }
00435 }                                       // namespace OSB_LIB
00436 #endif                                  // #ifndef _MONEY_H_

Generated on Sat Sep 2 14:06:33 2006 for OSB Library by  doxygen 1.4.7