summaryrefslogtreecommitdiffstats
path: root/ue5/verwendung.cpp
blob: 8a20e6ce6b0cccbd9a00b1ea2d7b2904fbac1a1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <cstddef> // for std::size_t
#include <cassert> // for assert
#include <utility> // for std::move
#include <stdexcept> // for std::out_of_range
#include <iostream> //TODO

struct Person
{
  Person (){}
  virtual int mark(){ return 0; }
  virtual ~Person(){}
};

struct Student : Person
{
  int m_mark;
  Student (int mark) :
    m_mark(mark)
  {}
  int mark()
  {
    return m_mark;
  }
};

struct Pupil : Person
{
  int m_mark;
  Pupil (int mark) :
    m_mark(mark)
  {}
  int mark()
  {
    return m_mark;
  }
};

#include <memory>
#include "shared_ptr.hpp"

#ifdef SOLVED_1
using Ti::shared_ptr;
using Ti::make_shared;
using Ti::shared_dynamic_cast;
#else
using std::shared_ptr;
using std::make_shared;
#endif

#include <array>
#include "array.hpp"

#ifdef SOLVED_2
using Ti::array;
using Ti::make_array;
#else
using std::array;
#endif

#include "mean_mark.hpp"
#ifdef SOLVED_3
using Ti::mean_mark;
using Ti::mean_mark_student;
using Ti::remove_greater;
#endif

#include <vector>
using std::vector;

int main()
{
  try {
    // initialize with new expression
    shared_ptr<Student> n1 (new Student(5));
    assert (n1.get() != 0);
    shared_ptr<Person> n2 (new Person);
    assert (n2.get() != 0);

    throw 0; // must be exception safe
  } catch (...)
  {}


  {
    // derived1 is 0.
    shared_ptr<Student> derived1;
    assert(derived1.get() == 0);

    // Other way for object creation
    derived1 = make_shared<Student>(3);
    assert(derived1.get() != 0);

    // Call object member
    derived1->mark();

    // Object creation with constructor parameters
    shared_ptr<Student> derived2 = make_shared<Student>(4);
    assert (derived2.get() != 0);

    shared_ptr<Person> base1 = make_shared<Person>();
    assert (base1.get() != 0);

    // Implicit upcast possible. The object make_sharedd in the previous line is
    // destroyed, because there are no more pointers referencing it.
    base1 = derived1;

#ifdef SOLVED_1
    // Explicit downcast possible. Some casts that are available: constCast,
    // staticCast, dynamicCast
    derived2 = shared_dynamic_cast<Student>(base1);

    // You can compare pointers.
    assert(derived1 == derived2);
#endif

    // Destroy most references to derived instance. References
    // (but not the object itself) are destroyed if they go out
    // of scope, or you can force reference destruction by multiple ways.
    derived1.reset();     // release reference
    assert(derived1.get() == 0);
  }


  {
    // array<int, 0> x; // Should fail with static assertion

    typedef array<shared_ptr<Student>, 5> sarray;
    sarray a;
    a [0] = shared_ptr<Student>(new Student(2));
    a [1] = shared_ptr<Student>(new Student(1));
    a [2] = shared_ptr<Student>(new Student(5));
    a [3] = shared_ptr<Student>(new Student(4));
    a [4] = shared_ptr<Student>();
    assert(a[0].get() != 0);
    assert(a[4].get() == 0);
    a [4] = shared_ptr<Student>(new Student(4));

    try {
      a.at(5); // throws exception
      assert(0);
    } catch (std::out_of_range const& oor) {
      oor.what();
    } catch (...) {
      assert(0);
    }

#ifdef SOLVED_2
    array<int,3> a1 = make_array<int,3>();
    a1.fill(3);

    array<int,3> a2 = make_array<int,3>();
    a2.fill(4);

    a1.swap(a2);
    assert(a1[0] == 4);
    assert(a2[2] == 3);

    for(array<int,3>::iterator it = a2.begin(); it != a2.end(); ++it)
      std::cout << (*it) << std::endl;
#endif

#ifdef SOLVED_3
    double mean = mean_mark(a.begin(), a.end());
    assert (mean >= 3.1 && mean <= 3.3);

    double mean_student = mean_mark_student(a.begin(), a.end());
    assert (mean_student >= 3.1 && mean_student <= 3.3);

    sarray::iterator end = remove_greater(a.begin(), a.end(), 3);
    double mean2 = mean_mark(a.begin(), end);
    assert (mean2 >= 1.4 && mean2 <= 1.6);
#endif
  }

  {
    typedef array<shared_ptr<Person>, 5> parray;
    parray m;
    m [0] = shared_ptr<Student>(new Student(2));
    m [1] = shared_ptr<Pupil>(new Pupil(1));
    m [2] = shared_ptr<Student>(new Student(5));
    m [3] = shared_ptr<Student>(new Student(4));
    m [4] = shared_ptr<Person>(new Person());

#ifdef SOLVED_3
    double mean = mean_mark(m.begin(), m.end());
    assert (mean >= 2.3 && mean <= 2.5);

    double mean_student = mean_mark_student(m.begin(), m.end());
    assert (mean_student >= 3.6 && mean_student <= 3.8);

    parray::iterator end = remove_greater(m.begin(), m.end(), 3);
    double mean2 = mean_mark(m.begin(), end);
    assert (mean2 >= 0.9 && mean2 <= 1.1);
#endif
  }

  {
    vector<shared_ptr<Person>> m;
    m.push_back(shared_ptr<Student>(new Student(2)));
    m.push_back(shared_ptr<Pupil>(new Pupil(1)));
    m.push_back(shared_ptr<Student>(new Student(5)));
    m.push_back(shared_ptr<Student>(new Student(4)));
    m.push_back(shared_ptr<Person>(new Person()));

#ifdef SOLVED_3
    double mean = mean_mark(m.begin(), m.end());
    assert (mean >= 2.3 && mean <= 2.5);

    double mean_student = mean_mark_student(m.begin(), m.end());
    assert (mean_student >= 3.6 && mean_student <= 3.8);

    assert(m.size() == 5);
    m.erase(remove_greater(m.begin(), m.end(), 3), m.end());
    assert(m.size() == 3);

    double mean2 = mean_mark(m.begin(), m.end());
    assert (mean2 >= 0.9 && mean2 <= 1.1);
#endif
  }

}

/* vim: set et sw=2 ts=2: */