forked from anandprabhakar0507/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatorVector.h
157 lines (140 loc) · 3.36 KB
/
CalculatorVector.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "Ratpack/CalcErr.h"
template <typename TType>
class CalculatorVector
{
public:
ResultCode GetAt(_In_opt_ unsigned int index, _Out_ TType *item)
{
ResultCode hr = S_OK;
try
{
*item = m_vector.at(index);
}
catch (const std::out_of_range& /*ex*/)
{
hr = E_BOUNDS;
}
return hr;
}
ResultCode GetSize(_Out_ unsigned int *size)
{
*size = static_cast<unsigned>(m_vector.size());
return S_OK;
}
ResultCode SetAt(_In_ unsigned int index, _In_opt_ TType item)
{
ResultCode hr = S_OK;
try
{
m_vector[index] = item;
}
catch (const std::out_of_range& /*ex*/)
{
hr = E_BOUNDS;
}
return hr;
}
ResultCode RemoveAt(_In_ unsigned int index)
{
ResultCode hr = S_OK;
if (index < m_vector.size())
{
m_vector.erase(m_vector.begin() + index);
}
else
{
hr = E_BOUNDS;
}
return hr;
}
ResultCode InsertAt(_In_ unsigned int index, _In_ TType item)
{
ResultCode hr = S_OK;
try
{
auto iter = m_vector.begin() + index;
m_vector.insert(iter, item);
}
catch (const std::bad_alloc& /*ex*/)
{
hr = E_OUTOFMEMORY;
}
return hr;
}
ResultCode Truncate(_In_ unsigned int index)
{
ResultCode hr = S_OK;
if (index < m_vector.size())
{
auto startIter = m_vector.begin() + index;
m_vector.erase(startIter, m_vector.end());
}
else
{
hr = E_BOUNDS;
}
return hr;
}
ResultCode Append(_In_opt_ TType item)
{
ResultCode hr = S_OK;
try
{
m_vector.push_back(item);
}
catch (const std::bad_alloc& /*ex*/)
{
hr = E_OUTOFMEMORY;
}
return hr;
}
ResultCode RemoveAtEnd()
{
m_vector.erase(--(m_vector.end()));
return S_OK;
}
ResultCode Clear()
{
m_vector.clear();
return S_OK;
}
ResultCode GetString(_Out_ std::wstring* expression)
{
ResultCode hr = S_OK;
unsigned int nTokens = 0;
std::pair <std::wstring, int> currentPair;
hr = this->GetSize(&nTokens);
if (SUCCEEDED(hr))
{
for (unsigned int i = 0; i < nTokens; i++)
{
hr = this->GetAt(i, ¤tPair);
if (SUCCEEDED(hr))
{
expression->append(currentPair.first);
if (i != (nTokens - 1))
{
expression->append(L" ");
}
}
}
std::wstring expressionSuffix{};
hr = GetExpressionSuffix(&expressionSuffix);
if (SUCCEEDED(hr))
{
expression->append(expressionSuffix);
}
}
return hr;
}
ResultCode GetExpressionSuffix(_Out_ std::wstring* suffix)
{
*suffix = L" =";
return S_OK;
}
private:
std::vector<TType> m_vector;
};