From 05b95b6b6b095821f1e79dabed802b853c296c9d Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 30 Aug 2013 21:56:20 +0300 Subject: [PATCH] Use some template magic to deduce the proper 3D type --- source/3d/allocation.cpp | 2 +- source/3d/layout.cpp | 2 +- source/3d/layout.h | 7 ++++--- source/3d/trackchain.cpp | 2 +- source/3d/trackcircuit.cpp | 2 +- source/3d/typemap.h | 24 ++++++++++++++++++++++++ source/designer/designer.cpp | 12 ++++++------ source/engineer/engineer.cpp | 4 ++-- 8 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 source/3d/typemap.h diff --git a/source/3d/allocation.cpp b/source/3d/allocation.cpp index 0611b05..3835f8c 100644 --- a/source/3d/allocation.cpp +++ b/source/3d/allocation.cpp @@ -38,7 +38,7 @@ void Allocation3D::block_reserved(Block &block, Train *t) const Block::TrackSet &tracks = block.get_tracks(); for(Block::TrackSet::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) { - Path3D *path = new Path3D(layout.get(**i)); + Path3D *path = new Path3D(layout.get_3d(**i)); bpaths.push_back(path); path->set_layer(1); float intensity = 0.5+train.get_block_allocator().is_block_current(block)*0.5; diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index fe3514f..59a23c9 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -52,7 +52,7 @@ void Layout3D::add(Object3D &o) insert_unique(objects, &o.get_object(), &o); } -Object3D &Layout3D::get(Object &o) const +Object3D &Layout3D::get_3d(Object &o) const { return *get_item(objects, &o); } diff --git a/source/3d/layout.h b/source/3d/layout.h index d5aad26..dd3c0f5 100644 --- a/source/3d/layout.h +++ b/source/3d/layout.h @@ -8,6 +8,7 @@ #include #include "libr2c2/layout.h" #include "catalogue.h" +#include "typemap.h" namespace R2C2 { @@ -40,13 +41,13 @@ public: void get_bounds(Vector &, Vector &) const; void add(Object3D &); - Object3D &get(Object &) const; + Object3D &get_3d(Object &) const; const ObjectMap &get_all() const { return objects; } void remove(Object3D &); template - T &get(Object &o) const - { return dynamic_cast(get(o)); } + typename TypeMap3D::Type3D &get_3d(T &o) const + { return dynamic_cast::Type3D &>(get_3d(static_cast(o))); } void add(Utility3D &); void remove(Utility3D &); diff --git a/source/3d/trackchain.cpp b/source/3d/trackchain.cpp index 3d972d1..a95d411 100644 --- a/source/3d/trackchain.cpp +++ b/source/3d/trackchain.cpp @@ -42,7 +42,7 @@ void TrackChain3D::set_layer(float l) void TrackChain3D::track_added(Track &track) { - Track3D &track_3d = layout.get(track); + Track3D &track_3d = layout.get_3d(track); Path3D *path = new Path3D(track_3d); paths.insert(PathMap::value_type(&track, path)); path->set_color(color); diff --git a/source/3d/trackcircuit.cpp b/source/3d/trackcircuit.cpp index d74ee20..44e844d 100644 --- a/source/3d/trackcircuit.cpp +++ b/source/3d/trackcircuit.cpp @@ -18,7 +18,7 @@ TrackCircuit3D::TrackCircuit3D(Layout3D &l, TrackCircuit &tc): TrackIter iter = BlockIter(block, 0).track_iter(); for(; (iter && &iter->get_block()==block); iter=iter.next()) { - Path3D *path = new Path3D(layout.get(*iter)); + Path3D *path = new Path3D(layout.get_3d(*iter)); paths.push_back(path); path->set_side(iter.entry()*2-1); } diff --git a/source/3d/typemap.h b/source/3d/typemap.h new file mode 100644 index 0000000..84a3eeb --- /dev/null +++ b/source/3d/typemap.h @@ -0,0 +1,24 @@ +#ifndef R2C2_3D_TYPEMAP_H_ +#define R2C2_3D_TYPEMAP_H_ + +#include +#include "signal.h" +#include "track.h" +#include "vehicle.h" + +namespace R2C2 { + +template +struct TypeMap3D; + +template +struct TypeMap3D: TypeMap3D +{ }; + +template<> struct TypeMap3D { typedef Signal3D Type3D; }; +template<> struct TypeMap3D { typedef Track3D Type3D; }; +template<> struct TypeMap3D { typedef Vehicle3D Type3D; }; + +} // namespace R2C2 + +#endif diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index 1df759b..30b022f 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -150,7 +150,7 @@ Designer::Designer(int argc, char **argv): const set &tracks = layout->get_all(); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - update_track_icon(layout_3d->get(**i)); + update_track_icon(layout_3d->get_3d(**i)); } Designer::~Designer() @@ -205,7 +205,7 @@ void Designer::erase_tracks() selection.clear(); for(set::iterator i=tracks.begin(); i!=tracks.end(); ++i) { - overlay->clear(layout_3d->get(**i)); + overlay->clear(layout_3d->get_3d(**i)); delete *i; } } @@ -406,7 +406,7 @@ void Designer::key_press(unsigned key) if(*j!=*i) (*i)->snap_to(**j, true); - update_track_icon(layout_3d->get(**i)); + update_track_icon(layout_3d->get_3d(**i)); } } else if(key==Msp::Input::KEY_F) @@ -540,7 +540,7 @@ void Designer::track_properties_response(int) { const set &tracks = selection.get_objects(); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - update_track_icon(layout_3d->get(**i)); + update_track_icon(layout_3d->get_3d(**i)); } void Designer::route_name_accept(const string &text) @@ -591,7 +591,7 @@ void Designer::show_route(const Route &route) const set &rtracks = route.get_tracks(); for(set::iterator i=rtracks.begin(); i!=rtracks.end(); ++i) { - Track3D &t3d = layout_3d->get(**i); + Track3D &t3d = layout_3d->get_3d(**i); Path3D *path = new Path3D(t3d); path->set_color(GL::Color(0.5, 0.8, 1.0)); if(unsigned tid = (*i)->get_turnout_id()) @@ -606,7 +606,7 @@ void Designer::show_zone(const Zone &zone) const Zone::TrackSet &ztracks = zone.get_tracks(); for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i) { - Track3D &t3d = layout_3d->get(**i); + Track3D &t3d = layout_3d->get_3d(**i); Path3D *path = new Path3D(t3d); path->set_color(GL::Color(0.8, 1.0, 0.5)); } diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index 9b49c84..fb21f99 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -76,7 +76,7 @@ Engineer::Engineer(int argc, char **argv): const set &tracks = layout.get_all(); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) if((*i)->get_type().is_turnout()) - new Path3D(layout_3d.get(**i)); + new Path3D(layout_3d.get_3d(**i)); if(FS::exists(options.state_fn)) DataFile::load(layout, options.state_fn); @@ -292,7 +292,7 @@ Object *Engineer::pick_object(const Vector &p) void Engineer::process_new_train(Train &train) { - Vehicle3D &loco3d = layout_3d.get(train.get_vehicle(0)); + Vehicle3D &loco3d = layout_3d.get_3d(train.get_vehicle(0)); overlay->set_label(loco3d, train.get_name()); train.signal_name_changed.connect(sigc::bind<0>(sigc::mem_fun(overlay, &Overlay3D::set_label), sigc::ref(loco3d))); } -- 2.45.2