]> www.ginac.de Git - ginac.git/commitdiff
Added callback mechanism to watch changes to 'Digits' (to be used for look-up tables)
authorJens Vollinga <vollinga@thep.physik.uni-mainz.de>
Thu, 10 Nov 2005 17:36:28 +0000 (17:36 +0000)
committerJens Vollinga <vollinga@thep.physik.uni-mainz.de>
Thu, 10 Nov 2005 17:36:28 +0000 (17:36 +0000)
ginac/numeric.cpp
ginac/numeric.h

index 9670c6b36e716eaebca1477ef53ef8f635580403..f62b0d2541538bf4b8e427f8f63291b2e1e1f271 100644 (file)
@@ -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);
                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)
 {
 }
 
 
 /** Assign a native long to global Digits object. */
 _numeric_digits& _numeric_digits::operator=(long prec)
 {
+       long digitsdiff = prec - digits;
        digits = prec;
        digits = prec;
-       cln::default_float_format = cln::float_format(prec); 
+       cln::default_float_format = cln::float_format(prec);
+
+       // call registered callbacks
+       std::vector<digits_changed_callback>::const_iterator it = callbacklist.begin(), end = callbacklist.end();
+       for (; it != end; ++it) {
+               (*it)(digitsdiff);
+       }
+
        return *this;
 }
 
        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);
 std::ostream& operator<<(std::ostream &os, const _numeric_digits &e)
 {
        e.print(os);
index 188883159942e3fde1e96c9d6f8f9d9d7b08677f..f2a1adbdc52d8cc070a15c8ad4460fa151d6db3e 100644 (file)
@@ -27,6 +27,7 @@
 #include "ex.h"
 
 #include <stdexcept>
 #include "ex.h"
 
 #include <stdexcept>
+#include <vector>
 
 #include <cln/complex.h>
 
 
 #include <cln/complex.h>
 
 
 namespace GiNaC {
 
 
 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
 /** 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();
        _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
 // 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<digits_changed_callback> callbacklist;
 };
 
 
 };