Re: Int to float?

2015-03-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 5 March 2015 at 20:32:20 UTC, anonymous wrote:

That's not really simpler, though.


Maybe, but I think the union is a bit nicer because then the 
compiler is responsible for more of the details. For example, it 
should work with class objects without the complication of 
dealing with the fact that they are already pointers under the 
hood.


Either way works though and should compile to the same 
instructions, just if I was doing it as a generic library, I 
think I'd use the union method.


Re: Int to float?

2015-03-05 Thread anonymous via Digitalmars-d-learn

On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote:

int someValue = 5;
float sameBinary = *(cast(float*)cast(void*)someValue);


The cast(void*) isn't necessary.


Re: Object as function argument

2015-03-05 Thread anonymous via Digitalmars-d-learn

On Thursday, 5 March 2015 at 19:51:09 UTC, Max Klyga wrote:
If you really need the actual pointer to object data you can 
use `*cast(void**)myObject`. Compiler cannot cast object 
reference to `void*` but we can trick it ;)


It can, actually. A class can define its own cast(void*) though, 
so the reinterpret way may be more robust. Also, I'm not sure if 
any of this is specified. So watch out for undefined (or 
underspecified) behaviour.


Object as function argument

2015-03-05 Thread Chris Sperandio via Digitalmars-d-learn

Hi,

I'm a developer coming from C and I've a question about class 
instance as method or function parameter.
In the book The D Programming Language, I read the instance was 
passed by reference to functions (in the opposite of structures). 
I understood that it was the same object in the function and the 
caller. But I'm think, I was wrong because when I print the 
addresses of an object before the function call and inside the 
function, they're not the same but the changes from the function 
are kept in the instance.
If I use the ref qualifier in the function declaration, the 2 
addresses are the same.


How do the changes work in the function? Is there a copy ? Or a 
magic trick :) ?


Chris


Re: Int to float?

2015-03-05 Thread Taylor Hillegeist via Digitalmars-d-learn

On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote:

Am 05.03.2015 um 21:00 schrieb Taylor Hillegeist:
How to I cast a Int to float without changing its binary 
representation?


int someValue = 5;
float sameBinary = *(cast(float*)cast(void*)someValue);


ahh of course! lol :)


Re: Int to float?

2015-03-05 Thread badlink via Digitalmars-d-learn

On Thursday, 5 March 2015 at 20:16:55 UTC, anonymous wrote:

On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote:

int someValue = 5;
float sameBinary = *(cast(float*)cast(void*)someValue);


The cast(void*) isn't necessary.


Actually even the cast is unecessary, just use a uniform.

union N {
int i;
float f;
}

http://dpaste.dzfl.pl/58b6eddcf725


Re: Object as function argument

2015-03-05 Thread Max Klyga via Digitalmars-d-learn

On 2015-03-05 19:35:34 +, Chris Sperandio said:


Hi,

I'm a developer coming from C and I've a question about class instance 
as method or function parameter.
In the book The D Programming Language, I read the instance was 
passed by reference to functions (in the opposite of structures). I 
understood that it was the same object in the function and the caller. 
But I'm think, I was wrong because when I print the addresses of an 
object before the function call and inside the function, they're not 
the same but the changes from the function are kept in the instance.
If I use the ref qualifier in the function declaration, the 2 
addresses are the same.


How do the changes work in the function? Is there a copy ? Or a magic 
trick :) ?


Chris


When you write `auto myObject = new MyObject();`
`myObject` is actually a pointer to object in GC memory. Its roughly 
equivalent to `struct MyObject *myobject` in C. So when you take a 
pointer you actually take a pointer to reference on the stack and thats 
why its different in the function - variable is higher up the stack.

`ref` qualifyer guaranties that you get the pointer to the same reference.

If you really need the actual pointer to object data you can use 
`*cast(void**)myObject`. Compiler cannot cast object reference to 
`void*` but we can trick it ;)




Re: Object as function argument

2015-03-05 Thread Chris Sperandio via Digitalmars-d-learn
Ok... So, in D when I work with Object, it's like if I work with 
only pointers.
Thus, I can return null from a function which returns an Item 
instance.

Is it clean to write this code below ?

  static Item nullReturn(Item item)
  {
   // ...
   // and for some cases
return null;
  }


Re: Object as function argument

2015-03-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 5 March 2015 at 20:08:08 UTC, Chris Sperandio wrote:

Is it clean to write this code below ?


yup. Though remember all the downsides of null - if you try to 
use a null object like accessing a member, the program will be 
terminated.


Int to float?

2015-03-05 Thread Taylor Hillegeist via Digitalmars-d-learn
How to I cast a Int to float without changing its binary 
representation?


Re: Int to float?

2015-03-05 Thread Benjamin Thaut via Digitalmars-d-learn

Am 05.03.2015 um 21:00 schrieb Taylor Hillegeist:

How to I cast a Int to float without changing its binary representation?


int someValue = 5;
float sameBinary = *(cast(float*)cast(void*)someValue);


Re: Object as function argument

2015-03-05 Thread Chris Sperandio via Digitalmars-d-learn

Below the code:

module item;

import std.stdio;

class Item
{
  ulong count;

  static void call1(Item item)
  {
writeln((call1) Addr: , item);
  }

  static void call2(ref Item item)
  {
writeln((call2) Addr: , item);
  }

  static Item call3(Item item)
  {
writeln((call3) Addr: , item);
return item;
  }

  static Item call4(Item item)
  {
// Here, I change the count
item.count = 100;
return item;
  }
}


void main()
{
  auto item = new Item();
  writeln((main) Addr item=, item);
  Item.call1(item);
  Item.call2(item);

  auto res3 = Item.call3(item);
  writeln((main) res3 item=, res3);

  auto res4 = Item.call4(item);
  writeln((main) res4 item=, res4);

  assert(item.count == 100);

}

I get:

(main) Addr item=7FFF5D797818
(call1) Addr: 7FFF5D7977F8
(call2) Addr: 7FFF5D797818
(call3) Addr: 7FFF5D7977F8
(main) res3 item=7FFF5D797820
(main) res4 item=7FFF5D797828


On Thursday, 5 March 2015 at 19:48:38 UTC, w0rp wrote:
On Thursday, 5 March 2015 at 19:35:35 UTC, Chris Sperandio 
wrote:

Hi,

I'm a developer coming from C and I've a question about class 
instance as method or function parameter.
In the book The D Programming Language, I read the instance 
was passed by reference to functions (in the opposite of 
structures). I understood that it was the same object in the 
function and the caller. But I'm think, I was wrong because 
when I print the addresses of an object before the function 
call and inside the function, they're not the same but the 
changes from the function are kept in the instance.
If I use the ref qualifier in the function declaration, the 
2 addresses are the same.


How do the changes work in the function? Is there a copy ? Or 
a magic trick :) ?


Chris


If you share your code, I'll be happy to take a look. Classes 
are reference types, so passing T for a class should pass the 
reference to the object.


Re: Object as function argument

2015-03-05 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, March 05, 2015 19:35:34 Chris Sperandio via Digitalmars-d-learn 
wrote:
 Hi,

 I'm a developer coming from C and I've a question about class
 instance as method or function parameter.
 In the book The D Programming Language, I read the instance was
 passed by reference to functions (in the opposite of structures).
 I understood that it was the same object in the function and the
 caller. But I'm think, I was wrong because when I print the
 addresses of an object before the function call and inside the
 function, they're not the same but the changes from the function
 are kept in the instance.
 If I use the ref qualifier in the function declaration, the 2
 addresses are the same.

 How do the changes work in the function? Is there a copy ? Or a
 magic trick :) ?

MyClass c;

is a reference to an object. So, if you do c, you're taking the address of
the reference, not the object. So, it's like if you had

MyClass* c;

in C/C++ and you did c. So, if you have

void bar()
{
auto c1 = new MyClass;
foo(c1);
}

void foo(MyClass c2)
{
}

then c1 and c2 have different addresses just like if they would if they were
explictly pointers. Changing foo to

void foo(ref MyClass c2)
{
}

means that you're passing the reference itself by ref, making it essentially
equivalent to

void foo(MyClass* c2)
{
}

in C++. So, c2's address is going to be the same as c1, because they're
essentially the same reference/pointer at that point. If you want the
address of the object itself rather than its reference, then you need to
cast it to void*. e.g. this code will print the same value for c1 and c2:

import std.stdio;

class MyClass {}

void main()
{
auto c1 = new MyClass;
writeln(cast(void*)c1);
foo(c1);
}

void foo(MyClass c2)
{
writeln(cast(void*)c2);
}

- Jonathan M Davis



Re: Int to float?

2015-03-05 Thread anonymous via Digitalmars-d-learn

On Thursday, 5 March 2015 at 20:21:18 UTC, badlink wrote:

On Thursday, 5 March 2015 at 20:16:55 UTC, anonymous wrote:
On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut 
wrote:

int someValue = 5;
float sameBinary = *(cast(float*)cast(void*)someValue);


The cast(void*) isn't necessary.


Actually even the cast is unecessary, just use a uniform.

union N {
int i;
float f;
}

http://dpaste.dzfl.pl/58b6eddcf725


That's not really simpler, though.


Re: Int to float?

2015-03-05 Thread Jesse Phillips via Digitalmars-d-learn
On Thursday, 5 March 2015 at 20:06:55 UTC, Taylor Hillegeist 
wrote:

On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote:

Am 05.03.2015 um 21:00 schrieb Taylor Hillegeist:
How to I cast a Int to float without changing its binary 
representation?


int someValue = 5;
float sameBinary = *(cast(float*)cast(void*)someValue);


ahh of course! lol :)


I think I read somewhere you don't want to use unions like this, 
but I think it is more because you generally don't want to 
reinterpret bits.


import std.stdio;
void main() {
union Fi {
float f;
int i;
}

Fi fi;
fi.i = 65;
writeln(fi.f);
}


Is std.signals deprecated?

2015-03-05 Thread weaselcat via Digitalmars-d-learn
Seems barely maintained and there was a proposed replacement 
claiming it was broken(http://wiki.dlang.org/Review/std.signal) 
that never got approved.


Is std.signals worth using over a dub package?


Re: how to write a string to a c pointer?

2015-03-05 Thread ketmar via Digitalmars-d-learn
On Thu, 05 Mar 2015 16:36:35 +0100, FG wrote:

 Damn those composite characters!

or invisible ones. or RTL switch.

unicode sux[1].

[1] http://file.bestmx.net/ee/articles/uni_vs_code.pdf

signature.asc
Description: PGP signature


Re: Int to float?

2015-03-05 Thread via Digitalmars-d-learn

On Thursday, 5 March 2015 at 23:50:28 UTC, Jesse Phillips wrote:
I think I read somewhere you don't want to use unions like 
this, but I think it is more because you generally don't want 
to reinterpret bits.


It is non-portable, since some hardware architectures may use 
different representations (e.g. different byte order on int and 
float).


D claims to follow C, so using unions for type punning is 
ultimately implementation defined.


In C++ using unions for type punning is illegal/undefined 
behaviour, so in C++ you should use memcpy. Memcpy also has the 
advantage of explicitly copying thus avoiding some aliasing 
issues.


Re: I want to introduce boost_asio to dlang

2015-03-05 Thread Messenger via Digitalmars-d-learn

On Thursday, 5 March 2015 at 07:38:35 UTC, ketmar wrote:

On Thu, 05 Mar 2015 06:05:55 +, zhmt wrote:


But I am not familiar with dlang


this is the root of the problem. please, make yourself familiar 
before

starting to wrap boost crap.


Unwarranted tone imo. Let's play nice.



Re: how to write a string to a c pointer?

2015-03-05 Thread Ali Çehreli via Digitalmars-d-learn

On 03/05/2015 03:25 PM, ketmar wrote:


unicode sux[1].

[1] http://file.bestmx.net/ee/articles/uni_vs_code.pdf


Thanks. I enjoyed the article and I agree with everything said in there.

It made me happy that I was not the only person who has been ruminating 
over alphabet as the crucial piece in this whole Unicode story. I've 
been giving the example of if I have a company name as the string ali  
jim, the uppercase of it should be ALİ  JIM because the different 
letter 'i's belong to different alphabets. Anyway...


Here is how I attempted to define an alphabet with its implied collation 
orders. For example, for the Turkish alphabet:


  https://code.google.com/p/trileri/source/browse/trunk/tr/alfabe.d#796

Unfortunately, the code itself is in Turkish, has never been finished, 
bad and older D code, and is abandoned at this point. :-/


Ali



Derelict Assimp not loading mesh properly? (Maybe index buffer)

2015-03-05 Thread Bennet via Digitalmars-d-learn
I wrote a custom OBJ file importer which worked fairly well 
however was not robust enough to support everything. I've decided 
to give AssImp a shot. I followed some tutorials and have set up 
my code to read in the vertices, tex coords, normals, and indices 
of an OBJ cube model that I have had success loading with my 
custom importer. The cube model's faces do not render properly, 
instead only rendering a few tri's of the cube. The way AssImp 
handles the data is obviously a bit different than my solution 
because the normals, positions/vertices, tex coords, and indices 
do not match my custom solution. I would very much appreciate 
some insight into why I'm having issues as all of the tutorials 
I've found have matched my approach. If a picture of the faulty 
rendered cube would be helpful I can work on getting a screenshot 
up. Thank you.

My Asset importing class is at:
http://codebin.org/view/4d2ec4d3
The rest of my code is at (Mesh Class is in source/graphics):
https://github.com/BennetLeff/PhySim


Re: I want to introduce boost_asio to dlang

2015-03-05 Thread Jack Applegame via Digitalmars-d-learn

On Thursday, 5 March 2015 at 06:05:56 UTC, zhmt wrote:

I am a gameserver developer, my programming lang is java now.

I want to change java to dlang, and I like boost_asio and it's 
coroutine,

so, I want to create a binding of boost_asio.

But I am not familiar with dlang, so I want to find someone 
help me, or develope this binding with me.


I will put the asio binding on github, and opensource, and free.

Anybody help or join?
There is no need to do it. Just use http://vibed.org/ instead of 
boost::asio.


Re: how to write a string to a c pointer?

2015-03-05 Thread FG via Digitalmars-d-learn

On 2015-03-05 at 10:42, Kagamin wrote:

string s;
char[] b = cast(char[])asArray();
b[0..s.length] = s[];


It's a bit more complicated than that if you include cutting string for buffers 
with smaller capacity, doing so respecting UTF-8, and adding a '\0' sentinel, 
since you may want to use the string in C (if I assume correctly). The 
setString function does all that:



import std.stdio, std.range, std.c.stdlib;

class Buffer {
private void *ptr;
private int size;
private int _cap;

public this(int cap) { ptr = malloc(cap); this._cap = cap; }
public ~this() { free(ptr); }
public ubyte[] asArray() { ubyte[] ret = (cast(ubyte*)ptr)[0..cap]; return 
ret; }
public void* getPtr() { return ptr; }
public int cap() { return _cap; }
}

int setString(Buffer buffer, string s)
{
assert(buffer.cap  0);
char[] b = cast(char[])buffer.asArray();
int len = min(s.length, buffer.cap - 1);
int break_at;
// The dchar is essential in walking over UTF-8 code points.
// break_at will hold the last position at which the string can be cleanly 
cut
foreach (int i, dchar v; s) {
if (i == len) { break_at = i; break; }
if (i  len) break;
break_at = i;
}
len = break_at;
b[0..len] = s[0..len];

// add a sentinel if you want to use the string in C
b[len] = '\0';
// you could at this point set buffer.size to len in order to use the 
string in D
return len;
}

void main()
{
string s = ąćęłńóśźż;
foreach (i; 1..24) {
Buffer buffer = new Buffer(i);
int len = setString(buffer, s);
printf(bufsize %2d -- strlen %2d -- %s --\n, i, len, buffer.getPtr);
}
}



Output of the program:

bufsize  1 -- strlen  0 --  --
bufsize  2 -- strlen  0 --  --
bufsize  3 -- strlen  2 -- ą --
bufsize  4 -- strlen  2 -- ą --
bufsize  5 -- strlen  4 -- ąć --
bufsize  6 -- strlen  4 -- ąć --
bufsize  7 -- strlen  6 -- ąćę --
bufsize  8 -- strlen  6 -- ąćę --
bufsize  9 -- strlen  8 -- ąćęł --
bufsize 10 -- strlen  8 -- ąćęł --
bufsize 11 -- strlen 10 -- ąćęłń --
bufsize 12 -- strlen 10 -- ąćęłń --
bufsize 13 -- strlen 12 -- ąćęłńó --
bufsize 14 -- strlen 12 -- ąćęłńó --
bufsize 15 -- strlen 14 -- ąćęłńóś --
bufsize 16 -- strlen 14 -- ąćęłńóś --
bufsize 17 -- strlen 16 -- ąćęłńóśź --
bufsize 18 -- strlen 16 -- ąćęłńóśź --
bufsize 19 -- strlen 16 -- ąćęłńóśź --
bufsize 20 -- strlen 16 -- ąćęłńóśź --
bufsize 21 -- strlen 16 -- ąćęłńóśź --
bufsize 22 -- strlen 16 -- ąćęłńóśź --
bufsize 23 -- strlen 16 -- ąćęłńóśź --




Re: I want to introduce boost_asio to dlang

2015-03-05 Thread ketmar via Digitalmars-d-learn
On Thu, 05 Mar 2015 07:46:04 +, zhmt wrote:

 I have studied for half a year, so I want to learn it in work,
 in solving problems.

that's a good way to learn. but starting from writing wrappers for 
something is not a good way. ;-)

if you want a wrapper for something, it's always better to ask if D needs 
it at all. i.e. the proper question is something like: i want to do async 
I/O in the spirit of boost::asio, are there any libraries for D that does 
such thing? and you will immideately be pointed at vibe.d. ;-)

signature.asc
Description: PGP signature


Re: how to write a string to a c pointer?

2015-03-05 Thread Kagamin via Digitalmars-d-learn

string s;
char[] b = cast(char[])asArray();
b[0..s.length] = s[];


Re: I want to introduce boost_asio to dlang

2015-03-05 Thread zhmt via Digitalmars-d-learn

On Thursday, 5 March 2015 at 08:22:33 UTC, Jack Applegame wrote:

On Thursday, 5 March 2015 at 06:05:56 UTC, zhmt wrote:

I am a gameserver developer, my programming lang is java now.

I want to change java to dlang, and I like boost_asio and it's 
coroutine,

so, I want to create a binding of boost_asio.

But I am not familiar with dlang, so I want to find someone 
help me, or develope this binding with me.


I will put the asio binding on github, and opensource, and 
free.


Anybody help or join?
There is no need to do it. Just use http://vibed.org/ instead 
of boost::asio.


It sounds like a good choice, is it as good as boost::asio?


Re: how to write a string to a c pointer?

2015-03-05 Thread zhmt via Digitalmars-d-learn

On Thursday, 5 March 2015 at 09:42:53 UTC, Kagamin wrote:

string s;
char[] b = cast(char[])asArray();
b[0..s.length] = s[];


Thank you very much. I should stop my developing , and read the 
dlang tutorial again.


Re: I want to introduce boost_asio to dlang

2015-03-05 Thread Atila Neves via Digitalmars-d-learn

On Thursday, 5 March 2015 at 09:38:27 UTC, zhmt wrote:

On Thursday, 5 March 2015 at 08:22:33 UTC, Jack Applegame wrote:

On Thursday, 5 March 2015 at 06:05:56 UTC, zhmt wrote:

I am a gameserver developer, my programming lang is java now.

I want to change java to dlang, and I like boost_asio and 
it's coroutine,

so, I want to create a binding of boost_asio.

But I am not familiar with dlang, so I want to find someone 
help me, or develope this binding with me.


I will put the asio binding on github, and opensource, and 
free.


Anybody help or join?
There is no need to do it. Just use http://vibed.org/ instead 
of boost::asio.


It sounds like a good choice, is it as good as boost::asio?


Having used both, it's better.

Atila


Re: I want to introduce boost_asio to dlang

2015-03-05 Thread zhmt via Digitalmars-d-learn
Thanks for all the suggestions and pointing the right direction,I 
will learn and try vibe.d, try to use it in my gameserver.