Line data Source code
1 : #ifndef _ATM_PERCENT_H 2 : #define _ATM_PERCENT_H 3 : /******************************************************************************* 4 : * ALMA - Atacama Large Millimiter Array 5 : * (c) Instituto de Estructura de la Materia, 2009 6 : * 7 : * This library is free software; you can redistribute it and/or 8 : * modify it under the terms of the GNU Lesser General Public 9 : * License as published by the Free Software Foundation; either 10 : * version 2.1 of the License, or (at your option) any later version. 11 : * 12 : * This library is distributed in the hope that it will be useful, 13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 : * Lesser General Public License for more details. 16 : * 17 : * You should have received a copy of the GNU Lesser General Public 18 : * License along with this library; if not, write to the Free Software 19 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 : * 21 : * "@(#) $Id: ATMPercent.h Exp $" 22 : * 23 : * who when what 24 : * -------- -------- ---------------------------------------------- 25 : * pardo 2009-03-24 created 26 : */ 27 : 28 : #ifndef __cplusplus 29 : #error "This is a C++ include file and cannot be used from plain C" 30 : #endif 31 : 32 : #include "ATMCommon.h" 33 : #include <string> 34 : 35 : 36 : 37 : ATM_NAMESPACE_BEGIN 38 : 39 : /*! \brief Defines a class for quantities that may be expressed in percent. 40 : * 41 : * This class is aimed at quantities that may be defined in percent. Default is proposed with no units 42 : * (relative to one), and an alternative is considered for units in %. 43 : */ 44 : class Percent 45 : { 46 : public: 47 : 48 : enum Units { 49 : UnitPercent, 50 : NumPercentUnits 51 : }; 52 : 53 : /** Default constructor */ 54 : Percent(); 55 : /** A full constructor: Amount in default units (relative to 1) */ 56 : Percent(double percent); 57 : /** A full constructor: Amount + units. Valid units are: % */ 58 : Percent(double percent, const std::string &units); 59 : 60 : Percent(double percent, Units units); 61 : 62 : ~Percent(); 63 : 64 : //@{ 65 : /** Accessor to the percent value in International System units */ 66 : double get()const; 67 : /** Accessor to the percent value in specified units. */ 68 : double get(const std::string &units)const; 69 : double get(Units units) const; 70 : //@} 71 : 72 : /** Operator "equal to a Percent" */ 73 147 : inline Percent& operator=(const Percent &rhs) { if(&rhs != this) valueIS_ = rhs.valueIS_; return *this; } 74 : /** Operator "equal to a double converted to Percent in %" */ 75 : inline Percent& operator=(double rhs) { valueIS_ = rhs; return *this; } 76 : /** Operator "addition of percentages" */ 77 : inline Percent operator+(const Percent &rhs) { return Percent(valueIS_ + rhs.get()); } 78 : /** Operator "substraction of percentages" */ 79 : inline Percent operator-(const Percent &rhs) { return Percent(valueIS_ - rhs.get()); } 80 : /** Operator "multiplication of a percent by a double" */ 81 : inline Percent operator*(double scf) { return Percent(valueIS_ * scf); } 82 : /** Operator "multiplication of a percent by a float" */ 83 : inline Percent operator*(float scf) { return Percent(valueIS_ * (double) scf); } 84 : /** Operator "multiplication of a percent by an int" */ 85 : inline Percent operator*(int scf) { return Percent(valueIS_ * (double) scf); } 86 : /** Operator "multiplication of a percent by an unsigned int" */ 87 : inline Percent operator*(unsigned int scf) { return Percent(valueIS_ * (double) scf); } 88 : /** Operator "division of a percent by a double" */ 89 : inline Percent operator/(double scf) { return Percent(valueIS_ / scf); } 90 : /** Operator "division of a percent by a float" */ 91 : inline Percent operator/(float scf) { return Percent(valueIS_ / (double) scf); } 92 : /** Operator "division of a percent by an int" */ 93 : inline Percent operator/(int scf) { return Percent(valueIS_ / (double) scf); } 94 : /** Operator "division of a percent by an unsigned int" */ 95 : inline Percent operator/(unsigned int scf) { return Percent(valueIS_ / (double) scf); } 96 : /** Operator "comparator < for two percentages" */ 97 : inline bool operator<(const Percent &rhs) const { return (valueIS_ < rhs.get()); } 98 : /** Operator "comparator > for two percentages" */ 99 : inline bool operator>(const Percent &rhs) const { return (valueIS_ > rhs.get()); } 100 : /** Operator "comparator <= for two percentages" */ 101 : inline bool operator<=(const Percent &rhs) const { return (valueIS_ <= rhs.get()); } 102 : /** Operator "comparator >= for two percentages" */ 103 : inline bool operator>=(const Percent &rhs) const { return (valueIS_ >= rhs.get()); } 104 : /** Operator "comparator == for two percentages" */ 105 : inline bool operator==(const Percent &rhs) const { return (valueIS_ == rhs.get()); } 106 : /** Operator "comparator != for two percentages" */ 107 : inline bool operator!=(const Percent &rhs) const { return (valueIS_ != rhs.get()); } 108 : 109 : private: 110 : double valueIS_; 111 : }; // class Percent 112 : 113 : ATM_NAMESPACE_END 114 : 115 : #endif /*!_ATM_PERCENT_H*/ 116 : 117 :