From: Mikko Rasa Date: Fri, 30 Aug 2013 19:18:47 +0000 (+0300) Subject: Use generic ObjectTypes in Catalogue X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7c98e23312cf01ce1fa6c1ffd8e36c22d2fa6110;p=r2c2.git Use generic ObjectTypes in Catalogue --- diff --git a/source/3d/catalogue.cpp b/source/3d/catalogue.cpp index 4d7b4a8..9daa7c8 100644 --- a/source/3d/catalogue.cpp +++ b/source/3d/catalogue.cpp @@ -23,58 +23,34 @@ Catalogue3D::Catalogue3D(Catalogue &c): add_type().creator(&Catalogue3D::create); add_type().creator(&Catalogue3D::create); - catalogue.signal_track_added.connect(sigc::mem_fun(this, &Catalogue3D::track_added)); - catalogue.signal_vehicle_added.connect(sigc::mem_fun(this, &Catalogue3D::vehicle_added)); - catalogue.signal_signal_added.connect(sigc::mem_fun(this, &Catalogue3D::signal_added)); + catalogue.signal_object_added.connect(sigc::mem_fun(this, &Catalogue3D::object_added)); - const Catalogue::TrackMap &trks = catalogue.get_tracks(); - for(Catalogue::TrackMap::const_iterator i=trks.begin(); i!=trks.end(); ++i) - track_added(*i->second); - const Catalogue::SignalMap &sigs = catalogue.get_signals(); - for(Catalogue::SignalMap::const_iterator i=sigs.begin(); i!=sigs.end(); ++i) - signal_added(*i->second); + const Catalogue::ObjectMap &objs = catalogue.get_all(); + for(Catalogue::ObjectMap::const_iterator i=objs.begin(); i!=objs.end(); ++i) + object_added(*i->second); build_endpoint_mesh(); } Catalogue3D::~Catalogue3D() { - for(map::iterator i=tracks.begin(); i!=tracks.end(); ++i) + for(map::iterator i=objects.begin(); i!=objects.end(); ++i) delete i->second; - for(map::iterator i=vehicles.begin(); i!=vehicles.end(); ++i) - delete i->second; - for(map::iterator i=signals.begin(); i!=signals.end(); ++i) - delete i->second; -} - -const TrackType3D &Catalogue3D::get_track(const TrackType &tt) const -{ - return *get_item(tracks, &tt); -} - -const VehicleType3D &Catalogue3D::get_vehicle(const VehicleType &vt) const -{ - return *get_item(vehicles, &vt); -} - -const SignalType3D &Catalogue3D::get_signal(const SignalType &vt) const -{ - return *get_item(signals, &vt); -} - -void Catalogue3D::track_added(const TrackType &track) -{ - tracks[&track] = new TrackType3D(*this, track); } -void Catalogue3D::vehicle_added(const VehicleType &veh) +void Catalogue3D::object_added(const ObjectType &ot) { - vehicles[&veh] = new VehicleType3D(*this, veh); + if(const TrackType *tt = dynamic_cast(&ot)) + objects[&ot] = new TrackType3D(*this, *tt); + else if(const SignalType *st = dynamic_cast(&ot)) + objects[&ot] = new SignalType3D(*this, *st); + else if(const VehicleType *vt = dynamic_cast(&ot)) + objects[&ot] = new VehicleType3D(*this, *vt); } -void Catalogue3D::signal_added(const SignalType &sig) +const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const { - signals[&sig] = new SignalType3D(*this, sig); + return *get_item(objects, &ot); } void Catalogue3D::build_endpoint_mesh() diff --git a/source/3d/catalogue.h b/source/3d/catalogue.h index ecac7dd..1cc096a 100644 --- a/source/3d/catalogue.h +++ b/source/3d/catalogue.h @@ -6,20 +6,17 @@ #include #include #include "libr2c2/catalogue.h" +#include "typemap.h" namespace R2C2 { -class SignalType3D; -class TrackType3D; -class VehicleType3D; +class ObjectType3D; class Catalogue3D: public Msp::DataFile::Collection { private: Catalogue &catalogue; - std::map tracks; - std::map vehicles; - std::map signals; + std::map objects; Msp::GL::Mesh endpoint_mesh; public: @@ -27,16 +24,21 @@ public: ~Catalogue3D(); const Catalogue &get_catalogue() const { return catalogue; } - const TrackType3D &get_track(const TrackType &) const; - const VehicleType3D &get_vehicle(const VehicleType &) const; - const SignalType3D &get_signal(const SignalType &) const; - const Msp::GL::Mesh &get_endpoint_mesh() const { return endpoint_mesh; } private: - void track_added(const TrackType &); - void vehicle_added(const VehicleType &); - void signal_added(const SignalType &); + void object_added(const ObjectType &); +public: + const ObjectType3D &get_3d(const ObjectType &) const; + + template + const typename TypeMap3D::Type3D &get_3d(const T &ot) const + { return dynamic_cast::Type3D &>(get_3d(static_cast(ot))); } + +private: void build_endpoint_mesh(); +public: + const Msp::GL::Mesh &get_endpoint_mesh() const { return endpoint_mesh; } +private: Msp::FS::Path locate_file(const std::string &); template diff --git a/source/3d/objecttype.h b/source/3d/objecttype.h new file mode 100644 index 0000000..2b1a833 --- /dev/null +++ b/source/3d/objecttype.h @@ -0,0 +1,20 @@ +#ifndef R2C2_3D_OBJECTTYPE_H_ +#define R2C2_3D_OBJECTTYPE_H_ + +namespace R2C2 { + +class Catalogue3D; + +class ObjectType3D +{ +protected: + Catalogue3D &catalogue; + + ObjectType3D(Catalogue3D &c): catalogue(c) { } +public: + virtual ~ObjectType3D() { } +}; + +} // namespace R2C2 + +#endif diff --git a/source/3d/signal.cpp b/source/3d/signal.cpp index 5872dc8..2e4a112 100644 --- a/source/3d/signal.cpp +++ b/source/3d/signal.cpp @@ -10,7 +10,7 @@ namespace R2C2 { Signal3D::Signal3D(Layout3D &l, Signal &s): Object3D(l, s), - GL::ObjectInstance(l.get_catalogue().get_signal(s.get_type()).get_object()), + GL::ObjectInstance(l.get_catalogue().get_3d(s.get_type()).get_object()), signal(s) { layout.get_scene().add(*this); diff --git a/source/3d/signaltype.cpp b/source/3d/signaltype.cpp index e5f36a9..1843e00 100644 --- a/source/3d/signaltype.cpp +++ b/source/3d/signaltype.cpp @@ -6,7 +6,7 @@ using namespace Msp; namespace R2C2 { SignalType3D::SignalType3D(Catalogue3D &c, const SignalType &type): - catalogue(c), + ObjectType3D(c), object(catalogue.get(type.get_object())) { } diff --git a/source/3d/signaltype.h b/source/3d/signaltype.h index 7ec360b..2f88623 100644 --- a/source/3d/signaltype.h +++ b/source/3d/signaltype.h @@ -3,15 +3,13 @@ #include #include "libr2c2/signaltype.h" +#include "objecttype.h" namespace R2C2 { -class Catalogue3D; - -class SignalType3D +class SignalType3D: public ObjectType3D { private: - Catalogue3D &catalogue; Msp::GL::Object &object; public: diff --git a/source/3d/track.cpp b/source/3d/track.cpp index ee7671c..549bc15 100644 --- a/source/3d/track.cpp +++ b/source/3d/track.cpp @@ -15,9 +15,9 @@ namespace R2C2 { Track3D::Track3D(Layout3D &l, Track &t): Object3D(l, t), - GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()), + GL::ObjectInstance(l.get_catalogue().get_3d(t.get_type()).get_object()), track(t), - type(layout.get_catalogue().get_track(track.get_type())) + type(layout.get_catalogue().get_3d(track.get_type())) { layout.get_scene().add(*this); diff --git a/source/3d/tracktype.cpp b/source/3d/tracktype.cpp index 31751f9..d73387c 100644 --- a/source/3d/tracktype.cpp +++ b/source/3d/tracktype.cpp @@ -9,13 +9,13 @@ using namespace Msp; namespace R2C2 { -TrackType3D::TrackType3D(Catalogue3D &cat3d, const TrackType &tt): - catalogue(cat3d), +TrackType3D::TrackType3D(Catalogue3D &c, const TrackType &tt): + ObjectType3D(c), mesh(0), object(0), own_data(false) { - const Catalogue &cat = cat3d.get_catalogue(); + const Catalogue &cat = catalogue.get_catalogue(); const vector &parts = tt.get_parts(); const Profile &ballast_profile = cat.get_ballast_profile(); diff --git a/source/3d/tracktype.h b/source/3d/tracktype.h index 27bf7fe..6cde93e 100644 --- a/source/3d/tracktype.h +++ b/source/3d/tracktype.h @@ -7,15 +7,13 @@ #include #include "libr2c2/profile.h" #include "libr2c2/tracktype.h" +#include "objecttype.h" namespace R2C2 { -class Catalogue3D; - -class TrackType3D +class TrackType3D: public ObjectType3D { private: - Catalogue3D &catalogue; Msp::GL::Mesh *mesh; Msp::GL::Object *object; bool own_data; diff --git a/source/3d/typemap.h b/source/3d/typemap.h index 84a3eeb..2772b1f 100644 --- a/source/3d/typemap.h +++ b/source/3d/typemap.h @@ -3,8 +3,11 @@ #include #include "signal.h" +#include "signaltype.h" #include "track.h" +#include "tracktype.h" #include "vehicle.h" +#include "vehicletype.h" namespace R2C2 { @@ -16,8 +19,11 @@ struct TypeMap3D: TypeMap3D { }; template<> struct TypeMap3D { typedef Signal3D Type3D; }; +template<> struct TypeMap3D { typedef SignalType3D Type3D; }; template<> struct TypeMap3D { typedef Track3D Type3D; }; +template<> struct TypeMap3D { typedef TrackType3D Type3D; }; template<> struct TypeMap3D { typedef Vehicle3D Type3D; }; +template<> struct TypeMap3D { typedef VehicleType3D Type3D; }; } // namespace R2C2 diff --git a/source/3d/vehicle.cpp b/source/3d/vehicle.cpp index fa1d751..5843c17 100644 --- a/source/3d/vehicle.cpp +++ b/source/3d/vehicle.cpp @@ -15,9 +15,9 @@ namespace R2C2 { Vehicle3D::Vehicle3D(Layout3D &l, Vehicle &v): Object3D(l, v), - GL::ObjectInstance(*l.get_catalogue().get_vehicle(v.get_type()).get_body_object()), + GL::ObjectInstance(*l.get_catalogue().get_3d(v.get_type()).get_body_object()), vehicle(v), - type(layout.get_catalogue().get_vehicle(vehicle.get_type())) + type(layout.get_catalogue().get_3d(vehicle.get_type())) { unsigned n_axles = vehicle.get_type().get_axles().size(); for(unsigned i=0; i ¶ms, const string &key, T def = T()) namespace R2C2 { VehicleType3D::VehicleType3D(Catalogue3D &c, const VehicleType &t): - catalogue(c), + ObjectType3D(c), type(t), body_object(0) { diff --git a/source/3d/vehicletype.h b/source/3d/vehicletype.h index 81a6468..02d1108 100644 --- a/source/3d/vehicletype.h +++ b/source/3d/vehicletype.h @@ -4,15 +4,13 @@ #include #include #include "libr2c2/vehicletype.h" +#include "objecttype.h" namespace R2C2 { -class Catalogue3D; - -class VehicleType3D +class VehicleType3D: public ObjectType3D { private: - Catalogue3D &catalogue; const VehicleType &type; std::map objects; Msp::GL::Object *body_object; diff --git a/source/designer/extendtool.cpp b/source/designer/extendtool.cpp index 3af4cf6..2a66181 100644 --- a/source/designer/extendtool.cpp +++ b/source/designer/extendtool.cpp @@ -154,20 +154,21 @@ void ExtendTool::finish() vector ExtendTool::create_straight(const Vector &start, const Angle &dir, float length, float limit) { - const Catalogue::TrackMap &track_types = designer.get_catalogue().get_tracks(); + const Catalogue::ObjectMap &object_types = designer.get_catalogue().get_all(); map types_by_length; unsigned preference = 0; - for(Catalogue::TrackMap::const_iterator i=track_types.begin(); i!=track_types.end(); ++i) - { - const vector &parts = i->second->get_parts(); - if(parts.size()!=1) - continue; - if(parts.front().is_curved() || parts.front().is_dead_end()) - continue; + for(Catalogue::ObjectMap::const_iterator i=object_types.begin(); i!=object_types.end(); ++i) + if(const TrackType *tt = dynamic_cast(i->second)) + { + const vector &parts = tt->get_parts(); + if(parts.size()!=1) + continue; + if(parts.front().is_curved() || parts.front().is_dead_end()) + continue; - types_by_length[parts.front().get_length()] = i->second; - preference = max(preference, i->second->get_autofit_preference()); - } + types_by_length[parts.front().get_length()] = tt; + preference = max(preference, tt->get_autofit_preference()); + } vector lengths; float removed = 0; diff --git a/source/engineer/newtraindialog.cpp b/source/engineer/newtraindialog.cpp index 78a3f11..006a819 100644 --- a/source/engineer/newtraindialog.cpp +++ b/source/engineer/newtraindialog.cpp @@ -42,10 +42,11 @@ NewTrainDialog::NewTrainDialog(Engineer &e): drp_protocol->set_selected_index(0); - const Catalogue::VehicleMap &cat_vtypes = engineer.get_layout().get_catalogue().get_vehicles(); - for(Catalogue::VehicleMap::const_iterator i=cat_vtypes.begin(); i!=cat_vtypes.end(); ++i) - if(i->second->is_locomotive()) - loco_types.append(i->second); + const Catalogue::ObjectMap &obj_types = engineer.get_layout().get_catalogue().get_all(); + for(Catalogue::ObjectMap::const_iterator i=obj_types.begin(); i!=obj_types.end(); ++i) + if(const VehicleType *vt = dynamic_cast(i->second)) + if(vt->is_locomotive()) + loco_types.append(vt); } void NewTrainDialog::on_response(int code) diff --git a/source/engineer/newvehicledialog.cpp b/source/engineer/newvehicledialog.cpp index f27370f..de4d14a 100644 --- a/source/engineer/newvehicledialog.cpp +++ b/source/engineer/newvehicledialog.cpp @@ -37,14 +37,15 @@ NewVehicleDialog::NewVehicleDialog(Train &t): dynamic_cast(get_item(widgets, "btn_add"))->signal_clicked.connect(sigc::mem_fun(this, &NewVehicleDialog::add_clicked)); - const Catalogue::VehicleMap &cat_vtypes = train.get_layout().get_catalogue().get_vehicles(); - for(Catalogue::VehicleMap::const_iterator i=cat_vtypes.begin(); i!=cat_vtypes.end(); ++i) - { - if(i->second->is_locomotive()) - loco_types.append(i->second); - else - wagon_types.append(i->second); - } + const Catalogue::ObjectMap &obj_types = train.get_layout().get_catalogue().get_all(); + for(Catalogue::ObjectMap::const_iterator i=obj_types.begin(); i!=obj_types.end(); ++i) + if(const VehicleType *vt = dynamic_cast(i->second)) + { + if(vt->is_locomotive()) + loco_types.append(vt); + else + wagon_types.append(vt); + } } void NewVehicleDialog::toggle_list(bool show, GLtk::List *lst) diff --git a/source/libr2c2/catalogue.cpp b/source/libr2c2/catalogue.cpp index ab268cd..221a6f0 100644 --- a/source/libr2c2/catalogue.cpp +++ b/source/libr2c2/catalogue.cpp @@ -19,11 +19,7 @@ Catalogue::Catalogue(): Catalogue::~Catalogue() { - for(TrackMap::iterator i=tracks.begin(); i!=tracks.end(); ++i) - delete i->second; - for(SignalMap::iterator i=signals.begin(); i!=signals.end(); ++i) - delete i->second; - for(VehicleMap::iterator i=vehicles.begin(); i!=vehicles.end(); ++i) + for(ObjectMap::iterator i=objects.begin(); i!=objects.end(); ++i) delete i->second; } @@ -32,37 +28,15 @@ float Catalogue::get_rail_elevation() const return ballast_profile.get_height()+rail_profile.get_height(); } -void Catalogue::add_track(TrackType &track) -{ - insert_unique(tracks, track.get_article_number(), &track); - signal_track_added.emit(track); -} - -const TrackType &Catalogue::get_track(const ArticleNumber &art_nr) const -{ - return *get_item(tracks, art_nr); -} - -void Catalogue::add_vehicle(VehicleType &veh) -{ - insert_unique(vehicles, veh.get_article_number(), &veh); - signal_vehicle_added.emit(veh); -} - -const VehicleType &Catalogue::get_vehicle(const ArticleNumber &art_nr) const -{ - return *get_item(vehicles, art_nr); -} - -void Catalogue::add_signal(SignalType &sig) +void Catalogue::add(ObjectType &object) { - insert_unique(signals, sig.get_article_number(), &sig); - signal_signal_added.emit(sig); + insert_unique(objects, object.get_article_number(), &object); + signal_object_added.emit(object); } -const SignalType &Catalogue::get_signal(const ArticleNumber &art_nr) const +const ObjectType &Catalogue::get(const ArticleNumber &art_nr) const { - return *get_item(signals, art_nr); + return *get_item(objects, art_nr); } @@ -107,32 +81,32 @@ void Catalogue::Loader::scale(float n, float d) void Catalogue::Loader::signal(ArticleNumber art_nr) { - if(obj.signals.count(art_nr)) + if(obj.objects.count(art_nr)) throw key_error(art_nr); RefPtr sig = new SignalType(art_nr); load_sub(*sig); - obj.add_signal(*sig.release()); + obj.add(*sig.release()); } void Catalogue::Loader::track(ArticleNumber art_nr) { - if(obj.tracks.count(art_nr)) + if(obj.objects.count(art_nr)) throw key_error(art_nr); RefPtr trk = new TrackType(art_nr); load_sub(*trk); - obj.add_track(*trk.release()); + obj.add(*trk.release()); } void Catalogue::Loader::vehicle(ArticleNumber art_nr) { - if(obj.vehicles.count(art_nr)) + if(obj.objects.count(art_nr)) throw key_error(art_nr); RefPtr veh = new VehicleType(art_nr); load_sub(*veh); - obj.add_vehicle(*veh.release()); + obj.add(*veh.release()); } } // namespace R2C2 diff --git a/source/libr2c2/catalogue.h b/source/libr2c2/catalogue.h index d13c982..dcde9f3 100644 --- a/source/libr2c2/catalogue.h +++ b/source/libr2c2/catalogue.h @@ -9,9 +9,7 @@ namespace R2C2 { -class SignalType; -class TrackType; -class VehicleType; +class ObjectType; class Catalogue { @@ -31,13 +29,9 @@ public: void vehicle(ArticleNumber); }; - typedef std::map TrackMap; - typedef std::map VehicleMap; - typedef std::map SignalMap; + typedef std::map ObjectMap; - sigc::signal signal_track_added; - sigc::signal signal_vehicle_added; - sigc::signal signal_signal_added; + sigc::signal signal_object_added; private: float scale; @@ -45,9 +39,7 @@ private: Profile rail_profile; Profile ballast_profile; std::string track_technique; - TrackMap tracks; - VehicleMap vehicles; - SignalMap signals; + ObjectMap objects; Layout layout; public: @@ -61,17 +53,13 @@ public: const Profile &get_ballast_profile() const { return ballast_profile; } const std::string &get_track_technique() const { return track_technique; } - void add_track(TrackType &); - const TrackType &get_track(const ArticleNumber &) const; - const TrackMap &get_tracks() const { return tracks; } + void add(ObjectType &); + const ObjectType &get(const ArticleNumber &) const; + const ObjectMap &get_all() const { return objects; } - void add_vehicle(VehicleType &); - const VehicleType &get_vehicle(const ArticleNumber &) const; - const VehicleMap &get_vehicles() const { return vehicles; } - - void add_signal(SignalType &); - const SignalType &get_signal(const ArticleNumber &) const; - const SignalMap &get_signals() const { return signals; } + template + const T &get(const ArticleNumber &an) const + { return dynamic_cast(get(an)); } Layout &get_layout() { return layout; } }; diff --git a/source/libr2c2/layout.cpp b/source/libr2c2/layout.cpp index f318958..598bf82 100644 --- a/source/libr2c2/layout.cpp +++ b/source/libr2c2/layout.cpp @@ -555,13 +555,13 @@ void Layout::Loader::route() void Layout::Loader::signal(ArticleNumber art_nr) { - Signal *sig = new Signal(obj, obj.catalogue.get_signal(art_nr)); + Signal *sig = new Signal(obj, obj.catalogue.get(art_nr)); load_sub(*sig); } void Layout::Loader::track(ArticleNumber art_nr) { - Track *trk = new Track(obj, obj.catalogue.get_track(art_nr)); + Track *trk = new Track(obj, obj.catalogue.get(art_nr)); load_sub(*trk); new_tracks = true; const set &tracks = obj.objects.get(); @@ -572,7 +572,7 @@ void Layout::Loader::track(ArticleNumber art_nr) void Layout::Loader::train(ArticleNumber art_nr, unsigned addr, const std::string &proto) { - Train *trn = new Train(obj, obj.catalogue.get_vehicle(art_nr), addr, proto); + Train *trn = new Train(obj, obj.catalogue.get(art_nr), addr, proto); load_sub(*trn); } diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index 7e4a690..b166463 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -602,7 +602,7 @@ void Train::Loader::router() void Train::Loader::vehicle(ArticleNumber art_nr) { - const VehicleType &vtype = obj.layout.get_catalogue().get_vehicle(art_nr); + const VehicleType &vtype = obj.layout.get_catalogue().get(art_nr); Vehicle *veh = new Vehicle(obj.layout, vtype); obj.vehicles.back()->attach_back(*veh); obj.vehicles.push_back(veh); diff --git a/source/network/train.cpp b/source/network/train.cpp index eb95637..4ba4df0 100644 --- a/source/network/train.cpp +++ b/source/network/train.cpp @@ -7,7 +7,7 @@ namespace R2C2 { NetTrain::NetTrain(Client &c, const TrainInfoPacket &pkt): client(c), - loco_type(client.get_catalogue().get_vehicle(pkt.loco_type)), + loco_type(client.get_catalogue().get(pkt.loco_type)), address(pkt.address), name(pkt.name), functions(0)