#define _CRT_SECURE_NO_WARNINGS
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
#include <ostream>
#include <stdexcept>
#include <climits>
namespace User2
{
template <class I>
class saturate
{
public:
typedef I int_type;
static const int_type nan = int_type(int_type(1) << (sizeof(int_type) * CHAR_BIT - 1));
static const int_type neg_inf = nan + 1;
static const int_type pos_inf = -neg_inf;
private:
int_type i_;
public:
saturate() : i_(nan) {}
explicit saturate(int_type i) : i_(i) {}
operator int_type() const;
saturate& operator+=(saturate x);
saturate& operator-=(saturate x) {return *this += -x;}
saturate& operator*=(saturate x);
saturate& operator/=(saturate x);
saturate& operator%=(saturate x);
saturate operator- () const {return saturate(-i_);}
saturate& operator++() {*this += saturate(int_type(1)); return *this;}
saturate operator++(int) {saturate tmp(*this); ++(*this); return tmp;}
saturate& operator--() {*this -= saturate(int_type(1)); return *this;}
saturate operator--(int) {saturate tmp(*this); --(*this); return tmp;}
friend saturate operator+(saturate x, saturate y) {return x += y;}
friend saturate operator-(saturate x, saturate y) {return x -= y;}
friend saturate operator*(saturate x, saturate y) {return x *= y;}
friend saturate operator/(saturate x, saturate y) {return x /= y;}
friend saturate operator%(saturate x, saturate y) {return x %= y;}
friend bool operator==(saturate x, saturate y)
{
if (x.i_ == nan || y.i_ == nan)
return false;
return x.i_ == y.i_;
}
friend bool operator!=(saturate x, saturate y) {return !(x == y);}
friend bool operator<(saturate x, saturate y)
{
if (x.i_ == nan || y.i_ == nan)
return false;
return x.i_ < y.i_;
}
friend bool operator<=(saturate x, saturate y)
{
if (x.i_ == nan || y.i_ == nan)
return false;
return x.i_ <= y.i_;
}
friend bool operator>(saturate x, saturate y)
{
if (x.i_ == nan || y.i_ == nan)
return false;
return x.i_ > y.i_;
}
friend bool operator>=(saturate x, saturate y)
{
if (x.i_ == nan || y.i_ == nan)
return false;
return x.i_ >= y.i_;
}
friend std::ostream& operator<<(std::ostream& os, saturate s)
{
switch (s.i_)
{
case pos_inf:
return os << "inf";
case nan:
return os << "nan";
case neg_inf:
return os << "-inf";
};
return os << s.i_;
}
};
template <class I>
saturate<I>::operator I() const
{
switch (i_)
{
case nan:
case neg_inf:
case pos_inf:
throw std::out_of_range("saturate special value can not convert to int_type");
}
return i_;
}
template <class I>
saturate<I>&
saturate<I>::operator+=(saturate x)
{
switch (i_)
{
case pos_inf:
switch (x.i_)
{
case neg_inf:
case nan:
i_ = nan;
}
return *this;
case nan:
return *this;
case neg_inf:
switch (x.i_)
{
case pos_inf:
case nan:
i_ = nan;
}
return *this;
}
switch (x.i_)
{
case pos_inf:
case neg_inf:
case nan:
i_ = x.i_;
return *this;
}
if (x.i_ >= 0)
{
if (i_ < pos_inf - x.i_)
i_ += x.i_;
else
i_ = pos_inf;
return *this;
}
if (i_ > neg_inf - x.i_)
i_ += x.i_;
else
i_ = neg_inf;
return *this;
}
template <class I>
saturate<I>&
saturate<I>::operator*=(saturate x)
{
switch (i_)
{
case 0:
switch (x.i_)
{
case pos_inf:
case neg_inf:
case nan:
i_ = nan;
}
return *this;
case pos_inf:
switch (x.i_)
{
case nan:
case 0:
i_ = nan;
return *this;
}
if (x.i_ < 0)
i_ = neg_inf;
return *this;
case nan:
return *this;
case neg_inf:
switch (x.i_)
{
case nan:
case 0:
i_ = nan;
return *this;
}
if (x.i_ < 0)
i_ = pos_inf;
return *this;
}
switch (x.i_)
{
case 0:
i_ = 0;
return *this;
case nan:
i_ = nan;
return *this;
case pos_inf:
if (i_ < 0)
i_ = neg_inf;
else
i_ = pos_inf;
return *this;
case neg_inf:
if (i_ < 0)
i_ = pos_inf;
else
i_ = neg_inf;
return *this;
}
int s = (i_ < 0 ? -1 : 1) * (x.i_ < 0 ? -1 : 1);
i_ = i_ < 0 ? -i_ : i_;
int_type x_i_ = x.i_ < 0 ? -x.i_ : x.i_;
if (i_ <= pos_inf / x_i_)
i_ *= x_i_;
else
i_ = pos_inf;
i_ *= s;
return *this;
}
template <class I>
saturate<I>&
saturate<I>::operator/=(saturate x)
{
switch (x.i_)
{
case pos_inf:
case neg_inf:
switch (i_)
{
case pos_inf:
case neg_inf:
case nan:
i_ = nan;
break;
default:
i_ = 0;
break;
}
return *this;
case nan:
i_ = nan;
return *this;
case 0:
switch (i_)
{
case pos_inf:
case neg_inf:
case nan:
return *this;
case 0:
i_ = nan;
return *this;
}
if (i_ > 0)
i_ = pos_inf;
else
i_ = neg_inf;
return *this;
}
switch (i_)
{
case 0:
case nan:
return *this;
case pos_inf:
case neg_inf:
if (x.i_ < 0)
i_ = -i_;
return *this;
}
i_ /= x.i_;
return *this;
}
template <class I>
saturate<I>&
saturate<I>::operator%=(saturate x)
{
switch (x.i_)
{
case nan:
case neg_inf:
case 0:
case pos_inf:
i_ = nan;
return *this;
}
switch (i_)
{
case neg_inf:
case pos_inf:
i_ = nan;
case nan:
return *this;
}
i_ %= x.i_;
return *this;
}
typedef boost::chrono::duration<saturate<long long>, boost::pico > picoseconds;
typedef boost::chrono::duration<saturate<long long>, boost::nano > nanoseconds;
typedef boost::chrono::duration<saturate<long long>, boost::micro > microseconds;
typedef boost::chrono::duration<saturate<long long>, boost::milli > milliseconds;
typedef boost::chrono::duration<saturate<long long> > seconds;
typedef boost::chrono::duration<saturate<long long>, boost::ratio< 60LL> > minutes;
typedef boost::chrono::duration<saturate<long long>, boost::ratio< 3600LL> > hours;
typedef boost::chrono::duration<saturate<long long>, boost::ratio< 86400LL> > days;
typedef boost::chrono::duration<saturate<long long>, boost::ratio< 31556952LL> > years;
typedef boost::chrono::duration<saturate<long long>, boost::ratio<31556952000LL> > millennium;
}
namespace User2 { namespace detail {
template <class T1, class T2, bool = boost::is_integral<T1>::value>
struct promote_helper;
template <class T1, class T2>
struct promote_helper<T1, saturate<T2>, true> {
typedef typename boost::common_type<T1, T2>::type rep;
typedef User2::saturate<rep> type;
};
template <class T1, class T2>
struct promote_helper<T1, saturate<T2>, false> {
typedef T1 type;
};
} }
namespace boost
{
template <class T1, class T2>
struct common_type<User2::saturate<T1>, User2::saturate<T2> >
{
typedef typename common_type<T1, T2>::type rep;
typedef User2::saturate<rep> type;
};
template <class T1, class T2>
struct common_type<T1, User2::saturate<T2> >
: User2::detail::promote_helper<T1, User2::saturate<T2> > {};
template <class T1, class T2>
struct common_type<User2::saturate<T1>, T2>
: User2::detail::promote_helper<T2, User2::saturate<T1> > {};
namespace chrono {
template <class I>
struct duration_values<User2::saturate<I> >
{
typedef User2::saturate<I> Rep;
public:
static Rep zero() {return Rep(0);}
static Rep max BOOST_PREVENT_MACRO_SUBSTITUTION () {return Rep(Rep::pos_inf-1);}
static Rep min BOOST_PREVENT_MACRO_SUBSTITUTION () {return -(max)();}
};
}
}
#include <iostream>
void testUser2()
{
std::cout << "*************\n";
std::cout << "* testUser2 *\n";
std::cout << "*************\n";
using namespace User2;
typedef seconds::rep sat;
years yr(sat(100));
std::cout << "100 years expressed as years = " << yr.count() << '\n';
nanoseconds ns = yr;
std::cout << "100 years expressed as nanoseconds = " << ns.count() << '\n';
ns += yr;
std::cout << "200 years expressed as nanoseconds = " << ns.count() << '\n';
ns += yr;
std::cout << "300 years expressed as nanoseconds = " << ns.count() << '\n';
std::cout << "yr = ns; // does not compile\n";
std::cout << "ps = yr; // does not compile\n";
ns = yr;
picoseconds ps = ns;
std::cout << "100 years expressed as picoseconds = " << ps.count() << '\n';
ps = ns / sat(1000);
std::cout << "0.1 years expressed as picoseconds = " << ps.count() << '\n';
yr = years(sat(-200000000));
std::cout << "200 million years ago encoded in years: " << yr.count() << '\n';
days d = boost::chrono::duration_cast<days>(yr);
std::cout << "200 million years ago encoded in days: " << d.count() << '\n';
millennium c = boost::chrono::duration_cast<millennium>(yr);
std::cout << "200 million years ago encoded in millennium: " << c.count() << '\n';
std::cout << "Demonstrate \"uninitialized protection\" behavior:\n";
seconds sec;
for (++sec; sec < seconds(sat(10)); ++sec)
;
std::cout << sec.count() << '\n';
std::cout << "\n";
}
void testStdUser()
{
std::cout << "***************\n";
std::cout << "* testStdUser *\n";
std::cout << "***************\n";
using namespace boost::chrono;
hours hr = hours(100);
std::cout << "100 hours expressed as hours = " << hr.count() << '\n';
nanoseconds ns = hr;
std::cout << "100 hours expressed as nanoseconds = " << ns.count() << '\n';
ns += hr;
std::cout << "200 hours expressed as nanoseconds = " << ns.count() << '\n';
ns += hr;
std::cout << "300 hours expressed as nanoseconds = " << ns.count() << '\n';
std::cout << "hr = ns; // does not compile\n";
std::cout << "hr * ns; // does not compile\n";
duration<double> fs(2.5);
std::cout << "duration<double> has count() = " << fs.count() << '\n';
std::cout << "seconds sec = duration<double> won't compile\n";
seconds sec = duration_cast<seconds>(fs);
std::cout << "seconds has count() = " << sec.count() << '\n';
std::cout << "\n";
}
int main()
{
testStdUser();
testUser2();
return 0;
}