Download | Plain Text | No Line Numbers


  1. /**
  2.  * @module cfile
  3.  * @author Manuel Mausz, 0728348
  4.  * @brief Abstract class for handling files.
  5.  * Needed for generic use in CScriptparser.
  6.  * @date 17.04.2009
  7.  */
  8.  
  9. #ifndef CFILE_H
  10. #define CFILE_H
  11.  
  12. #include <string>
  13. #include <set>
  14. #include <list>
  15. #include <fstream>
  16. #include <stdexcept>
  17.  
  18. /**
  19.  * @class CFile
  20.  * @brief Abstract class for handling files. Needed for generic use in
  21.  * CScriptparser.
  22.  *
  23.  * In order for CScriptparser to determine which instance of CFile supports
  24.  * which filetype, every implemententation need to insert their filetypes to
  25.  * the member m_types in their constructor.
  26.  *
  27.  * On error throw FileError.
  28.  */
  29. class CFile
  30. {
  31. public:
  32. /**
  33.   * @class FileError
  34.   * @brief Exception thrown by implemententations of CFile
  35.   */
  36. class FileError : public std::invalid_argument {
  37. public:
  38. /**
  39.   * @method FileError
  40.   * @brief Default exception ctor
  41.   * @param what message to pass along
  42.   * @return -
  43.   * @globalvars none
  44.   * @exception none
  45.   * @conditions none
  46.   */
  47. FileError(const std::string& what)
  48. : std::invalid_argument(what)
  49. {}
  50. };
  51.  
  52. /**
  53.   * @method ~CFile
  54.   * @brief Default dtor (virtual)
  55.   * @param -
  56.   * @return -
  57.   * @globalvars none
  58.   * @exception none
  59.   * @conditions none
  60.   */
  61. virtual ~CFile()
  62. {};
  63.  
  64. /**
  65.   * @method read
  66.   * @brief Pure virtual method (interface). Should read data from filestream.
  67.   * @param in filestream to read data from
  68.   * @return -
  69.   * @globalvars none
  70.   * @exception FileError
  71.   * @conditions none
  72.   */
  73. virtual void read(std::ifstream& in) = 0;
  74.  
  75. /**
  76.   * @method write
  77.   * @brief Pure virtual method (interface). Should write data to filestream.
  78.   * @param out filestream to write data to
  79.   * @return -
  80.   * @globalvars none
  81.   * @exception FileError
  82.   * @conditions none
  83.   */
  84. virtual void write(std::ofstream& out) = 0;
  85.  
  86. /**
  87.   * @method callFunc
  88.   * @brief Pure virtual method (interface). Should delegate the function
  89.   * and its parameters to the correct internal method.
  90.   * @param func function name
  91.   * @param params function parameters as list
  92.   * @return -
  93.   * @globalvars none
  94.   * @exception FileError
  95.   * @conditions none
  96.   */
  97. virtual void callFunc(const std::string& func, const std::list<std::string>& params) = 0;
  98.  
  99. /**
  100.   * @method supportsType
  101.   * @brief Check if filetype is supported by this implementation.
  102.   * @param type filetype
  103.   * @return true if filetype is supported. false otherwise
  104.   * @globalvars none
  105.   * @exception none
  106.   * @conditions none
  107.   */
  108. bool supportsType(const std::string& type)
  109. {
  110. return (m_types.find(type) == m_types.end()) ? false : true;
  111. }
  112.  
  113. protected:
  114. /* members */
  115. /** set of filetypes suppported by this implementation */
  116. std::set<std::string> m_types;
  117. };
  118.  
  119. #endif
  120.  
  121. /* vim: set et sw=2 ts=2: */
  122.