Skip to content

Commit 533bae8

Browse files
committed
Introduce ApplyVisitorReader
1 parent 2f2e690 commit 533bae8

4 files changed

Lines changed: 143 additions & 0 deletions

File tree

include/vsg/all.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
267267
#include <vsg/vk/vulkan.h>
268268

269269
// Input/Output header files
270+
#include <vsg/io/ApplyVisitorReader.h>
270271
#include <vsg/io/AsciiInput.h>
271272
#include <vsg/io/AsciiOutput.h>
272273
#include <vsg/io/BinaryInput.h>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
/* <editor-fold desc="MIT License">
4+
5+
Copyright(c) 2025 Chris Djali
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12+
13+
</editor-fold> */
14+
15+
#include <vsg/io/ReaderWriter.h>
16+
17+
namespace vsg
18+
{
19+
/// Class to wrap a ReaderWriter and apply a Visitor to everything it loads
20+
class VSG_DECLSPEC ApplyVisitorReader : public Inherit<ReaderWriter, ApplyVisitorReader>
21+
{
22+
public:
23+
ApplyVisitorReader(vsg::ref_ptr<ReaderWriter> in_child, vsg::ref_ptr<Visitor> in_visitor);
24+
ApplyVisitorReader(const ApplyVisitorReader& rhs, const CopyOp& copyop = {});
25+
26+
vsg::ref_ptr<ReaderWriter> child;
27+
vsg::ref_ptr<Visitor> visitor;
28+
29+
void read(Input& input) override;
30+
void write(Output& output) const override;
31+
32+
vsg::ref_ptr<vsg::Object> read(const vsg::Path& filename, vsg::ref_ptr<const vsg::Options> options = {}) const override;
33+
vsg::ref_ptr<vsg::Object> read(std::istream& fin, vsg::ref_ptr<const vsg::Options> options = {}) const override;
34+
vsg::ref_ptr<vsg::Object> read(const uint8_t* ptr, size_t size, vsg::ref_ptr<const vsg::Options> options = {}) const override;
35+
36+
bool write(const vsg::Object* object, const vsg::Path& filename, vsg::ref_ptr<const vsg::Options> options = {}) const override;
37+
bool write(const vsg::Object* object, std::ostream& fout, vsg::ref_ptr<const vsg::Options> options = {}) const override;
38+
39+
bool readOptions(vsg::Options& options, vsg::CommandLine& arguments) const override;
40+
41+
bool getFeatures(Features& features) const override;
42+
43+
protected:
44+
mutable std::mutex _visitorMutex;
45+
};
46+
VSG_type_name(vsg::ApplyVisitorReader);
47+
48+
} // namespace vsg

src/vsg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ set(SOURCES
155155
io/read.cpp
156156
io/write.cpp
157157
io/mem_stream.cpp
158+
io/ApplyVisitorReader.cpp
158159

159160
text/CpuLayoutTechnique.cpp
160161
text/GpuLayoutTechnique.cpp

src/vsg/io/ApplyVisitorReader.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* <editor-fold desc="MIT License">
2+
3+
Copyright(c) 2025 Chris Djali
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
11+
</editor-fold> */
12+
13+
#include <vsg/io/ApplyVisitorReader.h>
14+
15+
using namespace vsg;
16+
17+
vsg::ApplyVisitorReader::ApplyVisitorReader(vsg::ref_ptr<ReaderWriter> in_child, vsg::ref_ptr<Visitor> in_visitor) :
18+
child(in_child),
19+
visitor(in_visitor)
20+
{
21+
}
22+
23+
vsg::ApplyVisitorReader::ApplyVisitorReader(const ApplyVisitorReader& rhs, const CopyOp& copyop) :
24+
Inherit(rhs),
25+
child(copyop(rhs.child)),
26+
visitor(copyop(rhs.visitor))
27+
{
28+
}
29+
30+
void ApplyVisitorReader::read(Input& input)
31+
{
32+
input.readObject("child", child);
33+
input.readObject("visitor", visitor);
34+
}
35+
36+
void ApplyVisitorReader::write(Output& output) const
37+
{
38+
output.writeObject("child", child);
39+
output.writeObject("visitor", visitor);
40+
}
41+
42+
vsg::ref_ptr<vsg::Object> ApplyVisitorReader::read(const vsg::Path& filename, ref_ptr<const Options> options) const
43+
{
44+
auto object = child->read(filename, options);
45+
if (object)
46+
{
47+
std::scoped_lock<std::mutex> lock(_visitorMutex);
48+
object->accept(*visitor);
49+
}
50+
return object;
51+
}
52+
53+
vsg::ref_ptr<vsg::Object> ApplyVisitorReader::read(std::istream& fin, ref_ptr<const Options> options) const
54+
{
55+
auto object = child->read(fin, options);
56+
if (object)
57+
{
58+
std::scoped_lock<std::mutex> lock(_visitorMutex);
59+
object->accept(*visitor);
60+
}
61+
return object;
62+
}
63+
64+
vsg::ref_ptr<vsg::Object> ApplyVisitorReader::read(const uint8_t* ptr, size_t size, vsg::ref_ptr<const vsg::Options> options) const
65+
{
66+
auto object = child->read(ptr, size, options);
67+
if (object)
68+
{
69+
std::scoped_lock<std::mutex> lock(_visitorMutex);
70+
object->accept(*visitor);
71+
}
72+
return object;
73+
}
74+
75+
bool ApplyVisitorReader::write(const vsg::Object* object, const vsg::Path& filename, ref_ptr<const Options> options) const
76+
{
77+
return child->write(object, filename, options);
78+
}
79+
80+
bool ApplyVisitorReader::write(const vsg::Object* object, std::ostream& fout, vsg::ref_ptr<const vsg::Options> options) const
81+
{
82+
return child->write(object, fout, options);
83+
}
84+
85+
bool ApplyVisitorReader::readOptions(vsg::Options& options, vsg::CommandLine& arguments) const
86+
{
87+
return child->readOptions(options, arguments);
88+
}
89+
90+
bool ApplyVisitorReader::getFeatures(Features& features) const
91+
{
92+
return child->getFeatures(features);
93+
}

0 commit comments

Comments
 (0)