Path: blob/master/3rdparty/openexr/IlmImf/ImfAttribute.cpp
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536//-----------------------------------------------------------------------------37//38// class Attribute39//40//-----------------------------------------------------------------------------4142#include <ImfAttribute.h>43#include "IlmThreadMutex.h"44#include "Iex.h"45#include <string.h>46#include <map>474849namespace Imf {5051using IlmThread::Mutex;52using IlmThread::Lock;535455Attribute::Attribute () {}565758Attribute::~Attribute () {}596061namespace {6263struct NameCompare: std::binary_function <const char *, const char *, bool>64{65bool66operator () (const char *x, const char *y) const67{68return strcmp (x, y) < 0;69}70};717273typedef Attribute* (*Constructor)();74typedef std::map <const char *, Constructor, NameCompare> TypeMap;757677class LockedTypeMap: public TypeMap78{79public:8081Mutex mutex;82};838485LockedTypeMap &86typeMap ()87{88static Mutex criticalSection;89Lock lock (criticalSection);9091static LockedTypeMap* typeMap = 0;9293if (typeMap == 0)94typeMap = new LockedTypeMap ();9596return *typeMap;97}9899100} // namespace101102103bool104Attribute::knownType (const char typeName[])105{106LockedTypeMap& tMap = typeMap();107Lock lock (tMap.mutex);108109return tMap.find (typeName) != tMap.end();110}111112113void114Attribute::registerAttributeType (const char typeName[],115Attribute *(*newAttribute)())116{117LockedTypeMap& tMap = typeMap();118Lock lock (tMap.mutex);119120if (tMap.find (typeName) != tMap.end())121THROW (Iex::ArgExc, "Cannot register image file attribute "122"type \"" << typeName << "\". "123"The type has already been registered.");124125tMap.insert (TypeMap::value_type (typeName, newAttribute));126}127128129void130Attribute::unRegisterAttributeType (const char typeName[])131{132LockedTypeMap& tMap = typeMap();133Lock lock (tMap.mutex);134135tMap.erase (typeName);136}137138139Attribute *140Attribute::newAttribute (const char typeName[])141{142LockedTypeMap& tMap = typeMap();143Lock lock (tMap.mutex);144145TypeMap::const_iterator i = tMap.find (typeName);146147if (i == tMap.end())148THROW (Iex::ArgExc, "Cannot create image file attribute of "149"unknown type \"" << typeName << "\".");150151return (i->second)();152}153154155} // namespace Imf156157158