dl
dl_stream_output.h
浏览该文件的文档.
1
12#pragma once
13
14#include <vector>
15#include <list>
16#include <map>
17#include <unordered_map>
18#include <string>
19
20#include "base/dl_concept.h"
21#include "math/dl_algorithm.h"
22#include "memory/dl_buffer.h"
23#include "memory/dl_memory.h"
24
25namespace dl
26{
29{
30public:
33
37 const std::byte* GetData();
38
39
41 {
42 return _buf;
43 }
45 {
46 return _buf;
47 }
48
52 template <IsCopyable T>
53 void Write(const T* data)
54 {
55 Write(data, sizeof(T));
56 }
57
58 //---------------------------------------------特殊内容写入---------------------------
62 template <typename T>
63 void Write64(const T* data)
64 {
65 uint64_t temp = uint64_t(*data);
66 Write(&temp, sizeof(uint64_t));//防止64位递归调用
67 }
68
71 void WriteCString(const char* str)
72 {
73 Write(str, strlen(str) + 1);
74 }
75
76 template <typename T>
77 void Write(const std::list<T>* container)
78 {
79 Write64(pointer(container->size()));
80 for (auto& iter : *container)
81 {
82 Write(&iter);
83 }
84 }
85
86 template <typename T>
87 void Write(const std::vector<T>* container)
88 {
89 Write64(pointer(container->size()));
90 for (auto& iter : *container)
91 {
92 Write(&iter);
93 }
94 }
95
96 void Write(const std::string* str)
97 {
98 Write64(pointer(str->size()));
99 for (auto& iter : *str)
100 {
101 Write(&iter);
102 }
103 }
104
105 template <typename K, typename V>
106 void Write(const std::map<K, V>* container)
107 {
108 Write64(pointer(container->size()));
109 for (auto& iter : *container)
110 {
111 Write(&iter.first);
112 Write(&iter.second);
113 }
114 }
115
116 template <typename K, typename V>
117 void Write(const std::unordered_map<K, V>* container)
118 {
119 Write64(pointer(container->size()));
120 for (auto& iter : *container)
121 {
122 Write(&iter.first);
123 Write(&iter.second);
124 }
125 }
126 //----------------------------------------------------------------------------
130 void Write(const void* data, size_t byte_count);
131
132
136 void Fill(int val, size_t byte_count);
137
141 bool Save(std::string_view path_name, bool compress = false);
142
146 void Clear();
147
151 size_t GetPos() { return _head; }
152
156 void SetPos(size_t pos);
157
162
166 void SetDelete(bool d)
167 {
168 _delete = d;
169 }
170private:
171 void _realloc(size_t size);
172
173 Buffer _buf;
174 size_t _head;
175 bool _delete;//默认会删除堆内存
176};
177}
void SetPos(size_t pos)
设置 流位置,如果内存不够会扩大
void Compress(Memory::CompressMode mode)
压缩当前内存
void Write(const std::map< K, V > *container)
void Write(const std::unordered_map< K, V > *container)
void WriteCString(const char *str)
写入C风格字符串
bool Save(std::string_view path_name, bool compress=false)
保存到文件
const std::byte * GetData()
返回指向的内存指针
void Write(const std::string *str)
void Write(const std::list< T > *container)
void Write(const std::vector< T > *container)
size_t GetPos()
返回 流位置
void Fill(int val, size_t byte_count)
填充一段数据,本质调用memset
void SetDelete(bool d)
标记内存已经被移走所有权,析构函数不再释放
void Write64(const T *data)
写入一个类型以64位,对于在64位平台的大小不一致,所以特殊处理
void Write(const T *data)
写入一个对象
void Write(const void *data, size_t byte_count)
写入一段数据,最基础操作
void Clear()
清除数据(移动head标记到内存最开始)
表示一段内存
用于模板限定,更好的报错信息
内存操作相关
std::span< std::byte > BufferView
T * pointer(T &&x)
查找算法
std::vector< std::byte > Buffer