GCC Code Coverage Report


Directory: ./
File: openvdb/openvdb/Metadata.h
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 74 78 94.9%
Functions: 136 221 61.5%
Branches: 231 453 51.0%

Line Branch Exec Source
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #ifndef OPENVDB_METADATA_HAS_BEEN_INCLUDED
5 #define OPENVDB_METADATA_HAS_BEEN_INCLUDED
6
7 #include "version.h"
8 #include "Exceptions.h"
9 #include "Types.h"
10 #include "math/Math.h" // for math::isZero()
11 #include "util/Name.h"
12 #include <cstdint>
13 #include <iostream>
14 #include <string>
15 #include <vector>
16
17
18 namespace openvdb {
19 OPENVDB_USE_VERSION_NAMESPACE
20 namespace OPENVDB_VERSION_NAME {
21
22 /// @brief Base class for storing metadata information in a grid.
23 class OPENVDB_API Metadata
24 {
25 public:
26 using Ptr = SharedPtr<Metadata>;
27 using ConstPtr = SharedPtr<const Metadata>;
28
29
10/20
✓ Branch 1 taken 151 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 151 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
45234 Metadata() {}
30 virtual ~Metadata() {}
31
32 // Disallow copying of instances of this class.
33 Metadata(const Metadata&) = delete;
34 Metadata& operator=(const Metadata&) = delete;
35
36 /// Return the type name of the metadata.
37 virtual Name typeName() const = 0;
38
39 /// Return a copy of the metadata.
40 virtual Metadata::Ptr copy() const = 0;
41
42 /// Copy the given metadata into this metadata.
43 virtual void copy(const Metadata& other) = 0;
44
45 /// Return a textual representation of this metadata.
46 virtual std::string str() const = 0;
47
48 /// Return the boolean representation of this metadata (empty strings
49 /// and zeroVals evaluate to false; most other values evaluate to true).
50 virtual bool asBool() const = 0;
51
52 /// Return @c true if the given metadata is equivalent to this metadata.
53 bool operator==(const Metadata& other) const;
54 /// Return @c true if the given metadata is different from this metadata.
55
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
221 bool operator!=(const Metadata& other) const { return !(*this == other); }
56
57 /// Return the size of this metadata in bytes.
58 virtual Index32 size() const = 0;
59
60 /// Unserialize this metadata from a stream.
61 void read(std::istream&);
62 /// Serialize this metadata to a stream.
63 void write(std::ostream&) const;
64
65 /// Create new metadata of the given type.
66 static Metadata::Ptr createMetadata(const Name& typeName);
67
68 /// Return @c true if the given type is known by the metadata type registry.
69 static bool isRegisteredType(const Name& typeName);
70
71 /// Clear out the metadata registry.
72 static void clearRegistry();
73
74 /// Register the given metadata type along with a factory function.
75 static void registerType(const Name& typeName, Metadata::Ptr (*createMetadata)());
76 static void unregisterType(const Name& typeName);
77
78 protected:
79 /// Read the size of the metadata from a stream.
80 static Index32 readSize(std::istream&);
81 /// Write the size of the metadata to a stream.
82 void writeSize(std::ostream&) const;
83
84 /// Read the metadata from a stream.
85 virtual void readValue(std::istream&, Index32 numBytes) = 0;
86 /// Write the metadata to a stream.
87 virtual void writeValue(std::ostream&) const = 0;
88 };
89
90
91 /// @brief Subclass to hold raw data of an unregistered type
92 class OPENVDB_API UnknownMetadata: public Metadata
93 {
94 public:
95 using ByteVec = std::vector<uint8_t>;
96
97
1/2
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
17 explicit UnknownMetadata(const Name& typ = "<unknown>"): mTypeName(typ) {}
98
99 5 Name typeName() const override { return mTypeName; }
100 Metadata::Ptr copy() const override;
101 void copy(const Metadata&) override;
102 std::string str() const override { return (mBytes.empty() ? "" : "<binary data>"); }
103 bool asBool() const override { return !mBytes.empty(); }
104 6 Index32 size() const override { return static_cast<Index32>(mBytes.size()); }
105
106
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 void setValue(const ByteVec& bytes) { mBytes = bytes; }
107 const ByteVec& value() const { return mBytes; }
108
109 protected:
110 void readValue(std::istream&, Index32 numBytes) override;
111 void writeValue(std::ostream&) const override;
112
113 private:
114 Name mTypeName;
115 ByteVec mBytes;
116 };
117
118
119 /// @brief Templated metadata class to hold specific types.
120 template<typename T>
121 class TypedMetadata: public Metadata
122 {
123 public:
124 using Ptr = SharedPtr<TypedMetadata<T>>;
125 using ConstPtr = SharedPtr<const TypedMetadata<T>>;
126
127 TypedMetadata();
128 TypedMetadata(const T& value);
129 TypedMetadata(const TypedMetadata<T>& other);
130 ~TypedMetadata() override;
131
132 Name typeName() const override;
133 Metadata::Ptr copy() const override;
134 void copy(const Metadata& other) override;
135 std::string str() const override;
136 bool asBool() const override;
137 3491 Index32 size() const override { return static_cast<Index32>(sizeof(T)); }
138
139 /// Set this metadata's value.
140 void setValue(const T&);
141 /// Return this metadata's value.
142 T& value();
143 const T& value() const;
144
145 // Static specialized function for the type name. This function must be
146 // template specialized for each type T.
147
24/98
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1500076 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✓ Branch 20 taken 264 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 times.
✓ Branch 26 taken 1 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 2 times.
✓ Branch 31 taken 17 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 16 times.
✓ Branch 35 taken 1 times.
✓ Branch 37 taken 16 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 16 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 42 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 42 times.
✗ Branch 47 not taken.
✓ Branch 49 taken 29 times.
✗ Branch 50 not taken.
✓ Branch 52 taken 29 times.
✗ Branch 53 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 58 not taken.
✗ Branch 59 not taken.
✗ Branch 61 not taken.
✗ Branch 62 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
✗ Branch 70 not taken.
✗ Branch 71 not taken.
✗ Branch 73 not taken.
✗ Branch 74 not taken.
✗ Branch 76 not taken.
✗ Branch 77 not taken.
✗ Branch 79 not taken.
✗ Branch 80 not taken.
✗ Branch 82 not taken.
✗ Branch 83 not taken.
✗ Branch 85 not taken.
✗ Branch 86 not taken.
✗ Branch 88 not taken.
✗ Branch 89 not taken.
✗ Branch 91 not taken.
✗ Branch 92 not taken.
✗ Branch 94 not taken.
✗ Branch 95 not taken.
✗ Branch 97 not taken.
✗ Branch 98 not taken.
✗ Branch 100 not taken.
✗ Branch 101 not taken.
✗ Branch 103 not taken.
✗ Branch 104 not taken.
✗ Branch 106 not taken.
✗ Branch 107 not taken.
✗ Branch 109 not taken.
✗ Branch 110 not taken.
✗ Branch 112 not taken.
✗ Branch 113 not taken.
✗ Branch 115 not taken.
✗ Branch 116 not taken.
✗ Branch 118 not taken.
✗ Branch 119 not taken.
✗ Branch 121 not taken.
✗ Branch 122 not taken.
✗ Branch 124 not taken.
✗ Branch 125 not taken.
✗ Branch 127 not taken.
✗ Branch 128 not taken.
✗ Branch 130 not taken.
✗ Branch 131 not taken.
✗ Branch 133 not taken.
✗ Branch 134 not taken.
✗ Branch 136 not taken.
✗ Branch 137 not taken.
1500600 static Name staticTypeName() { return typeNameAsString<T>(); }
148
149 /// Create new metadata of this type.
150 static Metadata::Ptr createMetadata();
151
152 static void registerType();
153 static void unregisterType();
154 static bool isRegisteredType();
155
156 protected:
157 void readValue(std::istream&, Index32 numBytes) override;
158 void writeValue(std::ostream&) const override;
159
160 private:
161 T mValue;
162 };
163
164 /// Write a Metadata to an output stream
165 std::ostream& operator<<(std::ostream& ostr, const Metadata& metadata);
166
167
168 ////////////////////////////////////////
169
170
171 inline void
172 1123 Metadata::writeSize(std::ostream& os) const
173 {
174 1123 const Index32 n = this->size();
175 1123 os.write(reinterpret_cast<const char*>(&n), sizeof(Index32));
176 1123 }
177
178
179 inline Index32
180 Metadata::readSize(std::istream& is)
181 {
182 864 Index32 n = 0;
183
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
864 is.read(reinterpret_cast<char*>(&n), sizeof(Index32));
184
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
864 return n;
185 }
186
187
188 inline void
189 863 Metadata::read(std::istream& is)
190 {
191 const Index32 numBytes = this->readSize(is);
192 863 this->readValue(is, numBytes);
193 863 }
194
195
196 inline void
197 Metadata::write(std::ostream& os) const
198 {
199
24/48
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 1 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 1 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 1 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 1 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 1 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 1 times.
✗ Branch 47 not taken.
✓ Branch 49 taken 1 times.
✗ Branch 50 not taken.
✓ Branch 52 taken 1 times.
✗ Branch 53 not taken.
✓ Branch 55 taken 1 times.
✗ Branch 56 not taken.
✓ Branch 58 taken 1 times.
✗ Branch 59 not taken.
✓ Branch 61 taken 1 times.
✗ Branch 62 not taken.
✓ Branch 64 taken 1 times.
✗ Branch 65 not taken.
✓ Branch 67 taken 1 times.
✗ Branch 68 not taken.
✓ Branch 70 taken 1 times.
✗ Branch 71 not taken.
1122 this->writeSize(os);
200
24/48
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 1 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 1 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 1 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 1 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 1 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 1 times.
✗ Branch 47 not taken.
✓ Branch 49 taken 1 times.
✗ Branch 50 not taken.
✓ Branch 52 taken 1 times.
✗ Branch 53 not taken.
✓ Branch 55 taken 1 times.
✗ Branch 56 not taken.
✓ Branch 58 taken 1 times.
✗ Branch 59 not taken.
✓ Branch 61 taken 1 times.
✗ Branch 62 not taken.
✓ Branch 64 taken 1 times.
✗ Branch 65 not taken.
✓ Branch 67 taken 1 times.
✗ Branch 68 not taken.
✓ Branch 70 taken 1 times.
✗ Branch 71 not taken.
1123 this->writeValue(os);
201 31 }
202
203
204 ////////////////////////////////////////
205
206
207 template <typename T>
208 inline
209
38/57
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1505 times.
✓ Branch 3 taken 752 times.
✓ Branch 4 taken 421 times.
✓ Branch 5 taken 216 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 5 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 11 times.
✓ Branch 14 taken 5 times.
✓ Branch 15 taken 5 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 2 times.
✓ Branch 18 taken 2 times.
✓ Branch 19 taken 2 times.
✓ Branch 20 taken 2 times.
✓ Branch 21 taken 2 times.
✓ Branch 22 taken 2 times.
✓ Branch 23 taken 2 times.
✓ Branch 24 taken 1 times.
✓ Branch 25 taken 2 times.
✓ Branch 26 taken 1 times.
✓ Branch 27 taken 2 times.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 1 times.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✓ Branch 33 taken 1 times.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✓ Branch 36 taken 1 times.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✓ Branch 39 taken 1 times.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✓ Branch 42 taken 1 times.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✓ Branch 45 taken 1 times.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✓ Branch 48 taken 1 times.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✓ Branch 51 taken 1 times.
✗ Branch 52 not taken.
✓ Branch 54 taken 1 times.
✗ Branch 55 not taken.
✓ Branch 57 taken 1 times.
✗ Branch 58 not taken.
26252 TypedMetadata<T>::TypedMetadata() : mValue(T())
210 {
211 }
212
213 template <typename T>
214 inline
215
57/70
✓ Branch 1 taken 10431 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 154 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 149 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 146 times.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 1 times.
✓ Branch 13 taken 7 times.
✓ Branch 14 taken 1 times.
✓ Branch 15 taken 1 times.
✓ Branch 16 taken 280 times.
✓ Branch 17 taken 1 times.
✓ Branch 18 taken 5 times.
✓ Branch 19 taken 5 times.
✓ Branch 20 taken 1 times.
✓ Branch 21 taken 40 times.
✓ Branch 22 taken 3 times.
✓ Branch 23 taken 1 times.
✓ Branch 24 taken 545 times.
✓ Branch 25 taken 2 times.
✓ Branch 26 taken 1 times.
✓ Branch 27 taken 1 times.
✓ Branch 28 taken 2 times.
✓ Branch 29 taken 1 times.
✓ Branch 30 taken 1 times.
✓ Branch 31 taken 9 times.
✓ Branch 32 taken 1 times.
✓ Branch 33 taken 1 times.
✓ Branch 34 taken 2 times.
✓ Branch 35 taken 1 times.
✓ Branch 36 taken 1 times.
✓ Branch 37 taken 2 times.
✓ Branch 38 taken 1 times.
✓ Branch 39 taken 1 times.
✓ Branch 40 taken 2 times.
✓ Branch 41 taken 1 times.
✓ Branch 42 taken 1 times.
✓ Branch 43 taken 2 times.
✓ Branch 44 taken 1 times.
✓ Branch 45 taken 1 times.
✓ Branch 46 taken 2 times.
✓ Branch 47 taken 1 times.
✗ Branch 48 not taken.
✓ Branch 49 taken 2 times.
✓ Branch 50 taken 1 times.
✗ Branch 51 not taken.
✓ Branch 52 taken 2 times.
✓ Branch 53 taken 1 times.
✗ Branch 54 not taken.
✓ Branch 55 taken 2 times.
✗ Branch 56 not taken.
✓ Branch 58 taken 4 times.
✗ Branch 59 not taken.
✓ Branch 61 taken 2 times.
✗ Branch 62 not taken.
✓ Branch 64 taken 2 times.
✗ Branch 65 not taken.
✓ Branch 67 taken 1 times.
✗ Branch 68 not taken.
✓ Branch 70 taken 1 times.
✗ Branch 71 not taken.
✓ Branch 73 taken 1 times.
✗ Branch 74 not taken.
✓ Branch 76 taken 1 times.
✗ Branch 77 not taken.
20191 TypedMetadata<T>::TypedMetadata(const T &value) : mValue(value)
216 {
217 10894 }
218
219 template <typename T>
220 inline
221 TypedMetadata<T>::TypedMetadata(const TypedMetadata<T> &other) :
222 Metadata(),
223 mValue(other.mValue)
224 {
225 }
226
227 template <typename T>
228 inline
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
48546 TypedMetadata<T>::~TypedMetadata()
230 {
231 }
232
233 template <typename T>
234 inline Name
235 3004182 TypedMetadata<T>::typeName() const
236 {
237 3004182 return TypedMetadata<T>::staticTypeName();
238 }
239
240 template <typename T>
241 inline void
242 TypedMetadata<T>::setValue(const T& val)
243 {
244
11/20
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
12 mValue = val;
245 1 }
246
247 template <typename T>
248 inline T&
249 TypedMetadata<T>::value()
250 {
251
22/36
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 24 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 1 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 1 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 1 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 1 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 1 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 1 times.
✗ Branch 47 not taken.
212 return mValue;
252 }
253
254 template <typename T>
255 inline const T&
256 TypedMetadata<T>::value() const
257 {
258
2/4
✓ Branch 1 taken 1500066 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 257 times.
✗ Branch 5 not taken.
1527091 return mValue;
259 }
260
261 template <typename T>
262 inline Metadata::Ptr
263 46990 TypedMetadata<T>::copy() const
264 {
265 46990 Metadata::Ptr metadata(new TypedMetadata<T>());
266
1/2
✓ Branch 1 taken 23520 times.
✗ Branch 2 not taken.
46990 metadata->copy(*this);
267 46990 return metadata;
268 }
269
270 template <typename T>
271 inline void
272 47012 TypedMetadata<T>::copy(const Metadata &other)
273 {
274 47012 const TypedMetadata<T>* t = dynamic_cast<const TypedMetadata<T>*>(&other);
275
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 23533 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
47012 if (t == nullptr) OPENVDB_THROW(TypeError, "Incompatible type during copy");
276 47012 mValue = t->mValue;
277 47012 }
278
279
280 template<typename T>
281 inline void
282 945 TypedMetadata<T>::readValue(std::istream& is, Index32 /*numBytes*/)
283 {
284 //assert(this->size() == numBytes);
285 945 is.read(reinterpret_cast<char*>(&mValue), this->size());
286 945 }
287
288 template<typename T>
289 inline void
290 1273 TypedMetadata<T>::writeValue(std::ostream& os) const
291 {
292 1273 os.write(reinterpret_cast<const char*>(&mValue), this->size());
293 1273 }
294
295 template <typename T>
296 inline std::string
297 16 TypedMetadata<T>::str() const
298 {
299 32 std::ostringstream ostr;
300
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 ostr << mValue;
301 16 return ostr.str();
302 }
303
304 template<typename T>
305 inline bool
306
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
338 TypedMetadata<T>::asBool() const
307 {
308
6/12
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
343 return !math::isZero(mValue);
309 }
310
311 template <typename T>
312 inline Metadata::Ptr
313 1478 TypedMetadata<T>::createMetadata()
314 {
315 1478 Metadata::Ptr ret(new TypedMetadata<T>());
316 1478 return ret;
317 }
318
319 template <typename T>
320 inline void
321 11038 TypedMetadata<T>::registerType()
322 {
323
1/2
✓ Branch 1 taken 5519 times.
✗ Branch 2 not taken.
11038 Metadata::registerType(TypedMetadata<T>::staticTypeName(),
324 TypedMetadata<T>::createMetadata);
325 11038 }
326
327 template <typename T>
328 inline void
329 1 TypedMetadata<T>::unregisterType()
330 {
331
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 Metadata::unregisterType(TypedMetadata<T>::staticTypeName());
332 1 }
333
334 template <typename T>
335 inline bool
336 39 TypedMetadata<T>::isRegisteredType()
337 {
338 78 return Metadata::isRegisteredType(TypedMetadata<T>::staticTypeName());
339 }
340
341
342 template<>
343 inline std::string
344 TypedMetadata<bool>::str() const
345 {
346 return (mValue ? "true" : "false");
347 }
348
349
350 inline std::ostream&
351 operator<<(std::ostream& ostr, const Metadata& metadata)
352 {
353 ostr << metadata.str();
354 return ostr;
355 }
356
357
358 using BoolMetadata = TypedMetadata<bool>;
359 using DoubleMetadata = TypedMetadata<double>;
360 using FloatMetadata = TypedMetadata<float>;
361 using Int32Metadata = TypedMetadata<int32_t>;
362 using Int64Metadata = TypedMetadata<int64_t>;
363 using StringMetadata = TypedMetadata<std::string>;
364 using Vec2DMetadata = TypedMetadata<Vec2d>;
365 using Vec2IMetadata = TypedMetadata<Vec2i>;
366 using Vec2SMetadata = TypedMetadata<Vec2s>;
367 using Vec3DMetadata = TypedMetadata<Vec3d>;
368 using Vec3IMetadata = TypedMetadata<Vec3i>;
369 using Vec3SMetadata = TypedMetadata<Vec3s>;
370 using Vec4DMetadata = TypedMetadata<Vec4d>;
371 using Vec4IMetadata = TypedMetadata<Vec4i>;
372 using Vec4SMetadata = TypedMetadata<Vec4s>;
373 using Mat4SMetadata = TypedMetadata<Mat4s>;
374 using Mat4DMetadata = TypedMetadata<Mat4d>;
375
376
377 ////////////////////////////////////////
378
379
380 template<>
381 inline Index32
382 1474 StringMetadata::size() const
383 {
384 1474 return static_cast<Index32>(mValue.size());
385 }
386
387
388 template<>
389 inline std::string
390 51064 StringMetadata::str() const
391 {
392 51064 return mValue;
393 }
394
395
396 template<>
397 inline void
398 259 StringMetadata::readValue(std::istream& is, Index32 size)
399 {
400 259 mValue.resize(size, '\0');
401 259 is.read(&mValue[0], size);
402 259 }
403
404 template<>
405 inline void
406 737 StringMetadata::writeValue(std::ostream& os) const
407 {
408 737 os.write(reinterpret_cast<const char*>(&mValue[0]), this->size());
409 737 }
410
411 } // namespace OPENVDB_VERSION_NAME
412 } // namespace openvdb
413
414 #endif // OPENVDB_METADATA_HAS_BEEN_INCLUDED
415