Function bm2str

Summary

#include <src/c4/bitmask.hpp>

(1) template <class Enum>
    size_t bm2str(typename std::underlying_type< Enum >::type bits, char *str=nullptr, size_t sz=0, EnumOffsetType offst=EOFFS_PFX)

(2) template <class Enum>
    std::enable_if< is_scoped_enum< Enum >::value, size_t >::type bm2str(Enum bits, char *str=nullptr, size_t sz=0, EnumOffsetType offst=EOFFS_PFX)

Function overload

Synopsis

#include <src/c4/bitmask.hpp>

template <class Enum>
size_t bm2str(typename std::underlying_type< Enum >::type bits, char *str=nullptr, size_t sz=0, EnumOffsetType offst=EOFFS_PFX)

Description

convert a bitmask to string. return the number of characters written. To find the needed size, call first with str=nullptr and sz=0

Mentioned in

Source

Lines 120-178 in src/c4/bitmask.hpp.

template<class Enum>
size_t bm2str
(
    typename std::underlying_type<Enum>::type bits,
    char *str=nullptr,
    size_t sz=0,
    EnumOffsetType offst=EOFFS_PFX
)
{
    using I = typename std::underlying_type<Enum>::type;
    C4_ASSERT((str == nullptr) == (sz == 0));

    auto syms = esyms<Enum>();
    size_t pos = 0;
    typename EnumSymbols<Enum>::Sym const* zero = nullptr;

    // do reverse iteration to give preference to composite enum symbols,
    // which are likely to appear later in the enum sequence
    for(size_t i = syms.size()-1; i != size_t(-1); --i)
    {
        auto const p = syms[i];
        I b = static_cast<I>(p.value);
        if(b == 0)
        {
            zero = &p; // save this symbol for later
        }
        else if((bits & b) == b)
        {
            bits &= ~b;
            // append bit-or character
            if(pos > 0)
            {
                _c4prependchars(*str = '|', 1);
            }
            // append bit string
            const char *pname = p.name_offs(offst);
            size_t len = strlen(pname);
            _c4prependchars(strncpy(str, pname, len), len);
        }
    }

    C4_CHECK_MSG(bits == 0, "could not find all bits");
    if(pos == 0) // make sure at least something is written
    {
        if(zero) // if we have a zero symbol, use that
        {
            const char *pname = zero->name_offs(offst);
            size_t len = strlen(pname);
            _c4prependchars(strncpy(str, pname, len), len);
        }
        else // otherwise just write an integer zero
        {
            _c4prependchars(*str = '0', 1);
        }
    }
    _c4appendchars(str[pos] = '\0', 1);

    return pos;
}

Synopsis

#include <src/c4/bitmask.hpp>

template <class Enum>
std::enable_if< is_scoped_enum< Enum >::value, size_t >::type bm2str(Enum bits, char *str=nullptr, size_t sz=0, EnumOffsetType offst=EOFFS_PFX)

Description

cleanup!

scoped enums do not convert automatically to their underlying type, so this SFINAE overload will accept scoped enum symbols and cast them to the underlying type

Mentioned in

Source

Lines 189-201 in src/c4/bitmask.hpp.

template<class Enum>
typename std::enable_if<is_scoped_enum<Enum>::value, size_t>::type
bm2str
(
    Enum bits,
    char *str=nullptr,
    size_t sz=0,
    EnumOffsetType offst=EOFFS_PFX
)
{
    using I = typename std::underlying_type<Enum>::type;
    return bm2str<Enum>(static_cast<I>(bits), str, sz, offst);
}





Add Discussion as Guest

Log in