Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is there a way to branch on multiple condition without writing code that looks like a mess? Syntaxic sugar in C++11 or C++14 would be appreciated.

#include <iostream>

enum state
{
    STATE_1,
    STATE_2,
    STATE_3,
    STATE_4,
    STATE_5,
    STATE_6,
    STATE_7,
    STATE_8,
};

state f(int a, bool b, const std::string& str)
{
    // How not to:
    if (a < 0)
    {
        if (b == false)
        {
            if (str != "morning")
            {
                return STATE_1;
            }
            else
            {
                return STATE_2;
            }
        }
        else
        {
            if (str != "morning")
            {
                return STATE_3;
            }
            else
            {
                return STATE_4;
            }
        }
    }
    else // a >= 0
    {
        if (b == false)
        {
            if (str != "morning")
            {
                return STATE_5;
            }
            else
            {
                return STATE_6;
            }
        }
        else
        {
            if (str != "morning")
            {
                return STATE_7;
            }
            else
            {
                return STATE_8;
            }
        }
    }
}

int main()
{
    std::cout << "State: " << f(1, true, "morning") << std::endl;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

One could embed a list of boolean (condition results) in a POD at compile-time and switch on it.

Usage: main.cpp

#include <iostream> /* std::cout */
#include "mswitch.h" /* mswitch, mcase */

enum state
{
    STATE_1,
    STATE_2,
    STATE_3,
    STATE_4,
    STATE_5,
    STATE_6,
    STATE_7,
    STATE_8,
};

state f(int a, bool b, const std::string& str)
{
    mswitch(a >= 0, b == true, str == "morning")
    {
        mcase(false, false, false): return STATE_1;
        mcase(false, false, true) : return STATE_2;
        mcase(false, true, false) : return STATE_3;
        mcase(false, true, true)  : return STATE_4;
        mcase(true, false, false) : return STATE_5;
        mcase(true, false, true)  : return STATE_6;
        mcase(true, true, false)  : return STATE_7;
        mcase(true, true, true)   : return STATE_8;
    }
    return STATE_1;
}

int main()
{
    std::cout << "State: " << f(1, true, "morning") << std::endl;
}

Syntaxic sugar: mswitch.h

#ifndef MSWITCH_GUARD_H
#define MSWITCH_GUARD_H

#include <initializer_list>
#include <cstddef>

namespace mswitch
{
    constexpr long long encode(long long value, size_t size) { return value << 6 | (0x3F & size); }

    class mswitch
    {
        std::initializer_list<bool> _flags;
    public:
        mswitch(std::initializer_list<bool> const& l) : _flags(l) {}
        operator long long() const
        {
            long long result = 0;
            size_t index = 0;
            for (bool b : _flags) {
                result |= b << index++;
            }
            return encode(result, _flags.size());
        }
    };

    template<bool head, bool... tail>
    struct mcase
    {
        constexpr mcase() = default;
        constexpr operator long long() const
        {
            return encode(tll(), 1+sizeof...(tail));
        }
        constexpr long long tll() const { return head | mcase<tail...>().tll() << 1; }
    };

    template<bool b>
    struct mcase<b>
    {
        constexpr mcase() = default;
        constexpr operator long long() const { return encode(tll(), 1); }
        constexpr long long tll() const { return b; }
    };
}

#define mswitch(head, ...) switch(mswitch::mswitch{head, __VA_ARGS__})
#define mcase(head, ...) case mswitch::mcase<head, __VA_ARGS__>()

#endif // MSWITCH_GUARD_H

Compile with g++ -std=c++14 -O2 -Wall -pedantic main.cpp

How it works

The mswitch and mcase objects simply build (at compile-time if possible, using constexpr functions) a bijection between a boolean list and a switchable long long. Since mcases are given compile-time constants, all switch labels are in fact contiguous compile-time constant themselves.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...