]> git.tdb.fi Git - r2c2.git/commitdiff
Use ArticleNumber in shoppinglist
authorMikko Rasa <tdb@tdb.fi>
Tue, 2 Nov 2010 05:49:11 +0000 (05:49 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 2 Nov 2010 05:49:11 +0000 (05:49 +0000)
Some other refactoring as well

Build
source/shoppinglist/main.cpp

diff --git a/Build b/Build
index 9be8021817cb64ba9e1242a9a78a2389f616e908..a9b5308fdab52e2ae038501f280afb0d3b2d2bc9 100644 (file)
--- a/Build
+++ b/Build
@@ -6,6 +6,11 @@ package "märklin"
        program "shoppinglist"
        {
                source "source/shoppinglist";
+               build_info
+               {
+                       incpath "source";
+                       library "marklin";
+               };
                require "mspdatafile";
        };
 
index f71122239d3eb077e026809e14ecd4e3ccebd223..2b8f5bf00abd7249768b37f5526f2b1cbd87ec79 100644 (file)
@@ -6,19 +6,19 @@ Distributed under the GPL
 */
 
 #include <map>
+#include <msp/core/application.h>
 #include <msp/core/getopt.h>
 #include <msp/datafile/loader.h>
 #include <msp/datafile/parser.h>
 #include <msp/io/print.h>
+#include "libmarklin/articlenumber.h"
 
 using namespace std;
 using namespace Msp;
+using namespace Marklin;
 
-class ShoppingList
+class ShoppingList: public Application
 {
-public:
-       ShoppingList(int, char **);
-       void print(IO::Base &);
 private:
        class InventoryLoader: public DataFile::Loader
        {
@@ -27,7 +27,7 @@ private:
        private:
                ShoppingList &sl;
 
-               void track(unsigned, int);
+               void track(ArticleNumber, int);
        };
 
        class LayoutLoader: public DataFile::Loader
@@ -37,98 +37,104 @@ private:
        private:
                ShoppingList &sl;
 
-               void track(unsigned);
+               void track(ArticleNumber);
        };
 
-       map<unsigned, unsigned> inventory;
-       map<unsigned, unsigned> layout;
+       map<ArticleNumber, unsigned> inventory;
+       map<ArticleNumber, unsigned> layout;
+
+       static Application::RegApp<ShoppingList> reg;
 
+public:
+       ShoppingList(int, char **);
+       int main();
+private:
        void load_inventory(const string &);
        void load_layout(const string &);
 };
 
-int main(int argc, char **argv)
-{
-       ShoppingList sl(argc, argv);
-       sl.print(IO::cout);
-       return 0;
-}
+
+Application::RegApp<ShoppingList> ShoppingList::reg;
 
 ShoppingList::ShoppingList(int argc, char **argv)
 {
-       string inv_fn="inventory";
+       string inv_fn = "inventory";
        GetOpt getopt;
        getopt.add_option('i', "inventory", inv_fn, GetOpt::REQUIRED_ARG);
-       getopt(argc,argv);
+       getopt(argc, argv);
 
        load_inventory(inv_fn);
        load_layout(getopt.get_args().front());
 }
 
-void ShoppingList::load_inventory(const string &fn)
-{
-       IO::File in(fn);
-       DataFile::Parser parser(in, fn);
-       InventoryLoader il(*this);
-       il.load(parser);
-}
-
-void ShoppingList::load_layout(const string &fn)
-{
-       IO::File in(fn);
-       DataFile::Parser parser(in, fn);
-       LayoutLoader ll(*this);
-       ll.load(parser);
-}
-
-void ShoppingList::print(IO::Base &out)
+int ShoppingList::main()
 {
-       IO::print(out, "// Need to get:\n");
-       for(map<unsigned, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
+       IO::print("// Need to get:\n");
+       for(map<ArticleNumber, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
        {
-               map<unsigned, unsigned>::iterator j=inventory.find(i->first);
+               map<ArticleNumber, unsigned>::iterator j=inventory.find(i->first);
                if(j!=inventory.end())
                {
                        if(j->second<i->second)
-                               IO::print(out, "track %d %d;\n", i->first, i->second-j->second);
+                               IO::print("track %d %d;\n", i->first, i->second-j->second);
                }
                else
-                       IO::print(out, "track %d %d;\n", i->first, i->second);
+                       IO::print("track %d %d;\n", i->first, i->second);
        }
 
-       IO::print(out, "// Pre-existing:\n");
-       for(map<unsigned, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
+       IO::print("// Pre-existing:\n");
+       for(map<ArticleNumber, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
        {
-               map<unsigned, unsigned>::iterator j=inventory.find(i->first);
+               map<ArticleNumber, unsigned>::iterator j=inventory.find(i->first);
                if(j!=inventory.end())
-                       IO::print(out, "track %d %d;\n", i->first, min(i->second, j->second));
+                       IO::print("track %d %d;\n", i->first, min(i->second, j->second));
        }
 
-       IO::print(out, "// Unused:\n");
-       for(map<unsigned, unsigned>::iterator i=inventory.begin(); i!=inventory.end(); ++i)
+       IO::print("// Unused:\n");
+       for(map<ArticleNumber, unsigned>::iterator i=inventory.begin(); i!=inventory.end(); ++i)
        {
-               map<unsigned, unsigned>::iterator j=layout.find(i->first);
+               map<ArticleNumber, unsigned>::iterator j=layout.find(i->first);
                if(j!=layout.end())
                {
                        if(j->second<i->second)
-                               IO::print(out, "track %d %d;\n", i->first, i->second-j->second);
+                               IO::print("track %d %d;\n", i->first, i->second-j->second);
                }
                else
-                       IO::print(out, "track %d %d;\n", i->first, i->second);
+                       IO::print("track %d %d;\n", i->first, i->second);
        }
+
+       return 0;
 }
 
+void ShoppingList::load_inventory(const string &fn)
+{
+       IO::File in(fn);
+       DataFile::Parser parser(in, fn);
+       InventoryLoader il(*this);
+       il.load(parser);
+}
+
+void ShoppingList::load_layout(const string &fn)
+{
+       IO::File in(fn);
+       DataFile::Parser parser(in, fn);
+       LayoutLoader ll(*this);
+       ll.load(parser);
+}
+
+
 ShoppingList::InventoryLoader::InventoryLoader(ShoppingList &s):
        sl(s)
 {
        add("track", &InventoryLoader::track);
 }
 
-void ShoppingList::InventoryLoader::track(unsigned part, int count)
+void ShoppingList::InventoryLoader::track(ArticleNumber art_nr, int count)
 {
-       sl.inventory[part]+=count;
+       sl.inventory[art_nr] += count;
 }
 
+
 ShoppingList::LayoutLoader::LayoutLoader(ShoppingList &s):
        sl(s)
 {
@@ -137,7 +143,7 @@ ShoppingList::LayoutLoader::LayoutLoader(ShoppingList &s):
        add("route");
 }
 
-void ShoppingList::LayoutLoader::track(unsigned part)
+void ShoppingList::LayoutLoader::track(ArticleNumber art_nr)
 {
-       ++sl.layout[part];
+       ++sl.layout[art_nr];
 }