BaseImporter.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /** @file Definition of the base class for all importer worker classes. */
  2. #ifndef AI_BASEIMPORTER_H_INC
  3. #define AI_BASEIMPORTER_H_INC
  4. #include <string>
  5. struct aiScene;
  6. namespace Assimp
  7. {
  8. class IOSystem;
  9. // ---------------------------------------------------------------------------
  10. /** Simple exception class to be thrown if an error occurs while importing. */
  11. class ImportErrorException
  12. {
  13. public:
  14. /** Constructor with arguments */
  15. ImportErrorException( const std::string& pErrorText)
  16. {
  17. mErrorText = pErrorText;
  18. }
  19. /** Returns the error text provided when throwing the exception */
  20. const std::string& GetErrorText() const { return mErrorText; }
  21. private:
  22. std::string mErrorText;
  23. };
  24. // ---------------------------------------------------------------------------
  25. /** The BaseImporter defines a common interface for all importer worker
  26. * classes.
  27. *
  28. * The interface defines two functions: CanRead() is used to check if the
  29. * importer can handle the format of the given file. If an implementation of
  30. * this function returns true, the importer then calls ReadFile() which
  31. * imports the given file. ReadFile is not overridable, it just calls InternReadFile()
  32. * and catches any ImportErrorException that might occur.
  33. */
  34. class BaseImporter
  35. {
  36. friend class Importer;
  37. protected:
  38. /** Constructor to be privately used by Importer */
  39. BaseImporter();
  40. /** Destructor, private as well */
  41. virtual ~BaseImporter();
  42. public:
  43. // -------------------------------------------------------------------
  44. /** Returns whether the class can handle the format of the given file.
  45. * @param pFile Path and file name of the file to be examined.
  46. * @param pIOHandler The IO handler to use for accessing any file.
  47. * @return true if the class can read this file, false if not.
  48. */
  49. virtual bool CanRead( const std::string& pFile, IOSystem* pIOHandler) const = 0;
  50. // -------------------------------------------------------------------
  51. /** Imports the given file and returns the imported data.
  52. * If the import succeeds, ownership of the data is transferred to the caller.
  53. * If the import failes, NULL is returned. The function takes care that any
  54. * partially constructed data is destroyed beforehand.
  55. *
  56. * @param pFile Path of the file to be imported.
  57. * @param pIOHandler IO-Handler used to open this and possible other files.
  58. * @return The imported data or NULL if failed. If it failed a human-readable
  59. * error description can be retrieved by calling GetErrorText()
  60. *
  61. * @note This function is not intended to be overridden. Implement InternReadFile()
  62. * to do the import. If an exception is thrown somewhere in InternReadFile(),
  63. * this function will catch it and transform it into a suitable response to the caller.
  64. */
  65. aiScene* ReadFile( const std::string& pFile, IOSystem* pIOHandler);
  66. // -------------------------------------------------------------------
  67. /** Returns the error description of the last error that occured.
  68. * @return A description of the last error that occured. An empty string if no error.
  69. */
  70. const std::string& GetErrorText() const { return mErrorText; }
  71. protected:
  72. // -------------------------------------------------------------------
  73. /** Imports the given file into the given scene structure. The function is
  74. * expected to throw an ImportErrorException if there is an error. If it
  75. * terminates normally, the data in aiScene is expected to be correct.
  76. * Override this function to implement the actual importing.
  77. *
  78. * @param pFile Path of the file to be imported.
  79. * @param pScene The scene object to hold the imported data.
  80. * @param pIOHandler The IO handler to use for any file access.
  81. */
  82. virtual void InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) = 0;
  83. protected:
  84. /** Error description in case there was one. */
  85. std::string mErrorText;
  86. };
  87. } // end of namespace Assimp
  88. #endif // AI_BASEIMPORTER_H_INC