Coverage Report

Created: 2025-08-29 06:25

/src/openexr/src/lib/OpenEXR/ImfAttribute.cpp
Line
Count
Source (jump to first uncovered line)
1
//
2
// SPDX-License-Identifier: BSD-3-Clause
3
// Copyright (c) Contributors to the OpenEXR Project.
4
//
5
6
//-----------------------------------------------------------------------------
7
//
8
//  class Attribute
9
//
10
//-----------------------------------------------------------------------------
11
12
#include <Iex.h>
13
#include <ImfAttribute.h>
14
#include <map>
15
#include <string.h>
16
17
#include <IlmThreadConfig.h>
18
19
#if ILMTHREAD_THREADING_ENABLED
20
#    include <mutex>
21
#endif
22
23
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
24
25
Attribute::Attribute ()
26
23.0M
{}
27
28
Attribute::~Attribute ()
29
23.0M
{}
30
31
namespace
32
{
33
34
struct NameCompare
35
{
36
    bool operator() (const char* x, const char* y) const
37
1.24M
    {
38
1.24M
        return strcmp (x, y) < 0;
39
1.24M
    }
40
};
41
42
typedef Attribute* (*Constructor) ();
43
typedef std::map<const char*, Constructor, NameCompare> TypeMap;
44
45
class LockedTypeMap : public TypeMap
46
{
47
public:
48
#if ILMTHREAD_THREADING_ENABLED
49
    std::mutex mutex;
50
#endif
51
};
52
53
LockedTypeMap&
54
typeMap ()
55
196k
{
56
196k
    static LockedTypeMap tMap;
57
196k
    return tMap;
58
196k
}
59
60
} // namespace
61
62
bool
63
Attribute::knownType (const char typeName[])
64
193k
{
65
193k
    LockedTypeMap& tMap = typeMap ();
66
193k
#if ILMTHREAD_THREADING_ENABLED
67
193k
    std::lock_guard<std::mutex> lock (tMap.mutex);
68
193k
#endif
69
193k
    return tMap.find (typeName) != tMap.end ();
70
193k
}
71
72
void
73
Attribute::registerAttributeType (
74
    const char typeName[], Attribute* (*newAttribute) ())
75
31
{
76
31
    LockedTypeMap& tMap = typeMap ();
77
31
#if ILMTHREAD_THREADING_ENABLED
78
31
    std::lock_guard<std::mutex> lock (tMap.mutex);
79
31
#endif
80
31
    if (tMap.find (typeName) != tMap.end ())
81
0
        THROW (
82
31
            IEX_NAMESPACE::ArgExc,
83
31
            "Cannot register image file attribute "
84
31
            "type \""
85
31
                << typeName
86
31
                << "\". "
87
31
                   "The type has already been registered.");
88
89
31
    tMap.insert (TypeMap::value_type (typeName, newAttribute));
90
31
}
91
92
void
93
Attribute::unRegisterAttributeType (const char typeName[])
94
0
{
95
0
    LockedTypeMap& tMap = typeMap ();
96
0
#if ILMTHREAD_THREADING_ENABLED
97
0
    std::lock_guard<std::mutex> lock (tMap.mutex);
98
0
#endif
99
0
    tMap.erase (typeName);
100
0
}
101
102
Attribute*
103
Attribute::newAttribute (const char typeName[])
104
2.35k
{
105
2.35k
    LockedTypeMap& tMap = typeMap ();
106
2.35k
#if ILMTHREAD_THREADING_ENABLED
107
2.35k
    std::lock_guard<std::mutex> lock (tMap.mutex);
108
2.35k
#endif
109
2.35k
    TypeMap::const_iterator i = tMap.find (typeName);
110
111
2.35k
    if (i == tMap.end ())
112
0
        THROW (
113
2.35k
            IEX_NAMESPACE::ArgExc,
114
2.35k
            "Cannot create image file attribute of "
115
2.35k
            "unknown type \""
116
2.35k
                << typeName << "\".");
117
118
2.35k
    return (i->second) ();
119
2.35k
}
120
121
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT