Skip to content

Commit 2d06e9e

Browse files
committed
Heade and source files
1 parent 9c30f75 commit 2d06e9e

File tree

7 files changed

+306
-254
lines changed

7 files changed

+306
-254
lines changed

07_SimpleCppApp/07_SimpleCppApp.vcxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@
152152
</ItemDefinitionGroup>
153153
<ItemGroup>
154154
<ClCompile Include="main.cpp" />
155+
<ClCompile Include="SimpleApp.cpp" />
156+
</ItemGroup>
157+
<ItemGroup>
158+
<ClInclude Include="FunctionPreCacher.hpp" />
159+
<ClInclude Include="PreCacherContainer.hpp" />
160+
<ClInclude Include="SimpleApp.hpp" />
155161
</ItemGroup>
156162
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
157163
<ImportGroup Label="ExtensionTargets">

07_SimpleCppApp/07_SimpleCppApp.vcxproj.filters

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,19 @@
1818
<ClCompile Include="main.cpp">
1919
<Filter>Source Files</Filter>
2020
</ClCompile>
21+
<ClCompile Include="SimpleApp.cpp">
22+
<Filter>Source Files</Filter>
23+
</ClCompile>
24+
</ItemGroup>
25+
<ItemGroup>
26+
<ClInclude Include="FunctionPreCacher.hpp">
27+
<Filter>Header Files</Filter>
28+
</ClInclude>
29+
<ClInclude Include="PreCacherContainer.hpp">
30+
<Filter>Header Files</Filter>
31+
</ClInclude>
32+
<ClInclude Include="SimpleApp.hpp">
33+
<Filter>Header Files</Filter>
34+
</ClInclude>
2135
</ItemGroup>
2236
</Project>
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#pragma once
2+
3+
#include <stdexcept>
4+
#include <iostream>
5+
#include <cstdint>
6+
#include <cstring>
7+
8+
namespace SimpleApp
9+
{
10+
class FunctionPreCacher
11+
{
12+
public:
13+
FunctionPreCacher() = default;
14+
FunctionPreCacher(const FunctionPreCacher& other)
15+
{
16+
x = other.x;
17+
count = other.count;
18+
function = other.function;
19+
20+
if (other.values)
21+
{
22+
values = new int32_t[other.count];
23+
if (values)
24+
{
25+
std::memcpy(values, other.values, sizeof(int32_t) * other.count);
26+
}
27+
else
28+
{
29+
throw std::runtime_error("FunctionPreCacher new failed!");
30+
}
31+
}
32+
}
33+
FunctionPreCacher(FunctionPreCacher&& other) noexcept
34+
{
35+
x = other.x;
36+
count = other.count;
37+
function = other.function;
38+
values = other.values;
39+
40+
other.values = nullptr;
41+
}
42+
FunctionPreCacher(int32_t x, uint32_t count, int32_t(*function)(int32_t))
43+
{
44+
Setup(x, count, function);
45+
}
46+
~FunctionPreCacher()
47+
{
48+
Release();
49+
}
50+
51+
FunctionPreCacher& operator=(const FunctionPreCacher& other)
52+
{
53+
if (this != &other)
54+
{
55+
this->~FunctionPreCacher();
56+
this->FunctionPreCacher::FunctionPreCacher(other);
57+
}
58+
59+
return *this;
60+
}
61+
FunctionPreCacher& operator=(FunctionPreCacher&& other) noexcept
62+
{
63+
if (this != &other)
64+
{
65+
this->~FunctionPreCacher();
66+
this->FunctionPreCacher::FunctionPreCacher(std::move(other));
67+
}
68+
69+
return *this;
70+
}
71+
72+
void Setup(int32_t x, uint32_t count, int32_t(*function)(int32_t))
73+
{
74+
Release();
75+
this->x = x;
76+
this->count = count;
77+
this->function = function;
78+
}
79+
FunctionPreCacher& Compute()
80+
{
81+
if (function && count > 0)
82+
{
83+
Release();
84+
values = new int32_t[count];
85+
if (values)
86+
{
87+
for (uint32_t i = 1; i <= count; i++)
88+
{
89+
values[i - 1] = function(i * x);
90+
}
91+
}
92+
else
93+
{
94+
throw std::runtime_error("FunctionPreCacher new failed!");
95+
}
96+
}
97+
98+
return *this;
99+
}
100+
void Release()
101+
{
102+
if (values)
103+
{
104+
delete[] values;
105+
values = nullptr;
106+
}
107+
}
108+
void Print(std::ostream& os = std::cout) const
109+
{
110+
if (values)
111+
{
112+
for (uint32_t i = 1; i <= count; i++)
113+
os << "f(" << i * x << ") = " << values[i - 1] << std::endl;
114+
}
115+
}
116+
int32_t At(uint32_t index) const
117+
{
118+
if (index >= count)
119+
throw std::range_error("FunctionPreCacher index out of range!");
120+
return values[index];
121+
}
122+
uint32_t Size() const noexcept
123+
{
124+
return count;
125+
}
126+
127+
FunctionPreCacher& operator()()
128+
{
129+
return Compute();
130+
}
131+
FunctionPreCacher& operator()(int32_t x, uint32_t count, int32_t(*function)(int32_t))
132+
{
133+
Setup(x, count, function);
134+
return Compute();
135+
}
136+
int32_t operator[](uint32_t index) const
137+
{
138+
return At(index);
139+
}
140+
operator bool()
141+
{
142+
return values != nullptr;
143+
}
144+
145+
private:
146+
int32_t* values = nullptr;
147+
int32_t x = 0;
148+
uint32_t count = 0;
149+
int32_t(*function)(int32_t x) = nullptr;
150+
};
151+
}
152+
153+
inline std::ostream& operator<<(std::ostream& os, const SimpleApp::FunctionPreCacher& pc)
154+
{
155+
pc.Print(os);
156+
return os;
157+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#pragma once
2+
3+
#include "FunctionPreCacher.hpp"
4+
5+
#include <iostream>
6+
#include <stdexcept>
7+
8+
namespace SimpleApp
9+
{
10+
class PreCacherContainer
11+
{
12+
public:
13+
PreCacherContainer() = default;
14+
PreCacherContainer(const PreCacherContainer&) = default;
15+
PreCacherContainer(PreCacherContainer&&) noexcept = default;
16+
~PreCacherContainer() = default;
17+
18+
PreCacherContainer& operator=(const PreCacherContainer&) = default;
19+
PreCacherContainer& operator=(PreCacherContainer&&) noexcept = default;
20+
21+
void Print(std::ostream& os = std::cout) const
22+
{
23+
os << "Container at " << this << std::endl;
24+
for (int i = 0; i < m_usage; i++)
25+
{
26+
os << "FunctionPreCacher #" << (i + 1) << std::endl;
27+
os << m_preCachers[i];
28+
}
29+
}
30+
void Append(const FunctionPreCacher& pc)
31+
{
32+
if (m_usage >= 8)
33+
throw std::overflow_error("PreCacherContainer Container overflown");
34+
m_preCachers[m_usage++] = pc;
35+
}
36+
void Append(FunctionPreCacher&& pc)
37+
{
38+
if (m_usage >= 8)
39+
throw std::overflow_error("PreCacherContainer Container overflown");
40+
m_preCachers[m_usage++] = std::move(pc);
41+
}
42+
const FunctionPreCacher& At(int index) const
43+
{
44+
if (index >= 8 || index < 0)
45+
throw std::range_error("PreCacherContainer index out of range");
46+
return m_preCachers[index];
47+
}
48+
FunctionPreCacher& At(int index)
49+
{
50+
if (index >= 8 || index < 0)
51+
throw std::range_error("PreCacherContainer index out of range");
52+
return m_preCachers[index];
53+
}
54+
int Size() const noexcept
55+
{
56+
return m_usage;
57+
}
58+
59+
PreCacherContainer& operator<<(const FunctionPreCacher& pc)
60+
{
61+
Append(pc);
62+
return *this;
63+
}
64+
PreCacherContainer& operator<<(FunctionPreCacher&& pc)
65+
{
66+
Append(std::move(pc));
67+
return *this;
68+
}
69+
const FunctionPreCacher& operator[](int index) const
70+
{
71+
return At(index);
72+
}
73+
FunctionPreCacher& operator[](int index)
74+
{
75+
return At(index);
76+
}
77+
78+
private:
79+
FunctionPreCacher m_preCachers[8];
80+
int m_usage = 0;
81+
};
82+
}
83+
84+
inline std::ostream& operator<<(std::ostream& os, const SimpleApp::PreCacherContainer& pc)
85+
{
86+
pc.Print(os);
87+
return os;
88+
}

07_SimpleCppApp/SimpleApp.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "SimpleApp.hpp"
2+
3+
int SimpleApp::SaveMain()
4+
{
5+
int32_t x;
6+
uint32_t count;
7+
std::cout << "Enter the starting value: ";
8+
std::cin >> x;
9+
std::cout << "Enter the itterations: ";
10+
std::cin >> count;
11+
12+
PreCacherContainer cnt;
13+
cnt << std::move(FunctionPreCacher(x, count, &f)())
14+
<< std::move(FunctionPreCacher(x * 2, count, &f)())
15+
<< std::move(FunctionPreCacher(x, count * 2, &f)())
16+
<< std::move(FunctionPreCacher(x * 2, count * 2, &f)());
17+
std::cout << cnt;
18+
19+
return 0;
20+
}

07_SimpleCppApp/SimpleApp.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "FunctionPreCacher.hpp"
4+
#include "PreCacherContainer.hpp"
5+
6+
#include <cstdint>
7+
#include <iostream>
8+
9+
namespace SimpleApp
10+
{
11+
inline int32_t f(int32_t x)
12+
{
13+
return x * x + 5 * x + 5;
14+
}
15+
16+
int SaveMain();
17+
}

0 commit comments

Comments
 (0)