Skip to content

Commit 99031b7

Browse files
Change to std::get_if for gz::rendering::Variant for throwless behaviour (#1124)
Signed-off-by: kurohane <[email protected]>
1 parent b7973e1 commit 99031b7

File tree

5 files changed

+52
-56
lines changed

5 files changed

+52
-56
lines changed

ogre/src/OgreThermalCamera.cc

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#endif
2525

2626
#include <limits>
27+
#include <variant>
2728

2829
#include <gz/math/Helpers.hh>
2930
#include "gz/rendering/ShaderParams.hh"
@@ -218,29 +219,23 @@ Ogre::Technique *OgreThermalCameraMaterialSwitcher::handleSchemeNotFound(
218219
Variant tempAny = ogreVisual->UserData(tempKey);
219220
if (tempAny.index() != 0)
220221
{
221-
float temp = -1;
222-
try
222+
float temp = -1.0f;
223+
if (const float* floatPtr = std::get_if<float>(&tempAny))
224+
{
225+
temp = *floatPtr;
226+
}
227+
else if (const double* doublePtr = std::get_if<double>(&tempAny))
228+
{
229+
temp = static_cast<float>(*doublePtr);
230+
}
231+
else if (const int* intPtr = std::get_if<int>(&tempAny))
223232
{
224-
temp = std::get<float>(tempAny);
233+
temp = static_cast<float>(*intPtr);
225234
}
226-
catch(...)
235+
else
227236
{
228-
try
229-
{
230-
temp = static_cast<float>(std::get<double>(tempAny));
231-
}
232-
catch(...)
233-
{
234-
try
235-
{
236-
temp = static_cast<float>(std::get<int>(tempAny));
237-
}
238-
catch(std::bad_variant_access &e)
239-
{
240-
gzerr << "Error casting user data: " << e.what() << "\n";
241-
temp = -1.0;
242-
}
243-
}
237+
gzerr << "Error casting user data: variant containers unexpected type\n";
238+
temp = -1.0;
244239
}
245240

246241
// only accept positive temperature (in kelvin)

ogre2/src/Ogre2BoundingBoxMaterialSwitcher.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* limitations under the License.
1515
*
1616
*/
17+
18+
#include <variant>
19+
1720
#include "Ogre2BoundingBoxMaterialSwitcher.hh"
1821

1922
#include "gz/rendering/ogre2/Ogre2Scene.hh"
@@ -115,16 +118,12 @@ void Ogre2BoundingBoxMaterialSwitcher::cameraPreRenderScene(
115118

116119
// get class user data
117120
Variant labelAny = ogreVisual->UserData(this->labelKey);
118-
121+
// set default label to background
119122
int label = this->backgroundLabel;
120-
try
121-
{
122-
label = std::get<int>(labelAny);
123-
}
124-
catch(std::bad_variant_access &e)
123+
// if not background, then modify to label
124+
if (const int* labelPtr = std::get_if<int>(&labelAny))
125125
{
126-
// items with no class are considered background
127-
label = this->backgroundLabel;
126+
label = *labelPtr;
128127
}
129128

130129
// for full bbox, each pixel contains 1 channel for label

ogre2/src/Ogre2GpuRays.cc

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*
1616
*/
1717

18+
#include <variant>
19+
1820
#include <gz/math/Vector2.hh>
1921
#include <gz/math/Vector3.hh>
2022

@@ -326,18 +328,17 @@ void Ogre2LaserRetroMaterialSwitcher::passPreExecute(
326328
{
327329
// get laser_retro
328330
Variant tempLaserRetro = ogreVisual->UserData(laserRetroKey);
329-
330-
if (std::holds_alternative<float>(tempLaserRetro))
331+
if (const float* floatPtr = std::get_if<float>(&tempLaserRetro))
331332
{
332-
retroValue = std::get<float>(tempLaserRetro);
333+
retroValue = *floatPtr;
333334
}
334-
else if (std::holds_alternative<double>(tempLaserRetro))
335+
else if (const double* doublePtr = std::get_if<double>(&tempLaserRetro))
335336
{
336-
retroValue = static_cast<float>(std::get<double>(tempLaserRetro));
337+
retroValue = static_cast<float>(*doublePtr);
337338
}
338-
else if (std::holds_alternative<int>(tempLaserRetro))
339+
else if (const int* intPtr = std::get_if<int>(&tempLaserRetro))
339340
{
340-
retroValue = static_cast<float>(std::get<int>(tempLaserRetro));
341+
retroValue = static_cast<float>(*intPtr);
341342
}
342343
else
343344
{
@@ -442,17 +443,17 @@ void Ogre2LaserRetroMaterialSwitcher::passPreExecute(
442443
// get laser_retro
443444
Variant tempLaserRetro = visual->UserData(laserRetroKey);
444445

445-
if (std::holds_alternative<float>(tempLaserRetro))
446+
if (const float* floatPtr = std::get_if<float>(&tempLaserRetro))
446447
{
447-
retroValue = std::get<float>(tempLaserRetro);
448+
retroValue = *floatPtr;
448449
}
449-
else if (std::holds_alternative<double>(tempLaserRetro))
450+
else if (const double* doublePtr = std::get_if<double>(&tempLaserRetro))
450451
{
451-
retroValue = static_cast<float>(std::get<double>(tempLaserRetro));
452+
retroValue = static_cast<float>(*doublePtr);
452453
}
453-
else if (std::holds_alternative<int>(tempLaserRetro))
454+
else if (const int* intPtr = std::get_if<int>(&tempLaserRetro))
454455
{
455-
retroValue = static_cast<float>(std::get<int>(tempLaserRetro));
456+
retroValue = static_cast<float>(*intPtr);
456457
}
457458
else
458459
{

ogre2/src/Ogre2SegmentationMaterialSwitcher.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <algorithm>
2121
#include <utility>
2222
#include <vector>
23+
#include <variant>
2324

2425
#include <gz/common/Console.hh>
2526

@@ -86,11 +87,11 @@ Ogre::Vector4 Ogre2SegmentationMaterialSwitcher::ColorForVisual(
8687
// get class user data
8788
Variant labelAny = _visual->UserData("label");
8889
int label;
89-
try
90+
if (const int* labelPtr = std::get_if<int>(&labelAny))
9091
{
91-
label = std::get<int>(labelAny);
92+
label = *labelPtr;
9293
}
93-
catch (std::bad_variant_access &)
94+
else
9495
{
9596
// items with no class are considered background
9697
label = this->segmentationCamera->BackgroundLabel();

ogre2/src/Ogre2ThermalCamera.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,17 @@ void Ogre2ThermalCameraMaterialSwitcher::cameraPreRenderScene(
281281
{
282282
float temp = -1.0f;
283283
bool foundTemp = true;
284-
if (std::holds_alternative<float>(tempAny))
284+
if (const float* floatPtr = std::get_if<float>(&tempAny))
285285
{
286-
temp = std::get<float>(tempAny);
286+
temp = *floatPtr;
287287
}
288-
else if (std::holds_alternative<double>(tempAny))
288+
else if (const double* doublePtr = std::get_if<double>(&tempAny))
289289
{
290-
temp = static_cast<float>(std::get<double>(tempAny));
290+
temp = static_cast<float>(*doublePtr);
291291
}
292-
else if (std::holds_alternative<int>(tempAny))
292+
else if (const int* intPtr = std::get_if<int>(&tempAny))
293293
{
294-
temp = static_cast<float>(std::get<int>(tempAny));
294+
temp = static_cast<float>(*intPtr);
295295
}
296296
else
297297
{
@@ -554,17 +554,17 @@ void Ogre2ThermalCameraMaterialSwitcher::cameraPreRenderScene(
554554
{
555555
float temp = -1.0;
556556
bool foundTemp = true;
557-
if (std::holds_alternative<float>(tempAny))
557+
if (const float* floatPtr = std::get_if<float>(&tempAny))
558558
{
559-
temp = std::get<float>(tempAny);
559+
temp = *floatPtr;
560560
}
561-
else if (std::holds_alternative<double>(tempAny))
561+
else if (const double* doublePtr = std::get_if<double>(&tempAny))
562562
{
563-
temp = static_cast<float>(std::get<double>(tempAny));
563+
temp = static_cast<float>(*doublePtr);
564564
}
565-
else if (std::holds_alternative<int>(tempAny))
565+
else if (const int* intPtr = std::get_if<int>(&tempAny))
566566
{
567-
temp = static_cast<float>(std::get<int>(tempAny));
567+
temp = static_cast<float>(*intPtr);
568568
}
569569
else
570570
{

0 commit comments

Comments
 (0)