BaseImporter.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Definition of the base class for all importer worker classes. */
  34. #ifndef AI_BASEIMPORTER_H_INC
  35. #define AI_BASEIMPORTER_H_INC
  36. #include <string>
  37. struct aiScene;
  38. namespace Assimp
  39. {
  40. class IOSystem;
  41. // ---------------------------------------------------------------------------
  42. /** Simple exception class to be thrown if an error occurs while importing. */
  43. class ImportErrorException
  44. {
  45. public:
  46. /** Constructor with arguments */
  47. ImportErrorException( const std::string& pErrorText)
  48. {
  49. mErrorText = pErrorText;
  50. }
  51. // -------------------------------------------------------------------
  52. /** Returns the error text provided when throwing the exception */
  53. inline const std::string& GetErrorText() const
  54. { return mErrorText; }
  55. private:
  56. std::string mErrorText;
  57. };
  58. // ---------------------------------------------------------------------------
  59. /** The BaseImporter defines a common interface for all importer worker
  60. * classes.
  61. *
  62. * The interface defines two functions: CanRead() is used to check if the
  63. * importer can handle the format of the given file. If an implementation of
  64. * this function returns true, the importer then calls ReadFile() which
  65. * imports the given file. ReadFile is not overridable, it just calls
  66. * InternReadFile() and catches any ImportErrorException that might occur.
  67. */
  68. class BaseImporter
  69. {
  70. friend class Importer;
  71. protected:
  72. /** Constructor to be privately used by #Importer */
  73. BaseImporter();
  74. /** Destructor, private as well */
  75. virtual ~BaseImporter();
  76. public:
  77. // -------------------------------------------------------------------
  78. /** Returns whether the class can handle the format of the given file.
  79. * @param pFile Path and file name of the file to be examined.
  80. * @param pIOHandler The IO handler to use for accessing any file.
  81. * @return true if the class can read this file, false if not.
  82. *
  83. * @note Sometimes ASSIMP uses this method to determine whether a
  84. * a given file extension is generally supported. In this case the
  85. * file extension is passed in the pFile parameter, pIOHandler is NULL
  86. */
  87. virtual bool CanRead( const std::string& pFile,
  88. IOSystem* pIOHandler) const = 0;
  89. // -------------------------------------------------------------------
  90. /** Imports the given file and returns the imported data.
  91. * If the import succeeds, ownership of the data is transferred to
  92. * the caller. If the import failes, NULL is returned. The function
  93. * takes care that any partially constructed data is destroyed
  94. * beforehand.
  95. *
  96. * @param pFile Path of the file to be imported.
  97. * @param pIOHandler IO-Handler used to open this and possible other files.
  98. * @return The imported data or NULL if failed. If it failed a
  99. * human-readable error description can be retrieved by calling
  100. * GetErrorText()
  101. *
  102. * @note This function is not intended to be overridden. Implement
  103. * InternReadFile() to do the import. If an exception is thrown somewhere
  104. * in InternReadFile(), this function will catch it and transform it into
  105. * a suitable response to the caller.
  106. */
  107. aiScene* ReadFile( const std::string& pFile, IOSystem* pIOHandler);
  108. // -------------------------------------------------------------------
  109. /** Returns the error description of the last error that occured.
  110. * @return A description of the last error that occured. An empty
  111. * string if there was no error.
  112. */
  113. inline const std::string& GetErrorText() const
  114. { return mErrorText; }
  115. protected:
  116. // -------------------------------------------------------------------
  117. /** Called by Importer::GetExtensionList() for each loaded importer.
  118. * Importer implementations should append all file extensions
  119. * which they supported to the passed string.
  120. * Example: "*.blabb;*.quak;*.gug;*.foo" (no comma after the last!)
  121. * @param append Output string
  122. */
  123. virtual void GetExtensionList(std::string& append) = 0;
  124. // -------------------------------------------------------------------
  125. /** Imports the given file into the given scene structure. The
  126. * function is expected to throw an ImportErrorException if there is
  127. * an error. If it terminates normally, the data in aiScene is
  128. * expected to be correct. Override this function to implement the
  129. * actual importing.
  130. *
  131. * @param pFile Path of the file to be imported.
  132. * @param pScene The scene object to hold the imported data.
  133. * NULL is not a valid parameter.
  134. * @param pIOHandler The IO handler to use for any file access.
  135. * NULL is not a valid parameter.
  136. */
  137. virtual void InternReadFile( const std::string& pFile,
  138. aiScene* pScene, IOSystem* pIOHandler) = 0;
  139. protected:
  140. /** Error description in case there was one. */
  141. std::string mErrorText;
  142. };
  143. } // end of namespace Assimp
  144. #endif // AI_BASEIMPORTER_H_INC