lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [4833] Reimplement 'funds' and 'rounding' product files (V


From: Greg Chicares
Subject: [lmi-commits] [4833] Reimplement 'funds' and 'rounding' product files (VS)
Date: Wed, 07 Apr 2010 11:44:31 +0000

Revision: 4833
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=4833
Author:   chicares
Date:     2010-04-07 11:44:30 +0000 (Wed, 07 Apr 2010)
Log Message:
-----------
Reimplement 'funds' and 'rounding' product files (VS)

Modified Paths:
--------------
    lmi/trunk/ChangeLog
    lmi/trunk/ihs_funddata.cpp
    lmi/trunk/ihs_funddata.hpp
    lmi/trunk/ihs_rnddata.cpp
    lmi/trunk/ihs_rnddata.hpp
    lmi/trunk/main_wx.cpp
    lmi/trunk/my_prod.cpp
    lmi/trunk/my_rnd.cpp
    lmi/trunk/objects.make
    lmi/trunk/product_data.cpp
    lmi/trunk/xml_serialize.hpp

Modified: lmi/trunk/ChangeLog
===================================================================
--- lmi/trunk/ChangeLog 2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/ChangeLog 2010-04-07 11:44:30 UTC (rev 4833)
@@ -24804,3 +24804,17 @@
   mc_enum_types_aux.hpp
 Replace overloaded mc_str() with a template (thanks to VS).
 
+20100407T1144Z <address@hidden> [760]
+
+  ihs_funddata.cpp
+  ihs_funddata.hpp
+  ihs_rnddata.cpp
+  ihs_rnddata.hpp
+  main_wx.cpp
+  my_prod.cpp
+  my_rnd.cpp
+  objects.make
+  product_data.cpp
+  xml_serialize.hpp
+Reimplement 'funds' and 'rounding' product files (VS).
+

Modified: lmi/trunk/ihs_funddata.cpp
===================================================================
--- lmi/trunk/ihs_funddata.cpp  2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/ihs_funddata.cpp  2010-04-07 11:44:30 UTC (rev 4833)
@@ -1,4 +1,4 @@
-// Fund data.
+// Fund names and investment-management fees.
 //
 // Copyright (C) 1998, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
2010 Gregory W. Chicares.
 //
@@ -21,10 +21,6 @@
 
 // $Id$
 
-// This class describes funds: their names and investment mgmt fees.
-// TODO ?? An extension other than .fnd would be preferable: msw uses
-// .fnd for "find"
-
 #ifdef __BORLANDC__
 #   include "pchfile.hpp"
 #   pragma hdrstop
@@ -36,8 +32,11 @@
 #include "assert_lmi.hpp"
 #include "data_directory.hpp"
 #include "platform_dependent.hpp" // access()
+#include "xml_lmi.hpp"
+#include "xml_serialize.hpp"
 
-#include <fstream>
+#include <boost/filesystem/convenience.hpp>
+#include <boost/filesystem/path.hpp>
 
 //============================================================================
 FundInfo::FundInfo()
@@ -64,6 +63,30 @@
 {
 }
 
+namespace xml_serialize
+{
+template<> struct xml_io<FundInfo>
+{
+    static void to_xml(xml::element& e, FundInfo const& t)
+    {
+        set_element(e, "scalar_imf", t.ScalarIMF());
+        set_element(e, "short_name", t.ShortName());
+        set_element(e, "long_name" , t.LongName ());
+    }
+
+    static void from_xml(xml::element const& e, FundInfo& t)
+    {
+        double      scalar_imf;
+        std::string short_name;
+        std::string long_name;
+        get_element(e, "scalar_imf", scalar_imf);
+        get_element(e, "short_name", short_name);
+        get_element(e, "long_name" , long_name );
+        t = FundInfo(scalar_imf, short_name, long_name);
+    }
+};
+} // namespace xml_serialize
+
 //============================================================================
 FundData::FundData()
 {
@@ -80,6 +103,14 @@
 {
 }
 
+namespace
+{
+std::string xml_root_name()
+{
+    return "funds";
+}
+} // Unnamed namespace.
+
 //============================================================================
 void FundData::Read(std::string const& a_Filename)
 {
@@ -92,63 +123,29 @@
             << LMI_FLUSH
             ;
         }
-    std::ifstream is(a_Filename.c_str());
 
-    LMI_ASSERT(0 == FundInfo_.size());
-    for(;;)
-        {
-        if(EOF == is.peek())
-            {
-            break;
-            }
+    xml_lmi::dom_parser parser(a_Filename);
+    xml::element const& root = parser.root_node(xml_root_name());
 
-        FundInfo f;
-        is >> f.ScalarIMF_;
-        // First, a dummy call to eat the tab after the double.
-        std::string sink;
-        std::getline(is, sink, '\t');
-        std::getline(is, f.ShortName_, '\t');
-        std::getline(is, f.LongName_, '\n');
-        if(!is.good())
-            {
-            fatal_error()
-                << "Error reading fund file '"
-                << a_Filename
-                << "'. Try reinstalling."
-                << LMI_FLUSH
-                ;
-            }
-        FundInfo_.push_back(f);
-        }
+    xml_serialize::from_xml(root, FundInfo_);
 }
 
 //============================================================================
-void FundData::Write(std::string const& a_Filename)
+void FundData::Write(std::string const& a_Filename) const
 {
-    std::ofstream os(a_Filename.c_str());
+    xml_lmi::xml_document document(xml_root_name());
+    xml::element& root = document.root_node();
 
-    std::vector<FundInfo>::const_iterator i = FundInfo_.begin();
-    for(;i != FundInfo_.end(); i++)
-        {
-        os
-            << i->ScalarIMF_
-            << '\t'
-            << i->ShortName_
-            << '\t'
-            << i->LongName_
-            << '\n'
-            ;
-        }
+    xml_lmi::set_attr(root, "version", "0");
+    xml_serialize::to_xml(root, FundInfo_);
 
-    if(!os.good())
-        {
-        fatal_error()
-            << "Unable to write fund file '"
-            << a_Filename
-            << "'."
-            << LMI_FLUSH
-            ;
-        }
+    // Instead of this:
+//    document.save(a_Filename);
+    // for the nonce, explicitly change the extension, in order to
+    // force external product-file code to use the new extension.
+    fs::path path(a_Filename, fs::native);
+    path = fs::change_extension(path, ".funds");
+    document.save(path.string());
 }
 
 //============================================================================
@@ -162,6 +159,6 @@
             ,"Money Market Fund"
             )
         );
-    foo.Write(AddDataDir("sample.fnd"));
+    foo.Write(AddDataDir("sample.funds"));
 }
 

Modified: lmi/trunk/ihs_funddata.hpp
===================================================================
--- lmi/trunk/ihs_funddata.hpp  2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/ihs_funddata.hpp  2010-04-07 11:44:30 UTC (rev 4833)
@@ -1,4 +1,4 @@
-// Fund data.
+// Fund names and investment-management fees.
 //
 // Copyright (C) 1998, 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010 
Gregory W. Chicares.
 //
@@ -80,7 +80,7 @@
     FundData(); // Private, but implemented.
 
     void Read (std::string const& a_Filename);
-    void Write(std::string const& a_Filename);
+    void Write(std::string const& a_Filename) const;
 
     std::vector<FundInfo> FundInfo_;
 };

Modified: lmi/trunk/ihs_rnddata.cpp
===================================================================
--- lmi/trunk/ihs_rnddata.cpp   2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/ihs_rnddata.cpp   2010-04-07 11:44:30 UTC (rev 4833)
@@ -1,4 +1,4 @@
-// Rounding data.
+// Rounding rules.
 //
 // Copyright (C) 1998, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
2010 Gregory W. Chicares.
 //
@@ -32,10 +32,36 @@
 
 #include "alert.hpp"
 #include "data_directory.hpp"
+#include "mc_enum.hpp"
+#include "mc_enum_types.hpp"
 #include "platform_dependent.hpp" // access()
+#include "xml_lmi.hpp"
+#include "xml_serialize.hpp"
 
-#include <fstream>
+#include <boost/filesystem/convenience.hpp>
+#include <boost/filesystem/path.hpp>
 
+namespace xml_serialize
+{
+template<> struct xml_io<round_to<double> >
+{
+    static void to_xml(xml::element& e, round_to<double> const& t)
+    {
+        set_element(e, "decimals",                    t.decimals() );
+        set_element(e, "style"   , mce_rounding_style(t.style   ()));
+    }
+
+    static void from_xml(xml::element const& e, round_to<double>& t)
+    {
+        int                decimals;
+        mce_rounding_style style   ;
+        get_element(e, "decimals", decimals);
+        get_element(e, "style"   , style   );
+        t = round_to<double>(decimals, style.value());
+    }
+};
+} // namespace xml_serialize
+
 //============================================================================
 StreamableRoundingRules::StreamableRoundingRules()
 {
@@ -67,22 +93,10 @@
 
 namespace
 {
-    inline std::istream& operator>>(std::istream& is, round_to<double>& r)
-        {
-        int decimals;
-        is >> decimals;
-        int z;
-        is >> z;
-        rounding_style style = static_cast<rounding_style>(z);
-        r = round_to<double>(decimals, style);
-        return is;
-        }
-    inline std::ostream& operator<<(std::ostream& os, round_to<double> const& 
r)
-        {
-        os << r.decimals() << '\n';
-        os << r.style() << '\n';
-        return os;
-        }
+std::string xml_root_name()
+{
+    return "rounding";
+}
 } // Unnamed namespace.
 
 //============================================================================
@@ -94,84 +108,73 @@
             << "File '"
             << a_Filename
             << "' is required but could not be found. Try reinstalling."
+            << LMI_FLUSH
             ;
         }
-    std::ifstream is(a_Filename.c_str());
 
-    is >> round_specamt_;
-    is >> round_death_benefit_;
-    is >> round_naar_;
-    is >> round_coi_rate_;
-    is >> round_coi_charge_;
-    is >> round_gross_premium_;
-    is >> round_net_premium_;
-    is >> round_interest_rate_;
-    is >> round_interest_credit_;
-    is >> round_withdrawal_;
-    is >> round_loan_;
-    is >> round_corridor_factor_;
-    is >> round_surrender_charge_;
-    is >> round_irr_;
+    xml_lmi::dom_parser parser(a_Filename);
+    xml::element const& root = parser.root_node(xml_root_name());
 
-    bool okay = is.good();
-    if(!okay)
-        {
-        fatal_error()
-            << "Unexpected end of rounding file '"
-            << a_Filename
-            << "'. Try reinstalling."
-            << LMI_FLUSH
-            ;
-        }
-    std::string dummy;
-    is >> dummy;
-    okay = is.eof();
-    if(!okay)
-        {
-        fatal_error()
-            << "Data past expected end of rounding file '"
-            << a_Filename
-            << "'. Try reinstalling."
-            << LMI_FLUSH
-            ;
-        }
+#   define GET_ELEMENT(name) xml_serialize::get_element(root, #name, 
round_##name##_)
+
+    GET_ELEMENT(specamt         );
+    GET_ELEMENT(death_benefit   );
+    GET_ELEMENT(naar            );
+    GET_ELEMENT(coi_rate        );
+    GET_ELEMENT(coi_charge      );
+    GET_ELEMENT(gross_premium   );
+    GET_ELEMENT(net_premium     );
+    GET_ELEMENT(interest_rate   );
+    GET_ELEMENT(interest_credit );
+    GET_ELEMENT(withdrawal      );
+    GET_ELEMENT(loan            );
+    GET_ELEMENT(corridor_factor );
+    GET_ELEMENT(surrender_charge);
+    GET_ELEMENT(irr             );
+
+#   undef GET_ELEMENT
 }
 
 //============================================================================
-void StreamableRoundingRules::Write(std::string const& a_Filename)
+void StreamableRoundingRules::Write(std::string const& a_Filename) const
 {
-    std::ofstream os(a_Filename.c_str());
+    xml_lmi::xml_document document(xml_root_name());
+    xml::element& root = document.root_node();
 
-    os << round_specamt_;
-    os << round_death_benefit_;
-    os << round_naar_;
-    os << round_coi_rate_;
-    os << round_coi_charge_;
-    os << round_gross_premium_;
-    os << round_net_premium_;
-    os << round_interest_rate_;
-    os << round_interest_credit_;
-    os << round_withdrawal_;
-    os << round_loan_;
-    os << round_corridor_factor_;
-    os << round_surrender_charge_;
-    os << round_irr_;
+    xml_lmi::set_attr(root, "version", "0");
 
-    if(!os.good())
-        {
-        fatal_error()
-            << "Unable to write rounding file '"
-            << a_Filename
-            << "'."
-            << LMI_FLUSH
-            ;
-        }
+#   define SET_ELEMENT(name) xml_serialize::set_element(root, #name, 
round_##name##_)
+
+    SET_ELEMENT(specamt         );
+    SET_ELEMENT(death_benefit   );
+    SET_ELEMENT(naar            );
+    SET_ELEMENT(coi_rate        );
+    SET_ELEMENT(coi_charge      );
+    SET_ELEMENT(gross_premium   );
+    SET_ELEMENT(net_premium     );
+    SET_ELEMENT(interest_rate   );
+    SET_ELEMENT(interest_credit );
+    SET_ELEMENT(withdrawal      );
+    SET_ELEMENT(loan            );
+    SET_ELEMENT(corridor_factor );
+    SET_ELEMENT(surrender_charge);
+    SET_ELEMENT(irr             );
+
+#   undef SET_ELEMENT
+
+    // Instead of this:
+//    document.save(a_Filename);
+    // for the nonce, explicitly change the extension, in order to
+    // force external product-file code to use the new extension.
+    fs::path path(a_Filename, fs::native);
+    path = fs::change_extension(path, ".rounding");
+    document.save(path.string());
 }
 
 //============================================================================
 void StreamableRoundingRules::WriteRndFiles()
 {
     StreamableRoundingRules sample;
-    sample.Write(AddDataDir("sample.rnd"));
+    sample.Write(AddDataDir("sample.rounding"));
 }
 

Modified: lmi/trunk/ihs_rnddata.hpp
===================================================================
--- lmi/trunk/ihs_rnddata.hpp   2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/ihs_rnddata.hpp   2010-04-07 11:44:30 UTC (rev 4833)
@@ -1,4 +1,4 @@
-// Rounding rules, with document storage.
+// Rounding rules.
 //
 // Copyright (C) 1998, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
2010 Gregory W. Chicares.
 //
@@ -50,7 +50,7 @@
 
     void Init(std::string const& a_Filename);
     void Read(std::string const& a_Filename);
-    void Write(std::string const& a_Filename);
+    void Write(std::string const& a_Filename) const;
 };
 
 #endif // ihs_rnddata_hpp

Modified: lmi/trunk/main_wx.cpp
===================================================================
--- lmi/trunk/main_wx.cpp       2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/main_wx.cpp       2010-04-07 11:44:30 UTC (rev 4833)
@@ -388,9 +388,9 @@
     new(wx) wxDocTemplate
         (doc_manager_
         ,"Rounding"
-        ,"*.rnd"
+        ,"*.rounding"
         ,""
-        ,"rnd"
+        ,"rounding"
         ,"Rounding document"
         ,"Rounding view"
         ,CLASSINFO(RoundingDocument)

Modified: lmi/trunk/my_prod.cpp
===================================================================
--- lmi/trunk/my_prod.cpp       2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/my_prod.cpp       2010-04-07 11:44:30 UTC (rev 4833)
@@ -62,8 +62,8 @@
     // Generic data for the 'sample' product.
 
     z.DatabaseFilename               = "sample.db4";
-    z.FundFilename                   = "sample.fnd";
-    z.RoundingFilename               = "sample.rnd";
+    z.FundFilename                   = "sample.funds";
+    z.RoundingFilename               = "sample.rounding";
     z.TierFilename                   = "sample.strata";
 
     z.CorridorFilename               = "sample";

Modified: lmi/trunk/my_rnd.cpp
===================================================================
--- lmi/trunk/my_rnd.cpp        2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/my_rnd.cpp        2010-04-07 11:44:30 UTC (rev 4833)
@@ -68,7 +68,7 @@
     sample.round_surrender_charge_= round_to<double>(2, r_to_nearest);
     sample.round_irr_             = round_to<double>(5, r_downward  );
 
-    sample.Write(AddDataDir("sample.rnd"));
+    sample.Write(AddDataDir("sample.rounding"));
 
     // Another policy form....
 }

Modified: lmi/trunk/objects.make
===================================================================
--- lmi/trunk/objects.make      2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/objects.make      2010-04-07 11:44:30 UTC (rev 4833)
@@ -776,8 +776,10 @@
   $(common_test_objects) \
   $(xmlwrapp_objects) \
   data_directory.o \
+  datum_base.o \
   dbnames.o \
   expm1.o \
+  facets.o \
   global_settings.o \
   ihs_dbdict.o \
   ihs_dbvalue.o \
@@ -785,6 +787,8 @@
   ihs_funddata.o \
   ihs_pios.o \
   ihs_rnddata.o \
+  mc_enum.o \
+  mc_enum_types.o \
   miscellany.o \
   path_utility.o \
   product_data.o \

Modified: lmi/trunk/product_data.cpp
===================================================================
--- lmi/trunk/product_data.cpp  2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/product_data.cpp  2010-04-07 11:44:30 UTC (rev 4833)
@@ -247,8 +247,8 @@
     product_data z;
 
     z.DatabaseFilename               = "sample.db4";
-    z.FundFilename                   = "sample.fnd";
-    z.RoundingFilename               = "sample.rnd";
+    z.FundFilename                   = "sample.funds";
+    z.RoundingFilename               = "sample.rounding";
     z.TierFilename                   = "sample.strata";
 
     z.CorridorFilename               = "sample";

Modified: lmi/trunk/xml_serialize.hpp
===================================================================
--- lmi/trunk/xml_serialize.hpp 2010-04-05 23:45:21 UTC (rev 4832)
+++ lmi/trunk/xml_serialize.hpp 2010-04-07 11:44:30 UTC (rev 4833)
@@ -170,7 +170,7 @@
 template<typename T>
 void from_xml(xml::element const& e, T& t)
 {
-    xml_io<T>::from_xml(t, e);
+    xml_io<T>::from_xml(e, t);
 }
 } // Namespace xml_serialize.
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]