GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
metadata.h
Go to the documentation of this file.
1// Copyright 2017 The Draco Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15#ifndef DRACO_METADATA_METADATA_H_
16#define DRACO_METADATA_METADATA_H_
17
18#include <cstring>
19#include <map>
20#include <memory>
21#include <string>
22#include <vector>
23
25
26namespace draco {
27
28// Class for storing a value of an entry in Metadata. Internally it is
29// represented by a buffer of data. It can be accessed by various data types,
30// e.g. int, float, binary data or string.
32 public:
33 template <typename DataTypeT>
34 explicit EntryValue(const DataTypeT &data) {
35 const size_t data_type_size = sizeof(DataTypeT);
36 data_.resize(data_type_size);
37 memcpy(&data_[0], &data, data_type_size);
38 }
39
40 template <typename DataTypeT>
41 explicit EntryValue(const std::vector<DataTypeT> &data) {
42 const size_t total_size = sizeof(DataTypeT) * data.size();
43 data_.resize(total_size);
44 memcpy(&data_[0], &data[0], total_size);
45 }
46
47 EntryValue(const EntryValue &value);
48 explicit EntryValue(const std::string &value);
49
50 template <typename DataTypeT>
51 bool GetValue(DataTypeT *value) const {
52 const size_t data_type_size = sizeof(DataTypeT);
53 if (data_type_size != data_.size()) {
54 return false;
55 }
56 memcpy(value, &data_[0], data_type_size);
57 return true;
58 }
59
60 template <typename DataTypeT>
61 bool GetValue(std::vector<DataTypeT> *value) const {
62 if (data_.empty()) {
63 return false;
64 }
65 const size_t data_type_size = sizeof(DataTypeT);
66 if (data_.size() % data_type_size != 0) {
67 return false;
68 }
69 value->resize(data_.size() / data_type_size);
70 memcpy(&value->at(0), &data_[0], data_.size());
71 return true;
72 }
73
74 const std::vector<uint8_t> &data() const { return data_; }
75
76 private:
77 std::vector<uint8_t> data_;
78
79 friend struct EntryValueHasher;
80};
81
82// Functor for computing a hash from data stored within an EntryValue.
84 size_t operator()(const EntryValue &ev) const {
85 size_t hash = ev.data_.size();
86 for (size_t i = 0; i < ev.data_.size(); ++i) {
87 hash = HashCombine(ev.data_[i], hash);
88 }
89 return hash;
90 }
91};
92
93// Class for holding generic metadata. It has a list of entries which consist of
94// an entry name and an entry value. Each Metadata could also have nested
95// metadata.
96class Metadata {
97 public:
99 Metadata(const Metadata &metadata);
100 // In theory, we support all types of data as long as it could be serialized
101 // to binary data. We provide the following functions for inserting and
102 // accessing entries of common data types. For now, developers need to know
103 // the type of entries they are requesting.
104 void AddEntryInt(const std::string &name, int32_t value);
105
106 // Returns false if Metadata does not contain an entry with a key of |name|.
107 // This function does not guarantee that entry's type is int32_t.
108 bool GetEntryInt(const std::string &name, int32_t *value) const;
109
110 void AddEntryIntArray(const std::string &name,
111 const std::vector<int32_t> &value);
112
113 // Returns false if Metadata does not contain an entry with a key of |name|.
114 // This function does not guarantee that entry's type is a vector of int32_t.
115 bool GetEntryIntArray(const std::string &name,
116 std::vector<int32_t> *value) const;
117
118 void AddEntryDouble(const std::string &name, double value);
119
120 // Returns false if Metadata does not contain an entry with a key of |name|.
121 // This function does not guarantee that entry's type is double.
122 bool GetEntryDouble(const std::string &name, double *value) const;
123
124 void AddEntryDoubleArray(const std::string &name,
125 const std::vector<double> &value);
126
127 // Returns false if Metadata does not contain an entry with a key of |name|.
128 // This function does not guarantee that entry's type is a vector of double.
129 bool GetEntryDoubleArray(const std::string &name,
130 std::vector<double> *value) const;
131
132 void AddEntryString(const std::string &name, const std::string &value);
133
134 // Returns false if Metadata does not contain an entry with a key of |name|.
135 // This function does not guarantee that entry's type is std::string.
136 bool GetEntryString(const std::string &name, std::string *value) const;
137
138 // Add a blob of data as an entry.
139 void AddEntryBinary(const std::string &name,
140 const std::vector<uint8_t> &value);
141
142 // Returns false if Metadata does not contain an entry with a key of |name|.
143 // This function does not guarantee that entry's type is a vector of uint8_t.
144 bool GetEntryBinary(const std::string &name,
145 std::vector<uint8_t> *value) const;
146
147 bool AddSubMetadata(const std::string &name,
148 std::unique_ptr<Metadata> sub_metadata);
149 const Metadata *GetSubMetadata(const std::string &name) const;
150 Metadata *sub_metadata(const std::string &name);
151
152 void RemoveEntry(const std::string &name);
153
154 int num_entries() const { return static_cast<int>(entries_.size()); }
155 const std::map<std::string, EntryValue> &entries() const { return entries_; }
156 const std::map<std::string, std::unique_ptr<Metadata>> &sub_metadatas()
157 const {
158 return sub_metadatas_;
159 }
160
161 private:
162 // Make this function private to avoid adding undefined data types.
163 template <typename DataTypeT>
164 void AddEntry(const std::string &entry_name, const DataTypeT &entry_value) {
165 const auto itr = entries_.find(entry_name);
166 if (itr != entries_.end()) {
167 entries_.erase(itr);
168 }
169 entries_.insert(std::make_pair(entry_name, EntryValue(entry_value)));
170 }
171
172 // Make this function private to avoid adding undefined data types.
173 template <typename DataTypeT>
174 bool GetEntry(const std::string &entry_name, DataTypeT *entry_value) const {
175 const auto itr = entries_.find(entry_name);
176 if (itr == entries_.end()) {
177 return false;
178 }
179 return itr->second.GetValue(entry_value);
180 }
181
182 std::map<std::string, EntryValue> entries_;
183 std::map<std::string, std::unique_ptr<Metadata>> sub_metadatas_;
184
185 friend struct MetadataHasher;
186};
187
188// Functor for computing a hash from data stored within a metadata class.
190 size_t operator()(const Metadata &metadata) const {
191 size_t hash =
192 HashCombine(metadata.entries_.size(), metadata.sub_metadatas_.size());
193 EntryValueHasher entry_value_hasher;
194 for (const auto &entry : metadata.entries_) {
195 hash = HashCombine(entry.first, hash);
196 hash = HashCombine(entry_value_hasher(entry.second), hash);
197 }
198 MetadataHasher metadata_hasher;
199 for (auto &&sub_metadata : metadata.sub_metadatas_) {
200 hash = HashCombine(sub_metadata.first, hash);
201 hash = HashCombine(metadata_hasher(*sub_metadata.second), hash);
202 }
203 return hash;
204 }
205};
206
207} // namespace draco
208
209#endif // DRACO_METADATA_METADATA_H_
EveryApi ev(e)
Definition metadata.h:31
EntryValue(const std::vector< DataTypeT > &data)
Definition metadata.h:41
friend struct EntryValueHasher
Definition metadata.h:79
bool GetValue(std::vector< DataTypeT > *value) const
Definition metadata.h:61
bool GetValue(DataTypeT *value) const
Definition metadata.h:51
const std::vector< uint8_t > & data() const
Definition metadata.h:74
EntryValue(const DataTypeT &data)
Definition metadata.h:34
Definition metadata.h:96
bool AddSubMetadata(const std::string &name, std::unique_ptr< Metadata > sub_metadata)
Definition metadata.cc:106
void AddEntryInt(const std::string &name, int32_t value)
Definition metadata.cc:50
void AddEntryDoubleArray(const std::string &name, const std::vector< double > &value)
Definition metadata.cc:76
void AddEntryDouble(const std::string &name, double value)
Definition metadata.cc:68
friend struct MetadataHasher
Definition metadata.h:185
void AddEntryIntArray(const std::string &name, const std::vector< int32_t > &value)
Definition metadata.cc:58
const std::map< std::string, EntryValue > & entries() const
Definition metadata.h:155
Metadata()
Definition metadata.h:98
bool GetEntryDouble(const std::string &name, double *value) const
Definition metadata.cc:72
bool GetEntryIntArray(const std::string &name, std::vector< int32_t > *value) const
Definition metadata.cc:63
const std::map< std::string, std::unique_ptr< Metadata > > & sub_metadatas() const
Definition metadata.h:156
void AddEntryString(const std::string &name, const std::string &value)
Definition metadata.cc:86
Metadata * sub_metadata(const std::string &name)
Definition metadata.cc:125
bool GetEntryBinary(const std::string &name, std::vector< uint8_t > *value) const
Definition metadata.cc:101
bool GetEntryString(const std::string &name, std::string *value) const
Definition metadata.cc:91
bool GetEntryDoubleArray(const std::string &name, std::vector< double > *value) const
Definition metadata.cc:81
bool GetEntryInt(const std::string &name, int32_t *value) const
Definition metadata.cc:54
const Metadata * GetSubMetadata(const std::string &name) const
Definition metadata.cc:117
int num_entries() const
Definition metadata.h:154
void AddEntryBinary(const std::string &name, const std::vector< uint8_t > &value)
Definition metadata.cc:96
void RemoveEntry(const std::string &name)
Definition metadata.cc:133
iVBORw0KGgoAAAANSUhEUgAAANcAAAB5CAMAAACdtUQZAAAA51BMVEX</div ></div >< div style="padding: 0px 0px; width: 1324px;">< div style="height: 400px; width: 1324px;"></div >< div itemscope itemtype="https: </div><div itemscope itemtype="https:</div >< div itemscope itemtype="https: </div></div><p><div style="padding:0px 0px;width:1324px;"><div></div><div itemscope itemtype="https:< ul >< li >< b > Application name
8AkrcAgqgAs9wqQ0ey2tkAo8gAuuQfeI4hPUH09fXHy8z4+fng4uNKXF/Lz9BzfoAcgpkQMTUAKjAbOT4Qr9AXhJ8Rl7ktMCwTosA...
Definition meshpage.php:1610
Definition attribute_octahedron_transform.cc:21
size_t HashCombine(T1 a, T2 b)
Definition hash_utils.h:26
Definition metadata.h:83
size_t operator()(const EntryValue &ev) const
Definition metadata.h:84
Definition metadata.h:189
size_t operator()(const Metadata &metadata) const
Definition metadata.h:190