|
| 1 | +#include "object.hpp" |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +static char* _print_obj(char *buf, const Object& obj) |
| 6 | +{ |
| 7 | + sprintf(buf, "(name=%s, epoch=%d, alive=%i)", obj.name, obj.epoch, obj.alive); |
| 8 | + return buf; |
| 9 | +} |
| 10 | + |
| 11 | +static void _print(const char *action, const Object& from, const Object& to) |
| 12 | +{ |
| 13 | + char b1[512]; |
| 14 | + char b2[512]; |
| 15 | + printf("%s => %s -> %s\n", action, _print_obj(b1, from), _print_obj(b2, to)); |
| 16 | +} |
| 17 | + |
| 18 | +static void _print(const char *action, const Object& obj) |
| 19 | +{ |
| 20 | + char b1[512]; |
| 21 | + printf("%s => %s\n", action, _print_obj(b1, obj)); |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +Object::Object(const char *name) |
| 26 | + : name(name), epoch(0), alive(ALIVE) |
| 27 | +{ |
| 28 | + _print("Object", *this); |
| 29 | +} |
| 30 | + |
| 31 | +Object::Object(const Object& other) |
| 32 | + : name(other.name), epoch(other.epoch + 1), alive(other.alive) |
| 33 | +{ |
| 34 | + _print("Copy", other, *this); |
| 35 | +} |
| 36 | + |
| 37 | +Object& Object::operator=(const Object& other) |
| 38 | +{ |
| 39 | + if (this == &other) { |
| 40 | + return *this; |
| 41 | + } |
| 42 | + |
| 43 | + name = other.name; |
| 44 | + epoch = other.epoch + 1; |
| 45 | + alive = other.alive; |
| 46 | + |
| 47 | + _print("CopyOp", other, *this); |
| 48 | + |
| 49 | + return *this; |
| 50 | +} |
| 51 | + |
| 52 | +Object::Object(Object&& other) |
| 53 | + : name(other.name), epoch(other.epoch + 1), alive(true) |
| 54 | +{ |
| 55 | + _print("Move", other, *this); |
| 56 | + other.alive = MOVED; |
| 57 | +} |
| 58 | + |
| 59 | +Object& Object::operator=(Object&& other) |
| 60 | +{ |
| 61 | + if (this == &other) { |
| 62 | + return *this; |
| 63 | + } |
| 64 | + |
| 65 | + name = other.name; |
| 66 | + epoch = other.epoch + 1; |
| 67 | + alive = other.alive; |
| 68 | + |
| 69 | + _print("MoveOp", other, *this); |
| 70 | + other.alive = MOVED; |
| 71 | + |
| 72 | + return *this; |
| 73 | +} |
| 74 | + |
| 75 | +Object::~Object() |
| 76 | +{ |
| 77 | + _print("Destroy", *this); |
| 78 | + alive = DESTROYED; |
| 79 | +} |
| 80 | + |
| 81 | +void Object::doit() |
| 82 | +{ |
| 83 | + _print("DO", *this); |
| 84 | +} |
0 commit comments