Re: Multitasking question: Making a picture viewer, loading lots of images in the background

2019-04-02 Thread DanielG via Digitalmars-d-learn
I'm no threading expert but 1000 - if those indeed are real 
threads (like, preemptive OS threads) - is wa too many.


My understanding is that there's no benefit to creating more than 
the number of hyperthreads your CPU supports (minus your main 
thread?). So you'd want a work queue of all the available 
work/images, and then some reasonable number of threads (4-12 
depending on core count / hyperthreading) taking work as they 
need it from that queue.


You should probably be looking at std.parallelism (TaskPool etc) 
for this. Perhaps somebody can provide a more detailed how-to ...





Multitasking question: Making a picture viewer, loading lots of images in the background

2019-04-02 Thread realhet via Digitalmars-d-learn

Hi,

I'm writing a picture browser program and I'd like to ask for a 
bit of assistance.


My app is basically a Win32 window with an OpenGL surface. 
Currently I'm able to display a lot of pictures in it. The 
decompression of the bitmaps take place at the beginning of the 
program.


I want to make this more user friendly, so at first I only draw 
placeholders for the pictures while the actual loading and 
decompression is happening on worker threads.


I used std.concurrency.spawn. The parameters are the filename, 
and an index. After the decompression I let the main thread know 
about there is a bitmap it should upload to a texture later. I do 
it using synchronize, it works without error and was really easy 
to implements in D.


My problem is that when I launch 1000 spawn(), the work overload 
on the main thread is so heavy that the 60FPS timer which is 
refreshing the windows never can be called. Obviously spawn() is 
not the right choice for this.


In my previous projects I addressed this by creating 
NumberOfProcessorThreads threads,and locked them on specific 
ProcessorThreads by using ProcessAffinity, so the main thread has 
enough time to generate fluid user interface while the other 
worker threads used 100%CPU for the fastest processing.


I also remember that with OPENMP I was also able to lock workers 
onto specific ProcessorThreads, it also worked fine.


But how can I do this elegantly in D?

Thanks in advance!


(Also I'm not sure how spaw()-n is working exactly. Is it creates 
a new process for every spawn() call, or is it assigns the task 
to one of the N worker processes? 1000 CreateProcess() api calls 
would be not so nice. I just wanna know it in order to avoid it.)


Re: Access outer member of struct from inner struct

2019-04-02 Thread Q. Schroll via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 18:52:07 UTC, Jacob Carlborg wrote:

On 2019-04-02 20:44, Q. Schroll wrote:


After removing the calls to writeln, the error I get is:


`this` for `read` needs to be type `Outer` not type `Inner`


You cannot access stuff in Outer because Inner objects are not 
outer objects and don't implicitly own an Outer object. In 
your Inner method `write`, there is no Outer object present at 
all to call the method on.


It works if the struct is nested inside a function [1]. I 
thought it would work nested inside a struct too.


[1] https://dlang.org/spec/struct.html#nested


The reason it works inside a function is that the struct has a 
hidden pointer to the function context. The function's local 
values actually exist when an object of that struct type is being 
instantiated.


The main difference between a struct nested in a function and one 
inside another struct is that the one in a function cannot¹ be 
created outside of that function while constructing the latter is 
possible the way you think it is:


Outer.Inner innerObj = Outer.Inner(parameters);

¹ You can using reflection and stuff like that, but it's still 
broken if it uses the context.


Re: Access outer member of struct from inner struct

2019-04-02 Thread Jacob Carlborg via Digitalmars-d-learn

On 2019-04-02 20:44, Q. Schroll wrote:


After removing the calls to writeln, the error I get is:


`this` for `read` needs to be type `Outer` not type `Inner`


You cannot access stuff in Outer because Inner objects are not outer 
objects and don't implicitly own an Outer object. In your Inner method 
`write`, there is no Outer object present at all to call the method on.


It works if the struct is nested inside a function [1]. I thought it 
would work nested inside a struct too.


[1] https://dlang.org/spec/struct.html#nested

--
/Jacob Carlborg


Re: Get attributes of a field?

2019-04-02 Thread Jacob Carlborg via Digitalmars-d-learn

On 2019-04-02 15:23, Alex wrote:

__traits(getAttributes, T)

Requires a type and a field is unfortunately not a type ;/


enum attr;

struct Foo
{
@attr int a;
}

void main()
{
alias a = __traits(getAttributes, Foo.a);
}

--
/Jacob Carlborg


Re: Get mangled name of field?

2019-04-02 Thread Jacob Carlborg via Digitalmars-d-learn

On 2019-04-02 15:11, Alex wrote:
How do I get the mangled name of a field? mangledName!T requires passing 
a type ;/


struct Foo
{
int a;
}

void main()
{
writeln(Foo.a.mangleof);
}

--
/Jacob Carlborg


Re: Access outer member of struct from inner struct

2019-04-02 Thread Q. Schroll via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 18:20:09 UTC, Andrey wrote:

Hello,
In this example how can I access the members "read" and "q" of 
struct Outer from Inner struct?

struct Outer
{
ulong q = 1;
Inner inner;

void read(ulong value)
{
q += value;
}

void run()
{
q.writeln;
read(5);
}

struct Inner
{
void write(string text)
{
read(text.length);
writeln(q);
}
}
}

void main()
{
Outer ttt;
ttt.run();
}


During compilation I get:
onlineapp.d(55): Error: this for read needs to be type Outer 
not type Inner

onlineapp.d(56): Error: need this for q of type ulong


After removing the calls to writeln, the error I get is:


`this` for `read` needs to be type `Outer` not type `Inner`


You cannot access stuff in Outer because Inner objects are not 
outer objects and don't implicitly own an Outer object. In your 
Inner method `write`, there is no Outer object present at all to 
call the method on.


Re: gtkDcoding Blog Post for 2019-03-29 - Grid

2019-04-02 Thread Mike Wey via Digitalmars-d-learn

On 02-04-2019 17:48, Ron Tarrant wrote:

On Tuesday, 2 April 2019 at 14:13:09 UTC, number wrote:

Can somebody explain why getRgba() (apparently inherited from 
ColorChooser) does take an out parameter instead of returning an 
Gdk.RGBA?


My understanding is this:

Returning an object (as opposed to a single value) means returning a 
pointer rather than the entire object. And the object will cease to 
exist once the function returns because the scope no longer exists. So, 
it follows that an out variable passed in will preserve the object 
itself once program control returns to the caller.




While that would be true for things that live on the stack, this is not 
the case for RGBA. The C version of getRgba uses the "out" parameter so 
you can pass in a existing GdkRgba, even tough that would make it more 
like ref.
This doesn't make sense for the d binding since you will always get a 
new RGBA passed through the out parameter.


--
Mike Wey


Access outer member of struct from inner struct

2019-04-02 Thread Andrey via Digitalmars-d-learn

Hello,
In this example how can I access the members "read" and "q" of 
struct Outer from Inner struct?

struct Outer
{
ulong q = 1;
Inner inner;

void read(ulong value)
{
q += value;
}

void run()
{
q.writeln;
read(5);
}

struct Inner
{
void write(string text)
{
read(text.length);
writeln(q);
}
}
}

void main()
{
Outer ttt;
ttt.run();
}


During compilation I get:
onlineapp.d(55): Error: this for read needs to be type Outer 
not type Inner

onlineapp.d(56): Error: need this for q of type ulong


Re: gtkDcoding Blog Post for 2019-03-29 - Grid

2019-04-02 Thread Ron Tarrant via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 14:13:09 UTC, number wrote:


Thank you!

You're welcome. :)

The function ignores its argument and always uses member 
variable button2 instead. Changing the parameter type to 
MyRadioButton and using 'button' instead of 'button2' in the 
body works, so you could pass another default in 
RadioBox.this().


Thanks for catching my typos. I gotta stop messing with the code 
once it's working. :) Fixes are uploaded.


Anyway, you can also declare it as a RadioButton and that works, 
too... as long as the variables inside the function are changed 
to 'button.'


Can somebody explain why getRgba() (apparently inherited from 
ColorChooser) does take an out parameter instead of returning 
an Gdk.RGBA?


My understanding is this:

Returning an object (as opposed to a single value) means 
returning a pointer rather than the entire object. And the object 
will cease to exist once the function returns because the scope 
no longer exists. So, it follows that an out variable passed in 
will preserve the object itself once program control returns to 
the caller.




Re: Get attributes of a field?

2019-04-02 Thread Andre Pany via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 13:36:47 UTC, Alex wrote:

On Tuesday, 2 April 2019 at 13:23:37 UTC, Alex wrote:

__traits(getAttributes, T)

Requires a type and a field is unfortunately not a type ;/


I'd like to be able to get the attributes without having to 
instantiate the base type because that is problematic and I see 
no reason why it has to be instantiated to get CT values.


Could you provide a full example for this issue and for your 
other message? That makes it easier to help you


Kind regards
Andre


Re: gtkDcoding Blog Post for 2019-03-29 - Grid

2019-04-02 Thread number via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 11:31:39 UTC, Ron Tarrant wrote:
Today's the day for (yet) another blog post over on 
gtkDcoding.com and the subjects are:


- the RadioButton, and
- the ColorButton.

You can find it here:
 
http://gtkdcoding.com/2019/04/02/0023-radio-and-color-buttons.html


Thank you!

But if we want one of the others to be active on start-up, as 
well as syncing up the observed object, we have to call that 
button’s setActive(true) function. To simplify this two-step 
process, I broke it out into its own function, 
setActiveButton().


The function ignores its argument and always uses member variable 
button2 instead. Changing the parameter type to MyRadioButton and 
using 'button' instead of 'button2' in the body works, so you 
could pass another default in RadioBox.this().



Can somebody explain why getRgba() (apparently inherited from 
ColorChooser) does take an out parameter instead of returning an 
Gdk.RGBA?


Re: Get attributes of a field?

2019-04-02 Thread Alex via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 13:23:37 UTC, Alex wrote:

__traits(getAttributes, T)

Requires a type and a field is unfortunately not a type ;/


I'd like to be able to get the attributes without having to 
instantiate the base type because that is problematic and I see 
no reason why it has to be instantiated to get CT values.


Get attributes of a field?

2019-04-02 Thread Alex via Digitalmars-d-learn

__traits(getAttributes, T)

Requires a type and a field is unfortunately not a type ;/



Get mangled name of field?

2019-04-02 Thread Alex via Digitalmars-d-learn
How do I get the mangled name of a field? mangledName!T requires 
passing a type ;/




Re: Forbidding implicit conversions from an enum type to an integer

2019-04-02 Thread Dennis via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 09:37:26 UTC, Per Nordlöw wrote:
Are there any plans on deprecating implicit conversions of 
enums to integers?


Not that I know of. Given the precedence of this:
https://github.com/dlang/DIPs/blob/master/DIPs/rejected/DIP1015.md
I doubt enum/integer-types are going to get stricter.

Is there a particular bug you encountered because of this 
conversion?


Re: gtkDcoding Blog Post for 2019-03-29 - Grid

2019-04-02 Thread Ron Tarrant via Digitalmars-d-learn
Today's the day for (yet) another blog post over on 
gtkDcoding.com and the subjects are:


- the RadioButton, and
- the ColorButton.

You can find it here:
 
http://gtkdcoding.com/2019/04/02/0023-radio-and-color-buttons.html


Re: Forbidding implicit conversions from an enum type to an integer

2019-04-02 Thread Per Nordlöw via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 09:02:03 UTC, Dennis wrote:

You can make the enum type not an integer type.

```
struct Int{int i;}

enum E: Int {
first = Int(0),
second = Int(1),
}
```


Thanks.

Are there any plans on deprecating implicit conversions of enums 
to integers?


Re: Pass template parameter into q{} string

2019-04-02 Thread Marco de Wild via Digitalmars-d-learn

On Monday, 1 April 2019 at 17:32:29 UTC, Andrey wrote:

Hello,

enum Key : string
{
  First = "qwerty",
  Last = "zaqy"
}

void main()
{
enum decl(alias values1) = q{
static foreach(value; values1)
mixin("bool " ~ value ~ " = false;");
};

enum qqq = [Key.First, Key.Last];
mixin(decl!qqq);
}


I don't understand how to pass template parameter "values1" 
into q{} string to get this output:

static foreach(value; [Key.First, Key.Last])
mixin("bool " ~ value ~ " = false;");

or

static foreach(value; qqq)
mixin("bool " ~ value ~ " = false;");


A token string (q{}) is still a string literal[1] (but has 
autocomplete on individual tokens in many IDEs). It does not 
substitute the text for the values. As such, you'd need to format 
the string like usual. The following code worked:


enum Key : string
{
  First = "qwerty",
  Last = "zaqy"
}

void main()
{
import std.format;
enum decl(alias values1) = q{
static foreach(value; %s)
mixin("bool " ~ value ~ " = false;");
}.format(values1.stringof);

enum qqq = [Key.First, Key.Last];
mixin(decl!qqq);

import std.stdio;
writeln(qwerty);
}

Here we use the format function from the standard library. We 
have a format token (%s) in the original string, and replace it 
with `values1.stringof`. `.stringof` in this case means the 
original text used in the call site passed to argument values1 
("qqq"). (When we don't use `.stringof`, "First" and "Last" are 
not defined, as the compiler thinks qqq is an array of Keys 
instead of strings.)
You could also use a string interpolation library (e.g. [2]) from 
Dub.


[1] https://dlang.org/spec/lex.html#token_strings
[2] http://code.dlang.org/packages/stri


Re: Forbidding implicit conversions from an enum type to an integer

2019-04-02 Thread Dennis via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 08:38:28 UTC, Per Nordlöw wrote:
Is there a way (compiler flag) to forbid implicit conversions 
from an enum type to integer types?


You can make the enum type not an integer type.

```
struct Int{int i;}

enum E: Int {
first = Int(0),
second = Int(1),
}
```


Forbidding implicit conversions from an enum type to an integer

2019-04-02 Thread Per Nordlöw via Digitalmars-d-learn
Is there a way (compiler flag) to forbid implicit conversions 
from an enum type to integer types?


Re: Build an alias array

2019-04-02 Thread Alex via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 07:47:29 UTC, Stefan Koch wrote:

On Tuesday, 2 April 2019 at 03:15:36 UTC, Alex wrote:
Is there any way to build an alias array at compile time that 
isn't too heavy in resources?

{...}


Hi Alex,

I agree that there should be a way to do that.

As soon as newCTFE is a releasable state, I'll work on that 
again :)


I'd be intereseted in your usecases, so I can asses the 
requirements better.


Thanks in advance!

- Stefan


I'm just trying to filter out certain types from a static foreach 
to use later that are not used immediately. I usually need such a 
way to build an sequence though.


static foreach(a; Seq)
{
   static if (is(a == S))
   {
   } else arr.Add(a);
}

The idea is that I can then do

static foreach(a; arr)
{

}

to get the remaining elements.

Sometimes it's just building up up a sequence in some order for 
some reason such as collecting types and making decisions between 
those types.


Also, sometimes I want to create a hierarchy like a tree but 
nodes are sequences.


These things are easy to do at RT but nearly impossible at CT. 
With CTFE they can sorta be done but usually one still needs some 
way to build a sequence.


I mean, one can easily do this by using different names as I said:

alias arr1 = AliasSeq!(T1);
alias arr2 = AliasSeq!(arr1, T2);
alias arrnp1 = AliasSeq!(arrn, Tn);

but it's difficult to know the final name(well, one can use a 
mixin) and it has problems with scoping on static if's. The only 
point of doing it is because we can't reassign to an alias.


alias arr = AliasSeq!Empty;
static foreach(Tk; S)
{{
alias arr = AliasSeq!(arr, Tk);
}}

Then arr == S after the loop.

would be ideal.






Re: Build an alias array

2019-04-02 Thread Stefan Koch via Digitalmars-d-learn

On Tuesday, 2 April 2019 at 03:15:36 UTC, Alex wrote:
Is there any way to build an alias array at compile time that 
isn't too heavy in resources?

{...}


Hi Alex,

I agree that there should be a way to do that.

As soon as newCTFE is a releasable state, I'll work on that again 
:)


I'd be intereseted in your usecases, so I can asses the 
requirements better.


Thanks in advance!

- Stefan