UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
write_buffer.cpp
1
42// UFO
43#include <ufo/utility/io/write_buffer.hpp>
44
45// STL
46#include <cstring>
47
48namespace ufo
49{
50
51WriteBuffer::WriteBuffer(WriteBuffer const& other)
52{
53 if (other.data_) {
54 write(other.data_.get(), other.size_);
55 }
56}
57
58WriteBuffer& WriteBuffer::operator=(WriteBuffer const& rhs)
59{
60 size_ = {};
61 pos_ = {};
62 if (rhs.data_) {
63 write(rhs.data_.get(), rhs.size_);
64 }
65 return *this;
66}
67
68WriteBuffer& WriteBuffer::write(void const* src, size_type count)
69{
70 writeAt(pos_, src, count);
71 pos_ += count;
72 return *this;
73}
74
75WriteBuffer& WriteBuffer::write(std::istream& in, size_type count)
76{
77 writeAt(pos_, in, count);
78 pos_ += count;
79 return *this;
80}
81
82void WriteBuffer::writeAt(pos_type pos, void const* src, size_type count)
83{
84 reserve(pos + count);
85
86 std::memmove(data_.get() + pos, src, count);
87
88 size_ = std::max(size_, pos + count);
89}
90
91void WriteBuffer::writeAt(pos_type pos, std::istream& in, size_type count)
92{
93 reserve(pos + count);
94
95 in.read(reinterpret_cast<char*>(data_.get() + pos),
96 static_cast<std::streamsize>(count));
97
98 size_ = std::max(size_, pos + count);
99}
100
101void WriteBuffer::writeAt(offset_type off, IODir dir, void const* src, size_type count)
102{
103 return writeAt(pos(pos_, off, dir), src, count);
104}
105
106void WriteBuffer::writeAt(offset_type off, IODir dir, std::istream& in, size_type count)
107{
108 return writeAt(pos(pos_, off, dir), in, count);
109}
110
111void WriteBuffer::reserve(size_type new_cap)
112{
113 if (cap_ >= new_cap) [[likely]] {
114 return;
115 }
116
117 std::byte* prev = data_.get();
118 void* next = realloc(prev, new_cap * sizeof(std::byte));
119
120 if (nullptr != next) [[likely]] {
121 [[maybe_unused]] auto prev = data_.release();
122 data_.reset(static_cast<std::byte*>(next));
123 BaseBuffer::data_ = static_cast<std::byte const*>(next);
124 cap_ = new_cap;
125 } else [[unlikely]] {
126 free(prev);
127 BaseBuffer::data_ = nullptr;
128 size_ = {};
129 cap_ = {};
130 pos_ = {};
131 // TODO: Add message
132 throw std::bad_alloc();
133 }
134}
135
136void WriteBuffer::clear()
137{
138 size_ = 0;
139 pos_ = 0;
140}
141
142std::byte* WriteBuffer::data() { return data_.get(); }
143
144WriteBuffer::size_type WriteBuffer::capacity() const noexcept { return cap_; }
145
146WriteBuffer::pos_type WriteBuffer::writePos() const noexcept { return pos_; }
147
148WriteBuffer& WriteBuffer::writeSeek(pos_type pos) noexcept
149{
150 pos_ = pos;
151 return *this;
152}
153
154WriteBuffer& WriteBuffer::writeSeek(offset_type off, IODir dir) noexcept
155{
156 return writeSeek(pos(pos_, off, dir));
157}
158
159WriteBuffer::size_type WriteBuffer::writeLeft() const noexcept
160{
161 return size_ < pos_ ? 0 : pos_ - size_;
162}
163} // namespace ufo
All vision-related classes and functions.
Definition cloud.hpp:49