Pyrogenesis  trunk
write_buffer.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 Wildfire Games.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef INCLUDED_WRITE_BUFFER
24 #define INCLUDED_WRITE_BUFFER
25 
26 #include "lib/file/file.h"
27 
29 {
30 public:
31  WriteBuffer();
32 
33  void Append(const void* data, size_t size);
34  void Reserve(size_t size);
35  void Overwrite(const void* data, size_t size, size_t offset);
36 
37  std::shared_ptr<u8> Data() const
38  {
39  return m_data;
40  }
41 
42  size_t Size() const
43  {
44  return m_size;
45  }
46 
47 private:
48  void EnsureSufficientCapacity(size_t size);
49 
50  size_t m_capacity; // must come first (init order)
51 
52  std::shared_ptr<u8> m_data;
53  size_t m_size;
54 };
55 
56 
58 {
60 public:
61  UnalignedWriter(const PFile& file, off_t ofs);
62  ~UnalignedWriter();
63 
64  /**
65  * add data to the align buffer, writing it out to disk if full.
66  **/
67  Status Append(const u8* data, size_t size) const;
68 
69  /**
70  * zero-initialize any remaining space in the align buffer and write
71  * it to the file. this is called by the destructor.
72  **/
73  void Flush() const;
74 
75 private:
76  Status WriteBlock() const;
77 
79  std::shared_ptr<u8> m_alignedBuf;
81  mutable size_t m_bytesUsed;
82 };
83 
84 typedef std::shared_ptr<UnalignedWriter> PUnalignedWriter;
85 
86 #endif // #ifndef INCLUDED_WRITE_BUFFER
#define NONCOPYABLE(className)
Indicates that a class is noncopyable (usually due to const or reference members, or because the clas...
Definition: code_annotation.h:227
void Append(const void *data, size_t size)
Definition: write_buffer.cpp:54
void Reserve(size_t size)
Definition: write_buffer.cpp:62
PFile m_file
Definition: write_buffer.h:78
Definition: write_buffer.h:57
uint8_t u8
Definition: types.h:37
void Overwrite(const void *data, size_t size, size_t offset)
Definition: write_buffer.cpp:70
size_t m_size
Definition: write_buffer.h:53
void EnsureSufficientCapacity(size_t size)
Definition: write_buffer.cpp:41
std::shared_ptr< File > PFile
Definition: file.h:99
std::shared_ptr< u8 > m_alignedBuf
Definition: write_buffer.h:79
__int64 off_t
Definition: wposix_types.h:91
std::shared_ptr< u8 > m_data
Definition: write_buffer.h:52
size_t m_bytesUsed
Definition: write_buffer.h:81
i64 Status
Error handling system.
Definition: status.h:169
std::shared_ptr< UnalignedWriter > PUnalignedWriter
Definition: write_buffer.h:84
WriteBuffer()
Definition: write_buffer.cpp:35
size_t Size() const
Definition: write_buffer.h:42
Definition: write_buffer.h:28
off_t m_alignedOfs
Definition: write_buffer.h:80
size_t m_capacity
Definition: write_buffer.h:50
std::shared_ptr< u8 > Data() const
Definition: write_buffer.h:37