Remove dynamic exceptions
[anna.git] / source / core / util / Microsecond.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <time.h>
10 #include <sys/time.h>
11 #include <stdlib.h>
12
13 #include <anna/core/util/Microsecond.hpp>
14 #include <anna/core/util/Millisecond.hpp>
15 #include <anna/core/util/Second.hpp>
16 #include <anna/core/functions.hpp>
17
18 using namespace std;
19 using namespace anna;
20
21 #define implement_operator(op) \
22    bool Microsecond::operator op (const Millisecond& other) const \
23       \
24    {\
25       return a_value op (((type_t) other.a_value) * 1000);\
26    }\
27    bool Microsecond::operator op (const Second& other) const\
28       \
29    {\
30       return a_value op ((type_t) other.a_value * 1000000);\
31    }
32
33 Microsecond::Microsecond(const Millisecond& other) : a_value(other.a_value) { a_value *= (type_t)1000; }
34
35 Microsecond::Microsecond(const Second& other) : a_value(other.a_value) { a_value *= (type_t)1000000; }
36
37 Microsecond& Microsecond::operator= (const Millisecond & other)
38 {
39   a_value = other.a_value;
40   a_value *= (type_t)1000;
41   return *this;
42 }
43
44 Microsecond& Microsecond::operator= (const Second & other)
45 {
46   a_value = other.a_value;
47   a_value *= (type_t)1000000;
48   return *this;
49 }
50
51 implement_operator( ==)
52 implement_operator( !=)
53 implement_operator( >)
54 implement_operator( <)
55
56
57 //static
58 Microsecond Microsecond::getTime()
59 {
60   struct timeval tv;
61   gettimeofday(&tv, NULL);
62   Microsecond result(Second(tv.tv_sec));
63   result.a_value += tv.tv_usec;
64   return result;
65 }
66
67 string Microsecond::asString() const
68 {
69   string result(functions::asString(a_value));
70   return result += " us";
71 }
72
73 //static
74 Microsecond Microsecond::fromString(const std::string& value)
75 noexcept(false) {
76   if(value.find(" us") == string::npos) {
77     string msg("String: ");
78     msg += value;
79     msg += " | Invalid expression for Microsecond";
80     throw RuntimeException(msg, ANNA_FILE_LOCATION);
81   }
82
83   return Microsecond(atoll(value.c_str()));
84 }