PLYwoot
Header-only C++17 library for parsing and writing PLY files
Loading...
Searching...
No Matches
writer_variant.hpp
Go to the documentation of this file.
1/*
2 This file is part of PLYwoot, a header-only PLY parser.
3
4 Copyright (C) 2023-2025, Ton van den Heuvel
5
6 PLYwoot is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef PLYWOOT_WRITER_VARIANT_HPP
21#define PLYWOOT_WRITER_VARIANT_HPP
22
24
27#include "writer.hpp"
28
29#include <ostream>
30#include <variant>
31
32namespace plywoot::detail {
33
34class WriterVariant
35{
36public:
37 WriterVariant(std::ostream &os, PlyFormat format) : variant_{makeVariant(os, format)} {}
38
39 void write(const PlyElement &element, const std::uint8_t *src, std::size_t alignment) const
40 {
41 std::visit(
42 [&element, src, alignment](auto &&writer) { writer.write(element, src, alignment); }, variant_);
43 }
44
45 template<typename... Ts>
46 void write(const PlyElement &element, const reflect::Layout<Ts...> layout) const
47 {
48 std::visit([&element, layout](auto &&writer) { writer.write(element, layout); }, variant_);
49 }
50
51private:
52 using Variant = std::variant<
53 detail::Writer<detail::AsciiWriterPolicy>,
54 detail::Writer<detail::BinaryBigEndianWriterPolicy>,
55 detail::Writer<detail::BinaryLittleEndianWriterPolicy>>;
56
57 Variant makeVariant(std::ostream &os, PlyFormat format)
58 {
59 switch (format)
60 {
61 case PlyFormat::Ascii:
62 return Variant{std::in_place_type<detail::Writer<detail::AsciiWriterPolicy>>, os};
63 case PlyFormat::BinaryBigEndian:
64 return Variant{std::in_place_type<detail::Writer<detail::BinaryBigEndianWriterPolicy>>, os};
65 case PlyFormat::BinaryLittleEndian:
66 default:
67 break;
68 }
69
70 return Variant{std::in_place_type<detail::Writer<detail::BinaryLittleEndianWriterPolicy>>, os};
71 }
72
73 Variant variant_;
74};
75
76}
77
78#endif