Re: Passing struct to function

2018-06-13 Thread Michał via Digitalmars-d-learn
On Wednesday, 13 June 2018 at 17:37:44 UTC, Steven Schveighoffer 
wrote:


Hm... the only way to do it in D is to provide a function that 
checks whether the small vector optimization is in play, and 
return a pointer/slice to itself.


With D it is possible to alias the getter function that 
provides the actual data to allow code to look nicer.


For example (crude example):

struct Vector(T)
{
   bool svo; // small vector optimization
   union
   {
  T[4] local;
  T[] heap;
   }
   inout(T)[] get() inout { return svo ? local[], heap; }
   alias get this;
   ... // implement specialized append, concat operators, etc.
}

Now, you can use Vector as if it were an array, and it just 
works.


-Steve


Thanks for an idea, I will try it.


Re: Passing struct to function

2018-06-13 Thread Michał via Digitalmars-d-learn
On Wednesday, 13 June 2018 at 16:40:51 UTC, Steven Schveighoffer 
wrote:

On 6/13/18 10:43 AM, Michał wrote:
When I pass my struct to function something is going wrong. I 
don't know how to fix it.


Code:
import std.stdio;


void print(ref Vector v, string s){
     writefln("%s==%s    %s", , v.ptr, s);
}

struct Vector {
 int x;
 int* ptr;

 this(this) {
     ptr = 
     print(this, "postblit");
 }
}

void someFunc(Vector t) {
 print(t, "in someFunc");
}

void main() {
 auto tmpA = Vector();
 tmpA.ptr = 
 print(tmpA, "start");

 someFunc(tmpA);
}


Result on my machine:
7FFF7D70BC00==7FFF7D70BC00    start
7FFF7D70BBF0==7FFF7D70BBF0    postblit
7FFF7D70BBD0==7FFF7D70BBF0    in someFunc

In the last line pointers are not matching. I thought that 
postblit will do the thing but it is not the case. How to make 
'ptr' to be null or '' all the time?


D allows moving any struct instance without calling postblit, 
as long as the original is no longer used.


The optimizer is likely seeing here that the memory can be 
copied without calling postblit, because nobody is using tmpA 
after the call.


In general, you should NOT store an internal pointer in a 
struct, unless you allocate it on the heap.


-Steve



I need internal pointer because I want to implement vector(like 
in C++) with 'small vector optimization', when i have internal 
pointer it is very easy and functions like 'add' don't have 
additional checks.


If storing internal pointers is forbidden do you know some way to 
implement 'small vector optimization' without additional checks 
in 'add' function?


Passing struct to function

2018-06-13 Thread Michał via Digitalmars-d-learn
When I pass my struct to function something is going wrong. I 
don't know how to fix it.


Code:
import std.stdio;


void print(ref Vector v, string s){
writefln("%s==%s%s", , v.ptr, s);
}

struct Vector {
int x;
int* ptr;

this(this) {
ptr = 
print(this, "postblit");
}
}

void someFunc(Vector t) {
print(t, "in someFunc");
}

void main() {
auto tmpA = Vector();
tmpA.ptr = 
print(tmpA, "start");

someFunc(tmpA);
}


Result on my machine:
7FFF7D70BC00==7FFF7D70BC00start
7FFF7D70BBF0==7FFF7D70BBF0postblit
7FFF7D70BBD0==7FFF7D70BBF0in someFunc

In the last line pointers are not matching. I thought that 
postblit will do the thing but it is not the case. How to make 
'ptr' to be null or '' all the time?


Re: Building basic gtkd,opengl application

2015-09-21 Thread Michał via Digitalmars-d-learn

It turns out it was caused by a bug in the gtkd.
Thanks to Mike Wey already fixed.

My minimal working code if somebody was intrested.



Re: Building basic gtkd,opengl application

2015-09-21 Thread Michał via Digitalmars-d-learn

So no idea how to make basic gtk/opengl application working?


Re: Building basic gtkd,opengl application

2015-09-21 Thread Michał via Digitalmars-d-learn

On Monday, 21 September 2015 at 18:07:27 UTC, Ali Çehreli wrote:

Does that work?


Yes and clear output there.
I have tried some basic examples of gtkd and they seems to work. 
GLArea has some problems.


Re: Building basic gtkd,opengl application

2015-09-21 Thread Michał via Digitalmars-d-learn

On Monday, 21 September 2015 at 18:08:15 UTC, bachmeier wrote:

On Monday, 21 September 2015 at 17:59:17 UTC, Michał wrote:

So no idea how to make basic gtk/opengl application working?


The project has its own forum if you haven't already posted 
there: http://forum.gtkd.org/


I didn't. I will try there.
Thanks for help.


Building basic gtkd,opengl application

2015-09-20 Thread Michał via Digitalmars-d-learn

I am trying to make some application using gtkd and opengl.
I have made simple program but it didn't work and I have no idea 
why.


I have never used gtk so maybe I'm doing something stupid : /

The code:
http://pastebin.com/7NfbMqaK

Error:
http://pastebin.com/vaFAP0bu

Some ideas?
Any tips to gtkd/gtk/gl are welcome.