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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#ifndef __HOLDER_H_
#define __HOLDER_H_
#include <wincrypt.h>
#include "cor.h"
#include "staticcontract.h"
#include "volatile.h"
#include "palclr.h"
#ifdef PAL_STDCPP_COMPAT
#include <utility>
#include <type_traits>
#else
#include "clr_std/utility"
#include "clr_std/type_traits"
#endif
#if defined(FEATURE_COMINTEROP) && !defined(STRIKE)
#include <Activation.h>
#include <Inspectable.h>
#endif
// Note: you can't use CONTRACT's in this file. You can't use dynamic contracts because the impl of dynamic
// contracts depends on holders. You can't use static contracts because they include the function name as a string,
// and the template names in this file are just too long, so you get a compiler error.
//
// All the functions in this file are pretty basic, so the lack of CONTRACT's isn't a big loss.
#ifdef _MSC_VER
// Make sure we can recurse deep enough for FORCEINLINE
#pragma inline_recursion(on)
#pragma inline_depth(16)
#pragma warning(disable:4714)
#endif // _MSC_VER
//------------------------------------------------------------------------------------------------
// Declare but do not define methods that would normally be automatically generated by the
// compiler. This will produce a link-time error if they are used. This is involved in object
// initialization and operator eliding; see
// http://groups.google.com/group/comp.lang.c++/msg/3cd673ab749bed83?dmode=source
// for the intricate details.
#ifdef __GNUC__
// GCC checks accessibility of the copy ctor before performing elision optimization, so
// it must be declared as public. VC does not, so we can declare it private to give an
// earlier error message.
#define HIDE_GENERATED_METHODS(_NAME) \
public: \
_NAME(_NAME const &); \
private: \
_NAME & operator=(_NAME const &);
#else
#define HIDE_GENERATED_METHODS(_NAME) \
private: \
_NAME(_NAME const &); \
_NAME & operator=(_NAME const &);
#endif
#ifdef _DEBUG
//------------------------------------------------------------------------------------------------
// This is used to make Visual Studio autoexp.dat work sensibly with holders again.
// The problem is that certain codebases (particulary Fusion) implement key data structures
// using a class but refer to it using a holder to an interface type. This combination prevents
// autoexp rules from working as desired.
//
// Example: Take this useful autoexp rule for CAssemblyName.
//
// CAssemblyName=<_rProp._rProp[3].asStr>
//
// To get the same rule to fire when your assemblyname is wrapped in a ReleaseHolder,
// add these companion rules.
//
// HolderBase<CAssemblyName *>=<m_value->_rProp._rProp[3].asStr>
// HolderBase<IAssemblyName *>=<m_pAutoExpVisibleValue->_asCAssemblyName->_rProp._rProp[3].asStr>
//
//------------------------------------------------------------------------------------------------
struct AutoExpVisibleValue
{
private:
union
{
// Only include a class name here if it is customarily referred to through an abstract interface.
#if defined(FEATURE_APPX)
const class AppXBindResultImpl *_asAppXBindResultImpl;
#endif
const void *_pPreventEmptyUnion;
};
};
#endif //_DEBUG
//-----------------------------------------------------------------------------
// Holder is the base class of all holder objects. Any backout object should derive from it.
// (Eventually some additional bookeeping and exception handling code will be placed in this
// base class.)
//
// There are several ways to use this class:
// 1. Derive from HolderBase, and instantiate a Holder or Wrapper around your base. This is necessary
// if you need to add more state to your holder.
// 2. Instantiate the Holder template with your type and functions.
// 3. Instantiate the Wrapper template with your type and functions. The Wrapper adds some additional
// operator overloads to provide "smart pointer" like behavior
// 4. Use a prebaked Holder. This is ALWAYS the preferable strategy. It is expected that
// the general design patter is that Holders will be provided as part of a typical data abstraction.
// (See Crst for an example of this.)
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// HolderBase defines the base holder functionality. You can subtype and plug in
// a different base if you need to add more members for access during
// acquire & release
//-----------------------------------------------------------------------------
template <typename TYPE>
class HolderBase
{
protected:
TYPE m_value;
HolderBase(TYPE value)
: m_value(value)
{
#ifdef _DEBUG
m_pAutoExpVisibleValue = (const AutoExpVisibleValue *)(&m_value);
#endif //_DEBUG
}
void DoAcquire()
{
// Insert any global or thread bookeeping here
}
void DoRelease()
{
// Insert any global or thread bookeeping here
}
#ifdef _DEBUG
private:
// NOT DEAD CODE: This field is not referenced in the source code but it is not a dead field. See comments above "class AutoExpVisibleValue" for why this field exists.
// Note: What we really want is for m_value and m_pAutoExpVisibleValue to be members of a union. But the compiler won't let you build that
// since it can't prove that "TYPE" doesn't have a non-trivial constructor.
const AutoExpVisibleValue *m_pAutoExpVisibleValue; // This field is to be referenced from individual autoexp.dat files, NOT from the CLR source code.
#endif //_DEBUG
}; // class HolderBase<>
#ifndef _PREFAST_ // Work around an ICE error in EspX.dll
template <typename TYPE>
BOOL CompareDefault(TYPE value, TYPE defaultValue)
{
STATIC_CONTRACT_SUPPORTS_DAC;
return value == defaultValue;
}
#else
template <typename TYPE>
BOOL CompareDefault(TYPE value, TYPE defaultValue)
{
return FALSE;
}
#endif
template <typename TYPE>
BOOL NoNull(TYPE value, TYPE defaultValue)
{
return FALSE;
}
// Used e.g. for retail version of code:SyncAccessHolder
template <typename TYPE>
class NoOpBaseHolder
{
public:
FORCEINLINE NoOpBaseHolder()
{
}
FORCEINLINE NoOpBaseHolder(TYPE value, BOOL take = TRUE)
{
}
FORCEINLINE ~NoOpBaseHolder()
{
}
FORCEINLINE void Assign(TYPE value, BOOL fTake = TRUE)
{
}
FORCEINLINE void Acquire()
{
}
FORCEINLINE void Release()
{
}
FORCEINLINE void Clear()
{
}
FORCEINLINE void SuppressRelease()
{
}
FORCEINLINE TYPE Extract()
{
}
FORCEINLINE TYPE GetValue()
{
}
FORCEINLINE BOOL IsNull() const
{
return FALSE;
}
private:
NoOpBaseHolder& operator=(NoOpBaseHolder const &);
NoOpBaseHolder(NoOpBaseHolder const &);
}; // NoOpBaseHolder<>
template
<
typename TYPE,
typename BASE,
UINT_PTR DEFAULTVALUE = 0,
BOOL IS_NULL(TYPE, TYPE) = CompareDefault<TYPE>
>
class BaseHolder : protected BASE
{
protected:
BOOL m_acquired; // Have we acquired the resource?
static_assert(!std::is_pointer<TYPE>::value || DEFAULTVALUE == 0 || DEFAULTVALUE == UINT_PTR(-1 /*INVALID_HANDLE_VALUE*/),
"DEFAULTVALUE must be NULL for pointer holders and wrappers.");
public:
FORCEINLINE BaseHolder()
: BASE(TYPE(DEFAULTVALUE)),
m_acquired(FALSE)
{
}
FORCEINLINE BaseHolder(TYPE value)
: BASE(value),
m_acquired(FALSE)
{
if (!IsNull())
Acquire();
}
FORCEINLINE BaseHolder(TYPE value, BOOL takeOwnership)
: BASE(value),
m_acquired(FALSE)
{
if (takeOwnership)
Acquire();
}
FORCEINLINE ~BaseHolder()
{
Release();
}
// Sets the value to 'value'. Doesn't call Acquire if value is DEFAULTVALUE.
FORCEINLINE void Assign(TYPE value)
{
Assign(value, !IS_NULL(value, TYPE(DEFAULTVALUE)));
}
// Sets the value to 'value'. Doesn't call Acquire if fTake is FALSE.
FORCEINLINE void Assign(TYPE value, BOOL takeOwnership)
{
Release();
this->m_value = value;
if (takeOwnership)
Acquire();
}
FORCEINLINE void Acquire()
{
STATIC_CONTRACT_WRAPPER;
_ASSERTE(!m_acquired);
if (!IsNull())
{
this->DoAcquire();
m_acquired = TRUE;
}
}
FORCEINLINE void Release()
{
STATIC_CONTRACT_WRAPPER;
if (m_acquired)
{
_ASSERTE(!IsNull());
this->DoRelease();
m_acquired = FALSE;
}
}
FORCEINLINE void Clear()
{
STATIC_CONTRACT_WRAPPER;
Assign(TYPE(DEFAULTVALUE), FALSE);
}
FORCEINLINE void SuppressRelease()
{
STATIC_CONTRACT_LEAF;
m_acquired = FALSE;
}
FORCEINLINE TYPE Extract()
{
STATIC_CONTRACT_WRAPPER;
SuppressRelease();
return GetValue();
}
FORCEINLINE TYPE GetValue()
{
STATIC_CONTRACT_LEAF;
return this->m_value;
}
FORCEINLINE BOOL IsNull() const
{
STATIC_CONTRACT_WRAPPER;
return IS_NULL(this->m_value, TYPE(DEFAULTVALUE));
}
HIDE_GENERATED_METHODS(BaseHolder)
}; // BaseHolder<>
template <void (*ACQUIRE)(), void (*RELEASEF)()>
class StateHolder
{
private:
BOOL m_acquired; // Have we acquired the state?
public:
FORCEINLINE StateHolder(BOOL take = TRUE)
: m_acquired(FALSE)
{
STATIC_CONTRACT_WRAPPER;
if (take)
Acquire();
}
FORCEINLINE ~StateHolder()
{
STATIC_CONTRACT_WRAPPER;
Release();
}
FORCEINLINE void Acquire()
{
STATIC_CONTRACT_WRAPPER;
// Insert any global or thread bookeeping here
_ASSERTE(!m_acquired);
ACQUIRE();
m_acquired = TRUE;
}
FORCEINLINE void Release()
{
STATIC_CONTRACT_WRAPPER;
// Insert any global or thread bookeeping here
if (m_acquired)
{
RELEASEF();
m_acquired = FALSE;
}
}
FORCEINLINE void Clear()
{
STATIC_CONTRACT_WRAPPER;
if (m_acquired)
{
RELEASEF();
}
m_acquired = FALSE;
}
FORCEINLINE void SuppressRelease()
{
STATIC_CONTRACT_LEAF;
m_acquired = FALSE;
}
HIDE_GENERATED_METHODS(StateHolder)
}; // class StateHolder<>
// Holder for the case where the acquire function can fail.
template <typename VALUE, BOOL (*ACQUIRE)(VALUE value), void (*RELEASEF)(VALUE value)>
class ConditionalStateHolder
{
private:
VALUE m_value;
BOOL m_acquired; // Have we acquired the state?
public:
FORCEINLINE ConditionalStateHolder(VALUE value, BOOL take = TRUE)
: m_value(value), m_acquired(FALSE)
{
STATIC_CONTRACT_WRAPPER;
if (take)
Acquire();
}
FORCEINLINE ~ConditionalStateHolder()
{
STATIC_CONTRACT_WRAPPER;
Release();
}
FORCEINLINE BOOL Acquire()
{
STATIC_CONTRACT_WRAPPER;
// Insert any global or thread bookeeping here
_ASSERTE(!m_acquired);
m_acquired = ACQUIRE(m_value);
return m_acquired;
}
FORCEINLINE void Release()
{
STATIC_CONTRACT_WRAPPER;
// Insert any global or thread bookeeping here
if (m_acquired)
{
RELEASEF(m_value);
m_acquired = FALSE;
}
}
FORCEINLINE void Clear()
{
STATIC_CONTRACT_WRAPPER;
if (m_acquired)
{
RELEASEF(m_value);
}
m_acquired = FALSE;
}
FORCEINLINE void SuppressRelease()
{
STATIC_CONTRACT_LEAF;
m_acquired = FALSE;
}
FORCEINLINE BOOL Acquired()
{
STATIC_CONTRACT_LEAF;
return m_acquired;
}
HIDE_GENERATED_METHODS(ConditionalStateHolder)
}; // class ConditionalStateHolder<>
// Making the copy constructor private produces a warning about "can't generate copy
// constructor" on all holders (duh, that's the point.)
#ifdef _MSC_VER
#pragma warning(disable:4511)
#endif // _MSC_VER
//-----------------------------------------------------------------------------
// BaseWrapper is just Base like a Holder, but it "transparently" proxies the type it contains,
// using operator overloads. Use this when you want a holder to expose the functionality of
// the value it contains.
//-----------------------------------------------------------------------------
template <typename TYPE, typename BASE,
UINT_PTR DEFAULTVALUE = 0, BOOL IS_NULL(TYPE, TYPE) = CompareDefault<TYPE>>
class BaseWrapper : public BaseHolder<TYPE, BASE, DEFAULTVALUE, IS_NULL>
{
typedef BaseHolder<TYPE, BASE, DEFAULTVALUE, IS_NULL> BaseT;
#ifdef __GNUC__
//#pragma GCC visibility push(hidden)
#endif // __GNUC__
// This temporary object takes care of the case where we are initializing
// a holder's contents by passing it as an out parameter. The object is
// guaranteed to live longer than the call it is passed to, hence we should
// properly acquire the object on return
friend class AddressInitHolder;
class AddressInitHolder
{
protected:
BaseWrapper<TYPE,BASE,DEFAULTVALUE,IS_NULL> &m_holder;
public:
FORCEINLINE AddressInitHolder(BaseWrapper<TYPE,BASE,DEFAULTVALUE,IS_NULL> &holder)
: m_holder(holder)
{
//
// We must clear the value, to avoid the following scenario:
//
// ReleaseHolder<MyType> pMyType;
// hr = MyFunction(&pMyType, ...);
// <do something with pMyType>
// hr = MyFunction(&pMyType, ...); <- calls Release before call and Acquire on return.
//
// If the second call to MyFunction were to fail and return without updating the
// out parameter, then ~AddressInitHolder will all Acquire, which is a no-op on
// the underlying pointer but sets m_acquired to TRUE. Thus, when pMyType goes
// out of scope, ReleaseHolder will cause a double-release of pMyType.
//
// By calling Clear, then the call to Acquire in the dtor will not set m_acquired
// to true since IsNull(m_holder.m_value) will return true.
//
m_holder.Clear();
}
FORCEINLINE ~AddressInitHolder()
{
m_holder.Acquire();
}
// It's not optimal to have to declare these casting operators. But if we don't,
// people cannot cast the result of &holder to these values. (The C++ compiler won't
// automatically use the TYPE * cast as an intermediate value.) So we put them here,
// rather than forcing callers to double cast in these common cases.
FORCEINLINE operator IUnknown **()
{
IUnknown *unknown;
// Typesafe check. This will fail at compile time if
// m_holder.m_value can't be converted to an IUnknown *.
unknown = static_cast<IUnknown*>(m_holder.m_value);
// do the cast with an unsafe cast
return (IUnknown **)(&m_holder.m_value);
}
#if defined(FEATURE_COMINTEROP) && !defined(STRIKE)
FORCEINLINE operator IInspectable **()
{
IInspectable *inspectable;
// Typesafe check. This will fail at compile time if
// m_holder.m_value can't be converted to an IInspectable *.
inspectable = static_cast<IInspectable *>(m_holder.m_value);
return (IInspectable **)(&m_holder.m_value);
}
#endif // FEATURE_COMINTEROP
FORCEINLINE operator void **()
{
return (void **)(&m_holder.m_value);
}
FORCEINLINE operator void *()
{
return (void *)(&m_holder.m_value);
}
};
// Separate out method with TYPE * cast operator, since it may clash with IUnknown ** or
// void ** cast operator.
friend class TypedAddressInitHolder;
class TypedAddressInitHolder : public AddressInitHolder
{
public:
FORCEINLINE TypedAddressInitHolder(BaseWrapper & holder)
: AddressInitHolder(holder)
{
}
FORCEINLINE operator TYPE *()
{
return static_cast<TYPE *>(&this->m_holder.m_value);
}
};
#ifdef __GNUC__
//#pragma GCC visibility pop
#endif // __GNUC__
public:
FORCEINLINE BaseWrapper()
: BaseT(TYPE(DEFAULTVALUE), FALSE)
{
}
FORCEINLINE BaseWrapper(TYPE value)
: BaseT(value)
{
}
FORCEINLINE BaseWrapper(TYPE value, BOOL take)
: BaseT(value, take)
{
}
FORCEINLINE BaseWrapper& operator=(TYPE value)
{
BaseT::Assign(value);
return *this;
}
FORCEINLINE operator TYPE() const
{
return this->m_value;
}
FORCEINLINE TypedAddressInitHolder operator&()
{
return TypedAddressInitHolder(*this);
}
template <typename T>
FORCEINLINE bool operator==(T const & value) const
{
return !!(this->m_value == TYPE(value));
}
template <typename T>
FORCEINLINE bool operator!=(T const & value) const
{
return !!(this->m_value != TYPE(value));
}
#ifdef __llvm__
// This handles the NULL value that is an int and clang
// doesn't want to convert int to a pointer
FORCEINLINE bool operator==(int value) const
{
return !!(this->m_value == TYPE((void*)(SIZE_T)value));
}
FORCEINLINE bool operator!=(int value) const
{
return !!(this->m_value != TYPE((void*)(SIZE_T)value));
}
#endif // __llvm__
FORCEINLINE const TYPE &operator->() const
{
return this->m_value;
}
FORCEINLINE int operator!() const
{
return this->IsNull();
}
private:
HIDE_GENERATED_METHODS(BaseWrapper)
}; // class BaseWrapper<>
//-----------------------------------------------------------------------------
// Generic templates to use to wrap up acquire/release functionality for Holder
//-----------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void DoNothing(TYPE value)
{
// @TODO: Due to prefast template problems, implementations of the DoNothing macro have been changed
// Search by prefast, and remove them when prefast is ready
}
FORCEINLINE void DoNothing()
{
}
// Prefast stuff.We should have DoNothing<type*> in the holder declaration, but currently
// prefast doesnt support, it, so im stuffing all these here so if we need to change the template you can change
// everything here. When prefast works, remove the following functions
struct ConnectionCookie;
FORCEINLINE void ConnectionCookieDoNothing(ConnectionCookie* p)
{
}
class ComCallWrapper;
FORCEINLINE void CCWHolderDoNothing(ComCallWrapper* p)
{
}
FORCEINLINE void DispParamHolderDoNothing(VARIANT* p)
{
}
FORCEINLINE void VariantPtrDoNothing(VARIANT* p)
{
}
FORCEINLINE void VariantDoNothing(VARIANT)
{
}
FORCEINLINE void ZeroDoNothing(VOID* p)
{
}
class CtxEntry;
FORCEINLINE void CtxEntryDoNothing(CtxEntry* p)
{
}
struct RCW;
FORCEINLINE void NewRCWHolderDoNothing(RCW*)
{
}
// Prefast stuff.We should have DoNothing<SafeArray*> in the holder declaration
FORCEINLINE void SafeArrayDoNothing(SAFEARRAY* p)
{
}
//-----------------------------------------------------------------------------
// Holder/Wrapper are the simplest way to define holders - they synthesizes a base class out of
// function pointers
//-----------------------------------------------------------------------------
template <typename TYPE, void (*ACQUIREF)(TYPE), void (*RELEASEF)(TYPE)>
class FunctionBase : protected HolderBase<TYPE>
{
protected:
FORCEINLINE FunctionBase(TYPE value)
: HolderBase<TYPE>(value)
{
}
FORCEINLINE void DoAcquire()
{
ACQUIREF(this->m_value);
}
void DoRelease()
{
RELEASEF(this->m_value);
}
}; // class Function<>
template
<
typename TYPE,
void (*ACQUIREF)(TYPE),
void (*RELEASEF)(TYPE),
UINT_PTR DEFAULTVALUE = 0,
BOOL IS_NULL(TYPE, TYPE) = CompareDefault<TYPE>,
// For legacy compat (see EEJitManager::WriterLockHolder), where default ctor
// causes ACQUIREF(DEFAULTVALUE), but ACQUIREF ignores the argument and
// operates on static or global value instead.
bool DEFAULT_CTOR_ACQUIRE = true
>
class Holder : public BaseHolder<TYPE, FunctionBase<TYPE, ACQUIREF, RELEASEF>,
DEFAULTVALUE, IS_NULL>
{
typedef BaseHolder<TYPE, FunctionBase<TYPE, ACQUIREF, RELEASEF>,
DEFAULTVALUE, IS_NULL> BaseT;
public:
FORCEINLINE Holder()
: BaseT(TYPE(DEFAULTVALUE), DEFAULT_CTOR_ACQUIRE)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Holder(TYPE value)
: BaseT(value)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Holder(TYPE value, BOOL takeOwnership)
: BaseT(value, takeOwnership)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Holder& operator=(TYPE p)
{
STATIC_CONTRACT_WRAPPER;
BaseT::operator=(p);
return *this;
}
HIDE_GENERATED_METHODS(Holder)
};
//---------------------------------------------------------------------------------------
//
template
<
typename TYPE,
void (*ACQUIREF)(TYPE),
void (*RELEASEF)(TYPE),
UINT_PTR DEFAULTVALUE = 0,
BOOL IS_NULL(TYPE, TYPE) = CompareDefault<TYPE>,
// For legacy compat (see EEJitManager::WriterLockHolder), where default ctor
// causes ACQUIREF(DEFAULTVALUE), but ACQUIREF ignores the argument and
// operates on static or global value instead.
bool DEFAULT_CTOR_ACQUIRE = true
>
class Wrapper : public BaseWrapper<TYPE, FunctionBase<TYPE, ACQUIREF, RELEASEF>,
DEFAULTVALUE, IS_NULL>
{
typedef BaseWrapper<TYPE, FunctionBase<TYPE, ACQUIREF, RELEASEF>,
DEFAULTVALUE, IS_NULL> BaseT;
public:
FORCEINLINE Wrapper()
: BaseT(TYPE(DEFAULTVALUE), DEFAULT_CTOR_ACQUIRE)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Wrapper(TYPE value)
: BaseT(value)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Wrapper(TYPE value, BOOL takeOwnership)
: BaseT(value, takeOwnership)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Wrapper& operator=(TYPE const & value)
{
STATIC_CONTRACT_WRAPPER;
BaseT::operator=(value);
return *this;
}
HIDE_GENERATED_METHODS(Wrapper)
}; // Wrapper<>
//---------------------------------------------------------------------------------------
// - Cannot use the standard INDEBUG macro: holder.h is used in places where INDEBUG is defined in the nonstandard way
#if defined(_DEBUG)
#define INDEBUG_AND_WINDOWS_FOR_HOLDERS(x) x
#else
#define INDEBUG_AND_WINDOWS_FOR_HOLDERS(x)
#endif
//---------------------------------------------------------------------------------------
//
// New template wrapper type macros. These save some effort when specializing
// existing holder templates. (We would rather use a construct like:
//
// template <P1>
// typedef Holder<...> NewHolder;
//
// But this construct doesn't exist in C++. These macros ease some of the cruft necessary
// to get similar functionality out of class templates.
//-----------------------------------------------------------------------------
// Dev10 VC++ has some of the new C++0x language extensions. Of particular interest here:
// rvalue references, which enables differentiation between named (lvalue) and
// temporary (rvalue) object references, enabling move semantics and perfect forwarding.
// See http://msdn.microsoft.com/en-us/library/dd293668.aspx for more information.
// Enable copy construction and assignment from temporary objects. This permits Wrapper objects
// to be returned from methods, and for move assignment.
#define NEW_WRAPPER_TEMPLATE_RVALREF_METHODS(_NAME) \
public: \
FORCEINLINE _NAME(_NAME && other) \
: BaseT(NULL, FALSE) \
{ \
STATIC_CONTRACT_WRAPPER; \
INDEBUG_AND_WINDOWS_FOR_HOLDERS(m_pvalue = &this->m_value;) \
*this = std::move(other); \
} \
FORCEINLINE _NAME& operator=(_NAME && other) \
{ \
std::swap(BaseT::m_value, other.BaseT::m_value); \
std::swap(BaseT::m_acquired, other.BaseT::m_acquired); \
return *this; \
}
#define NEW_WRAPPER_TEMPLATE1(_NAME, _RELEASEF) \
template <typename _TYPE> \
class _NAME : public Wrapper<_TYPE*, DoNothing<_TYPE*>, _RELEASEF, NULL> \
{ \
typedef Wrapper<_TYPE*, DoNothing<_TYPE*>, _RELEASEF, NULL> BaseT; \
public: \
FORCEINLINE _NAME() : BaseT(NULL, FALSE) \
{ \
STATIC_CONTRACT_WRAPPER; \
INDEBUG_AND_WINDOWS_FOR_HOLDERS(m_pvalue = &this->m_value;) \
} \
FORCEINLINE _NAME(_TYPE* value) : BaseT(value) \
{ \
STATIC_CONTRACT_WRAPPER; \
INDEBUG_AND_WINDOWS_FOR_HOLDERS(m_pvalue = &this->m_value;) \
} \
FORCEINLINE _NAME(_TYPE* value, BOOL takeOwnership) : BaseT(value, takeOwnership) \
{ \
STATIC_CONTRACT_WRAPPER; \
INDEBUG_AND_WINDOWS_FOR_HOLDERS(m_pvalue = &this->m_value;) \
} \
FORCEINLINE ~_NAME() \
{ \
} \
FORCEINLINE _NAME& operator=(_TYPE * value) \
{ \
STATIC_CONTRACT_WRAPPER; \
BaseT::operator=(value); \
return *this; \
} \
/* Since operator& is overloaded we need a way to get a type safe this pointer. */ \
FORCEINLINE _NAME* GetAddr() \
{ \
STATIC_CONTRACT_LEAF; \
return this; \
} \
NEW_WRAPPER_TEMPLATE_RVALREF_METHODS(_NAME) \
HIDE_GENERATED_METHODS(_NAME) \
private: \
/* m_ppValue: Do not use from source code: Only for convenient use from debugger */ \
/* watch windows - saves five mouseclicks when inspecting holders. */ \
INDEBUG_AND_WINDOWS_FOR_HOLDERS(_TYPE ** m_pvalue;) \
};
//-----------------------------------------------------------------------------
// NOTE: THIS IS UNSAFE TO USE IN THE VM for interop COM objects!!
// WE DO NOT CORRECTLY CHANGE TO PREEMPTIVE MODE BEFORE CALLING RELEASE!!
// USE SafeComHolder
//
// ReleaseHolder : COM Interface holder for use outside the VM (or on well known instances
// which do not need preemptive Relesae)
//
// Usage example:
//
// {
// ReleaseHolder<IFoo> foo;
// hr = FunctionToGetRefOfFoo(&foo);
// // Note ComHolder doesn't call AddRef - it assumes you already have a ref (if non-0).
// } // foo->Release() on out of scope (WITHOUT RESPECT FOR GC MODE!!)
//
//-----------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void DoTheRelease(TYPE *value)
{
if (value)
{
value->Release();
}
}
NEW_WRAPPER_TEMPLATE1(DoNothingHolder, DoNothing<_TYPE*>);
NEW_WRAPPER_TEMPLATE1(ReleaseHolder, DoTheRelease<_TYPE>);
NEW_WRAPPER_TEMPLATE1(NonVMComHolder, DoTheRelease<_TYPE>);
//-----------------------------------------------------------------------------
// StubHolder : holder for stubs
//
// Usage example:
//
// {
// StubHolder<Stub> foo;
// foo = new Stub();
// foo->AddRef();
// // Note StubHolder doesn't call AddRef for you.
// } // foo->DecRef() on out of scope
//
//-----------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void StubRelease(TYPE* value)
{
if (value)
value->DecRef();
}
NEW_WRAPPER_TEMPLATE1(StubHolder, StubRelease<_TYPE>);
//-----------------------------------------------------------------------------
// CoTaskMemHolder : CoTaskMemAlloc allocated memory holder
//
// {
// CoTaskMemHolder<Foo> foo = (Foo*) CoTaskMemAlloc(sizeof(Foo));
// } // delete foo on out of scope
//-----------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void DeleteCoTaskMem(TYPE *value)
{
if (value)
CoTaskMemFree(value);
}
NEW_WRAPPER_TEMPLATE1(CoTaskMemHolder, DeleteCoTaskMem<_TYPE>);
//-----------------------------------------------------------------------------
// NewHolder : New'ed memory holder
//
// {
// NewHolder<Foo> foo = new Foo ();
// } // delete foo on out of scope
//-----------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void Delete(TYPE *value)
{
STATIC_CONTRACT_LEAF;
static_assert(!std::is_same<typename std::remove_cv<TYPE>::type, WCHAR>::value,
"Must use NewArrayHolder (not NewHolder) for strings.");
static_assert(!std::is_same<typename std::remove_cv<TYPE>::type, CHAR>::value,
"Must use NewArrayHolder (not NewHolder) for strings.");
delete value;
}
NEW_WRAPPER_TEMPLATE1(NewHolder, Delete<_TYPE>);
//-----------------------------------------------------------------------------
// NewExecutableHolder : New'ed memory holder for executable memory.
//
// {
// NewExecutableHolder<Foo> foo = (Foo*) new (executable) Byte[num];
// } // delete foo on out of scope
//-----------------------------------------------------------------------------
// IJW
template<class T> void DeleteExecutable(T *p);
NEW_WRAPPER_TEMPLATE1(NewExecutableHolder, DeleteExecutable<_TYPE>);
//-----------------------------------------------------------------------------
// NewArrayHolder : New []'ed pointer holder
// {
// NewArrayHolder<Foo> foo = new Foo [30];
// } // delete [] foo on out of scope
//-----------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void DeleteArray(TYPE *value)
{
STATIC_CONTRACT_WRAPPER;
delete [] value;
value = NULL;
}
NEW_WRAPPER_TEMPLATE1(NewArrayHolder, DeleteArray<_TYPE>);
typedef NewArrayHolder<CHAR> AStringHolder;
typedef NewArrayHolder<WCHAR> WStringHolder;
//-----------------------------------------------------------------------------
// A special array holder that expects its contents are interface pointers,
// and will call Release() on them.
//
// NOTE: You may ONLY use this if you've determined that it is SAFE to call
// Release() on the contained interface pointers (e.g., as opposed to SafeRelease)
//
template <typename INTERFACE>
class NewInterfaceArrayHolder : public NewArrayHolder<INTERFACE *>
{
public:
NewInterfaceArrayHolder() :
NewArrayHolder<INTERFACE *>(),
m_cElements(0)
{
STATIC_CONTRACT_WRAPPER;
}
NewInterfaceArrayHolder& operator=(INTERFACE ** value)
{
STATIC_CONTRACT_WRAPPER;
NewArrayHolder<INTERFACE *>::operator=(value);
return *this;
}
void SetElementCount(ULONG32 cElements)
{
STATIC_CONTRACT_LEAF;
m_cElements = cElements;
}
~NewInterfaceArrayHolder()
{
STATIC_CONTRACT_LEAF;
for (ULONG32 i=0; i < m_cElements; i++)
{
if (this->m_value[i] != NULL)
this->m_value[i]->Release();
}
}
protected:
ULONG32 m_cElements;
};
//-----------------------------------------------------------------------------
// ResetPointerHolder : pointer which needs to be set to NULL
// {
// ResetPointerHolder<Foo> holder = &pFoo;
// } // "*pFoo=NULL" on out of scope
//-----------------------------------------------------------------------------
#ifdef __GNUC__
// With -fvisibility-inlines-hidden, the Invoke methods below
// get hidden, which causes warnings when visible classes expose them.
#define VISIBLE __attribute__ ((visibility("default")))
#else
#define VISIBLE
#endif // __GNUC__
namespace detail
{
template <typename T>
struct ZeroMem
{
static VISIBLE void Invoke(T * pVal)
{
ZeroMemory(pVal, sizeof(T));
}
};
template <typename T>
struct ZeroMem<T*>
{
static VISIBLE void Invoke(T ** pVal)
{
*pVal = NULL;
}
};
}
#undef VISIBLE
NEW_WRAPPER_TEMPLATE1(ResetPointerHolder, detail::ZeroMem<_TYPE>::Invoke);
NEW_WRAPPER_TEMPLATE1(FieldNuller, detail::ZeroMem<_TYPE>::Invoke);
//-----------------------------------------------------------------------------
// Wrap win32 functions using HANDLE
//-----------------------------------------------------------------------------
FORCEINLINE void VoidCloseHandle(HANDLE h) { if (h != NULL) CloseHandle(h); }
// (UINT_PTR) -1 is INVALID_HANDLE_VALUE
FORCEINLINE void VoidCloseFileHandle(HANDLE h) { if (h != ((HANDLE)((LONG_PTR) -1))) CloseHandle(h); }
FORCEINLINE void VoidFindClose(HANDLE h) { FindClose(h); }
FORCEINLINE void VoidUnmapViewOfFile(void *ptr) { UnmapViewOfFile(ptr); }
template <typename TYPE>
FORCEINLINE void TypeUnmapViewOfFile(TYPE *ptr) { UnmapViewOfFile(ptr); }
// (UINT_PTR) -1 is INVALID_HANDLE_VALUE
//@TODO: Dangerous default value. Some Win32 functions return INVALID_HANDLE_VALUE, some return NULL (such as CreatEvent).
typedef Wrapper<HANDLE, DoNothing<HANDLE>, VoidCloseHandle, (UINT_PTR) -1> HandleHolder;
typedef Wrapper<HANDLE, DoNothing<HANDLE>, VoidCloseFileHandle, (UINT_PTR) -1> FileHandleHolder;
typedef Wrapper<HANDLE, DoNothing<HANDLE>, VoidFindClose, (UINT_PTR) -1> FindHandleHolder;
typedef Wrapper<void *, DoNothing, VoidUnmapViewOfFile> MapViewHolder;
#ifdef WszDeleteFile
// Deletes a file with the specified path. Do not use if you care about failures
// deleting the file, as failures are ignored by VoidDeleteFile.
FORCEINLINE void VoidDeleteFile(LPCWSTR wszFilePath) { WszDeleteFile(wszFilePath); }
typedef Wrapper<LPCWSTR, DoNothing<LPCWSTR>, VoidDeleteFile, NULL> DeleteFileHolder;
#endif // WszDeleteFile
//-----------------------------------------------------------------------------
// Misc holders
//-----------------------------------------------------------------------------
// A holder for HMODULE.
FORCEINLINE void HolderFreeLibrary(HMODULE h) { FreeLibrary(h); }
typedef Wrapper<HMODULE, DoNothing<HMODULE>, HolderFreeLibrary, NULL> HModuleHolder;
template <typename T> FORCEINLINE
void DoLocalFree(T* pMem) { (LocalFree)((HLOCAL)pMem); }
NEW_WRAPPER_TEMPLATE1(LocalAllocHolder, DoLocalFree<_TYPE>);
inline void BoolSet( _Out_ bool * val ) { *val = true; }
inline void BoolUnset( _Out_ bool * val ) { *val = false; }
typedef Wrapper< bool *, BoolSet, BoolUnset > BoolFlagStateHolder;
//
// We need the following methods to have volatile arguments, so that they can accept
// raw pointers in addition to the results of the & operator on Volatile<T>.
//
FORCEINLINE void CounterIncrease(RAW_KEYWORD(volatile) LONG* p) {InterlockedIncrement(p);};
FORCEINLINE void CounterDecrease(RAW_KEYWORD(volatile) LONG* p) {InterlockedDecrement(p);};
typedef Wrapper<RAW_KEYWORD(volatile) LONG*, CounterIncrease, CounterDecrease, (UINT_PTR)0, CompareDefault<RAW_KEYWORD(volatile) LONG*>> CounterHolder;
#ifndef FEATURE_PAL
FORCEINLINE void RegKeyRelease(HKEY k) {RegCloseKey(k);};
typedef Wrapper<HKEY,DoNothing,RegKeyRelease> RegKeyHolder;
#endif // !FEATURE_PAL
class ErrorModeHolder
{
UINT m_oldMode;
public:
ErrorModeHolder(UINT newMode){m_oldMode=SetErrorMode(newMode);};
~ErrorModeHolder(){SetErrorMode(m_oldMode);};
UINT OldMode() {return m_oldMode;};
};
#ifndef FEATURE_PAL
//-----------------------------------------------------------------------------
// HKEYHolder : HKEY holder, Calls RegCloseKey on scope exit.
//
// {
// HKEYHolder hFoo = NULL;
// WszRegOpenKeyEx(HKEY_CLASSES_ROOT, L"Interface",0, KEY_READ, hFoo);
//
// } // close key on out of scope via RegCloseKey.
//-----------------------------------------------------------------------------
class HKEYHolder
{
public:
HKEYHolder()
{
STATIC_CONTRACT_LEAF;
m_value = 0;
}
~HKEYHolder()
{
STATIC_CONTRACT_WRAPPER;
if (m_value != NULL)
::RegCloseKey(m_value);
}
FORCEINLINE void operator=(HKEY p)
{
STATIC_CONTRACT_LEAF;
if (p != 0)
m_value = p;
}
FORCEINLINE operator HKEY()
{
STATIC_CONTRACT_LEAF;
return m_value;
}
FORCEINLINE operator HKEY*()
{
STATIC_CONTRACT_LEAF;
return &m_value;
}
FORCEINLINE HKEY* operator&()
{
STATIC_CONTRACT_LEAF;
return &m_value;
}
private:
HKEY m_value;
};
#endif // !FEATURE_PAL
//----------------------------------------------------------------------------
//
// External data access does not want certain holder implementations
// to be active as locks should not be taken and so on. Provide
// a no-op in that case.
//
//----------------------------------------------------------------------------
#ifndef DACCESS_COMPILE
#define DacHolder Holder
#else
template <typename TYPE, void (*ACQUIRE)(TYPE), void (*RELEASEF)(TYPE), UINT_PTR DEFAULTVALUE = 0, BOOL IS_NULL(TYPE, TYPE) = CompareDefault<TYPE>, BOOL VALIDATE_BACKOUT_STACK = TRUE>
class DacHolder
{
protected:
TYPE m_value;
private:
BOOL m_acquired; // Have we acquired the resource?
public:
FORCEINLINE DacHolder()
: m_value(TYPE(DEFAULTVALUE)),
m_acquired(FALSE)
{
STATIC_CONTRACT_SUPPORTS_DAC;
}
// construct a new instance of DacHolder
// Arguments:
// input: value - the resource held
// take - indicates whether the lock should be taken--the default is true. See Notes:
// Note: In DAC builds, the Acquire function does not actually take the lock, instead
// it determines whether the lock is held (by the LS). If it is, the locked data
// is assumed to be inconsistent and the Acquire function will throw.
FORCEINLINE DacHolder(TYPE value, BOOL take = TRUE)
: m_value(value),
m_acquired(FALSE)
{
STATIC_CONTRACT_SUPPORTS_DAC;
if (take)
Acquire();
}
FORCEINLINE ~DacHolder()
{
STATIC_CONTRACT_SUPPORTS_DAC;
}
// Sets the value to 'value'. Doesn't call Acquire/Release if fTake is FALSE.
FORCEINLINE void Assign(TYPE value, BOOL fTake = TRUE)
{
m_value = value;
}
FORCEINLINE void Acquire()
{
STATIC_CONTRACT_SUPPORTS_DAC;
if (!IsNull())
{
m_acquired = TRUE;
// because ACQUIRE is a template argument, if the line m_acquired = TRUE is placed after the call
// where it logically belongs, the compiler flags it as "unreachable code."
ACQUIRE(this->m_value);
}
}
FORCEINLINE void Release()
{
// Insert any global or thread bookeeping here
if (m_acquired)
{
m_acquired = FALSE;
}
}
FORCEINLINE void Clear()
{
m_value = TYPE(DEFAULTVALUE);
m_acquired = FALSE;
}
FORCEINLINE void SuppressRelease()
{
m_acquired = FALSE;
}
FORCEINLINE TYPE GetValue()
{
return m_value;
}
FORCEINLINE BOOL IsNull() const
{
return IS_NULL(m_value, TYPE(DEFAULTVALUE));
}
private:
FORCEINLINE DacHolder& operator=(const Holder<TYPE, ACQUIRE, RELEASEF> &holder)
{
}
FORCEINLINE DacHolder(const Holder<TYPE, ACQUIRE, RELEASEF> &holder)
{
}
};
#endif // #ifndef DACCESS_COMPILE
// Holder-specific clr::SafeAddRef and clr::SafeRelease helper functions.
namespace clr
{
// Copied from utilcode.h. We can't include the header directly because there
// is circular reference.
// Forward declare the overload which is used by 'SafeAddRef' below.
template <typename ItfT>
static inline
typename std::enable_if< std::is_pointer<ItfT>::value, ItfT >::type
SafeAddRef(ItfT pItf);
template < typename ItfT > __checkReturn
ItfT *
SafeAddRef(ReleaseHolder<ItfT> & pItf)
{
STATIC_CONTRACT_LIMITED_METHOD;
//@TODO: Would be good to add runtime validation that the return value is used.
return SafeAddRef(pItf.GetValue());
}
namespace detail
{
template <typename T>
char IsHolderHelper(HolderBase<T>*);
int IsHolderHelper(...);
template <typename T>
struct IsHolder : public std::conditional<
sizeof(IsHolderHelper(reinterpret_cast<T*>(NULL))) == sizeof(char),
std::true_type,
std::false_type>::type
{};
}
template < typename T >
typename std::enable_if<detail::IsHolder<T>::value, ULONG>::type
SafeRelease(T& arg)
{
STATIC_CONTRACT_LIMITED_METHOD;
return arg.Release();
}
}
#endif // __HOLDER_H_
|