Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
ErrorCode.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "ErrorCode.h"
14 
15 #include "CoreMacros.h"
16 
17 #include <string>
18 #include <vector>
19 
20 std::vector<std::pair<std::string, unsigned int>>* pErrorList;
21 
22 // get error code
23 unsigned int errorcode( const char* errorName ) {
24  // static variable
25  static unsigned int maxErrorCode = 0;
26 
27  // create string object
28  std::string s = std::string( NVL( errorName, "" ) );
29 
30  // get error code
31  unsigned code = maxErrorCode;
32 
33  unsigned errorIndex = -1;
34 
35  if (pErrorList == NULL)
36  {
37  pErrorList = new std::vector<std::pair<std::string, unsigned int>>;
38  }
39 
40  for (int i = 0; (i < static_cast<int>(pErrorList->size())) && (errorIndex == -1); i++)
41  {
42  if ((*pErrorList)[i].first.compare(errorName) == 0)
43  {
44  errorIndex = i;
45  }
46  }
47 
48  if( errorIndex == -1 ) { // new error code
49  pErrorList->push_back(std::pair<std::string, unsigned int>(errorName, code));
50  maxErrorCode++;
51  }
52  else { // already exists
53  code = (*pErrorList)[errorIndex].second;
54  }
55 
56  return code;
57 }
58 
59 // get error string for an existing error
60 const char* errorstring( unsigned int errorCode ) {
61 
62  unsigned errorIndex = -1;
63 
64  // get error string
65  for (int i = 0; (i < static_cast<int>(pErrorList->size())) && (errorIndex == -1); i++)
66  {
67  if ((*pErrorList)[i].second == errorCode)
68  {
69  errorIndex = i;
70  }
71  }
72 
73  if (errorIndex != -1)
74  {
75  return (*pErrorList)[errorIndex].first.c_str();
76  }
77  else
78  {
79  return "";
80  }
81 }
82 
83 // deallocate error strings object
84 void errorcleanup( void )
85 {
86 // >>>>>> @Date:2013/10/18 <Modified> A.Ozaki
87  if ( pErrorList )
88  {
89  pErrorList->clear();
90  delete pErrorList;
91  }
92 // <<<<<< @Date:2013/10/18 <Modified> A.Ozaki
93  pErrorList = NULL;
94 }
basic macro collection
unsigned int errorcode(const char *errorName)
get new error code
Definition: ErrorCode.cpp:23
common error code definition
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
#define NULL
Definition: CoreMacros.h:18
const char * errorstring(unsigned int errorCode)
get error string for an existing error
Definition: ErrorCode.cpp:60
void errorcleanup(void)
deallocate error strings object
Definition: ErrorCode.cpp:84