/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. // class Attribute
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include <ImfAttribute.h>
  40. #include "IlmThreadMutex.h"
  41. #include "Iex.h"
  42. #include <string.h>
  43. #include <map>
  44. namespace Imf {
  45. using IlmThread::Mutex;
  46. using IlmThread::Lock;
  47. Attribute::Attribute () {}
  48. Attribute::~Attribute () {}
  49. namespace {
  50. struct NameCompare: std::binary_function <const char *, const char *, bool>
  51. {
  52. bool
  53. operator () (const char *x, const char *y) const
  54. {
  55. return strcmp (x, y) < 0;
  56. }
  57. };
  58. typedef Attribute* (*Constructor)();
  59. typedef std::map <const char *, Constructor, NameCompare> TypeMap;
  60. class LockedTypeMap: public TypeMap
  61. {
  62. public:
  63. Mutex mutex;
  64. };
  65. LockedTypeMap &
  66. typeMap ()
  67. {
  68. static Mutex criticalSection;
  69. Lock lock (criticalSection);
  70. static LockedTypeMap* typeMap = 0;
  71. if (typeMap == 0)
  72. typeMap = new LockedTypeMap ();
  73. return *typeMap;
  74. }
  75. } // namespace
  76. bool
  77. Attribute::knownType (const char typeName[])
  78. {
  79. LockedTypeMap& tMap = typeMap();
  80. Lock lock (tMap.mutex);
  81. return tMap.find (typeName) != tMap.end();
  82. }
  83. void
  84. Attribute::registerAttributeType (const char typeName[],
  85. Attribute *(*newAttribute)())
  86. {
  87. LockedTypeMap& tMap = typeMap();
  88. Lock lock (tMap.mutex);
  89. if (tMap.find (typeName) != tMap.end())
  90. THROW (Iex::ArgExc, "Cannot register image file attribute "
  91. "type \"" << typeName << "\". "
  92. "The type has already been registered.");
  93. tMap.insert (TypeMap::value_type (typeName, newAttribute));
  94. }
  95. void
  96. Attribute::unRegisterAttributeType (const char typeName[])
  97. {
  98. LockedTypeMap& tMap = typeMap();
  99. Lock lock (tMap.mutex);
  100. tMap.erase (typeName);
  101. }
  102. Attribute *
  103. Attribute::newAttribute (const char typeName[])
  104. {
  105. LockedTypeMap& tMap = typeMap();
  106. Lock lock (tMap.mutex);
  107. TypeMap::const_iterator i = tMap.find (typeName);
  108. if (i == tMap.end())
  109. THROW (Iex::ArgExc, "Cannot create image file attribute of "
  110. "unknown type \"" << typeName << "\".");
  111. return (i->second)();
  112. }
  113. } // namespace Imf