From: Mikko Rasa Date: Thu, 21 May 2009 19:52:04 +0000 (+0000) Subject: Add state loading and saving to engineer X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3df8cb5c78fbb0b919bcb79677c6c788b8028482;p=r2c2.git Add state loading and saving to engineer Modernize all load/save code --- diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index 5a7ee62..39999df 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -70,7 +70,7 @@ Designer::Designer(int argc, char **argv): if(argc>1) { - layout->load(argv[1]); + DataFile::load(*layout, argv[1]); const list <racks=layout_3d->get_tracks(); for(list::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i) { diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index 5466302..581a301 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -10,6 +10,7 @@ Distributed under the GPL #include #include #include +#include #include #include #include @@ -29,8 +30,6 @@ using namespace std; using namespace Marklin; using namespace Msp; -#include - Engineer::Engineer(int argc, char **argv): screen_w(1280), screen_h(960), @@ -84,10 +83,12 @@ Engineer::Engineer(int argc, char **argv): const vector &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 &sensors=control.get_sensors(); for(map::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 &trains=trfc_mgr->get_trains(); + int y=main_panel->get_geometry().y; + for(list::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(); diff --git a/source/libmarklin/catalogue.cpp b/source/libmarklin/catalogue.cpp index c8ed80c..8faaa10 100644 --- a/source/libmarklin/catalogue.cpp +++ b/source/libmarklin/catalogue.cpp @@ -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 */ @@ -51,34 +51,30 @@ void Catalogue::load(const string &fn) Catalogue::Loader::Loader(Catalogue &c): - cat(c) + DataFile::BasicLoader(c) { add("locomotive", &Loader::locomotive); add("track", &Loader::track); } -void Catalogue::Loader::locomotive(unsigned art_no) +void Catalogue::Loader::locomotive(unsigned art_nr) { - map::iterator i=cat.locos.find(art_no); - if(i!=cat.locos.end()) + if(obj.locos.count(art_nr)) throw Exception("Duplicate locomotive number"); - RefPtr loco=new LocoType(art_no); + RefPtr 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::iterator i=cat.tracks.find(art_no); - if(i!=cat.tracks.end()) + if(obj.tracks.count(art_nr)) throw Exception("Duplicate track number"); - RefPtr trk=new TrackType(art_no); + RefPtr 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 diff --git a/source/libmarklin/catalogue.h b/source/libmarklin/catalogue.h index 7ce3e73..5f13999 100644 --- a/source/libmarklin/catalogue.h +++ b/source/libmarklin/catalogue.h @@ -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 */ @@ -19,13 +19,11 @@ class TrackType; class Catalogue { public: - class Loader: public Msp::DataFile::Loader + class Loader: public Msp::DataFile::BasicLoader { public: Loader(Catalogue &); private: - Catalogue &cat; - void locomotive(unsigned); void track(unsigned); }; diff --git a/source/libmarklin/constants.cpp b/source/libmarklin/constants.cpp index 9107361..e6f5946 100644 --- a/source/libmarklin/constants.cpp +++ b/source/libmarklin/constants.cpp @@ -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; diff --git a/source/libmarklin/layout.cpp b/source/libmarklin/layout.cpp index 4835f2e..50eba62 100644 --- a/source/libmarklin/layout.cpp +++ b/source/libmarklin/layout.cpp @@ -5,9 +5,9 @@ Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ -#include #include #include +#include #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::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(l) { add("base", &Layout::base); add("track", &Loader::track); } +void Layout::Loader::finish() +{ + obj.check_links(); + + for(set::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 trk=new Track(type); load_sub(*trk); - layout.add_track(*trk.release()); + obj.add_track(*trk.release()); } } // namespace Marklin diff --git a/source/libmarklin/layout.h b/source/libmarklin/layout.h index ae03cef..f603d39 100644 --- a/source/libmarklin/layout.h +++ b/source/libmarklin/layout.h @@ -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 { 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 tracks; //RouteSeq routes; @@ -42,16 +39,16 @@ public: sigc::signal signal_track_added; sigc::signal 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 &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 diff --git a/source/libmarklin/locotype.cpp b/source/libmarklin/locotype.cpp index bfb7bc3..9eed205 100644 --- a/source/libmarklin/locotype.cpp +++ b/source/libmarklin/locotype.cpp @@ -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 */ @@ -17,7 +17,7 @@ LocoType::LocoType(unsigned an): LocoType::Loader::Loader(LocoType <): - ltype(lt) + Msp::DataFile::BasicLoader(lt) { add("function", &Loader::function); add("name", &LocoType::name); @@ -25,7 +25,7 @@ LocoType::Loader::Loader(LocoType <): void LocoType::Loader::function(unsigned i, const string &f) { - ltype.funcs[i]=f; + obj.funcs[i]=f; } } // namespace Marklin diff --git a/source/libmarklin/locotype.h b/source/libmarklin/locotype.h index 190422e..7509eca 100644 --- a/source/libmarklin/locotype.h +++ b/source/libmarklin/locotype.h @@ -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 */ @@ -15,14 +15,10 @@ namespace Marklin { class LocoType { public: - class Loader: public Msp::DataFile::Loader + class Loader: public Msp::DataFile::BasicLoader { - private: - LocoType <ype; - public: Loader(LocoType &); - LocoType &get_object() const { return ltype; } private: void function(unsigned, const std::string &); }; diff --git a/source/libmarklin/track.cpp b/source/libmarklin/track.cpp index e544081..b6674ae 100644 --- a/source/libmarklin/track.cpp +++ b/source/libmarklin/track.cpp @@ -12,8 +12,6 @@ Distributed under the GPL using namespace std; using namespace Msp; -#include - namespace Marklin { Track::Track(const TrackType &t): @@ -319,12 +317,22 @@ Track *Track::copy() const return trk; } -/******************* -** Track::Loader -*/ +void Track::save(list &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(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 diff --git a/source/libmarklin/track.h b/source/libmarklin/track.h index 11c9fb3..3c96a82 100644 --- a/source/libmarklin/track.h +++ b/source/libmarklin/track.h @@ -20,14 +20,10 @@ class TrackType; class Track { public: - class Loader: public Msp::DataFile::Loader + class Loader: public Msp::DataFile::BasicLoader { - 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 &) const; private: // Direct copying not allowed due to links. See the copy() function. diff --git a/source/libmarklin/trackpart.cpp b/source/libmarklin/trackpart.cpp index 046b927..da177f8 100644 --- a/source/libmarklin/trackpart.cpp +++ b/source/libmarklin/trackpart.cpp @@ -53,7 +53,7 @@ Point TrackPart::get_point(float d) const TrackPart::Loader::Loader(TrackPart &p): - part(p) + Msp::DataFile::BasicLoader(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 diff --git a/source/libmarklin/trackpart.h b/source/libmarklin/trackpart.h index 05fa0ba..de449b3 100644 --- a/source/libmarklin/trackpart.h +++ b/source/libmarklin/trackpart.h @@ -16,17 +16,12 @@ namespace Marklin { struct TrackPart { - class Loader: public Msp::DataFile::Loader + class Loader: public Msp::DataFile::BasicLoader { - private: - TrackPart ∂ - public: Loader(TrackPart &); - TrackPart &get_object() { return part; } private: virtual void finish(); - void start(float, float, float); }; diff --git a/source/libmarklin/tracktype.cpp b/source/libmarklin/tracktype.cpp index 2376e90..7fc7962 100644 --- a/source/libmarklin/tracktype.cpp +++ b/source/libmarklin/tracktype.cpp @@ -83,7 +83,7 @@ void TrackType::collect_endpoints() TrackType::Loader::Loader(TrackType &t): - ttype(t) + Msp::DataFile::BasicLoader(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 diff --git a/source/libmarklin/tracktype.h b/source/libmarklin/tracktype.h index 310d17f..ba1b95f 100644 --- a/source/libmarklin/tracktype.h +++ b/source/libmarklin/tracktype.h @@ -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 */ @@ -17,17 +17,12 @@ namespace Marklin { class TrackType { public: - class Loader: public Msp::DataFile::Loader + class Loader: public Msp::DataFile::BasicLoader { - private: - TrackType &ttype; - public: Loader(TrackType &); - TrackType &get_object() { return ttype; } private: virtual void finish(); - void part(); void position(float, float, float); }; diff --git a/source/libmarklin/trafficmanager.cpp b/source/libmarklin/trafficmanager.cpp index 6de46a2..4075415 100644 --- a/source/libmarklin/trafficmanager.cpp +++ b/source/libmarklin/trafficmanager.cpp @@ -6,9 +6,12 @@ Distributed under the GPL */ #include +#include #include +#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::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(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 diff --git a/source/libmarklin/trafficmanager.h b/source/libmarklin/trafficmanager.h index ac3f395..b09c500 100644 --- a/source/libmarklin/trafficmanager.h +++ b/source/libmarklin/trafficmanager.h @@ -19,6 +19,15 @@ class Turnout; class TrafficManager { +public: + class Loader: public Msp::DataFile::BasicLoader + { + public: + Loader(TrafficManager &); + private: + void train(unsigned, unsigned); + }; + private: Control &control; Layout &layout; @@ -38,6 +47,7 @@ public: const std::list &get_trains() const { return trains; } void add_train(Train *); void tick(); + void save(const std::string &) const; private: void turnout_route_changed(unsigned, Turnout *); }; diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index f55d7ad..f8ef171 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -139,6 +139,12 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) } } +void Train::save(list &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(t) +{ + add("name", &Train::name); + add("speed_scale", &Train::speed_scale, &Train::speed_scale_weight); +} + } // namespace Marklin diff --git a/source/libmarklin/train.h b/source/libmarklin/train.h index 6a7d2de..b84844a 100644 --- a/source/libmarklin/train.h +++ b/source/libmarklin/train.h @@ -21,6 +21,13 @@ class TrafficManager; class Train: public sigc::trackable { +public: + class Loader: public Msp::DataFile::BasicLoader + { + 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 &) const; private: void sensor_event(bool, Sensor *); unsigned reserve_more(); diff --git a/source/libmarklin/turnout.cpp b/source/libmarklin/turnout.cpp index 63687cf..60bab12 100644 --- a/source/libmarklin/turnout.cpp +++ b/source/libmarklin/turnout.cpp @@ -5,7 +5,6 @@ Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ -#include #include #include #include "command.h"