www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Purpose of template DECLARE_HANDLE in druntime

reply Andre Pany <andre s-e-a-p.de> writes:
Hi,

I noticed in druntime this template is used often:

package template DECLARE_HANDLE(string name, base = HANDLE) {
     mixin ("alias " ~ base.stringof ~ " " ~ name ~ ";");

The disadvantage is, IDEs like IntelliJ are not able to find the 
symbols using this template e.g.
mixin DECLARE_HANDLE!("SC_HANDLE");

What is the benefit of this template?
Why can't we just use
alias HANDLE SC_HANDLE;

Kind regards
Andre
Jan 31
next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/31/19 3:51 PM, Andre Pany wrote:
 Hi,
 
 I noticed in druntime this template is used often:
 
 package template DECLARE_HANDLE(string name, base = HANDLE) {
      mixin ("alias " ~ base.stringof ~ " " ~ name ~ ";");
 
 The disadvantage is, IDEs like IntelliJ are not able to find the symbols 
 using this template e.g.
 mixin DECLARE_HANDLE!("SC_HANDLE");
 
 What is the benefit of this template?
 Why can't we just use
 alias HANDLE SC_HANDLE;
 
Most likely it's a direct translation from a C preprocessor macro. -Steve
Jan 31
prev sibling next sibling parent Seb <seb wilzba.ch> writes:
On Thursday, 31 January 2019 at 20:51:37 UTC, Andre Pany wrote:
 Hi,

 I noticed in druntime this template is used often:

 package template DECLARE_HANDLE(string name, base = HANDLE) {
     mixin ("alias " ~ base.stringof ~ " " ~ name ~ ";");

 The disadvantage is, IDEs like IntelliJ are not able to find 
 the symbols using this template e.g.
 mixin DECLARE_HANDLE!("SC_HANDLE");

 What is the benefit of this template?
 Why can't we just use
 alias HANDLE SC_HANDLE;

 Kind regards
 Andre
Have you tried changing it to alias and check whether the testsuite still passes? Druntime is a bit old, so I wouldn't be too surprised if this is an old relict.
Jan 31
prev sibling next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 31 January 2019 at 20:51:37 UTC, Andre Pany wrote:
 I noticed in druntime this template is used often:

 package template DECLARE_HANDLE(string name, base = HANDLE) {
     mixin ("alias " ~ base.stringof ~ " " ~ name ~ ";");
If my memory is correct, it is actually for old D compatibility. It used to do `version(D1) typedef, else alias`. You can see in the code right above it that there is a commented struct and a usage of library Typedef too. A little bit further up is a comment about Typedef being because of new versions of D deprecating the keyword. Old typedef fit the bill quite nicely, but it got deprecated and removed from the language. These headers used to be usable for various versions of D, and the mixin string lets you use keywords that no longer exist and centralize it a bit. I don't think it tries to support old D any more, but I just don't believe anyone has wanted to change it. Programmers don't like to remove clever indirection tricks after writing them... what if you think of some better way to do it later?!
Jan 31
prev sibling parent reply Kagamin <spam here.lot> writes:
It's a strong typed handle, in C it's declared as

#ifdef STRICT
typedef void *HANDLE;
#if 0 && (_MSC_VER > 1000)
#define DECLARE_HANDLE(name) struct name##__; typedef struct 
name##__ *name
#else
#define DECLARE_HANDLE(name) struct name##__{int unused;}; 
typedef struct name##__ *name
#endif
#else
typedef PVOID HANDLE;
#define DECLARE_HANDLE(name) typedef HANDLE name
#endif
Jan 31
parent Andre Pany <andre s-e-a-p.de> writes:
On Friday, 1 February 2019 at 07:35:34 UTC, Kagamin wrote:
 It's a strong typed handle, in C it's declared as

 #ifdef STRICT
 typedef void *HANDLE;
 #if 0 && (_MSC_VER > 1000)
 #define DECLARE_HANDLE(name) struct name##__; typedef struct 
 name##__ *name
 #else
 #define DECLARE_HANDLE(name) struct name##__{int unused;}; 
 typedef struct name##__ *name
 #endif
 #else
 typedef PVOID HANDLE;
 #define DECLARE_HANDLE(name) typedef HANDLE name
 #endif
Thanks for all answers. I understand, there was a reason in the past, but now it is a superfluous construct. I will create an issue and after that checks whether I can create a pull request. Kind regards André
Feb 01