]> git.tdb.fi Git - r2c2.git/commitdiff
Use some template magic to deduce the proper 3D type
authorMikko Rasa <tdb@tdb.fi>
Fri, 30 Aug 2013 18:56:20 +0000 (21:56 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 30 Aug 2013 18:56:20 +0000 (21:56 +0300)
source/3d/allocation.cpp
source/3d/layout.cpp
source/3d/layout.h
source/3d/trackchain.cpp
source/3d/trackcircuit.cpp
source/3d/typemap.h [new file with mode: 0644]
source/designer/designer.cpp
source/engineer/engineer.cpp

index 0611b058f95a0d08f36ae3da0ba340cdfb6bdb30..3835f8c8df2ba99ae70c0c597025c9cc189bac78 100644 (file)
@@ -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<Track3D>(**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;
index fe3514f4cd29437627cd1f213833e6e2f759a3ff..59a23c935aab6be2f3e41c5e53c4e383e3975f14 100644 (file)
@@ -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);
 }
index d5aad2634fbbe2343f4f2d38027347b531957f2b..dd3c0f5ea9b150bd54f5340743961dd4fe3187ad 100644 (file)
@@ -8,6 +8,7 @@
 #include <msp/gl/simplescene.h>
 #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<typename T>
-       T &get(Object &o) const
-       { return dynamic_cast<T &>(get(o)); }
+       typename TypeMap3D<T>::Type3D &get_3d(T &o) const
+       { return dynamic_cast<typename TypeMap3D<T>::Type3D &>(get_3d(static_cast<Object &>(o))); }
 
        void add(Utility3D &);
        void remove(Utility3D &);
index 3d972d164dd03acf2c4a9e184b9f738309c5421c..a95d411a64814cd425fdd2802ee920b9087d67ff 100644 (file)
@@ -42,7 +42,7 @@ void TrackChain3D::set_layer(float l)
 
 void TrackChain3D::track_added(Track &track)
 {
-       Track3D &track_3d = layout.get<Track3D>(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);
index d74ee20fa6a9f7a303c969e29260f75f6dd6d05f..44e844dfa6a39a70dbc42c47a98d466e5cef4cec 100644 (file)
@@ -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<Track3D>(*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 (file)
index 0000000..84a3eeb
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef R2C2_3D_TYPEMAP_H_
+#define R2C2_3D_TYPEMAP_H_
+
+#include <msp/core/meta.h>
+#include "signal.h"
+#include "track.h"
+#include "vehicle.h"
+
+namespace R2C2 {
+
+template<typename T>
+struct TypeMap3D;
+
+template<typename T>
+struct TypeMap3D<const T>: TypeMap3D<T>
+{ };
+
+template<> struct TypeMap3D<Signal> { typedef Signal3D Type3D; };
+template<> struct TypeMap3D<Track> { typedef Track3D Type3D; };
+template<> struct TypeMap3D<Vehicle> { typedef Vehicle3D Type3D; };
+
+} // namespace R2C2
+
+#endif
index 1df759b2367ffb417b6b25b89b51f3abffd9de38..30b022f8597e12e8e539d6b247d8e38d92728b9c 100644 (file)
@@ -150,7 +150,7 @@ Designer::Designer(int argc, char **argv):
 
        const set<Track *> &tracks = layout->get_all<Track>();
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               update_track_icon(layout_3d->get<Track3D>(**i));
+               update_track_icon(layout_3d->get_3d(**i));
 }
 
 Designer::~Designer()
@@ -205,7 +205,7 @@ void Designer::erase_tracks()
        selection.clear();
        for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
        {
-               overlay->clear(layout_3d->get<Track3D>(**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<Track3D>(**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<Track *> &tracks = selection.get_objects<Track>();
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               update_track_icon(layout_3d->get<Track3D>(**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<Track *> &rtracks = route.get_tracks();
        for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
        {
-               Track3D &t3d = layout_3d->get<Track3D>(**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<Track3D>(**i);
+               Track3D &t3d = layout_3d->get_3d(**i);
                Path3D *path = new Path3D(t3d);
                path->set_color(GL::Color(0.8, 1.0, 0.5));
        }
index 9b49c847ca5a21ff11229f0bb42e5252f622b9fb..fb21f9917b0e718b4031e0e8a42937aea3375d67 100644 (file)
@@ -76,7 +76,7 @@ Engineer::Engineer(int argc, char **argv):
        const set<Track *> &tracks = layout.get_all<Track>();
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                if((*i)->get_type().is_turnout())
-                       new Path3D(layout_3d.get<Track3D>(**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<Vehicle3D>(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)));
 }