Skip to content

Commit 1c9cfb5

Browse files
committed
Templates
1 parent 2d06e9e commit 1c9cfb5

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

07_SimpleCppApp/FunctionPreCacher.hpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace SimpleApp
99
{
10+
template<typename T>
1011
class FunctionPreCacher
1112
{
1213
public:
@@ -19,10 +20,13 @@ namespace SimpleApp
1920

2021
if (other.values)
2122
{
22-
values = new int32_t[other.count];
23+
values = new T[other.count];
2324
if (values)
2425
{
25-
std::memcpy(values, other.values, sizeof(int32_t) * other.count);
26+
for (uint32_t i = 0; i < other.count; i++)
27+
{
28+
values[i] = other.values[i];
29+
}
2630
}
2731
else
2832
{
@@ -39,7 +43,7 @@ namespace SimpleApp
3943

4044
other.values = nullptr;
4145
}
42-
FunctionPreCacher(int32_t x, uint32_t count, int32_t(*function)(int32_t))
46+
FunctionPreCacher(int32_t x, uint32_t count, T(*function)(int32_t))
4347
{
4448
Setup(x, count, function);
4549
}
@@ -69,7 +73,7 @@ namespace SimpleApp
6973
return *this;
7074
}
7175

72-
void Setup(int32_t x, uint32_t count, int32_t(*function)(int32_t))
76+
void Setup(int32_t x, uint32_t count, T(*function)(int32_t))
7377
{
7478
Release();
7579
this->x = x;
@@ -81,7 +85,7 @@ namespace SimpleApp
8185
if (function && count > 0)
8286
{
8387
Release();
84-
values = new int32_t[count];
88+
values = new T[count];
8589
if (values)
8690
{
8791
for (uint32_t i = 1; i <= count; i++)
@@ -128,12 +132,12 @@ namespace SimpleApp
128132
{
129133
return Compute();
130134
}
131-
FunctionPreCacher& operator()(int32_t x, uint32_t count, int32_t(*function)(int32_t))
135+
FunctionPreCacher& operator()(int32_t x, uint32_t count, T(*function)(int32_t))
132136
{
133137
Setup(x, count, function);
134138
return Compute();
135139
}
136-
int32_t operator[](uint32_t index) const
140+
const T& operator[](uint32_t index) const
137141
{
138142
return At(index);
139143
}
@@ -143,14 +147,15 @@ namespace SimpleApp
143147
}
144148

145149
private:
146-
int32_t* values = nullptr;
150+
T* values = nullptr;
147151
int32_t x = 0;
148152
uint32_t count = 0;
149-
int32_t(*function)(int32_t x) = nullptr;
153+
T(*function)(int32_t x) = nullptr;
150154
};
151155
}
152156

153-
inline std::ostream& operator<<(std::ostream& os, const SimpleApp::FunctionPreCacher& pc)
157+
template<typename T>
158+
inline std::ostream& operator<<(std::ostream& os, const SimpleApp::FunctionPreCacher<T>& pc)
154159
{
155160
pc.Print(os);
156161
return os;

07_SimpleCppApp/PreCacherContainer.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace SimpleApp
99
{
10+
template<typename T, typename Pc = FunctionPreCacher<T>>
1011
class PreCacherContainer
1112
{
1213
public:
@@ -27,25 +28,25 @@ namespace SimpleApp
2728
os << m_preCachers[i];
2829
}
2930
}
30-
void Append(const FunctionPreCacher& pc)
31+
void Append(const Pc& pc)
3132
{
3233
if (m_usage >= 8)
3334
throw std::overflow_error("PreCacherContainer Container overflown");
3435
m_preCachers[m_usage++] = pc;
3536
}
36-
void Append(FunctionPreCacher&& pc)
37+
void Append(Pc&& pc)
3738
{
3839
if (m_usage >= 8)
3940
throw std::overflow_error("PreCacherContainer Container overflown");
4041
m_preCachers[m_usage++] = std::move(pc);
4142
}
42-
const FunctionPreCacher& At(int index) const
43+
const Pc& At(int index) const
4344
{
4445
if (index >= 8 || index < 0)
4546
throw std::range_error("PreCacherContainer index out of range");
4647
return m_preCachers[index];
4748
}
48-
FunctionPreCacher& At(int index)
49+
Pc& At(int index)
4950
{
5051
if (index >= 8 || index < 0)
5152
throw std::range_error("PreCacherContainer index out of range");
@@ -56,32 +57,33 @@ namespace SimpleApp
5657
return m_usage;
5758
}
5859

59-
PreCacherContainer& operator<<(const FunctionPreCacher& pc)
60+
PreCacherContainer& operator<<(const Pc& pc)
6061
{
6162
Append(pc);
6263
return *this;
6364
}
64-
PreCacherContainer& operator<<(FunctionPreCacher&& pc)
65+
PreCacherContainer& operator<<(Pc&& pc)
6566
{
6667
Append(std::move(pc));
6768
return *this;
6869
}
69-
const FunctionPreCacher& operator[](int index) const
70+
const Pc& operator[](int index) const
7071
{
7172
return At(index);
7273
}
73-
FunctionPreCacher& operator[](int index)
74+
Pc& operator[](int index)
7475
{
7576
return At(index);
7677
}
7778

7879
private:
79-
FunctionPreCacher m_preCachers[8];
80+
Pc m_preCachers[8];
8081
int m_usage = 0;
8182
};
8283
}
8384

84-
inline std::ostream& operator<<(std::ostream& os, const SimpleApp::PreCacherContainer& pc)
85+
template<typename T>
86+
inline std::ostream& operator<<(std::ostream& os, const SimpleApp::PreCacherContainer<T>& pc)
8587
{
8688
pc.Print(os);
8789
return os;

07_SimpleCppApp/SimpleApp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int SimpleApp::SaveMain()
99
std::cout << "Enter the itterations: ";
1010
std::cin >> count;
1111

12-
PreCacherContainer cnt;
12+
PreCacherContainer<Vector2> cnt;
1313
cnt << std::move(FunctionPreCacher(x, count, &f)())
1414
<< std::move(FunctionPreCacher(x * 2, count, &f)())
1515
<< std::move(FunctionPreCacher(x, count * 2, &f)())

07_SimpleCppApp/SimpleApp.hpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,34 @@
88

99
namespace SimpleApp
1010
{
11-
inline int32_t f(int32_t x)
11+
struct Vector2
1212
{
13-
return x * x + 5 * x + 5;
13+
float x = 0.f, y = 0.f;
14+
15+
Vector2 operator+(const Vector2& rhs) const
16+
{
17+
return Vector2{ x + rhs.x, y + rhs.y };
18+
}
19+
Vector2 operator-(const Vector2& rhs) const
20+
{
21+
return Vector2{ x - rhs.x, y - rhs.y };
22+
}
23+
Vector2 operator*(const Vector2& rhs) const
24+
{
25+
return Vector2{ x * rhs.x, y * rhs.y };
26+
}
27+
};
28+
29+
inline Vector2 f(int32_t x)
30+
{
31+
return Vector2{ (float)x, (float)x } * Vector2{ 1.0f, 2.0f };
1432
}
1533

1634
int SaveMain();
1735
}
36+
37+
inline std::ostream& operator<<(std::ostream& os, const SimpleApp::Vector2& vec)
38+
{
39+
os << "(" << vec.x << ", " << vec.y << ")";
40+
return os;
41+
}

0 commit comments

Comments
 (0)