CLEANUP: Macro for special value blues

Development directions, tasks, and features being actively implemented or pursued by the development team.

CLEANUP: Macro for special value blues

Postby chuck_starchaser » Sun Jan 31, 2010 2:30 pm

Ridiculously often, in the vs engine, special meanings are attached to special values of floats. This is ultra-bad because it puts the onus on future programmers to look into the history and genesis of every variable to make sure it couldn't have some special value in some circumstances, before they can work with the code confidently.
But to fix the problem properly, we'd have to package the special state as an enum or a bool, together with the float into a struct.
A compromise is to have the float inside a struct, and functions that handle the special value.
I just made a macro to make this easier (not tested, tho):

Code: Select all
#define SPECIAL_VALUE_TYPE( type, name, special_val, meaning ) \
class name                                       \
{                                                \
  type value;                                    \
public:                                          \
  name() : value(special_val) {}                 \
  name(type const & v) : value(v)                \
  {                                              \
    assert( value != special_val );              \
  }                                              \
  type operator=(type const & v)                 \
  {                                              \
    assert( v != special_val );                  \
    return (value = v);                          \
  }                                              \
  bool meaning() const                           \
  { return value == special_val; }               \
  void set_#meaning() { value = special_val; }   \
  operator type() const                          \
  {                                              \
    assert( value != special_val );              \
    return value;                                \
  }                                              \
}

Usage:
SPECIAL_VALUE_TYPE( float, fuel_in_tank, -1.0f, this_is_a_station_dogbreath );
fuel_in_tank my_fuel;
my_fuel.this_is_a_station_dogbreath() returns true
my_fuel = 77.7f;
my_fuel.this_is_a_station_dogbreath() returns false
float a = my_fuel; works
my_fuel.set_this_is_a_station_dogbreath();
my_fuel.this_is_a_station_dogbreath() returns true
float b = my_fuel; assertion fails
User avatar
chuck_starchaser
Elite
Elite
 
Posts: 8014
Topics: 195
Joined: Thu Sep 04, 2003 9:03 pm
Location: Montreal

Share On:

Share on Facebook Facebook Share on Twitter Twitter Share on Digg Digg

Return to Engine Development

Who is online

Users browsing this forum: No registered users and 2 guests

cron