-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathObject.cc
66 lines (52 loc) · 1.42 KB
/
Object.cc
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
#include "Object.h"
#include "items/Weapon.h"
#include "items/Armor.h"
#include "items/Potion.h"
#include "items/Scroll.h"
#include "items/FireScroll.h"
#include "items/PoisonPotion.h"
#include "ObjectConcept.h"
/**
* just done the inheritance thing. How is this better?
* It is better not because it affords you more functionality than the inheritance approach,
* but because it does not tighly couple Weapons and Armors etc through a common base class.
* It gives me the power of retaining type as templates do.
*/
template<typename T>
struct ObjectModel
: ObjectConcept
{
ObjectModel(T const& t)
: object(t)
{}
~ObjectModel() override = default;
bool has_attack_concept() const override
{
return object.can_attack();
}
std::string name() const override
{
return typeid(object).name();
}
private:
T object;
};
#pragma mark - Object
template<typename T>
Object::Object(T const& obj )
: object_(new ObjectModel<T>(obj))
{}
template Object::Object(Weapon const&);
template Object::Object(Armor const&);
template Object::Object(Potion const&);
template Object::Object(Scroll const&);
template Object::Object(FireScroll const&);
template Object::Object(PoisonPotion const&);
std::string Object::name() const
{
return object_->name();
}
bool Object::has_attack_concept() const
{
return object_->has_attack_concept();
}