Skip to content

Commit

Permalink
version 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Răzvan Ștefănescu committed Feb 20, 2021
1 parent 9dd7872 commit 66ea19a
Show file tree
Hide file tree
Showing 224 changed files with 118,582 additions and 117,972 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/bin
/.dub
/.vs
/.vs
/x64
Binary file modified .vs/wind/v16/.suo
Binary file not shown.
49 changes: 35 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,31 @@ enum : int
```

## Design choices
## Design

- Strongly typed handles. In the metadata, most of the ```HANDLE``` types are strongly typed as a ```struct``` with a single member, forcing the use of that struct as a parameter for various functions. For backward compatibility reasons, these structs are unfolded and converted to plain ```ptrdiff_t``` values. This is needed beacause corresponding constants are integers, you cannot assign or compare these constants to structs.
```
//Original metadata
struct HBITMAP
{
ptrdiff_t Value;
}
### Strongly typed handles

//D code
alias HBITMAP = ptrdiff_t;
In the metadata, most of the ```HANDLE``` types are strongly typed as a ```struct``` with a single member, forcing the use of that struct as a parameter for various functions. Also, these structs are decorated with a special attribute called ```RAIIFree``` that you can use to auto-dispose the handle.
```
@RAIIFree!DeleteObject
struct HBITMAP { ... }
```
You will usually write the following code:
```
HBITMAP bitmap = CreateBitmap(128, 128, 1, 32, null);
//use bitmap
DeleteObject(bitmap)
```
Now you can take advantage of this attribute, by calling ```autofree```.
```
auto bitmap = CreateBitmap(128, 128, 1, 32, null).autoFree;
//use bitmap
//no need to call DeleteObject, it is called automatically when ```bitmap``` will go out of scope
```

### GUID decorating.

- GUID decorating. Interfaces and several structs are decorated with GUID attributes. The old ```IID_``` constants from Windows headers are missing, therefore the generator will create them based on the name of each decorated item. The template doing this mapping (```GUIDOF```) can be found in the file ```core.d```. Conventionally, every COM interface has an associated ```IID_``` guid, any other type having an associated ```CLSID_``` guid.
Interfaces and several structs are decorated with GUID attributes. The old ```IID_``` constants from Windows headers are missing, therefore the generator will create them based on the name of each decorated item. The template doing this mapping (```GUIDOF```) can be found in the file ```core.d```. Conventionally, every COM interface has an associated ```IID_``` guid, any other type having an associated ```CLSID_``` guid.
```
@GUID("8BA5FB08-5195-40E2-AC58-0D989C3A0102")
interface ID3DBlob : IUnknown
Expand All @@ -130,19 +140,30 @@ const GUID IID_ID3DBlob = GUIDOF!ID3DBlob;
const GUID CLSID_CTraceRelogger = GUIDOF!CTraceRelogger;
```

- Library decorating. All functions are decorated with a ```@DllImport``` attribute stating the corresponding library file where the function can be found. This can be useful later for generating .lib files or for loading function using dynamic bindings.
### Library decorating

All functions are decorated with a ```@DllImport``` attribute stating the corresponding library file where the function can be found. This can be useful later for generating .lib files or for loading function using dynamic bindings.
```
@DllImport("d3d12")
HRESULT D3D12EnableExperimentalFeatures(uint NumFeatures, char* pIIDs, char* pConfigurationStructs,
char* pConfigurationStructSizes);
```

- Identifier renaming. Every type, method or field identifier is renamed by adding an underscore at the end if there is a conflict with any of existing D keywords.
### Renaming

Every type, method or field identifier is renamed by adding an underscore at the end if there is a conflict with any of existing D keywords.
```
HRESULT GetVersion(ulong* version_);
```

- Not all attributes found in the metadata have a direct translation in D language. ```Obsolete``` is translated in a corresponding ```deprecated``` attribute. ```Guid``` keeps the same semantics as explained above. ```NativeTypedef``` is used to decide where ```struct``` unfolding is necessary for strongly typed handles. ```UnmanagedFunctionPointer``` is used to decide what calling convention is used for callback functions. ```Const``` attribute is translated as a ```const``` qualifier for all fields or all parameters. The following attributes are ignored: ```RAIIIFree```, ```ComOutPtr```.
### Other attributes

Not all attributes found in the metadata have a direct translation in D language.
- ```Obsolete``` is translated in a corresponding ```deprecated``` attribute. ```Guid``` keeps the same semantics as explained above.
- ```NativeTypedef``` is used to decide where ```struct``` is in facta a strong typed handles.
- ```UnmanagedFunctionPointer``` is used to decide what calling convention is used for callback functions.
- ```Const``` attribute is translated as a ```const``` qualifier for all fields or all parameters.
The following attributes are ignored: ```NativeArrayInfo```, ```ComOutPtr```, ```RetVal```, ```NullNullTerminated```, ```NotNullTerminated```. Any suggestion about how to take advantage of them is appreciated.
```
enum : int
{
Expand Down
56 changes: 56 additions & 0 deletions cfg/core.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Written in the D programming language.
module windows.core;



struct GUID {
align(1):
uint Data1;
Expand Down Expand Up @@ -50,6 +52,7 @@ struct GUID {

}


template GUIDOF(T, A...)
{
static if (A.length == 0)
Expand All @@ -65,10 +68,63 @@ template GUIDOF(T, A...)
alias GUIDOF = GUIDOF!(T, A[1 .. $]);
}



struct DllImport
{
string libName;
}

struct RAIIFree(alias H)
{
private alias Handler = H;
}

auto autoFree(T)(T handle)
{
return RAIIWrapper!(T, FreeFunctionOf!T)(handle);
}

struct RAIIWrapper(Handle, alias CloseHandler)
{

Handle s;
alias s this;
@disable this();
@disable this(this);
this(Handle value)
{
this.s = value;
}
~this()
{
CloseHandler(s);
}
}


private template FreeFunctionOf(T, A...)
{
static if (A.length == 0)
{
alias attrs = __traits(getAttributes, T);
static assert(attrs.length > 0, T.stringof ~ " doesn't have any attribute attached to it");
alias FreeFunctionOf = FreeFunctionOf!(T, attrs);
}
else static if (A.length == 1)
{
static assert(isFreeHandler!(T, A[0]), T.stringof ~ " doesn't have a correct @RAIIFree attribute attached to it");
alias FreeFunctionOf = A[0].Handler;
}
else static if (isFreeHandler!(T, A[0]))
alias FreeFunctionOf = A[0].Handler;
else
alias FreeFunctionOf = FreeFunctionOf!(T, A[1 .. $]);
}

private template isFreeHandler(T, alias A)
{
enum isFreeHandler = is(typeof(A.Handler))
&& is(typeof(A.Handler(T.init)));
}

10 changes: 0 additions & 10 deletions cfg/dub.json

This file was deleted.

36 changes: 26 additions & 10 deletions dub.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
{
"name": "windows-d",
"description": "The D Windows SDK projection",
"name": "windows-d",
"description": "The D Windows SDK projections",
"authors": ["Răzvan Ștefănescu"],
"license": "GPL-2.0",
"targetType": "executable",
"targetName": "wind",
"targetPath": "bin",
"sourcePaths": ["src"]
"license": "BSL-1.0",
"targetType": "none",
"dependencies": {
"windows-d:wind" : "*",
"windows-d:windows": "*"
},
"subPackages": [
{
"name": "wind",
"description": "The D Windows SDK projection generator",
"targetName": "wind",
"targetType": "executable",
"targetPath": "bin",
"sourcePaths": ["src"]
},
{
"name": "windows",
"description": "The D Windows SDK projection library",
"targetType": "staticLibrary",
"targetName": "windows",
"targetPath": "bin",
"sourcePaths": ["out/windows"]
}
]
}

//example: winmd --meta ./meta/Windows.Win32.winmd --out .\out --core ./cfg/core.d --replace ./cfg/replace.cfg --ignore ./cfg/ignore.cfg

5 changes: 0 additions & 5 deletions dub.selections.json

This file was deleted.

Binary file modified meta/Windows.Win32.winmd
Binary file not shown.
10 changes: 0 additions & 10 deletions out/dub.json

This file was deleted.

Loading

0 comments on commit 66ea19a

Please sign in to comment.