-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathCalculatorVector.h
155 lines (138 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include <string>
#include <vector>
#include "winerror_cross_platform.h"
#include "Ratpack/CalcErr.h"
#include <stdexcept> // for std::out_of_range
#include "sal_cross_platform.h" // for SAL
template <typename TType>
class CalculatorVector
{
public:
ResultCode GetAt(_In_opt_ unsigned int index, _Out_ TType* item)
{
try
{
*item = m_vector.at(index);
}
catch (const std::out_of_range& /*ex*/)
{
return E_BOUNDS;
}
return S_OK;
}
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)
{
try
{
m_vector[index] = item;
}
catch (const std::out_of_range& /*ex*/)
{
return E_BOUNDS;
}
return S_OK;
}
ResultCode RemoveAt(_In_ unsigned int index)
{
if (index < m_vector.size())
{
m_vector.erase(m_vector.begin() + index);
}
else
{
return E_BOUNDS;
}
return S_OK;
}
ResultCode InsertAt(_In_ unsigned int index, _In_ TType item)
{
try
{
auto iter = m_vector.begin() + index;
m_vector.insert(iter, item);
}
catch (const std::bad_alloc& /*ex*/)
{
return E_OUTOFMEMORY;
}
return S_OK;
}
ResultCode Truncate(_In_ unsigned int index)
{
if (index < m_vector.size())
{
auto startIter = m_vector.begin() + index;
m_vector.erase(startIter, m_vector.end());
}
else
{
return E_BOUNDS;
}
return S_OK;
}
ResultCode Append(_In_opt_ TType item)
{
try
{
m_vector.push_back(item);
}
catch (const std::bad_alloc& /*ex*/)
{
return E_OUTOFMEMORY;
}
return S_OK;
}
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)
{
unsigned int nTokens = 0;
ResultCode hr = this->GetSize(&nTokens);
if (SUCCEEDED(hr))
{
std::pair<std::wstring, int> currentPair;
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;
};