Skip to content

Commit 039f516

Browse files
committed
Operator overloading
1 parent 1c937a7 commit 039f516

File tree

1 file changed

+76
-17
lines changed

1 file changed

+76
-17
lines changed

07_SimpleCppApp/main.cpp

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class FunctionPreCacher
5454

5555
return *this;
5656
}
57-
5857
FunctionPreCacher& operator=(FunctionPreCacher&& other) noexcept
5958
{
6059
if (this != &other)
@@ -73,7 +72,6 @@ class FunctionPreCacher
7372
this->count = count;
7473
this->function = function;
7574
}
76-
7775
FunctionPreCacher& Compute()
7876
{
7977
if (function && count > 0)
@@ -91,7 +89,6 @@ class FunctionPreCacher
9189

9290
return *this;
9391
}
94-
9592
void Release()
9693
{
9794
if (values)
@@ -100,15 +97,41 @@ class FunctionPreCacher
10097
values = nullptr;
10198
}
10299
}
103-
104-
void Print() const
100+
void Print(std::ostream& os = std::cout) const
105101
{
106102
if (values)
107103
{
108104
for (uint32_t i = 1; i <= count; i++)
109-
std::cout << "f(" << i * x << ") = " << values[i - 1] << std::endl;
105+
os << "f(" << i * x << ") = " << values[i - 1] << std::endl;
110106
}
111107
}
108+
int32_t At(uint32_t index) const
109+
{
110+
// TODO: Check range
111+
return values[index];
112+
}
113+
uint32_t Size() const
114+
{
115+
return count;
116+
}
117+
118+
FunctionPreCacher& operator()()
119+
{
120+
return Compute();
121+
}
122+
FunctionPreCacher& operator()(int32_t x, uint32_t count, int32_t(*function)(int32_t))
123+
{
124+
Setup(x, count, function);
125+
return Compute();
126+
}
127+
int32_t operator[](uint32_t index) const
128+
{
129+
return At(index);
130+
}
131+
operator bool()
132+
{
133+
return values != nullptr;
134+
}
112135

113136
private:
114137
int32_t* values = nullptr;
@@ -128,37 +151,72 @@ class PreCacherContainer
128151
PreCacherContainer& operator=(const PreCacherContainer&) = default;
129152
PreCacherContainer& operator=(PreCacherContainer&&) noexcept = default;
130153

131-
void Print() const
154+
void Print(std::ostream& os = std::cout) const
132155
{
133-
std::cout << "Container at " << this << std::endl;
156+
os << "Container at " << this << std::endl;
134157
for (int i = 0; i < m_usage; i++)
135158
{
136-
std::cout << "FunctionPreCacher #" << (i + 1) << std::endl;
137-
m_preCachers[i].Print();
159+
os << "FunctionPreCacher #" << (i + 1) << std::endl;
160+
m_preCachers[i].Print(os);
138161
}
139162
}
140-
141163
void Append(const FunctionPreCacher& pc)
142164
{
143165
if (m_usage < 8)
144166
{
145167
m_preCachers[m_usage++] = pc;
146168
}
147169
}
148-
149170
void Append(FunctionPreCacher&& pc)
150171
{
151172
if (m_usage < 8)
152173
{
153174
m_preCachers[m_usage++] = std::move(pc);
154175
}
155176
}
177+
const FunctionPreCacher& At(int index) const
178+
{
179+
return m_preCachers[index];
180+
}
181+
FunctionPreCacher& At(int index)
182+
{
183+
return m_preCachers[index];
184+
}
185+
int Size() const
186+
{
187+
return m_usage;
188+
}
189+
190+
PreCacherContainer& operator<<(const FunctionPreCacher& pc)
191+
{
192+
Append(pc);
193+
return *this;
194+
}
195+
PreCacherContainer& operator<<(FunctionPreCacher&& pc)
196+
{
197+
Append(std::move(pc));
198+
return *this;
199+
}
200+
const FunctionPreCacher& operator[](int index) const
201+
{
202+
return At(index);
203+
}
204+
FunctionPreCacher& operator[](int index)
205+
{
206+
return At(index);
207+
}
156208

157209
private:
158210
FunctionPreCacher m_preCachers[8];
159211
int m_usage = 0;
160212
};
161213

214+
std::ostream& operator<<(std::ostream& os, const PreCacherContainer& pc)
215+
{
216+
pc.Print(os);
217+
return os;
218+
}
219+
162220
int main()
163221
{
164222
int32_t x;
@@ -169,9 +227,10 @@ int main()
169227
std::cin >> count;
170228

171229
PreCacherContainer cnt;
172-
cnt.Append(std::move(FunctionPreCacher(x, count, &f).Compute()));
173-
cnt.Append(std::move(FunctionPreCacher(x * 2, count, &f).Compute()));
174-
cnt.Append(std::move(FunctionPreCacher(x, count * 2, &f).Compute()));
175-
cnt.Append(std::move(FunctionPreCacher(x * 2, count * 2, &f).Compute()));
176-
cnt.Print();
230+
cnt << std::move(FunctionPreCacher(x, count, &f)())
231+
<< std::move(FunctionPreCacher(x * 2, count, &f)())
232+
<< std::move(FunctionPreCacher(x, count * 2, &f)())
233+
<< std::move(FunctionPreCacher(x * 2, count * 2, &f)());
234+
235+
std::cout << cnt;
177236
}

0 commit comments

Comments
 (0)