CLEANUP: Macro for special value blues

Development directions, tasks, and features being actively implemented or pursued by the development team.
Post Reply
chuck_starchaser
Elite
Elite
Posts: 8014
Joined: Fri Sep 05, 2003 4:03 am
Location: Montreal
Contact:

CLEANUP: Macro for special value blues

Post by chuck_starchaser »

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
Post Reply