-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorState.cpp
116 lines (82 loc) · 2.07 KB
/
ErrorState.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
***********************************
Student Name:<Badal Sarkar>
Student#: <137226189>
Student e-mail: [email protected]
Subject: OOP244
Section: <SAA>
Topic: Assignment MS 5
***********************************
*/
#include<iostream>
#include<cstring>
#include"ErrorState.h"
using namespace std;
#define _CRT_SECURE_NO_WARNINGS
namespace ama {
//default constructor
ErrorState::ErrorState(const char* errorMessage){
init(errorMessage);
}
//destructor
ErrorState::~ErrorState() {
delete[]errorStateMsg;
errorStateMsg = nullptr;
}
//bool operator
//returns true if there is error
ErrorState::operator bool() const{
return(errorStateMsg != nullptr ? true : false);
}
//operator=
//this function sotres the text in the parameter
//into the data member
ErrorState & ErrorState::operator=(const char * pText){
delete[] errorStateMsg;
init(pText);
return *this;
}
//message function
void ErrorState::message(const char* pText) {
delete[] errorStateMsg;
init(pText);
}
//function to query stored message
const char* ErrorState::message() const {
return((errorStateMsg) ? errorStateMsg : nullptr);
}
//function to validate message
bool ErrorState::dataIsValid(const char* message)const{
return ((message != nullptr&&message[0] != '\0') ? true : false);
}
//function init
//this function provides the copy functionality
void ErrorState::init(const char* message) {
if (dataIsValid(message)) {
//allocate dynamic memory
int totalElement = strlen(message);
errorStateMsg = new (nothrow) char[totalElement + 1];
//if memory allocation is successful
//store message into member
if (errorStateMsg != nullptr) {
strncpy(errorStateMsg, message, totalElement);
errorStateMsg[totalElement] = '\0';
}
//if memory not allocated
else {
errorStateMsg = nullptr;
}
}
//when data is not valid
else {
errorStateMsg = nullptr;
}
}
}
//function operator<<
std::ostream& operator<<(std::ostream& os, const ama::ErrorState& src){
if (src.message() != nullptr) {
os << src.message();
}
return os;
}