[MOAB-dev] commit/MOAB: danwu: Replaced ERRORR and ERRORS macros with new error handling macros in ReadNC.cpp.

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Mon Mar 10 15:51:24 CDT 2014


1 new commit in MOAB:

https://bitbucket.org/fathomteam/moab/commits/ce442fb064e9/
Changeset:   ce442fb064e9
Branch:      error_handling_enhancement
User:        danwu
Date:        2014-03-10 21:51:07
Summary:     Replaced ERRORR and ERRORS macros with new error handling macros in ReadNC.cpp.

Affected #:  1 file

diff --git a/src/io/ReadNC.cpp b/src/io/ReadNC.cpp
index bc60243..53b4dd7 100644
--- a/src/io/ReadNC.cpp
+++ b/src/io/ReadNC.cpp
@@ -5,12 +5,6 @@
 #include "MBTagConventions.hpp"
 #include "moab/FileOptions.hpp"
 
-#define ERRORR(rval, str) \
-  if (MB_SUCCESS != rval) { readMeshIface->report_error("%s", str); return rval; }
-
-#define ERRORS(err, str) \
-  if (err) { readMeshIface->report_error("%s", str); return MB_FAILURE; }
-
 namespace moab {
 
 ReaderIface* ReadNC::factory(Interface* iface)
@@ -50,16 +44,14 @@ ErrorCode ReadNC::load_file(const char* file_name, const EntityHandle* file_set,
 
   // Get and cache predefined tag handles
   int dum_val = 0;
-  rval = mbImpl->tag_get_handle(GLOBAL_ID_TAG_NAME, 1, MB_TYPE_INTEGER, mGlobalIdTag, MB_TAG_DENSE | MB_TAG_CREAT, &dum_val);
-  if (MB_SUCCESS != rval)
-    return rval;
+  rval = mbImpl->tag_get_handle(GLOBAL_ID_TAG_NAME, 1, MB_TYPE_INTEGER, mGlobalIdTag, MB_TAG_DENSE | MB_TAG_CREAT, &dum_val);CHK_ERR(rval);
 
   // Store the pointer to the tag; if not null, set when global id tag
   // is set too, with the same data, duplicated
   mpFileIdTag = file_id_tag;
 
-  rval = parse_options(opts, var_names, tstep_nums, tstep_vals);
-  ERRORR(rval, "Trouble parsing option string.");
+  rval = parse_options(opts, var_names, tstep_nums, tstep_vals); \
+  CHK_ERR1(rval, "Trouble parsing option string");
 
   // Open the file
   dbgOut.tprintf(1, "Opening file %s\n", file_name);
@@ -75,20 +67,21 @@ ErrorCode ReadNC::load_file(const char* file_name, const EntityHandle* file_set,
   success = NCFUNC(open)(file_name, 0, &fileId);
 #endif
 
-  ERRORS(success, "Trouble opening file.");
+  if (success)
+    SET_ERR(MB_FAILURE, "Trouble opening file");
 
   // Read the header (num dimensions, dimensions, num variables, global attribs)
-  rval = read_header();
-  ERRORR(rval, "Trouble reading file header.");
+  rval = read_header(); \
+  CHK_ERR1(rval, "Trouble reading file header");
 
   // Make sure there's a file set to put things in
   EntityHandle tmp_set;
   if (noMesh && !file_set) {
-    ERRORR(MB_FAILURE, "NOMESH option requires non-NULL file set on input.\n");
+    SET_ERR(MB_FAILURE, "NOMESH option requires non-NULL file set on input");
   }
   else if (!file_set || (file_set && *file_set == 0)) {
-    rval = mbImpl->create_meshset(MESHSET_SET, tmp_set);
-    ERRORR(rval, "Trouble creating file set.");
+    rval = mbImpl->create_meshset(MESHSET_SET, tmp_set); \
+    CHK_ERR1(rval, "Trouble creating file set");
   }
   else
     tmp_set = *file_set;
@@ -105,24 +98,24 @@ ErrorCode ReadNC::load_file(const char* file_name, const EntityHandle* file_set,
   // Get appropriate NC helper instance based on information read from the header
   myHelper = NCHelper::get_nc_helper(this, fileId, opts, tmp_set);
   if (NULL == myHelper) {
-    ERRORR(MB_FAILURE, "Failed to get NCHelper class instance.");
+    SET_ERR(MB_FAILURE, "Failed to get NCHelper class instance");
   }
 
   // Initialize mesh values
-  rval = myHelper->init_mesh_vals();
-  ERRORR(rval, "Trouble initializing mesh values.");
+  rval = myHelper->init_mesh_vals(); \
+  CHK_ERR1(rval, "Trouble initializing mesh values");
 
   // Check existing mesh from last read
   if (noMesh && !noVars) {
-    rval = myHelper->check_existing_mesh();
-    ERRORR(rval, "Trouble checking mesh from last read.\n");
+    rval = myHelper->check_existing_mesh(); \
+    CHK_ERR1(rval, "Trouble checking mesh from last read");
   }
 
   // Create mesh vertex/edge/face sequences
   Range faces;
   if (!noMesh) {
-    rval = myHelper->create_mesh(faces);
-    ERRORR(rval, "Trouble creating mesh.");
+    rval = myHelper->create_mesh(faces); \
+    CHK_ERR1(rval, "Trouble creating mesh");
   }
 
   // Read variables onto grid
@@ -149,34 +142,32 @@ ErrorCode ReadNC::load_file(const char* file_name, const EntityHandle* file_set,
   // Create partition set, and populate with elements
   if (isParallel) {
     EntityHandle partn_set;
-    rval = mbImpl->create_meshset(MESHSET_SET, partn_set);
-    ERRORR(rval, "Trouble creating partition set.");
+    rval = mbImpl->create_meshset(MESHSET_SET, partn_set); \
+    CHK_ERR1(rval, "Trouble creating partition set");
 
-    rval = mbImpl->add_entities(partn_set, faces);
-    ERRORR(rval, "Couldn't add new faces to partition set.");
+    rval = mbImpl->add_entities(partn_set, faces); \
+    CHK_ERR1(rval, "Couldn't add new faces to partition set");
 
     Range verts;
-    rval = mbImpl->get_connectivity(faces, verts);
-    ERRORR(rval, "Couldn't get verts of faces");
+    rval = mbImpl->get_connectivity(faces, verts); \
+    CHK_ERR1(rval, "Couldn't get verts of faces");
 
-    rval = mbImpl->add_entities(partn_set, verts);
-    ERRORR(rval, "Couldn't add new verts to partition set.");
+    rval = mbImpl->add_entities(partn_set, verts); \
+    CHK_ERR1(rval, "Couldn't add new verts to partition set");
 
     myPcomm->partition_sets().insert(partn_set);
 
     // Write partition tag name on partition set
     Tag part_tag = myPcomm->partition_tag();
     int dum_rank = myPcomm->proc_config().proc_rank();
-    rval = mbImpl->tag_set_data(part_tag, &partn_set, 1, &dum_rank);
-    if (MB_SUCCESS != rval)
-      return rval;
+    rval = mbImpl->tag_set_data(part_tag, &partn_set, 1, &dum_rank);CHK_ERR(rval);
   }
 #endif
 
   // Create NC conventional tags when loading header info only
   if (noMesh && noVars) {
-    rval = myHelper->create_conventional_tags(tstep_nums);
-    ERRORR(rval, "Trouble creating NC conventional tags.");
+    rval = myHelper->create_conventional_tags(tstep_nums); \
+    CHK_ERR1(rval, "Trouble creating NC conventional tags");
   }
 
   mbImpl->release_interface(scdi);
@@ -184,7 +175,8 @@ ErrorCode ReadNC::load_file(const char* file_name, const EntityHandle* file_set,
 
   // Close the file
   success = NCFUNC(close)(fileId);
-  ERRORS(success, "Trouble closing file.");
+  if (success)
+    SET_ERR(MB_FAILURE, "Trouble closing file");
 
   return MB_SUCCESS;
 }
@@ -244,12 +236,11 @@ ErrorCode ReadNC::parse_options(const FileOptions& opts, std::vector<std::string
 
   rval = opts.get_int_option("GATHER_SET", 0, gatherSetRank);
   if (MB_TYPE_OUT_OF_RANGE == rval) {
-    readMeshIface->report_error("Invalid value for GATHER_SET option");
     SET_ERR(rval, "Invalid value for GATHER_SET option");
   }
 
 #ifdef USE_MPI
-  isParallel = (opts.match_option("PARALLEL","READ_PART") != MB_ENTITY_NOT_FOUND);
+  isParallel = (opts.match_option("PARALLEL", "READ_PART") != MB_ENTITY_NOT_FOUND);
 
   if (!isParallel)
   // Return success here, since rval still has _NOT_FOUND from not finding option
@@ -259,8 +250,7 @@ ErrorCode ReadNC::parse_options(const FileOptions& opts, std::vector<std::string
 
   int pcomm_no = 0;
   rval = opts.get_int_option("PARALLEL_COMM", pcomm_no);
-  if (rval == MB_TYPE_OUT_OF_RANGE) {
-    readMeshIface->report_error("Invalid value for PARALLEL_COMM option");
+  if (MB_TYPE_OUT_OF_RANGE == rval) {
     SET_ERR(rval, "Invalid value for PARALLEL_COMM option");
   }
   myPcomm = ParallelComm::get_pcomm(mbImpl, pcomm_no);
@@ -272,11 +262,10 @@ ErrorCode ReadNC::parse_options(const FileOptions& opts, std::vector<std::string
 
   int dum;
   rval = opts.match_option("PARTITION_METHOD", ScdParData::PartitionMethodNames, dum);
-  if (rval == MB_FAILURE) {
-    readMeshIface->report_error("Unknown partition method specified.");
+  if (MB_FAILURE == rval) {
     SET_ERR(rval, "Unknown partition method specified");
   }
-  else if (rval == MB_ENTITY_NOT_FOUND)
+  else if (MB_ENTITY_NOT_FOUND == rval)
     partMethod = ScdParData::ALLJORKORI;
   else
     partMethod = dum;
@@ -293,21 +282,22 @@ ErrorCode ReadNC::read_header()
   int numgatts;
   int success;
   success = NCFUNC(inq_natts )(fileId, &numgatts);
-  ERRORS(success, "Couldn't get number of global attributes.");
+  if (success)
+    SET_ERR(MB_FAILURE, "Couldn't get number of global attributes");
 
   // Read attributes into globalAtts
-  ErrorCode result = get_attributes(NC_GLOBAL, numgatts, globalAtts);
-  ERRORR(result, "Getting attributes.");
+  ErrorCode result = get_attributes(NC_GLOBAL, numgatts, globalAtts); \
+  CHK_ERR1(result, "Trouble getting attributes");
   dbgOut.tprintf(1, "Read %u attributes\n", (unsigned int) globalAtts.size());
 
   // Read in dimensions into dimNames and dimLens
-  result = get_dimensions(fileId, dimNames, dimLens);
-  ERRORR(result, "Getting dimensions.");
+  result = get_dimensions(fileId, dimNames, dimLens); \
+  CHK_ERR1(result, "Trouble getting dimensions");
   dbgOut.tprintf(1, "Read %u dimensions\n", (unsigned int) dimNames.size());
 
   // Read in variables into varInfo
-  result = get_variables();
-  ERRORR(result, "Getting variables.");
+  result = get_variables(); \
+  CHK_ERR1(result, "Trouble getting variables");
   dbgOut.tprintf(1, "Read %u variables\n", (unsigned int) varInfo.size());
 
   return MB_SUCCESS;
@@ -320,12 +310,14 @@ ErrorCode ReadNC::get_attributes(int var_id, int num_atts, std::map<std::string,
   for (int i = 0; i < num_atts; i++) {
     // Get the name
     int success = NCFUNC(inq_attname)(fileId, var_id, i, dum_name);
-    ERRORS(success, "Trouble getting attribute name.");
+    if (success)
+      SET_ERR(MB_FAILURE, "Trouble getting attribute name");
 
     AttData &data = atts[std::string(dum_name)];
     data.attName = std::string(dum_name);
     success = NCFUNC(inq_att)(fileId, var_id, dum_name, &data.attDataType, &data.attLen);
-    ERRORS(success, "Trouble getting attribute info.");
+    if (success)
+      SET_ERR(MB_FAILURE, "Trouble getting attribute info");
     data.attVarId = var_id;
 
     dbgOut.tprintf(2, "%sAttribute %s: length=%u, varId=%d, type=%d\n", (prefix ? prefix : ""), data.attName.c_str(),
@@ -340,11 +332,11 @@ ErrorCode ReadNC::get_dimensions(int file_id, std::vector<std::string>& dim_name
   // Get the number of dimensions
   int num_dims;
   int success = NCFUNC(inq_ndims)(file_id, &num_dims);
-  ERRORS(success, "Trouble getting number of dimensions.");
+  if (success)
+    SET_ERR(MB_FAILURE, "Trouble getting number of dimensions");
 
   if (num_dims > NC_MAX_DIMS) {
-    readMeshIface->report_error("ReadNC: File contains %d dims but NetCDF library supports only %d\n", num_dims, (int) NC_MAX_DIMS);
-    return MB_FAILURE;
+    SET_ERR_STR(MB_FAILURE, "ReadNC: File contains " << num_dims << " dims but NetCDF library supports only " << NC_MAX_DIMS);
   }
 
   char dim_name[NC_MAX_NAME + 1];
@@ -354,7 +346,8 @@ ErrorCode ReadNC::get_dimensions(int file_id, std::vector<std::string>& dim_name
 
   for (int i = 0; i < num_dims; i++) {
     success = NCFUNC(inq_dim)(file_id, i, dim_name, &dim_len);
-    ERRORS(success, "Trouble getting dimension info.");
+    if (success)
+      SET_ERR(MB_FAILURE, "Trouble getting dimension info");
 
     dim_names[i] = std::string(dim_name);
     dim_lens[i] = dim_len;
@@ -381,11 +374,11 @@ ErrorCode ReadNC::get_variables()
   // Get the number of variables
   int num_vars;
   int success = NCFUNC(inq_nvars)(fileId, &num_vars);
-  ERRORS(success, "Trouble getting number of variables.");
+  if (success)
+    SET_ERR(MB_FAILURE, "Trouble getting number of variables");
 
   if (num_vars > NC_MAX_VARS) {
-    readMeshIface->report_error("ReadNC: File contains %d vars but NetCDF library supports only %d\n", num_vars, (int) NC_MAX_VARS);
-    return MB_FAILURE;
+    SET_ERR_STR(MB_FAILURE, "ReadNC: File contains " << num_vars << " vars but NetCDF library supports only " << NC_MAX_VARS);
   }
 
   char var_name[NC_MAX_NAME + 1];
@@ -394,7 +387,8 @@ ErrorCode ReadNC::get_variables()
   for (int i = 0; i < num_vars; i++) {
     // Get the name first, so we can allocate a map iterate for this var
     success = NCFUNC(inq_varname )(fileId, i, var_name);
-    ERRORS(success, "Trouble getting var name.");
+    if (success)
+      SET_ERR(MB_FAILURE, "Trouble getting var name");
     VarData &data = varInfo[std::string(var_name)];
     data.varName = std::string(var_name);
     data.varId = i;
@@ -402,26 +396,30 @@ ErrorCode ReadNC::get_variables()
 
     // Get the data type
     success = NCFUNC(inq_vartype)(fileId, i, &data.varDataType);
-    ERRORS(success, "Trouble getting variable data type.");
+    if (success)
+      SET_ERR(MB_FAILURE, "Trouble getting variable data type");
 
     // Get the number of dimensions, then the dimensions
     success = NCFUNC(inq_varndims)(fileId, i, &var_ndims);
-    ERRORS(success, "Trouble getting number of dims of a variable.");
+    if (success)
+      SET_ERR(MB_FAILURE, "Trouble getting number of dims of a variable");
     data.varDims.resize(var_ndims);
 
     success = NCFUNC(inq_vardimid)(fileId, i, &data.varDims[0]);
-    ERRORS(success, "Trouble getting variable dimensions.");
+    if (success)
+      SET_ERR(MB_FAILURE, "Trouble getting variable dimensions");
 
     // Finally, get the number of attributes, then the attributes
     success = NCFUNC(inq_varnatts)(fileId, i, &data.numAtts);
-    ERRORS(success, "Trouble getting number of dims of a variable.");
+    if (success)
+      SET_ERR(MB_FAILURE, "Trouble getting number of dims of a variable");
 
     // Print debug info here so attribute info comes afterwards
     dbgOut.tprintf(2, "Variable %s: Id=%d, numAtts=%d, datatype=%d, num_dims=%u\n", data.varName.c_str(), data.varId, data.numAtts,
         data.varDataType, (unsigned int) data.varDims.size());
 
-    ErrorCode rval = get_attributes(i, data.numAtts, data.varAtts, "   ");
-    ERRORR(rval, "Trouble getting attributes for a variable.");
+    ErrorCode rval = get_attributes(i, data.numAtts, data.varAtts, "   "); \
+    CHK_ERR1(rval, "Trouble getting attributes for a variable");
   }
 
   return MB_SUCCESS;

Repository URL: https://bitbucket.org/fathomteam/moab/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the moab-dev mailing list