diff options
Diffstat (limited to 'indra/llcommon/llunit.h')
-rw-r--r-- | indra/llcommon/llunit.h | 376 |
1 files changed, 257 insertions, 119 deletions
diff --git a/indra/llcommon/llunit.h b/indra/llcommon/llunit.h index e8f6b2b2ba..d980989c91 100644 --- a/indra/llcommon/llunit.h +++ b/indra/llcommon/llunit.h @@ -34,60 +34,43 @@ template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT = BASE struct LLUnitType : public BASE_UNIT { typedef DERIVED_UNIT unit_t; - typedef typename BASE_UNIT::value_t value_t; + + typedef typename STORAGE_TYPE storage_t; typedef void is_unit_t; LLUnitType() {} - explicit LLUnitType(value_t value) + explicit LLUnitType(storage_t value) : BASE_UNIT(convertToBase(value)) {} + // implicit downcast operator unit_t& () { return static_cast<unit_t&>(*this); } - value_t value() const + storage_t value() const { return convertToDerived(mBaseValue); } template<typename CONVERTED_TYPE> - value_t value() const + storage_t value() const { return CONVERTED_TYPE(*this).value(); } - static value_t convertToBase(value_t derived_value) - { - return (value_t)((F32)derived_value * unit_t::conversionToBaseFactor()); - } - - static value_t convertToDerived(value_t base_value) - { - return (value_t)((F32)base_value / unit_t::conversionToBaseFactor()); - } - - unit_t operator + (const unit_t other) const - { - return unit_t(mBaseValue + other.mBaseValue); - } - - unit_t operator - (const unit_t other) const - { - return unit_t(mBaseValue - other.mBaseValue); - } - - unit_t operator * (value_t multiplicand) const +protected: + static storage_t convertToBase(storage_t derived_value) { - return unit_t(mBaseValue * multiplicand); + return (storage_t)((F32)derived_value * unit_t::conversionToBaseFactor()); } - unit_t operator / (value_t divisor) const + static storage_t convertToDerived(storage_t base_value) { - return unit_t(mBaseValue / divisor); + return (storage_t)((F32)base_value / unit_t::conversionToBaseFactor()); } }; @@ -96,43 +79,40 @@ template<typename STORAGE_TYPE, typename T> struct LLUnitType<STORAGE_TYPE, T, T> { typedef T unit_t; - typedef typename STORAGE_TYPE value_t; + typedef STORAGE_TYPE storage_t; typedef void is_unit_t; + typedef T base_unit_t; LLUnitType() : mBaseValue() {} - explicit LLUnitType(value_t value) + explicit LLUnitType(storage_t value) : mBaseValue(value) {} - unit_t& operator=(value_t value) + unit_t& operator=(storage_t value) { setBaseValue(value); return *this; } + //implicit downcast operator unit_t& () { return static_cast<unit_t&>(*this); } - value_t value() const { return mBaseValue; } + storage_t value() const { return mBaseValue; } - static value_t convertToBase(value_t derived_value) + static storage_t convertToBase(storage_t derived_value) { - return (value_t)derived_value; + return (storage_t)derived_value; } - static value_t convertToDerived(value_t base_value) + static storage_t convertToDerived(storage_t base_value) { - return (value_t)base_value; - } - - unit_t operator + (const unit_t other) const - { - return unit_t(mBaseValue + other.mBaseValue); + return (storage_t)base_value; } void operator += (const unit_t other) @@ -140,108 +120,266 @@ struct LLUnitType<STORAGE_TYPE, T, T> mBaseValue += other.mBaseValue; } - unit_t operator - (const unit_t other) const - { - return unit_t(mBaseValue - other.mBaseValue); - } - void operator -= (const unit_t other) { mBaseValue -= other.mBaseValue; } - unit_t operator * (value_t multiplicand) const - { - return unit_t(mBaseValue * multiplicand); - } - - void operator *= (value_t multiplicand) + void operator *= (storage_t multiplicand) { mBaseValue *= multiplicand; } - unit_t operator / (value_t divisor) const - { - return unit_t(mBaseValue / divisor); - } - - void operator /= (value_t divisor) + void operator /= (storage_t divisor) { mBaseValue /= divisor; } protected: - void setBaseValue(value_t value) + void setBaseValue(storage_t value) { mBaseValue = value; } - value_t mBaseValue; + storage_t mBaseValue; }; -#define LL_DECLARE_BASE_UNIT(unit_name) \ - struct unit_name : public LLUnitType<F64, unit_name> \ - { \ - typedef unit_name base_unit_t; \ - typedef LLUnitType<F64, unit_name> unit_t; \ - typedef F64 storage_t; \ - \ - unit_name(F64 value) \ - : LLUnitType(value) \ - {} \ - \ - unit_name() \ - {} \ - \ - template <typename SOURCE_STORAGE_TYPE, typename SOURCE_TYPE> \ - unit_name(const LLUnitType<SOURCE_STORAGE_TYPE, unit_name, SOURCE_TYPE>& source) \ - { \ - setBaseValue((F64)source.unit_name::unit_t::value()); \ - } \ - \ - using LLUnitType::operator +; \ - using LLUnitType::operator +=; \ - using LLUnitType::operator -; \ - using LLUnitType::operator -=; \ - using LLUnitType::operator *; \ - using LLUnitType::operator *=; \ - using LLUnitType::operator /; \ - using LLUnitType::operator /=; \ +// +// operator + +// +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +DERIVED_UNIT operator + (typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t first, LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> second) +{ + return DERIVED_UNIT(first + second.value()); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +DERIVED_UNIT operator + (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t second) +{ + return DERIVED_UNIT(first.value() + second); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT, typename OTHER_DERIVED_UNIT> +DERIVED_UNIT operator + (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, LLUnitType<STORAGE_TYPE, BASE_UNIT, OTHER_DERIVED_UNIT> second) +{ + return DERIVED_UNIT(first.value() + second.value()); +} + +// +// operator - +// +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +DERIVED_UNIT operator - (typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t first, LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> second) +{ + return DERIVED_UNIT(first - second.value()); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +DERIVED_UNIT operator - (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t second) +{ + return DERIVED_UNIT(first.value() - second); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT, typename OTHER_DERIVED_UNIT> +DERIVED_UNIT operator - (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, LLUnitType<STORAGE_TYPE, BASE_UNIT, OTHER_DERIVED_UNIT> second) +{ + return DERIVED_UNIT(first.value() - second.value()); +} + +// +// operator * +// +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +DERIVED_UNIT operator * (typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t first, LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> second) +{ + return DERIVED_UNIT(first * second.value()); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +DERIVED_UNIT operator * (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t second) +{ + return DERIVED_UNIT(first.value() * second); +} + +// +// operator / +// +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +DERIVED_UNIT operator / (typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t first, LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> second) +{ + return DERIVED_UNIT(first * second.value()); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +DERIVED_UNIT operator / (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t second) +{ + return DERIVED_UNIT(first.value() * second); +} + +// +// operator < +// +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator < (typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t first, LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> second) +{ + return first < second.value(); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator < (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t second) +{ + return first.value() < second; +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT, typename OTHER_DERIVED_UNIT> +bool operator < (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, LLUnitType<STORAGE_TYPE, BASE_UNIT, OTHER_DERIVED_UNIT> second) +{ + return first.value() < second.value(); +} + +// +// operator <= +// +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator <= (typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t first, LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> second) +{ + return first <= second.value(); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator <= (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t second) +{ + return first.value() <= second; +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT, typename OTHER_DERIVED_UNIT> +bool operator <= (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, LLUnitType<STORAGE_TYPE, BASE_UNIT, OTHER_DERIVED_UNIT> second) +{ + return first.value() <= second.value(); +} + +// +// operator > +// +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator > (typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t first, LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> second) +{ + return first > second.value(); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator > (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t second) +{ + return first.value() > second; +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT, typename OTHER_DERIVED_UNIT> +bool operator > (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, LLUnitType<STORAGE_TYPE, BASE_UNIT, OTHER_DERIVED_UNIT> second) +{ + return first.value() > second.value(); +} +// +// operator >= +// +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator >= (typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t first, LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> second) +{ + return first >= second.value(); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator >= (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t second) +{ + return first.value() >= second; +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT, typename OTHER_DERIVED_UNIT> +bool operator >= (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, LLUnitType<STORAGE_TYPE, BASE_UNIT, OTHER_DERIVED_UNIT> second) +{ + return first.value() >= second.value(); +} + +// +// operator == +// +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator == (typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t first, LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> second) +{ + return first == second.value(); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator == (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t second) +{ + return first.value() == second; +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT, typename OTHER_DERIVED_UNIT> +bool operator == (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, LLUnitType<STORAGE_TYPE, BASE_UNIT, OTHER_DERIVED_UNIT> second) +{ + return first.value() == second.value(); +} + +// +// operator != +// +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator != (typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t first, LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> second) +{ + return first != second.value(); +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT> +bool operator != (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, typename LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT>::storage_t second) +{ + return first.value() != second; +} + +template<typename STORAGE_TYPE, typename BASE_UNIT, typename DERIVED_UNIT, typename OTHER_DERIVED_UNIT> +bool operator != (LLUnitType<STORAGE_TYPE, BASE_UNIT, DERIVED_UNIT> first, LLUnitType<STORAGE_TYPE, BASE_UNIT, OTHER_DERIVED_UNIT> second) +{ + return first.value() != second.value(); +} + +#define LL_DECLARE_BASE_UNIT(unit_name) \ + template<typename STORAGE> \ + struct unit_name : public LLUnitType<STORAGE, unit_name<STORAGE>, unit_name<STORAGE> > \ + { \ + typedef LLUnitType<STORAGE, unit_name> unit_t; \ + \ + unit_name(storage_t value = 0) \ + : LLUnitType(value) \ + {} \ + \ + template <typename SOURCE_STORAGE_TYPE, typename SOURCE_TYPE> \ + unit_name(LLUnitType<SOURCE_STORAGE_TYPE, unit_name<SOURCE_STORAGE_TYPE>, SOURCE_TYPE> source) \ + { \ + setBaseValue((storage_t)source.unit_name<SOURCE_STORAGE_TYPE>::unit_t::value()); \ + } \ + \ }; -#define LL_DECLARE_DERIVED_UNIT(base_unit, derived_unit, conversion_factor) \ - struct derived_unit : public LLUnitType<F64, base_unit, derived_unit> \ - { \ - typedef base_unit base_unit_t; \ - typedef LLUnitType<F64, base_unit, derived_unit> unit_t; \ - typedef F64 storage_t; \ - \ - derived_unit(value_t value) \ - : LLUnitType(value) \ - {} \ - \ - derived_unit() \ - {} \ - \ - template <typename SOURCE_STORAGE_TYPE, typename SOURCE_TYPE> \ - derived_unit(const LLUnitType<SOURCE_STORAGE_TYPE, base_unit, SOURCE_TYPE>& source) \ - { \ - setBaseValue((F64)source.base_unit::unit_t::value()); \ - } \ - \ - static F32 conversionToBaseFactor() { return (F32)(conversion_factor); } \ - \ - using LLUnitType::operator +; \ - using LLUnitType::operator +=; \ - using LLUnitType::operator -; \ - using LLUnitType::operator -=; \ - using LLUnitType::operator *; \ - using LLUnitType::operator *=; \ - using LLUnitType::operator /; \ - using LLUnitType::operator /=; \ +#define LL_DECLARE_DERIVED_UNIT(base_unit, derived_unit, conversion_factor) \ + template<typename STORAGE> \ + struct derived_unit : public LLUnitType<STORAGE, base_unit<STORAGE>, derived_unit<STORAGE> > \ + { \ + typedef LLUnitType<STORAGE, base_unit<STORAGE>, derived_unit<STORAGE> > unit_t; \ + \ + derived_unit(storage_t value = 0) \ + : LLUnitType(value) \ + {} \ + \ + template <typename SOURCE_STORAGE_TYPE, typename SOURCE_TYPE> \ + derived_unit(LLUnitType<SOURCE_STORAGE_TYPE, base_unit<SOURCE_STORAGE_TYPE>, SOURCE_TYPE> source) \ + { \ + setBaseValue((storage_t)source.base_unit<SOURCE_STORAGE_TYPE>::unit_t::value()); \ + } \ + \ + static F32 conversionToBaseFactor() { return (F32)(conversion_factor); } \ + \ }; -namespace LLUnits +namespace LLUnit { LL_DECLARE_BASE_UNIT(Bytes); LL_DECLARE_DERIVED_UNIT(Bytes, Kilobytes, 1024); |