Use of GUID constants

KlausO via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 10 06:52:16 PST 2016


For GUIDs you often have to take the address (e.g. for calls to 
QueryInterface), so I think phobos does not correctly implement this.

In the meantime I took a look at the VisualD project which accesses the 
COM interfaces of visual studio. They solve the problem by using the 
following idiom (see 
https://github.com/D-Programming-Language/visuald/blob/master/sdk/port/servprov.d 
for an example):

const GUID IID_IServiceProvider = IServiceProvider.iid;

interface IServiceProvider : IUnknown
{
	static const GUID iid = { 0x6d5140c1, 0x7436, 0x11ce, [ 0x80, 0x34, 
0x00, 0xaa, 0x00, 0x60, 0x09, 0xfa ] };
public:
	/* [local] */ HRESULT QueryService(
		/* [in] */ in GUID* guidService,
		/* [in] */ in IID* riid,
		/* [out] */ void **ppvObject);
}

If every interface declaration contains the static iid member this 
enables you to provide something similar to the the __uuidof operator in 
VisualC (see https://msdn.microsoft.com/de-de/library/zaah6a61.aspx).

In VisualD something along that line is implemented in the qi_cast 
template (see 
https://github.com/D-Programming-Language/visuald/blob/master/stdext/com.d).


Is the above pair (const GUID and static member) the right way to 
declare this idiom or would the following be better ?
Both compile and seem to be equivalent (in terms of achieving the same 
goal):

enum IID IID_IServiceProvider = { 0x6d5140c1, 0x7436, 0x11ce, [ 0x80, 
0x34, 0x00, 0xaa, 0x00, 0x60, 0x09, 0xfa ] };

interface IServiceProvider : IUnknown
{
	static immutable GUID iid = IID_IServiceProvider;
public:
/* [local] */ HRESULT QueryService(
/* [in] */ in GUID* guidService,
/* [in] */ in IID* riid,
/* [out] */ void **ppvObject);
}

Thanks for your insights

-- KlausO


Am 10.03.2016 um 14:49 schrieb Mike Parker:
> On Thursday, 10 March 2016 at 10:16:30 UTC, KlausO wrote:
>> Ok, but what's the intention behind defining GUIDs as enums in the
>> first place ?
>
> Probably just an implementation error, i.e. someone not fully
> appreciating how GUIDs are intended to be used.
>
>> Is there a recommended way to declare/define constants (e.g. as enums
>> or consts) ?
>
> Generally, you should use a manifest constant, e.g.
>
> enum myConst = 10;
>
> Unless you need to take the address, then you should use immutable:
>
> immutable myConst = 10;
>
> The value of a manifest constant is substituted for the symbol at the
> point of use and is not stored in the data segment (so has no memory
> address), but an immutable (or const) variable is stored in the data
> segment.



More information about the Digitalmars-d-learn mailing list