Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Error checking a list of values.  (Read 1455 times)

0 Members and 1 Guest are viewing this topic.

Josh_M

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Error checking a list of values.
« 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.
« Last Edit: October 05, 2013, 05:47:53 pm by Josh_M »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Error checking a list of values.
« Reply #1 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.

Josh_M

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Error checking a list of values.
« Reply #2 on: October 06, 2013, 03:47:21 am »
Ahh cheers, I'll try implementing something like that and see how it goes.