PLYwoot
Header-only C++17 library for parsing and writing PLY files
Loading...
Searching...
No Matches
binary_writer_policy.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_BINARY_WRITER_POLICY_HPP
21#define PLYWOOT_BINARY_WRITER_POLICY_HPP
22
24
25#include "buffered_ostream.hpp"
26#include "endian.hpp"
27
28#include <ostream>
29
30namespace plywoot::detail {
31
33template<typename Endianness>
34class BinaryWriterPolicy
35{
36public:
37 BinaryWriterPolicy(std::ostream &os) : os_{os} {}
38
41 template<typename T, typename EndiannessDependent = Endianness>
42 void writeNumber(T t) const
43 {
44 if constexpr (std::is_same_v<EndiannessDependent, HostEndian>)
45 {
46 os_.write(reinterpret_cast<const char *>(&t), sizeof(T));
47 }
48 else
49 {
50 const auto bs = byte_swap(t);
51 os_.write(reinterpret_cast<const char *>(&bs), sizeof(T));
52 }
53 }
54
59 template<typename PlySizeT, typename PlyT, typename SrcT, typename EndiannessDependent = Endianness>
60 void writeList(const SrcT *t, std::size_t n) const
61 {
62 writeNumber<PlySizeT>(n);
63 writeNumbers<PlyT, SrcT>(t, n);
64 }
65
68 template<typename PlyT, typename SrcT, typename EndiannessDependent = Endianness>
69 void writeNumbers(const SrcT *t, std::size_t n) const
70 {
71 if constexpr (std::is_same_v<PlyT, SrcT> && std::is_same_v<EndiannessDependent, HostEndian>)
72 {
73 os_.write(reinterpret_cast<const char *>(t), sizeof(SrcT) * n);
74 }
75 else
76 {
77 for (std::size_t i = 0; i < n; ++i) { writeNumber<PlyT>(*t++); }
78 }
79 }
80
86 void writeMissingProperties(PlyPropertyConstIterator first, PlyPropertyConstIterator last) const
87 {
88 while (first != last)
89 {
90 switch (first->isList() ? first->sizeType() : first->type())
91 {
92 case PlyDataType::Char:
93 writeNumber<char>(0);
94 break;
95 case PlyDataType::UChar:
96 writeNumber<unsigned char>(0);
97 break;
98 case PlyDataType::Short:
99 writeNumber<short>(0);
100 break;
101 case PlyDataType::UShort:
102 writeNumber<unsigned short>(0);
103 break;
104 case PlyDataType::Int:
105 writeNumber<int>(0);
106 break;
107 case PlyDataType::UInt:
108 writeNumber<unsigned int>(0);
109 break;
110 case PlyDataType::Float:
111 writeNumber<float>(0);
112 break;
113 case PlyDataType::Double:
114 writeNumber<double>(0);
115 break;
116 }
117 ++first;
118 }
119 }
120
122 void writeNewline() const {}
124 void writeTokenSeparator() const {}
125
126private:
127 mutable detail::BufferedOStream os_;
128};
129
131using BinaryLittleEndianWriterPolicy = BinaryWriterPolicy<LittleEndian>;
133using BinaryBigEndianWriterPolicy = BinaryWriterPolicy<BigEndian>;
134
135}
136
137#endif
BinaryWriterPolicy< BigEndian > BinaryBigEndianWriterPolicy
Convenience type alias for the binary big endian writer policy.
BinaryWriterPolicy< LittleEndian > BinaryLittleEndianWriterPolicy
Convenience type alias for the binary little endian writer policy.
T byte_swap(T t)
Definition endian.hpp:56