[DEV] update to the generic audio time echrono (std::chrono wrapper)

This commit is contained in:
Edouard DUPIN 2016-10-14 21:25:03 +02:00
parent 0f137bda93
commit 131fb76624
5 changed files with 13 additions and 353 deletions

View File

@ -1,155 +0,0 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <audio/Time.hpp>
#include <audio/Duration.hpp>
#include <audio/debug.hpp>
audio::Duration::Duration(int _val) {
m_data = std::chrono::nanoseconds(_val);
}
audio::Duration::Duration(int64_t _valSec, int64_t _valNano) {
m_data = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds(_valSec));
m_data += std::chrono::nanoseconds(_valNano);
}
audio::Duration::Duration(int64_t _val) {
m_data = std::chrono::nanoseconds(_val);
}
audio::Duration::Duration(double _val) {
m_data = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds(int64_t(_val)));
m_data += std::chrono::nanoseconds(int64_t(((_val - int64_t(_val)) * 1000000000.0)));
}
audio::Duration::Duration(std::chrono::nanoseconds _val) {
m_data = std::chrono::duration_cast<std::chrono::nanoseconds>(_val);
}
audio::Duration::Duration(std::chrono::microseconds _val) {
m_data = std::chrono::duration_cast<std::chrono::nanoseconds>(_val);
}
audio::Duration::Duration(std::chrono::milliseconds _val) {
m_data = std::chrono::duration_cast<std::chrono::nanoseconds>(_val);
}
audio::Duration::Duration(std::chrono::seconds _val) {
m_data = std::chrono::duration_cast<std::chrono::nanoseconds>(_val);
}
int64_t audio::Duration::count() const {
return m_data.count();
}
const audio::Duration& audio::Duration::operator= (const audio::Duration& _obj) {
m_data = _obj.m_data;
return *this;
}
bool audio::Duration::operator== (const audio::Duration& _obj) const {
return _obj.m_data == m_data;
}
bool audio::Duration::operator!= (const audio::Duration& _obj) const {
return _obj.m_data != m_data;
}
bool audio::Duration::operator< (const audio::Duration& _obj) const {
return m_data < _obj.m_data;
}
bool audio::Duration::operator<= (const audio::Duration& _obj) const {
return m_data <= _obj.m_data;
}
bool audio::Duration::operator> (const audio::Duration& _obj) const {
return m_data > _obj.m_data;
}
bool audio::Duration::operator>= (const audio::Duration& _obj) const {
return m_data >= _obj.m_data;
}
const audio::Duration& audio::Duration::operator+= (const audio::Duration& _obj) {
m_data += _obj.m_data;
return *this;
}
audio::Duration audio::Duration::operator+ (const audio::Duration& _obj) const {
audio::Duration tmpp(m_data);
tmpp.m_data += _obj.m_data;
return tmpp;
}
const audio::Duration& audio::Duration::operator-= (const audio::Duration& _obj) {
m_data -= _obj.m_data;
return *this;
}
audio::Duration audio::Duration::operator- (const audio::Duration& _obj) const {
audio::Duration tmpp(m_data);
tmpp.m_data -= _obj.m_data;
return tmpp;
}
audio::Duration& audio::Duration::operator++() {
m_data += std::chrono::nanoseconds(1);
return *this;
}
audio::Duration audio::Duration::operator++(int _unused) {
audio::Duration result(m_data);
m_data += std::chrono::nanoseconds(1);
return result;
}
audio::Duration& audio::Duration::operator--() {
m_data -= std::chrono::nanoseconds(1);
return *this;
}
audio::Duration audio::Duration::operator--(int _unused) {
m_data -= std::chrono::nanoseconds(1);
audio::Duration result(m_data);
return result;
}
std::ostream& audio::operator <<(std::ostream& _os, const audio::Duration& _obj) {
int64_t totalSecond = _obj.count()/1000000000;
int64_t millisecond = (_obj.count()%1000000000)/1000000;
int64_t microsecond = (_obj.count()%1000000)/1000;
int64_t nanosecond = _obj.count()%1000;
//_os << totalSecond << "s " << millisecond << "ms " << microsecond << "µs " << nanosecond << "ns";
int32_t second = totalSecond % 60;
int32_t minute = (totalSecond/60)%60;
int32_t hour = (totalSecond/3600)%24;
int32_t day = (totalSecond/(24*3600))%365;
int32_t year = totalSecond/(24*3600*365);
bool start = false;
if (year != 0) {
start = true;
_os << year << "y ";
}
if (day != 0 || start == true) {
start = true;
_os << day << "d ";
}
if (hour != 0 || start == true) {
start = true;
_os << hour << "h ";
}
if (minute != 0 || start == true) {
start = true;
_os << minute << ":";
}
if (second != 0 || start == true) {
start = true;
_os << second << "s ";
}
if (millisecond != 0 || start == true) {
start = true;
_os << millisecond << "ms ";
}
if (microsecond != 0 || start == true) {
start = true;
_os << microsecond << "us ";
}
_os << nanosecond << "ns ";
return _os;
}

View File

@ -6,43 +6,12 @@
#pragma once
#include <etk/types.hpp>
#include <chrono>
#include <echrono/echrono.hpp>
#include <echrono/Steady.hpp>
#include <echrono/Duration.hpp>
#include <echrono/Time.hpp>
namespace audio {
class Duration {
private:
std::chrono::nanoseconds m_data;
public:
Duration() { };
Duration(int _val); //value in nanosecond
Duration(int64_t _val); //value in nanosecond
Duration(int64_t _valSec, int64_t _valNano); //value in second and nanosecond
Duration(double _val); //value in second
Duration(std::chrono::nanoseconds _val);
Duration(std::chrono::microseconds _val);
Duration(std::chrono::milliseconds _val);
Duration(std::chrono::seconds _val);
~Duration() { };
int64_t count() const;
const std::chrono::nanoseconds& get() const {
return m_data;
}
const Duration& operator= (const Duration& _obj);
bool operator== (const Duration& _obj) const;
bool operator!= (const Duration& _obj) const;
bool operator< (const audio::Duration& _obj) const;
bool operator<= (const audio::Duration& _obj) const;
bool operator> (const audio::Duration& _obj) const;
bool operator>= (const audio::Duration& _obj) const;
const Duration& operator+= (const Duration& _obj);
Duration operator+ (const Duration& _obj) const;
const Duration& operator-= (const Duration& _obj);
Duration operator- (const Duration& _obj) const;
Duration& operator++();
Duration operator++(int _unused);
Duration& operator--();
Duration operator--(int _unused);
};
std::ostream& operator <<(std::ostream& _os, const audio::Duration& _obj);
using Duration=echrono::Duration;
}

View File

@ -3,135 +3,3 @@
* @copyright 2011, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <audio/Time.hpp>
#include <audio/Duration.hpp>
#include <audio/debug.hpp>
audio::Time::Time(int64_t _valSec, int64_t _valNano) {
m_data = std::chrono::steady_clock::time_point(std::chrono::seconds(_valSec));
m_data += std::chrono::nanoseconds(_valNano);
//AUDIO_PRINT("sec= " << _valSec << " nano=" << _valNano << " ==> " << m_data);
}
audio::Time::Time(const std::chrono::steady_clock::time_point& _val) {
m_data = _val;
}
audio::Time audio::Time::now() {
return audio::Time(std::chrono::steady_clock::now());
}
const audio::Time& audio::Time::operator= (const audio::Time& _obj) {
m_data = _obj.m_data;
return *this;
}
bool audio::Time::operator== (const audio::Time& _obj) const {
return m_data == _obj.m_data;
}
bool audio::Time::operator!= (const audio::Time& _obj) const {
return m_data != _obj.m_data;
}
bool audio::Time::operator< (const audio::Time& _obj) const {
return m_data < _obj.m_data;
}
bool audio::Time::operator<= (const audio::Time& _obj) const {
return m_data <= _obj.m_data;
}
bool audio::Time::operator> (const audio::Time& _obj) const {
return m_data > _obj.m_data;
}
bool audio::Time::operator>= (const audio::Time& _obj) const {
return m_data >= _obj.m_data;
}
const audio::Time& audio::Time::operator+= (const audio::Duration& _obj) {
#if defined(__TARGET_OS__MacOs) || defined(__TARGET_OS__IOs)
std::chrono::microseconds ms = std::chrono::duration_cast<std::chrono::microseconds>(_obj.get());
m_data += ms;
#else
m_data += _obj.get();
#endif
return *this;
}
audio::Time audio::Time::operator+ (const audio::Duration& _obj) const {
audio::Time time(m_data);
time += _obj;
return time;
}
const audio::Time& audio::Time::operator-= (const audio::Duration& _obj) {
#if defined(__TARGET_OS__MacOs) || defined(__TARGET_OS__IOs)
std::chrono::microseconds ms = std::chrono::duration_cast<std::chrono::microseconds>(_obj.get());
m_data -= ms;
#else
m_data -= _obj.get();
#endif
return *this;
}
audio::Time audio::Time::operator- (const audio::Duration& _obj) const {
audio::Time time(m_data);
time -= _obj;
return time;
}
audio::Duration audio::Time::operator- (const audio::Time& _obj) const {
std::chrono::nanoseconds ns = std::chrono::duration_cast<std::chrono::nanoseconds>(m_data.time_since_epoch());
std::chrono::nanoseconds ns2 = std::chrono::duration_cast<std::chrono::nanoseconds>(_obj.m_data.time_since_epoch());
audio::Duration duration(ns);
audio::Duration duration2(ns2);
return duration - duration2;
}
std::ostream& audio::operator <<(std::ostream& _os, const audio::Time& _obj) {
std::chrono::nanoseconds ns = std::chrono::duration_cast<std::chrono::nanoseconds>(_obj.get().time_since_epoch());
int64_t totalSecond = ns.count()/1000000000;
int64_t millisecond = (ns.count()%1000000000)/1000000;
int64_t microsecond = (ns.count()%1000000)/1000;
int64_t nanosecond = ns.count()%1000;
//_os << totalSecond << "s " << millisecond << "ms " << microsecond << "µs " << nanosecond << "ns";
int32_t second = totalSecond % 60;
int32_t minute = (totalSecond/60)%60;
int32_t hour = (totalSecond/3600)%24;
int32_t day = (totalSecond/(24*3600))%365;
int32_t year = totalSecond/(24*3600*365);
bool start = false;
if (year != 0) {
start = true;
_os << year << "y ";
}
if (day != 0 || start == true) {
start = true;
_os << day << "d ";
}
if (hour != 0 || start == true) {
start = true;
_os << hour << "h ";
}
if (minute != 0 || start == true) {
start = true;
_os << minute << ":";
}
if (second != 0 || start == true) {
start = true;
_os << second << "s ";
}
if (millisecond != 0 || start == true) {
start = true;
_os << millisecond << "ms ";
}
if (microsecond != 0 || start == true) {
start = true;
_os << microsecond << "us ";
}
_os << nanosecond << "ns ";
return _os;
}

View File

@ -7,36 +7,12 @@
#include <string>
#include <etk/types.hpp>
#include <chrono>
#include <echrono/echrono.hpp>
#include <echrono/Steady.hpp>
#include <echrono/Duration.hpp>
#include <echrono/Time.hpp>
namespace audio {
class Duration;
class Time {
private:
std::chrono::steady_clock::time_point m_data;
public:
Time() {};
//Time(const audio::Duration& _val) {}; //value in second
Time(int64_t _valSec, int64_t _valNano);
Time(const std::chrono::steady_clock::time_point& _val);
~Time() {};
const std::chrono::steady_clock::time_point& get() const {
return m_data;
}
static Time now();
const Time& operator= (const audio::Time& _obj );
bool operator== (const audio::Time& _obj) const;
bool operator!= (const audio::Time& _obj) const;
bool operator< (const audio::Time& _obj) const;
bool operator<= (const audio::Time& _obj) const;
bool operator> (const audio::Time& _obj) const;
bool operator>= (const audio::Time& _obj) const;
const Time& operator+= (const audio::Duration& _obj);
Time operator+ (const audio::Duration& _obj) const;
const Time& operator-= (const audio::Duration& _obj);
Time operator- (const audio::Duration& _obj) const;
Duration operator- (const audio::Time& _obj) const;
};
std::ostream& operator <<(std::ostream& _os, const audio::Time& _obj);
using Time=echrono::Steady;
}

View File

@ -61,7 +61,9 @@ def configure(target, my_module):
'audio/double_t.hpp'
])
my_module.add_depend([
'etk'
'etk',
'elog',
'echrono',
])
my_module.add_path(".")
return True