From: Jens Vollinga Date: Thu, 10 Nov 2005 17:36:28 +0000 (+0000) Subject: Added callback mechanism to watch changes to 'Digits' (to be used for look-up tables) X-Git-Tag: release_1-4-0~133 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=6715b097ae926fecfc62e53d4af38b7217634908 Added callback mechanism to watch changes to 'Digits' (to be used for look-up tables) --- diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp index 9670c6b3..f62b0d25 100644 --- a/ginac/numeric.cpp +++ b/ginac/numeric.cpp @@ -1999,14 +1999,25 @@ _numeric_digits::_numeric_digits() throw(std::runtime_error("I told you not to do instantiate me!")); too_late = true; cln::default_float_format = cln::float_format(17); + + // add callbacks for built-in functions + // like ... add_callback(Li_lookuptable); } /** Assign a native long to global Digits object. */ _numeric_digits& _numeric_digits::operator=(long prec) { + long digitsdiff = prec - digits; digits = prec; - cln::default_float_format = cln::float_format(prec); + cln::default_float_format = cln::float_format(prec); + + // call registered callbacks + std::vector::const_iterator it = callbacklist.begin(), end = callbacklist.end(); + for (; it != end; ++it) { + (*it)(digitsdiff); + } + return *this; } @@ -2026,6 +2037,13 @@ void _numeric_digits::print(std::ostream &os) const } +/** Add a new callback function. */ +void _numeric_digits::add_callback(digits_changed_callback callback) +{ + callbacklist.push_back(callback); +} + + std::ostream& operator<<(std::ostream &os, const _numeric_digits &e) { e.print(os); diff --git a/ginac/numeric.h b/ginac/numeric.h index 18888315..f2a1adbd 100644 --- a/ginac/numeric.h +++ b/ginac/numeric.h @@ -27,6 +27,7 @@ #include "ex.h" #include +#include #include @@ -39,6 +40,12 @@ namespace GiNaC { +/** Function pointer to implement callbacks in the case 'Digits' gets changed. + * Main purpose of such callbacks is to adjust look-up tables of certain + * functions to the new precision. Parameter contains the signed difference + * between new Digits and old Digits. */ +typedef void (* digits_changed_callback)(long); + /** This class is used to instantiate a global singleton object Digits * which behaves just like Maple's Digits. We need an object rather * than a dumber basic type since as a side-effect we let it change @@ -55,11 +62,14 @@ public: _numeric_digits(); _numeric_digits& operator=(long prec); operator long(); - void print(std::ostream &os) const; + void print(std::ostream& os) const; + void add_callback(digits_changed_callback callback); // member variables private: long digits; ///< Number of decimal digits static bool too_late; ///< Already one object present + // Holds a list of functions that get called when digits is changed. + std::vector callbacklist; };