Commit 4579ad4e authored by Alexis Lebis's avatar Alexis Lebis

some redef of mag operator. Warning : rebase does not save modif (check @ mainl27)

parent a45af4aa
......@@ -6,12 +6,29 @@
#include <queenEval.h>
#include <model/magnitude.h>
#include <model/exception/magnitudeException.h>
int main(int argc, char* argv[]){
Magnitude * m = Magnitude::build(-10.0);
Magnitude m = Magnitude::build(0.5);
Magnitude n = Magnitude::build(0.3);
std::cout << "Magnitude" << m.value() << std::endl;
m += n;
std::cout << "New magnitude is " << m.value() << std::endl;
try{
std::cout << "After addition mag is : " << (m + m).value() << std::endl;
}
catch(MagnitudeException e)
{
e.getMagnitude().rebase();
std::cout << "REBASE! New magnitude value is" << e.getMagnitude().rebase() << std::endl;
std::cout << "Accessing magnitude value : " << e.getMagnitude().value() << std::endl;
}
std::cout << "Magnitude" << m->value() << std::endl;
//Define a QUEEN -> 1 line
QUEEN s1;
......
......@@ -4,17 +4,19 @@
#include <exception>
#include <string>
#include "../magnitude.h"
class MagnitudeException : public std::exception
{
private:
double _triedValue;
Magnitude _triedValue;
std::string _phrase;
public:
MagnitudeException(double v) throw()
: _triedValue(v)
MagnitudeException(Magnitude & m) throw()
: _triedValue(m)
{
this->_phrase = "Magnitude Exception: value (" + std::to_string(v) + ") is out of bound [0;1].";
this->_phrase = "Magnitude Exception: value (" + std::to_string(m.value()) + ") is out of bound [0;1].";
}
virtual const char* what() const throw()
......@@ -24,6 +26,8 @@ class MagnitudeException : public std::exception
virtual ~MagnitudeException() throw()
{}
Magnitude getMagnitude() const {return this->_triedValue;}
};
#endif // SRC_EXCEPTION_MAGNITUDE_EXCEPTION_H_
\ No newline at end of file
......@@ -2,14 +2,17 @@
#include "exception/magnitudeException.h"
// === FACTORY
Magnitude* Magnitude::build(double value = 0)
Magnitude Magnitude::build(double value = 0)
{
bool isInRange = !(_inRange(value));
if( !isInRange )
throw MagnitudeException(value);
{
Magnitude mag(value);
throw MagnitudeException(mag);
}
else
return new Magnitude(value);
return Magnitude(value);
}
// === CONSTRUCTOR
......@@ -23,15 +26,52 @@ Magnitude::Magnitude(double d)
this->_value=d;
}
Magnitude::Magnitude(const Magnitude & m)
{
this->_value = m._value;
}
// === OPERATOR
Magnitude & Magnitude::operator+=(const double d)
{
this->_value+=d;
if( Magnitude::_inRange(this->_value) )
throw MagnitudeException(*this);
return *this;
}
Magnitude & Magnitude::operator+=(const Magnitude & m)
{
this->_value += m._value;
if( Magnitude::_inRange(this->_value) )
throw MagnitudeException(*this);
return *this;
}
Magnitude operator+(const Magnitude & m, const Magnitude & n)
{
Magnitude mag(m);
mag+= n.value();
return mag;
}
Magnitude operator+(const Magnitude & m, const double d)
{
Magnitude mag(m);
mag += d;
return mag;
}
// === FUNCTION
/** Checks if the value v is in [0;1]. If yes, return 0. Else, if v < 0 return -1, 1 otherwise
*/
const int Magnitude::_inRange(double v)
int Magnitude::_inRange(double v)
{
return v < 0 ? -1 : v > 1 ? 1 : 0;
}
const double Magnitude::value(){return this->_value;}
double Magnitude::value() const {return this->_value;}
/** Set the magnitude value of a competency. Indicates whether or not there is an overflow in
* the given v value (and that it has been automatically corrected)
......@@ -56,3 +96,12 @@ bool Magnitude::set(double v)
return !isInRange;
}
double Magnitude::rebase()
{
if(this->_value < 0)
this->_value = 0;
else if(this->_value > 1)
this->_value = 1;
return this->_value;
}
\ No newline at end of file
......@@ -15,16 +15,27 @@ class Magnitude
Magnitude(double);
// function
static const int _inRange(double);
static int _inRange(double);
public:
static Magnitude* build(double); //factory
static Magnitude build(double); //factory
Magnitude(const Magnitude & m);
double rebase(); /// if value is out of range, assign it the nearest value. Usefull when a MagnitudeException is caught
// Operator
Magnitude & operator+=(const Magnitude & m);
Magnitude & operator+=(double const d);
//GETTER
const double value();
double value() const;
//SETTER
bool set(double v);
};
Magnitude operator+(const Magnitude & m1, const Magnitude & m2);
Magnitude operator+(const Magnitude & m1, double d);
#endif // SRC_MAGNITUDE_H_
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment