Re: Interfacing with C - calling member function of D struct from C?

2017-06-24 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 25 June 2017 at 02:05:35 UTC, unleashy wrote: How would I call `addToBar` from C code? You don't. Instead write it like: struct Foo { int bar; } extern(C) void addToBar(Foo* foo, int what) { foo.bar += what; } Then define it in C the same way and you call it the

Interfacing with C - calling member function of D struct from C?

2017-06-24 Thread unleashy via Digitalmars-d-learn
Hello! If I have a D struct like: struct Foo { int bar; void addToBar(int what) { bar += what; } } How would I call `addToBar` from C code? Would I need to put the `addToBar` function outside of the struct and mark it as `extern (C)` and in normal D code take advantage of

Re: casting to structure

2017-06-24 Thread Igor Shirkalin via Digitalmars-d-learn
On Saturday, 24 June 2017 at 21:41:22 UTC, Moritz Maxeiner wrote: Hi, unfortunately not: - Operator overloading is supported via member functions only [1]. - Corollary: You cannot overload operators for builtin types (i.e. where the cast gets rewritten to `e.opOverloaded` where `e` is a

Re: casting to structure

2017-06-24 Thread Moritz Maxeiner via Digitalmars-d-learn
On Saturday, 24 June 2017 at 21:11:13 UTC, Igor Shirkalin wrote: Is it possible without such a constructor? No. Also, the above works because of the following [1]: Casting a value v to a struct S, when value is not a struct of the same type, is equivalent to: `S(v)` [1]

Re: casting to structure

2017-06-24 Thread Moritz Maxeiner via Digitalmars-d-learn
On Saturday, 24 June 2017 at 20:43:48 UTC, Igor Shirkalin wrote: I'm in trouble with opCast function. Is it possible to cast some (integral) type to user defined structure? Hi, unfortunately not: - Operator overloading is supported via member functions only [1]. - Corollary: You cannot

Re: casting to structure

2017-06-24 Thread Igor Shirkalin via Digitalmars-d-learn
On Saturday, 24 June 2017 at 20:43:48 UTC, Igor Shirkalin wrote: struct A { void * data; // void * type is just for example // no matter what is here } I know that if I add constructor this(int) struct A { void * p; this(int k) { p = cast(void*)k; } } auto a = cast(A)

casting to structure

2017-06-24 Thread Igor Shirkalin via Digitalmars-d-learn
Hello! I'm in trouble with opCast function. Is it possible to cast some (integral) type to user defined structure? We have a structure: struct A { void * data; // void * type is just for example // no matter what is here } How can we define opCast operator to make the following

Re: What's the fastest way to check if a slice points to static data

2017-06-24 Thread via Digitalmars-d-learn
On Saturday, 24 June 2017 at 18:46:06 UTC, ketmar wrote: Petar Kirov [ZombineDev] wrote: Oh, I should have mentioned that I don't expect anything but ugly platform-specific hacks possibly involving the object file format ;) Just enough of them to claim that the solution is somewhat

Re: What's the fastest way to check if a slice points to static data

2017-06-24 Thread ketmar via Digitalmars-d-learn
Petar Kirov [ZombineDev] wrote: Oh, I should have mentioned that I don't expect anything but ugly platform-specific hacks possibly involving the object file format ;) Just enough of them to claim that the solution is somewhat cross-platform :D i guess you can loot at how TSL scanning is done

Re: Targetting the 8051

2017-06-24 Thread Russel Winder via Digitalmars-d-learn
On Sat, 2017-06-24 at 18:04 +, FoxyBrown via Digitalmars-d-learn wrote: > Is it possible? Using LDC or GCC? I do not need any libraries, > druntime, etc. Just bare bones with the nice D meta > features(templates, etc). Sorry but this is an ill-defined wish list. 8051 processors about in the

Re: What's the fastest way to check if a slice points to static data

2017-06-24 Thread via Digitalmars-d-learn
On Saturday, 24 June 2017 at 18:05:55 UTC, ketmar wrote: Petar Kirov [ZombineDev] wrote: *** But in any case, the null-terminated string was just an example application. I'm interested in a fast way to determine the "storage class" of the memory a slice or a pointer point to. I'm expecting

Re: What's the fastest way to check if a slice points to static data

2017-06-24 Thread ketmar via Digitalmars-d-learn
Petar Kirov [ZombineDev] wrote: *** But in any case, the null-terminated string was just an example application. I'm interested in a fast way to determine the "storage class" of the memory a slice or a pointer point to. I'm expecting some magic along the lines of checking the range of

Targetting the 8051

2017-06-24 Thread FoxyBrown via Digitalmars-d-learn
Is it possible? Using LDC or GCC? I do not need any libraries, druntime, etc. Just bare bones with the nice D meta features(templates, etc).

Re: What's the fastest way to check if a slice points to static data

2017-06-24 Thread via Digitalmars-d-learn
On Saturday, 24 June 2017 at 14:18:33 UTC, ketmar wrote: with the edge case when something like the code i posted below managed to make `a` perfectly aligned with r/o area, and you got segfault by accising out-of-bounds byte. BTW, are you sure? AFAIU, it doesn't matter if the CTFE engine

Re: Help me escape optional parens hell

2017-06-24 Thread Meta via Digitalmars-d-learn
On Saturday, 24 June 2017 at 08:08:33 UTC, ketmar wrote: Meta wrote: So I have no clue what I'm doing wrong. This is driving me insane. h. known $#^#$@^@%. enum SymName = ().stringof[2..$]; // this, instead of symbol.stringof dirty hack, let's hope that DMD devs won't change

Re: Are padding bits always zero?

2017-06-24 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/24/17 10:32 AM, Honey wrote: On Saturday, 24 June 2017 at 12:41:47 UTC, Steven Schveighoffer wrote: Any padding bits between fields should be 0 as long as the struct is initialized (i.e. as long as you don't do Struct s = void). Padding bits after the fields I assume would be 0, but I

Re: Are padding bits always zero?

2017-06-24 Thread Honey via Digitalmars-d-learn
On Saturday, 24 June 2017 at 14:03:16 UTC, Adam D. Ruppe wrote: On Saturday, 24 June 2017 at 12:41:47 UTC, Steven Schveighoffer wrote: There is no spec for this, but I know that when the compiler has to fill gaps with something it chooses 0. I'm almost certain there at least used to be a spec

Re: Are padding bits always zero?

2017-06-24 Thread Honey via Digitalmars-d-learn
On Saturday, 24 June 2017 at 14:03:16 UTC, Adam D. Ruppe wrote: On the other hand though, the zero padding requirement does simplify equality to being memcmp. That's the reason for my question.

Re: Are padding bits always zero?

2017-06-24 Thread Honey via Digitalmars-d-learn
On Saturday, 24 June 2017 at 12:41:47 UTC, Steven Schveighoffer wrote: Any padding bits between fields should be 0 as long as the struct is initialized (i.e. as long as you don't do Struct s = void). Padding bits after the fields I assume would be 0, but I don't know if this is defined. It's

Re: What's the fastest way to check if a slice points to static data

2017-06-24 Thread ketmar via Digitalmars-d-learn
ketmar wrote: p.s.: btw, druntime tries to avoid that edge case by not checking for trailing out-of-bounds zero if string ends exactly on dword boundary. it will miss some strings this way, but otherwise it is perfectly safe. oops. not druntime, phobos, in `std.string.toStringz()`.

Re: What's the fastest way to check if a slice points to static data

2017-06-24 Thread ketmar via Digitalmars-d-learn
p.s.: btw, druntime tries to avoid that edge case by not checking for trailing out-of-bounds zero if string ends exactly on dword boundary. it will miss some strings this way, but otherwise it is perfectly safe.

Re: What's the fastest way to check if a slice points to static data

2017-06-24 Thread ketmar via Digitalmars-d-learn
Petar Kirov [ZombineDev] wrote: Please note that not all static immutable strings have to be null terminated. It is possible to generate a string at ctfe which may appear the same as string literal, but does not have the \0 at the end. But in that case, the check `s.ptr[s.length] == 0` in

Re: What's the fastest way to check if a slice points to static data

2017-06-24 Thread via Digitalmars-d-learn
On Saturday, 24 June 2017 at 13:11:02 UTC, Stefan Koch wrote: On Saturday, 24 June 2017 at 12:22:54 UTC, Petar Kirov [ZombineDev] wrote: [ ... ] /** * Returns: * A pointer to a null-terminated string in O(1) time, * (with regards to the length of the string and the required * memory, if

Re: Converting a ubyte[] to a struct with respect to endianness?

2017-06-24 Thread Stefan Koch via Digitalmars-d-learn
On Saturday, 24 June 2017 at 02:52:23 UTC, Felix wrote: I'm trying to read in just the first part of a .png file to peek at it's width and height without loading in the whole file. I'm using FreeImage for reading the whole file but since it doesn't have a function to let me peek at the image

Re: Are padding bits always zero?

2017-06-24 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 24 June 2017 at 12:41:47 UTC, Steven Schveighoffer wrote: There is no spec for this, but I know that when the compiler has to fill gaps with something it chooses 0. I'm almost certain there at least used to be a spec for this, because I remember citing a link to someone who then

Re: What's the fastest way to check if a slice points to static data

2017-06-24 Thread Stefan Koch via Digitalmars-d-learn
On Saturday, 24 June 2017 at 12:22:54 UTC, Petar Kirov [ZombineDev] wrote: [ ... ] /** * Returns: * A pointer to a null-terminated string in O(1) time, * (with regards to the length of the string and the required * memory, if any) or `null` if * the time constraint * can't be met. */

Re: Are padding bits always zero?

2017-06-24 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/24/17 4:44 AM, Honey wrote: Hi everyone! Are there any guarantees about the values of padding bits in structs? Thanks, Honey Any padding bits between fields should be 0 as long as the struct is initialized (i.e. as long as you don't do Struct s = void). Padding bits after the fields

Re: Converting a ubyte[] to a struct with respect to endianness?

2017-06-24 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/24/17 1:18 AM, H. S. Teoh via Digitalmars-d-learn wrote: On Fri, Jun 23, 2017 at 10:10:22PM -0700, Ali Çehreli via Digitalmars-d-learn wrote: On 06/23/2017 09:26 PM, Felix wrote: That works, thanks! I've just tried this, which seems cleaner: import std.stdio; import std.system; import

What's the fastest way to check if a slice points to static data

2017-06-24 Thread via Digitalmars-d-learn
I need a fast and hopefully relatively cross-platform (ELF, OMF, COFF and MachO) way of checking if a slice points to data in the read-only section of the binary, i.e. it's pointing to a statically-allocated piece of memory. Of course a simple solution using meta programming would be: ---

Re: Help me escape optional parens hell

2017-06-24 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/24/17 3:53 AM, Meta wrote: The code: alias Response = Nullable!(string, "empty response (error)"); Response processMessage(string commandModule)(string message, bool isCommand) { import std.meta; import std.string; import std.traits; import command_uda;

Re: More optional parens concerns

2017-06-24 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, June 24, 2017 9:56:55 AM MDT Russel Winder via Digitalmars-d- learn wrote: > I note that: > > x.map!(…).array.sort > > is not valid, you have to have the parentheses on sort: > > x.map!(…).array.sort() > > Why? Because the built-in sort for arrays has been deprecated but not yet

Re: More optional parens concerns

2017-06-24 Thread ketmar via Digitalmars-d-learn
Russel Winder wrote: On Sat, 2017-06-24 at 11:58 +0300, ketmar via Digitalmars-d-learn wrote: Russel Winder wrote: I note that: x.map!(…).array.sort is not valid, you have to have the parentheses on sort: x.map!(…).array.sort() Why? built-in property .sort kicks in and ruins

Re: More optional parens concerns

2017-06-24 Thread Russel Winder via Digitalmars-d-learn
On Sat, 2017-06-24 at 11:58 +0300, ketmar via Digitalmars-d-learn wrote: > Russel Winder wrote: > > > I note that: > > > > x.map!(…).array.sort > > > > is not valid, you have to have the parentheses on sort: > > > > x.map!(…).array.sort() > > > > Why? > > built-in property .sort

Alias template parameter to a private function

2017-06-24 Thread Sebastien Alaiwan via Digitalmars-d-learn
Hi, I'm trying to call std.algorithm.iteration.filter with a private function as a predicate. Here's a reduced example code: // yo.d import std.algorithm; void moduleEntryPoint() { privateFunction1(); privateFunction2(); } private: void privateFunction1() { auto array = [0, 1, 2, 3,

Re: More optional parens concerns

2017-06-24 Thread ketmar via Digitalmars-d-learn
Russel Winder wrote: I note that: x.map!(…).array.sort is not valid, you have to have the parentheses on sort: x.map!(…).array.sort() Why? built-in property .sort kicks in and ruins the day. luckily, it will be completely removed in the upcoming major release.

More optional parens concerns

2017-06-24 Thread Russel Winder via Digitalmars-d-learn
I note that: x.map!(…).array.sort is not valid, you have to have the parentheses on sort: x.map!(…).array.sort() Why? -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip:

Are padding bits always zero?

2017-06-24 Thread Honey via Digitalmars-d-learn
Hi everyone! Are there any guarantees about the values of padding bits in structs? Thanks, Honey

Re: Help me escape optional parens hell

2017-06-24 Thread ketmar via Digitalmars-d-learn
ketmar wrote: Meta wrote: So I have no clue what I'm doing wrong. This is driving me insane. h. known $#^#$@^@%. enum SymName = ().stringof[2..$]; // this, instead of symbol.stringof dirty hack, let's hope that DMD devs won't change `.toString()` output (i.e. first two chars will

Re: Help me escape optional parens hell

2017-06-24 Thread ketmar via Digitalmars-d-learn
Meta wrote: So I have no clue what I'm doing wrong. This is driving me insane. h. known $#^#$@^@%. enum SymName = ().stringof[2..$]; // this, instead of symbol.stringof dirty hack, let's hope that DMD devs won't change `.toString()` output (i.e. first two chars will always be

Re: Help me escape optional parens hell

2017-06-24 Thread ketmar via Digitalmars-d-learn
ketmar wrote: `.toString()` toChars(). ;-)

Help me escape optional parens hell

2017-06-24 Thread Meta via Digitalmars-d-learn
The code: alias Response = Nullable!(string, "empty response (error)"); Response processMessage(string commandModule)(string message, bool isCommand) { import std.meta; import std.string; import std.traits; import command_uda; mixin("import " ~ commandModule ~ ';');

Re: Converting a ubyte[] to a struct with respect to endianness?

2017-06-24 Thread Ali Çehreli via Digitalmars-d-learn
On 06/23/2017 10:18 PM, H. S. Teoh via Digitalmars-d-learn wrote: On Fri, Jun 23, 2017 at 10:10:22PM -0700, Ali Çehreli via Digitalmars-d-learn wrote: On 06/23/2017 09:26 PM, Felix wrote: That works, thanks! I've just tried this, which seems cleaner: import std.stdio; import std.system;