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

Asked

Viewed 38 times

1

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

PS: I can not limit by 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;
}
  • In the line you comment "Not safe" (...) you are practically throwing away the class that limits the value.... The comment is correct, which makes it seem that you already know where the problem is...

  • Yes, I know where the problem is, but I need to overload this operator because of the dependency on the other libraries of the project

1 answer

0

The guy float does not have the built-in value limitation option. If you need to limit values, you need to do it while the type is still interpreted as that class you created, and then convert it to the type required by the external library. In addition, I advise you to declare an explicit function for this, such as:

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

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

To 'automatically' limit the value, you has 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>
  • This does not solve my problems, as I would have to modify all the functions of the other libraries that my project uses (and there are many), what I mean is that the type of variable that will be passed to each function of other libraries of my project must be int, float, double, etc., I cannot simply modify each function (thousands of functions) of a huge project.

  • If you want a class that limits value, this class is one, if you want a double, it’s something else. There is no way to change the behavior of doubles (or floats etc). I understand your problem, I am saying that the value limitation has to occur before to pass as double.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.