]> git.tdb.fi Git - r2c2.git/commitdiff
Add state loading and saving to engineer
authorMikko Rasa <tdb@tdb.fi>
Thu, 21 May 2009 19:52:04 +0000 (19:52 +0000)
committerMikko Rasa <tdb@tdb.fi>
Thu, 21 May 2009 19:52:04 +0000 (19:52 +0000)
Modernize all load/save code

20 files changed:
source/designer/designer.cpp
source/engineer/engineer.cpp
source/libmarklin/catalogue.cpp
source/libmarklin/catalogue.h
source/libmarklin/constants.cpp
source/libmarklin/layout.cpp
source/libmarklin/layout.h
source/libmarklin/locotype.cpp
source/libmarklin/locotype.h
source/libmarklin/track.cpp
source/libmarklin/track.h
source/libmarklin/trackpart.cpp
source/libmarklin/trackpart.h
source/libmarklin/tracktype.cpp
source/libmarklin/tracktype.h
source/libmarklin/trafficmanager.cpp
source/libmarklin/trafficmanager.h
source/libmarklin/train.cpp
source/libmarklin/train.h
source/libmarklin/turnout.cpp

index 5a7ee62ac3f6c04a7ef2f39d0f91d0937df52300..39999df2e5d69b869159f59ec06fb04e8fdcc46c 100644 (file)
@@ -70,7 +70,7 @@ Designer::Designer(int argc, char **argv):
 
        if(argc>1)
        {
-               layout->load(argv[1]);
+               DataFile::load(*layout, argv[1]);
                const list<Track3D *> &ltracks=layout_3d->get_tracks();
                for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
                {
index 5466302ca7cfbbc60955deb1b36c57ce39e85e5e..581a301d8f194df79574671c15e95d165e4714c0 100644 (file)
@@ -10,6 +10,7 @@ Distributed under the GPL
 #include <GL/gl.h>
 #include <msp/core/except.h>
 #include <msp/core/getopt.h>
+#include <msp/fs/stat.h>
 #include <msp/gbase/display.h>
 #include <msp/gbase/window.h>
 #include <msp/gl/immediate.h>
@@ -29,8 +30,6 @@ using namespace std;
 using namespace Marklin;
 using namespace Msp;
 
-#include <iostream>
-
 Engineer::Engineer(int argc, char **argv):
        screen_w(1280),
        screen_h(960),
@@ -84,10 +83,12 @@ Engineer::Engineer(int argc, char **argv):
        const vector<string> &args=getopt.get_args();
        if(args.empty())
                throw UsageError("No layout given");
-       layout.load(args.front());
+       DataFile::load(layout, args.front());
 
        trfc_mgr=new TrafficManager(control, layout);
        trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
+       if(FS::exists("engineer.state"))
+               DataFile::load(*trfc_mgr, "engineer.state");
 
        const map<unsigned, Sensor *> &sensors=control.get_sensors();
        for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
@@ -98,6 +99,7 @@ Engineer::Engineer(int argc, char **argv):
 
 Engineer::~Engineer()
 {
+       trfc_mgr->save("engineer.state");
        delete trfc_mgr;
 }
 
@@ -179,6 +181,18 @@ int Engineer::main()
        main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
        main_panel->set_visible(true);
 
+       const list<Train *> &trains=trfc_mgr->get_trains();
+       int y=main_panel->get_geometry().y;
+       for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+       {
+               TrainPanel *tpanel=new TrainPanel(*this, ui_res, **i);
+               root->add(*tpanel);
+               tpanel->set_position(0, y-tpanel->get_geometry().h);
+               train_panels.push_back(tpanel);
+               tpanel->set_visible(true);
+               y-=tpanel->get_geometry().h;
+       }
+
        wnd->show();
 
        Application::main();
index c8ed80c9c137703901238b266f0f052746d3c361..8faaa102596a2db071d23bc70135bdf227f29fcd 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -51,34 +51,30 @@ void Catalogue::load(const string &fn)
 
 
 Catalogue::Loader::Loader(Catalogue &c):
-       cat(c)
+       DataFile::BasicLoader<Catalogue>(c)
 {
        add("locomotive", &Loader::locomotive);
        add("track", &Loader::track);
 }
 
-void Catalogue::Loader::locomotive(unsigned art_no)
+void Catalogue::Loader::locomotive(unsigned art_nr)
 {
-       map<unsigned, LocoType *>::iterator i=cat.locos.find(art_no);
-       if(i!=cat.locos.end())
+       if(obj.locos.count(art_nr))
                throw Exception("Duplicate locomotive number");
 
-       RefPtr<LocoType> loco=new LocoType(art_no);
+       RefPtr<LocoType> loco=new LocoType(art_nr);
        load_sub(*loco);
-       unsigned art_nr=loco->get_article_number();
-       cat.locos[art_nr]=loco.release();
+       obj.locos[art_nr]=loco.release();
 }
 
-void Catalogue::Loader::track(unsigned art_no)
+void Catalogue::Loader::track(unsigned art_nr)
 {
-       map<unsigned, TrackType *>::iterator i=cat.tracks.find(art_no);
-       if(i!=cat.tracks.end())
+       if(obj.tracks.count(art_nr))
                throw Exception("Duplicate track number");
 
-       RefPtr<TrackType> trk=new TrackType(art_no);
+       RefPtr<TrackType> trk=new TrackType(art_nr);
        load_sub(*trk);
-       unsigned art_nr=trk->get_article_number();
-       cat.tracks[art_nr]=trk.release();
+       obj.tracks[art_nr]=trk.release();
 }
 
 } // namespace Marklin
index 7ce3e73c0d30a3d98cd252a8f0d47a726d200b88..5f13999dd26e7ed91fd648c60eec766d066d022a 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -19,13 +19,11 @@ class TrackType;
 class Catalogue
 {
 public:
-       class Loader: public Msp::DataFile::Loader
+       class Loader: public Msp::DataFile::BasicLoader<Catalogue>
        {
        public:
                Loader(Catalogue &);
        private:
-               Catalogue &cat;
-
                void locomotive(unsigned);
                void track(unsigned);
        };
index 9107361c29987fc642656a2584f256a54d3cf702..e6f5946491feb42f231c4a05bf035318fc93bf47 100644 (file)
@@ -1,3 +1,10 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2008-2009  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
 #include "constants.h"
 
 using namespace std;
index 4835f2e89bd70a7b7062710998777eb5188cea30..50eba62ab6f096b04427c3feb09e093b7858f3ef 100644 (file)
@@ -5,9 +5,9 @@ Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
-#include <fstream>
 #include <msp/core/refptr.h>
 #include <msp/datafile/parser.h>
+#include <msp/datafile/writer.h>
 #include "catalogue.h"
 #include "layout.h"
 #include "tracktype.h"
@@ -17,7 +17,7 @@ using namespace Msp;
 
 namespace Marklin {
 
-Layout::Layout(Catalogue &c):
+Layout::Layout(const Catalogue &c):
        catalogue(c)
 { }
 
@@ -66,25 +66,20 @@ void Layout::check_links()
                                (*i)->snap_to(**j, true);
 }
 
-void Layout::load(const string &fn)
+void Layout::save(const string &fn)
 {
-       IO::File in(fn);
-       IO::Buffered inb(in);
-
-       filename=fn;
-       DataFile::Parser parser(inb, fn);
-       Loader loader(*this);
-       loader.load(parser);
-
-       check_links();
-
+       IO::BufferedFile out(fn, IO::M_WRITE);
+       DataFile::Writer writer(out);
+       if(!base.empty())
+               writer.write((DataFile::Statement("base"), base));
        for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               (*i)->check_slope();
-}
-
-int Layout::save(const string &fn)
-{
-       ofstream out(fn.c_str());
+       {
+               DataFile::Statement st("track");
+               st.append((*i)->get_type().get_article_number());
+               (*i)->save(st.sub);
+               writer.write(st);
+       }
+       /*ofstream out(fn.c_str());
        if(!out) return -1;
 
        filename=fn;
@@ -113,24 +108,32 @@ int Layout::save(const string &fn)
                out<<"};\n";
        }
 
-       return 0;
+       return 0;*/
 }
 
 
 Layout::Loader::Loader(Layout &l):
-       layout(l)
+       DataFile::BasicLoader<Layout>(l)
 {
        add("base",  &Layout::base);
        add("track", &Loader::track);
 }
 
+void Layout::Loader::finish()
+{
+       obj.check_links();
+
+       for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
+               (*i)->check_slope();
+}
+
 void Layout::Loader::track(unsigned art_nr)
 {
-       TrackType &type=layout.catalogue.get_track(art_nr);
+       TrackType &type=obj.catalogue.get_track(art_nr);
 
        RefPtr<Track> trk=new Track(type);
        load_sub(*trk);
-       layout.add_track(*trk.release());
+       obj.add_track(*trk.release());
 }
 
 } // namespace Marklin
index ae03cef6f30a2ca970656f7a6a9662255595ef9d..f603d39f2f0b8ad3e4262e9b583254c4e13dc6eb 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -20,20 +20,17 @@ class Catalogue;
 class Layout
 {
 public:
-       class Loader: public Msp::DataFile::Loader
+       class Loader: public Msp::DataFile::BasicLoader<Layout>
        {
        public:
                Loader(Layout &);
-               Layout &get_object() { return layout; }
        private:
-               Layout &layout;
-
+               virtual void finish();
                void track(unsigned);
        };
 
 private:
-       Catalogue   &catalogue;
-       std::string filename;
+       const Catalogue &catalogue;
        std::string base;
        std::set<Track *> tracks;
        //RouteSeq    routes;
@@ -42,16 +39,16 @@ public:
        sigc::signal<void, Track &> signal_track_added;
        sigc::signal<void, Track &> signal_track_removed;
 
-       Layout(Catalogue &);
+       Layout(const Catalogue &);
        ~Layout();
 
+       const Catalogue &get_catalogue() const { return catalogue; }
        const std::string &get_base() const { return base; }
        const std::set<Track *> &get_tracks() const { return tracks; }
        void  add_track(Track &);
        void  remove_track(Track &);
        void  check_links();
-       void  load(const std::string &);
-       int   save(const std::string &);
+       void  save(const std::string &);
 };
 
 } // namespace Marklin
index bfb7bc301feb180785d5c34fca2715efa73e1c0f..9eed205289a74c61566349f9f239a179e53796c1 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -17,7 +17,7 @@ LocoType::LocoType(unsigned an):
 
 
 LocoType::Loader::Loader(LocoType &lt):
-       ltype(lt)
+       Msp::DataFile::BasicLoader<LocoType>(lt)
 {
        add("function", &Loader::function);
        add("name",     &LocoType::name);
@@ -25,7 +25,7 @@ LocoType::Loader::Loader(LocoType &lt):
 
 void LocoType::Loader::function(unsigned i, const string &f)
 {
-       ltype.funcs[i]=f;
+       obj.funcs[i]=f;
 }
 
 } // namespace Marklin
index 190422ee7ec0b39a67f82169866c98c7646f78be..7509eca1c991078d98235ef2bd842cefda8a3374 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -15,14 +15,10 @@ namespace Marklin {
 class LocoType
 {
 public:
-       class Loader: public Msp::DataFile::Loader
+       class Loader: public Msp::DataFile::BasicLoader<LocoType>
        {
-       private:
-               LocoType &ltype;
-
        public:
                Loader(LocoType &);
-               LocoType &get_object() const { return ltype; }
        private:
                void function(unsigned, const std::string &);
        };
index e544081d04f6754f6c9074245df581e358c3b8f2..b6674aec0a2ea86e8242cc5fd8053a86a8fb57ab 100644 (file)
@@ -12,8 +12,6 @@ Distributed under the GPL
 using namespace std;
 using namespace Msp;
 
-#include <iostream>
-
 namespace Marklin {
 
 Track::Track(const TrackType &t):
@@ -319,12 +317,22 @@ Track *Track::copy() const
        return trk;
 }
 
-/*******************
-** Track::Loader
-*/
+void Track::save(list<DataFile::Statement> &st) const
+{
+       st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
+       st.push_back((DataFile::Statement("rotation"), rot));
+       st.push_back((DataFile::Statement("slope"), slope));
+       if(turnout_id)
+               st.push_back((DataFile::Statement("turnout_id"), turnout_id));
+       if(sensor_id)
+               st.push_back((DataFile::Statement("sensor_id"), sensor_id));
+       if(flex)
+               st.push_back((DataFile::Statement("flex"), true));
+}
+
 
 Track::Loader::Loader(Track &t):
-       track(t)
+       DataFile::BasicLoader<Track>(t)
 {
        add("position",    &Loader::position);
        add("rotation",    &Track::rot);
@@ -336,7 +344,7 @@ Track::Loader::Loader(Track &t):
 
 void Track::Loader::position(float x, float y, float z)
 {
-       track.pos=Point(x, y, z);
+       obj.pos=Point(x, y, z);
 }
 
 } // namespace Marklin
index 11c9fb3c332cc127e3392a39691de16d2d4af9ab..3c96a82d2cec51f6463c3e958d1b417c3ebb003a 100644 (file)
@@ -20,14 +20,10 @@ class TrackType;
 class Track
 {
 public:
-       class Loader: public Msp::DataFile::Loader
+       class Loader: public Msp::DataFile::BasicLoader<Track>
        {
-       private:
-               Track &track;
-
        public:
                Loader(Track &);
-               Track &get_object() { return track; }
        private:
                void position(float, float, float);
        };
@@ -77,6 +73,8 @@ public:
        won't have any links to other tracks, nor a turnout or sensor id.
        */
        Track *copy() const;
+
+       void save(std::list<Msp::DataFile::Statement> &) const;
 private:
 
        // Direct copying not allowed due to links.  See the copy() function.
index 046b9276ac23025a34ac586ef2509d74ca2b1d7f..da177f8a73387cf21308d70efbe9a4da21989a79 100644 (file)
@@ -53,7 +53,7 @@ Point TrackPart::get_point(float d) const
 
 
 TrackPart::Loader::Loader(TrackPart &p):
-       part(p)
+       Msp::DataFile::BasicLoader<TrackPart>(p)
 {
        add("start",    &Loader::start);
        add("length",   &TrackPart::length);
@@ -64,23 +64,23 @@ TrackPart::Loader::Loader(TrackPart &p):
 
 void TrackPart::Loader::finish()
 {
-       if(part.radius)
+       if(obj.radius)
        {
-               part.length*=M_PI/180;
-               part.radius/=1000;
+               obj.length*=M_PI/180;
+               obj.radius/=1000;
        }
        else
-               part.length/=1000;
+               obj.length/=1000;
 
-       part.pos.x/=1000;
-       part.pos.y/=1000;
-       part.dir*=M_PI/180;
+       obj.pos.x/=1000;
+       obj.pos.y/=1000;
+       obj.dir*=M_PI/180;
 }
 
 void TrackPart::Loader::start(float x, float y, float d)
 {
-       part.pos=Point(x, y);
-       part.dir=d;
+       obj.pos=Point(x, y);
+       obj.dir=d;
 }
 
 } // namespace Marklin
index 05fa0ba9dabd594cd2d882c99f579f1bdf165e50..de449b3a59e05e5d4c6c873b9ff8400a3db66396 100644 (file)
@@ -16,17 +16,12 @@ namespace Marklin {
 
 struct TrackPart
 {
-       class Loader: public Msp::DataFile::Loader
+       class Loader: public Msp::DataFile::BasicLoader<TrackPart>
        {
-       private:
-               TrackPart &part;
-
        public:
                Loader(TrackPart &);
-               TrackPart &get_object() { return part; }
        private:
                virtual void finish();
-
                void start(float, float, float);
        };
 
index 2376e909484395e3ddd4b276bcbcf1df3da03a03..7fc796245ef126dba67253e1ae1ab4ce447e6131 100644 (file)
@@ -83,7 +83,7 @@ void TrackType::collect_endpoints()
 
 
 TrackType::Loader::Loader(TrackType &t):
-       ttype(t)
+       Msp::DataFile::BasicLoader<TrackType>(t)
 {
        add("description", &TrackType::description);
        add("part",        &Loader::part);
@@ -91,14 +91,14 @@ TrackType::Loader::Loader(TrackType &t):
 
 void TrackType::Loader::finish()
 {
-       ttype.collect_endpoints();
+       obj.collect_endpoints();
 }
 
 void TrackType::Loader::part()
 {
        TrackPart p;
        load_sub(p);
-       ttype.parts.push_back(p);
+       obj.parts.push_back(p);
 }
 
 } // namespace Marklin
index 310d17f4eca60fc748ef79e7d5a6964add537f69..ba1b95ff6aee5148d5335f47648e9c2e5de84b00 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -17,17 +17,12 @@ namespace Marklin {
 class TrackType
 {
 public:
-       class Loader: public Msp::DataFile::Loader
+       class Loader: public Msp::DataFile::BasicLoader<TrackType>
        {
-       private:
-               TrackType &ttype;
-
        public:
                Loader(TrackType &);
-               TrackType &get_object() { return ttype; }
        private:
                virtual void finish();
-
                void part();
                void position(float, float, float);
        };
index 6de46a2673b904b691d0e14f358a7aac2c46f4c3..4075415ac95fdbe703a3287ad0e1f8d6e586d2d0 100644 (file)
@@ -6,9 +6,12 @@ Distributed under the GPL
 */
 
 #include <algorithm>
+#include <msp/datafile/writer.h>
 #include <msp/time/utils.h>
+#include "catalogue.h"
 #include "control.h"
 #include "layout.h"
+#include "locotype.h"
 #include "tracktype.h"
 #include "trafficmanager.h"
 #include "turnout.h"
@@ -84,8 +87,37 @@ void TrafficManager::tick()
                (*i)->tick(t, dt);
 }
 
+void TrafficManager::save(const string &fn) const
+{
+       IO::BufferedFile out(fn, IO::M_WRITE);
+       DataFile::Writer writer(out);
+       for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+       {
+               const Locomotive &loco=(*i)->get_locomotive();
+               DataFile::Statement st("train");
+               st.append(loco.get_type().get_article_number());
+               st.append(loco.get_address());
+               (*i)->save(st.sub);
+               writer.write(st);
+       }
+}
+
 void TrafficManager::turnout_route_changed(unsigned, Turnout *)
 {
 }
 
+
+TrafficManager::Loader::Loader(TrafficManager &tm):
+       DataFile::BasicLoader<TrafficManager>(tm)
+{
+       add("train", &Loader::train);
+}
+
+void TrafficManager::Loader::train(unsigned art_nr, unsigned addr)
+{
+       Locomotive *loco=new Locomotive(obj.layout.get_catalogue().get_locomotive(art_nr), obj.control, addr);
+       Train *trn=new Train(obj, *loco);
+       load_sub(*trn);
+}
+
 } // namespace Marklin
index ac3f395ce45a50bd1f3ad248c2dc246a0bdffa2f..b09c500ff39fabf0ed18d7f0ac2e154ffdd4e5d5 100644 (file)
@@ -19,6 +19,15 @@ class Turnout;
 
 class TrafficManager
 {
+public:
+       class Loader: public Msp::DataFile::BasicLoader<TrafficManager>
+       {
+       public:
+               Loader(TrafficManager &);
+       private:
+               void train(unsigned, unsigned);
+       };
+
 private:
        Control &control;
        Layout &layout;
@@ -38,6 +47,7 @@ public:
        const std::list<Train *> &get_trains() const { return trains; }
        void add_train(Train *);
        void tick();
+       void save(const std::string &) const;
 private:
        void turnout_route_changed(unsigned, Turnout *);
 };
index f55d7ad4ea79fbff8acdc522aee39e231909887f..f8ef1715dbc5aa6d7a6a33bd476bb9db510d0939 100644 (file)
@@ -139,6 +139,12 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
        }
 }
 
+void Train::save(list<DataFile::Statement> &st) const
+{
+       st.push_back((DataFile::Statement("name"), name));
+       st.push_back((DataFile::Statement("speed_scale"), speed_scale, speed_scale_weight));
+}
+
 void Train::sensor_event(bool state, Sensor *sensor)
 {
        unsigned addr=sensor->get_address();
@@ -306,4 +312,12 @@ void Train::set_position(const Block::Endpoint &bep)
        pos=cur_track->get_endpoint_position(cur_track_ep);
 }
 
+
+Train::Loader::Loader(Train &t):
+       DataFile::BasicLoader<Train>(t)
+{
+       add("name",        &Train::name);
+       add("speed_scale", &Train::speed_scale, &Train::speed_scale_weight);
+}
+
 } // namespace Marklin
index 6a7d2dea949dfe728a698155bcfd4adc7bace723..b84844aa050a999ddeeb5b93cb8ce97c5666ab46 100644 (file)
@@ -21,6 +21,13 @@ class TrafficManager;
 
 class Train: public sigc::trackable
 {
+public:
+       class Loader: public Msp::DataFile::BasicLoader<Train>
+       {
+       public:
+               Loader(Train &);
+       };
+
 private:
        struct BlockRef
        {
@@ -66,6 +73,7 @@ public:
        void place(Block *, unsigned);
        bool free_block(Block *);
        void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
+       void save(std::list<Msp::DataFile::Statement> &) const;
 private:
        void sensor_event(bool, Sensor *);
        unsigned reserve_more();
index 63687cf9af1bb2a29960aeb53a486cc929d1d4c4..60bab121042bf1f8b8c46aaebe36b6245cab7edf 100644 (file)
@@ -5,7 +5,6 @@ Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
-#include <iostream>
 #include <msp/time/timer.h>
 #include <msp/time/units.h>
 #include "command.h"