-
Notifications
You must be signed in to change notification settings - Fork 1
/
morden_c++_note.txt
454 lines (352 loc) · 11.1 KB
/
morden_c++_note.txt
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
morden_c++_note.txt
:Author: qingyun
:Email: [email protected]
:Date: 2018-07-17 21:14
template <class T, class U> class SmartPtr {…};
template <class U> class SmartPtr<Widget, U> {…};
template <class T>
struct OpNewCreator {
static *T Create() {
return new T;
}
};
template <class T>
struct MallocCreator {
struct T* Create() {
void *buf = std::malloc(sizeof(T));
if (!buf) return 0;
return new(buf) T;
}
};
template <class T>
struct PrototypeCreator {
PrototypeCreator(T *pObj = 0): pPrototype_(pObj) {}
T* Create() {
return pPrototype_ ? pPrototype_->Clone() : 0;
}
T* GetPrototype() { return pPrototype_; }
void SetPrototype(T *pObj) { pPrototype_ = pObj; }
private:
T* pPrototype_;
}
tempalte <class CreationPolicy>
class WidgetManager: public CreationPolicy {...};
typedef WidgetManager<OpNewCreator<Widget>> MyWidgetMgr;
template <template <class Created> class CreationPolicy>
class WidgetManager: public CreationPolicy<Widget> {
void DoSomething() {
Gadget *pW = CreationPolicy<Gadget>().Create();
}
void SwitchPrototype(Widget *pNewPrototype) {
CreationPolicy<Widget> &myPolicy = *this;
delete myPolicy.GetPrototype();
myPolicy.SetPrototype(pNewPrototype);
}
};
typedef WidgetManager<OpNewCreator> MyWidgetMgr;
template <template <class> class CreationPolicy = OpNewCreator>
class WidgetManager: public CreationPolicy<Widget> {...};
struct OpNewCreator {
template <class T>
static T* Create() {
return new T;
}
protected:
~OpNewCreator() {}
};
template <class T> struct NoChecking {
static void Check(T*) {}
};
template <class T> struct EnforceNotNull {
class NullPointerException: public std::exception {...};
static void Check(T* ptr) {
if (!ptr) throw NullPointerException();
}
static void Check(T*& ptr) {
if (!ptr) ptr = GetDefaultValue();
}
};
template <class T>
class DefaultSmartPtrStorage {
public:
typedef T* PointerType;
typedef T& ReferenceType;
protected:
PointerType GetPointer() { return ptr_; }
void SetPointer(PointerType ptr) { ptr_ = ptr; }
private:
PointerType pointee_;
};
template <class T, template <class> class CheckingPolicy, template <class> class ThreadingModel, template <class> class Storate = DefaultSmartPtrStorage>
class SmartPtr: public CheckingPolicy<T>, public ThreadingModel<SmartPtr> {
T* operator->() {
typename ThreadingModel<SmartPtr>::Lock guard(*this);
CheckingPolicy<T>::Check(pointee_);
return pointee_;
}
private:
T* pointee_;
};
typedef SmartPtr<Widget, NoChecking, SingleThread> WidgetPtr;
typedef SmartPtr<Widget, EnforceNotNull, SingleThread> SafeWidgetPtr;
template <class T, template <class> class CheckingPolicy>
class SmartPtr: public CheckingPolicy<T> {
template<class T1, template <class> class CP1>
SmartPtr(const SmartPtr<T1, CP1> &other): pointee_(other.pointee_), CheckingPolicy<T>(other) {...}
}
template <class To, class From>
To safe_reinterpret_cast(From from) {
assert(sizeof(From) <= sizeof(To));
return reinterpret_cast<To>(from);
}
#define STATIC_CHECK(expr) { char unnamed[(expr) ? 1 : 0]; }
template <class To, class From>
To safe_reinterpret_cast(From from) {
STATIC_CHECK(sizeof(From) < sizeof(To));
return reinterpret_cast<To>(from);
}
template<bool> struct CompileTimeError;
template<> struct CompileTimeError<true> {};
#define STATIC_CHECK(expr) (CompileTimeError<(expr) != 0>())
template<bool> struct CompiletimeChecker {
CompileTimeChecker(...);
}
template<> struct CompileTimeChecker<false> {};
#define STATIC_CHECK(expr, msg) \
{\
class ERROR_##msg {};
(void)sizeof(CompileTimeChecker<(expr)>(ERROR_##msg()));\
}
template <class To, class From>
To safe_reinterpret_cast(From from) {
STATIC_CHECK(sizeof(From) <= sizeof(To), Destination_Type_Too_Narrow);
return reinterpret_cast<To>(from);
}
int i = 5;
char *p = safe_reinterpret_cast<char *>(i);
template <class Window, class Controller>
class Widget {
...
};
template <>
clas Widget<ModalDialog, MyController> {
...
};
template <class Window>
class Widget<Window, MyController> {
...
};
template <class ButtonArg>
class Widget<Button<ButtonArg>, MyController> {
...
};
template <class T, class U> T Fun(U obj);
template <class T> T Fun(Window obj);
class Interface {
public:
virtual void Fun() = 0;
};
template <class T, class P>
Interface* MakeAdapter(const T& obj, const P& arg) {
class Local: public Interface {
public:
Local(const T& obj, const P&arg): obj_(obj), arg_(arg) {}
virtual void Fun() {
obj_.Call(arg_);
}
private:
T obj_;
P arg_;
};
return new Local(obj, arg);
}
template <int v>
struct Int2Type {
enum { value = v };
};
template <typename T, bool isPolymorphic>
class NiftyContainer {
private:
void DoSomething(T* pObj, Int2Type<true>) {
T* pNewObj = pObj->Clone();
}
void DoSomething(T* pObj, Int2Type<false>) {
T* pNewObj = new T(*pObj);
}
public:
void DoSomething(T* pObj) {
DoSomething(pObj, Int2Type<isPolymorphic>());
}
}
template <typename T>
struct Type2Type {
typedef T OriginalType;
};
template <class T, class U>
T* Create(const U& arg, Type2Type<T>) {
return new T(arg);
}
template <class U>
Widget* Create(const U& arg, Type2Type<Widget>) {
return new Widget(arg, -1);
}
String* pStr = Create("Hello", Type2Type<String>());
Widget* pw = Create(100, Type2Type<Widget>());
template <typename T, bool isPolymorphic>
struct NiftyContainerValueTraits {
typedef T* ValueType;
};
template <typename T>
struct NiftyContainerValueTraits<T, false> {
typedef T ValueType;
};
template <typename T, bool isPolymorphic>
class NiftyContainer {
typedef NiftyContainerValueTraits<T, isPolymorphic> Traits;
typedef typename Traits::ValueType ValueType;
};
template <bool flag, typename T, typename U>
struct Select {
typedef T Result;
};
template <typename T, typename U>
struct Select<false, T, U> {
typename U Result;
};
template <typename T, bool isPolymorphic>
class NiftyContainer {
typedef Select<isPolymorphic, T*, T>::Result ValueType;
};
typedef char Small;
class Big { char dummy[2]; };
Small Test(U);
Bit Test(...);
const bool convExists = sizeof(Test(T())) == sizeof(Small);
T MakeT(); // if T doens't have default constructor, use this
const bool convExists = sizeof(Test(MakeT())) == sizeof(Small);
template <class T, class U>
class Conversion {
typedef char Small;
class Big { char dummy[2]; };
static Small Test(U);
static Big Test(...);
static T MakeT();
public:
enum { exists = sizeof(Test(MakeT())) == sizeof(Small) };
enum { exists2Way = exists && Conversoion<U, T>::exists };
enum { sameType = false };
};
template <class T>
class Conversion<T, T> {
public:
enum { exists = 1, exists2Way = 1, sameType = 1 };
}
#define SUPERSUBCLASS(T, U) \
(Conversion<const U*, const T*>::exists && \
!Conversion<const T*, const void*>::sameType)
#define SUPERSUBCLASS_STRICT(T, U) \
(SUPERSUBCLASS(T, U) && \
!Conversion<const T, const U>::sameType)
int main() {
using namespace std;
cout << Conversion<double, int>::exists << ' '
<< Conversion<char, char*>::exists << ' '
<< Conversion<size_t, vector<int>>::exists;
}
class TypeInfo {
public:
TypeInfo();
TypeInfo(const std::type_info&);
TypeInfo(const TypeInfo&);
TypeInfo& operator=(const TypeInfo&);
bool before(const TypeInfo&) const;
const char* name() const;
private:
const std::type_info* pInfo_;
};
bool operator==(const TypeInfo&, const TypeInfo&);
bool operator!=(const TypeInfo&, const TypeInfo&);
bool operator<(const TypeInfo&, const TypeInfo&);
bool operator<=(const TypeInfo&, const TypeInfo&);
bool operator>(const TypeInfo&, const TypeInfo&);
bool operator>=(const TypeInfo&, const TypeInfo&);
void Fun(Base* pObj) {
TypeInfo info = typeid(Derived);
if (typeid(*pObj) == info) {
}
}
class NullType; // no definition
struct EmptyType {};
template <typename T>
class TypeTraits {
private:
template <class U> struct PointerTraits {
enum { result = false };
typedef NullType PointeeType;
};
template <class U> struct PointerTraits<U*> {
enum { result = true };
typedef U PointeeType;
};
template <class U> struct PToMTraits {
enum { result = false };
};
template <class U, class V> struct PToMTraits<U V::*> {
enum { result = true };
};
template <class U> struct UnCost {
typedef U Result;
};
template <class U> struct UnCost<const U> {
typedef U Result;
};
public:
enum { isPointer = PointerTraits<T>::result };
typedef PointerTraits<T>::PointeeType PointeeType;
enum { isMemberPointer = PToMTraits<T>::result };
typedef TYPELIST_4(unsigned char, unsigned short int, unsigned int, unsigned long int) UnsignedInts;
typedef TYPELIST_4(signed char, signed short int, signed int, signed long int) SignedInts;
typedef TYPELIST_3(bool, char, wchar_t) OtherInts;
typedef TYPELIST_3(float, double, long double) Floats;
enum { isStdUnsignedInt = TL::IndexOf<T, UnsignedInts>::value >= 0 };
enum { isStdSignedInt = TL::IndexOf<T, SignedInts>::value >= 0 };
enum { isStdIntegral = isStdUnsignedInt || isStdSignedInt || TL::IndexOf<T, OtherInts>::value >= 0 };
enum { isStdFloat = TL:<T, Floats>::value >= 0 };
enum { isStdArith = isStdIntegral || isStdFloat };
enum { isStdFundamental = isStdArith || isStdFloat || Conversion<T, void>::sameType };
typedef Select<isStdArith || isPointer || isMemberPointer, T, ReferencedType&>::Result ParameterType;
typedef UnConst<T>::Result NonConstType;
};
int main() {
const bool iterIsPtr = TypeTraits<vector<int>>::iterator>::isPointer;
cout << "vector<int>::iterator is " << iterIsPtr ? "fast" : "smart" << endl;
}
enum CopyAlgoSelector { Conservative, Fast };
template <typename Init, typename OutIt>
OutIt CopyImpl(InIt first, InIt last, OutIt result, Int2Type<Conservative>) {
for (; first != last; ++first, ++result)
*result = *first;
}
template <typename InIt, typename OutIt>
OutIt CopyImpl(InIt first, InIt last, OutIt result, Int2Type<Fast>) {
const size_t n = last - first;
BitBlast(first, result, n * sizeof(*first));
return result + n;
}
template <typename T> struct SupportsBitwiseCopy {
enum { result = TypeTraits<T>::isStdFundamental };
};
template <> struct SupportBitwiseCopy<MyType> {
enum { result = true; }
};
template <typename InIt, typename OutIt>
OutIt Copy(InIt first, InIt last, OutIt result) {
typedef TypeTraits<InIt>::PointeeType SrcPointee;
typedef TypeTraits<OutIt>::PointeeType DestPointee;
enum { copyAlgo = TypeTraits<InIt>::isPointer &&
TypeTraits<OutIt>::isPointer &&
SupportsBitwiseCopy<SrcPointee>::result &&
SupportsBitwiseCopy<DestPointee>::result &&
sizeof(SrcPointee) == sizeof(DestPointee) ? Fast : Conservative };
return CopyImpl(first, last, Int2Type<useBitBlast>);
}