cmCableData.cxx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #include "cmCableData.h"
  12. #include "cmCacheManager.h"
  13. /**
  14. * Free all data that was stored here.
  15. */
  16. cmCableData::~cmCableData()
  17. {
  18. for(OutputFiles::iterator i = m_OutputFiles.begin();
  19. i != m_OutputFiles.end(); ++i)
  20. {
  21. delete i->second;
  22. }
  23. }
  24. /**
  25. * The constructor attempts to open the file for writing.
  26. */
  27. cmCableData::OutputFile
  28. ::OutputFile(std::string file, const cmCableCommand* command):
  29. m_FileStream(file.c_str()),
  30. m_FirstReferencingCommand(command),
  31. m_LastReferencingCommand(command)
  32. {
  33. if(!m_FileStream)
  34. {
  35. cmSystemTools::Error("Error can not open for write: ", file.c_str());
  36. }
  37. }
  38. /**
  39. * Destructor closes the file, if it was open.
  40. */
  41. cmCableData::OutputFile
  42. ::~OutputFile()
  43. {
  44. if(m_FileStream)
  45. m_FileStream.close();
  46. }
  47. /**
  48. * Get the output stream associated with this OutputFile.
  49. */
  50. std::ostream&
  51. cmCableData::OutputFile
  52. ::GetStream()
  53. {
  54. return m_FileStream;
  55. }
  56. void
  57. cmCableData::OutputFile
  58. ::SetLastReferencingCommand(const cmCableCommand* command)
  59. {
  60. m_LastReferencingCommand = command;
  61. }
  62. bool
  63. cmCableData::OutputFile
  64. ::FirstReferencingCommandIs(const cmCableCommand* command) const
  65. {
  66. return (m_FirstReferencingCommand == command);
  67. }
  68. bool
  69. cmCableData::OutputFile
  70. ::LastReferencingCommandIs(const cmCableCommand* command) const
  71. {
  72. return (m_LastReferencingCommand == command);
  73. }
  74. /**
  75. * Get the OutputFile for the file with the given name. Automatically
  76. * maintains first and last referencing commands.
  77. */
  78. cmCableData::OutputFile*
  79. cmCableData::GetOutputFile(const std::string& name,
  80. const cmCableCommand* command)
  81. {
  82. OutputFiles::iterator f = m_OutputFiles.find(name);
  83. // If the file hasn't yet been opened, create an entry for it.
  84. if(f == m_OutputFiles.end())
  85. {
  86. OutputFile* outputFile = new OutputFile(name, command);
  87. m_OutputFiles[name] = outputFile;
  88. return outputFile;
  89. }
  90. // The file has already been opened. Set the command as the last
  91. // referencing command.
  92. f->second->SetLastReferencingCommand(command);
  93. return f->second;
  94. }