#include <C:/Work/specialnumbers_svn/NumberLib/NumbersLib/BufferedBool.h>
It works as follows: an internal value is maintained. Every time this class is set to true, the value is increased; every time it is set to false, that value is reduced. When the value becomes larger than a certain threshold, the output value is switched to true. Similarly when the value becomes smaller than a certain threshold, the output value is switched to false. These two thresholds needn't (and in most cases shouldn't) be the same.
This class is supposed to mimick bools. However, assignement and other operators have not been overloaded so that this class won't be confused with an actual bool.
For example, suppose the internal float value is well below the threshold. Then the following code results print "false";
b.setValue(true) printf(b.getValue() ? "true" : "false");
If we overloaded the operators, we would get
b = false; printf(b ? "true" : "false");
which would be more confusing. The less convenient but explicit syntax reminds us that this is not pure bool, but something special.
Using with floating point
Consider this example:
BufferedBool b(0.3f, 0.7f, 0.1f); for(int i = 0; i < 8; i++) { b.setValue(true); cout << b.getValue() << " "; }
This will print
false false false false false false true true
false false false false false false false true
This is because the top threshold is converted to .69999999 and adding the increment 0.1 seven times is to 0 gives 0.70000005, so the internal float becomes greater than the threshold one iteration too early. Never rely on exact transitions!
Definition at line 71 of file BufferedBool.h.
Public Member Functions | |
BufferedBool (float bottomThreshold, float topThreshold, float increment=0.1f) | |
Constructs a new BufferedBool. | |
void | forceValue (bool value) |
Forces the next value to be the given value. | |
float | getFloatValue () const |
Returns the internal float value that regulates the boolean value. | |
bool | getValue () const |
Returns the boolean value, which depends on which threshold was last triggered. | |
void | setIncrement (float increment) |
Sets the uincreemnt value for this buffered bool. | |
void | setValue (bool value, float ellapsedTime=1) |
The internal float is incremented if this is true, otherwise it is decremented. | |
Private Attributes | |
bool | mBoolValue |
float | mBottomThreshold |
ClampedNumber< float > | mFloatValue |
float | mFrameTime |
float | mTopThreshold |
BufferedBool | ( | float | bottomThreshold, | |
float | topThreshold, | |||
float | increment = 0.1f | |||
) |
Constructs a new BufferedBool.
bottomThreshold | if the internal float value drops below this point, the bool value next returned will be false. | |
topThreshold | if the internal float value rises above this point, the bool value next returned will be true. | |
increment | The value with which the internal float is adjusted each time setValue() is called. |
Definition at line 8 of file BufferedBool.cpp.
void forceValue | ( | bool | value | ) |
Forces the next value to be the given value.
Definition at line 45 of file BufferedBool.cpp.
References RangedNumber::max(), BufferedBool::mBoolValue, BufferedBool::mFloatValue, RangedNumber::min(), and RangedNumber::setValue().
float getFloatValue | ( | ) | const |
Returns the internal float value that regulates the boolean value.
This function is useful for debugging.
Definition at line 56 of file BufferedBool.cpp.
References RangedNumber::getValue(), and BufferedBool::mFloatValue.
bool getValue | ( | ) | const [virtual] |
Returns the boolean value, which depends on which threshold was last triggered.
Implements UpdateableNumber< bool >.
Definition at line 40 of file BufferedBool.cpp.
References BufferedBool::mBoolValue.
void setIncrement | ( | float | increment | ) |
Sets the uincreemnt value for this buffered bool.
In general, the larger this value, the quicker the bool will chnage states.
Definition at line 51 of file BufferedBool.cpp.
References BufferedBool::mFloatValue, and RangedNumber::setIncrement().
void setValue | ( | bool | value, | |
float | ellapsedTime = 1 | |||
) | [virtual] |
The internal float is incremented if this is true, otherwise it is decremented.
If one of the thresholds is triggered, the boolean value is adjusted accordingly.
Implements UpdateableNumber< bool >.
Definition at line 16 of file BufferedBool.cpp.
References ClampedNumber::dec(), ClampedNumber::inc(), BufferedBool::mBoolValue, BufferedBool::mBottomThreshold, BufferedBool::mFloatValue, and BufferedBool::mTopThreshold.
bool mBoolValue [private] |
Definition at line 77 of file BufferedBool.h.
Referenced by BufferedBool::forceValue(), BufferedBool::getValue(), and BufferedBool::setValue().
float mBottomThreshold [private] |
ClampedNumber<float> mFloatValue [private] |
Definition at line 76 of file BufferedBool.h.
Referenced by BufferedBool::forceValue(), BufferedBool::getFloatValue(), BufferedBool::setIncrement(), and BufferedBool::setValue().
float mFrameTime [private] |
Definition at line 78 of file BufferedBool.h.
float mTopThreshold [private] |