Skip to content

Commit 4a655ff

Browse files
committed
Better approach to use std::variant
Signed-off-by: Maksim Derbasov <[email protected]>
1 parent 9e874c0 commit 4a655ff

File tree

2 files changed

+59
-83
lines changed

2 files changed

+59
-83
lines changed

ogre2/src/Ogre2GpuRays.cc

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class GZ_RENDERING_OGRE2_HIDDEN
8484
private: virtual void passPreExecute(
8585
Ogre::CompositorPass *_pass) override;
8686

87-
/// \brief Callback when each pass is finisned executing.
87+
/// \brief Callback when each pass is finished executing.
8888
/// \param[in] _pass Ogre pass which has already executed
8989
private: virtual void passPosExecute(
9090
Ogre::CompositorPass *_pass) override;
@@ -156,7 +156,7 @@ class GZ_RENDERING_OGRE2_HIDDEN gz::rendering::Ogre2GpuRaysPrivate
156156
/// range data
157157
public: std::set<unsigned int> cubeFaceIdx;
158158

159-
/// \brief Main pass definition (used for visibility mask manipuluation).
159+
/// \brief Main pass definition (used for visibility mask manipulation).
160160
public: Ogre::CompositorPassSceneDef *mainPassSceneDef = nullptr;
161161

162162
/// \brief 1st pass compositor workspace. One for each cubemap camera
@@ -295,7 +295,7 @@ void Ogre2LaserRetroMaterialSwitcher::passPreExecute(
295295
const Ogre::HlmsBlendblock *noBlend =
296296
hlmsManager->getBlendblock(Ogre::HlmsBlendblock());
297297

298-
const std::string laserRetroKey = "laser_retro";
298+
static const std::string laserRetroKey = "laser_retro";
299299

300300
auto itor = this->scene->OgreSceneManager()->getMovableObjectIterator(
301301
Ogre::ItemFactory::FACTORY_TYPE_NAME);
@@ -327,27 +327,21 @@ void Ogre2LaserRetroMaterialSwitcher::passPreExecute(
327327
// get laser_retro
328328
Variant tempLaserRetro = ogreVisual->UserData(laserRetroKey);
329329

330-
try
330+
if (std::holds_alternative<float>(tempLaserRetro))
331331
{
332332
retroValue = std::get<float>(tempLaserRetro);
333333
}
334-
catch(...)
334+
else if (std::holds_alternative<double>(tempLaserRetro))
335335
{
336-
try
337-
{
338-
retroValue = static_cast<float>(std::get<double>(tempLaserRetro));
339-
}
340-
catch(...)
341-
{
342-
try
343-
{
344-
retroValue = static_cast<float>(std::get<int>(tempLaserRetro));
345-
}
346-
catch(std::bad_variant_access &e)
347-
{
348-
gzerr << "Error casting user data: " << e.what() << "\n";
349-
}
350-
}
336+
retroValue = static_cast<float>(std::get<double>(tempLaserRetro));
337+
}
338+
else if (std::holds_alternative<int>(tempLaserRetro))
339+
{
340+
retroValue = static_cast<float>(std::get<int>(tempLaserRetro));
341+
}
342+
else
343+
{
344+
gzerr << "Error casting user data: laser_retro\n";
351345
}
352346
}
353347

@@ -448,27 +442,21 @@ void Ogre2LaserRetroMaterialSwitcher::passPreExecute(
448442
// get laser_retro
449443
Variant tempLaserRetro = visual->UserData(laserRetroKey);
450444

451-
try
445+
if (std::holds_alternative<float>(tempLaserRetro))
452446
{
453447
retroValue = std::get<float>(tempLaserRetro);
454448
}
455-
catch (...)
449+
else if (std::holds_alternative<double>(tempLaserRetro))
456450
{
457-
try
458-
{
459-
retroValue = static_cast<float>(std::get<double>(tempLaserRetro));
460-
}
461-
catch (...)
462-
{
463-
try
464-
{
465-
retroValue = static_cast<float>(std::get<int>(tempLaserRetro));
466-
}
467-
catch (std::bad_variant_access &e)
468-
{
469-
gzerr << "Error casting user data: " << e.what() << "\n";
470-
}
471-
}
451+
retroValue = static_cast<float>(std::get<double>(tempLaserRetro));
452+
}
453+
else if (std::holds_alternative<int>(tempLaserRetro))
454+
{
455+
retroValue = static_cast<float>(std::get<int>(tempLaserRetro));
456+
}
457+
else
458+
{
459+
gzerr << "Error casting user data: laser_retro\n";
472460
}
473461
}
474462

ogre2/src/Ogre2ThermalCamera.cc

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ class Ogre2ThermalCameraMaterialSwitcher : public Ogre::Camera::Listener
9090
/// \param[in] _resolution Temperature linear resolution
9191
public: void SetLinearResolution(double _resolution);
9292

93-
/// \brief Callback when a camara is about to be rendered
93+
/// \brief Callback when a camera is about to be rendered
9494
/// \param[in] _cam Ogre camera pointer which is about to render
9595
private: virtual void cameraPreRenderScene(
9696
Ogre::Camera * _cam) override;
9797

98-
/// \brief Callback when a camera is finisned being rendered
98+
/// \brief Callback when a camera is finished being rendered
9999
/// \param[in] _cam Ogre camera pointer which has already render
100100
private: virtual void cameraPostRenderScene(
101101
Ogre::Camera * _cam) override;
@@ -250,7 +250,7 @@ void Ogre2ThermalCameraMaterialSwitcher::cameraPreRenderScene(
250250
const Ogre::HlmsBlendblock *noBlend =
251251
hlmsManager->getBlendblock(Ogre::HlmsBlendblock());
252252

253-
const std::string tempKey = "temperature";
253+
static const std::string tempKey = "temperature";
254254

255255
auto itor = this->scene->OgreSceneManager()->getMovableObjectIterator(
256256
Ogre::ItemFactory::FACTORY_TYPE_NAME);
@@ -279,38 +279,32 @@ void Ogre2ThermalCameraMaterialSwitcher::cameraPreRenderScene(
279279
Variant tempAny = ogreVisual->UserData(tempKey);
280280
if (tempAny.index() != 0 && !std::holds_alternative<std::string>(tempAny))
281281
{
282-
float temp = -1.0;
282+
float temp = -1.0f;
283283
bool foundTemp = true;
284-
try
284+
if (std::holds_alternative<float>(tempAny))
285285
{
286286
temp = std::get<float>(tempAny);
287287
}
288-
catch(...)
288+
else if (std::holds_alternative<double>(tempAny))
289289
{
290-
try
291-
{
292-
temp = static_cast<float>(std::get<double>(tempAny));
293-
}
294-
catch(...)
295-
{
296-
try
297-
{
298-
temp = static_cast<float>(std::get<int>(tempAny));
299-
}
300-
catch(std::bad_variant_access &e)
301-
{
302-
gzerr << "Error casting user data: " << e.what() << "\n";
303-
temp = -1.0;
304-
foundTemp = false;
305-
}
306-
}
290+
temp = static_cast<float>(std::get<double>(tempAny));
291+
}
292+
else if (std::holds_alternative<int>(tempAny))
293+
{
294+
temp = static_cast<float>(std::get<int>(tempAny));
295+
}
296+
else
297+
{
298+
gzerr << "Error casting user data: temperature\n";
299+
temp = -1.0f;
300+
foundTemp = false;
307301
}
308302

309303
// if a non-positive temperature was given, clamp it to 0
310304
if (foundTemp && temp < 0.0)
311305
{
312306
temp = 0.0;
313-
gzwarn << "Unable to set negatve temperature for: "
307+
gzwarn << "Unable to set negative temperature for: "
314308
<< ogreVisual->Name() << ". Value cannot be lower than absolute "
315309
<< "zero. Clamping temperature to 0 degrees Kelvin."
316310
<< std::endl;
@@ -560,36 +554,30 @@ void Ogre2ThermalCameraMaterialSwitcher::cameraPreRenderScene(
560554
{
561555
float temp = -1.0;
562556
bool foundTemp = true;
563-
try
557+
if (std::holds_alternative<float>(tempAny))
564558
{
565559
temp = std::get<float>(tempAny);
566560
}
567-
catch (...)
561+
else if (std::holds_alternative<double>(tempAny))
568562
{
569-
try
570-
{
571-
temp = static_cast<float>(std::get<double>(tempAny));
572-
}
573-
catch (...)
574-
{
575-
try
576-
{
577-
temp = static_cast<float>(std::get<int>(tempAny));
578-
}
579-
catch (std::bad_variant_access &e)
580-
{
581-
gzerr << "Error casting user data: " << e.what() << "\n";
582-
temp = -1.0;
583-
foundTemp = false;
584-
}
585-
}
563+
temp = static_cast<float>(std::get<double>(tempAny));
564+
}
565+
else if (std::holds_alternative<int>(tempAny))
566+
{
567+
temp = static_cast<float>(std::get<int>(tempAny));
568+
}
569+
else
570+
{
571+
gzerr << "Error casting user data: temperature\n";
572+
temp = -1.0f;
573+
foundTemp = false;
586574
}
587575

588576
// if a non-positive temperature was given, clamp it to 0
589577
if (foundTemp && temp < 0.0)
590578
{
591579
temp = 0.0;
592-
gzwarn << "Unable to set negatve temperature for: " << visual->Name()
580+
gzwarn << "Unable to set negative temperature for: " << visual->Name()
593581
<< ". Value cannot be lower than absolute "
594582
<< "zero. Clamping temperature to 0 degrees Kelvin."
595583
<< std::endl;
@@ -1068,7 +1056,7 @@ void Ogre2ThermalCamera::CreateThermalTexture()
10681056
this->dataPtr->ogreThermalTexture->scheduleTransitionTo(
10691057
Ogre::GpuResidency::Resident);
10701058

1071-
// create compositor worksspace
1059+
// create compositor workspace
10721060
this->dataPtr->ogreCompositorWorkspace =
10731061
ogreCompMgr->addWorkspace(
10741062
this->scene->OgreSceneManager(),
@@ -1078,7 +1066,7 @@ void Ogre2ThermalCamera::CreateThermalTexture()
10781066
false);
10791067

10801068
// add thermal material switcher to render target listener
1081-
// so we can switch to use heat material when the camera is being udpated
1069+
// so we can switch to use heat material when the camera is being updated
10821070
Ogre::CompositorNode *node =
10831071
this->dataPtr->ogreCompositorWorkspace->getNodeSequence()[0];
10841072
auto channels = node->getLocalTextures();

0 commit comments

Comments
 (0)