Re: Interfacing C++ to D -- or better: C++ -- C --- D (DLL)

2014-03-30 Thread evilrat

On Saturday, 29 March 2014 at 15:03:18 UTC, BeschBesch wrote:
I want to use a set of functions that rely on a library which 
is not available for D (Harfbuzz). So I just wrote myself a set 
a functions that will do anything I need (Init, Free, Fonts, 
Text, etc).


The DLL is working fine and dynamically linking should work (it 
does in C++ -- LoadLibrary). Unfortunately, i get Error 42 
evertime i try to call a function.


As i suspected that some things written by me could be 
responsible for this, I build a test application with three 
functions (Test, Test2,Test3), got myself the D-.lib for 
statical-linking (implib-Dmd) and tried that one out. The 
result: Error 42.


What did I miss that can still cause linker errors? So far I 
did:


DLL is written in C (C89 -- VC++ only can afford that one it 
seems). And yes I changed it to C from C++ in the compiler 
settings.


DLL has an entry function AND a module-definition file (.def). 
A .lib-file is created alongside with the DLL (.def specified 
in porject settings). OF COURSE, I built a D-conform .lib by 
using implib with /s option (and without) /s option.


Thank you, if you are still reading.

CODE:
// declared in Test.h
void Test1(void);
int Test2(int i);
long Test3(int* ptr);
// Example implementation in Test.c (include test.h
void Test1(void)
{
printf(FUNCTION WAS CALLED\n);
}
/ .def-File
LIBRARY CtoDtestDll
EXPORTS
Test1 @1
Test2 @2
Test3 @3
/// SETTINGS (C++)
Calling-convention: __cdecl (= std. ??)
Compiling for:   C (/TC)
/// SETTINGS (LINKER)
Modul-definition-file: CTestDll.def

// D-Module for usage with .lib from .dll (implib! from bup)
module CoreFuncs; // just a name

pragma (lib,CtoDtestDll.lib); // statically linking (could 
user settings too)


extern (C)   /// needed so the compiler can link the functions
{

void Test();

}


have you looked at exported symbols in dll? it may be C++ from 
what you said(lib not designed for C++?), so you can try 
extern(C++) on D side instead extern(C). or add manually #ifdef 
__cplusplus and extern C on lib side(if its not that big of 
course).


Re: Getting the current datetime converted into a string for writing to stdout

2014-03-30 Thread Andre Artus

On Saturday, 29 March 2014 at 05:01:14 UTC, Gary Miller wrote:
-- SNIP --

I only need it down to the second and from my current machine 
so I don't need to use any of the more accurate clocks used 
for benchmarking right now.


On Saturday, 29 March 2014 at 09:30:44 UTC, Gary Willoughby 
wrote:

This is the easiest way i know.

import std.stdio;
import std.datetime;

void main(string[] args)
{
writeln(Clock.currTime.toLocalTime.toSimpleString);
}


toSimpleString (and toString) returns a string with fractional
seconds; which I suspect is not what Gary Miller wants.



It would be nice if all the primitive datatypes had a 
consistent toString cast or function but since I'm not seeing 
those I guess writeFormatted must be the preferred way of 
getting values into string format.


See std.conv and the 'to' function.

data.to!(string)
data.to!(int)
etc...

http://dlang.org/phobos/std_conv.html#.to


Considering the international spread of D users I find it
surprising that the date  time formatting functions in phobos
are not localizable.

When working in Windows I prefer to wrap GetDateFormatEx, but it
would be nice to have an OS agnostic version.


Re: Interfacing C++ to D -- or better: C++ -- C --- D (DLL)

2014-03-30 Thread BeschBesch
have you looked at exported symbols in dll? it may be C++ from 
what you said(lib not designed for C++?), so you can try 
extern(C++) on D side instead extern(C). or add manually #ifdef 
__cplusplus and extern C on lib side(if its not that big of 
course).


First of all: I started with a C++-dll and tried to declare the 
functions with extern (C++) -- did not work. Then I switched the 
compiler to C. As VC++ still uses only C89 as standard you can 
imagine there is NOTHING C++-ish that could have survived. I also 
tested this DLL in a C++-application (static) and it worked like 
a charm.


ALSO:
As i suspected that some things written by me could be 
responsible for this, I build a test application with three 
functions (Test, Test2,Test3), got myself the D-.lib for 
statical-linking (implib-Dmd) and tried that one out. The 
result: Error 42.


So, actually I am trying to get a DLL containing three 
single-line test functions to work. What I have already achieved 
is to load it dynamically, and it worked 
(Derelict.Util.Loader.SharedLibLoader Class). As I also use the 
Derelict-packages, I may intend to build a custom loader class as 
a work-around.


Re: Interfacing C++ to D -- or better: C++ -- C --- D (DLL)

2014-03-30 Thread evilrat

On Sunday, 30 March 2014 at 11:16:16 UTC, BeschBesch wrote:
have you looked at exported symbols in dll? it may be C++ from 
what you said(lib not designed for C++?), so you can try 
extern(C++) on D side instead extern(C). or add manually 
#ifdef __cplusplus and extern C on lib side(if its not that 
big of course).


First of all: I started with a C++-dll and tried to declare the 
functions with extern (C++) -- did not work. Then I switched 
the compiler to C. As VC++ still uses only C89 as standard you 
can imagine there is NOTHING C++-ish that could have survived. 
I also tested this DLL in a C++-application (static) and it 
worked like a charm.


ALSO:
As i suspected that some things written by me could be 
responsible for this, I build a test application with three 
functions (Test, Test2,Test3), got myself the D-.lib for 
statical-linking (implib-Dmd) and tried that one out. The 
result: Error 42.


So, actually I am trying to get a DLL containing three 
single-line test functions to work. What I have already 
achieved is to load it dynamically, and it worked 
(Derelict.Util.Loader.SharedLibLoader Class). As I also use the 
Derelict-packages, I may intend to build a custom loader class 
as a work-around.


it is not clear for me what are you trying to achieve and 
where/what is error 42. do you want implicit loading? if so, have 
you tried converting lib using coffimpblib tool from 
ftp.digitalmars.com? keep in mind that you don't have to convert 
libs when building in 64 bit mode.


Re: Template magic exercise

2014-03-30 Thread Jack Applegame

Sorry. Not getter, should be elementsAccessor instead.


Template magic exercise

2014-03-30 Thread Jack Applegame
Target is to create a template for mapping member array 
accessor to member function.


For example:

class Foo {
  ...
  int elementsAccessor(size_t index) { ... }
  ...
  mixin MagicTemplateMixin!(elements, elementsAccessor);
  // or better
  alias elements = SuperMagicTemplate!elementsAccessor;
}

and now we can call Foo.getter like this:

auto foo = new Foo;
int el = foo.elements[10]; // int el = foo.getter(10);

I wrote poor and ugly solution with proxy structure:
http://dpaste.dzfl.pl/93085910f8c7
I hate it. :)

I need more powerful spell, but my magic level is too low.


scanner parser generator

2014-03-30 Thread needs

hi,
i need a scanner parser generator that generates d code. i found
abandoned, primitive and d1 projects. is there something one can
use with the current version and -m64?

regards


Re: scanner parser generator

2014-03-30 Thread evilrat

On Sunday, 30 March 2014 at 13:08:08 UTC, needs wrote:

hi,
i need a scanner parser generator that generates d code. i found
abandoned, primitive and d1 projects. is there something one can
use with the current version and -m64?

regards


pegged?
http://code.dlang.org


Re: Template magic exercise

2014-03-30 Thread anonymous

On Sunday, 30 March 2014 at 12:42:16 UTC, Jack Applegame wrote:
Target is to create a template for mapping member array 
accessor to member function.


For example:

class Foo {
  ...
  int elementsAccessor(size_t index) { ... }
  ...
  mixin MagicTemplateMixin!(elements, elementsAccessor);
  // or better
  alias elements = SuperMagicTemplate!elementsAccessor;
}

and now we can call Foo.getter like this:

auto foo = new Foo;
int el = foo.elements[10]; // int el = foo.getter(10);

I wrote poor and ugly solution with proxy structure:
http://dpaste.dzfl.pl/93085910f8c7
I hate it. :)

I need more powerful spell, but my magic level is too low.


Maybe less is more here:

---
struct OpIndexFromAccessor(R, PS) {
 R delegate(PS) dg;
 R opIndex(PS ps) { return dg(ps); }
}
auto opIndexFromAccessor(R, PS)(R delegate(PS) accessor) {
 return OpIndexFromAccessor!(R, PS)(accessor);
}
class Foo {
 int elementsAccessor(size_t index) { ... }
 auto elements() {return
opIndexFromAccessor(elementsAccessor);}
 /* This is hardly more complex than the mixin or alias
variants. */
}
---


Re: scanner parser generator

2014-03-30 Thread needs

thanks i take a look at it. actually i thought along the lines of
antlr or coo/r.


On Sunday, 30 March 2014 at 13:20:49 UTC, evilrat wrote:

On Sunday, 30 March 2014 at 13:08:08 UTC, needs wrote:

hi,
i need a scanner parser generator that generates d code. i 
found
abandoned, primitive and d1 projects. is there something one 
can

use with the current version and -m64?

regards


pegged?
http://code.dlang.org


Re: Template magic exercise

2014-03-30 Thread Jack Applegame

Wraper structure again. Is there solution without it?


Does anybody have an example of overloading a function to accept char[] or string parameter

2014-03-30 Thread Gary Miller


In a lot of my string manipulation functions I need the 
flexibility of passing a string but other times I need to pass a 
char[] so that I can change the contents of the string in the 
function.  Because the string data type is immutable I get errors 
if I pass it as a parameter and try to change it in the function.


Can I overload a function to work for either a char[] or a string?

Many of the library functions that I need to call require string 
arguments such as the replace below.


I believe I can create a wrapper function for it like this to 
still get at the functionality. But this seems like a lot of work 
for every function.


I'm almost sorry that the the string datatype was created because 
then probably all the library string handling would have been 
written for char[].


Are there any alternate libraries for D that have a mutable 
string datatype or is there a way to override the immutable 
characteristic of the string datatype by reallocating it or 
something?


I realize that the reduced memory reallocation in string handling 
is probably a major reason that D is faster than other more 
dynamic languages like Python but


Maybe the functions in std.string could be overloaded into a 
std.mutablestring library at some point to eliminate emulate the 
functionality of more dynamic languages for those programs that 
need it.


char[] ReplaceAllSubstrings(inout char[] Original,
in char[] SearchString,
in char[] Substring)
{
string SOriginal = Original.dup;
string SSearchString = SearchString.dup;
string SSubstring = Substring.dup;
SOriginal.replace(SSearchString, SSubstring);
return Original.dup;
}





Does anyone have an example of use of core.sync.Condition

2014-03-30 Thread Matt
I have little experience in multi-threading programming, and was 
digging into std.concurrency, but I don't really understand the 
Condition class as it was used there. Could someone provide a 
bare-bones use of this class? I would be much obliged, thanks.


Re: Does anybody have an example of overloading a function to accept char[] or string parameter

2014-03-30 Thread evilrat

On Sunday, 30 March 2014 at 15:58:52 UTC, Gary Miller wrote:

Are there any alternate libraries for D that have a mutable 
string datatype or is there a way to override the immutable 
characteristic of the string datatype by reallocating it or 
something?


string.dup property does a copy of original array(! note that 
this is array property and would work for any other arrays and 
slices).


you can have overloaded variant for strings but i think compiler 
would optimize to call everything as string variant, you can 
figure this out on your own.


Re: Getting a class' name, but not fully qualified?

2014-03-30 Thread Matt

On Friday, 28 March 2014 at 13:38:01 UTC, Dicebot wrote:

.classinfo name is name for actual class instance referenced 
from the variable it was applied to. It happens to be fully 
qualified but key difference here is that it uses RTTI 
(run-time type information) to access real type of 
polymorphic entity.


Thanks for the clarification, Dicebot. Does it mention those 
points in the docs, because I don't remember seeing them? So we 
could be looking at something closer to:


  import std.string;
  import std.stdio;

  class Foo{
int a;
  }

  class Bar: Foo{
int b;
  }

  void main(){
Foo bar = new Bar();
writeln (class name:  ~ 
bar.classinfo.name[bar.classinfo.name.lastIndexOf(.) .. $].dup);

  }

if I'm correct?


Re: Getting a class' name, but not fully qualified?

2014-03-30 Thread Dicebot

On Sunday, 30 March 2014 at 16:26:48 UTC, Matt wrote:

On Friday, 28 March 2014 at 13:38:01 UTC, Dicebot wrote:

.classinfo name is name for actual class instance referenced 
from the variable it was applied to. It happens to be fully 
qualified but key difference here is that it uses RTTI 
(run-time type information) to access real type of 
polymorphic entity.


Thanks for the clarification, Dicebot. Does it mention those 
points in the docs, because I don't remember seeing them? So we 
could be looking at something closer to:


  import std.string;
  import std.stdio;

  class Foo{
int a;
  }

  class Bar: Foo{
int b;
  }

  void main(){
Foo bar = new Bar();
writeln (class name:  ~ 
bar.classinfo.name[bar.classinfo.name.lastIndexOf(.) .. 
$].dup);

  }

if I'm correct?


Yes. You don't need .dup though and it will be `lastIndexOf + 1` 
for the start index but key idea is solid.


This is not explicitly mentioned anywhere because runtime type 
information (http://dlang.org/phobos/object.html#.Classinfo) is 
a wide-spread term with strong meaning.


Re: Getting a class' name, but not fully qualified?

2014-03-30 Thread Matt

On Sunday, 30 March 2014 at 16:33:47 UTC, Dicebot wrote:
This is not explicitly mentioned anywhere because runtime type 
information (http://dlang.org/phobos/object.html#.Classinfo) 
is a wide-spread term with strong meaning.


what about .stringof? I don't think it mentions about the fact 
that that gets the string at compile time


Re: Getting a class' name, but not fully qualified?

2014-03-30 Thread Dicebot

On Sunday, 30 March 2014 at 16:42:54 UTC, Matt wrote:

On Sunday, 30 March 2014 at 16:33:47 UTC, Dicebot wrote:
This is not explicitly mentioned anywhere because runtime 
type information 
(http://dlang.org/phobos/object.html#.Classinfo) is a 
wide-spread term with strong meaning.


what about .stringof? I don't think it mentions about the fact 
that that gets the string at compile time


http://dlang.org/property.html#stringof

Term such as source representation has no meaning for run-time, 
at least not for natively compiled languages.


Re: Does anybody have an example of overloading a function to accept char[] or string parameter

2014-03-30 Thread bearophile

Gary Miller:


char[] ReplaceAllSubstrings(inout char[] Original,
in char[] SearchString,
in char[] Substring)
{
string SOriginal = Original.dup;
string SSearchString = SearchString.dup;
string SSubstring = Substring.dup;
SOriginal.replace(SSearchString, SSubstring);
return Original.dup;
}


Here if you care for some efficiency you need to dup only 
SOriginal. And at the end you can call assumeUnique if you want 
to return a string (or you can use a less efficient idup).


Note that in D string/function names start with a lowercase.

It's also better to use auto instead of string in that 
function, because the result of dup is not a string.


Bye,
bearophile


Re: Does anyone have an example of use of core.sync.Condition

2014-03-30 Thread Ali Çehreli

On 03/30/2014 09:09 AM, Matt wrote:

I have little experience in multi-threading programming, and was digging
into std.concurrency, but I don't really understand the Condition class
as it was used there. Could someone provide a bare-bones use of this
class? I would be much obliged, thanks.


I haven't used it either but I think this page explains the need for it:


http://en.wikipedia.org/wiki/Monitor_%28synchronization%29#Condition_variables

Ali



Re: Does anyone have an example of use of core.sync.Condition

2014-03-30 Thread Jack Applegame

On Sunday, 30 March 2014 at 16:09:37 UTC, Matt wrote:
I have little experience in multi-threading programming, and 
was digging into std.concurrency, but I don't really understand 
the Condition class as it was used there. Could someone provide 
a bare-bones use of this class? I would be much obliged, thanks.


Simple example of sending signal from one thread to another.

import std.stdio;
import core.thread;
import core.sync.condition;


class Foo {
bool signal = false;
Condition condition;
this() {
condition = new Condition(new Mutex);
}
void sendSignal() {
writeln(sending signal);
synchronized(condition.mutex) {
signal = true;
condition.notify();
}
writeln(signal sent);
}
void waitForSignal() {
new Thread({
writeln(waiting for signal);
synchronized(condition.mutex) {
while(!signal) {
condition.wait();
}
}
writeln(signal received);
}).start();
}
}

void main() {
auto foo = new Foo;
foo.waitForSignal();
Thread.sleep(2.seconds);
foo.sendSignal();
}




Change forum password

2014-03-30 Thread Carl Sturtivant

I don't know where to ask this.
How do I change the password associated with my use of this forum?


destructor, postblit constructor --- force calling always

2014-03-30 Thread Carl Sturtivant


Context: struct values that are (non-ref) parameters  local 
variables.


Is there a way to arrange that a particular struct's special 
constructors (postblit,destructor) should always be called even 
on move, or on destruction of a default initialized value, 
temporary or not, etcetera, i.e. ensure they should always be 
called?


Is a struct's destructor always called when it goes out of scope, 
even if it is default initialized?




Avoiding Range Checks in Slice Expressions

2014-03-30 Thread Nordlöw
Does DMD currently avoid range checks in array slice expressions 
such as


f(x[0..$/2])
f(x[$/2..$])

typically found in divide-and-conquer algorithms such as 
quicksort?


If not, what would it require to implement it?


Re: Avoiding Range Checks in Slice Expressions

2014-03-30 Thread bearophile

Nordlöw:

Does DMD currently avoid range checks in array slice 
expressions such as


f(x[0..$/2])
f(x[$/2..$])


You have two simple ways to answer your question: try to go past 
the slices and look if D raises an error, and look at the asm 
produced with various compiler switches.


Bye,
bearophile


Re: destructor, postblit constructor --- force calling always

2014-03-30 Thread Benjamin Thaut

Am 30.03.2014 21:35, schrieb Carl Sturtivant:


Context: struct values that are (non-ref) parameters  local variables.

Is there a way to arrange that a particular struct's special
constructors (postblit,destructor) should always be called even on move,
or on destruction of a default initialized value, temporary or not,
etcetera, i.e. ensure they should always be called?

Is a struct's destructor always called when it goes out of scope, even
if it is default initialized?



D's structs don't have identity. That means, they can be moved without 
notice at any point in the program. AFAIK the compiler even does that 
when handling exceptions in a few cases (e.g. with structs on the 
stack). Having moveable value types allows for a lot of optimizations, 
both on the compiler side as well as when implementing e.g. containers 
in user code. So no there is no way to always get notified. I also 
proposed a move constructor in the past, but the idea was not well 
recieved. When I needed a move constructor, usually adding another level 
of indirection solved the problem.


Kind Regards
Benjamin Thaut


Re: Template magic exercise

2014-03-30 Thread Artur Skawina
On 03/30/14 15:36, Jack Applegame wrote:
 Wraper structure again. Is there solution without it?

No. D only allows op overloading in structs/unions/classes, so
one of those will be necessary. The simpliest solution would be
something like:

   class C {
  int elementsAccessor(size_t index) { ... }

  @property ref elements() {
 struct Elements(O) {
auto opIndex(size_t i) {
   auto o = *cast(O*)this;
   return o.elementsAccessor(i);
}
@disable this(this);
 }
 return *cast(Elements!(typeof(this))*)this;
  }
   }

and if you need that code to be @safe:

 auto opIndex(size_t i) @safe {
auto o = (f) @trusted { return *cast(O*)f; }(this);
return o.elementsAccessor(i);
 }
 // etc

artur


Re: Template magic exercise

2014-03-30 Thread Frustrated

On Sunday, 30 March 2014 at 12:42:16 UTC, Jack Applegame wrote:
Target is to create a template for mapping member array 
accessor to member function.


For example:

class Foo {
  ...
  int elementsAccessor(size_t index) { ... }
  ...
  mixin MagicTemplateMixin!(elements, elementsAccessor);
  // or better
  alias elements = SuperMagicTemplate!elementsAccessor;
}

and now we can call Foo.getter like this:

auto foo = new Foo;
int el = foo.elements[10]; // int el = foo.getter(10);

I wrote poor and ugly solution with proxy structure:
http://dpaste.dzfl.pl/93085910f8c7
I hate it. :)

I need more powerful spell, but my magic level is too low.


While this doesn't work maybe you can put in a request to add the 
functionality:


import std.stdio, std.cstream;
import std.variant;
import std.conv;

class Foo
{
Variant[] opDispatchIndex(string name, E...)(E elements)
{
writeln(name);
return null;
}
}

void main()
{

auto f = new Foo;

f.elements[3]; // Works if opDispatchIndex existed  
din.getc();
}


alternatively, if you don't mind the extra bloat:


import std.stdio, std.cstream;
import std.variant;
import std.conv;

struct opIndexStruct
{
void opIndex(in size_t i)
{
writeln(op - ,  i);
}
}

class Foo
{
@property opIndexStruct elements() { opIndexStruct o; return o; }
}

void main()
{

auto f = new Foo;

f.elements[3];

din.getc();
}



this works because elements is a property and we don't have to 
call with parenthesis. e.g., f.elements()[3]. Hence f.elements 
works. [3] is acted on by the return value of elements(), which 
is a struct that supports opIndex(one could alternative use alias 
this to wrap the struct in a type that supports indexing.


As is the code isn't very useful but should provide you with the 
ability to achieve what you want.


Re: destructor, postblit constructor --- force calling always

2014-03-30 Thread Carl Sturtivant
D's structs don't have identity. That means, they can be moved 
without notice at any point in the program. AFAIK the compiler 
even does that when handling exceptions in a few cases (e.g. 
with structs on the stack). Having moveable value types allows 
for a lot of optimizations, both on the compiler side as well 
as when implementing e.g. containers in user code. So no there 
is no way to always get notified. I also proposed a move 
constructor in the past, but the idea was not well recieved. 
When I needed a move constructor, usually adding another level 
of indirection solved the problem.


Thanks for that discussion. As you might have guessed, adding 
another level of indirection is what I was trying to avoid. A 
move constructor would have taken care of my problem.


What about destructors, are they always called, or is this 
another optimization if the struct is in it's default .init state?




Re: Change forum password

2014-03-30 Thread evilrat

On Sunday, 30 March 2014 at 19:17:38 UTC, Carl Sturtivant wrote:

I don't know where to ask this.
How do I change the password associated with my use of this 
forum?


the only way is to ask administrator.


Parsing

2014-03-30 Thread Joel
I've got a program that uses user input, but I'm having trouble 
with it.


Here's an example, the unenclosed numbers (1 2 3 in example) add 
entries:


0 Achievement
1 2 3 cWon! st4 5 6 - user input

(create 3 entries all with st4 5 6)

0 Achievement
1 house [4, 5, 6]
2 rock [4, 5, 6]
3 mouse [4, 5, 6]

cmud - user input

0 Achievement
1 house [4, 5, 6] mud
2 rock [4, 5, 6] mud
3 mouse [4, 5, 6] mud

So add the entries and while they are still hot, you can edit 
them.


I don't know if this an impossible ask for help, but I though I 
might get some help. I'll keep going over my code to work it out.


Thanks.


best D equivalent to C'stimeval

2014-03-30 Thread ed

Hi,

Just wondering what the best replacement for C timeval is in D. 
I'm looking at std.datetime.SysTime, but std.datetime is huge so 
I'm not sure.


Thanks,
ed