diff --git a/src/lib/time/digxel.hpp b/src/lib/time/digxel.hpp index 7256c0061..fddae6168 100644 --- a/src/lib/time/digxel.hpp +++ b/src/lib/time/digxel.hpp @@ -175,6 +175,14 @@ namespace time { CountFormatter() : PrintfFormatter("%04ld") { } }; + + struct SignFormatter + { + void clear() { } + size_t maxlen() const { return 1; } + CBuf show (int val) { return val<0? "-":" "; } + }; + } //(End) digxel configuration namespace @@ -273,7 +281,25 @@ namespace time { /* == predefined Digxel configurations == */ typedef Digxel< int, digxel::SexaFormatter> SexaDigit; ///< for displaying time components (sexagesimal) typedef Digxel HexaDigit; ///< for displaying a hex byte - typedef Digxel CountVal; ///< for displaying a hex byte + typedef Digxel CountVal; ///< for displaying a counter + + + /** special Digxel to show a sign. + * @note values limited to +1 and -1 */ + struct Signum + : Digxel + { + Signum() { setValueRaw(1); } + + void + operator= (int n) + { + int newSign = 0 > mutator(n)? -1:+1; + this->setValueRaw (newSign); + } + + friend int operator*= (Signum s, int c) { s = c*s; return s; } + }; }} // lib::time diff --git a/tests/lib/time/digxel-configurations-test.cpp b/tests/lib/time/digxel-configurations-test.cpp index 6a226eb23..607be80b1 100644 --- a/tests/lib/time/digxel-configurations-test.cpp +++ b/tests/lib/time/digxel-configurations-test.cpp @@ -69,7 +69,27 @@ namespace test{ verifyConfiguration (0xc); verifyConfiguration (0x6f); verifyConfiguration (-1234567890); - } + + verifySignum(); + } + + + void + verifySignum() + { + Signum sig; + CHECK (1 == sig); + + sig = 123; + CHECK (1 == sig); + sig = -sig; + CHECK (-1 == sig); + sig = -98; + CHECK (-1 == sig); + CHECK (sig.show() == string("-")); + sig *= -1; + CHECK (sig.show() == string("-")); + } template