[ismrmrd] 49/177: Fixed C++ api to only use references to prevent unecessary data copies.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Jan 14 20:02:01 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 2f8fb73ebc5991f7b69e3e83d0edb1c79938a240
Author: Souheil Inati <souheil.inati at nih.gov>
Date:   Wed Sep 10 14:01:44 2014 -0400

    Fixed C++ api to only use references to prevent unecessary data copies.
---
 ismrmrd_dataset.c   | 85 +++++++++++++++++++++--------------------------------
 ismrmrd_dataset.cpp | 54 ++++++++--------------------------
 ismrmrd_dataset.h   | 22 ++++++--------
 3 files changed, 55 insertions(+), 106 deletions(-)

diff --git a/ismrmrd_dataset.c b/ismrmrd_dataset.c
index 6ba1411..13154b6 100644
--- a/ismrmrd_dataset.c
+++ b/ismrmrd_dataset.c
@@ -23,8 +23,7 @@ extern "C" {
 /******************************/
 static bool link_exists(const ISMRMRD_Dataset *dset, const char *link_path) {
 
-    htri_t val;
-    val = H5Lexists(dset->fileid, link_path, H5P_DEFAULT);
+    htri_t val = H5Lexists(dset->fileid, link_path, H5P_DEFAULT);
 
     if (val < 0 ) {
         return false;
@@ -42,12 +41,12 @@ static int create_link(const ISMRMRD_Dataset *dset, const char *link_path) {
         return ISMRMRD_NOERROR;
     }
     else {
-        hid_t lcpl_id;
-        lcpl_id = H5Pcreate(H5P_LINK_CREATE);
+        hid_t lcpl_id = H5Pcreate(H5P_LINK_CREATE);
         H5Pset_create_intermediate_group(lcpl_id, 1);
         hid_t gid = H5Gcreate2(dset->fileid, link_path, lcpl_id, H5P_DEFAULT, H5P_DEFAULT);
         H5Gclose(gid);
-        // TODO does this thing return error
+	H5Pclose(lcpl_id);
+        // TODO can this thing ever return an error?
         return ISMRMRD_NOERROR;
     }
 }
@@ -561,62 +560,46 @@ int ismrmrd_open_dataset(ISMRMRD_Dataset *dset, const bool create_if_needed) {
     // TODO add a mode for clobbering the dataset if it exists.
 
     hid_t       fileid;
-    herr_t      h5status;
 
-    // TODO Opening the dataset when it doesn't already exist causes spew in the error log.
-    /* Turn of HDF5 errors completely*/
-    /* This is bad.  Maybe have it compile time dependent */
-     /* or add to our error log */
-    //H5Eset_auto2(NULL, NULL, NULL);
-    
-    
-    /* Check if the file exists and is an HDF5 File */
-    h5status = H5Fis_hdf5(dset->filename);
-    
-    if (h5status > 0) {
-        /* Positive value for exists and is HDF5 */
-        /* Open it in readwrite mode */
+    /* Try opening the file */
+    /* Note the is_hdf5 function doesn't work well when trying to open multiple files */
+    /* Suppress errors with the try macro. */
+    H5E_BEGIN_TRY {
         fileid = H5Fopen(dset->filename, H5F_ACC_RDWR, H5P_DEFAULT);
-        if (fileid > 0) {
-            dset->fileid = fileid;
-        }
-        else {
-           /* Error opening the existing file */
-           // TODO raise error
-           return ISMRMRD_FILEERROR;
-        }
-    }
-    else if (h5status == 0) {
-       /* Zero value for exists and is NOT HDF5 */
-       //TODO raise error
-       return ISMRMRD_FILEERROR;
+    } H5E_END_TRY
+
+    if (fileid > 0) {
+        dset->fileid = fileid;
     }
     else {
-        /* Negative value for does NOT exist or other error */
+        /* Some sort of error opening the file */
+        /* Maybe it doesn't exist? */
         if (create_if_needed == false) {
-            ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to open file.");
-            return ISMRMRD_FILEERROR;
-        }
-        else {
-            /* Create a new file using the default properties. */
+	    ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to open file.");
+	    return ISMRMRD_FILEERROR;
+	}
+	else {
+	    /* Try creating a new file using the default properties. */
             /* this will be readwrite */
-            fileid = H5Fcreate(dset->filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
-            if (fileid > 0) {
-                dset->fileid = fileid;
-            }
-            else {
-                /* Error creating file */
-                // TODO raise error
-                return ISMRMRD_FILEERROR;
-            }
-        }
+	    H5E_BEGIN_TRY {
+	        fileid = H5Fcreate(dset->filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
+	    } H5E_END_TRY
+	    if (fileid > 0) {
+	        dset->fileid = fileid;
+	    }
+	    else {
+	        /* Error opening the file */
+	        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to open file.");
+		return ISMRMRD_FILEERROR;
+	    }
+	}
     }
 
     /* Open the existing dataset */
     /* insure that /groupname exists */
-    int val = create_link(dset, dset->groupname);
+    create_link(dset, dset->groupname);
 
-    return val;
+    return ISMRMRD_NOERROR;
 }
 
 int ismrmrd_close_dataset(ISMRMRD_Dataset *dset) {
@@ -829,7 +812,7 @@ int ismrmrd_append_image(const ISMRMRD_Dataset *dset, const char *varname,
     /* /groupname/varname */
     char *path = make_path(dset, varname);
     /* Make sure the path exists */
-    status = create_link(dset, path);        
+    create_link(dset, path);        
 
     /* Handle the header */
     char *headerpath = append_to_path(dset, path, "header");
diff --git a/ismrmrd_dataset.cpp b/ismrmrd_dataset.cpp
index b17ed9f..e8797b4 100644
--- a/ismrmrd_dataset.cpp
+++ b/ismrmrd_dataset.cpp
@@ -32,7 +32,7 @@ Dataset::~Dataset()
 }
 
 // XML Header
-int Dataset::writeHeader(const std::string xmlstring)
+int Dataset::writeHeader(const std::string &xmlstring)
 {
     int status = ismrmrd_write_header(&dset_, xmlstring.c_str());
     return status;
@@ -46,24 +46,14 @@ int Dataset::readHeader(std::string& xmlstring){
 }
 
 // Acquisitions
-int Dataset::appendAcquisition(const Acquisition acq)
+int Dataset::appendAcquisition(const Acquisition &acq)
 {
     int status = ismrmrd_append_acquisition(&dset_, reinterpret_cast<const ISMRMRD_Acquisition*>(&acq));
     return status;
 }
 
-Acquisition * Dataset::readAcquisition(uint32_t index)
-{
-    Acquisition * acq = new Acquisition();
-    int status = ismrmrd_read_acquisition(&dset_, index, reinterpret_cast<ISMRMRD_Acquisition*>(acq));
-    if (status != ISMRMRD_NOERROR) {
-      //TODO throw an exception
-    }
-    return acq;
-}
-
-int Dataset::readAcquisition(uint32_t index, Acquisition * acqptr) {
-    int status = ismrmrd_read_acquisition(&dset_, index, reinterpret_cast<ISMRMRD_Acquisition*>(acqptr));
+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
     }
@@ -78,7 +68,7 @@ uint32_t Dataset::getNumberOfAcquisitions()
 }
 
 // Images
-int Dataset::appendImage(const std::string var, const ISMRMRD_BlockModes blockmode, const Image im)
+int Dataset::appendImage(const std::string &var, const ISMRMRD_BlockModes blockmode, const Image &im)
 {
     int status = ismrmrd_append_image(&dset_, var.c_str(), blockmode, reinterpret_cast<const ISMRMRD_Image*>(&im));
     if (status != ISMRMRD_NOERROR) {
@@ -87,25 +77,15 @@ int Dataset::appendImage(const std::string var, const ISMRMRD_BlockModes blockmo
     return status;
 }
 
-Image * Dataset::readImage(const std::string var, uint32_t index)
-{
-    Image * im = new Image();
-    int status = ismrmrd_read_image(&dset_, var.c_str(), index, reinterpret_cast<ISMRMRD_Image*>(im));
-    if (status != ISMRMRD_NOERROR) {
-      //TODO throw an exception
-    }
-    return im;
-}
-
-int Dataset::readImage(const std::string var, uint32_t index, Image * imptr) {
-    int status = ismrmrd_read_image(&dset_, var.c_str(), index, reinterpret_cast<ISMRMRD_Image*>(imptr));
+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
     }
     return status;
 }
 
-uint32_t Dataset::getNumberOfImages(const std::string var)
+uint32_t Dataset::getNumberOfImages(const std::string &var)
 {
     uint32_t num =  ismrmrd_get_number_of_images(&dset_, var.c_str());
     return num;
@@ -113,7 +93,7 @@ uint32_t Dataset::getNumberOfImages(const std::string var)
 
 
 // NDArrays
-int Dataset::appendNDArray(const std::string var, const ISMRMRD_BlockModes blockmode, const NDArray arr)
+int Dataset::appendNDArray(const std::string &var, const ISMRMRD_BlockModes blockmode, const NDArray &arr)
 {
     int status = ismrmrd_append_array(&dset_, var.c_str(), blockmode, reinterpret_cast<const ISMRMRD_NDArray*>(&arr));
     if (status != ISMRMRD_NOERROR) {
@@ -122,25 +102,15 @@ int Dataset::appendNDArray(const std::string var, const ISMRMRD_BlockModes block
     return status;
 }
 
-NDArray * Dataset::readNDArray(const std::string var, uint32_t index)
-{
-    NDArray * arr = new NDArray();
-    int status = ismrmrd_read_array(&dset_, var.c_str(), index, reinterpret_cast<ISMRMRD_NDArray*>(arr));
-    if (status != ISMRMRD_NOERROR) {
-      //TODO throw an exception
-    }
-    return arr;
-}
-
-int Dataset::readNDArray(const std::string var, uint32_t index, NDArray *arrptr) {
-    int status = ismrmrd_read_array(&dset_, var.c_str(), index, reinterpret_cast<ISMRMRD_NDArray*>(arrptr));
+int Dataset::readNDArray(const std::string &var, uint32_t index, NDArray &arr) {
+    int status = ismrmrd_read_array(&dset_, var.c_str(), index, reinterpret_cast<ISMRMRD_NDArray*>(&arr));
     if (status != ISMRMRD_NOERROR) {
       //TODO throw an exception
     }
     return status;
 }
 
-uint32_t Dataset::getNumberOfNDArrays(const std::string var)
+uint32_t Dataset::getNumberOfNDArrays(const std::string &var)
 {
     uint32_t num =  ismrmrd_get_number_of_arrays(&dset_, var.c_str());
     return num;
diff --git a/ismrmrd_dataset.h b/ismrmrd_dataset.h
index ecb2875..07bf2ac 100644
--- a/ismrmrd_dataset.h
+++ b/ismrmrd_dataset.h
@@ -162,24 +162,20 @@ public:
     
     // Methods
     // XML Header
-    int writeHeader(const std::string xmlstring);
+    int writeHeader(const std::string &xmlstring);
     int readHeader(std::string& xmlstring);
-    char * readHeader();
     // Acquisitions
-    int appendAcquisition(const Acquisition acq);
-    Acquisition * readAcquisition(uint32_t index);
-    int readAcquisition(uint32_t index, Acquisition *acq);
+    int appendAcquisition(const Acquisition &acq);
+    int readAcquisition(uint32_t index, Acquisition &acq);
     uint32_t getNumberOfAcquisitions();
     // Images
-    int appendImage(const std::string var, const ISMRMRD_BlockModes blockmode, const Image im);
-    Image * readImage(const std::string var, uint32_t index);
-    int readImage(const std::string var, uint32_t index, Image *imptr);
-    uint32_t getNumberOfImages(const std::string var);
+    int appendImage(const std::string &var, const ISMRMRD_BlockModes blockmode, const Image &im);
+    int readImage(const std::string &var, uint32_t index, Image &im);
+    uint32_t getNumberOfImages(const std::string &var);
     // NDArrays
-    int appendNDArray(const std::string var, const ISMRMRD_BlockModes blockmode, const NDArray arr);
-    NDArray * readNDArray(const std::string var, uint32_t index);
-    int readNDArray(const std::string var, uint32_t index, NDArray *arrptr);
-    uint32_t getNumberOfNDArrays(const std::string var);
+    int appendNDArray(const std::string &var, const ISMRMRD_BlockModes blockmode, const NDArray &arr);
+    int readNDArray(const std::string &var, uint32_t index, NDArray &arr);
+    uint32_t getNumberOfNDArrays(const std::string &var);
 
 protected:
     ISMRMRD_Dataset dset_;

-- 
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