11#include < iostream>
2+ #include < stdexcept>
23#include < cstdlib>
34#include < cstdint>
45
@@ -24,6 +25,10 @@ class FunctionPreCacher
2425 {
2526 std::memcpy (values, other.values , sizeof (int32_t ) * other.count );
2627 }
28+ else
29+ {
30+ throw std::runtime_error (" FunctionPreCacher new failed!" );
31+ }
2732 }
2833 }
2934 FunctionPreCacher (FunctionPreCacher&& other) noexcept
@@ -85,6 +90,10 @@ class FunctionPreCacher
8590 values[i - 1 ] = function (i * x);
8691 }
8792 }
93+ else
94+ {
95+ throw std::runtime_error (" FunctionPreCacher new failed!" );
96+ }
8897 }
8998
9099 return *this ;
@@ -107,10 +116,11 @@ class FunctionPreCacher
107116 }
108117 int32_t At (uint32_t index) const
109118 {
110- // TODO: Check range
119+ if (index >= count)
120+ throw std::range_error (" FunctionPreCacher index out of range!" );
111121 return values[index];
112122 }
113- uint32_t Size () const
123+ uint32_t Size () const noexcept
114124 {
115125 return count;
116126 }
@@ -162,27 +172,29 @@ class PreCacherContainer
162172 }
163173 void Append (const FunctionPreCacher& pc)
164174 {
165- if (m_usage < 8 )
166- {
167- m_preCachers[m_usage++] = pc;
168- }
175+ if (m_usage >= 8 )
176+ throw std::overflow_error (" PreCacherContainer Container overflown" );
177+ m_preCachers[m_usage++] = pc;
169178 }
170179 void Append (FunctionPreCacher&& pc)
171180 {
172- if (m_usage < 8 )
173- {
174- m_preCachers[m_usage++] = std::move (pc);
175- }
181+ if (m_usage >= 8 )
182+ throw std::overflow_error (" PreCacherContainer Container overflown" );
183+ m_preCachers[m_usage++] = std::move (pc);
176184 }
177185 const FunctionPreCacher& At (int index) const
178186 {
187+ if (index >= 8 || index < 0 )
188+ throw std::range_error (" PreCacherContainer index out of range" );
179189 return m_preCachers[index];
180190 }
181191 FunctionPreCacher& At (int index)
182192 {
193+ if (index >= 8 || index < 0 )
194+ throw std::range_error (" PreCacherContainer index out of range" );
183195 return m_preCachers[index];
184196 }
185- int Size () const
197+ int Size () const noexcept
186198 {
187199 return m_usage;
188200 }
@@ -216,8 +228,13 @@ std::ostream& operator<<(std::ostream& os, const PreCacherContainer& pc)
216228 pc.Print (os);
217229 return os;
218230}
231+ std::ostream& operator <<(std::ostream& os, const FunctionPreCacher& pc)
232+ {
233+ pc.Print (os);
234+ return os;
235+ }
219236
220- int main ()
237+ int SaveMain ()
221238{
222239 int32_t x;
223240 uint32_t count;
@@ -231,6 +248,24 @@ int main()
231248 << std::move (FunctionPreCacher (x * 2 , count, &f)())
232249 << std::move (FunctionPreCacher (x, count * 2 , &f)())
233250 << std::move (FunctionPreCacher (x * 2 , count * 2 , &f)());
234-
235251 std::cout << cnt;
252+
253+ return 0 ;
254+ }
255+
256+ int main () noexcept
257+ {
258+ try
259+ {
260+ return SaveMain ();
261+ }
262+ catch (const std::exception& ex)
263+ {
264+ std::cout << " Exception occured: " << ex.what () << std::endl;
265+ }
266+ catch (...)
267+ {
268+ std::cout << " Unknown exception occured!" << std::endl;
269+ }
270+ return -1 ;
236271}
0 commit comments