From 45dd0aa6e8f2107f1bd7d37fe4b171ee2259af60 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 20 Dec 2022 14:03:45 +0200 Subject: [PATCH] Use dynamic_cast to reference when incorrect type is not acceptable --- source/gnuarchiver.cpp | 7 +------ source/gnulinker.cpp | 12 ++++-------- source/msvcarchiver.cpp | 7 +------ source/msvclinker.cpp | 7 +------ 4 files changed, 7 insertions(+), 26 deletions(-) diff --git a/source/gnuarchiver.cpp b/source/gnuarchiver.cpp index 742278d..f802031 100644 --- a/source/gnuarchiver.cpp +++ b/source/gnuarchiver.cpp @@ -28,12 +28,7 @@ Target *GnuArchiver::create_target(const list &sources, const string & list objs; for(Target *s: sources) - { - if(ObjectFile *obj = dynamic_cast(s)) - objs.push_back(obj); - else - throw invalid_argument("GnuArchiver::create_target"); - } + objs.push_back(&dynamic_cast(*s)); const Component &comp = *objs.front()->get_component(); StaticLibrary *lib = new StaticLibrary(builder, comp, objs); diff --git a/source/gnulinker.cpp b/source/gnulinker.cpp index 0d730a4..ddad8d1 100644 --- a/source/gnulinker.cpp +++ b/source/gnulinker.cpp @@ -47,14 +47,10 @@ Target *GnuLinker::create_target(const list &sources, const string &ar Linker *linker = default_linker; for(Target *s: sources) { - if(ObjectFile *obj = dynamic_cast(s)) - { - objs.push_back(obj); - if(obj->get_tool()->get_tag()=="CXX") - linker = cxx_linker; - } - else - throw invalid_argument("GnuLinker::create_target"); + ObjectFile &obj = dynamic_cast(*s); + objs.push_back(&obj); + if(obj.get_tool()->get_tag()=="CXX") + linker = cxx_linker; } const Component &comp = *objs.front()->get_component(); diff --git a/source/msvcarchiver.cpp b/source/msvcarchiver.cpp index 5ac406b..4a44bee 100644 --- a/source/msvcarchiver.cpp +++ b/source/msvcarchiver.cpp @@ -26,12 +26,7 @@ Target *MsvcArchiver::create_target(const list &sources, const string list objs; for(Target *s: sources) - { - if(ObjectFile *obj = dynamic_cast(s)) - objs.push_back(obj); - else - throw invalid_argument("MsvcArchiver::create_target"); - } + objs.push_back(&dynamic_cast(*s)); const Component &comp = *objs.front()->get_component(); StaticLibrary *lib = new StaticLibrary(builder, comp, objs); diff --git a/source/msvclinker.cpp b/source/msvclinker.cpp index 8ff4d9c..4495db7 100644 --- a/source/msvclinker.cpp +++ b/source/msvclinker.cpp @@ -36,12 +36,7 @@ Target *MsvcLinker::create_target(const list &sources, const string &a list objs; for(Target *s: sources) - { - if(ObjectFile *obj = dynamic_cast(s)) - objs.push_back(obj); - else - throw invalid_argument("MsvcLinker::create_target"); - } + objs.push_back(&dynamic_cast(*s)); const Component &comp = *objs.front()->get_component(); Binary *bin = 0; -- 2.45.2