Source

swish_comet / include / comet / ptr.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
 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
/** \file
  * Provides the com_ptr type.
  */
/*
 * Copyright Š 2000-2002 Sofus Mortensen, Michael Geddes
 *
 * This material is provided "as is", with absolutely no warranty
 * expressed or implied. Any use is at your own risk. Permission to
 * use or copy this software for any purpose is hereby granted without
 * fee, provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is
 * granted, provided the above notices are retained, and a notice that
 * the code was modified is included with the above copyright notice.
 *
 * This header is part of comet.
 * http://www.lambdasoft.dk/comet
 */
/*! \addtogroup COMType
 */
//@{

/** \page cometcomptr Comet com_ptr
 * \section cometcomptroverview Overview
 * The \link comet::com_ptr com_ptr \endlink type is essentially a reference
 * counting wrapper for objects that support AddRef / Release in a way that
 * is STL container compatible.
 *
 * It also provides a mechanism for doing casting (QueryInterface) assignments
 * as well as for doing assignment-compatible assignments and for providing
 * smart wrappers for the interfaces.
 *
 * \section cometcomptrwrapping What Can Be Wrapped?
 *
 * The com_ptr has been designed to allow most classes to be wrapped, specifically
 * it allows for wrapping ::IUnknown based interfaces, and also <b>coclass</b> implementations.
 *
 * It should be noted that some coclasses can have two implementations of
 * ::IUnknown (aggregateable coclasses being a prime example).  These classes
 * support \link comet::implement_qi::get_unknown get_unknown() \endlink which allows
 * the com_ptr to get a the unknown responsible for the lifetime management of
 * the object.
 *
 * \section cometcomptrassign Assigning Pointers
 *
 * There are three ways of assigning and constructing com_ptr objects, depending on your
 * objective; direct assignment and the two cast operators com_cast and try_cast.
 *
 * \subsection cometcomptrassigndirect Direct Assignment

 * The first way is trivial, but quite important and is the simple
 * assignment of one com_ptr to another.  This is done in such a way as to make
 * it possible to assign assignment compatible interfaces (eg an ::IUnknown pointer can be
 * assigned from an ::IDispatch pointer).  A compile-time error will occur if
 * the pointers are not compatible.
 *
 * Unfortunately there are certain circumstances that MSVC6 compiler does not
 * provide a complete enough template instantiation backtrace in order to be
 * able to directly work out an offending assignment.  Our appologies, but a
 * decent compiler would be more accomodating!
 *
 * \subsection cometcomptrassignquery Casting Assignment
 *
 * To cause a COM cast or ::QueryInterface to happen, you need to use the
 * wrapper functions com_cast and try_cast.  The cause a QueryInterface on
 * both assignment and construction, allowing you the choice between having
 * an exception thrown on failure (try_cast) or to silently fail leaving the
 * object NULL (com_cast).
 *
 * \code

			com_ptr<IViewObject> viewobj = com_cast( obj ); // <-- non-throwing query-interface
			if (viewobj.is_null())
			{
				com_ptr<IViewObject2> viewobj2 = try_cast( obj ); // <-- throwing query-interface
				viewobj = viewobj2;	// <-- Assignment between assignment compatible types.
			}
 * \endcode

 * This shows the three different ways of assigning.  The first uses <b>com_cast</b>
 * that causes the assignment to do a QueryInterface, but does not throw an
 * error.  The second uses <b>try_cast</b> that again causes the assignment to
 * do a QueryInterface, but causes errors to be thrown.  The third assignment is
 * for assignment compatible objects only, and will cause a compiler-error
 * unless (in this case) IViewObject2 inherits off IViewObject (which it does).</p>
 *
 * \subsection cometcomptrassignvariant  Assignment From a variant_t
 *
 * Assignment to a com_ptr from a variant_t must be done by either com_cast or
 * try_cast, as any assignment from a variant is effectively a cast.
 *
 * \section cometcomptrsmartwrapper Smart Wrappers
 *
 * The com_ptr \link comet::com_ptr::operator-> operator-> \endlink provides wrapped
 * access to the methods on the interface.  For interfaces that have been
 * generated by tlb2h, the interface returned is a wrapper that allows access
 * only to the wrapped methods, otherwise, access to the raw interface is
 * default.
 *
 * The design of the wrappers allows for system interfaces to be wrapped as
 * well.  There is an \link comet::com_ptr< ::IDispatch> IDispatch wrapper \endlink
 * defined in comet/dispatch.h that provides methods to call functions and
 * property accessors by name or by dispatch id.
 *
 * There is also a wrapper for the API TypeLibrary information interfaces
 * defined in comet/tlbinfo.h.  In addition to providing some simple wrappers
 * to hide the raw COM types, it also provides accessor wrappers to the structs
 * that are allocated/deallocated by the interfaces.
 *
 * \subsection cometcomptrsmartwrapperdetail Implementation
 * It is <b>not</b> necessary to know how the wrappers are implemented in order
 * to use them, however the technique is quite interesting.
 *
 * They key to the wrappers is template specialisation.  The operator->
 * returns the 'this' pointer with a <kbd>reinterpret_cast</kbd> to a wrap_t
 * struct instantiated to the interface contained by the com_ptr.  The default
 * wrap_t template definition is to inherit from the interface, thus giving
 * direct access to methods.
 *
 * The tlb2h generated headers (as well as comet/tlbinfo.h and
 * a0omet/dispatch.h) provide alternate specialisations for wrap_t that wrap the
 * arguments and call the real methods (which would be Invoke if it is an dispinterface)
 * by using reinterpret_cast to cast back to the original interface.
 *
 * \section cometcomptraccess Raw COM Access
 * Whether calling raw COM interfaces, or trying to understand how the wrappers
 * work, you will come across the \ref cometrawcomaccess methods.  These are standard
 * across the types (see \ref cometrawcomaccess), however com_ptr classes also
 * have a raw() method that is equivalent to get() and proivdes access to the
 * raw COM methods.
 */
 //@}

#ifndef COMET_PTR_H
#define COMET_PTR_H

#include <comet/config.h>

#include <algorithm>

#include <comet/error_fwd.h>
#include <comet/typelist.h>
#include <comet/interface.h>
#include <comet/common.h>
#include <comet/uuid_fwd.h>

// Primary namespace for comet
namespace comet {
	class uuid_t;

	template<typename Itf> struct wrap_t : public Itf {};

	template<> struct wrap_t< ::IUnknown> {};

	template<typename Itf> Itf* raw( wrap_t<Itf>* p ) { return reinterpret_cast<Itf*>(p); }

//	template<typename Itf> struct prop_wrapper : public Itf {};

	// Forward declaration
	class variant_t;
	template<typename Itf> class com_ptr;
	class identity_ptr;
	namespace thread_model{
		enum thread_model_t;
	}
	template<typename T, enum thread_model::thread_model_t TM, COMET_LIST_TEMPLATE_0 > struct coclass;
	template<class C> struct aggregate_inner_unknown ;

	// comet implementation details
	namespace impl {

		template<typename Itf> class com_cast_t {
			public:
				explicit com_cast_t(Itf* p) : ptr_(p) {};
				Itf* get() const { return ptr_; }
			private:
				Itf* ptr_;
				com_cast_t& operator=(const com_cast_t&);
		};

		template<> class com_cast_t<variant_t> {
		public:
			explicit com_cast_t(const variant_t& v) : val_(v) {};
			const variant_t& get() const { return val_; }
		private:
			const variant_t& val_;
			com_cast_t& operator=(const com_cast_t&);
		};

		/** Allow calling specific, known ambiguous IUnknown implementations.
		* \param B \b true Call via get_unknown. \b false Call directly
		*/
		template <bool B>
		struct IUnknown_caller
		{
			template< typename T > static inline long AddRef( T *y)
			{ return y->AddRef(); }
			template <typename T> static inline long Release( T *y)
			{ return y->Release(); }
			template <typename T> static inline HRESULT QueryInterface( T *y, REFIID iid, void  **NewIface)
			{ return y->QueryInterface(iid, (void **)NewIface);	}
		};

		template <>
		struct IUnknown_caller<true>
		{
			template<typename T> static inline long AddRef( T *y)
			{ return y->get_unknown()->AddRef(); }
			template< typename T> static inline long Release( T *y)
			{ return y->get_unknown()->Release(); }
			template <typename T> static inline HRESULT QueryInterface( T *y, REFIID iid, void  **NewIface)
			{ return y->get_unknown()->QueryInterface(iid, NewIface); }
		};
//		template<typename ITF_LIST> struct implement_internal_qi;

		/** Namespace for dummy functions for allowing IUnknown choice to be
		* deferred to the get_unknown() function.
		*/
		namespace unknown_choice
		{
//			template<typename T, enum thread_model::thread_model_t TM> long dummy_( coclass<T,TM> *);
//			template<typename ITF_LIST> long dummy_(implement_internal_qi<ITF_LIST> *);
			template<class C> long dummy_(aggregate_inner_unknown<C> *);
			char dummy_( ... );
		}

		/** \internal
		*/
		template <typename C>
		struct iunknown_chooser
		{

			enum { matches = (sizeof( unknown_choice::dummy_( static_cast< C *>(0) ))==sizeof(long)) };
		};

	}
	/*! \addtogroup COMType
	 */
	//@{

	//! Cast com_ptr.
	/*! Allows QueryInterface when casting to different com_ptr type.
		\code
			com_ptr<IFoo> foo;
			com_ptr<IBar> bar;
			bar = com_cast(foo);
			if (!bar.is_null()) {
				// Cast is ok.
				bar->DoTheThing();
			}
		\endcode
	 * \param t com_ptr to cast
	 * \relates com_ptr
	*/
	template<typename Itf> inline impl::com_cast_t<Itf> com_cast(const com_ptr<Itf>& t) { return impl::com_cast_t<Itf>(t.get()); }
	template<typename Itf> inline impl::com_cast_t<Itf> com_cast(Itf* t) { return impl::com_cast_t<Itf>(t); }
	inline impl::com_cast_t<variant_t> com_cast(const variant_t& v) { return impl::com_cast_t<variant_t>(v); }

	namespace impl {
		template<typename Itf> class try_cast_t {
			public:
				explicit try_cast_t(Itf* p) : ptr_(p) {};
				Itf* get() const { return ptr_; }
			private:
				Itf* ptr_;
				try_cast_t& operator=(const try_cast_t&);
		};

		template<> class try_cast_t<variant_t> {
		public:
			explicit try_cast_t(const variant_t& v) : val_(v) {};
			const variant_t& get() const { return val_; }
		private:
			const variant_t& val_;
			try_cast_t& operator=(const try_cast_t&);
		};
	}


	namespace impl {

		//!  IUnknown wrapper.
		/** Hides the members of IUnknown.
		 * \relates com_ptr
		*/
		template<typename Itf>	class safe_interface : public Itf {
		private:
			HRESULT __stdcall QueryInterface(REFIID, void**);
			ULONG __stdcall AddRef();
			ULONG __stdcall Release();
		};

	}

	//! Represents the identity Unknown of an object.
	/** Is the only really efficient and safe way of representing an object for
	 *  comparisons.
	 */
	class identity_ptr
	{
		public:
			//! Default constructor.
			/** Initialises the pointer to Null.
			 */
			identity_ptr() throw() : ptr_(NULL){}
			//! Destructor.
			/** Releases the pointer.
			 */
			~identity_ptr() throw() { release();}

			//! Copy constructor.
			/*! Simple pointer copy.  They are both already identity unknowns.
			 */
			identity_ptr( const identity_ptr &rhs) : ptr_(rhs.ptr_) {addref();}

			//! Constructor from Try Cast.
			/*! Used for construction from any Interface pointer.  Always QIs to
			 * guarantee this is the identity.
			 */
			template<typename Itf>
			explicit identity_ptr( const impl::try_cast_t<Itf> &x ) throw(com_error)
				: ptr_(NULL)
			{
				IUnknown *p=static_cast<IUnknown*>(x.get());
				if (p != NULL)
					p->QueryInterface(uuidof< ::IUnknown >(), reinterpret_cast<void**>(&ptr_)) | raise_exception;
			}

			//! Constructor from variant_t Try Cast.
			/*! Used for construction from a variant.  Always QIs to
			 * guarantee this is the identity.
			 */
			inline explicit identity_ptr(const impl::try_cast_t<variant_t>& v) throw(com_error);

			//! Constructor from Com Cast.
			/*! Used for construction from any Interface pointer.  Always QIs to
			 * guarantee this is the identity.
			 */
			template<typename Itf>
			explicit identity_ptr( const impl::com_cast_t<Itf> &x ) throw(com_error)
				: ptr_(NULL)
			{
				IUnknown *p=static_cast<IUnknown*>(x.get());
				if (p != NULL)
					p->QueryInterface(uuidof< ::IUnknown >(), reinterpret_cast<void**>(&ptr_));
			}
			//! Constructor from variant_t com_cast.
			/*! Used for construction from a variant.  Always QIs to
			 * guarantee this is the identity.
			 */
			inline explicit identity_ptr(const impl::com_cast_t<variant_t>& v) throw(com_error);

			//! Constructions of null pointer
			/*!
				\param null
					Only 0 is valid. Any other value will cause E_POINTER to be thrown.

				\exception com_error
					Throws E_POINTER if a non-zero value is specified.
			*/
			explicit identity_ptr(int null) throw(com_error)
				: ptr_(NULL)
			{ if(null != 0) raise_exception(E_POINTER); }

			//! Copy assignment.
			/*! Don't QI as this is definitely identity already.
			 */
			identity_ptr &operator =( const identity_ptr &rhs) throw()
			{
				identity_ptr tmp(rhs);
				swap(tmp);
				return *this;
			}

			//! Other assignment.
			/*! Always QIs. Handles other assignments that have
			 * available constructors.
			 */
			template<typename T>
			identity_ptr &operator =(const T &rhs) throw(com_error)
			{
				identity_ptr tmp(rhs);
				swap(tmp);
				return *this;
			}
			//! Null assignment
			/*!
				Only null is allowed as argument. Attempting to assign a non-zero value will result in E_POINTER (wrapped in com_error) being thrown.

				\exception com_error
					Throw E_POINTER if a non-zero value is specified.
			*/
			identity_ptr& operator=(int null) throw(com_error)
			{ if(null != 0) raise_exception(E_POINTER); release(); return *this; }

			/// Get at the raw pointer.
			IUnknown *get() const throw() { return  ptr_; }
			/// Pass to an [in] parameter.
			IUnknown *in() const throw() { return ptr_; }
			/// Get at raw interface.
			IUnknown *raw() const throw() { return ptr_; }

			//! Returns true if the wrapped pointer is null.
			bool is_null() const throw()
			{ return (ptr_ ==NULL); }

			//! Null comparison
			/*!
				Only comparison with a value of zero is allowed. Non-zero values will result
				in E_POINTER (wrapped in com_error) being thrown.

				\exception com_error
			*/
			bool operator==(int null) const
			{ if (null != 0) raise_exception(E_POINTER); return is_null(); }
			//! Null comparison
			/*!
				Only comparison with a value of zero is allowed. Non-zero values will result
				in E_POINTER (wrapped in com_error) being thrown.

				\exception com_error
			*/
			bool operator!=(int null) const
			{ if (null != 0) raise_exception(E_POINTER); return !is_null();	}

			/**! \name Pointer Comparison
			  */
			//@{
			bool operator<(const identity_ptr& x) const throw()
			{ return ptr_ < x.ptr_; }

			bool operator>(const identity_ptr& x) const throw()
			{ return ptr_ > x.ptr_; }


			bool operator<=(const identity_ptr& x) const throw()
			{ return ptr_ <= x.ptr_; }

			bool operator>=(const identity_ptr& x) const throw()
			{ return ptr_ >= x.ptr_; }

			bool operator==(const identity_ptr& x) const throw()
			{ return (ptr_ == x.ptr_); }

			bool operator!=(const identity_ptr& x) const throw()
			{ return ptr_ != x.ptr_; }
			//@}

		protected:
			IUnknown *ptr_;

			void swap(identity_ptr& x) throw()
			{ std::swap(ptr_, x.ptr_); }

			void addref() const throw()
			{ if (ptr_ != NULL) ptr_->AddRef(); }

			void release() throw()
			{ if (ptr_ != NULL) { ptr_->Release(); ptr_ = NULL; } }

		private:
			// Can't use an identity_ptr for out, inout, use com_ptr<IUnknown>
			IUnknown **inout();
			IUnknown *out();

	};

	//! Interface smart pointer
	/*! \note All members are exception safe.
	 * \sa cometcomptr
	*/
	template<typename Itf> class com_ptr
	{
	public:
		const uuid_t& iid() const throw()
		{ return uuidof<Itf>();	}

	private:
		enum{ chooser_matches_= impl::iunknown_chooser<Itf>::matches };
		typedef typename impl::IUnknown_caller< chooser_matches_ > Unknown_caller_;

		void addref() const throw()
		{ if (ptr_) Unknown_caller_::AddRef(ptr_); }

		void release() throw()
		{ if (ptr_) { Unknown_caller_::Release(ptr_); ptr_ = 0; } }

	public:
		//! Type of pointer to wrapped interface
		typedef Itf* interface_pointer;

		//! Safe interface pointer.
		/*!
			Interface pointer where the methods from IUnknown have been hidden.
		*/
#ifndef COMET_USE_RAW_WRAPPERS
		typedef wrap_t<Itf>* safe_interface_pointer;
#else
		typedef impl::safe_interface< Itf >* safe_interface_pointer;
#endif

	private:
		interface_pointer ptr_;

	public:
		//! Default constructor
		/*!
			Initialises the wrapped pointer to null.
		*/
		com_ptr() throw() : ptr_(NULL) {}

		//! Destructor
		/*!
			Calls Release() on pointer if necessary.
		*/
		~com_ptr() throw()
		{ release(); }

		//! Upcasting constructor
		/*!
			Used for upcasting interface pointer without invocation of QueryInterface.

			This contructor only allows com_ptr upcasts.

			\param x
				com_ptr to cast.
		*/

		// If you are getting:
		//        error C2440: '=' : cannot convert from 'struct ...
		//
		// Use either try_cast or com_cast to cast from one interface to another
		// example:
		//
		//    com_ptr<IFoo> p = try_cast( q );
		//
		// or
		//
		//    com_ptr<IFoo> p = com_cast( q );
		template<typename Itf2> com_ptr(const com_ptr<Itf2>& x) throw()
		{ ptr_ = (Itf2*)x.get(); addref(); }

		//! QueryInterface construction
		/*!
			Uses QueryInterface to query for interface of type Itf. If the pointer is incompatible, the pointer will be initialised to null.

			\code
			com_ptr<IFoo> foo;
			com_ptr<IBar> bar( com_cast(foo) );
			\endcode

			\param x
				com_ptr to cast
		*/
		template<typename Itf2> com_ptr(const impl::com_cast_t<Itf2>& x) throw()
		{ ptr_ = NULL; Itf2* p = x.get(); if (p != NULL) Unknown_caller_::QueryInterface(p, iid(), reinterpret_cast<void**>(&ptr_)); }
		//                                                                   ^ Note: no error checking!!!

		//! QueryInterface construction
		/*!
			Uses QueryInterface to query for interface of type Itf. Should the pointer be incompatible, com_error will be thrown.

			\code
			com_ptr<IFoo> foo = Foo::create();
			com_ptr<IBar> bar( try_cast(foo) );
			\endcode

			\param x
				com_ptr to cast

			\exception com_error
				Throws E_NOINTERFACE if cast fails.
				Throws E_POINTER if pointer is zero.
		*/
		template<typename Itf2> com_ptr(const impl::try_cast_t<Itf2>& x) throw(com_error)
			: ptr_(NULL)
		{
			Itf2* p = x.get(); if (p == NULL) return;
			Unknown_caller_::QueryInterface(p, iid(), reinterpret_cast<void**>(&ptr_)) | raise_exception;
			if (ptr_ == NULL) raise_exception(E_NOINTERFACE);
		}

		//! Copy constructor
		/*!
			\param x com_ptr to copy.
		*/
		com_ptr(const com_ptr& x) throw()
			: ptr_(x.ptr_)
		{ addref(); }

		//! Copy from identity_unknown class.
		/** Itf  must be IUnknown for this to work. Otherwise use try_cast or
		 * com_cast.
		 */
		com_ptr( const identity_ptr &x) throw()
			: ptr_(x.ptr_)
		{ addref(); }

	private:
		// If you are getting:
		//        error C2248: '=' : cannot access private member ...
		//
		// Use either try_cast or com_cast to convert a variant_t to a com_ptr.
		// example:
		//
		//    com_ptr<IFoo> p = try_cast( v );
		//
		// or
		//
		//    com_ptr<IFoo> p = com_cast( v );
		com_ptr(const variant_t&);
		com_ptr& operator=(const variant_t&);

	public:

		//! Construction from variant_t
		/*!
			This constructor does not throw. The wrapped pointer is initialised to null if the variant is incompatible.

			\param v
				Wrapped com_variant to construct com_ptr from
		*/
		com_ptr(const impl::com_cast_t<variant_t>& v) throw()
		{ create_nothrow(v.get()); }

		//! Construction from variant_t
		/*!
			Throws com_error should the variant be incompatible.

			\param v
				Wrapped com_variant to construct com_ptr from

			\exception com_error
				Throws E_NOINTERFACE if cast fails.
		*/
		com_ptr(const impl::try_cast_t<variant_t>& v) throw(com_error)
		{ create(v.get()); }

		//! Construction from a raw interface pointer
		/*!
			Calls AddRef, and thus does \em not take ownership of the pointer.

			\param p
				Interface pointer to construct com_ptr from.
		*/
		com_ptr(interface_pointer p) throw()
			: ptr_(p)
		{ addref();	}

		//! Attach construction of raw interface pointer
		/*!
			An attachment construction does not AddRef on the interface, and is said
			to take ownership of the interface pointer.

			\param p
				auto_attach wrapped pointer to construct com_ptr from.
		*/
		com_ptr(const impl::auto_attach_t<interface_pointer>& p) throw()
			: ptr_( p.get() )
		{}

		//! Construction from CLSID
		/*!
			\param clsid
				Class ID of coclass.
			\param dwClsContext
				Class context to create object in.

			\exception com_error
		*/
		explicit com_ptr(const uuid_t& clsid, DWORD dwClsContext = CLSCTX_ALL) throw(com_error)
		{ create(clsid, NULL, dwClsContext); }

		//! Construction of aggregated object from CLSID
		/*!
			\param clsid
				Class ID of coclass.
			\param outer
				Pointer to outer object.
			\param dwClsContext
				Class context to create object in.

			\exception com_error
		*/
		/*template<typename Itf2>*/ com_ptr(const uuid_t& clsid, const com_ptr< ::IUnknown>& outer, DWORD dwClsContext = CLSCTX_ALL) throw(com_error)
		{ create(clsid, outer, dwClsContext); }

		//! Constructions of null pointer
		/*!
			\param null
				Only 0 is valid. Any other value will cause E_POINTER to be thrown.

			\exception com_error
				Throws E_POINTER if a non-zero value is specified.
		*/
		explicit com_ptr(int null) throw(com_error)
			: ptr_(NULL)
		{ if(null != 0) raise_exception(E_POINTER); }

		//! Construction from ProgID
		/*!
			\param progid
				ProgID of class.
			\param dwClsContext
				Class context to create object in.

			\exception com_error
		*/
		explicit com_ptr(const wchar_t* progid, DWORD dwClsContext = CLSCTX_ALL) throw(com_error)
		{ create(progid, 0, dwClsContext); }

		//! Construction from ProgID
		/*!
			\param progid
				ProgID of class.
			\param outer
				Pointer to outer object.
			\param dwClsContext
				Class context to create object in.

			\exception com_error
		*/
		/*template<typename Itf2> */com_ptr(const wchar_t* progid, const com_ptr< ::IUnknown>& outer, DWORD dwClsContext = CLSCTX_ALL) throw(com_error)
		{ create(progid, outer, dwClsContext); }

		//! Construction using CoGetObject
		explicit com_ptr(const wchar_t* name, BIND_OPTS& bind_opts)
		{ create(name, bind_opts); }

		//! Arrow operator
		/*!
			Used to access methods and properties of the wrapped interface.

			\note The methods of IUnknown are deliberately hidden.
		*/
		safe_interface_pointer operator->() const throw(com_error)
		{ if( ptr_ == NULL) raise_exception(E_POINTER);	return get_safe_ptr(); }

		//! Assignment of raw interface pointer
		com_ptr& operator=(interface_pointer x) throw()
		{ com_ptr t(x);	swap(t); return *this; }

		//! QueryInterface assignment
		/*!
			In order to cast unrelated interfaces you must use either com_cast or try_cast.

			A com_cast that fails results in a null pointer assignment.

			\code
				com_ptr<IFoo> foo;
				com_ptr<IBar> bar;
				bar = com_cast(foo);
				if (!bar.is_null()) {
					// Cast is ok.
					bar->DoTheThing();
				}
			\endcode
		*/
		template<typename Itfx>	com_ptr& operator=(const impl::com_cast_t<Itfx>& x) throw()
		{ com_ptr t(x); swap(t); return *this; }

		//! QueryInterface assignment
		/*!
			In order to cast unrelated interfaces you must use either com_cast or try_cast.

			A try_cast that fails will throw E_NOINTERFACE wrapped in com_error.

			\code
				com_ptr<IFoo> foo;
				com_ptr<IBar> bar;
				try {
					bar = try_cast(foo);
					bar->DoTheThing();
				} catch (com_error&) {
					// Cast didn't work.
				}
			\endcode

			\exception com_error
				Throws E_NOINTERFACE if cast fails.
		*/
		template<typename Itf2> com_ptr& operator=(const impl::try_cast_t<Itf2>& x) throw(com_error)
		{ com_ptr t(x); swap(t); return *this; }

		template<typename Itf2>
		com_ptr& operator=(const com_ptr<Itf2>& x) throw()
		{ com_ptr t(x);	swap(t); return *this; }

		//! Default assigment operator.
		com_ptr& operator=(const com_ptr& x) throw()
		{ com_ptr t(x);	swap(t); return *this; }

		//! Null assignment
		/*!
			Only null is allowed as argument. Attempting to assign a non-zero value will result in E_POINTER (wrapped in com_error) being thrown.

			\exception com_error
				Throw E_POINTER if a non-zero value is specified.
		*/
		com_ptr& operator=(int null) throw(com_error)
		{ if(null != 0) raise_exception(E_POINTER); release(); return *this; }

		//! Attaching assignment
		/*!
			Attaches a raw interface pointer to the com_ptr.

			\code
				com_ptr<IFoo> foo;
				foo = auto_attach( raw_foo_pointer );
			\endcode
		*/
		com_ptr& operator=(const impl::auto_attach_t<interface_pointer>& p) throw()
		{ release(); ptr_ = p.get(); return *this; }

	private:
		template<typename Itf2>
		int compare_unknown(const com_ptr<Itf2>& x) const throw(com_error)
		{
			if ( (void *)ptr_ ==  (void *)x.get()) return 0;
			::IUnknown* p1 = 0;
			::IUnknown* p2 = 0;

			if (ptr_ != NULL) {
				Unknown_caller_::QueryInterface(ptr_,uuidof< ::IUnknown >(), reinterpret_cast<void**>(&p1)) | raise_exception;
				p1->Release();
			}

			if (x.get()) {
				Unknown_caller_::QueryInterface(x.get(), uuidof< ::IUnknown >(), reinterpret_cast<void**>(&p2)) | raise_exception;
				p2->Release();
			}

			return p1 - p2;
		}
		int compare_unknown(const identity_ptr& x) const throw(com_error)
		{
			if (ptr_ == x.get()) return 0;
			::IUnknown* p1 = 0;
            ::IUnknown* p2 = 0;

			if (ptr_ != NULL) {
				Unknown_caller_::QueryInterface(x.get(), uuidof< ::IUnknown >(), reinterpret_cast<void**>(&p2)) | raise_exception;
				p2->Release();
			}

			return p1 - p2;
		}
	public:
		/**! \name Pointer Comparison
		  */
		//@{
		template<typename T> bool operator<(const T& x) const throw(com_error)
		{ return compare_unknown(x) < 0; }

		template<typename T> bool operator>(const T& x) const throw(com_error)
		{ return compare_unknown(x) > 0; }

		template<typename T> bool operator<=(const T& x) const throw(com_error)
		{ return compare_unknown(x) <= 0; }

		template<typename T> bool operator>=(const T& x) const throw(com_error)
		{ return compare_unknown(x) >= 0; }

		/** Object equality.
		 */
		template<typename Itf2> bool operator==(const com_ptr<Itf2>& x) const throw(com_error)
		{ return compare_unknown(x) == 0; }

		template<typename Itf2> bool operator!=(const com_ptr<Itf2>& x) const throw(com_error)
		{ return compare_unknown(x) != 0; }

		//@}

		//! Returns true if the wrapped pointer is null.
		bool is_null() const throw()
		{ return (ptr_ ==NULL); }

		//! Returns true if the wrapped pointer is null.
		bool operator!() const throw()
		{ return is_null(); }

		//! Null comparison
		/*!
			Only comparison with a value of zero is allowed. Non-zero values will result
			in E_POINTER (wrapped in com_error) being thrown.

			\exception com_error
		*/
		bool operator==(int null) const
		{ if (null != 0) raise_exception(E_POINTER); return is_null(); }

		//! Null comparison
		/*!
			Only comparison with a value of zero is allowed. Non-zero values will result
			in E_POINTER (wrapped in com_error) being thrown.

			\exception com_error
		*/
		bool operator!=(int null) const
		{ if (null != 0) raise_exception(E_POINTER); return !is_null();	}

		//! Pointer comparison
		/*!
		   Returns true if the two pointers are the same.
		  */
		template<typename Itf2> inline bool same_pointer( const com_ptr<Itf2> &x) const throw(com_error)
		{ return (IUnknown *) ptr_ ==  (IUnknown *)x.ptr_; }

		//! Swap operation
		/*!
			This method is very fast, since it does not call AddRef or Release.
		*/
		void swap(com_ptr& x) throw()
		{ std::swap(ptr_, x.ptr_); }

		//! Detaches ownership.
		/*!
			Detaches the pointer from the wrapper and returns it as raw pointer.

			This method is primarily for use by the interface wrapper code. You should very seldomly find use for it.
		*/
		interface_pointer detach() throw()
		{ interface_pointer t = ptr_; ptr_ = 0;	return t; }

		//! Detaches ownership
		/*!
		*/
		static interface_pointer detach(com_ptr& x) throw()
		{ return x.detach(); }

		/**! Create a const reference to a pointer without invoking reference-counting.
		  * Since neither constructor or destructor get called.
		  */
		static const com_ptr& create_const_reference(const interface_pointer& x)
		{ return *reinterpret_cast<const com_ptr*>(&x);	}

		/**! Create a reference to a pointer without invoking reference-counting.
		  * Since neither constructor or destructor get called.
		  */
		static com_ptr& create_reference(interface_pointer& x)
		{ return *reinterpret_cast<com_ptr*>(&x);	}

		//! \name Adapter methods.
		//@{

		//! [in] adapter
		/*!
			Used when calling raw interfaces that require an [in] IXXX * argument.

			\code
				com_ptr<IFoo> foo;
				HRESULT hr = pRawInterface->raw_Method(foo.in());
			\endcode

			Only use this wrapper when forced to deal with raw interface.
		*/
		interface_pointer in() const throw()
		{ return ptr_; }

		interface_pointer raw() const throw()
		{ return ptr_; }

		interface_pointer get() const throw()
		{ return ptr_; }

		//! [out] adapter.
		/*!
			Used when calling raw interfaces that require an [out] IXXX ** argument.

			\code
				com_ptr<IFoo> foo;
				HRESULT hr = pRawInterface->raw_MethodThatReturnsPtr(foo.out());
			\endcode

			Only use this wrapper when forced to deal with raw interface.
		*/
		interface_pointer* out() throw()
		{ release(); return &ptr_; }

		//! [in, out] adapter.
		/*!
			Used when calling raw interfaces that require an [in, out] IXXX ** argument.

			\code
				com_ptr<IFoo> foo;
				HRESULT hr = pRawInterface->raw_MethodThatChangesPtr(foo.inout());
			\endcode

			Only use this wrapper when forced to deal with raw interface.
		*/
		interface_pointer* inout() throw()
		{ return &ptr_; }

		//@}

	private:
		class bool_tester
		{ void operator delete(void*); };

	public:
		operator bool_tester*() const throw()
		{ if (is_null()) return 0; static bool_tester test;	return &test; }

	private:
	   safe_interface_pointer get_safe_ptr() const throw()
	   { return reinterpret_cast<safe_interface_pointer>(ptr_); }

	   inline void create_nothrow(const variant_t& v) throw();

	   inline void create(const variant_t& v) throw(com_error);

	   void create(const uuid_t& clsid, const com_ptr< ::IUnknown>& outer, DWORD dwClsContext = CLSCTX_ALL) throw(com_error)
	   { CoCreateInstance(clsid, outer.in(), dwClsContext, iid(), reinterpret_cast<void**>(&ptr_)) | raise_exception; }

	   void create(const wchar_t* clsidString, const com_ptr< ::IUnknown>& outer, DWORD dwClsContext = CLSCTX_ALL) throw(com_error)
	   {
		   if (clsidString == NULL) raise_exception(E_INVALIDARG);

		   CLSID clsid;

		   if (clsidString[0] == '{')
			   CLSIDFromString(const_cast<LPOLESTR>(clsidString), &clsid) | raise_exception;
		   else
			   CLSIDFromProgID(clsidString, &clsid) | raise_exception;

		   create(clsid, outer, dwClsContext);;
	   }

	   void create(const wchar_t* name, BIND_OPTS& bind_opts) throw(com_error)
	   { CoGetObject(name, &bind_opts, iid(), reinterpret_cast<void**>(&ptr_)) | raise_exception; }
	}; // class

	//! Comparison with null
	/*!
		Only comparison with a value of zero is allowed. Non-zero values will result in E_POINTER (wrapped in com_error) being thrown.

		\relates com_ptr
		\exception com_error
			Throws E_POINTER if a non-zero value is specified.
	*/
	template<typename Itf2> inline bool operator==(int null, const com_ptr<Itf2>& x) throw(com_error)
	{ if(null != 0) raise_exception(E_POINTER); return x.is_null(); }

	//! Comparison with null
	/*!
		Only comparison with a value of zero is allowed. Non-zero values will result in E_POINTER (wrapped in com_error) being thrown.

		\relates identity_ptr
		\exception com_error
			Throws E_POINTER if a non-zero value is specified.
	*/
	static inline bool operator==(int null, const identity_ptr& x) throw(com_error)
	{ if(null != 0) raise_exception(E_POINTER); return x.is_null(); }

	//! Comparison with null
	/*!
		Only comparison with a value of zero is allowed. Non-zero values will result in E_POINTER (wrapped in com_error) being thrown.

		\relates com_ptr
		\exception com_error
			Throws E_POINTER if a non-zero value is specified.
	*/
	template<typename Itf> inline bool operator!=(int null, const com_ptr<Itf>& x) throw(com_error)
	{ return x != null; }

	//! Comparison with null
	/*!
		Only comparison with a value of zero is allowed. Non-zero values will result in E_POINTER (wrapped in com_error) being thrown.

		\relates com_ptr
		\exception com_error
			Throws E_POINTER if a non-zero value is specified.
	*/
	inline static bool operator!=(int null, const identity_ptr& x) throw(com_error)
	{ if(null != 0) raise_exception(E_POINTER); return !x.is_null(); }

	namespace impl {
		template <typename Itf>
		class try_caster_t
		{
			com_ptr<Itf> ptr_;
			public:
				template <typename Itf2 >
				try_caster_t( const com_ptr<Itf2> &ptr2)
				: ptr_(try_cast(ptr2))
				{}

				const com_ptr<Itf> &get() const { return ptr_; }

		};
	}
	/*! Version of try_cast more to the style of \b dynamic_cast.
	  This is aimed at being used for casts that get used once in an environment with many interfaces.
	  \code
	 	com_ptr<IDomSession> session;
	 	com_ptr<IDomUser>   = try_cast( try_cast_ptr<ISession>(session)->User() );
	  \endcode
	  The impl::try_caster_t is implementation only - let the compiler do the
	  cast from com_ptr to impl::try_caster_t. Note that try_cast would still
	  be the preferred method for most casts.
	  \sa try_cast
	  \relates com_ptr
	 */
	template<typename Itf> inline com_ptr<Itf> try_cast_ptr( const impl::try_caster_t<Itf> &caster)
	{ return (com_ptr<Itf>)caster.get(); }

	//! Cast com_ptr.
	/*!
		Allows QueryInterface when casting to different com_ptr type.

		\code
			com_ptr<IFoo> foo;
			com_ptr<IBar> bar;
			try {
				bar = try_cast(foo);
				bar->DoTheThing();
			} catch (com_error&) {
				// Cast didn't work.
			}
		\endcode

	*/
	template<typename Itf> inline impl::try_cast_t<Itf> try_cast(const com_ptr<Itf>& t) { return impl::try_cast_t<Itf>(t.get()); }
	static inline impl::try_cast_t< ::IUnknown> try_cast(const identity_ptr& t) { return impl::try_cast_t< ::IUnknown>(t.get()); }
	template<typename Itf> inline impl::try_cast_t<Itf> try_cast(Itf* t) { return impl::try_cast_t<Itf>(t); }
	inline impl::try_cast_t<variant_t> try_cast(const variant_t& v) { return impl::try_cast_t<variant_t>(v); }
	//@}

} // namespace

#include <comet/variant.h>
#include <comet/error.h>

namespace comet {
	template<typename Itf>
	inline void com_ptr<Itf>::create_nothrow(const variant_t& v) throw()
	{
		ptr_ = 0;
		::IUnknown *pUnk;
		switch (v.get_vt()) {
		case VT_DISPATCH:
			pUnk = V_DISPATCH(&v.get());
			break;
		case VT_UNKNOWN:
			pUnk = V_UNKNOWN(&v.get());
			break;
		case VT_DISPATCH|VT_BYREF:
			pUnk = *V_DISPATCHREF(&v.get());
			break;
		case VT_UNKNOWN|VT_BYREF:
			pUnk = *V_UNKNOWNREF(&v.get());
			break;
		default:
			// don't try anything fancy.
			return;
		}
		if( pUnk ==NULL) return;
		Unknown_caller_::QueryInterface(pUnk,iid(), reinterpret_cast<void**>(&ptr_));
	}
	template<typename Itf>
	inline void com_ptr<Itf>::create(const variant_t& v) throw(com_error)
	{
		ptr_ = 0;
		::IUnknown *pUnk;
		switch (v.get_vt()) {
		case VT_DISPATCH:
			pUnk = V_DISPATCH(&v.get());
			break;
		case VT_UNKNOWN:
			pUnk = V_UNKNOWN(&v.get());
			break;
		case VT_DISPATCH|VT_BYREF:
			pUnk = *V_DISPATCHREF(&v.get());
			break;
		case VT_UNKNOWN|VT_BYREF:
			pUnk = *V_UNKNOWNREF(&v.get());
			break;
		case VT_EMPTY:
		case VT_NULL:
			pUnk = NULL;
			break;
		default:
			// don't try anything fancy.
			raise_exception( E_INVALIDARG );
		}
		if( pUnk ==NULL) return;
		Unknown_caller_::QueryInterface(pUnk,iid(), reinterpret_cast<void**>(&ptr_)) | raise_exception;
	}

	inline identity_ptr::identity_ptr(const impl::try_cast_t<variant_t>& v) throw(com_error)
		: ptr_(NULL)
	{
		VARIANT vv = v.get().get();
		::IUnknown *pUnk;
		switch (V_VT(&vv)) {
			case VT_DISPATCH:
				pUnk = V_DISPATCH(&vv);
				break;
			case VT_UNKNOWN:
				pUnk = V_UNKNOWN(&vv);
				break;
			case VT_DISPATCH|VT_BYREF:
				pUnk = *V_DISPATCHREF(&vv);
				break;
			case VT_UNKNOWN|VT_BYREF:
				pUnk = *V_UNKNOWNREF(&vv);
				break;
			case VT_EMPTY:
			case VT_NULL:
				pUnk = NULL;
				break;
			default:
				// don't try anything fancy.
				raise_exception( E_INVALIDARG );
		}
		if( pUnk ==NULL) return;
		pUnk->QueryInterface( IID_IUnknown, reinterpret_cast<void **>(&ptr_)) | raise_exception;
	}

	inline identity_ptr::identity_ptr(const impl::com_cast_t<variant_t>& v) throw(com_error)
		: ptr_(NULL)
	{
		VARIANT vv = v.get().get();
		::IUnknown *pUnk;
		switch (V_VT(&vv)) {
			case VT_DISPATCH:
				pUnk = V_DISPATCH(&vv);
				break;
			case VT_UNKNOWN:
				pUnk = V_UNKNOWN(&vv);
				break;
			case VT_DISPATCH|VT_BYREF:
				pUnk = *V_DISPATCHREF(&vv);
				break;
			case VT_UNKNOWN|VT_BYREF:
				pUnk = *V_UNKNOWNREF(&vv);
				break;
			default:
				// don't try anything fancy.
				return;
		}
		if( pUnk ==NULL) return;
		pUnk->QueryInterface( IID_IUnknown, reinterpret_cast<void **>(&ptr_));
	}
}

namespace boost {
	template<typename Itf> inline Itf* get_pointer(const comet::com_ptr<Itf>& sp)
	{
		return sp.raw();
	}
	static inline IUnknown* get_pointer(const comet::identity_ptr &sp)
	{
		return sp.raw();
	}
}

//! Macro that specialises std::swap for com_ptr.
#define COMET_SPECIALISE_STD_SWAP_ITF(Itf) namespace std { inline void swap(comet::com_ptr<Itf>& x, comet::com_ptr<Itf>& y) {x.swap(y); } }

#endif