/src/freeimage-svn/FreeImage/trunk/Source/OpenEXR/IlmImf/ImfAttribute.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /////////////////////////////////////////////////////////////////////////// |
2 | | // |
3 | | // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas |
4 | | // Digital Ltd. LLC |
5 | | // |
6 | | // All rights reserved. |
7 | | // |
8 | | // Redistribution and use in source and binary forms, with or without |
9 | | // modification, are permitted provided that the following conditions are |
10 | | // met: |
11 | | // * Redistributions of source code must retain the above copyright |
12 | | // notice, this list of conditions and the following disclaimer. |
13 | | // * Redistributions in binary form must reproduce the above |
14 | | // copyright notice, this list of conditions and the following disclaimer |
15 | | // in the documentation and/or other materials provided with the |
16 | | // distribution. |
17 | | // * Neither the name of Industrial Light & Magic nor the names of |
18 | | // its contributors may be used to endorse or promote products derived |
19 | | // from this software without specific prior written permission. |
20 | | // |
21 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
24 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
25 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | // |
33 | | /////////////////////////////////////////////////////////////////////////// |
34 | | |
35 | | |
36 | | |
37 | | //----------------------------------------------------------------------------- |
38 | | // |
39 | | // class Attribute |
40 | | // |
41 | | //----------------------------------------------------------------------------- |
42 | | |
43 | | #include <ImfAttribute.h> |
44 | | #include "IlmThreadMutex.h" |
45 | | #include "Iex.h" |
46 | | #include <string.h> |
47 | | #include <map> |
48 | | |
49 | | #include "ImfNamespace.h" |
50 | | |
51 | | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER |
52 | | |
53 | | |
54 | | using ILMTHREAD_NAMESPACE::Mutex; |
55 | | using ILMTHREAD_NAMESPACE::Lock; |
56 | | |
57 | | |
58 | 0 | Attribute::Attribute () {} |
59 | | |
60 | | |
61 | 0 | Attribute::~Attribute () {} |
62 | | |
63 | | |
64 | | namespace { |
65 | | |
66 | | struct NameCompare: std::binary_function <const char *, const char *, bool> |
67 | | { |
68 | | bool |
69 | | operator () (const char *x, const char *y) const |
70 | 864 | { |
71 | 864 | return strcmp (x, y) < 0; |
72 | 864 | } |
73 | | }; |
74 | | |
75 | | |
76 | | typedef Attribute* (*Constructor)(); |
77 | | typedef std::map <const char *, Constructor, NameCompare> TypeMap; |
78 | | |
79 | | |
80 | | class LockedTypeMap: public TypeMap |
81 | | { |
82 | | public: |
83 | | |
84 | | Mutex mutex; |
85 | | }; |
86 | | |
87 | | |
88 | | LockedTypeMap & |
89 | | typeMap () |
90 | 58 | { |
91 | 58 | static Mutex criticalSection; |
92 | 58 | Lock lock (criticalSection); |
93 | | |
94 | 58 | static LockedTypeMap* typeMap = 0; |
95 | | |
96 | 58 | if (typeMap == 0) |
97 | 2 | typeMap = new LockedTypeMap (); |
98 | | |
99 | 58 | return *typeMap; |
100 | 58 | } |
101 | | |
102 | | |
103 | | } // namespace |
104 | | |
105 | | |
106 | | bool |
107 | | Attribute::knownType (const char typeName[]) |
108 | 0 | { |
109 | 0 | LockedTypeMap& tMap = typeMap(); |
110 | 0 | Lock lock (tMap.mutex); |
111 | |
|
112 | 0 | return tMap.find (typeName) != tMap.end(); |
113 | 0 | } |
114 | | |
115 | | |
116 | | void |
117 | | Attribute::registerAttributeType (const char typeName[], |
118 | | Attribute *(*newAttribute)()) |
119 | 58 | { |
120 | 58 | LockedTypeMap& tMap = typeMap(); |
121 | 58 | Lock lock (tMap.mutex); |
122 | | |
123 | 58 | if (tMap.find (typeName) != tMap.end()) |
124 | 0 | THROW (IEX_NAMESPACE::ArgExc, "Cannot register image file attribute " |
125 | 58 | "type \"" << typeName << "\". " |
126 | 58 | "The type has already been registered."); |
127 | | |
128 | 58 | tMap.insert (TypeMap::value_type (typeName, newAttribute)); |
129 | 58 | } |
130 | | |
131 | | |
132 | | void |
133 | | Attribute::unRegisterAttributeType (const char typeName[]) |
134 | 0 | { |
135 | 0 | LockedTypeMap& tMap = typeMap(); |
136 | 0 | Lock lock (tMap.mutex); |
137 | |
|
138 | 0 | tMap.erase (typeName); |
139 | 0 | } |
140 | | |
141 | | |
142 | | Attribute * |
143 | | Attribute::newAttribute (const char typeName[]) |
144 | 0 | { |
145 | 0 | LockedTypeMap& tMap = typeMap(); |
146 | 0 | Lock lock (tMap.mutex); |
147 | |
|
148 | 0 | TypeMap::const_iterator i = tMap.find (typeName); |
149 | |
|
150 | 0 | if (i == tMap.end()) |
151 | 0 | THROW (IEX_NAMESPACE::ArgExc, "Cannot create image file attribute of " |
152 | 0 | "unknown type \"" << typeName << "\"."); |
153 | | |
154 | 0 | return (i->second)(); |
155 | 0 | } |
156 | | |
157 | | |
158 | | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT |