From 6d02c152df37051bc845fac0fe445c4ab45206b1 Mon Sep 17 00:00:00 2001 From: Maksim Derbasov Date: Thu, 19 Dec 2024 02:24:25 +0900 Subject: [PATCH] Better approach to use std::variant (#1093) * Better approach to use std::variant Signed-off-by: Maksim Derbasov * Iteration Signed-off-by: Maksim Derbasov --------- Signed-off-by: Maksim Derbasov (cherry picked from commit 77656668486bbedd1e9df254a2a41c6150e891e7) --- ogre2/src/Ogre2GpuRays.cc | 60 ++++++++++--------------- ogre2/src/Ogre2ThermalCamera.cc | 78 ++++++++++++++------------------- 2 files changed, 57 insertions(+), 81 deletions(-) diff --git a/ogre2/src/Ogre2GpuRays.cc b/ogre2/src/Ogre2GpuRays.cc index d07da5a86..32cd15f61 100644 --- a/ogre2/src/Ogre2GpuRays.cc +++ b/ogre2/src/Ogre2GpuRays.cc @@ -84,7 +84,7 @@ class GZ_RENDERING_OGRE2_HIDDEN private: virtual void passPreExecute( Ogre::CompositorPass *_pass) override; - /// \brief Callback when each pass is finisned executing. + /// \brief Callback when each pass is finished executing. /// \param[in] _pass Ogre pass which has already executed private: virtual void passPosExecute( Ogre::CompositorPass *_pass) override; @@ -156,7 +156,7 @@ class GZ_RENDERING_OGRE2_HIDDEN gz::rendering::Ogre2GpuRaysPrivate /// range data public: std::set cubeFaceIdx; - /// \brief Main pass definition (used for visibility mask manipuluation). + /// \brief Main pass definition (used for visibility mask manipulation). public: Ogre::CompositorPassSceneDef *mainPassSceneDef = nullptr; /// \brief 1st pass compositor workspace. One for each cubemap camera @@ -327,27 +327,21 @@ void Ogre2LaserRetroMaterialSwitcher::passPreExecute( // get laser_retro Variant tempLaserRetro = ogreVisual->UserData(laserRetroKey); - try + if (std::holds_alternative(tempLaserRetro)) { retroValue = std::get(tempLaserRetro); } - catch(...) + else if (std::holds_alternative(tempLaserRetro)) { - try - { - retroValue = static_cast(std::get(tempLaserRetro)); - } - catch(...) - { - try - { - retroValue = static_cast(std::get(tempLaserRetro)); - } - catch(std::bad_variant_access &e) - { - gzerr << "Error casting user data: " << e.what() << "\n"; - } - } + retroValue = static_cast(std::get(tempLaserRetro)); + } + else if (std::holds_alternative(tempLaserRetro)) + { + retroValue = static_cast(std::get(tempLaserRetro)); + } + else + { + gzerr << "Error casting user data: laser_retro\n"; } } @@ -448,27 +442,21 @@ void Ogre2LaserRetroMaterialSwitcher::passPreExecute( // get laser_retro Variant tempLaserRetro = visual->UserData(laserRetroKey); - try + if (std::holds_alternative(tempLaserRetro)) { retroValue = std::get(tempLaserRetro); } - catch (...) + else if (std::holds_alternative(tempLaserRetro)) { - try - { - retroValue = static_cast(std::get(tempLaserRetro)); - } - catch (...) - { - try - { - retroValue = static_cast(std::get(tempLaserRetro)); - } - catch (std::bad_variant_access &e) - { - gzerr << "Error casting user data: " << e.what() << "\n"; - } - } + retroValue = static_cast(std::get(tempLaserRetro)); + } + else if (std::holds_alternative(tempLaserRetro)) + { + retroValue = static_cast(std::get(tempLaserRetro)); + } + else + { + gzerr << "Error casting user data: laser_retro\n"; } } diff --git a/ogre2/src/Ogre2ThermalCamera.cc b/ogre2/src/Ogre2ThermalCamera.cc index fa06f380f..9d225e452 100644 --- a/ogre2/src/Ogre2ThermalCamera.cc +++ b/ogre2/src/Ogre2ThermalCamera.cc @@ -90,12 +90,12 @@ class Ogre2ThermalCameraMaterialSwitcher : public Ogre::Camera::Listener /// \param[in] _resolution Temperature linear resolution public: void SetLinearResolution(double _resolution); - /// \brief Callback when a camara is about to be rendered + /// \brief Callback when a camera is about to be rendered /// \param[in] _cam Ogre camera pointer which is about to render private: virtual void cameraPreRenderScene( Ogre::Camera * _cam) override; - /// \brief Callback when a camera is finisned being rendered + /// \brief Callback when a camera is finished being rendered /// \param[in] _cam Ogre camera pointer which has already render private: virtual void cameraPostRenderScene( Ogre::Camera * _cam) override; @@ -279,38 +279,32 @@ void Ogre2ThermalCameraMaterialSwitcher::cameraPreRenderScene( Variant tempAny = ogreVisual->UserData(tempKey); if (tempAny.index() != 0 && !std::holds_alternative(tempAny)) { - float temp = -1.0; + float temp = -1.0f; bool foundTemp = true; - try + if (std::holds_alternative(tempAny)) { temp = std::get(tempAny); } - catch(...) + else if (std::holds_alternative(tempAny)) { - try - { - temp = static_cast(std::get(tempAny)); - } - catch(...) - { - try - { - temp = static_cast(std::get(tempAny)); - } - catch(std::bad_variant_access &e) - { - gzerr << "Error casting user data: " << e.what() << "\n"; - temp = -1.0; - foundTemp = false; - } - } + temp = static_cast(std::get(tempAny)); + } + else if (std::holds_alternative(tempAny)) + { + temp = static_cast(std::get(tempAny)); + } + else + { + gzerr << "Error casting user data: temperature\n"; + temp = -1.0f; + foundTemp = false; } // if a non-positive temperature was given, clamp it to 0 if (foundTemp && temp < 0.0) { temp = 0.0; - gzwarn << "Unable to set negatve temperature for: " + gzwarn << "Unable to set negative temperature for: " << ogreVisual->Name() << ". Value cannot be lower than absolute " << "zero. Clamping temperature to 0 degrees Kelvin." << std::endl; @@ -560,36 +554,30 @@ void Ogre2ThermalCameraMaterialSwitcher::cameraPreRenderScene( { float temp = -1.0; bool foundTemp = true; - try + if (std::holds_alternative(tempAny)) { temp = std::get(tempAny); } - catch (...) + else if (std::holds_alternative(tempAny)) { - try - { - temp = static_cast(std::get(tempAny)); - } - catch (...) - { - try - { - temp = static_cast(std::get(tempAny)); - } - catch (std::bad_variant_access &e) - { - gzerr << "Error casting user data: " << e.what() << "\n"; - temp = -1.0; - foundTemp = false; - } - } + temp = static_cast(std::get(tempAny)); + } + else if (std::holds_alternative(tempAny)) + { + temp = static_cast(std::get(tempAny)); + } + else + { + gzerr << "Error casting user data: temperature\n"; + temp = -1.0f; + foundTemp = false; } // if a non-positive temperature was given, clamp it to 0 if (foundTemp && temp < 0.0) { temp = 0.0; - gzwarn << "Unable to set negatve temperature for: " << visual->Name() + gzwarn << "Unable to set negative temperature for: " << visual->Name() << ". Value cannot be lower than absolute " << "zero. Clamping temperature to 0 degrees Kelvin." << std::endl; @@ -1068,7 +1056,7 @@ void Ogre2ThermalCamera::CreateThermalTexture() this->dataPtr->ogreThermalTexture->scheduleTransitionTo( Ogre::GpuResidency::Resident); - // create compositor worksspace + // create compositor workspace this->dataPtr->ogreCompositorWorkspace = ogreCompMgr->addWorkspace( this->scene->OgreSceneManager(), @@ -1078,7 +1066,7 @@ void Ogre2ThermalCamera::CreateThermalTexture() false); // add thermal material switcher to render target listener - // so we can switch to use heat material when the camera is being udpated + // so we can switch to use heat material when the camera is being updated Ogre::CompositorNode *node = this->dataPtr->ogreCompositorWorkspace->getNodeSequence()[0]; auto channels = node->getLocalTextures();