How to limit a value entered / modified by the memory address?

1

How to limit a value that has been entered / changed by the memory address?

PS: I can not limit the function, this function is just an example function showing the problem, I will need to pass this object to functions of other libraries in which I can not change.

#include <iostream>

template <typename type>
class Var
{
private:
    type Value               = (type)0;
    type ClampMin            = (type)0;
    type ClampMax            = (type)0;
    bool NeedClamp           = false;
public:
    Var()                                    { }
    Var(type initialVal) : Value(initialVal) { }
    Var(type initialVal, type Min, type Max)
    {
        this->NeedClamp = true;
        this->ClampMin = Min; this->ClampMax = Max;

        this->operator=(initialVal);
    }

    constexpr bool IsClampActive() const
    {
        return this->NeedClamp;
    }

    constexpr type Min() const
    {
        return this->ClampMin;
    }

    constexpr type Max() const
    {
        return this->ClampMax;
    }

    // Operador seguro pois consegue limitar os valores inseridos
    type& operator=(type val) noexcept
    {
        if (this->NeedClamp)
        {
            if (val > this->ClampMax)
                this->Value = this->ClampMax;
            else if (val < this->ClampMin)
                this->Value = this->ClampMin;
            else
                this->Value = val;
        }
        else
        {
            this->Value = val;
        }
        return this->Value;
    }

    // Para converter automaticamente o tipo
    // Não seguro
    // Como limitar o valor inserido nesse operador?
    operator type*()
    {
        return &this->Value;
    }

    // Para converter automaticamente o tipo
    // Não seguro
    // Como limitar o valor inserido nesse operador?
    operator type&()
    {
        return this->Value;
    }

    template <typename typeVal>
    constexpr bool operator==(typeVal val) const
    {
        return (this->Value == (type)val);
    }

    template <typename typeVal>
    constexpr bool operator!=(typeVal val) const
    {
        return (this->Value != (type)val);
    }
};

#define MIN_VALORTESTE                    1.f
#define MAX_VALORTESTE                    100.f

float problema(float& valor)
{
    valor = 200.f; // Vai alterar o valor para um valor maior que o limite definido(100.f), como limitar o valor inserido nesse caso?
    return valor;
}

int main()
{
    //Var<float> Valor_Teste = 50.f;
    Var<float> Valor_Teste = { /* Valor inicial da variável */ 50.f, /* Valor minimo permitido para a variável*/ MIN_VALORTESTE, /* Valor maximo permitido para a variável */ MAX_VALORTESTE };

    std::cout << problema(Valor_Teste) << std::endl;

    // Mostrando o novo valor da variável(vai mostrar um valor inválido, pois está maior que o limite definido(MAX_VALORTESTE))
    std::cout << Valor_Teste << std::endl;

    std::cin.get();

    return 0;
}
    
asked by anonymous 19.05.2018 / 20:02

1 answer

0

Type float does not have built-in value limitation option. If you need to limit values, you must do so while the type is still interpreted as this class you created, and then convert it to the type required by the external library. Also, I would advise you to declare an explicit function for this, of the type:

template <typename type>
//(...)
type* converter_para_type_pt{
    return &this->Value;
}
//(...)

And call this function when you need to switch to an external library, and do not rely on an implicit operator, as this can be inferred by the compiler in many situations ...

To 'automatically' limit the value, you have to use the defined class:

template <typename T>
T problema(Var<T>& valor)
//aqui desenvolve a função usando os métodos de Var<T>
    
19.05.2018 / 20:29