dl
dl_model_cache.h
浏览该文件的文档.
1
10#pragma once
11
12#include <cstdint>
13#include <vector>
14#include <string>
15#include <unordered_map>
16
19
20namespace dl
21{
22//版本,不同则重新生成(修改ModelObject代码也需要更新)
23constexpr uint32_t VERSION_MODEL_CACHE = 2;
24
25//模型缓存文件
27{
28public:
29 struct Node
30 {
31 uint32_t _crc32;// 文件效验值
32 time_t _t; // 修改时间戳
33 std::vector<std::string> _allSub;//子模型缓存路径
35 {
36 s.Read(&_crc32);
37 s.Read(&_t);
38 s.Read(&_allSub);
39 }
41 {
42 s.Write(&_crc32);
43 s.Write(&_t);
44 s.Write(&_allSub);
45 }
46 };
47 Node* Get(std::string_view path_name)
48 {
49 auto iter = _allModel.find(std::string{ path_name });
50 if (iter == _allModel.end())
51 return nullptr;
52 return &iter->second;
53 }
54 Node* Add(std::string_view path_name)
55 {
56 auto p = _allModel.insert(std::make_pair(path_name, Node()));
57 if (p.second)
58 {
59 return &p.first->second;
60 }
61 return nullptr;
62 }
63 void Load(std::string_view path_name)
64 {
66 if (!s.LoadFromFile(path_name))
67 return;
68
69 uint32_t version;
70 s.Read(&version);
71 if (version != VERSION_MODEL_CACHE)
72 return;
73
74 size_t size;
75 s.Read64(&size);
76 for (size_t i = 0; i < size; ++i)
77 {
78 std::string name;
79 Node node;
80 s.Read(&name);
81 node.Load(s);
82 if (IsVaild(node))
83 _allModel[name] = node;
84 }
85 }
86 void Save(std::string_view path_name)
87 {
89
91
92 size_t size = _allModel.size();
93 s.Write64(&size);
94 for (auto& iter : _allModel)
95 {
96 s.Write(&iter.first);
97 iter.second.Save(s);
98 }
99
100 s.Save(path_name);
101 }
102 bool Empty() { return _allModel.empty(); }
103
104 bool IsVaild(const Node& node)
105 {
106 for (auto& iter : node._allSub)
107 {
108 if (!File::IsExist(iter))
109 return false;
110 }
111 return true;
112 }
113private:
114 //所有模型 文件名 -> node
115 std::unordered_map<std::string, Node> _allModel;
116};
117
118}
Node * Get(std::string_view path_name)
void Save(std::string_view path_name)
bool IsVaild(const Node &node)
void Load(std::string_view path_name)
Node * Add(std::string_view path_name)
二进制输入流
void Read64(T *data)
写入一个类型,写为64位,用于在64位平台的大小不一致,所以特殊处理
bool LoadFromFile(std::string_view path_name, bool decompress=false, size_t length=0)
从文件读到内存,可选长度
二进制输出流
bool Save(std::string_view path_name, bool compress=false)
保存到文件
void Write64(const T *data)
写入一个类型以64位,对于在64位平台的大小不一致,所以特殊处理
void Write(const T *data)
写入一个对象
bool IsExist(std::string_view path_name)
返回文件是否存在
constexpr uint32_t VERSION_MODEL_CACHE
void Save(StreamOutput &s)
void Load(StreamInput &s)
std::vector< std::string > _allSub