C ++ checks all status bits in a structure

I need to save the error status bit as boolvariables inside a struct. Now I need a way to check if any / not all variables are set.

I know there is std::bitset, but it only allows me to access values ​​by index, not by name. Therefore, I made this construction:

#include <cassert>
union ErrorStatus
{
    struct {
        bool error1;
        bool error2;
        bool error3;
    };
    std::array<bool, 3> bits;
    bool any()const{
        return std::any_of(bits.begin(), bits.end(), [](const bool& b){return b == true; });
        }
    bool none() const {
        return !any();
    }
    bool all()const{
        return !std::any_of(bits.begin(), bits.end(), [](const bool& b){return b == false; });
    }

};

int main(int argc, char** argv)
{

    ErrorStatus errorStatus;
    assert(errorStatus.none() == true);
    errorStatus.error2 = true;
    assert(errorStatus.none() == false);

    return 0;
}

But with this, I had a problem in that I would need to change the number in the declaration std::arrayin accordance with the number of logical values ​​inside struct.

Is there a way to synchronize the size arraywith the number of bools in the structure? Are there alternatives that are more elegant (and hopefully read well)?

+4
source share
2

, (, , )?

enum :

enum {
  error1,
  error2,
  error3,
  errorsSize // must be last
};

using ErrorStatus = std::array<bool, errorsSize>;

ErrorStatus errors;
errors[error1] = false;

errorsSize "" .

+4

, , usabe :

#include <iostream>
#include <algorithm>

// Configure structure packing for Visual Studio.
// See your compiler documentation on how to do this.
#pragma pack(push)
#pragma pack(1)

struct Errors
{
    bool error1;
    bool error2;
    bool error3;

    inline const bool* begin() const
    {
        return reinterpret_cast<const bool*>(this);
    }

    inline const bool* end() const
    {
        return reinterpret_cast<const bool*>(this + 1);
    }

    inline bool any() const
    {
        return std::count(begin(), end(), true) > 0;
    }

    inline bool all() const
    {
        return std::count(begin(), end(), true) == end() - begin();
    }

    inline bool none() const
    {
        return std::count(begin(), end(), true) == 0;
    }
};

#pragma pack(pop)

int main()
{

    Errors e;
    memset(&e, 0, sizeof(e));

    e.error1 = false;
    e.error2 = false;
    e.error3 = false;
    std::cout << "any=" << e.any() << ", all=" << e.all() << ", none=" << e.none() << std::endl;

    e.error1 = true;
    e.error2 = false;
    e.error3 = false;
    std::cout << "any=" << e.any() << ", all=" << e.all() << ", none=" << e.none() << std::endl;

    e.error1 = true;
    e.error2 = true;
    e.error3 = true;
    std::cout << "any=" << e.any() << ", all=" << e.all() << ", none=" << e.none() << std::endl;

    return 0;
}

, .

-

enum class Enum
{
  Error1,
  Error2,
  Error3
};

typedef std::set<Error> Errors;
0

Source: https://habr.com/ru/post/1687563/


All Articles