-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnyObject.h
203 lines (181 loc) · 7.16 KB
/
AnyObject.h
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
#ifndef ANYOBJECT_H
#define ANYOBJECT_H
//---------------------------------------------------------------------------
// Include
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Declaration of IEmpty
//---------------------------------------------------------------------------
class IEmpty {
protected:
virtual ~IEmpty() = default;
};
//---------------------------------------------------------------------------
// Declaration of CAnyObjectBase
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
class CAnyObjectBase {
class IObjectStored;
using CStoredPtr = std::unique_ptr<IObjectStored>;
public:
inline CAnyObjectBase() = default;
template<class TObject>
inline CAnyObjectBase(TObject Object);
inline CAnyObjectBase(const CAnyObjectBase& Other);
CAnyObjectBase(CAnyObjectBase&& Other) = default;
inline CAnyObjectBase& operator=(const CAnyObjectBase& Other);
CAnyObjectBase& operator=(CAnyObjectBase&& Other) = default;
inline bool isDefined() const;
inline TInterface<IEmpty>* operator->();
inline const TInterface<IEmpty>* operator->() const;
protected:
~CAnyObjectBase() = default;
private:
//---------------------------------------------------------------------------
// Declaration of CAnyObjectBase::IObjectStored
//---------------------------------------------------------------------------
class IObjectStored : public TInterface<IEmpty> {
public:
~IObjectStored() override = default;
protected:
friend class CAnyObjectBase;
virtual CStoredPtr _make_copy() const = 0;
};
//---------------------------------------------------------------------------
// Declaration of CAnyObjectBase::CObjectKeeper
//---------------------------------------------------------------------------
template<class TObject>
class CObjectKeeper : public IObjectStored {
public:
CObjectKeeper(TObject Object);
~CObjectKeeper() override = default;
protected:
inline TObject& Object();
inline const TObject& Object() const;
using CObjectType = TObject;
private:
TObject Object_;
};
//---------------------------------------------------------------------------
// Declaration of CAnyObjectBase::CObjectStored
//---------------------------------------------------------------------------
template<class TObject>
class CObjectStored :
public TImplementation<CObjectKeeper<TObject>, TObject> {
using CBase = TImplementation<CObjectKeeper<TObject>, TObject>;
public:
using CBase::CBase;
~CObjectStored() override = default;
protected:
CStoredPtr _make_copy() const override;
};
//---------------------------------------------------------------------------
protected:
CStoredPtr& StoredPtr() {
return pIObject_;
}
const CStoredPtr& StoredPtr() const {
return pIObject_;
}
private:
CStoredPtr pIObject_;
};
//---------------------------------------------------------------------------
// Declaration of CAnyObject
//---------------------------------------------------------------------------
template< template<class>class TAnyImplementation,
template<class>class TInterface,
template<class, class>class TImplementation>
class CAnyObject :
public TAnyImplementation<CAnyObjectBase<TInterface, TImplementation>> {
using CBase =
TAnyImplementation<CAnyObjectBase<TInterface, TImplementation>>;
public:
using CBase::CBase;
};
//---------------------------------------------------------------------------
// Definition of CAnyObjectBase
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
template<class TObject>
CAnyObjectBase<TInterface, TImplementation>::CAnyObjectBase(TObject Object)
: pIObject_(std::make_unique<CObjectStored<TObject>>(std::move(Object))) {
}
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
CAnyObjectBase<TInterface, TImplementation>::CAnyObjectBase(
const CAnyObjectBase& Other)
: pIObject_(Other.pIObject_ ? Other.pIObject_->_make_copy() : nullptr) {
}
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
CAnyObjectBase<TInterface, TImplementation>&
CAnyObjectBase<TInterface, TImplementation>::operator=(
const CAnyObjectBase& Other) {
return *this = CAnyObjectBase(Other);
}
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
bool CAnyObjectBase<TInterface, TImplementation>::isDefined() const {
return pIObject_.operator bool();
}
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
TInterface<IEmpty>*
CAnyObjectBase<TInterface, TImplementation>::operator->() {
return pIObject_.get();
}
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
const TInterface<IEmpty>*
CAnyObjectBase<TInterface, TImplementation>::operator->() const {
return pIObject_.get();
}
//---------------------------------------------------------------------------
// Definition of CAnyObjectBase::CObjectKeeper
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
template<class TObject>
CAnyObjectBase<TInterface, TImplementation>::
CObjectKeeper<TObject>::CObjectKeeper(TObject Object)
: Object_(std::move(Object)) {
}
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
template<class TObject>
TObject&
CAnyObjectBase<TInterface, TImplementation>::
CObjectKeeper<TObject>::Object() {
return Object_;
}
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
template<class TObject>
const TObject&
CAnyObjectBase<TInterface, TImplementation>::
CObjectKeeper<TObject>::Object() const {
return Object_;
}
//---------------------------------------------------------------------------
// Definition of CAnyObjectBase::CObjectStored
//---------------------------------------------------------------------------
template< template<class>class TInterface,
template<class, class>class TImplementation>
template<class TObject>
std::unique_ptr<
typename CAnyObjectBase<TInterface, TImplementation>::IObjectStored>
CAnyObjectBase<TInterface, TImplementation>::
CObjectStored<TObject>::_make_copy() const {
return std::make_unique<CObjectStored>(*this);
}
#endif // ANYOBJECT_H