diff --git a/src/lib/time/digxel.cpp b/src/lib/time/digxel.cpp index 5c8046de7..ddf441561 100644 --- a/src/lib/time/digxel.cpp +++ b/src/lib/time/digxel.cpp @@ -21,12 +21,7 @@ * *****************************************************/ -#include "lib/lumitime.hpp" -#include "lib/time/timecode.hpp" -#include "lib/time/timequant.hpp" -#include "lib/time/formats.hpp" - -using std::string; +#include "lib/time/digxel.hpp" namespace lib { diff --git a/src/lib/time/digxel.hpp b/src/lib/time/digxel.hpp index 6cdc3888e..7d4e37d3a 100644 --- a/src/lib/time/digxel.hpp +++ b/src/lib/time/digxel.hpp @@ -63,10 +63,12 @@ #define LIB_TIME_DIGXEL_H #include "lib/error.hpp" -//#include "lib/symbol.hpp" +#include "lib/symbol.hpp" -//#include -#include ////////////////////TODO +#include +#include +#include ///////////TODO +#include #include @@ -85,38 +87,97 @@ namespace time { * - will then format these numbers and cache the formatted representation * - can store and invoke a mutation functor * - * @param TODO + * @note comparisons are assumed to be not performance relevant * @see lib::time::TCode * @todo WIP-WIP-WIP */ class Digxel + : public boost::totally_ordered > > { + typedef const char* CBuf; public: virtual ~Digxel (); ///< this is an ABC - + + operator int() const { return getIntValue(); } + operator double() const { return getDoubleValue(); } + + CBuf show() { return getFormatted(); } + void operator= (int i) { return changeTo(i); } + void operator= (double d){ return changeTo(d); } + + + // Supporting totally_ordered + bool operator< (Digxel const& o) const { return double(*this) < double(o); } + bool operator== (Digxel const& o) const { return double(*this) == double(o); } + bool operator== (int i) const { return int(*this) == i ; } + bool operator< (int i) const { return int(*this) < i ; } + bool operator> (int i) const { return int(*this) > i ; } + bool operator== (double d) const { return double(*this) == d ; } + bool operator< (double d) const { return double(*this) < d ; } + bool operator> (double d) const { return double(*this) > d ; } + + protected: + virtual int getIntValue() const =0; + virtual double getDoubleValue() const =0; + virtual CBuf getFormatted() =0; + virtual void changeTo (int i) =0; + virtual void changeTo (double d) =0; }; namespace digxel { -// using lib::Literal; + using std::string; + using lib::Literal; + using boost::lexical_cast; + + template - struct PrintfFormatter + struct ValTrait; + + template<> + struct ValTrait { - enum{ len = 6 - , bufsiz = len+1 - }; + static int asInt (int val) { return val; } + static double asDouble (int val) { return val; } + }; + + template<> + struct ValTrait + { + static int asInt (double val) { return std::floor(0.5+val); } ///< in accordance with Lumiera's time handling RfC + static double asDouble (double val) { return val; } + }; + + + + template + class PrintfFormatter + { + enum{ bufsiz = len+1 }; char printbuffer_[bufsiz]; + Literal formatSpec_; - static void + public: + PrintfFormatter (Literal fmt) + : printbuffer_() + , formatSpec_(fmt) + { + printbuffer_[0] = '\0'; + } + + void show (NUM val) { size_t space = std::snprintf (printbuffer_, bufsiz, "%5d", val); REQUIRE (space <= bufsiz, "Digxel value exceeded available buffer size. " - "For showing %d, %d chars instead of just %d would be required." - , val, space, bufsiz); + "For showing %s, %d chars instead of just %d would be required." + , lexical_cast(val), space, bufsiz); } }; @@ -125,11 +186,17 @@ namespace time { template<> struct Formatter - : PrintfFormatter + : PrintfFormatter { - enum{ len = 6 - , bufsiz = len+1 - }; + Formatter() : PrintfFormatter("%5d") { } + + }; + + template<> + struct Formatter + : PrintfFormatter + { + Formatter() : PrintfFormatter("%06.3f") { } }; @@ -150,12 +217,46 @@ namespace time { FMT buffer_; NUM value_; + + /* === Digxel implementation === */ + int + getIntValue() const + { + return ValTrait::asInt (value_); + } + + double + getDoubleValue() const + { + return ValTrait::asDouble (value_); + } + + CBuf + getFormatted() + { + UNIMPLEMENTED("call formatting or cache"); + } + + void + changeTo (int i) + { + UNIMPLEMENTED("mutate INT"); + } + + void + changeTo (double d) + { + UNIMPLEMENTED("mutate FLOAT"); + } + + public: Holder () : buffer_() , value_() { } - + + using Digxel::operator=; }; } diff --git a/tests/lib/time/digxel-test.cpp b/tests/lib/time/digxel-test.cpp index e5fffccc2..5a76553f3 100644 --- a/tests/lib/time/digxel-test.cpp +++ b/tests/lib/time/digxel-test.cpp @@ -141,7 +141,7 @@ namespace test{ digi = arbitrary; // invoke the mutation functor CHECK (sum == checksum, "divergence after adding %f in iteration %d", arbitrary, i); - CHECK (digi == abitrary); + CHECK (digi == arbitrary); } }