SFML community forums

Help => General => Topic started by: Josh_M on October 05, 2013, 05:22:02 pm

Title: Error checking a list of values.
Post by: Josh_M on October 05, 2013, 05:22:02 pm
Hi, I'm just wondering how I'd implement some specific error checking code. If I have for example, some key bindings in a namespace of type sf::KeyboardKey, which I equate to some of SFML's key enumerations, how can I check that I am passing a binding and not SFML's predefined enum to a function? (to avoid redefining SFML's enumerations by accident!)

Some code to demonstrate what I mean clearly...

Say I have a binding namespace like this:
namespace kb
{
        sf::Keyboard::Key moveLeft = sf::Keyboard::A;
        // etc...
}

And call a function like this (yes it's rather trivial, but the point remains):
void KeyBindings::SetKeyBinding(sf::Keyboard::Key &binding, sf::Keyboard::Key newKey)
{
        // check binding is a binding and not a predefined key
        if(binding is a binding)
        {      
                binding = newKey;
        }
}

I considered using an enumerated list for the bindings, but realised they're supposed to be rvalues, when I need lvalues. And the obvious problem with checking if binding == moveLeft (or whatever) is that binding == sf::Keyboard::A too ::).

Cheers.
Title: Re: Error checking a list of values.
Post by: Ixrec on October 05, 2013, 09:20:40 pm
The simplest answer is probably using an enum class (a C++11 feature; these have the type safety you want that regular enums don't) and having a std::map represent the bindings between your enum and SFML's enum.
Title: Re: Error checking a list of values.
Post by: Josh_M on October 06, 2013, 03:47:21 am
Ahh cheers, I'll try implementing something like that and see how it goes.