PageRenderTime 43ms CodeModel.GetById 17ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/src/FreeImage/Source/OpenEXR/IlmImf/ImfAttribute.cpp

https://bitbucket.org/cabalistic/ogredeps/
C++ | 156 lines | 75 code | 43 blank | 38 comment | 7 complexity | 2fda9e34e10ecc1fc466abcdb731e700 MD5 | raw file
  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
 50namespace Imf {
 51
 52using IlmThread::Mutex;
 53using IlmThread::Lock;
 54
 55
 56Attribute::Attribute () {}
 57
 58
 59Attribute::~Attribute () {}
 60
 61
 62namespace {
 63
 64struct NameCompare: std::binary_function <const char *, const char *, bool>
 65{
 66    bool
 67    operator () (const char *x, const char *y) const
 68    {
 69	return strcmp (x, y) < 0;
 70    }
 71};
 72
 73
 74typedef Attribute* (*Constructor)();
 75typedef std::map <const char *, Constructor, NameCompare> TypeMap;
 76
 77
 78class LockedTypeMap: public TypeMap
 79{
 80  public:
 81
 82    Mutex mutex;
 83};
 84
 85
 86LockedTypeMap &
 87typeMap ()
 88{
 89    static Mutex criticalSection;
 90    Lock lock (criticalSection);
 91
 92    static LockedTypeMap* typeMap = 0;
 93
 94    if (typeMap == 0)
 95	typeMap = new LockedTypeMap ();
 96
 97    return *typeMap;
 98}
 99
100
101} // namespace
102
103
104bool		
105Attribute::knownType (const char typeName[])
106{
107    LockedTypeMap& tMap = typeMap();
108    Lock lock (tMap.mutex);
109
110    return tMap.find (typeName) != tMap.end();
111}
112
113
114void	
115Attribute::registerAttributeType (const char typeName[],
116			          Attribute *(*newAttribute)())
117{
118    LockedTypeMap& tMap = typeMap();
119    Lock lock (tMap.mutex);
120
121    if (tMap.find (typeName) != tMap.end())
122	THROW (Iex::ArgExc, "Cannot register image file attribute "
123			    "type \"" << typeName << "\". "
124			    "The type has already been registered.");
125
126    tMap.insert (TypeMap::value_type (typeName, newAttribute));
127}
128
129
130void
131Attribute::unRegisterAttributeType (const char typeName[])
132{
133    LockedTypeMap& tMap = typeMap();
134    Lock lock (tMap.mutex);
135
136    tMap.erase (typeName);
137}
138
139
140Attribute *
141Attribute::newAttribute (const char typeName[])
142{
143    LockedTypeMap& tMap = typeMap();
144    Lock lock (tMap.mutex);
145
146    TypeMap::const_iterator i = tMap.find (typeName);
147
148    if (i == tMap.end())
149	THROW (Iex::ArgExc, "Cannot create image file attribute of "
150			    "unknown type \"" << typeName << "\".");
151
152    return (i->second)();
153}
154
155
156} // namespace Imf