From 18cb466b6bf4c3d8d026c3d644c1452026869f3d Mon Sep 17 00:00:00 2001 From: guy Date: Mon, 17 Oct 2005 10:10:18 +0000 Subject: [PATCH] Patch level : 2.2 Files correlati : Ricompilazione Demo : [ ] Commento : Migliorata ed elegantita gestione cache delle unita' di misura git-svn-id: svn://10.65.10.50/trunk@13425 c028cbd2-c16b-5b4b-a496-9718f37d4682 --- db/dblib.cpp | 58 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/db/dblib.cpp b/db/dblib.cpp index 7d5326b2a..124822da7 100755 --- a/db/dblib.cpp +++ b/db/dblib.cpp @@ -13,31 +13,53 @@ #include "../mg/rmovmag.h" #include "../include/rdoc.h" +/////////////////////////////////////////////////////////// +// Cache fattori di conversione +/////////////////////////////////////////////////////////// + +class TConversionCache : public TCache +{ + TLocalisamfile _umart; + +private: + virtual TObject* key2obj(const char* key); + +public: + TConversionCache(); +}; + +TObject* TConversionCache::key2obj(const char* key) +{ + TToken_string str = key; + _umart.put(UMART_CODART, str.get(0)); + _umart.put(UMART_UM, str.get()); + _umart.setkey(2); + + real num; + if (_umart.read() == NOERR) + num = _umart.get_real(UMART_FC); + else + num = UNO; // Should never happen + return new fraction(num, UNO); +} + +TConversionCache::TConversionCache() + : _umart(LF_UMART) +{ } + /////////////////////////////////////////////////////////// // TQuantita' /////////////////////////////////////////////////////////// const fraction& TQuantita::get_factor(const TCodice_um& um) const { - static TAssoc_array factors; + static TConversionCache* factors = NULL; + if (factors == NULL) + factors = new TConversionCache; TString80 code; - code << _articolo << '|' << (um.empty() ? _um : um); - - fraction* conv = (fraction*)factors.objptr(code); - if (conv == NULL) - { - TLocalisamfile umart(LF_UMART); - umart.setkey(2); - umart.put(UMART_CODART, _articolo); - umart.put(UMART_UM, um.empty() ? _um : um); - if (umart.read() == NOERR) - conv = new fraction(umart.get_real(UMART_FC), UNO); - else - conv = new fraction(1,1); - factors.add(code, conv); - } - + code << _articolo << '|' << (um.blank() ? _um : um); + const fraction* conv = (const fraction*)factors->objptr(code); return *conv; } @@ -91,7 +113,7 @@ void TQuantita::convert(real& val, const TCodice_um& from_um, const TCodice_um& { const fraction& mul = get_factor(from_um); const fraction& div = get_factor(to_um); - val = fraction(val, UNO) * mul / div; + val = mul / div * fraction(val, UNO); TArticolo::round_um(val, to_um); } }