libStatGen Software  1
StatGenStatus.cpp
1 /*
2  * Copyright (C) 2010-2011 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "StatGenStatus.h"
19 
20 const char* StatGenStatus::enumStatusString[] = {
21  "SUCCESS",
22  "UNKNOWN",
23  "NO_MORE_RECS",
24  "FAIL_IO",
25  "FAIL_ORDER",
26  "FAIL_PARSE",
27  "INVALID_SORT",
28  "INVALID",
29  "FAIL_MEM"
30 };
31 
32 
34 {
35  return(enumStatusString[statusEnum]);
36 }
37 
38 
39 // Returns whether or not it is "safe" to keep processing the file
40 // after the specified status return.
42 {
43  if(status == StatGenStatus::SUCCESS || status == StatGenStatus::FAIL_PARSE ||
45  {
46  // The status is such that file processing can continue.
47  return(true);
48  }
49  // UNKNOWN, NO_MORE_RECS, FAIL_IO, FAIL_ORDER, FAIL_MEM
50  return(false);
51 }
52 
53 
54 // Constructor
56  : myHandlingType(handleType)
57 {
58  reset();
59 }
60 
61 
62 // Destructor
64 {
65 }
66 
67 
68 // Resets this status.
70 {
71  myType = UNKNOWN;
72  myMessage.clear();
73 }
74 
75 
77 {
78  myHandlingType = handleType;
79 }
80 
81 
82 // Set the status with the specified values.
83 void StatGenStatus::setStatus(Status newStatus, const char* newMessage)
84 {
85  myType = newStatus;
86  myMessage = getStatusString(newStatus);
87  myMessage += ": ";
88  myMessage += newMessage;
89 
90  if(newStatus != SUCCESS)
91  {
92  handleError(newStatus, newMessage);
93  }
94 }
95 
96 
97 // Adds the specified error message to the status message.
98 // Sets the status to newStatus if the current status is SUCCESS.
99 void StatGenStatus::addError(Status newStatus, const char* newMessage)
100 {
101  if(myType == StatGenStatus::SUCCESS)
102  {
103  myType = newStatus;
104  }
105  else
106  {
107  myMessage += "\n";
108  }
109  myMessage += getStatusString(newStatus);
110  myMessage += ": ";
111  myMessage += newMessage;
112 
113  if(newStatus != SUCCESS)
114  {
115  handleError(newStatus, newMessage);
116  }
117 }
118 
119 
120 // Adds the specified status to the status message.
121 // Sets the status to newStatus if the current status is SUCCESS.
123 {
124  if(myType == StatGenStatus::SUCCESS)
125  {
126  myType = newStatus.myType;
127  }
128  else
129  {
130  myMessage += "\n";
131  }
132  myMessage += newStatus.myMessage;
133 
134  if(newStatus != SUCCESS)
135  {
136  handleError(newStatus.myType, newStatus.myMessage.c_str());
137  }
138 }
139 
140 
141 // Return the enum for this status.
143 {
144  return(myType);
145 }
146 
147 
148 // Return the status message.
150 {
151  return(myMessage.c_str());
152 }
153 
154 
155 // Overload operator = to set the sam status type to the
156 // passed in status and to clear the message string.
158 {
159  myType = newStatus;
160  myMessage.clear();
161 
162  if(newStatus != SUCCESS)
163  {
164  handleError(newStatus, "");
165  }
166  return(*this);
167 }
168 
169 
170 // Overload operator = to copy the specified status object to this one.
172 {
173  myType = newStatus.myType;
174  myMessage = newStatus.myMessage;
175  myHandlingType = newStatus.myHandlingType;
176  return(*this);
177 }
178 
179 
180 // Overload operator != to determine if the passed in type is not equal
181 // to this status's type.
183 {
184  return(compStatus != myType);
185 }
186 
187 
188 // Overload operator != to determine if the passed in type is equal
189 // to this status's type.
191 {
192  return(compStatus == myType);
193 }
194 
195 
196 void StatGenStatus::handleError(Status newStatus, const char* newMessage)
197 {
198  // If the status is not success and not NO_MORE_RECS, handle
199  // the error (SUCCESS & NO_MORE_RECS are not real errors.)
200  if((newStatus != SUCCESS) && (newStatus != NO_MORE_RECS))
201  {
202  std::string message = getStatusString(newStatus);
203  message += ": ";
204  message += newMessage;
205 
206  ErrorHandler::handleError(message.c_str(), myHandlingType);
207  }
208 }
static void handleError(const char *message, HandlingType handlingType=EXCEPTION)
Handle an error based on the error handling type.
HandlingType
This specifies how this class should respond to errors.
Definition: ErrorHandler.h:29
This class is used to track the status results of some methods in the BAM classes.
Definition: StatGenStatus.h:27
StatGenStatus & operator=(Status newStatus)
Overload operator = to set the StatGen status type to the passed in status and to clear the message s...
~StatGenStatus()
Destructor.
bool operator!=(const StatGenStatus::Status &compStatus) const
Overload operator != to determine if the passed in type is not equal to this status's type.
bool operator==(const StatGenStatus::Status &compStatus) const
Overload operator == to determine if the passed in type is equal to this status's type.
void reset()
Reset this status to a default state.
StatGenStatus(ErrorHandler::HandlingType handleType=ErrorHandler::EXCEPTION)
Constructor that takes in the handling type, defaulting it to exception.
const char * getStatusMessage() const
Return the status message for this object.
Status
Return value enum for StatGenFile methods.
Definition: StatGenStatus.h:32
@ UNKNOWN
unknown result (default value should never be used)
Definition: StatGenStatus.h:33
@ NO_MORE_RECS
NO_MORE_RECS: failed to read a record since there are no more to read either in the file or section i...
Definition: StatGenStatus.h:36
@ SUCCESS
method completed successfully.
Definition: StatGenStatus.h:32
@ INVALID
invalid other than for sorting.
Definition: StatGenStatus.h:44
@ INVALID_SORT
record is invalid due to it not being sorted.
Definition: StatGenStatus.h:43
@ FAIL_PARSE
failed to parse a record/header - invalid format.
Definition: StatGenStatus.h:42
void setStatus(Status newStatus, const char *newMessage)
Set the status with the specified status enum and message.
static bool isContinuableStatus(StatGenStatus::Status status)
Returns whether or not it is "safe" to keep processing the file after the specified status return.
Status getStatus() const
Return the enum for this status object.
void addError(Status newStatus, const char *newMessage)
Add the specified error message to the status message, setting the status to newStatus if the current...
void setHandlingType(ErrorHandler::HandlingType handleType)
Set how to handle the errors when they are set.
static const char * getStatusString(StatGenStatus::Status statusEnum)
Return a string representation of the passed in status enum.