All Projects → falemagn → uberswitch

falemagn / uberswitch

Licence: Unlicense license
A header-only, unobtrusive, almighty alternative to the C++ switch statement that looks just like the original.

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to uberswitch

fameta-counter
Compile time counter that works with all major modern compilers
Stars: ✭ 34 (-59.04%)
Mutual labels:  tricks, cxx11, cxx14, cxx17
Thrust
The C++ parallel algorithms library.
Stars: ✭ 3,595 (+4231.33%)
Mutual labels:  cxx11, cxx14, cxx17
Libcudacxx
The C++ Standard Library for your entire system.
Stars: ✭ 1,861 (+2142.17%)
Mutual labels:  cxx11, cxx14, cxx17
relearn
A Reinforcement Learning Library for C++11/14
Stars: ✭ 22 (-73.49%)
Mutual labels:  cxx11, cxx14
botw-unexplored
Easily view the unexplored korok seeds and locations in your BotW savefile on your Switch
Stars: ✭ 79 (-4.82%)
Mutual labels:  switch
NSW-Custom-Game-Icons
Nintendo Switch custom game icons, icon repo for NX-GiC
Stars: ✭ 33 (-60.24%)
Mutual labels:  switch
xash3d-switch
Check out https://github.com/fgsfdsfgs/xash3d-fwgs for an updated version
Stars: ✭ 60 (-27.71%)
Mutual labels:  switch
RESegmentedControl
Customizable segmented control with style presets...
Stars: ✭ 76 (-8.43%)
Mutual labels:  switch
GDCheckbox
Customizable CheckBox / RadioButton component for iOS
Stars: ✭ 23 (-72.29%)
Mutual labels:  switch
Rich-Presence-U
Display your Nintendo games on Discord.
Stars: ✭ 77 (-7.23%)
Mutual labels:  switch
unifi-controllable-switch
TOUGHswitch firmware to integrate with the UniFi Controller (experimental).
Stars: ✭ 24 (-71.08%)
Mutual labels:  switch
homebridge-http-rgb-push
Homebridge plugin to control a web/http-based RGB device.
Stars: ✭ 16 (-80.72%)
Mutual labels:  switch
gen-cisco
🧨 Generates Cisco scripts based on YAML files
Stars: ✭ 29 (-65.06%)
Mutual labels:  switch
aoscx-ansible-role
Ansible roles for AOS-CX switches
Stars: ✭ 15 (-81.93%)
Mutual labels:  switch
smashtierlist
🎮⚡️ Popular tier lists for Super Smash Bros. series
Stars: ✭ 40 (-51.81%)
Mutual labels:  switch
view-admin-as
View the WordPress admin as a different role, switch between users, temporarily change your capabilities, set default screen settings for roles, manage your roles and capabilities.
Stars: ✭ 44 (-46.99%)
Mutual labels:  switch
switch-cmake
CMake toolchain for Nintendo Switch homebrew development
Stars: ✭ 38 (-54.22%)
Mutual labels:  switch
yrs
Yarn registry switch tool
Stars: ✭ 12 (-85.54%)
Mutual labels:  switch
alfred-browser-tabs
🔍 Search browser tabs from Chrome, Brave, Safari, etc..
Stars: ✭ 302 (+263.86%)
Mutual labels:  switch
ngx-ui-switch
Angular UI Switch component
Stars: ✭ 109 (+31.33%)
Mutual labels:  switch

uberswitch

A header-only, unobtrusive, almighty alternative to the C++ switch statement that looks just like the original.

Sample usage (incomplete - see it working on godbolt: https://godbolt.org/z/WTx6K1)

// Enable nesting
#define UBERSWITCH_ALLOW_NESTING 1

// Include the tool 
#include <uberswitch/uberswitch.hpp>

// The holy grail of the switches: the string switch!
int string2num(std::string s) {
    uswitch (s) {
        ucase ("one"):
            return 1;

        ucase ("two"):
            return 2;

        ucase ("three"):
            return 3;

        // fallthrough works too
        ucase ("four"):
        ucase ("f0ur"):
            return 4;

        default: return -1;
    }
}
    
// More unexpected types work too
std::string pair2string(std::pair<int, int> p) {
    uswitch (p) {
        ucase (std::make_pair(1, 2)):
            return "12";

        // List initialization works too
        ucase ({3, 4}):
            return "34";

        default:
            return "Unknown pair";
    }
}

// You can actually switch over multiple items without packing them explicitly
// and the whole construct is constexpr!
constexpr const char* pair2string(int a, int b) {
    uswitch (a, b) {
        ucase (1, 2):
            return "12";

        ucase (3, 4):
            return "34";

        // And if you don't care about one of the items, you can use a wildcard
        ucase (uberswitch::any, 5):
            return "any5";
            
        // Fallthrough works as well.
        ucase (6, 7):
        ucase (8, 9):
            return "67 or 89";
            
        // And you can of course break out of the switch at any time.
        ucase (0, 0):
            break;
            
        default:
            return "Unknown (a,b)";
    }
    
    return "You found the code to break out!";
}

// Uberswitches can be nested if you define UBERSWITCH_ALLOW_NESTING to 1.
// In that case, fameta::counter will be used to get access to compile time scoped counters, which are needed for the nesting functionality.
constexpr const char* pair2string(int a, int b, int c) {
    uswitch (a, b) {
        ucase (1, 2):
            return "12";

        ucase (3, 4):
            return "34";

        default:
            // Starting a new uberswitch here!
            uswitch (a, c) {
                ucase (3, 5):
                    return "35";
                    
                // And you can of course also break out of a nested switch.
                ucase (0, 0):
                    break;

                default: 
                    return "Unknown (a,b)";
            }
            
            break;        
    }
    
    return "You found the code to break out!";
}

// Within the standard uberswitch the continue keyword acts just like the break keyword, which makes it unusable for its intended purpose.
// However, for those cases in which such a functionality is required, uberswitch_c and case_c can be used. 
// The trailing 'c' stands for 'context', which is an identifier or a number used as the first parameter of both and needs to be kept in synch between them. 
// Uberswitch_c and case_c cannot be used in constexpr functions and require c++17. This is a necessary cost to be able to use the continue keyword as it was intended.
std::string pairs_in_map(const std::map<int, std::string> &map) {
    std::string ret;
    
    for (const auto &p: map) {
        uswitch_c (M, p.first, p.second) {
            ucase_c (M, 1, "2"):
                ret.append("12");
                break;

            ucase_c (M, 3, "4"):
                ret.append("34");
                break;

            default:
                ret.append("[").append(std::to_string(p.first)).append(p.second).append("]");
                continue;
        }
        
        ret.append("-");
    }
        
    return ret;
}

// The above function, given the following map as input:
//
//     std::map<int, std::string> m {
//         { 2, "4"},
//         { 1, "2"},
//         { 5, "6"},
//         { 3, "4"},
//         { 7, "8"}
//    };
//
// Produces the following output:
//
//     12-[24]34-[56][78]    
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].