From 3c1804898a17a070c89a02ac59f474e1fee7c6fa Mon Sep 17 00:00:00 2001 From: yumetodo Date: Wed, 23 Mar 2016 01:30:29 +0900 Subject: [PATCH] add arithmetic like operators to bel_c class --- dxlibex/basic_types/bel.hpp | 208 +++++++++++++++++++++++-- samples/sound_play/sound_play/main.cpp | 4 +- 2 files changed, 197 insertions(+), 15 deletions(-) diff --git a/dxlibex/basic_types/bel.hpp b/dxlibex/basic_types/bel.hpp index 430678f..19f4a01 100644 --- a/dxlibex/basic_types/bel.hpp +++ b/dxlibex/basic_types/bel.hpp @@ -14,6 +14,7 @@ Distributed under the Boost Software License, Version 1.0. #include "dxlibex/type_traits/is_ratio.hpp" #include "dxlibex/type_traits/enable_if.hpp" #include +#include namespace dxle { typedef std::ratio<1, 10000> myrio; namespace sound_units { @@ -22,6 +23,14 @@ namespace dxle { class bel_c; template::value, nullptr_t> = nullptr> inline DXLE_CONSTEXPR To bel_cast(const bel_c& o); + + /// bel_c_values + template struct bel_c_values{ + static DXLE_CONSTEXPR _Rep zero(){ return _Rep(0); } + static DXLE_CONSTEXPR _Rep max(){ return std::numeric_limits<_Rep>::max();} + static DXLE_CONSTEXPR _Rep min(){ return std::numeric_limits<_Rep>::lowest(); } + }; + template::value, nullptr_t>> class bel_c { public: @@ -53,6 +62,67 @@ namespace dxle { DXLE_CONSTEXPR value_type count() const { return value_; } DXLE_CONSTEXPR value_type get() const { return value_; } DXLE_CONSTEXPR value_type operator*() const { return value_; } + DXLE_CONSTEXPR bel_c operator+() const { return *this; } + DXLE_CONSTEXPR bel_c operator-() const { return bel_c(-value_); } + bel_c& operator++() + { + ++value_; + return *this; + } + + bel_c operator++(int) { return bel_c(value_++); } + + bel_c& operator--() + { + --value_; + return *this; + } + + bel_c operator--(int) { return bel_c(value_--); } + + bel_c& operator+=(const bel_c& o) + { + value_ += o.count(); + return *this; + } + + bel_c& operator-=(const bel_c& o) + { + value_ -= o.count(); + return *this; + } + + bel_c& operator*=(const T& r) + { + value_ *= r; + return *this; + } + + bel_c& operator/=(const T& r) + { + value_ /= r; + return *this; + } + + // DR 934. + template::value, std::nullptr_t> = nullptr> + bel_c& operator%=(const T& r) + { + value_ %= r; + return *this; + } + + template::value, std::nullptr_t> = nullptr> + bel_c& operator%=(const bel_c& o) + { + value_ %= o.count(); + return *this; + } + + //special values + static DXLE_CONSTEXPR bel_c zero() { return bel_c(bel_c_values::zero()); } + static DXLE_CONSTEXPR bel_c min() { return bel_c(bel_c_values::min()); } + static DXLE_CONSTEXPR bel_c max() { return bel_c(bel_c_values::max()); } }; template struct is_bel_c : std::false_type {}; template struct is_bel_c> : std::true_type{}; @@ -85,27 +155,137 @@ namespace dxle { } template::value, nullptr_t>> inline DXLE_CONSTEXPR To bel_cast(const bel_c& o) - { // convert duration to another duration + { // convert bel_c to another bel_c typedef std::ratio_divide CF; typedef typename To::value_type ToT; typedef std::common_type_t CR; return detail::bel_cast_helper::cast(o); - //return (CF::num == 1 && CF::den == 1 - //? To(static_cast(_Dur.count())) - //: CF::num != 1 && CF::den == 1 - //? To(static_cast( - // static_cast( - // _Dur.count()) * static_cast(CF::num))) - //: CF::num == 1 && CF::den != 1 - //? To(static_cast( - // static_cast(_Dur.count()) - // / static_cast(CF::den))) - //: To(static_cast( - // static_cast(_Dur.count()) * static_cast(CF::num) - // / static_cast(CF::den)))); } + template + constexpr common_type_t, bel_c> + operator+(const bel_c& l, const bel_c& r) + { + typedef bel_c bel_c1; + typedef bel_c bel_c2; + typedef common_type_t result_t; + return result_t(result_t(l).count() + result_t(r).count()); + } + + template + constexpr common_type_t, bel_c> + operator-(const bel_c& l, const bel_c& r) + { + typedef bel_c bel_c1; + typedef bel_c bel_c2; + typedef common_type_t result_t; + return result_t(result_t(l).count() - result_t(r).count()); + } + + namespace detail { + template + struct common_rep_type_impl : enable_if::value, Result> {}; + template + struct common_rep_type : common_rep_type_impl> {}; + template + using common_rep_type_t = typename common_rep_type::type; + } + + template + constexpr bel_c, _Period> + operator*(const bel_c& b, const T2& r) + { + typedef bel_c, _Period> result_t; + return result_t(result_t(b).count() * r); + } + + template + constexpr bel_c, _Period> + operator*(const T1& r, const bel_c& b) + { + return b * r; + } + + template + constexpr bel_c::value, T2>>, _Period> + operator/(const bel_c& b, const T2& r) + { + typedef bel_c, _Period> result_t; + return result_t(result_t(b).count() / r); + } + + template + constexpr common_type_t + operator/(const bel_c& l, const bel_c& r) + { + typedef bel_c bel_c1; + typedef bel_c bel_c2; + typedef common_type_t result_t; + return result_t(l).count() / result_t(r).count(); + } + + // DR 934. + template + constexpr bel_c::value, T2>>, _Period> + operator%(const bel_c& b, const T2& r) + { + typedef bel_c, _Period> result_t; + return result_t(result_t(b).count() % r); + } + + template + constexpr common_type_t, bel_c> + operator%(const bel_c& l, const bel_c& r) + { + typedef bel_c bel_c1; + typedef bel_c bel_c2; + typedef common_type_t result_t; + return result_t(result_t(l).count() % result_t(r).count()); + } + + // comparisons + template + constexpr bool operator==(const bel_c& l, const bel_c& r) + { + typedef bel_c bel_c1; + typedef bel_c bel_c2; + typedef common_type_t __ct; + return __ct(l).count() == __ct(r).count(); + } + + template + constexpr bool operator<(const bel_c& l, const bel_c& r) + { + typedef bel_c bel_c1; + typedef bel_c bel_c2; + typedef common_type_t __ct; + return __ct(l).count() < __ct(r).count(); + } + + template + constexpr bool operator!=(const bel_c& l, const bel_c& r) + { + return !(l == r); + } + + template + constexpr bool operator<=(const bel_c& l, const bel_c& r) + { + return !(r < l); + } + + template + constexpr bool operator>(const bel_c& l, const bel_c& r) + { + return r < l; + } + + template + constexpr bool operator>=(const bel_c& l, const bel_c& r) + { + return !(l < r); + } typedef bel_c myrio_bel; typedef bel_c deci_bel; diff --git a/samples/sound_play/sound_play/main.cpp b/samples/sound_play/sound_play/main.cpp index 0e46080..4b8045e 100644 --- a/samples/sound_play/sound_play/main.cpp +++ b/samples/sound_play/sound_play/main.cpp @@ -1,4 +1,4 @@ -//#include +#include #include #include int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nCmdShow*/) { @@ -8,6 +8,8 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR / constexpr auto rate = 300_dB; constexpr auto rate2 = 4000_myrioB; constexpr auto rate3 = dxle::bel_cast(rate); + constexpr auto rate2_2 = rate2; #endif constexpr dxle::deci_bel rate4(100); + constexpr dxle::myrio_bel rate5{ 20 }; }