Download | Plain Text | No Line Numbers


  1. #include <cstddef> // for std::size_t
  2. #include <cassert> // for assert
  3. #include <utility> // for std::move
  4. #include <stdexcept> // for std::out_of_range
  5.  
  6. struct Person
  7. {
  8. Person (){}
  9. virtual int mark(){ return 0; }
  10. virtual ~Person(){}
  11. };
  12.  
  13. struct Student : Person
  14. {
  15. int m_mark;
  16. Student (int mark) :
  17. m_mark(mark)
  18. {}
  19. int mark()
  20. {
  21. return m_mark;
  22. }
  23. };
  24.  
  25. struct Pupil : Person
  26. {
  27. int m_mark;
  28. Pupil (int mark) :
  29. m_mark(mark)
  30. {}
  31. int mark()
  32. {
  33. return m_mark;
  34. }
  35. };
  36.  
  37. #include <memory>
  38. #include "shared_ptr.hpp"
  39.  
  40. #ifdef SOLVED_1
  41. using Ti::shared_ptr;
  42. using Ti::make_shared;
  43. using Ti::shared_dynamic_cast;
  44. #else
  45. using std::shared_ptr;
  46. using std::make_shared;
  47. #endif
  48.  
  49. #include <array>
  50. #include "array.hpp"
  51.  
  52. #ifdef SOLVED_2
  53. using Ti::array;
  54. using Ti::make_array;
  55. #else
  56. using std::array;
  57. #endif
  58.  
  59. #include "mean_mark.hpp"
  60. #ifdef SOLVED_3
  61. using Ti::mean_mark;
  62. using Ti::mean_mark_student;
  63. using Ti::remove_greater;
  64. #endif
  65.  
  66. #include <vector>
  67. using std::vector;
  68.  
  69. int main()
  70. {
  71. try {
  72. // initialize with new expression
  73. shared_ptr<Student> n1 (new Student(5));
  74. assert (n1.get() != 0);
  75. shared_ptr<Person> n2 (new Person);
  76. assert (n2.get() != 0);
  77.  
  78. throw 0; // must be exception safe
  79. } catch (...)
  80. {}
  81.  
  82. {
  83. // derived1 is 0.
  84. shared_ptr<Student> derived1;
  85. assert(derived1.get() == 0);
  86.  
  87. // Other way for object creation
  88. derived1 = make_shared<Student>(3);
  89. assert(derived1.get() != 0);
  90.  
  91. // Call object member
  92. derived1->mark();
  93.  
  94. // Object creation with constructor parameters
  95. shared_ptr<Student> derived2 = make_shared<Student>(4);
  96. assert (derived2.get() != 0);
  97.  
  98. shared_ptr<Person> base1 = make_shared<Person>();
  99. assert (base1.get() != 0);
  100.  
  101. // Implicit upcast possible. The object make_sharedd in the previous line is
  102. // destroyed, because there are no more pointers referencing it.
  103. base1 = derived1;
  104.  
  105. #ifdef SOLVED_1
  106. // Explicit downcast possible. Some casts that are available: constCast,
  107. // staticCast, dynamicCast
  108. derived2 = shared_dynamic_cast<Student>(base1);
  109.  
  110. // You can compare pointers.
  111. assert(derived1 == derived2);
  112. #endif
  113.  
  114. // Destroy most references to derived instance. References
  115. // (but not the object itself) are destroyed if they go out
  116. // of scope, or you can force reference destruction by multiple ways.
  117. derived1.reset(); // release reference
  118. assert(derived1.get() == 0);
  119. }
  120.  
  121.  
  122. {
  123. // array<int, 0> x; // Should fail with static assertion
  124.  
  125. typedef array<shared_ptr<Student>, 5> sarray;
  126. sarray a;
  127. a [0] = shared_ptr<Student>(new Student(2));
  128. a [1] = shared_ptr<Student>(new Student(1));
  129. a [2] = shared_ptr<Student>(new Student(5));
  130. a [3] = shared_ptr<Student>(new Student(4));
  131. a [4] = shared_ptr<Student>();
  132. assert(a[0].get() != 0);
  133. assert(a[4].get() == 0);
  134. a [4] = shared_ptr<Student>(new Student(4));
  135.  
  136. try {
  137. a.at(5); // throws exception
  138. assert(0);
  139. } catch (std::out_of_range const& oor) {
  140. oor.what();
  141. } catch (...) {
  142. assert(0);
  143. }
  144.  
  145. #ifdef SOLVED_2
  146. array<int,3> a1 = make_array<int,3>();
  147. a1.fill(3);
  148.  
  149. array<int,3> a2 = make_array<int,3>();
  150. a2.fill(4);
  151.  
  152. a1.swap(a2);
  153. assert(a1[0] == 4);
  154. assert(a2[2] == 3);
  155. #endif
  156.  
  157. #ifdef SOLVED_3
  158. double mean = mean_mark(a.begin(), a.end());
  159. assert (mean >= 3.1 && mean <= 3.3);
  160.  
  161. double mean_student = mean_mark_student(a.begin(), a.end());
  162. assert (mean_student >= 3.1 && mean_student <= 3.3);
  163.  
  164. sarray::iterator end = remove_greater(a.begin(), a.end(), 3);
  165. double mean2 = mean_mark(a.begin(), end);
  166. assert (mean2 >= 1.4 && mean2 <= 1.6);
  167. #endif
  168. }
  169.  
  170. {
  171. typedef array<shared_ptr<Person>, 5> parray;
  172. parray m;
  173. m [0] = shared_ptr<Student>(new Student(2));
  174. m [1] = shared_ptr<Pupil>(new Pupil(1));
  175. m [2] = shared_ptr<Student>(new Student(5));
  176. m [3] = shared_ptr<Student>(new Student(4));
  177. m [4] = shared_ptr<Person>(new Person());
  178.  
  179. #ifdef SOLVED_3
  180. double mean = mean_mark(m.begin(), m.end());
  181. assert (mean >= 2.3 && mean <= 2.5);
  182.  
  183. double mean_student = mean_mark_student(m.begin(), m.end());
  184. assert (mean_student >= 3.6 && mean_student <= 3.8);
  185.  
  186. parray::iterator end = remove_greater(m.begin(), m.end(), 3);
  187. double mean2 = mean_mark(m.begin(), end);
  188. assert (mean2 >= 0.9 && mean2 <= 1.1);
  189. #endif
  190. }
  191.  
  192. {
  193. vector<shared_ptr<Person>> m;
  194. m.push_back(shared_ptr<Student>(new Student(2)));
  195. m.push_back(shared_ptr<Pupil>(new Pupil(1)));
  196. m.push_back(shared_ptr<Student>(new Student(5)));
  197. m.push_back(shared_ptr<Student>(new Student(4)));
  198. m.push_back(shared_ptr<Person>(new Person()));
  199.  
  200. #ifdef SOLVED_3
  201. double mean = mean_mark(m.begin(), m.end());
  202. assert (mean >= 2.3 && mean <= 2.5);
  203.  
  204. double mean_student = mean_mark_student(m.begin(), m.end());
  205. assert (mean_student >= 3.6 && mean_student <= 3.8);
  206.  
  207. assert(m.size() == 5);
  208. m.erase(remove_greater(m.begin(), m.end(), 3), m.end());
  209. assert(m.size() == 3);
  210.  
  211. double mean2 = mean_mark(m.begin(), m.end());
  212. assert (mean2 >= 0.9 && mean2 <= 1.1);
  213. #endif
  214. }
  215.  
  216. }
  217.  
  218. /* vim: set et sw=2 ts=2: */
  219.