GCC Code Coverage Report


Directory: ./
File: openvdb/openvdb/Metadata.cc
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 55 57 96.5%
Functions: 12 12 100.0%
Branches: 31 64 48.4%

Line Branch Exec Source
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #include "Metadata.h"
5
6 #include <algorithm> // for std::min()
7 #include <map>
8 #include <mutex>
9 #include <sstream>
10 #include <vector>
11
12
13 namespace openvdb {
14 OPENVDB_USE_VERSION_NAMESPACE
15 namespace OPENVDB_VERSION_NAME {
16
17 using createMetadata = Metadata::Ptr (*)();
18 using MetadataFactoryMap = std::map<Name, createMetadata>;
19 using MetadataFactoryMapCIter = MetadataFactoryMap::const_iterator;
20
21 struct LockedMetadataTypeRegistry {
22 LockedMetadataTypeRegistry() {}
23 2 ~LockedMetadataTypeRegistry() {}
24 std::mutex mMutex;
25 MetadataFactoryMap mMap;
26 };
27
28 // Global function for accessing the regsitry
29 static LockedMetadataTypeRegistry*
30 33426 getMetadataTypeRegistry()
31 {
32
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 33424 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
33426 static LockedMetadataTypeRegistry registry;
33 33426 return &registry;
34 }
35
36 bool
37 24773 Metadata::isRegisteredType(const Name &typeName)
38 {
39 24773 LockedMetadataTypeRegistry *registry = getMetadataTypeRegistry();
40 24773 std::lock_guard<std::mutex> lock(registry->mMutex);
41
42
1/2
✓ Branch 0 taken 24773 times.
✗ Branch 1 not taken.
24773 return (registry->mMap.find(typeName) != registry->mMap.end());
43 }
44
45 void
46 7138 Metadata::registerType(const Name &typeName, Metadata::Ptr (*createMetadata)())
47 {
48 7138 LockedMetadataTypeRegistry *registry = getMetadataTypeRegistry();
49 7138 std::lock_guard<std::mutex> lock(registry->mMutex);
50
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7138 times.
7138 if (registry->mMap.find(typeName) != registry->mMap.end()) {
52 OPENVDB_THROW(KeyError,
53 "Cannot register " << typeName << ". Type is already registered");
54 }
55
56
2/4
✓ Branch 1 taken 7138 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7138 times.
✗ Branch 4 not taken.
7138 registry->mMap[typeName] = createMetadata;
57 7138 }
58
59 void
60 3 Metadata::unregisterType(const Name &typeName)
61 {
62 3 LockedMetadataTypeRegistry *registry = getMetadataTypeRegistry();
63 3 std::lock_guard<std::mutex> lock(registry->mMutex);
64
65 registry->mMap.erase(typeName);
66 3 }
67
68 Metadata::Ptr
69 826 Metadata::createMetadata(const Name &typeName)
70 {
71 826 LockedMetadataTypeRegistry *registry = getMetadataTypeRegistry();
72 826 std::lock_guard<std::mutex> lock(registry->mMutex);
73
74
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 825 times.
826 MetadataFactoryMapCIter iter = registry->mMap.find(typeName);
75
76
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 825 times.
826 if (iter == registry->mMap.end()) {
77
1/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
4 OPENVDB_THROW(LookupError,
78 "Cannot create metadata for unregistered type " << typeName);
79 }
80
81
1/2
✓ Branch 1 taken 825 times.
✗ Branch 2 not taken.
1650 return (iter->second)();
82 }
83
84 void
85 686 Metadata::clearRegistry()
86 {
87 686 LockedMetadataTypeRegistry *registry = getMetadataTypeRegistry();
88 686 std::lock_guard<std::mutex> lock(registry->mMutex);
89
90 registry->mMap.clear();
91 686 }
92
93
94 ////////////////////////////////////////
95
96
97 bool
98 222 Metadata::operator==(const Metadata& other) const
99 {
100
1/2
✓ Branch 2 taken 222 times.
✗ Branch 3 not taken.
222 if (other.size() != this->size()) return false;
101
2/4
✓ Branch 2 taken 222 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 222 times.
✗ Branch 6 not taken.
444 if (other.typeName() != this->typeName()) return false;
102
103 std::ostringstream
104 444 bytes(std::ios_base::binary),
105
1/2
✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
444 otherBytes(std::ios_base::binary);
106 try {
107
1/2
✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
222 this->writeValue(bytes);
108
1/2
✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
222 other.writeValue(otherBytes);
109
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
222 return (bytes.str() == otherBytes.str());
110 } catch (Exception&) {}
111 return false;
112 }
113
114
115 ////////////////////////////////////////
116
117
118 Metadata::Ptr
119 6 UnknownMetadata::copy() const
120 {
121
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 Metadata::Ptr metadata{new UnknownMetadata{mTypeName}};
122
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 static_cast<UnknownMetadata*>(metadata.get())->setValue(mBytes);
123 6 return metadata;
124 }
125
126
127 void
128 1 UnknownMetadata::copy(const Metadata& other)
129 {
130 2 std::ostringstream ostr(std::ios_base::binary);
131 other.write(ostr);
132
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
3 std::istringstream istr(ostr.str(), std::ios_base::binary);
133 const auto numBytes = readSize(istr);
134
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 readValue(istr, numBytes);
135 1 }
136
137
138 void
139
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
12 UnknownMetadata::readValue(std::istream& is, Index32 numBytes)
140 {
141 mBytes.clear();
142
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
12 if (numBytes > 0) {
143 10 ByteVec buffer(numBytes);
144
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 is.read(reinterpret_cast<char*>(&buffer[0]), numBytes);
145 mBytes.swap(buffer);
146 }
147 12 }
148
149
150 void
151
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 UnknownMetadata::writeValue(std::ostream& os) const
152 {
153
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!mBytes.empty()) {
154 6 os.write(reinterpret_cast<const char*>(&mBytes[0]), mBytes.size());
155 }
156 6 }
157
158 } // namespace OPENVDB_VERSION_NAME
159 } // namespace openvdb
160