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 #include "draco/metadata/metadata.h"
16 #include <utility>
17 
18 namespace draco {
19 
EntryValue(const EntryValue & value)20 EntryValue::EntryValue(const EntryValue &value) {
21   data_.resize(value.data_.size());
22   memcpy(&data_[0], &value.data_[0], value.data_.size());
23 }
24 
EntryValue(const std::string & value)25 EntryValue::EntryValue(const std::string &value) {
26   data_.resize(value.size());
27   memcpy(&data_[0], &value[0], value.size());
28 }
29 
30 template <>
GetValue(std::string * value) const31 bool EntryValue::GetValue(std::string *value) const {
32   if (data_.empty())
33     return false;
34   value->resize(data_.size());
35   memcpy(&value->at(0), &data_[0], data_.size());
36   return true;
37 }
38 
Metadata(const Metadata & metadata)39 Metadata::Metadata(const Metadata &metadata) {
40   entries_.insert(metadata.entries_.begin(), metadata.entries_.end());
41   for (const auto &sub_metadata_entry : metadata.sub_metadatas_) {
42     std::unique_ptr<Metadata> sub_metadata =
43         std::unique_ptr<Metadata>(new Metadata(*sub_metadata_entry.second));
44     sub_metadatas_[sub_metadata_entry.first] = std::move(sub_metadata);
45   }
46 }
47 
AddEntryInt(const std::string & name,int32_t value)48 void Metadata::AddEntryInt(const std::string &name, int32_t value) {
49   AddEntry(name, value);
50 }
51 
GetEntryInt(const std::string & name,int32_t * value) const52 bool Metadata::GetEntryInt(const std::string &name, int32_t *value) const {
53   return GetEntry(name, value);
54 }
55 
AddEntryIntArray(const std::string & name,const std::vector<int32_t> & value)56 void Metadata::AddEntryIntArray(const std::string &name,
57                                 const std::vector<int32_t> &value) {
58   AddEntry(name, value);
59 }
60 
GetEntryIntArray(const std::string & name,std::vector<int32_t> * value) const61 bool Metadata::GetEntryIntArray(const std::string &name,
62                                 std::vector<int32_t> *value) const {
63   return GetEntry(name, value);
64 }
65 
AddEntryDouble(const std::string & name,double value)66 void Metadata::AddEntryDouble(const std::string &name, double value) {
67   AddEntry(name, value);
68 }
69 
GetEntryDouble(const std::string & name,double * value) const70 bool Metadata::GetEntryDouble(const std::string &name, double *value) const {
71   return GetEntry(name, value);
72 }
73 
AddEntryDoubleArray(const std::string & name,const std::vector<double> & value)74 void Metadata::AddEntryDoubleArray(const std::string &name,
75                                    const std::vector<double> &value) {
76   AddEntry(name, value);
77 }
78 
GetEntryDoubleArray(const std::string & name,std::vector<double> * value) const79 bool Metadata::GetEntryDoubleArray(const std::string &name,
80                                    std::vector<double> *value) const {
81   return GetEntry(name, value);
82 }
83 
AddEntryString(const std::string & name,const std::string & value)84 void Metadata::AddEntryString(const std::string &name,
85                               const std::string &value) {
86   AddEntry(name, value);
87 }
88 
GetEntryString(const std::string & name,std::string * value) const89 bool Metadata::GetEntryString(const std::string &name,
90                               std::string *value) const {
91   return GetEntry(name, value);
92 }
93 
AddEntryBinary(const std::string & name,const std::vector<uint8_t> & value)94 void Metadata::AddEntryBinary(const std::string &name,
95                               const std::vector<uint8_t> &value) {
96   AddEntry(name, value);
97 }
98 
GetEntryBinary(const std::string & name,std::vector<uint8_t> * value) const99 bool Metadata::GetEntryBinary(const std::string &name,
100                               std::vector<uint8_t> *value) const {
101   return GetEntry(name, value);
102 }
103 
AddSubMetadata(const std::string & name,std::unique_ptr<Metadata> sub_metadata)104 bool Metadata::AddSubMetadata(const std::string &name,
105                               std::unique_ptr<Metadata> sub_metadata) {
106   auto sub_ptr = sub_metadatas_.find(name);
107   // Avoid accidentally writing over a sub-metadata with the same name.
108   if (sub_ptr != sub_metadatas_.end()) {
109     return false;
110   }
111   sub_metadatas_[name] = std::move(sub_metadata);
112   return true;
113 }
114 
GetSubMetadata(const std::string & name) const115 const Metadata *Metadata::GetSubMetadata(const std::string &name) const {
116   auto sub_ptr = sub_metadatas_.find(name);
117   if (sub_ptr == sub_metadatas_.end()) {
118     return nullptr;
119   }
120   return sub_ptr->second.get();
121 }
122 
RemoveEntry(const std::string & name)123 void Metadata::RemoveEntry(const std::string &name) {
124   // Actually just remove "name", no need to check if it exists.
125   auto entry_ptr = entries_.find(name);
126   if (entry_ptr != entries_.end()) {
127     entries_.erase(entry_ptr);
128   }
129 }
130 }  // namespace draco
131