vsg 1.1.3
VulkanSceneGraph library
Loading...
Searching...
No Matches
ReaderWriter.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission 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
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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/core/Inherit.h>
16#include <vsg/io/FileSystem.h>
17#include <vsg/io/Options.h>
18
19namespace vsg
20{
21
24 struct ReadError : public Inherit<Object, ReadError>
25 {
26 explicit ReadError(const std::string& msg) :
27 message(msg) {}
28
29 std::string message;
30 };
31 VSG_type_name(vsg::ReadError);
32
34 class VSG_DECLSPEC ReaderWriter : public Inherit<Object, ReaderWriter>
35 {
36 public:
37 using vsg::Object::read;
38 using vsg::Object::write;
39
41 template<class T>
43 {
44 auto object = read(filename, options);
45 return vsg::ref_ptr<T>(dynamic_cast<T*>(object.get()));
46 }
47
49 template<class T>
50 vsg::ref_ptr<T> read_cast(std::istream& fin, vsg::ref_ptr<const vsg::Options> options = {}) const
51 {
52 auto object = read(fin, options);
53 return vsg::ref_ptr<T>(dynamic_cast<T*>(object.get()));
54 }
55
57 template<class T>
58 vsg::ref_ptr<T> read_cast(const uint8_t* ptr, size_t size, vsg::ref_ptr<const vsg::Options> options = {}) const
59 {
60 auto object = read(ptr, size, options);
61 return vsg::ref_ptr<T>(dynamic_cast<T*>(object.get()));
62 }
63
66
68 virtual vsg::ref_ptr<vsg::Object> read(std::istream& /*fin*/, vsg::ref_ptr<const vsg::Options> = {}) const { return vsg::ref_ptr<vsg::Object>(); }
69
71 virtual vsg::ref_ptr<vsg::Object> read(const uint8_t* /*ptr*/, size_t /*size*/, vsg::ref_ptr<const vsg::Options> = {}) const { return vsg::ref_ptr<vsg::Object>(); }
72
74 virtual bool write(const vsg::Object* /*object*/, const vsg::Path& /*filename*/, vsg::ref_ptr<const vsg::Options> = {}) const { return false; }
75
77 virtual bool write(const vsg::Object* /*object*/, std::ostream& /*fout*/, vsg::ref_ptr<const vsg::Options> = {}) const { return false; }
78
80 virtual bool readOptions(Options&, CommandLine&) const { return false; }
81
82 enum FeatureMask
83 {
84 READ_FILENAME = (1 << 0),
85 READ_ISTREAM = (1 << 1),
86 READ_MEMORY = (1 << 2),
87 WRITE_FILENAME = (1 << 3),
88 WRITE_OSTREAM = (1 << 4)
89 };
90
91 struct Features
92 {
93 std::map<vsg::Path, FeatureMask> protocolFeatureMap;
94 std::map<vsg::Path, FeatureMask> extensionFeatureMap;
95 std::map<std::string, std::string> optionNameTypeMap;
96 };
97
99 virtual bool getFeatures(Features&) const { return false; }
100 };
101 VSG_type_name(vsg::ReaderWriter);
102
104 class VSG_DECLSPEC CompositeReaderWriter : public Inherit<ReaderWriter, CompositeReaderWriter>
105 {
106 public:
107 using ReaderWriters = std::vector<vsg::ref_ptr<ReaderWriter>>;
108 ReaderWriters readerWriters;
109
110 void add(ref_ptr<ReaderWriter> reader);
111
112 void read(Input& input) override;
113 void write(Output& output) const override;
114
115 vsg::ref_ptr<vsg::Object> read(const vsg::Path& filename, vsg::ref_ptr<const vsg::Options> options = {}) const override;
116 vsg::ref_ptr<vsg::Object> read(std::istream& fin, vsg::ref_ptr<const vsg::Options> options = {}) const override;
117 vsg::ref_ptr<vsg::Object> read(const uint8_t* ptr, size_t size, vsg::ref_ptr<const vsg::Options> options = {}) const override;
118
119 bool write(const vsg::Object* object, const vsg::Path& filename, vsg::ref_ptr<const vsg::Options> options = {}) const override;
120 bool write(const vsg::Object* object, std::ostream& fout, vsg::ref_ptr<const vsg::Options> options = {}) const override;
121
123 bool readOptions(vsg::Options& options, vsg::CommandLine& arguments) const override;
124
125 bool getFeatures(Features& features) const override;
126
127 protected:
128 };
129 VSG_type_name(vsg::CompositeReaderWriter);
130
131} // namespace vsg
Definition CommandLine.h:44
Class for managing a list of ReaderWriter, providing a single read/write call to invoke each ReaderWr...
Definition ReaderWriter.h:105
vsg::ref_ptr< vsg::Object > read(const uint8_t *ptr, size_t size, vsg::ref_ptr< const vsg::Options > options={}) const override
read object from memory block, return object on success, return null ref_ptr<> if format not supporte...
bool getFeatures(Features &features) const override
get the Features supported by this ReaderWriter
bool readOptions(vsg::Options &options, vsg::CommandLine &arguments) const override
read the command line arguments for any options appropriate for this ReaderWriter
vsg::ref_ptr< vsg::Object > read(std::istream &fin, vsg::ref_ptr< const vsg::Options > options={}) const override
read object from input stream, return object on success, return null ref_ptr<> if format not supporte...
bool write(const vsg::Object *object, const vsg::Path &filename, vsg::ref_ptr< const vsg::Options > options={}) const override
write object to file, return true on success, return false on failure.
vsg::ref_ptr< vsg::Object > read(const vsg::Path &filename, vsg::ref_ptr< const vsg::Options > options={}) const override
read object from file, return object on success, return null ref_ptr<> if format not supported,...
bool write(const vsg::Object *object, std::ostream &fout, vsg::ref_ptr< const vsg::Options > options={}) const override
write object to output stream, return true on success, return false on failure.
Definition Inherit.h:28
Definition Input.h:44
Definition Object.h:60
Class for passing IO related options to vsg::read/write calls.
Definition Options.h:37
Definition Output.h:41
Definition Path.h:34
Base class for providing support for reading and/or writing various file formats and IO protocols.
Definition ReaderWriter.h:35
virtual bool readOptions(Options &, CommandLine &) const
read the command line arguments for any options appropriate for this ReaderWriter
Definition ReaderWriter.h:80
vsg::ref_ptr< T > read_cast(std::istream &fin, vsg::ref_ptr< const vsg::Options > options={}) const
convenience method for casting a read object to a specified type.
Definition ReaderWriter.h:50
vsg::ref_ptr< T > read_cast(const vsg::Path &filename, vsg::ref_ptr< const vsg::Options > options={}) const
convenience method for casting a read object to a specified type.
Definition ReaderWriter.h:42
virtual bool write(const vsg::Object *, const vsg::Path &, vsg::ref_ptr< const vsg::Options >={}) const
write object to file, return true on success, return false on failure.
Definition ReaderWriter.h:74
virtual bool getFeatures(Features &) const
get the Features supported by this ReaderWriter
Definition ReaderWriter.h:99
virtual vsg::ref_ptr< vsg::Object > read(const uint8_t *, size_t, vsg::ref_ptr< const vsg::Options >={}) const
read object from memory block, return object on success, return null ref_ptr<> if format not supporte...
Definition ReaderWriter.h:71
virtual vsg::ref_ptr< vsg::Object > read(const vsg::Path &, vsg::ref_ptr< const vsg::Options >={}) const
read object from file, return object on success, return null ref_ptr<> if format not supported,...
Definition ReaderWriter.h:65
virtual bool write(const vsg::Object *, std::ostream &, vsg::ref_ptr< const vsg::Options >={}) const
write object to output stream, return true on success, return false on failure.
Definition ReaderWriter.h:77
virtual vsg::ref_ptr< vsg::Object > read(std::istream &, vsg::ref_ptr< const vsg::Options >={}) const
read object from input stream, return object on success, return null ref_ptr<> if format not supporte...
Definition ReaderWriter.h:68
vsg::ref_ptr< T > read_cast(const uint8_t *ptr, size_t size, vsg::ref_ptr< const vsg::Options > options={}) const
convenience method for casting a read object to a specified type.
Definition ReaderWriter.h:58
Definition ref_ptr.h:22
Definition ReaderWriter.h:25
Definition ReaderWriter.h:92