aboutsummaryrefslogtreecommitdiffstats
path: root/src/lesson24_oop_virtl03.cpp
blob: 0d4499c47e3307b1876f611e57b041620dd9a783 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//============================================================================
// Author      : Sven Göthel
// Copyright   : 2024 Göthel Software e.K.
// License     : MIT
// Description : C++ Lesson 2.4 OOP (inheritance w/ virtual lifecycle managment)
//============================================================================

#include <limits>
#include <string>
#include <memory>
#include <vector>
#include <atomic>
#include <cassert>
#include <iostream>

class creature_t {
    private:
        static std::atomic_uint64_t next_instance;
        uint64_t m_id;
        int m_init_lifetime_left;
        int m_init_childs_left;
        int m_lifetime_left;
        int m_childs_left;

    protected:
        /** Custom constructor (ctor) */
        creature_t(int lifetime_left, int childs_left) noexcept
        : m_id(++next_instance),
          m_init_lifetime_left(lifetime_left), m_init_childs_left(childs_left),
          m_lifetime_left(lifetime_left), m_childs_left(childs_left) {}

        /** Copy ctor, actually creating a new life (asexual) w/ a new instance id() */
        creature_t(const creature_t& o) noexcept
        : m_id(++next_instance),
          m_init_lifetime_left(o.m_init_lifetime_left), m_init_childs_left(o.m_init_childs_left),
          m_lifetime_left(o.m_init_lifetime_left), m_childs_left(o.m_init_childs_left) {}

        /** Copy ctor, actually creating a new life (sexual) w/ a new instance id() */
        creature_t(const creature_t& a, const creature_t& b) noexcept
        : m_id(++next_instance),
          m_init_lifetime_left((a.m_init_lifetime_left+b.m_init_lifetime_left)/2),
          m_init_childs_left((a.m_init_childs_left+b.m_init_childs_left)/2),
          m_lifetime_left((a.m_init_lifetime_left+b.m_init_lifetime_left)/2),
          m_childs_left((a.m_init_childs_left+b.m_init_childs_left)/2) {}

        /** Move ctor */
        creature_t(creature_t&& o) noexcept
        : m_id(o.m_id), m_init_lifetime_left(o.m_lifetime_left), m_init_childs_left(o.m_childs_left),
          m_lifetime_left(o.m_lifetime_left), m_childs_left(o.m_childs_left)
        {
            o.m_id = 0;
            o.m_childs_left = 0;
            o.m_lifetime_left = 0;
        }

    public:
        /** Virtual destructor (dtor) */
        virtual ~creature_t() noexcept = default;

        /** reproduction type */
        enum class repro_t {
            /** asexual reproduction only */
            asexual,
            /** sexual reproduction only */
            sexual,
            /** asexual and sexual reproduction */
            dual
        };
        /** Returns the reproduction type */
        virtual repro_t repro_type() const noexcept = 0;

    protected:
        /** Fully virtual copy-ctor function (asexual) on this super class */
        virtual std::shared_ptr<creature_t> new_instance() noexcept = 0;

        /** Fully virtual copy-ctor function (sexual) on this super class */
        virtual std::shared_ptr<creature_t> new_instance(const creature_t& o) noexcept = 0;

    public:
        /** create one offspring (asexual) if childs_left() > 0 and repro_type() is not repro_t::sexual, otherwise returns nullptr. */
        std::shared_ptr<creature_t> procreate() noexcept {
            if( repro_t::sexual != repro_type() &&
                m_childs_left > 0 )
            {
                --m_childs_left;
                return new_instance();
            } else {
                return nullptr;
            }
        }

        /** create one offspring (sexual) if childs_left() > 0 and repro_type() is not repro_t::asexual, otherwise returns nullptr. */
        std::shared_ptr<creature_t> procreate(creature_t& o) noexcept {
            if( repro_t::asexual != repro_type() &&
                m_childs_left > 0 && o.childs_left() > 0 )
            {
                --m_childs_left;
                --o.m_childs_left;
                return new_instance(o);
            } else {
                return nullptr;
            }
        }

        /** returns the unique creature id */
        uint64_t id() const noexcept { return m_id; }

        int total_lifetime() const noexcept { return m_init_lifetime_left; }
        int lifetime_left() const noexcept { return m_lifetime_left; }
        int age() const noexcept { return total_lifetime() - lifetime_left(); }
        bool alive() const noexcept { return lifetime_left() > 0; }
        std::string lifesign() const noexcept { return alive() ? "☀" : "✝"; }

        int total_childs() const noexcept { return m_init_childs_left; }
        int childs_left() const noexcept { return m_childs_left; }
        int children() const noexcept { return total_childs() - childs_left(); }

        /** Subtract given years of life and return whether there is lifetime left */
        bool tick(int years) noexcept {
            if( m_lifetime_left > years ) {
                m_lifetime_left -= years;
                return true;
            } else {
                m_lifetime_left = 0;
                return false;
            }
        }

        /** Fully virtual to_string() method to be implemented in children classes */
        virtual std::string to_string() const noexcept = 0;

    protected:
        std::string to_substring() const noexcept {
            return "id "+std::to_string(id())+", age "+std::to_string(age())+"/"+std::to_string(total_lifetime())+", childs "+std::to_string(children())+"/"+std::to_string(total_childs());
        }
};
std::atomic_uint64_t creature_t::next_instance;

typedef std::shared_ptr<creature_t> shared_creature_t;

std::ostream& operator<<(std::ostream& out, const creature_t& v) {
    return out << v.to_string();
}

/**
 * A plant_t creature_t type supporting both, asexual and sexual procreation.
 */
class plant_t : public creature_t {
    public:
        /** Custom constructor (ctor) */
        plant_t(int lifetime_left, int childs_left) noexcept
        : creature_t(lifetime_left, childs_left) {}

        /** Move ctor */
        plant_t(plant_t&& o) noexcept
        : creature_t(std::move(o)) {}

        virtual repro_t repro_type() const noexcept override { return repro_t::dual; }

        /** Virtual to_string() override  */
        virtual std::string to_string() const noexcept override { return "plant"+lifesign()+"["+creature_t::to_substring()+"]"; }

    protected:
        /** Copy ctor, actually creating a new life (asexual) w/ a new instance id() */
        plant_t(const plant_t& o) noexcept
        : creature_t(o) { }

        /** Copy ctor, actually creating a new life (sexual) w/ a new instance id() */
        plant_t(const plant_t& a, const plant_t& b) noexcept
        : creature_t(a, b) { }

        /** Virtual new_copy() (asexual) override  */
        virtual std::shared_ptr<creature_t> new_instance() noexcept override {
            return std::shared_ptr<plant_t>( new plant_t(*this) );
        }

        /** Virtual new_instance() (sexual) override */
        virtual std::shared_ptr<creature_t> new_instance(const creature_t& o) noexcept override {
            return std::shared_ptr<plant_t>( new plant_t(*this, *static_cast<const plant_t*>(&o)) );
        }
};

/**
 * An animal_t creature_t type supporting sexual procreation only
 */
class animal_t : public creature_t {
    public:
        /** Custom constructor (ctor) */
        animal_t(int lifetime_left, int childs_left) noexcept
        : creature_t(lifetime_left, childs_left) {}

        /** Move ctor */
        animal_t(animal_t&& o) noexcept
        : creature_t(std::move(o)) {}

        virtual repro_t repro_type() const noexcept override { return repro_t::sexual; }

        /** Virtual to_string() override  */
        virtual std::string to_string() const noexcept override { return "animal"+lifesign()+"["+creature_t::to_substring()+"]"; }

    protected:
        /** Copy ctor, actually creating a new life (asexual) w/ a new instance id() */
        animal_t(const animal_t& o) noexcept
        : creature_t(o) { }

        /** Copy ctor, actually creating a new life (sexual) w/ a new instance id() */
        animal_t(const animal_t& a, const animal_t& b) noexcept
        : creature_t(a, b) { }

        /** Virtual new_copy() (asexual) override - returns nullptr  */
        virtual std::shared_ptr<creature_t> new_instance() noexcept override {
            return std::shared_ptr<animal_t>();
        }
        /** Virtual new_instance() (sexual) override */
        virtual std::shared_ptr<creature_t> new_instance(const creature_t& o) noexcept override {
            return std::shared_ptr<animal_t>( new animal_t(*this, *static_cast<const animal_t*>(&o)) );
        }
};

int main(int, char*[]) {
    plant_t p1(3, 1000);
    animal_t a1(80, 6), a2(40, 3);

    std::cout << "00.p1 = " << p1 << std::endl;
    std::cout << "00.a1 = " << a1 << std::endl;
    std::cout << "00.a2 = " << a2 << std::endl;

    creature_t& c1p1 = p1, &c2a1 = a1;
    std::cout << "00.c1p1 = " << c1p1 << std::endl;
    std::cout << "00.c2a1 = " << c2a1 << std::endl;

    p1.tick(1);
    a1.tick(1);
    a2.tick(2);
    std::cout << "01.p1 = " << p1 << std::endl;
    std::cout << "01.a1 = " << a1 << std::endl;
    std::cout << "01.a2 = " << a2 << std::endl;

    shared_creature_t p2 = p1.procreate();
    assert( nullptr != p2 );
    std::cout << "00.p2 = " << *p2 << std::endl;
    {
        // casting shared_ptr<super-class> to shared_ptr<sub-class> is possible, if you must
        std::shared_ptr<plant_t> p2_plant = std::static_pointer_cast<plant_t>( p2 );
        assert( nullptr != p2_plant );
    }
    {
        shared_creature_t a12 = a1.procreate(); // only sexual procreation supported for this animal_t
        assert( nullptr == a12 );
    }
    shared_creature_t a12 = a1.procreate(a2);
    assert( nullptr != a12 );
    std::cout << "00.a12 = " << *a12 << std::endl;

    p1.tick(2);
    a1.tick(79);
    std::cout << "03.p1 = " << p1 << std::endl;
    std::cout << "79.a1 = " << a1 << std::endl;

    p2->tick(3);
    std::cout << "03.p2 = " << *p2 << std::endl;
    {
        std::vector<shared_creature_t> a12childs;
        {
            shared_creature_t c;
            while( nullptr != ( c = a12->procreate(a1) ) ) {
                a12childs.push_back(c);
            }
            assert( 0 < a12childs.size() );
        }
        std::cout << "00.a12 = " << *a12 << ", created "+std::to_string(a12childs.size()) << std::endl;
        for(shared_creature_t c : a12childs) {
            std::cout << "00.a12.c = " << *c << std::endl;
        }
        a12->tick(60);
        std::cout << "60.a12 = " << *a12 << std::endl;
    }

    {
        // animal_t a3(80, 4);
        // animal_t a3b(a3); // error, copy-ctor is non-public
    }
    {
        animal_t a3(80, 4);
        animal_t a3b(std::move(a3));
        std::cout << "00.a3 = " << a3 << " -> " << a3b << std::endl;
    }
    {
        animal_t a3(80, 4);
        animal_t a3b = std::move(a3);
        std::cout << "00.a3 = " << a3 << " -> " << a3b << std::endl;
    }

    return 0;
}