Re: Class info on interfaces

2015-08-28 Thread Jacob Carlborg via Digitalmars-d-learn

On 2015-08-26 20:59, Adam D. Ruppe wrote:


Yes, exactly. COM and C++ things won't necessarily have a D TypeInfo
available and since interfaces can be them, it can't be sure.

What I do there is to just cast the interface to Object. Then you should
check for null to cover those cases, then you can typeid or classinfo it.


Is it possible to detect at compile time if an interface is not a native 
D interface?


Now when I think about it, we actually have four (!) different kinds of 
interfaces. Native D, C++, Objective-C and COM.


--
/Jacob Carlborg


How to get the current Timezone

2015-08-28 Thread wobbles via Digitalmars-d-learn

From the docs in std.datetime, I figured I could write:

Clock.currTime.timezone().name()

to get the timezone this system is in. However, it just returns 
an empty string.

Anyone know how to get the timezone of the machine easily?

Thanks!


Re: Class info on interfaces

2015-08-28 Thread rumbu via Digitalmars-d-learn

On Friday, 28 August 2015 at 06:19:55 UTC, Jacob Carlborg wrote:

On 2015-08-26 20:59, Adam D. Ruppe wrote:

Yes, exactly. COM and C++ things won't necessarily have a D 
TypeInfo

available and since interfaces can be them, it can't be sure.

What I do there is to just cast the interface to Object. Then 
you should
check for null to cover those cases, then you can typeid or 
classinfo it.


Is it possible to detect at compile time if an interface is not 
a native D interface?


Now when I think about it, we actually have four (!) different 
kinds of interfaces. Native D, C++, Objective-C and COM.


I don't know about Objective-C, but:

- for native D interfaces __traits(getVirtualIndex, 
NativeInterface.firstFunction) == 1 since the first entry in vtbl 
is the contained object
- for C++ interfaces __traits(getVirtualIndex, 
CPPInterface.firstFunction) == 0
- COM interfaces: __traits(getVirtualIndex, 
CPPInterface.firstFunction) == 0 and inherit IUnknown


At runtime, it's simple, you have the m_flags member of 
TypeInfo_Class (isCPPclass, isCOMclass)




Re: How to get the current Timezone

2015-08-28 Thread Kagamin via Digitalmars-d-learn

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation ?


Re: (De)Serializing interfaces

2015-08-28 Thread Jacob Carlborg via Digitalmars-d-learn

On 2015-08-22 21:14, nims wrote:

I think interfaces are very powerful and I heavily use them. The only
problem I have with them is that serializing/deserializing them to XML
or JSON doesn't seem to work. So far I got to try Orange and
painlessjson. Using Orange all I got was a lot of compiler errors.


I've added support for interfaces to Orange now.

--
/Jacob Carlborg


Re: How to get the current Timezone

2015-08-28 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 28 August 2015 at 14:07:37 UTC, wobbles wrote:

However, it just returns an empty string.


from the doc:

Note that this always returns the empty string. This is because 
time zones cannot be uniquely identified by the attributes given 
by the OS (such as the stdName and dstName), and neither Posix 
systems nor Windows systems provide an easy way to get the TZ 
Database name of the local time zone.



Anyone know how to get the timezone of the machine easily?


Best you can do is try the stdName or dstName properties of 
Timezone, perhaps checking if dstInEffect to choose which one to 
print. Or ou could use the utcOffsetAt function to print a number 
(like UTC-4:00, after formatting it)


The actual location the user sets in the timezone settings of the 
computer I think the docs are right about - it isn't available on 
either OS, even using platform specific APIs... though I'm sure 
it is stored somewhere and maybe you could e.g. dig into the 
Windows registry to find it.


Re: Class info on interfaces

2015-08-28 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 28 August 2015 at 06:19:55 UTC, Jacob Carlborg wrote:
Is it possible to detect at compile time if an interface is not 
a native D interface?


Not fully, no, but you might be able to reflect into the methods 
and see what kind of linkage they have.


http://dlang.org/phobos/std_traits.html#functionLinkage

The interface itself won't necessarily be marked extern - on the 
binary level, they are all the same (I think... just a pointer to 
a list of function pointers), but if you look at the methods you 
can make a reasonably good guess.


However, you can't do anything with an interface that isn't in 
there anyway without a runtime cast, so you might want to just 
skip any compile time guesses and just go with the runtime check.


Now when I think about it, we actually have four (!) different 
kinds of interfaces. Native D, C++, Objective-C and COM.


aye, and since the interface is so simple at the binary level, it 
is possible to use them for other things too (I think a glib 
object in C is also binary compatible...); I'm sure this won't be 
the end of the list.


Re: How to get the current Timezone

2015-08-28 Thread rumbu via Digitalmars-d-learn

On Friday, 28 August 2015 at 23:03:16 UTC, Jonathan M Davis wrote:



I _really_ wish that Microsoft would just use the TZ database 
like everyone else...


- Jonathan M Davis


Starting with Windows 8.1, it does, but only in Windows Runtime 
(so called modern/store apps).


Pointer to std.container.Array data for C library

2015-08-28 Thread Oleg via Digitalmars-d-learn

Hello!
Is it possible to get pointer to a data in std.container.Array 
like .ptr from an array? I need to pass a pointer to some C 
function (from DerelictGL3 binding) and avoid GC allocation.

Thank you!


Re: Pointer to std.container.Array data for C library

2015-08-28 Thread Oleg via Digitalmars-d-learn

On Friday, 28 August 2015 at 18:21:04 UTC, John Colvin wrote:

On Friday, 28 August 2015 at 17:45:21 UTC, Oleg wrote:

Hello!
Is it possible to get pointer to a data in std.container.Array 
like .ptr from an array? I need to pass a pointer to some C 
function (from DerelictGL3 binding) and avoid GC allocation.

Thank you!


I'm pretty sure you can just take the address of the first 
element, e.g. a[0] or a.front


I've tried, it throws
core.exception.RangeError@/usr/include/dmd/phobos/std/container/array.d(571): 
Range violation

If I make an array from the Array container with foreach ad pass 
it's .ptr - it works


Re: Pointer to std.container.Array data for C library

2015-08-28 Thread Oleg via Digitalmars-d-learn

On Friday, 28 August 2015 at 18:46:23 UTC, Oleg wrote:

I found solution. I call length instead of reserve. It calls 
ensureInitialized and everything works fine. By default, Array 
won't initialize store.


Oh, reserve calls it too. My mistake.
I found the problem, that's because I passed an empty Array (like 
Array!int.init).






Re: Pointer to std.container.Array data for C library

2015-08-28 Thread Oleg via Digitalmars-d-learn

On Friday, 28 August 2015 at 18:40:33 UTC, John Colvin wrote:

On Friday, 28 August 2015 at 18:31:00 UTC, Oleg wrote:

On Friday, 28 August 2015 at 18:21:04 UTC, John Colvin wrote:

On Friday, 28 August 2015 at 17:45:21 UTC, Oleg wrote:

Hello!
Is it possible to get pointer to a data in 
std.container.Array like .ptr from an array? I need to pass 
a pointer to some C function (from DerelictGL3 binding) and 
avoid GC allocation.

Thank you!


I'm pretty sure you can just take the address of the first 
element, e.g. a[0] or a.front


I've tried, it throws
core.exception.RangeError@/usr/include/dmd/phobos/std/container/array.d(571): 
Range violation


That's unexpected. Could you provide a full example that does 
this?


I found solution. I call length instead of reserve. It calls 
ensureInitialized and everything works fine. By default, Array 
won't initialize store.


Re: How to get the current Timezone

2015-08-28 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 28 August 2015 at 17:59:06 UTC, WhatMeWorry wrote:
Stupid question. If it always returns an empty string, why is 
it even there?


It can return meaningful information in other subclasses; it is a 
method from the interface and is just blank in the LocalTime 
class.


If you construct one of the other subclasses, you may provide a 
name which can be retrieved through it, or perhaps some future 
class might implement it too.


Re: Pointer to std.container.Array data for C library

2015-08-28 Thread John Colvin via Digitalmars-d-learn

On Friday, 28 August 2015 at 18:31:00 UTC, Oleg wrote:

On Friday, 28 August 2015 at 18:21:04 UTC, John Colvin wrote:

On Friday, 28 August 2015 at 17:45:21 UTC, Oleg wrote:

Hello!
Is it possible to get pointer to a data in 
std.container.Array like .ptr from an array? I need to pass a 
pointer to some C function (from DerelictGL3 binding) and 
avoid GC allocation.

Thank you!


I'm pretty sure you can just take the address of the first 
element, e.g. a[0] or a.front


I've tried, it throws
core.exception.RangeError@/usr/include/dmd/phobos/std/container/array.d(571): 
Range violation


That's unexpected. Could you provide a full example that does 
this?


Re: How to get the current Timezone

2015-08-28 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, August 28, 2015 18:04:16 Adam D. Ruppe via Digitalmars-d-learn wrote:
 On Friday, 28 August 2015 at 17:59:06 UTC, WhatMeWorry wrote:
  Stupid question. If it always returns an empty string, why is
  it even there?

 It can return meaningful information in other subclasses; it is a
 method from the interface and is just blank in the LocalTime
 class.

 If you construct one of the other subclasses, you may provide a
 name which can be retrieved through it, or perhaps some future
 class might implement it too.

A prime example would be PosixTimeZone. It knows exactly what its name is.
Ultimately though, the name field probably isn't very useful - particularly
since I really need to deprecate the functions for converting between the TZ
database names and Microsft's names, because it's system dependent, and they
keep changing it, so there's no guarantee that the mapping is correct or
even that the result exists on a given box (since that box could be
out-of-date, or the D program may not have been updated). That being the
case, the name property really just becomes applicable to PosixTimeZone.

I _really_ wish that Microsoft would just use the TZ database like everyone
else...

- Jonathan M Davis



Re: Class info on interfaces

2015-08-28 Thread rumbu via Digitalmars-d-learn

On Friday, 28 August 2015 at 19:36:37 UTC, Jacob Carlborg wrote:

On 2015-08-28 17:41, rumbu wrote:


I don't know about Objective-C, but:

- for native D interfaces __traits(getVirtualIndex,
NativeInterface.firstFunction) == 1 since the first entry in 
vtbl is the

contained object
- for C++ interfaces __traits(getVirtualIndex,
CPPInterface.firstFunction) == 0
- COM interfaces: __traits(getVirtualIndex, 
CPPInterface.firstFunction)

== 0 and inherit IUnknown


I'm wondering how reliable that is. Might be better to check 
the linkage of a method in the interface as Adam suggested.


The linkage check it's good as long you don't have an abomination 
like this:


extern(C++) interface CPPInterface
{
extern(D) void foo();
}

Anyway, the problem is the availability of such function. If the 
interface doesn't contain any function, there is no way to detect 
the type, except for COM Interfaces which are clearly 
implementing IUnknown.


But deriving a new interface and defining a sentinel function, it 
works:


import std.stdio;
import std.traits;
import core.sys.windows.com;

interface NativeInterface {}

interface COMInterface : IUnknown {}

extern(C++) interface CPPInterface {}

enum InterfaceKind { native, windows, cpp }

template interfaceKind(I) if (is(I == interface))
{
interface J : I { void __foo__(); }
static if (functionLinkage!(J.__foo__) == D)
enum interfaceKind = InterfaceKind.native;
else static if (functionLinkage!(J.__foo__) == Windows)
enum interfaceKind = InterfaceKind.windows;
else static if (functionLinkage!(J.__foo__) == C++)
enum interfaceKind = InterfaceKind.cpp;
else static assert(false, Unknown interface kind.);
}

int main(string[] argv)
{
   static assert(interfaceKind!NativeInterface == 
InterfaceKind.native);
   static assert(interfaceKind!COMInterface == 
InterfaceKind.windows);

   static assert(interfaceKind!CPPInterface == InterfaceKind.cpp);
   return 0;
}







Re: How to get the current Timezone

2015-08-28 Thread WhatMeWorry via Digitalmars-d-learn

On Friday, 28 August 2015 at 14:18:24 UTC, Adam D. Ruppe wrote:

On Friday, 28 August 2015 at 14:07:37 UTC, wobbles wrote:

However, it just returns an empty string.


from the doc:

Note that this always returns the empty string. This is because 
time zones cannot be uniquely identified by the attributes 
given by the OS (such as the stdName and dstName), and neither 
Posix systems nor Windows systems provide an easy way to get 
the TZ Database name of the local time zone.


Stupid question. If it always returns an empty string, why is it 
even there?


Re: Pointer to std.container.Array data for C library

2015-08-28 Thread John Colvin via Digitalmars-d-learn

On Friday, 28 August 2015 at 17:45:21 UTC, Oleg wrote:

Hello!
Is it possible to get pointer to a data in std.container.Array 
like .ptr from an array? I need to pass a pointer to some C 
function (from DerelictGL3 binding) and avoid GC allocation.

Thank you!


I'm pretty sure you can just take the address of the first 
element, e.g. a[0] or a.front


Re: Class info on interfaces

2015-08-28 Thread Jacob Carlborg via Digitalmars-d-learn

On 2015-08-28 16:31, Adam D. Ruppe wrote:


Not fully, no, but you might be able to reflect into the methods and see
what kind of linkage they have.

http://dlang.org/phobos/std_traits.html#functionLinkage


That might work.


However, you can't do anything with an interface that isn't in there
anyway without a runtime cast, so you might want to just skip any
compile time guesses and just go with the runtime check.


Well, this would be for my serialization library. If the static type 
it's not a native D interface I don't have way to recreate the instance 
when deserializing, at least not with the default deserialization process.


--
/Jacob Carlborg


Re: Class info on interfaces

2015-08-28 Thread Jacob Carlborg via Digitalmars-d-learn

On 2015-08-28 17:41, rumbu wrote:


I don't know about Objective-C, but:

- for native D interfaces __traits(getVirtualIndex,
NativeInterface.firstFunction) == 1 since the first entry in vtbl is the
contained object
- for C++ interfaces __traits(getVirtualIndex,
CPPInterface.firstFunction) == 0
- COM interfaces: __traits(getVirtualIndex, CPPInterface.firstFunction)
== 0 and inherit IUnknown


I'm wondering how reliable that is. Might be better to check the linkage 
of a method in the interface as Adam suggested.


--
/Jacob Carlborg