[ismrmrd] 114/177: adding exception throws using error stack messages

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Jan 14 20:02:09 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to annotated tag v1.1.0.beta.1
in repository ismrmrd.

commit e762da58b5ac3d73ab5536d09d651e2516f96859
Author: Joseph Naegele <joseph.naegele at gmail.com>
Date:   Mon Sep 29 14:04:34 2014 -0400

    adding exception throws using error stack messages
---
 include/ismrmrd/ismrmrd.h |   3 +
 libsrc/dataset.c          |  14 ++---
 libsrc/dataset.cpp        |  34 +++++++----
 libsrc/ismrmrd.cpp        | 149 +++++++++++++++++++++++++++++++++++++---------
 4 files changed, 149 insertions(+), 51 deletions(-)

diff --git a/include/ismrmrd/ismrmrd.h b/include/ismrmrd/ismrmrd.h
index 49e85bf..7a8fd3b 100644
--- a/include/ismrmrd/ismrmrd.h
+++ b/include/ismrmrd/ismrmrd.h
@@ -416,6 +416,9 @@ EXPORTISMRMRD void ismrmrd_quaternion_to_directions(float quat[4], float read_di
 
 ///  ISMRMRD C++ Interface
 
+/// Construct exception message from ISMRMRD error stack
+std::string build_exception_string(void);
+
 /// Some typedefs to beautify the namespace
 typedef  ISMRMRD_EncodingCounters EncodingCounters;
 
diff --git a/libsrc/dataset.c b/libsrc/dataset.c
index 5a4b25a..fd6b971 100644
--- a/libsrc/dataset.c
+++ b/libsrc/dataset.c
@@ -601,17 +601,16 @@ int append_element(const ISMRMRD_Dataset * dset, const char * path, void * elem,
 /********************/
 int ismrmrd_init_dataset(ISMRMRD_Dataset *dset, const char *filename, const char *groupname) {
     if (dset) {
-        dset->filename = (char *) malloc(strlen(filename) + 1);
+        dset->filename = strdup(filename);
         if (dset->filename == NULL) {
             return ISMRMRD_PUSH_ERR(ISMRMRD_MEMORYERROR, "Failed to malloc dataset groupname");
         }
-        strcpy(dset->filename, filename);
         
-        dset->groupname = (char *) malloc(strlen(groupname) + 1);
+        dset->groupname = strdup(groupname);
         if (dset->groupname == NULL) {
             return ISMRMRD_PUSH_ERR(ISMRMRD_MEMORYERROR, "Failed to malloc dataset groupname");
         }
-        strcpy(dset->groupname, groupname);
+
         dset->fileid = 0;
         return ISMRMRD_NOERROR;
     }
@@ -635,10 +634,9 @@ int ismrmrd_open_dataset(ISMRMRD_Dataset *dset, const bool create_if_needed) {
         if (fileid > 0) {
             dset->fileid = fileid;
         }
-        else {
+        else if (create_if_needed == false) {
             /* Some sort of error opening the file */
             /* Maybe it doesn't exist? */
-            if (create_if_needed == false) {
             return ISMRMRD_PUSH_ERR(ISMRMRD_FILEERROR, "Failed to open file.");
         }
         else {
@@ -655,10 +653,8 @@ int ismrmrd_open_dataset(ISMRMRD_Dataset *dset, const bool create_if_needed) {
                 return ISMRMRD_PUSH_ERR(ISMRMRD_FILEERROR, "Failed to open file.");
             }
         }
-        }
-        
         /* Open the existing dataset */
-        /* insure that /groupname exists */
+        /* ensure that /groupname exists */
         create_link(dset, dset->groupname);
         
         return ISMRMRD_NOERROR;
diff --git a/libsrc/dataset.cpp b/libsrc/dataset.cpp
index 7ff576c..ad07fca 100644
--- a/libsrc/dataset.cpp
+++ b/libsrc/dataset.cpp
@@ -3,6 +3,7 @@
 // for memcpy and free in older compilers
 #include <string.h>
 #include <stdlib.h>
+#include <stdexcept>
 
 namespace ISMRMRD {
 //
@@ -15,10 +16,13 @@ Dataset::Dataset(const char* filename, const char* groupname, bool create_file_i
     // Initialize the dataset
     int status;
     status = ismrmrd_init_dataset(&dset_, filename, groupname);
+    if (status != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
     // Open the file
     status = ismrmrd_open_dataset(&dset_, create_file_if_needed);
     if (status != ISMRMRD_NOERROR) {
-      // TODO throw an exception
+        throw std::runtime_error(build_exception_string());
     }
 }
 
@@ -27,7 +31,7 @@ Dataset::~Dataset()
 {
     int status = ismrmrd_close_dataset(&dset_);
     if (status != ISMRMRD_NOERROR) {
-      // TODO throw an exception
+        throw std::runtime_error(build_exception_string());
     }
 }
 
@@ -40,8 +44,12 @@ int Dataset::writeHeader(const std::string &xmlstring)
 
 int Dataset::readHeader(std::string& xmlstring){
     char * temp = ismrmrd_read_header(&dset_);
-    xmlstring = std::string(temp);
-    free(temp);
+    if (NULL == temp) {
+        throw std::runtime_error(build_exception_string());
+    } else {
+        xmlstring = std::string(temp);
+        free(temp);
+    }
     return ISMRMRD_NOERROR;
 }
 
@@ -55,7 +63,7 @@ int Dataset::appendAcquisition(const Acquisition &acq)
 int Dataset::readAcquisition(uint32_t index, Acquisition & acq) {
     int status = ismrmrd_read_acquisition(&dset_, index, reinterpret_cast<ISMRMRD_Acquisition*>(&acq));
     if (status != ISMRMRD_NOERROR) {
-      //TODO throw an exception
+        throw std::runtime_error(build_exception_string());
     }
     return status;
 }
@@ -63,7 +71,7 @@ int Dataset::readAcquisition(uint32_t index, Acquisition & acq) {
 
 uint32_t Dataset::getNumberOfAcquisitions()
 {
-    uint32_t num =  ismrmrd_get_number_of_acquisitions(&dset_);
+    uint32_t num = ismrmrd_get_number_of_acquisitions(&dset_);
     return num;
 }
 
@@ -72,7 +80,7 @@ int Dataset::appendImage(const std::string &var, const ISMRMRD_BlockModes blockm
 {
     int status = ismrmrd_append_image(&dset_, var.c_str(), blockmode, reinterpret_cast<const ISMRMRD_Image*>(&im));
     if (status != ISMRMRD_NOERROR) {
-      //TODO throw an exception
+        throw std::runtime_error(build_exception_string());
     }
     return status;
 }
@@ -81,7 +89,7 @@ int Dataset::appendImage(const std::string &var, const ISMRMRD_BlockModes blockm
 {
     int status = ismrmrd_append_image(&dset_, var.c_str(), blockmode, im);
     if (status != ISMRMRD_NOERROR) {
-      //TODO throw an exception
+        throw std::runtime_error(build_exception_string());
     }
     return status;
 }
@@ -89,7 +97,7 @@ int Dataset::appendImage(const std::string &var, const ISMRMRD_BlockModes blockm
 int Dataset::readImage(const std::string &var, uint32_t index, Image &im) {
     int status = ismrmrd_read_image(&dset_, var.c_str(), index, reinterpret_cast<ISMRMRD_Image*>(&im));
     if (status != ISMRMRD_NOERROR) {
-      //TODO throw an exception
+        throw std::runtime_error(build_exception_string());
     }
     return status;
 }
@@ -106,7 +114,7 @@ template <typename T> int Dataset::appendNDArray(const std::string &var, const I
 {
     int status = ismrmrd_append_array(&dset_, var.c_str(), blockmode, static_cast<const ISMRMRD_NDArray*>(&arr));
     if (status != ISMRMRD_NOERROR) {
-      //TODO throw an exception
+        throw std::runtime_error(build_exception_string());
     }
     return status;
 }
@@ -124,7 +132,7 @@ int Dataset::appendNDArray(const std::string &var, const ISMRMRD_BlockModes bloc
 {
     int status = ismrmrd_append_array(&dset_, var.c_str(), blockmode, arr);
     if (status != ISMRMRD_NOERROR) {
-      //TODO throw an exception
+        throw std::runtime_error(build_exception_string());
     }
     return status;
 }
@@ -132,14 +140,14 @@ int Dataset::appendNDArray(const std::string &var, const ISMRMRD_BlockModes bloc
 template <typename T> int Dataset::readNDArray(const std::string &var, uint32_t index, NDArray<T> &arr) {
     int status = ismrmrd_read_array(&dset_, var.c_str(), index, static_cast<ISMRMRD_NDArray*>(&arr));
     if (status != ISMRMRD_NOERROR) {
-      //TODO throw an exception
+        throw std::runtime_error(build_exception_string());
     }
     return status;
 }
 
 uint32_t Dataset::getNumberOfNDArrays(const std::string &var)
 {
-    uint32_t num =  ismrmrd_get_number_of_arrays(&dset_, var.c_str());
+    uint32_t num = ismrmrd_get_number_of_arrays(&dset_, var.c_str());
     return num;
 }
 
diff --git a/libsrc/ismrmrd.cpp b/libsrc/ismrmrd.cpp
index 888fcfe..20c87ba 100644
--- a/libsrc/ismrmrd.cpp
+++ b/libsrc/ismrmrd.cpp
@@ -1,10 +1,25 @@
 #include <string.h>
 #include <stdlib.h>
+#include <sstream>
+#include <stdexcept>
 
 #include "ismrmrd/ismrmrd.h"
 
 namespace ISMRMRD {
 
+std::string build_exception_string(void)
+{
+    char *file = NULL, *func = NULL, *msg = NULL;
+    int line = 0, code = 0;
+    std::stringstream stream;
+    while (ismrmrd_pop_error(&file, &line, &func, &code, &msg)) {
+        stream << "ISMRMRD " << ismrmrd_strerror(code) << " in " << func <<
+                " (" << file << ":" << line << ": " << msg << std::endl;
+    }
+    return stream.str();
+}
+
+
 // Internal function to control the allowed data types for NDArrays
 template <typename T>  ISMRMRD_DataTypes get_data_type();
 template <> ISMRMRD_DataTypes get_data_type<uint16_t>()
@@ -52,7 +67,9 @@ template <> inline ISMRMRD_DataTypes get_data_type<complex_double_t>()
 //
 // Constructors
 AcquisitionHeader::AcquisitionHeader() {
-    ismrmrd_init_acquisition_header(this);
+    if (ismrmrd_init_acquisition_header(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 // Flag methods
@@ -79,27 +96,45 @@ void AcquisitionHeader::clearAllFlags() {
 //
 // Constructors, assignment operator, destructor
 Acquisition::Acquisition() {
-    ismrmrd_init_acquisition(this);
+    if (ismrmrd_init_acquisition(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 Acquisition::Acquisition(const Acquisition &other) {
+    int err = 0;
     // This is a deep copy
-    ismrmrd_init_acquisition(this);
-    ismrmrd_copy_acquisition(this, &other);
+    err = ismrmrd_init_acquisition(this);
+    if (err) {
+        throw std::runtime_error(build_exception_string());
+    }
+    err = ismrmrd_copy_acquisition(this, &other);
+    if (err) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 Acquisition & Acquisition::operator= (const Acquisition &other) {
     // Assignment makes a copy
+    int err = 0;
     if (this != &other )
     {
-        ismrmrd_init_acquisition(this);
-        ismrmrd_copy_acquisition(this, &other);
+        err = ismrmrd_init_acquisition(this);
+        if (err) {
+            throw std::runtime_error(build_exception_string());
+        }
+        err = ismrmrd_copy_acquisition(this, &other);
+        if (err) {
+            throw std::runtime_error(build_exception_string());
+        }
     }
     return *this;
 }
 
 Acquisition::~Acquisition() {
-    ismrmrd_cleanup_acquisition(this);
+    if (ismrmrd_cleanup_acquisition(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 // Accessors and mutators
@@ -133,7 +168,9 @@ const uint16_t &Acquisition::number_of_samples() {
 
 void Acquisition::number_of_samples(uint16_t num_samples) {
     head.number_of_samples = num_samples;
-    ismrmrd_make_consistent_acquisition(this);
+    if (ismrmrd_make_consistent_acquisition(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 uint16_t &Acquisition::available_channels() {
@@ -146,7 +183,9 @@ const uint16_t &Acquisition::active_channels() {
 
 void Acquisition::active_channels(uint16_t num_channels) {
     head.active_channels = num_channels;
-    ismrmrd_make_consistent_acquisition(this);
+    if (ismrmrd_make_consistent_acquisition(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 const uint64_t (&Acquisition::channel_mask()) [ISMRMRD_CHANNEL_MASKS] {
@@ -175,7 +214,9 @@ const uint16_t &Acquisition::trajectory_dimensions() {
 
 void Acquisition::trajectory_dimensions(uint16_t traj_dim) {
     head.trajectory_dimensions =  traj_dim;
-    ismrmrd_make_consistent_acquisition(this);
+    if (ismrmrd_make_consistent_acquisition(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 
@@ -234,7 +275,9 @@ AcquisitionHeader & Acquisition::getHead() {
 
 void Acquisition::setHead(const AcquisitionHeader other) {
     memcpy(&head, &other, sizeof(AcquisitionHeader));
-    ismrmrd_make_consistent_acquisition(this);
+    if (ismrmrd_make_consistent_acquisition(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 complex_float_t * Acquisition::getData() {
@@ -267,7 +310,9 @@ void Acquisition::clearAllFlags() {
 
 // Constructor
 ImageHeader::ImageHeader() {
-    ismrmrd_init_image_header(this);
+    if (ismrmrd_init_image_header(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 };
 
 // Flag methods
@@ -293,27 +338,45 @@ void ImageHeader::clearAllFlags() {
 
 // Constructors
 Image::Image() {
-    ismrmrd_init_image(this);
+    if (ismrmrd_init_image(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 };
 
 Image::Image(const Image &other) {
+    int err = 0;
     // This is a deep copy
-    ismrmrd_init_image(this);
-    ismrmrd_copy_image(this, &other);
+    err = ismrmrd_init_image(this);
+    if (err) {
+        throw std::runtime_error(build_exception_string());
+    }
+    err = ismrmrd_copy_image(this, &other);
+    if (err) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 Image & Image::operator= (const Image &other) {
+    int err = 0;
     // Assignment makes a copy
     if (this != &other )
     {
-        ismrmrd_init_image(this);
-        ismrmrd_copy_image(this, &other);
+        err = ismrmrd_init_image(this);
+        if (err) {
+            throw std::runtime_error(build_exception_string());
+        }
+        err = ismrmrd_copy_image(this, &other);
+        if (err) {
+            throw std::runtime_error(build_exception_string());
+        }
     }
     return *this;
 }
 
 Image::~Image() {
-    ismrmrd_cleanup_image(this);
+    if (ismrmrd_cleanup_image(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 // Accessors and mutators
@@ -328,7 +391,9 @@ const uint16_t &Image::data_type() {
 void Image::data_type(uint16_t dtype) {
     // TODO function to check if type is valid
     head.data_type = dtype;
-    ismrmrd_make_consistent_image(this);
+    if (ismrmrd_make_consistent_image(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 };
 
 const uint64_t &Image::flags() {
@@ -355,7 +420,9 @@ void Image::matrix_size(const uint16_t msize[3]) {
     } else {
         head.matrix_size[2] = 1;
     }
-    ismrmrd_make_consistent_image(this);
+    if (ismrmrd_make_consistent_image(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 };
 
 float (&Image::field_of_view())[3] {
@@ -372,7 +439,9 @@ void Image::channels(const uint16_t num_channels) {
     } else {
         head.channels = 1;
     }
-    ismrmrd_make_consistent_image(this);
+    if (ismrmrd_make_consistent_image(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 };
 
 float (&Image::position())[3] {
@@ -459,7 +528,9 @@ ImageHeader &Image::getHead() {
 
 void Image::setHead(const ImageHeader other) {
     memcpy(&head, &other, sizeof(ImageHeader));
-    ismrmrd_make_consistent_image(this);
+    if (ismrmrd_make_consistent_image(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 void Image::setAttributeString(std::string attr) {
@@ -502,35 +573,55 @@ void Image::clearAllFlags() {
 //
 template <typename T> NDArray<T>::NDArray()
 {
-    ismrmrd_init_ndarray(this);
+    if (ismrmrd_init_ndarray(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
     data_type = get_data_type<T>();
 }
 
 template <typename T> NDArray<T>::NDArray(const std::vector<size_t> dimvec)
 {
-    ismrmrd_init_ndarray(this);
+    if (ismrmrd_init_ndarray(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
     data_type = get_data_type<T>();
     resize(dimvec);
 }
 
 template <typename T> NDArray<T>::NDArray(const NDArray<T> &other)
 {
-    ismrmrd_init_ndarray(this);
-    ismrmrd_copy_ndarray(this, &other);
+    int err = 0;
+    err = ismrmrd_init_ndarray(this);
+    if (err) {
+        throw std::runtime_error(build_exception_string());
+    }
+    err = ismrmrd_copy_ndarray(this, &other);
+    if (err) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 template <typename T> NDArray<T>::~NDArray()
 {
-    ismrmrd_cleanup_ndarray(this);
+    if (ismrmrd_cleanup_ndarray(this) != ISMRMRD_NOERROR) {
+        throw std::runtime_error(build_exception_string());
+    }
 }
 
 template <typename T> NDArray<T> & NDArray<T>::operator= (const NDArray<T> &other)
 {
+    int err = 0;
     // Assignment makes a copy
     if (this != &other )
     {
-        ismrmrd_init_ndarray(this);
-        ismrmrd_copy_ndarray(this, &other);
+        err = ismrmrd_init_ndarray(this);
+        if (err) {
+            throw std::runtime_error(build_exception_string());
+        }
+        err = ismrmrd_copy_ndarray(this, &other);
+        if (err) {
+            throw std::runtime_error(build_exception_string());
+        }
     }
     return *this;
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/ismrmrd.git



More information about the debian-science-commits mailing list