ImfAttribute.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include "ImfNamespace.h"
  45. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  46. using ILMTHREAD_NAMESPACE::Mutex;
  47. using ILMTHREAD_NAMESPACE::Lock;
  48. Attribute::Attribute () {}
  49. Attribute::~Attribute () {}
  50. namespace {
  51. struct NameCompare
  52. {
  53. bool
  54. operator () (const char *x, const char *y) const
  55. {
  56. return strcmp (x, y) < 0;
  57. }
  58. };
  59. typedef Attribute* (*Constructor)();
  60. typedef std::map <const char *, Constructor, NameCompare> TypeMap;
  61. class LockedTypeMap: public TypeMap
  62. {
  63. public:
  64. Mutex mutex;
  65. };
  66. LockedTypeMap &
  67. typeMap ()
  68. {
  69. // c++11 requires thread-safe static variable initialization
  70. #if __cplusplus >= 201103L
  71. static LockedTypeMap tMap;
  72. return tMap;
  73. #else
  74. static Mutex criticalSection;
  75. Lock lock (criticalSection);
  76. static LockedTypeMap* typeMap = 0;
  77. if (typeMap == 0)
  78. typeMap = new LockedTypeMap ();
  79. return *typeMap;
  80. #endif
  81. }
  82. } // namespace
  83. bool
  84. Attribute::knownType (const char typeName[])
  85. {
  86. LockedTypeMap& tMap = typeMap();
  87. Lock lock (tMap.mutex);
  88. return tMap.find (typeName) != tMap.end();
  89. }
  90. void
  91. Attribute::registerAttributeType (const char typeName[],
  92. Attribute *(*newAttribute)())
  93. {
  94. LockedTypeMap& tMap = typeMap();
  95. Lock lock (tMap.mutex);
  96. if (tMap.find (typeName) != tMap.end())
  97. THROW (IEX_NAMESPACE::ArgExc, "Cannot register image file attribute "
  98. "type \"" << typeName << "\". "
  99. "The type has already been registered.");
  100. tMap.insert (TypeMap::value_type (typeName, newAttribute));
  101. }
  102. void
  103. Attribute::unRegisterAttributeType (const char typeName[])
  104. {
  105. LockedTypeMap& tMap = typeMap();
  106. Lock lock (tMap.mutex);
  107. tMap.erase (typeName);
  108. }
  109. Attribute *
  110. Attribute::newAttribute (const char typeName[])
  111. {
  112. LockedTypeMap& tMap = typeMap();
  113. Lock lock (tMap.mutex);
  114. TypeMap::const_iterator i = tMap.find (typeName);
  115. if (i == tMap.end())
  116. THROW (IEX_NAMESPACE::ArgExc, "Cannot create image file attribute of "
  117. "unknown type \"" << typeName << "\".");
  118. return (i->second)();
  119. }
  120. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT