AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AMetric.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <AUI/Core.h>
   15#include <type_traits>
   16#include <ostream>
   17#include <tuple>
   18#include "AUI/Util/Assert.h"
   19
   20class AString;
   21
   74class API_AUI_VIEWS AMetric
   75{
   76public:
   77    enum Unit
   78    {
   79        T_UNKNOWN,
   80        T_PX,
   81        T_DP,
   82        T_PT,
   83    };
   84
   85private:
   86    float mValue;
   87    Unit mUnit;
   88
   89    float convertValueToMyUnit(const AMetric& anotherMetric) {
   90        return fromPxToMetric(anotherMetric.getValuePx(), mUnit);
   91    }
   92
   93public:
   94    constexpr AMetric():
   95        AMetric(0, T_PX) {}
   96
  110    template<typename T, typename std::enable_if_t<std::is_integral_v<T>, bool> = 0>
  111    constexpr AMetric(T value):
  112        AMetric(value, T_PX)
  113    {
  114        AUI_ASSERTX(value == 0,
  115                    "please use _px, _dp or _pt literal for AMetric initialisation. only zero allowed to "
  116                    "initialise AMetric without literal");
  117    }
  118
  119    constexpr AMetric(float value, Unit unit): mValue(value), mUnit(unit) {
  120
  121    }
  122
  123    AMetric(const AString& text);
  124
  125    [[nodiscard]] float getRawValue() const
  126    {
  127        return mValue;
  128    }
  129
  130    [[nodiscard]] Unit getUnit() const
  131    {
  132        return mUnit;
  133    }
  134
  135    [[nodiscard]] float getValuePx() const;
  136    [[nodiscard]] float getValueDp() const;
  137
  138    static float fromPxToMetric(float value, Unit unit);
  139
  140    operator float() const {
  141        return getValuePx();
  142    }
  143    
  144    AMetric operator-() const {
  145        return {-mValue, mUnit};
  146    }
  147
  148    AMetric& operator+=(AMetric rhs) noexcept {
  149        AUI_ASSERT(mUnit == rhs.mUnit);
  150        mValue += rhs.mValue;
  151        return *this;
  152    }
  153
  154    AMetric& operator-=(AMetric rhs) noexcept {
  155        AUI_ASSERT(mUnit == rhs.mUnit);
  156        mValue -= rhs.mValue;
  157        return *this;
  158    }
  159
  160    AMetric& operator*=(AMetric rhs) noexcept {
  161        AUI_ASSERT(mUnit == rhs.mUnit);
  162        mValue *= rhs.mValue;
  163        return *this;
  164    }
  165
  166    AMetric& operator/=(AMetric rhs) noexcept {
  167        AUI_ASSERT(mUnit == rhs.mUnit);
  168        mValue /= rhs.mValue;
  169        return *this;
  170    }
  171
  172    AMetric operator+(AMetric rhs) const noexcept {
  173        AUI_ASSERT(mUnit == rhs.mUnit);
  174        auto copy = *this;
  175        copy += rhs;
  176        return copy;
  177    }
  178
  179    AMetric operator-(AMetric rhs) const noexcept {
  180        AUI_ASSERT(mUnit == rhs.mUnit);
  181        auto copy = *this;
  182        copy -= rhs;
  183        return copy;
  184    }
  185
  186    AMetric operator*(AMetric rhs) const noexcept {
  187        AUI_ASSERT(mUnit == rhs.mUnit);
  188        auto copy = *this;
  189        copy *= rhs;
  190        return copy;
  191    }
  192
  193    AMetric operator/(AMetric rhs) const noexcept {
  194        AUI_ASSERT(mUnit == rhs.mUnit);
  195        auto copy = *this;
  196        copy /= rhs;
  197        return copy;
  198    }
  199
  200    AMetric& operator*=(float rhs) noexcept {
  201        mValue *= rhs;
  202        return *this;
  203    }
  204
  205    AMetric& operator/=(float rhs) noexcept {
  206        mValue /= rhs;
  207        return *this;
  208    }
  209
  210    AMetric operator*(float rhs) const noexcept {
  211        auto copy = *this;
  212        copy *= rhs;
  213        return copy;
  214    }
  215
  216    AMetric operator/(float rhs) const noexcept {
  217        auto copy = *this;
  218        copy /= rhs;
  219        return copy;
  220    }
  221
  222    bool operator==(const AMetric& rhs) const {
  223        return std::tie(mValue, mUnit) == std::tie(rhs.mValue, rhs.mUnit);
  224    }
  225
  226    bool operator!=(const AMetric& rhs) const {
  227        return !(rhs == *this);
  228    }
  229};
  230
  231
  232constexpr inline AMetric operator""_px(unsigned long long v)
  233{
  234    return AMetric(static_cast<float>(static_cast<long long>(v)), AMetric::T_PX);
  235}
  236constexpr inline AMetric operator""_dp(unsigned long long v)
  237{
  238    return AMetric(static_cast<float>(static_cast<long long>(v)), AMetric::T_DP);
  239}
  240constexpr inline AMetric operator""_pt(unsigned long long v)
  241{
  242    return AMetric(static_cast<float>(static_cast<long long>(v)), AMetric::T_PT);
  243}
  244
  245inline std::ostream& operator<<(std::ostream& o, const AMetric& value) {
  246    o << value.getRawValue();
  247    switch (value.getUnit()) {
  248        case AMetric::T_PX:
  249            o << "_px";
  250            break;
  251        case AMetric::T_DP:
  252            o << "_dp";
  253            break;
  254        case AMetric::T_PT:
  255            o << "_pt";
  256            break;
  257
  258        default:
  259            break;
  260    }
  261    return o;
  262}
Stores dimensions in scalable units (dp, pt, etc...).
Definition AMetric.h:75
constexpr AMetric(T value)
Constructor for AMetric a; a = 0 without unit specifier. Can be used only for zero initialization (se...
Definition AMetric.h:111
Represents a Unicode character string.
Definition AString.h:38
#define AUI_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition Assert.h:55
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74