Re: Cycle detected between modules with ctors/dtors

2015-03-04 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/3/15 4:24 PM, rumbu wrote:

On Tuesday, 3 March 2015 at 18:55:49 UTC, Steven Schveighoffer wrote:



Only one of the listed modules has a static contructor
(system.globalization) and that constructor doesn't use any information
from other modules.


It's a complex problem. Because we don't control the linker, we cannot
establish an order of execution for the module system. It must be a
directed acyclic graph. Although, cycles are allowed that only involve
a single module with ctor/dtors, or with no ctor/dtors. But we have to
detect it at runtime during startup.

two of those modules should have static ctor or dtor (they may have
either to cause the error).


The message is not too helpful, I have no clue where to start searching
for the error source, the source code has more than 25k LOC. In fact, I
don't believe this error is true, since I have only two non-linked
modules with static contructors.


Just look for static this and static ~this inside those files
identified.


-Steve


One of the listed modules had a ctor. Another one - curiously - not
listed in the cyclic error - had a static dtor, and this module was
imported by two of them.


curiously not listed? That doesn't make any sense. If there is not a 
cycle in the listed modules, where at least 2 modules have a static ctor 
or static dtor, then there is a bug in the cycle detection code.




Anyway, reduced case scenario:

module a;
import b;
static this();
-
module b;
import a;
static ~this();

As I supposed, this is a false error, the order of execution seems very
clear to me, but not for the runtime. Even if I have two ctors, I think
it's better to relax this rule, maybe the constructors are not dependent
one of each other to have cyclic errors. In my case, one of the
constructors loads some resources bound to the executable and the
destructor is simply a winapi call to CoUninitialize, totally unrelated.




Clearly, the above is not an issue, but at the moment, the code treats 
having a ctor and having a dtor as the same thing. How to fix it? I'm 
not exactly sure, you need to have the modules sorted according to ctor 
and also according to dtor. The runtime is supposed to run dtors in 
reverse order from ctor calls, and I think we should not break that. But 
there is definitely room to allow for reordering when ctors are not 
affected.


I'll have to think about it some more.

-Steve


Re: Cycle detected between modules with ctors/dtors

2015-03-04 Thread rumbu via Digitalmars-d-learn
On Wednesday, 4 March 2015 at 16:08:00 UTC, Steven Schveighoffer 
wrote:




curiously not listed? That doesn't make any sense. If there is 
not a cycle in the listed modules, where at least 2 modules 
have a static ctor or static dtor, then there is a bug in the 
cycle detection code.


Source code is available here: https://github.com/rumbu13/sharp

The runtime error chain was system.globalization - 
internals.locale -

system.runtime.interopservices - system.io -
system.globalization

Three of these modules import internals.resources containing a 
static constructor. The static dtor is in 
system.runtime.interopservices.


Clearly, the above is not an issue, but at the moment, the code 
treats having a ctor and having a dtor as the same thing. How 
to fix it? I'm not exactly sure, you need to have the modules 
sorted according to ctor and also according to dtor. The 
runtime is supposed to run dtors in reverse order from ctor 
calls, and I think we should not break that. But there is 
definitely room to allow for reordering when ctors are not 
affected.


I'll have to think about it some more.

-Steve


Why bother? I'll leave this to the programmer, he can take full 
responsibility to not mess up module ctors/dtors. I remember the 
old Delphi days where this was assumed from the start: 
initialization and finalization order of execution (equivalent of 
static ctor/dtor) were not guaranteed. It was better to obtain an 
AccessViolation or a StackOverflow and correct the circular 
reference, instead of a NEVER running executable.




Re: Filling a char array with letters and element type of char[]

2015-03-04 Thread Kadir Erdem Demir via Digitalmars-d-learn

@Tobias @Ali @Jonathan @Steven

Thanks alot for your answers.
I will try to learn as much as I can from them.

Regards
Erdem


Re: Cycle detected between modules with ctors/dtors

2015-03-04 Thread ketmar via Digitalmars-d-learn
On Wed, 04 Mar 2015 11:06:50 -0500, Steven Schveighoffer wrote:

 Clearly, the above is not an issue, but at the moment, the code treats
 having a ctor and having a dtor as the same thing. How to fix it? I'm
 not exactly sure, you need to have the modules sorted according to ctor
 and also according to dtor. The runtime is supposed to run dtors in
 reverse order from ctor calls, and I think we should not break that. But
 there is definitely room to allow for reordering when ctors are not
 affected.
 
 I'll have to think about it some more.

maybe do simply the thing you wrote, and have two independent lists. or, 
strictly saying, four independent lists: for shared ctors, for static 
ctors, for static dtors, for shared dtors.

signature.asc
Description: PGP signature


how to write a string to a c pointer?

2015-03-04 Thread zhmt via Digitalmars-d-learn
I am writing a asio binding. Objects need to be serialized into a 
buffer (void *),


for example, write utf8 string into buffer,
write int into buffer,
write long into buffer,

Here is my class

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;
}
}

how can i write a utf8 string into the buffer?


Re: I want to introduce boost_asio to dlang

2015-03-04 Thread ketmar via Digitalmars-d-learn
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. you may find that you don't need it at all.

signature.asc
Description: PGP signature


Getting the socket information from HTTP and DNS sessions

2015-03-04 Thread Kadir Erdem Demir via Digitalmars-d-learn
I have been ask to write a test tool which initiates 
DNS-HTTP-HTTPS-TCP sessions. And ofcourse I wrote this with D.



For HTTP I used std.net like m_HTTP = 
HTTP(m_url);m_HTTP.perform();

For DNS I simply used getAddressInfo(m_domainName);

Than tool makes some simple checks  which are npt subjects of 
this thread and the tool works well,


But now I need to make an improvement. I need to get sourceIP(ip 
which program runs), sourcePort( eg: 53212 not 80), destIP, 
destPort(most probably 80) from the session which I initiated.


I need functions like std.socket.localAddress, 
std.socket.remoteAddress for HTTP-DNS but couldn't managed to 
find them.


Is there any way that I can get socket info in HTTP and DNS 
sessions which I initiated?


Regards
Kadir Erdem


I want to introduce boost_asio to dlang

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

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?


Re: is expression and type tuples

2015-03-04 Thread bearophile via Digitalmars-d-learn

Jack Applegame:


On Tuesday, 3 March 2015 at 17:49:24 UTC, bearophile wrote:

That's 1 + n-1 :-)

Could you please explain what does '1 + n-1' mean?


This is your code:


template Is(ARGS...) if(ARGS.length % 2 == 0) {
   enum N = ARGS.length/2;
   static if(N == 1) enum Is = is(ARGS[0] : ARGS[1]);
   else enum Is = is(ARGS[0] : ARGS[N])  Is!(ARGS[1..N], 
ARGS[N+1..$]);

}


The recursion scheme you are using is working on a single item (a 
single pair of items), and then calling the recursion on all 
other items but the first.


If you look in std.traits you see examples of a different 
recursion that reduces compilation time:


template isExpressionTuple(T ...)
{
static if (T.length = 2)
enum bool isExpressionTuple =
isExpressionTuple!(T[0 .. $/2]) 
isExpressionTuple!(T[$/2 .. $]);
else static if (T.length == 1)
enum bool isExpressionTuple =
!is(T[0])  __traits(compiles, { auto ex = T[0]; });
else
enum bool isExpressionTuple = true; // default
}


Bye,
bearophile


Incorrect display in Cyrillic Windows

2015-03-04 Thread Dennis Ritchie via Digitalmars-d-learn

Hi.
It's normal for Windows?

http://i.imgur.com/TEx4H3k.png

import std.uni;
import std.stdio;

void main() {
string s;
foreach(ch; CodepointSet('А', 'Я' + 1, 'а', 'я' + 1).byCodepoint)
s ~= ch;
writeln(s);
writeln(s[0] = , s[0]);
}


Re: Incorrect display in Cyrillic Windows

2015-03-04 Thread Martin Krejcirik via Digitalmars-d-learn

On Wednesday, 4 March 2015 at 11:32:40 UTC, Dennis Ritchie wrote:

Hi.
It's normal for Windows?


You have to set your console encoding to UTF-8 first.

You can do it by command chcp 65001 or by calling Windows API 
function:


extern(Windows) BOOL SetConsoleOutputCP( UINT );
SetConsoleOutputCP( 65001 );

Also change your font to Lucida Console.



[rosettacode] std.container.DList problems

2015-03-04 Thread bearophile via Digitalmars-d-learn

This code used to work in DMD 2.065:
http://rosettacode.org/wiki/Strand_sort#D


import std.stdio, std.container;

DList!T strandSort(T)(DList!T list) {
static DList!T merge(DList!T left, DList!T right) {
DList!T result;
while (!left.empty  !right.empty) {
if (left.front = right.front) {
result.insertBack(left.front);
left.removeFront();
} else {
result.insertBack(right.front);
right.removeFront();
}
}
result.insertBack(left[]);
result.insertBack(right[]);
return result;
}

DList!T result, sorted, leftover;

while (!list.empty) {
leftover.clear();
sorted.clear();
sorted.insertBack(list.front);
list.removeFront();
foreach (item; list) {
if (sorted.back = item)
sorted.insertBack(item);
else
leftover.insertBack(item);
}
result = merge(sorted, result);
list = leftover;
}

return result;
}

void main() {
auto lst = DList!int([-2,0,-2,5,5,3,-1,-3,5,5,0,2,-4,4,2]);
foreach (e; lst.strandSort)
write(e,  );
}



Now it gives a runtime error like 
phobos\std\container\dlist.d(329): DList.front: List is empty. 
I think the cause is that list = leftover; has a different 
semantics. Is this a regression fit for Bugzilla?


Bye,
bearophile


Re: Incorrect display in Cyrillic Windows

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

https://issues.dlang.org/show_bug.cgi?id=2742


Re: Incorrect display in Cyrillic Windows

2015-03-04 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 4 March 2015 at 12:14:01 UTC, Martin Krejcirik 
wrote:

You have to set your console encoding to UTF-8 first.

You can do it by command chcp 65001 or by calling Windows API 
function:


extern(Windows) BOOL SetConsoleOutputCP( UINT );
SetConsoleOutputCP( 65001 );

Also change your font to Lucida Console.


Thanks.