]> Creatis software - clitk.git/commitdiff
error are now handled by exception
authordsarrut <dsarrut>
Mon, 4 Oct 2010 07:46:52 +0000 (07:46 +0000)
committerdsarrut <dsarrut>
Mon, 4 Oct 2010 07:46:52 +0000 (07:46 +0000)
14 files changed:
common/clitkCommon.cxx
common/clitkCommon.h
common/clitkCommon.txx
common/clitkFilterBase.cxx
common/clitkFilterBase.h
common/clitkImageCommon.cxx
common/clitkImageToImageGenericFilterBase.cxx
common/clitkImageToImageGenericFilterBase.h
segmentation/clitkConnectedComponentLabeling.cxx
segmentation/clitkExtractBones.cxx
segmentation/clitkExtractBonesGenericFilter.txx
segmentation/clitkExtractPatient.cxx
segmentation/clitkExtractPatientGenericFilter.txx
tools/clitkAutoCrop.cxx

index d3f6016..7afeb07 100644 (file)
@@ -240,7 +240,7 @@ void clitk::openFileForReading(std::ifstream & is, const std::string & filename)
 {
   is.open(filename.c_str(), std::ios::in);
   if ( is.fail() ) {
-    itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
+    clitkExceptionMacro("Could not open file (for reading): " << filename);
   }
 }
 //--------------------------------------------------------------------
@@ -251,7 +251,7 @@ void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)
 {
   os.open(filename.c_str(), std::ios::out);
   if ( os.fail() ) {
-    itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
+    clitkExceptionMacro("Could not open file (for writing): " << filename);
   }
 }
 //--------------------------------------------------------------------
index 476c0f1..879d54f 100644 (file)
@@ -22,6 +22,7 @@
 // clitk include
 #include "clitkConfiguration.h"
 #include "clitkPortability.h"
+#include "clitkExceptionObject.h"
 
 // itk include (include std)
 #include <itkContinuousIndex.h>
index d7c9140..b5a3471 100644 (file)
 ======================================================================-====*/
 #ifndef CLITKCOMMON_TXX
 #define CLITKCOMMON_TXX
-/**
-   -------------------------------------------------
-   * @file   clitkCommon.txx
-   * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
-   * @date   18 May 2006
-   *
-   -------------------------------------------------*/
 
 //-------------------------------------------------------
 // Utility functions for text file parsing (author: joel schaerer)
 
+//--------------------------------------------------------------------
 template<class ElementType>
 ElementType parse_value(std::string str)
 {
@@ -37,7 +31,10 @@ ElementType parse_value(std::string str)
   assert(!parser.fail());
   return value;
 }
+//--------------------------------------------------------------------
+
 
+//--------------------------------------------------------------------
 template<class ElementType>
 std::vector<ElementType> parse_string(std::string str,char delim)
 {
@@ -49,7 +46,10 @@ std::vector<ElementType> parse_string(std::string str,char delim)
   }
   return result;
 }
+//--------------------------------------------------------------------
 
+
+//--------------------------------------------------------------------
 template<class ElementType>
 std::vector<std::vector<ElementType> > parse_file(const char* filename,char delim)
 {
@@ -62,6 +62,8 @@ std::vector<std::vector<ElementType> > parse_file(const char* filename,char deli
   }
   return result;
 }
+//--------------------------------------------------------------------
+
 
 //--------------------------------------------------------------------
 // Convert float, double ... to string
index 07b9925..0f7e852 100644 (file)
 //--------------------------------------------------------------------
 clitk::FilterBase::FilterBase() 
 {
-  SetMustStop(false);
   SetVerboseOption(false);
   SetCurrentStepNumber(0);
   SetCurrentStepBaseId("");
-  StopOnErrorOn();
-  ResetLastError();
   VerboseWarningOffOn(); // OffOn, it's cool no ?
   SetWarning("");
-}
-//--------------------------------------------------------------------
-
-
-//--------------------------------------------------------------------
-void clitk::FilterBase::ResetLastError()
-{
-  m_LastError = "";
-}
-//--------------------------------------------------------------------
-
-
-//--------------------------------------------------------------------
-void clitk::FilterBase::SetLastError(std::string e)
-{
-  m_LastError = e;
-  if (GetStopOnError()) {
-    std::cerr << GetLastError() << std::endl;
-    exit(0);
-  }
+  m_IsCancelled = false;
 }
 //--------------------------------------------------------------------
 
@@ -68,8 +46,10 @@ void clitk::FilterBase::SetWarning(std::string e)
 //--------------------------------------------------------------------
 void clitk::FilterBase::StartNewStep(std::string s) 
 {
-  //m_CurrentStepTimer.Reset();
-  // m_CurrentStepTimer.Start();
+  if (Cancelled()) {
+    throw clitk::ExceptionObject("Filter is canceled.");
+  }
+
   m_CurrentStepNumber++;
   if (GetCurrentStepBaseId() != "") {
     std::ostringstream oss;
@@ -94,33 +74,23 @@ void clitk::FilterBase::StartNewStep(std::string s)
 //--------------------------------------------------------------------
 void clitk::FilterBase::StopCurrentStep() 
 {
-  // m_CurrentStepTimer.Stop();
-  //  m_CurrentStepTimer.Print(std::cout);
-  //  std::ostringstream oss;
-  //oss << " (" << 
-    //  m_CurrentStepName = m_CurrentStepName +"
+  
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::FilterBase::SetMustStop(bool b)
+void clitk::FilterBase::Cancel()
 {
-  m_MustStop = b;
-  if (GetMustStop()) {
-    SetLastError("Filter is interrupted.");  
-  }
+  m_IsCancelled = true;
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-bool clitk::FilterBase::GetMustStop()
-{
-  if (m_MustStop) return true;
-  if (HasError()) return true;
-  return false;
-}
+ bool clitk::FilterBase::Cancelled()
+ {
+   return m_IsCancelled;
+ }
 //--------------------------------------------------------------------
 
-
index a53a181..1cd7579 100644 (file)
@@ -32,10 +32,11 @@ namespace clitk {
   
   //--------------------------------------------------------------------
   /*
-    Convenient class to manage options from GGO gengetopt) to filter
+    Convenient class to manage options from GGO (gengetopt) to filter
   */
   //--------------------------------------------------------------------
-  class FilterBase {
+  class FilterBase
+  {
 
   public:
     // Standard class typedefs
@@ -90,15 +91,6 @@ namespace clitk {
     template<class OptionType>
     void VerboseOptionV(std::string name, int nb, OptionType * value);
 
-    // Error 
-    void SetLastError(std::string e);
-    void ResetLastError();
-    itkGetConstMacro(LastError, std::string);
-    bool HasError() { return (GetLastError() != ""); }
-    itkSetMacro(StopOnError, bool);
-    itkGetConstMacro(StopOnError, bool);
-    itkBooleanMacro(StopOnError);    
-
     void SetWarning(std::string e);
     itkGetConstMacro(Warning, std::string);
     itkSetMacro(VerboseWarningOff, bool);
@@ -106,10 +98,10 @@ namespace clitk {
     itkBooleanMacro(VerboseWarningOff);
     GGO_DefineOption_Flag(verboseWarningOff, SetVerboseWarningOff);
     
-    // Use this function to stop (when threaded)
-    void SetMustStop(bool b);
-    bool GetMustStop();
-    
+    // Use this function to cancel the filter between step
+    void Cancel();
+    bool Cancelled();
+
   protected:
     FilterBase();
     virtual ~FilterBase() {}    
@@ -125,13 +117,12 @@ namespace clitk {
     int m_NumberOfSteps;
     std::string m_CurrentStepId;
     std::string m_CurrentStepBaseId;
-    std::string m_LastError;
     std::string m_CurrentStepName;
-    bool m_StopOnError;
     std::string m_Warning;
     bool m_VerboseWarningOff;
-    bool m_MustStop;
+    bool m_IsCancelled;
     Timer m_CurrentStepTimer;
+    
 
   private:
     FilterBase(const Self&); //purposely not implemented
@@ -143,8 +134,6 @@ namespace clitk {
 } // end namespace clitk
 //--------------------------------------------------------------------
 
-#define StartNewStepOrStop(s) StartNewStep(s); if (GetMustStop()) return;
-
 #ifndef ITK_MANUAL_INSTANTIATION
 #include "clitkFilterBase.txx"
 #endif
index d3cd8d8..9367257 100644 (file)
 
   - BSD        See included LICENSE.txt file
   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
-======================================================================-====*/
+  ======================================================================-====*/
+
 #ifndef CLITKIMAGECOMMON_CXX
 #define CLITKIMAGECOMMON_CXX
-/**
-   ------------------------------------------------=
-   * @file   clitkImageCommon.cxx
-   * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
-   * @date   02 Oct 2007 14:30:47
-   *
-   * @brief
-   *
-   *
-   ------------------------------------------------=*/
 
 #include "clitkImageCommon.h"
 
 //--------------------------------------------------------------------
 void clitk::ReadImageDimensionAndPixelType(const std::string & filename,
-    int & dimension,
-    std::string & pixeType)
+                                           int & dimension,
+                                           std::string & pixeType)
 {
   itk::ImageIOBase::Pointer genericReader =
     itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
   if (!genericReader) {
-    std::cerr << "Image file format unknown while reading file <" << filename << ">" << std::endl;
-    exit(0);
+    clitkExceptionMacro("Image file format unknown while reading file <" << filename << ">");
   }
   genericReader->SetFileName(filename.c_str());
   genericReader->ReadImageInformation();
@@ -51,14 +41,13 @@ void clitk::ReadImageDimensionAndPixelType(const std::string & filename,
 
 //--------------------------------------------------------------------
 void clitk::ReadImageDimensionAndPixelType(const std::string & filename,
-    int & dimension,
-    std::string & pixeType, int & components)
+                                           int & dimension,
+                                           std::string & pixeType, int & components)
 {
   itk::ImageIOBase::Pointer genericReader =
     itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
   if (!genericReader) {
-    std::cerr << "Image file format unknown while reading " << filename << std::endl;
-    exit(0);
+    clitkExceptionMacro("Image file format unknown while reading file <" << filename << ">");
   }
   genericReader->SetFileName(filename.c_str());
   genericReader->ReadImageInformation();
@@ -91,10 +80,8 @@ itk::ImageIOBase::Pointer clitk::readImageHeader(const std::string & filename, b
     itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
   if (!reader) {
     if (exit_on_error) { //default behavior for tools who don't handle the problem
-      std::cerr << "Error reading file " << filename << ", exiting immediately" << std::endl;
-      std::exit(-1);
-    } else
-      return NULL;
+      clitkExceptionMacro("Error reading file " << filename << ", exiting immediately");
+    } else return NULL;
   }
   reader->SetFileName(filename);
   reader->ReadImageInformation();
index 259ae04..70dcba6 100644 (file)
@@ -34,8 +34,8 @@ clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string
   m_FilterName = n;
   m_FailOnImageTypeError = true;
   m_ReadOnDisk = true;
-  m_LastError = "";
-  StopOnErrorOn();
+  // m_LastError = "";
+  // StopOnErrorOn();
   SetFilterBase(NULL);
 }
 //--------------------------------------------------------------------
@@ -310,12 +310,12 @@ typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsig
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::MustStop()
-{
-  if (m_FilterBase != NULL) {
-    m_FilterBase->SetMustStop(true);
-  }
-}
+// void clitk::ImageToImageGenericFilterBase::MustStop()
+// {
+//   if (m_FilterBase != NULL) {
+//     m_FilterBase->SetMustStop(true);
+//   }
+// }
 //--------------------------------------------------------------------
 
 
index aa61904..86cd77a 100644 (file)
@@ -54,9 +54,9 @@ namespace clitk {
     void SetFilterName(std::string & n);
     
     // Error management
-    itkSetMacro(LastError, std::string);
-    itkGetConstMacro(LastError, std::string);
-    bool HasError() { return (GetLastError() != ""); }
+    // itkSetMacro(LastError, std::string);
+    // itkGetConstMacro(LastError, std::string);
+    // bool HasError() { return (GetLastError() != ""); }
 
     // Generic IO
     /// Returns the dimension and pixel type of the *first* input
@@ -94,11 +94,11 @@ namespace clitk {
     FilterBase * GetFilterBase() { return m_FilterBase; }
     
     // Indicate that the filter must stop as soon as possible (if threaded)
-    void MustStop();
+    // void MustStop();
     void DeleteLastOutputImage();
-    itkSetMacro(StopOnError, bool);
-    itkGetConstMacro(StopOnError, bool);
-    itkBooleanMacro(StopOnError);    
+    // itkSetMacro(StopOnError, bool);
+    // itkGetConstMacro(StopOnError, bool);
+    // itkBooleanMacro(StopOnError);    
 
   protected:  
     bool m_ReadOnDisk;
@@ -126,10 +126,10 @@ namespace clitk {
     void SetImageTypeError();
     bool m_FailOnImageTypeError;
     
-    std::string m_LastError;
+    // std::string m_LastError;
     void SetFilterBase(FilterBase * f) { m_FilterBase = f; }
     FilterBase * m_FilterBase;
-    bool m_StopOnError;
+    // bool m_StopOnError;
 
   }; // end class clitk::ImageToImageGenericFilter
 
index 620c5c4..bc2fcb1 100644 (file)
@@ -33,10 +33,10 @@ int main(int argc, char * argv[])
   FilterType::Pointer filter = FilterType::New();
 
   filter->SetArgsInfo(args_info);
-  filter->Update();
-
-  if (filter->HasError()) {
-    std::cout << filter->GetLastError() << std::endl;
+  try {
+    filter->Update();
+  } catch(std::runtime_error e) {
+    std::cout << e.what() << std::endl;
   }
 
   return EXIT_SUCCESS;
index 2122fc7..64cc29b 100644 (file)
@@ -33,7 +33,12 @@ int main(int argc, char * argv[])
   FilterType::Pointer filter = FilterType::New();
 
   filter->SetArgsInfo(args_info);
-  filter->Update();
+  
+  try {
+    filter->Update();
+  } catch(std::runtime_error e) {
+    std::cout << e.what() << std::endl;
+  }
 
   return EXIT_SUCCESS;
 } // This is the end, my friend
index 58f00f8..dd85634 100644 (file)
@@ -82,13 +82,6 @@ void clitk::ExtractBonesGenericFilter<ArgsInfoType>::UpdateWithInputImageType()
   // Go !
   filter->Update();
   
-  // Check if error
-  if (filter->HasError()) {
-    SetLastError(filter->GetLastError());
-    // No output
-    return;
-  }
-
   // Write/Save results
   typename OutputImageType::Pointer output = filter->GetOutput();
   this->template SetNextOutput<OutputImageType>(output); 
index 8e78809..027a62c 100644 (file)
@@ -32,10 +32,11 @@ int main(int argc, char * argv[]) {
   FilterType::Pointer filter = FilterType::New();
   
   filter->SetArgsInfo(args_info);
-  filter->Update();
-
-  if (filter->HasError()) {
-    std::cout << filter->GetLastError() << std::endl;
+  
+  try {
+    filter->Update();
+  } catch(std::runtime_error e) {
+    std::cout << e.what() << std::endl;
   }
 
   return EXIT_SUCCESS;
index f90c1c7..7fc0740 100644 (file)
@@ -82,12 +82,12 @@ void clitk::ExtractPatientGenericFilter<ArgsInfoType>::UpdateWithInputImageType(
   // Go !
   filter->Update();
   
-  // Check if error
-  if (filter->HasError()) {
-    SetLastError(filter->GetLastError());
-    // No output
-    return;
-  }
+  // // Check if error
+  // if (filter->HasError()) {
+  //   SetLastError(filter->GetLastError());
+  //   // No output
+  //   return;
+  // }
 
   // Write/Save results
   typename OutputImageType::Pointer output = filter->GetOutput();
index 93d0e01..e22fa6a 100644 (file)
@@ -33,10 +33,11 @@ int main(int argc, char * argv[])
   FilterType::Pointer filter = FilterType::New();
 
   filter->SetArgsInfo(args_info);
-  filter->Update();
 
-  if (filter->HasError()) {
-    std::cout << filter->GetLastError() << std::endl;
+  try {
+    filter->Update();
+  } catch(std::runtime_error e) {
+    std::cout << e.what() << std::endl;
   }
 
   return EXIT_SUCCESS;