competency.cpp 3.1 KB
Newer Older
1 2
#include <iostream>

Alexis Lebis's avatar
Alexis Lebis committed
3 4
#include "competency.h"
#include "magnitude.h"
5
#include "tools.h"
6

Alexis Lebis's avatar
Alexis Lebis committed
7
#include "exception/magnitudeException.h"
8
#include "exception/competencyEvolvingException.h"
9
#include "exception/idOverflowException.h"
10 11

int Competency::COMPETENCY_COUNTER = 0;
12
int Competency::COMPETENCY_TMP_COUNTER = ID_RANGE_FOR_OBJECT + 1;
Alexis Lebis's avatar
Alexis Lebis committed
13 14 15

// === FACTORY

16
Competency Competency::build(Magnitude & m, std::string name)
Alexis Lebis's avatar
Alexis Lebis committed
17 18 19
{
    int id = Competency::assignID();
    if(name.empty())
20
        name = "Competency#"+std::to_string(id);
Alexis Lebis's avatar
Alexis Lebis committed
21 22 23 24
    
    return Competency(id, m, name);
}

25
Competency Competency::build(double d = 0, std::string name)
Alexis Lebis's avatar
Alexis Lebis committed
26 27 28
{
    int id = Competency::assignID();
    if(name.empty())
29
        name = "Competency#"+std::to_string(id);
Alexis Lebis's avatar
Alexis Lebis committed
30 31 32 33 34 35 36 37 38

    try
    {
        Magnitude m = Magnitude::build(d);
        return Competency(id, m, name); 
    }
    catch(MagnitudeException & e)
    {
        e.getMagnitude().rebase();
39
        throw CompetencyEvolvingException(new Competency(id, e.getMagnitude(), name)); 
Alexis Lebis's avatar
Alexis Lebis committed
40 41 42
    }  
}

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
Competency Competency::buildTMP(double d, std::string name)
{
    int id = Competency::assignID4TMP();
    if(name.empty())
        name = "Competency#"+std::to_string(id);

    try
    {
        Magnitude m = Magnitude::build(d);
        return Competency(id, m, name); 
    }
    catch(MagnitudeException & e)
    {
        e.getMagnitude().rebase();
        throw CompetencyEvolvingException(new Competency(id, e.getMagnitude(), name)); 
    }  
}

Alexis Lebis's avatar
Alexis Lebis committed
61 62 63 64 65 66 67 68 69 70 71 72
// === CONSTRUCTOR

Competency::Competency(int id, Magnitude m, std::string s)
    : _id(id), _m(m), _name(s)
{

}

// === STATIC

int Competency::assignID()
{
73 74
    if(Competency::COMPETENCY_COUNTER + 1 > ID_RANGE_FOR_OBJECT)
        throw idOverflowException("assignID()@Competency.cpp");
Alexis Lebis's avatar
Alexis Lebis committed
75 76
    return ++Competency::COMPETENCY_COUNTER;
}
77 78 79 80 81 82 83 84 85 86

int Competency::assignID4TMP()
{
    if(Competency::COMPETENCY_TMP_COUNTER + 1 > ID_RANGE_FOR_TEMPORARY_OBJECT)
    {
        std::cout << "INFO: COMPETENCY_TMP_COUNTER was about to overflow: restored to ID_RANGE_OBJECT + 1" << std::endl;
        COMPETENCY_TMP_COUNTER = ID_RANGE_FOR_OBJECT + 1;
    }
    return ++Competency::COMPETENCY_TMP_COUNTER;
}
Alexis Lebis's avatar
Alexis Lebis committed
87 88 89 90 91

// === FUNCTION

void Competency::evolveTowards(Magnitude & m)
{
92 93 94 95 96 97 98 99 100 101
    try
    {
        this->_m+= m;
    }
    catch(MagnitudeException & e)
    {
        e.getMagnitude().rebase();
        this->_m = e.getMagnitude();
        throw CompetencyEvolvingException(this);
    }
Alexis Lebis's avatar
Alexis Lebis committed
102 103 104 105
}

void Competency::evolveTowards(double d)
{
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    try
    {
        this->_m += d;
    }
    catch(MagnitudeException & e)
    {
        e.getMagnitude().rebase();
        this->_m = e.getMagnitude();
        throw CompetencyEvolvingException(this);
    }
}

const double Competency::competencyValue() const
{
    return this->_m.value();
}
Alexis Lebis's avatar
Alexis Lebis committed
122

123 124 125 126 127 128
// === OPERATOR
std::ostream& operator<<(std::ostream& Stream, const Competency & c) 
{ 
    std::string s = "Competency\n\tid:"+std::to_string(c.id())+"\n\tname:"+c.c_name()+"\n\tvalue:"+std::to_string(c.competencyValue());
    Stream << s ; 
    return Stream; 
129 130 131 132 133
}

bool Competency::operator==(const Competency & c) const
{
    return ( this->_id == c.id() || ( this->_name.compare(c.c_name()) == 0 ) );
Alexis Lebis's avatar
Alexis Lebis committed
134
}