SourcePro® API Reference Guide

 
List of all members | Public Member Functions | Related Functions
RWMoney< T > Class Template Reference

Represents a specified amount of money in a specified currency. More...

#include <rw/currency/money.h>

Public Member Functions

 RWMoney ()
 
 RWMoney (const RWMoney< T > &m)
 
 RWMoney (const T &amount, const RWCString &currencyMnemonic)
 
 RWMoney (const RWCString &currencyMnemonic)
 
amount () const
 
RWCString currency () const
 
RWMoney< T > & operator*= (const T &rhs)
 
RWMoney< T > & operator+= (const RWMoney< T > &rhs)
 
RWMoney< T > & operator- ()
 
RWMoney< T > & operator-= (const RWMoney< T > &rhs)
 
RWMoney< T > & operator/= (const T &rhs)
 
RWMoney< T > & operator= (const RWMoney< T > &rhs)
 
void restoreFrom (RWvistream &)
 
void restoreFrom (RWFile &)
 
void saveOn (RWvostream &) const
 
void saveOn (RWFile &) const
 
void setAmount (const T &amount)
 
void setCurrency (const RWCString &currencyMnemonic)
 

Related Functions

(Note that these are not member functions.)

template<class T >
bool operator!= (const RWMoney< T > &lhs, const RWMoney< T > &rhs)
 
template<class T >
RWMoney< T > operator* (const RWMoney< T > &lhs, const T &rhs)
 
template<class T >
RWMoney< T > operator* (const T &lhs, const RWMoney< T > &rhs)
 
template<class T >
RWMoney< T > operator+ (const RWMoney< T > &lhs, const RWMoney< T > &rhs)
 
template<class T >
RWMoney< T > operator- (const RWMoney< T > &lhs, const RWMoney< T > &rhs)
 
template<class T >
RWMoney< T > operator/ (const RWMoney< T > &lhs, const T &rhs)
 
template<class T >
bool operator< (const RWMoney< T > &l, const RWMoney< T > &r)
 
template<class T >
RWvostreamoperator<< (RWvostream &strm, const RWMoney< T > &n)
 
template<class T >
RWFileoperator<< (RWFile &f, const RWMoney< T > &n)
 
template<class T >
bool operator<= (const RWMoney< T > &lhs, const RWMoney< T > &rhs)
 
template<class T >
bool operator== (const RWMoney< T > &l, const RWMoney< T > &r)
 
template<class T >
bool operator> (const RWMoney< T > &lhs, const RWMoney< T > &rhs)
 
template<class T >
bool operator>= (const RWMoney< T > &lhs, const RWMoney< T > &rhs)
 
template<class T >
RWvistreamoperator>> (RWvistream &strm, RWMoney< T > &n)
 
template<class T >
RWFileoperator>> (RWFile &f, RWMoney< T > &n)
 
template<class T >
RWMoney< T > round (const RWMoney< T > &x, unsigned int places)
 

Detailed Description

template<class T>
class RWMoney< T >

Class RWMoney provides a mapping between an amount and a currency, by encapsulating a decimal value and referencing a currency. In C++, the value is an arbitrary decimal type specified as a template parameter.

When RWMoney objects are used in arithmetic operations, all operands must be the same currency. If the currencies for RWMoney operands used in the same equation differ, the operator will throw an exception. Similarly, all assignments to RWMoney objects must have the same currency as the original object. If not, an exception is thrown.

Operations between monies of different currencies may be performed using class RWMoneyCalculator.

Synopsis
#include <rw/currency/money.h>
RWMoney<double> m(1.25, "CAD");
Example

Here is an example assignment and some arithmetic calculations:

.
.
.
RWMoney<RWDecimal64> x("1.23","USD"), y("9.87","USD"),
z("7.99","CAD");
RWMoney<RWDecimal64> s("0","USD");
s = x + y; // Okay, same currencies
s = y - z; // Error, different currencies. Exception thrown.
s = x; // Okay, same currencies
s = z; // Error, different currencies. Exception thrown.
.
.
.

Constructor & Destructor Documentation

template<class T>
RWMoney< T >::RWMoney ( )

Default constructor. Behavior is undefined.

template<class T>
RWMoney< T >::RWMoney ( const RWMoney< T > &  m)

Copy constructor.

template<class T>
RWMoney< T >::RWMoney ( const T &  amount,
const RWCString currencyMnemonic 
)

Constructs a money object with the given amount and currency.

template<class T>
RWMoney< T >::RWMoney ( const RWCString currencyMnemonic)

Constructs a money object with the given currency and amount equal to zero.

Member Function Documentation

template<class T >
T RWMoney< T >::amount ( ) const
inline

Returns the amount.

template<class T >
RWCString RWMoney< T >::currency ( ) const
inline

Returns the currency's mnemonic

template<class T >
RWMoney< T > & RWMoney< T >::operator*= ( const T &  rhs)
inline

Sets contents of self to self times rhs.

template<class T >
RWMoney< T > & RWMoney< T >::operator+= ( const RWMoney< T > &  rhs)
inline

Sets contents of self to self plus rhs.

template<class T >
RWMoney< T > & RWMoney< T >::operator- ( )
inline

Returns negation of self. Returned money object will have the same currency as self, but amount will be the negative of self.

template<class T >
RWMoney< T > & RWMoney< T >::operator-= ( const RWMoney< T > &  rhs)
inline

Sets contents of self to self minus rhs.

template<class T >
RWMoney< T > & RWMoney< T >::operator/= ( const T &  rhs)
inline

Sets contents of self to self divided by rhs.

template<class T >
RWMoney< T > & RWMoney< T >::operator= ( const RWMoney< T > &  rhs)
inline

Sets contents of self to rhs.

template<class T>
void RWMoney< T >::restoreFrom ( RWvistream )

Restore from a virtual stream.

template<class T>
void RWMoney< T >::restoreFrom ( RWFile )

Restore from an RWFile.

template<class T>
void RWMoney< T >::saveOn ( RWvostream ) const

Persist to a virtual stream.

template<class T>
void RWMoney< T >::saveOn ( RWFile ) const

Persist to an RWFile.

template<class T >
void RWMoney< T >::setAmount ( const T &  amount)
inline

Sets the amount.

template<class T >
void RWMoney< T >::setCurrency ( const RWCString currencyMnemonic)
inline

Sets the currency mnemonic

Friends And Related Function Documentation

template<class T >
bool operator!= ( const RWMoney< T > &  lhs,
const RWMoney< T > &  rhs 
)
related

Inequality operator.

template<class T >
RWMoney< T > operator* ( const RWMoney< T > &  lhs,
const T &  rhs 
)
related

Multiplication operator.

template<class T >
RWMoney< T > operator* ( const T &  lhs,
const RWMoney< T > &  rhs 
)
related

Multiplication operator.

template<class T >
RWMoney< T > operator+ ( const RWMoney< T > &  lhs,
const RWMoney< T > &  rhs 
)
related

Addition operator.

template<class T >
RWMoney< T > operator- ( const RWMoney< T > &  lhs,
const RWMoney< T > &  rhs 
)
related

Subtraction operator.

template<class T >
RWMoney< T > operator/ ( const RWMoney< T > &  lhs,
const T &  rhs 
)
related

Division operator.

template<class T >
bool operator< ( const RWMoney< T > &  l,
const RWMoney< T > &  r 
)
related

Less than operator.

template<class T >
RWvostream & operator<< ( RWvostream strm,
const RWMoney< T > &  n 
)
related

Saves the object to a virtual stream.

template<class T >
RWFile & operator<< ( RWFile f,
const RWMoney< T > &  n 
)
related

Saves the object to an RWFile.

template<class T >
bool operator<= ( const RWMoney< T > &  lhs,
const RWMoney< T > &  rhs 
)
related

Less than or equal to operator.

template<class T >
bool operator== ( const RWMoney< T > &  l,
const RWMoney< T > &  r 
)
related

Equality operator.

template<class T >
bool operator> ( const RWMoney< T > &  lhs,
const RWMoney< T > &  rhs 
)
related

Greater than operator.

template<class T >
bool operator>= ( const RWMoney< T > &  lhs,
const RWMoney< T > &  rhs 
)
related

Greater than or equal to operator.

template<class T >
RWvistream & operator>> ( RWvistream strm,
RWMoney< T > &  n 
)
related

Restores a object from a virtual stream.

template<class T >
RWFile & operator>> ( RWFile f,
RWMoney< T > &  n 
)
related

Restores a object from an RWFile.

template<class T >
RWMoney< T > round ( const RWMoney< T > &  x,
unsigned int  places 
)
related

Rounds the amount to the indicated number of decimal places. See the description of class RWMoneyCalculator for a discussion of the available rounding methods.

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.