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 "dl_concept.h"
21#include "dl_algorithm.h"
22#include "memory/dl_buffer.h"
23
24namespace dl
25{
26
28{
29public:
32
36 const std::byte* GetBuffer();
37
41 template <IsCopyable T>
42 void Write(const T* data)
43 {
44 Write(data, sizeof(T));
45 }
46
47 //---------------------------------------------特殊内容写入---------------------------
51 template <typename T>
52 void Write64(const T* data)
53 {
54 uint64_t temp = uint64_t(*data);
55 Write(&temp, sizeof(uint64_t));//防止64位递归调用
56 }
57
58 template <typename T>
59 void Write(const std::list<T>* container)
60 {
61 Write64(pointer(container->size()));
62 for (auto& iter : *container)
63 {
64 Write(&iter);
65 }
66 }
67
68 template <typename T>
69 void Write(const std::vector<T>* container)
70 {
71 Write64(pointer(container->size()));
72 for (auto& iter : *container)
73 {
74 Write(&iter);
75 }
76 }
77
78 void Write(const std::string* str)
79 {
80 Write64(pointer(str->size()));
81 for (auto& iter : *str)
82 {
83 Write(&iter);
84 }
85 }
86
87 template <typename K, typename V>
88 void Write(const std::map<K, V>* container)
89 {
90 Write64(pointer(container->size()));
91 for (auto& iter : *container)
92 {
93 Write(&iter.first);
94 Write(&iter.second);
95 }
96 }
97
98 template <typename K, typename V>
99 void Write(const std::unordered_map<K, V>* container)
100 {
101 Write64(pointer(container->size()));
102 for (auto& iter : *container)
103 {
104 Write(&iter.first);
105 Write(&iter.second);
106 }
107 }
108 //----------------------------------------------------------------------------
112 void Write(const void* data, size_t byte_count);
113
114
118 void Fill(int val, size_t byte_count);
119
123 bool Save(std::string_view path_name, bool compress = false);
124
128 void Clear();
129
133 size_t GetPos() { return _head; }
134
138 void SetPos(size_t pos);
139
140
144 void SetDelete(bool d)
145 {
146 _delete = d;
147 }
148private:
149 void _realloc(size_t size);
150
151 Buffer _buf;
152 size_t _head;
153 bool _delete;//默认会删除堆内存
154};
155}
void SetPos(size_t pos)
设置 流位置,如果内存不够会扩大
void Write(const std::map< K, V > *container)
void Write(const std::unordered_map< K, V > *container)
const std::byte * GetBuffer()
返回指向的内存指针
bool Save(std::string_view path_name, bool compress=false)
保存到文件
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标记到内存最开始)
表示一段内存
用于模板限定,更好的报错信息
Definition: dl_array2.h:17
T * pointer(T &&x)
查找算法
Definition: dl_algorithm.h:177
std::vector< std::byte > Buffer
Definition: dl_buffer.h:21