Re: COM Expertise needed: COM Callbacks

2017-05-05 Thread Mike B Johnson via Digitalmars-d-learn
I've modified the code and it seems to call GetTypeInfo but the 
values passed seem frivolous. The modified code I'm using is 
below. Maybe we can get this to work? I'm interested in a 
photoshop interop too. Seems like it should be rather trivial to 
get to work but things don't add up ;/







module main;
import std.stdio;

import core.sys.windows.winnt;

import Gen;

string Guid2Str(GUID guid)
{
import std.conv, std.string, std.algorithm;
auto bytes = cast(byte[16])guid;
	auto a = to!string(*(cast(DWORD*)(bytes[0..4])), 
16).rightJustify(8, '0');
	auto b = to!string(*(cast(WORD*)(bytes[4..6])), 
16).rightJustify(4, '0');
	auto c = to!string(*(cast(WORD*)(bytes[6..8])), 
16).rightJustify(4, '0');
	auto d = to!string(*(cast(BYTE*)(bytes[8..9])), 
16).rightJustify(1, '0');
	auto e = to!string(*(cast(BYTE*)(bytes[9..10])), 
16).rightJustify(1, '0');

auto q = bytes[10..16];
reverse(q);
	auto f = to!string(*(cast(long*)(q)) & 0x, 
16).rightJustify(12, '0');


auto ret = a~"-"~b~"-"~c~"-"~d~e~"-"~f;
return ret;
}


// Creates a guid from a guid hex string. e.g., 
"B3C35001-B625-48D7-9D3B-C9D66D9CF5F1" -> {0xC09F153E, 0xDFF7, 
0x4EFF, [0xA5, 0x70, 0xAF, 0x82, 0xC1, 0xA5, 0xA2, 0xA8]}

GUID GuidN(string str)
{
// 3   2   15E5C5D5C5B5A4L4H
// 4dc0c1dbecc8691b 49af51c5f6655e85

//DEFINE_GUID(IID_INoMarshal,0xecc8691b,0xc1db,0x4dc0,0x85,0x5e,0x65,0xf6,0xc5,0x51,0xaf,0x49);
//MIDL_INTERFACE("ecc8691b-c1db-4dc0-855e-65f6c551af49")

import std.string;
GUID guid;
auto bytes = cast(byte*)(cast(void*));
if (str == "") return IID.init;
	auto parts = split(str, "-"); auto p3 = parts[3].rightJustify(4, 
'0'); auto p4 = parts[4].rightJustify(12, '0');


	*(cast(long*)(bytes[0..8])) = to!long(parts[2].rightJustify(4, 
'0')~parts[1].rightJustify(4, '0')~parts[0].rightJustify(8, '0'), 
16);	
	*(cast(long*)(bytes[8..16])) = 
to!long(p4[10..$]~p4[8..10]~p4[6..8]~p4[4..6]~p4[2..4]~p4[0..2]~p3[2..$]~p3[0..2], 16);


return guid;
}

int main(string[] argv)
{


	// Issue QueryInterface seems to have the issue(not the same 
behavior as what is returned by the PS COM Interface)


auto x = new cApplication();
auto x1 = x.Path();
auto x2 = x.Name(); 
auto x3 = x.SystemInformation();
auto x4 = x.Version();
auto e = x.RecentFiles();
auto g = x.DisplayDialogs();
auto x5 = x.Documents();
auto x6 = x5.Count();   


auto dd = x.ForegroundColor();
auto __red = dd.RGB().Red();
auto __green = dd.RGB().Green();
auto __blue = dd.RGB().Blue();

GUID clsID = Guid!("4549DC9D-8A15-46F0-A0ED-7DB9C02FCB18");
GUID iID = Guid!("45F1195F-3554-4B3F-A00A-E1D189C0DC3E");
auto rgb1 = dd.RGB();
auto clsids = Guid2Str(rgb1.clsID);
auto rgb2 = new icRGBColor();
rgb2.iDispatch = rgb2;
//auto rgb = cast(icRGBColor)
//dd.RGB(cast(cRGBColor)(cast(void*)rgb));
//dd.RGB(cast(icRGBColor)(cast(void*)rgb));


cSolidColor.RGBSet = 
auto rgb = new icRGBColor();
//auto rgb = cast(icRGBColor)(cast(void*)dd.RGB());
dd.RGB(rgb, rgb1);
//dd.RGB2(rgb1);

return 0;
}


void RGB(icRGBColor ic, cRGBColor c, cSolidColor s)
{
GUID clsIDa = Guid!("AEADF007-9EE5-41D7-8CB1-AB5F353D1151");
GUID iIDa = Guid!("D2D1665E-C1B9-4CA0-8AC9-529F6A3D9002");
ic.iDispatch = s.iDispatch;
ic.rgb = c;

import main;
EXCEPINFO exception;
uint argErr = 0;
auto iidNULL = IID_NULL;
auto RT = new VARIANT();
VARIANT[1] paramVars;
	DISPPARAMS params = {rgvarg: paramVars.ptr, cArgs: 1, 
cNamedArgs: 0};

auto ID = s.COMMethodIDs[`RGB`];
	paramVars[0].punkVal = ic; paramVars[0].vt = 
VARENUM.VT_DISPATCH; scope(exit) VariantClear([0]);

DISPID mydispid = DISPID_PROPERTYPUT;
params.rgdispidNamedArgs = 
params.cNamedArgs = 1;
writeln("---");
	auto res = s.iDispatch.Invoke(cast(int)ID, , 0, 
DISPATCH_PROPERTYPUT, , cast(VARIANT*)RT, , 
);
	assert(res == S_OK, `Could not invoke COM Function 
cSolidColor.RGB. Error `~to!string(res, 16));


}






//version = wrap;


import std.conv, core.sys.windows.windows, core.sys.windows.com, 
core.sys.windows.wtypes, core.sys.windows.basetyps, 
core.sys.windows.unknwn, core.sys.windows.oaidl;


public class icRGBColor : IDispatch
{


	import std.conv, core.sys.windows.windows, core.sys.windows.com, 
core.sys.windows.wtypes, core.sys.windows.basetyps, 
core.sys.windows.unknwn, core.sys.windows.oaidl;

public IDispatch iDispatch = null;
int RefCount = 0;
cRGBColor rgb;

version(all)
{
void printFunc(string S = __FUNCTION__)()
{   
writeln(S); 
}

double Red()

Re: MAKELCID C++ translation

2017-05-05 Thread Mike B Johnson via Digitalmars-d-learn

On Friday, 5 May 2017 at 23:02:53 UTC, Adam D. Ruppe wrote:

On Friday, 5 May 2017 at 22:57:50 UTC, Mike B Johnson wrote:

D seems to be missing these macros and definitions


import core.sys.windows.winnt;

then they should work



thanks.


Re: MAKELCID C++ translation

2017-05-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 5 May 2017 at 22:57:50 UTC, Mike B Johnson wrote:

D seems to be missing these macros and definitions


import core.sys.windows.winnt;

then they should work


MAKELCID C++ translation

2017-05-05 Thread Mike B Johnson via Digitalmars-d-learn


How to translate something like?

#define LCID_ENGLISH MAKELCID(MAKELANGID(0x09, 0x01), 
SORT_DEFAULT)
#define LCID_GERMAN  MAKELCID(MAKELANGID(0x07, 0x01), 
SORT_DEFAULT)


D seems to be missing these macros and definitions


Re: Trying to understand DIP1000

2017-05-05 Thread Dukc via Digitalmars-d-learn

On Friday, 5 May 2017 at 18:04:32 UTC, Dukc wrote:

@safe void killDMan()
{   auto outer = [RefCountedSlice!int(1)];
foreach(ref fail; outer)
{   outer = [RefCountedSlice!int(1)];
gcActivationAttempt = new int[3];
fail[0] = 24;
}
}


should be

int[] gcActivationAttempt;

@safe void killDMan()
{   auto outer = [RefCountedSlice!int(1)];
foreach(ref fail; outer[0])
{   outer = [RefCountedSlice!int(1)];
gcActivationAttempt = new int[3];
fail = 24;
}
}

but this compiles too




Re: Trying to understand DIP1000

2017-05-05 Thread Dukc via Digitalmars-d-learn

On Friday, 5 May 2017 at 18:04:32 UTC, Dukc wrote:
The compiler is too cunning to let you to leak test[x] out of a 
function by reference, or take an address of it. Nor you can 
assing it to another variable, because that means copy 
semantics.


And oh, the identity function won't fool the compiler either, it 
infers return scope, -dip25 or no.





Trying to understand DIP1000

2017-05-05 Thread Dukc via Digitalmars-d-learn
Walters exciting yesterday talk made me think about dip1000 
again. I found out that I do not understand this:


Containers that own their data will be able to give access to 
elements by scope ref. The compiler ensures that the references 
returned never outlive the container. Therefore, the container 
can deallocate its payload (subject to control of multiple 
container copies, e.g. by means of reference counting). A basic 
outline of a reference counted slice is shown below:


@safe struct RefCountedSlice(T) {
private T[] payload;
private uint* count;

this(size_t initialSize) {
payload = new T[initialSize];
count = new size_t;
*count = 1;
}

this(this) {
if (count) ++*count;
}

// Prevent reassignment as references to payload may still exist
@system opAssign(RefCountedSlice rhs) {
this.__dtor();
payload = rhs.payload;
count = rhs.count;
++*count;
}

// Interesting fact #1: destructor can be @trusted
@trusted ~this() {
if (count && !--*count) {
delete payload;
delete count;
}
}

// Interesting fact #2: references to internals can be given 
away

scope ref T opIndex(size_t i) {
return payload[i];
}

// ...
}

// Prevent premature destruction as references to payload may 
still exist

// (defined in object.d)
@system void destroy(T)(ref RefCountedSlice!T rcs);


I did some hacking and found out that -dip1000 flag and scope are 
not even needed for most cases:


@safe struct RefCountedSlice(T) {
private T[] payload;
private uint* count;

this(size_t initialSize) {
payload = new T[initialSize];
count = new size_t;
*count = 1;
}

this(this) {
if (count) ++*count;
}

// Prevent reassignment as references to payload may still 
exist

@system opAssign(RefCountedSlice rhs) {
this.__dtor();
payload = rhs.payload;
count = rhs.count;
++*count;
}

@trusted ~this() {
if (count && !--*count) {
delete payload;
delete count;
import std.stdio;
}
}
ref opIndex(size_t i) {
return payload[i];
}

ref front(){return payload[0];}

void popFront(){payload = payload[1 .. $];}

auto empty(){return payload.length == 0;}
}

I tested that this does compile and work correctly:

@safe void main()
{   auto test = RefCountedSlice!string(16);

int i = 0;
foreach(ref e; test)
{   foreach(unused; 0 .. i++) e ~= "s";
}
foreach(j; 2 .. 10)
{   import std.stdio;
test[j].writeln;
}
}

The compiler is too cunning to let you to leak test[x] out of a 
function by reference, or take an address of it. Nor you can 
assing it to another variable, because that means copy semantics. 
And that all applies without using -dip1000 or even -dip25.


There's still one way I found to fool the compiler, at least 
without flags:


int[] gcActivationAttempt;

@safe void killDMan()
{   auto outer = [RefCountedSlice!int(1)];
foreach(ref fail; outer)
{   outer = [RefCountedSlice!int(1)];
gcActivationAttempt = new int[3];
fail[0] = 24;
}
}

Because that's the only hole I found that prevents generic @safe 
refcounted containers, I suppose the "scope ref" keyword somehow 
deals with that. But I cannot think of how.


Can somebody explain that?


Re: Looking for an equivalent to C++ std::getline in D

2017-05-05 Thread Stanislav Blinov via Digitalmars-d-learn

On Friday, 5 May 2017 at 09:54:03 UTC, k-five wrote:

Hi all.
I have a simple command-line program utility in C++ that can 
rename or remove files, based on regular expression.
After finding D that is more fun than C++ is, I want to port 
the code, but I have problem with this part of it:


std::getline( iss, match, delimiter );
std::getline( iss, substitute, delimiter );

I need to read from iss ( = std::istringstream iss( argv[ 1 ] ) 
and separate them by delimiter.


Since the program gets the input from a user, and it can be 
something like: 's/\d+[a-z]+@(?=\.)//g'  or '/[A-Za-z0-9]+//'


So for: s/\d+[a-z]+@(?=\.)//g
I need:
s
\d+[a-z]+@(?=\.)
g

and for: /[A-Za-z0-9]+/
It should be:
[A-Za-z0-9]+

---

I tired ( std.string: split or format ) or ( std.regex split ). 
In fact I need to read from a stream up to a delimiter.


Does someone knows a way to do this in D? Thanks


So, you need to consume input one element at a time (delimited), 
dropping empty elements? Try std.algorithm.iteration splitter and 
filter:


auto advance(Range)(ref Range r)
{
assert(!r.empty);
auto result = r.front;
r.popFront();
return result;
}

void main(string[] args)
{
import std.algorithm.iteration : splitter, filter;
import std.range : empty;

auto input = args[1].splitter('/').filter!"!a.empty"();

import std.stdio : writeln;
while (!input.empty)
writeln(input.advance());
}

The advance() function reads the next delimited element and pops 
it from the range.


Re: What is this error message telling me?

2017-05-05 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, April 11, 2017 8:41:21 AM CEST Jonathan M Davis via Digitalmars-
d-learn wrote:
> So, this should be reported as a bug in icmp.
>
> https://issues.dlang.org/

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

https://github.com/dlang/phobos/pull/5361

- Jonathan M Davis



How to declare "abstract" delegates list?

2017-05-05 Thread RedCAT via Digitalmars-d-learn

Hello!

Is it possible to create a list of slightly different delegates?

For example, there is a class hierarchy:

class Base;
class DerivedOne : Base;
class DerivedTwo : Base;

And there are several delegates:

void delegate(int, Base);
void delegate(int, DerivedOne);
void delegate(int, DerivedTwo);

It's easy to see that the only difference in the declarations of 
these delegates is the type of the second parameter, this is the 
class inherited from Base.


How can I create a list or an array where I can add these 
delegates?


Re: opCall with struct alias this

2017-05-05 Thread Patric Dexheimer via Digitalmars-d-learn

On Friday, 5 May 2017 at 13:19:03 UTC, Adam D. Ruppe wrote:

On Friday, 5 May 2017 at 13:05:35 UTC, Patric Dexheimer wrote:

[...]


You should never use static opCall. They are ambiguous with 
non-static opCall as well as with constructors. It is a buggy 
mess that should never have been in the language in the first 
place.


[...]



It make sense. Even liking of the possibilities of static opCall.
But I believe that somethings must exist to evade opCall usages 
now

like empty params ctor on structs.


Re: opCall with struct alias this

2017-05-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 5 May 2017 at 13:05:35 UTC, Patric Dexheimer wrote:

static T opCall(Args...)( Args args ){


You should never use static opCall. They are ambiguous with 
non-static opCall as well as with constructors. It is a buggy 
mess that should never have been in the language in the first 
place.



// Expected: opCall of Base!Data, but get error
// auto x = Data(10);
// opCall called. What just happened here?


It is ambiguous with a constructor call on Data. It is trying to 
construct a Data, but there's not applicable constructor so it is 
an error. Note that alias this NEVER applies to constructors (and 
since static opCall is ambiguous with constructors, that doesn't 
work either), but only works on existing objects.



// never saw anything about init as function.
auto y = Data.init(10);


T.init is an existing value used to initialize any type, so it is 
no longer a constructor and can call operator overloads


But, while this should be a non-static opCall, static opCall will 
hijack that too.



Your short code hit the two big reasons why static opCall ought 
to be deprecated and totally removed: it gets confused for ctors 
and non-static opCalls, breaking in situations where you think it 
would be them, and cannot coexist with them either.


I suggest you find some other way entirely to write the code.


opCall with struct alias this

2017-05-05 Thread Patric Dexheimer via Digitalmars-d-learn

struct Base(T){
static T opCall(Args...)( Args args ){
writeln(args);
writeln("opCall");
T t;
return t;
}
}

struct Data{
Base!Data b;
alias b this;
}

void main()
{   
// Expected: opCall of Base!Data, but get error
// auto x = Data(10);
// opCall called. What just happened here?
// never saw anything about init as function.
auto y = Data.init(10);
}


Re: What's a good wat to trunctate a time point

2017-05-05 Thread Dukc via Digitalmars-d-learn

On Friday, 5 May 2017 at 09:14:21 UTC, Biotronic wrote:


Here's an implementation that supports start of year, month, 
week, day, hour, minute and second. Works for DateTime and 
SysTime. Not heavily tested (all tests included):


[lots of code]


Wow! You might want to create a pr to phobos from that -It is so 
general, and despite what you say I have seen much worse tested 
stuff there.


Thanks for the reply.


Looking for an equivalent to C++ std::getline in D

2017-05-05 Thread k-five via Digitalmars-d-learn

Hi all.
I have a simple command-line program utility in C++ that can 
rename or remove files, based on regular expression.
After finding D that is more fun than C++ is, I want to port the 
code, but I have problem with this part of it:


std::getline( iss, match, delimiter );
std::getline( iss, substitute, delimiter );

I need to read from iss ( = std::istringstream iss( argv[ 1 ] ) 
and separate them by delimiter.


Since the program gets the input from a user, and it can be 
something like: 's/\d+[a-z]+@(?=\.)//g'  or '/[A-Za-z0-9]+//'


So for: s/\d+[a-z]+@(?=\.)//g
I need:
s
\d+[a-z]+@(?=\.)
g

and for: /[A-Za-z0-9]+/
It should be:
[A-Za-z0-9]+

---

I tired ( std.string: split or format ) or ( std.regex split ). 
In fact I need to read from a stream up to a delimiter.


Does someone knows a way to do this in D? Thanks


Re: What's a good wat to trunctate a time point

2017-05-05 Thread Biotronic via Digitalmars-d-learn

On Friday, 5 May 2017 at 08:02:15 UTC, Dukc wrote:
I have a time point, be it SysTime or DateTime, whatever. I 
want to trunctate it to weeks, eg. I want it to become the 
first point of time during the week it was representing. What's 
a good way to do that? Only hacks came to my mind.


The solution does not have to be generic, trough I'd prefer it 
to be if it can be without much extra work.


Here's an implementation that supports start of year, month, 
week, day, hour, minute and second. Works for DateTime and 
SysTime. Not heavily tested (all tests included):


import std.datetime;

DateTime startOf(string unit, DayOfWeek start = 
DayOfWeek.sun)(DateTime dt) {

static if (unit == "year") {
return DateTime(dt.year, 1, 1);
} else static if (unit == "month") {
return DateTime(dt.year, dt.month, 1);
} else static if (unit == "week") {
auto delta = dt.dayOfWeek - start;
if (delta < 0) delta += 7;
return DateTime(dt.year, dt.month, dt.day) - 
dur!"days"(delta);

} else static if (unit == "day") {
return DateTime(dt.year, dt.month, dt.day);
} else static if (unit == "hour") {
return DateTime(dt.year, dt.month, dt.day, dt.hour);
} else static if (unit == "minute") {
return DateTime(dt.year, dt.month, dt.day, dt.hour, 
dt.minute);

} else static if (unit == "second") {
return DateTime(dt.year, dt.month, dt.day, dt.hour, 
dt.minute, dt.second);

}
}

SysTime startOf(string unit)(SysTime st) {
return SysTime(startOf!unit(cast(DateTime)st), st.timezone);
}

unittest {
auto now= DateTime(2017, 5,  5, 10, 39, 17);

auto expectedYear   = DateTime(2017, 1,  1,  0,  0,  0);
auto expectedMonth  = DateTime(2017, 5,  1,  0,  0,  0);
auto expectedWeek   = DateTime(2017, 4, 30,  0,  0,  0);
auto expectedDay= DateTime(2017, 5,  5,  0,  0,  0);
auto expectedHour   = DateTime(2017, 5,  5, 10,  0,  0);
auto expectedMinute = DateTime(2017, 5,  5, 10, 39,  0);
auto expectedSecond = DateTime(2017, 5,  5, 10, 39, 17);

auto startOfYear   = now.startOf!"year";
auto startOfMonth  = now.startOf!"month";
auto startOfWeek   = now.startOf!"week";
auto startOfDay= now.startOf!"day";
auto startOfHour   = now.startOf!"hour";
auto startOfMinute = now.startOf!"minute";
auto startOfSecond = now.startOf!"second";

assert(expectedYear   == startOfYear);
assert(expectedMonth  == startOfMonth);
assert(expectedWeek   == startOfWeek);
assert(expectedDay== startOfDay);
assert(expectedHour   == startOfHour);
assert(expectedMinute == startOfMinute);
assert(expectedSecond == startOfSecond);

now = DateTime(2017, 4, 30, 10, 39, 17);
auto expectedWeek2  = DateTime(2017, 4, 24,  0,  0,  0);
auto startOfWeek2   = now.startOf!("week", DayOfWeek.mon);
auto expectedWeek3  = DateTime(2017, 4, 29,  0,  0,  0);
auto startOfWeek3   = now.startOf!("week", DayOfWeek.sat);

assert(startOfWeek2 == expectedWeek2);
assert(startOfWeek3 == expectedWeek3);
}


What's a good wat to trunctate a time point

2017-05-05 Thread Dukc via Digitalmars-d-learn
I have a time point, be it SysTime or DateTime, whatever. I want 
to trunctate it to weeks, eg. I want it to become the first point 
of time during the week it was representing. What's a good way to 
do that? Only hacks came to my mind.


The solution does not have to be generic, trough I'd prefer it to 
be if it can be without much extra work.