PLYwoot
Header-only C++17 library for parsing and writing PLY files
Loading...
Searching...
No Matches
ascii_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_ASCII_WRITER_POLICY_HPP
21#define PLYWOOT_ASCII_WRITER_POLICY_HPP
22
24
25#include "buffered_ostream.hpp"
26
27#include <ostream>
28
29namespace plywoot::detail {
30
31namespace {
32
33template<typename T>
34struct CharToInt
35{
36 template<typename U>
37 U operator()(U &&u) const
38 {
39 return u;
40 }
41
42 int operator()(char c) const { return static_cast<int>(c); }
43 int operator()(signed char c) const { return static_cast<int>(c); }
44 unsigned operator()(unsigned char c) const { return static_cast<unsigned>(c); }
45};
46
47}
48
50class AsciiWriterPolicy
51{
52public:
53 AsciiWriterPolicy(std::ostream &os) : os_{os} {}
54
57 template<typename T>
58 void writeNumber(T t) const
59 {
60 os_.writeAscii(CharToInt<T>{}(t));
61 }
62
68 template<typename PlySizeT, typename PlyT, typename SrcT>
69 void writeList(const SrcT *t, std::size_t n) const
70 {
71 os_.writeAscii(n);
72 if (n > 0)
73 {
74 os_.put(' ');
75 writeNumbers<PlyT, SrcT>(t, n);
76 }
77 }
78
83 template<typename PlyT, typename SrcT>
84 void writeNumbers(const SrcT *t, std::size_t n) const
85 {
86 for (std::size_t i = 0; i < n - 1; ++i)
87 {
88 os_.writeAscii(*t++);
89 os_.put(' ');
90 }
91 os_.writeAscii(*t);
92 }
93
98 void writeMissingProperties(PlyPropertyConstIterator first, PlyPropertyConstIterator last) const
99 {
100 while (first++ != last) { os_.write(" 0", 2); }
101 }
102
104 void writeNewline() const { os_.put('\n'); }
105
107 void writeTokenSeparator() const { os_.put(' '); }
108
109private:
110 mutable detail::BufferedOStream os_;
111};
112
113}
114
115#endif