Re: gcc -E -dD; dstep; sqlite3

2022-12-14 Thread Siarhei Siamashka via Digitalmars-d-learn

On Thursday, 8 December 2022 at 14:28:02 UTC, johannes wrote:

doing the following : (sqlite3 version 340)


gcc -E -dD sqlite3ext.h > sqlite3ext.i
dstep sqlit3ext.i -o/we/sqlite3/package.d


when compiling a program using this interface (import 
we.sqlite3), I receive Errors like :


[...]



Have you tried any of the existing sqlite3 wrapper libraries? 
https://code.dlang.org/search?q=sqlite




Re: unique_ptr | Unique for autoclose handle

2022-12-14 Thread Sergey via Digitalmars-d-learn
On Wednesday, 14 December 2022 at 11:30:07 UTC, Vitaliy Fadeev 
wrote:

Teach me the most beautiful way.
How to make beautiful?
Thanks!


Just for information there is a library that also could be 
helpful https://code.dlang.org/packages/autoptr


Re: How to use version in dub?

2022-12-14 Thread Leonardo via Digitalmars-d-learn

On Tuesday, 13 December 2022 at 20:35:28 UTC, ryuukk_ wrote:

On Tuesday, 13 December 2022 at 20:01:40 UTC, torhu wrote:

On Tuesday, 13 December 2022 at 19:50:15 UTC, torhu wrote:
On Tuesday, 13 December 2022 at 19:28:44 UTC, Leonardo A 
wrote:

Hello. How to use version in dub?

https://dlang.org/spec/version.html
"The version level and version identifier can be set on the 
command line by the -version"


I tried everything but noting.


In SDL syntax, either at the top level, in a configuration, 
or build type:


```
versions "something" "other"
```


To be more clear: When using dub you need to put this in the 
dub file, dub.sdl or dub.json. If you want to be able to 
choose from the command line, use a configuration:


```
configuration "something" {
versions "something"
}
```

Then you can do:
```
dub build -c=something
```


How can this be the official solution?

It should be as easy as dub build -v "something"


Thanks for the replies. I think definitely we need a better 
documentation of this at least. It seems to have more than one 
way to do.


I tried this at first:
```
dub build -version=USE_SDL

Error Error processing arguments: Can't parse string: bool 
should be case->> insensitive 'true' or 'false'

  Run 'dub help' for usage information.
```

Dub seems to understand my 'version' argument, but this has not 
in the help section.


And in this documentation we have more than one way to declare 
versions:

https://dub.pm/package-format-json.html#configurations

1. { "versions": ["PrintfDebugging"] }

2. like out friend said in configuration
{
"name": "somepackage",
"configurations": [
{
"name": "glut-app",
"targetType": "executable",
"versions": ["GlutApp"]
}
]
}

The -c argument refers to config build.
Maybe I'm confusing with package version.
But my thoughts about dub is that we need to have the best 
documentation as we can to provide a good understanding for 
newcomers in D language.


Re: unique_ptr | Unique for autoclose handle

2022-12-14 Thread Leonardo via Digitalmars-d-learn
On Wednesday, 14 December 2022 at 11:30:07 UTC, Vitaliy Fadeev 
wrote:

Hi! I open a device under Windows:

```
HANDLE h = CreateFileW( ... );
```

in procedure:

```
HANDLE open_keyboard_device2( LPCWSTR path, int* error_number )
{
   ...

HANDLE dev_handle =
CreateFileW(
path,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);

...

return dev_handle;
}
```

and I want to close HANDLE automatically, when **dev_handle** 
destroyed by GC:


```
CloseHandle( h );
```

How to do it?
How to define HANDLE var ?  What to return from procedure? How 
to call CloseHandle( h ) when variable destroyed?


I was trying **std.typecons.Unique**. But where I must put 
**CloseHandle( h )** ?
I was trying **std.typecons.Unique** with custom class 
**SafeHabdle**

```
class SafeHandle
{
HANDLE h;

this( HANDLE h )
{
this.h = h;
}

~this()
{
if ( h != INVALID_HANDLE_VALUE )
CloseHandle( h );
}
}
```

and using it:
```
Unique!SafeHandle open_keyboard_device2( LPCWSTR path, int* 
error_number )

{
...
Unique!SafeHandle dev_handle =
new SafeHandle(
CreateFileW(
path,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
)
);
...
}
```
It complex. Because needed:
```
Unique!SafeHandle open_keyboard_device2( LPCWSTR path, int* 
error_number )
Unique!SafeHandle dev_handle = new SafeHandle( CreateFileW( 
... ) );

DeviceIoControl( dev_handle.h, ...);
```
vs
```
HANDLE open_keyboard_device2( LPCWSTR path, int* 
error_number )

HANDLE dev_handle = CreateFileW( ... );
DeviceIoControl( dev_handle, ...);
```

Last is readable.

Teach me the most beautiful way.
How to make beautiful?
Thanks!


If you need an specific shutdown maybe you can use scopes. But 
I'm here learning too.

https://tour.dlang.org/tour/en/gems/scope-guards


Re: unique_ptr | Unique for autoclose handle

2022-12-14 Thread Vitaliy Fadeev via Digitalmars-d-learn
On Wednesday, 14 December 2022 at 11:30:07 UTC, Vitaliy Fadeev 
wrote:
How to define HANDLE var ?  What to return from procedure? How 
to call CloseHandle( h ) when variable destroyed?


I was trying **std.typecons.Unique**. But where I must put 
**CloseHandle( h )** ?
I was trying **std.typecons.Unique** with custom class 
**SafeHabdle**


Last try is:

```
struct SafeHandle
{
Unique!void _safe;
alias _safe this;

this( HANDLE h )
{
this._safe = h;
}

~this()
{
if ( cast(HANDLE)_safe !is null )
if ( cast(HANDLE)_safe != INVALID_HANDLE_VALUE )
{
if ( CloseHandle( cast(HANDLE)_safe ) == 0 )
cast(HANDLE)_safe = null;
}
}

ref HANDLE get()
{
return cast( HANDLE )_safe;
}
}

```

and using:

```
SafeHandle open_keyboard_device2( LPCWSTR path, int* error_number 
)

{
   ...
   SafeHandle dev_handle =
CreateFileW(
path,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);

if ( dev_handle.get() != INVALID_HANDLE_VALUE )
...
   ...
}

void processDevice( ... )
{
auto dev_handle = open_keyboard_device2( path, &err );
set_keyboard_indicator2( dev_handle, KEYBOARD_CAPS_LOCK_ON );
...
}
```

May be good, but testing...


Re: unique_ptr | Unique for autoclose handle

2022-12-14 Thread Ali Çehreli via Digitalmars-d-learn

On 12/14/22 05:58, Vitaliy Fadeev wrote:
> On Wednesday, 14 December 2022 at 11:30:07 UTC, Vitaliy Fadeev wrote:
>> How to define HANDLE var ?  What to return from procedure? How to call
>> CloseHandle( h ) when variable destroyed?

An obvious way is an RAII type where the destructor calls CloseHandle.

> struct SafeHandle
> {
>  Unique!void _safe;

So you made Unique a member of SafeHandle. I've never used Unique but I 
think it has a bug (or a design issue?): Its destructor is the following:


~this()
{
if (_p !is null)
{
destroy(_p);
_p = null;
}
}

Because _p is a pointer, destroy(_p) will not dereference and destroy 
what it points to. I think this is a bug with Unique. I think it should do


  destroy(*_p);

In any case, I would use a Handle RAII type that calls CloseHandle in 
its destructor. Here is the code that made sense to me:


import std;

// Some values and types to make the code compile:
alias HANDLE = void*;
alias LPCWSTR = string;
enum INVALID_HANDLE_VALUE = null;
enum FILE_SHARE_READ = 1;
enum FILE_SHARE_WRITE = 2;
enum NULL = null;
enum OPEN_EXISTING = 1000;

// Some mocks of system functions
HANDLE CreateFileW(LPCWSTR path, int, int, void*, int, int, void*) {
auto handle = cast(HANDLE)(new int(42));
writeln("Created ", handle);
return handle;
}

int CloseHandle(HANDLE handle) {
writeln("Closing ", handle);
return 0;
}

// This is the RAII type for closing system handles
struct Handle {
HANDLE value;

// Disabling copying and assignment
@disable this(this);
@disable typeof(this) opAssign(const(typeof(this)));

this(HANDLE value) {
this.value = value;
writeln("Constructed Handle with ", value);
}

~this() {
const ret = CloseHandle(value);
if (ret) {
stderr.writefln!"Failed to close handle %s"(value);
}
}
}

Handle open_keyboard_device2( LPCWSTR path, int* error_number )
{
   // ...
   HANDLE dev_handle =
CreateFileW(
path,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);

   // According to documentation, the handler must be created dynamically:
   // We make a unique owner for it:
   auto result = Handle(dev_handle);
   writeln("Exiting open_keyboard_device2");
   return result;

// if ( dev_handle.get() != INVALID_HANDLE_VALUE ) {
// // ...
// }
   // ...
}

void processDevice( ... )
{
int err;
auto dev_handle = open_keyboard_device2("foo", &err );
// set_keyboard_indicator2( dev_handle, KEYBOARD_CAPS_LOCK_ON );
// ...
writeln("Exiting processDevice");
}

void main() {
processDevice();
writeln("Exiting main");
}

The output of the program looks acceptable to me:

Created 7F1BD5A7D000
Constructed Handle with 7F1BD5A7D000
Exiting open_keyboard_device2
Exiting processDevice
Closing 7F1BD5A7D000
Exiting main

Ali



Re: unique_ptr | Unique for autoclose handle

2022-12-14 Thread Ali Çehreli via Digitalmars-d-learn

On 12/14/22 09:41, Ali Çehreli wrote:

> // According to documentation, the handler must be created 
dynamically:

> // We make a unique owner for it:

Ignore that part. It's a leftover from my experiments with Unique!Handle.

Ali



Re: How to compiler dlang code on Apple M1?

2022-12-14 Thread Sergey via Digitalmars-d-learn
On Tuesday, 13 December 2022 at 15:21:41 UTC, Steven 
Schveighoffer wrote:

On 12/13/22 10:20 AM, Steven Schveighoffer wrote:
Yeah, that's a known issue: 
https://github.com/ldc-developers/ldc/issues/3864


Try building with `-b plain` to avoid the debug build


Oh, also, I have MACOSX_DEPLOYMENT_TARGET=11 in my environment, 
that helps to avoid it as well.


-Steve


This export solves the issue (at least for me). Thanks Steven!


Re: unique_ptr | Unique for autoclose handle

2022-12-14 Thread Vitaliy Fadeev via Digitalmars-d-learn

On Wednesday, 14 December 2022 at 17:41:07 UTC, Ali Çehreli wrote:

On 12/14/22 05:58, Vitaliy Fadeev wrote:
> On Wednesday, 14 December 2022 at 11:30:07 UTC, Vitaliy
Fadeev wrote:
>> How to define HANDLE var ?  What to return from procedure?
How to call
>> CloseHandle( h ) when variable destroyed?

An obvious way is an RAII type where the destructor calls 
CloseHandle.


...

Created 7F1BD5A7D000
Constructed Handle with 7F1BD5A7D000
Exiting open_keyboard_device2
Exiting processDevice
Closing 7F1BD5A7D000
Exiting main

Ali


You like a boss, Ali. Thank, Ali!



Re: unique_ptr | Unique for autoclose handle

2022-12-14 Thread Vitaliy Fadeev via Digitalmars-d-learn

On Wednesday, 14 December 2022 at 17:44:05 UTC, Ali Çehreli wrote:

On 12/14/22 09:41, Ali Çehreli wrote:

> // According to documentation, the handler must be
created dynamically:
> // We make a unique owner for it:



Last try is customized **std.typecons.Unique**:

```
module safehandle;

import core.sys.windows.windows;
import std.typecons;

struct Unique( T, alias DTOR )
{
...

~this()
{
if (_p !is null)
{
DTOR( _p );
destroy(_p);
_p = null;
}
}

   ...
}

alias SafeHandle = Unique!(void,CloseHandle);
```

and using :

```
SafeHandle open_keyboard_device2( ... )
{
...

SafeHandle dev_handle =
CreateFileW( ... );

...

return dev_handle;
}
```

```
void main()
{
...

auto dev = open_keyboard_device2( ... );
process_device( dev );

...

// auto-close handle
}
```

Open source is good!
Thanks all!

and in future:
```
alias SafeHandle= Unique!(void,CloseHandle)
alias Safe_SDL_Window   = Unique!(SDL_Window,SDL_DestroyWindow)
alias Safe_SDL_Surface  = Unique!(SDL_Surface,SDL_FreeSurface)
alias Safe_SDL_Texture  = Unique!(SDL_Texture,SDL_DestroyTexture)
alias Safe_SDL_Renderer = 
Unique!(SDL_Renderer,SDL_DestroyRenderer)

```
Code is good, but testing