Skip to content

Commit 0a0209b

Browse files
committed
Introduce ScopeGuard to ensure streams are flushed.
Address typo and minor cleanup.
1 parent 7c00957 commit 0a0209b

2 files changed

Lines changed: 31 additions & 24 deletions

File tree

source/MaterialXTest/MaterialXRender/RenderUtil.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@
1919

2020
namespace mx = MaterialX;
2121

22+
namespace
23+
{
24+
25+
// RAII helper that invokes a callable on scope exit (normal or exception).
26+
template <typename F>
27+
class ScopeGuard
28+
{
29+
public:
30+
explicit ScopeGuard(F fn) : _fn(std::move(fn)) {}
31+
~ScopeGuard() { _fn(); }
32+
33+
ScopeGuard(const ScopeGuard&) = delete;
34+
ScopeGuard& operator=(const ScopeGuard&) = delete;
35+
36+
private:
37+
F _fn;
38+
};
39+
40+
} // anonymous namespace
41+
2242
namespace RenderUtil
2343
{
2444

@@ -45,6 +65,15 @@ bool ShaderRenderTester::validate(const mx::FilePath optionsFilePath)
4565
_logger.start(target, _testRun.options);
4666
_profiler.start();
4767

68+
// Ensure cleanup on any exit path (normal return or exception).
69+
ScopeGuard cleanup([this]() {
70+
_profiler.end(_testRun.options, _logger.profilingLog(), _testRun.dependLib);
71+
_logger.end();
72+
#ifdef MATERIALX_BUILD_PERFETTO_TRACING
73+
_tracer.end();
74+
#endif
75+
});
76+
4877
// Data search path
4978
_testRun.searchPath = mx::getDefaultDataSearchPath();
5079

@@ -81,12 +110,6 @@ bool ShaderRenderTester::validate(const mx::FilePath optionsFilePath)
81110
}
82111
}
83112

84-
_profiler.end(_testRun.options, _logger.profilingLog(), _testRun.dependLib);
85-
_logger.end();
86-
#ifdef MATERIALX_BUILD_PERFETTO_TRACING
87-
_tracer.end();
88-
#endif
89-
90113
return true;
91114
}
92115

@@ -105,7 +128,7 @@ void ShaderRenderTester::loadDependentLibraries()
105128
}
106129
}
107130

108-
// Load any addition per renderer libraries
131+
// Load any additional per-renderer libraries
109132
loadAdditionalLibraries(_testRun.dependLib, _testRun.options);
110133
}
111134

@@ -131,17 +154,6 @@ void ShaderRenderTester::getGenerationOptions(const GenShaderUtil::TestSuiteOpti
131154
}
132155
}
133156

134-
void ShaderRenderTester::printRunLog(const RenderProfileTimes &profileTimes,
135-
const GenShaderUtil::TestSuiteOptions& options,
136-
std::ostream& stream,
137-
mx::DocumentPtr)
138-
{
139-
profileTimes.print(stream);
140-
141-
stream << "---------------------------------------" << std::endl;
142-
options.print(stream);
143-
}
144-
145157
void ShaderRenderTester::addAdditionalTestStreams(mx::MeshPtr mesh)
146158
{
147159
size_t vertexCount = mesh->getVertexCount();

source/MaterialXTest/MaterialXRender/RenderUtil.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <MaterialXRender/Util.h>
1515

1616
#include <fstream>
17+
#include <iostream>
1718
#include <memory>
1819

1920
#define LOG_TO_FILE
@@ -225,12 +226,6 @@ class ShaderRenderTester
225226
const mx::GenOptions& originalOptions,
226227
std::vector<mx::GenOptions>& optionsList);
227228

228-
// Print execution summary
229-
void printRunLog(const RenderProfileTimes &profileTimes,
230-
const GenShaderUtil::TestSuiteOptions& options,
231-
std::ostream& stream,
232-
mx::DocumentPtr dependLib);
233-
234229
// If these streams don't exist add them for testing purposes
235230
void addAdditionalTestStreams(mx::MeshPtr mesh);
236231

0 commit comments

Comments
 (0)