static init c struct with array filed

2022-03-16 Thread test via Digitalmars-d-learn

```c
struct Test {
int32_t a;
}
struct Test2 {
int32_t a;
Test arr[];
}
```

I need static const init Test2, then pass it to c library 
late(third library, can not change the type def).



I am not sure how to do it in D.


Re: question about bitfields to decode websocket header

2018-11-07 Thread test via Digitalmars-d-learn
On Wednesday, 7 November 2018 at 14:22:43 UTC, lithium iodate 
wrote:

On Wednesday, 7 November 2018 at 13:05:49 UTC, test wrote:

I am confused about the bitfields order.


The bitfields start with the least significant bits:
fin -> 1
rsv1 -> 0
rsv2 -> 0
rsv3 -> 0
opcode -> 1000 = 8

mask -> 1
_size -> 101 = 65

This order will likely be what you want:
mixin(bitfields!(
opcode, "opcode", 4,
bool,   "rsv3",   1,
bool,   "rsv2",   1,
bool,   "rsv1",   1,
bool,   "fin",1,

ubyte,  "_size",  7,
bool,   "mask",   1,
));

Also beware of endianness when mapping bytes to it.


After I use your code it working now.

my other question is: if the opcode bit cross byte, how do we 
define the bitfields ?


for example if the opcode is a 6 bit number instead 4bit :  
F1|R1|R1|R1|opcode6|Mask1|Size5


I has to split the opcode here ?

mixin(bitfields!(
 opcode, "opcode4", 4,
 bool,   "rsv3",   1,
 bool,   "rsv2",   1,
 bool,   "rsv1",   1,
 bool,   "fin",1,

 ubyte,  "_size",  5,
 bool,   "mask",   1,
 bool,   "opcode2",   1,
 ));



question about bitfields to decode websocket header

2018-11-07 Thread test via Digitalmars-d-learn

I am confused about the bitfields order.

mixin(bitfields!(
bool,   "fin",1,
bool,   "rsv1",   1,
bool,   "rsv2",   1,
bool,   "rsv3",   1,
Opcode, "opcode", 4,

bool,   "mask",   1,
ubyte,  "_size",  7,
));


output for first byte is 1001 ,  the 2st byte 1011

my code output:  opcode=8 mask=true size=65

the real size is 3 byte, and opcode is 1;

how to fix this ?



Re: need help about get all public static function name

2018-10-22 Thread test via Digitalmars-d-learn

On Monday, 22 October 2018 at 12:32:57 UTC, test wrote:
On Monday, 22 October 2018 at 12:16:50 UTC, Stanislav Blinov 
wrote:

On Monday, 22 October 2018 at 12:03:22 UTC, test wrote:
You're trying to call a static function 'getThis' on Fiber.
The type 'Fiber' is really a Proxy!FiberS. Proxy doesn't have 
a static getThis() function. So the compiler tries an 'alias 
this', which forwards to a non-static member function 
getPayload(). To call that function, you need an instance of 
'Proxy', which you don't have.
I guess the first error message ("this for getPayload needs to 
be type Proxy not type TcpStream") just doesn't report that 
clearly.



But this work:


extern(C) void main(){
   auto id = Fiber.getThis(); // work here
}


here also dont have Fiber instance.



I think this is a bug.

If I use "alias this " with a FiberS instance tuple memebr, it 
will work.  but if I use alias this with @propery , it only work 
on static method, on delegate method it will use delegate this.


This is kind like Javascript method this is come from caller 
place. I think it is a bug.  please help me confirm this.  If so 
please made a bug report for me.


Re: need help about get all public static function name

2018-10-22 Thread test via Digitalmars-d-learn
On Monday, 22 October 2018 at 12:16:50 UTC, Stanislav Blinov 
wrote:

On Monday, 22 October 2018 at 12:03:22 UTC, test wrote:
You're trying to call a static function 'getThis' on Fiber.
The type 'Fiber' is really a Proxy!FiberS. Proxy doesn't have a 
static getThis() function. So the compiler tries an 'alias 
this', which forwards to a non-static member function 
getPayload(). To call that function, you need an instance of 
'Proxy', which you don't have.
I guess the first error message ("this for getPayload needs to 
be type Proxy not type TcpStream") just doesn't report that 
clearly.



But this work:


extern(C) void main(){
   auto id = Fiber.getThis(); // work here
}


here also dont have Fiber instance.


Re: need help about get all public static function name

2018-10-22 Thread test via Digitalmars-d-learn

On Monday, 22 October 2018 at 11:59:21 UTC, test wrote:

On Monday, 22 October 2018 at 11:42:59 UTC, test wrote:
I try made a simple example but it has no error:



I find the way to show the error:

https://run.dlang.io/is/f8cULz


import std.traits;

struct FiberS {
static auto getThis(){
return Fiber.getId();
}
}

struct Proxy(T){
T* ptr;
alias getPayload this;

@property ref auto getPayload() inout return {
return * ptr ;
}

static auto getId(){
return 1;
}
}
alias Fiber = Proxy!(FiberS);


extern(C) void main(){
   auto id = Fiber.getThis(); // work here
}

struct TcpStream {
void read(ubyte[] data){
   auto id = Fiber.getThis(); // not work here in my case
}
}


Re: need help about get all public static function name

2018-10-22 Thread test via Digitalmars-d-learn

On Monday, 22 October 2018 at 11:42:59 UTC, test wrote:
On Monday, 22 October 2018 at 11:18:20 UTC, Stanislav Blinov 
wrote:


Error: template instance `TypeTemplate!(BaseType)` error 
instantiating



I use alias this to allow call static method on proxyType.

It work some time, but on other case it report:



The code throw error is:

struct TcpStream {

   void read(ubyte[] data){
   auto id2 = Fiber.getThis();  // Error: this for 
getBoxPayload needs to be type BoxedTypeNode not type TcpStream

  }
}

If the read is static method, then there is no error.

I can not fix this so I try use static method alias, but it work 
on normal case not my code (100KB+).



I try made a simple example but it has no error:








Re: need help about get all public static function name

2018-10-22 Thread test via Digitalmars-d-learn
On Monday, 22 October 2018 at 11:18:20 UTC, Stanislav Blinov 
wrote:

On Monday, 22 October 2018 at 10:49:10 UTC, test wrote:


note the added check that the member is not a type: 
is(typeof(__traits(getMember, BaseType, name))).
You'll still need to iterate overloads if you want to get all 
static functions.



Thanks for this.

some how when I call "is(typeof(__traits(getMember, BaseType, 
name))) " in the template, my code report others error like:


Error: no property fromPointer for type void
Error: template instance TypeTemplate!(BaseType) is used as a type

Error: template instance `TypeTemplate!(BaseType)` error 
instantiating





Re: need help about get all public static function name

2018-10-22 Thread test via Digitalmars-d-learn

On Monday, 22 October 2018 at 10:49:10 UTC, test wrote:

On Monday, 22 October 2018 at 10:45:07 UTC, test wrote:
On Monday, 22 October 2018 at 10:29:56 UTC, Stanislav Blinov 
wrote:

extern(C) void main(){
GetPub!XX;
}


https://run.dlang.io/is/f295qE


Re: need help about get all public static function name

2018-10-22 Thread test via Digitalmars-d-learn

On Monday, 22 October 2018 at 10:45:07 UTC, test wrote:
On Monday, 22 October 2018 at 10:29:56 UTC, Stanislav Blinov 
wrote:

On Monday, 22 October 2018 at 10:16:22 UTC, test wrote:

and


On Monday, 22 October 2018 at 10:45:07 UTC, test wrote:

test1.d
=
alias Fn1   = void function();
struct XX {
alias Fn= Fn1;
enum Y {
aa
}
static void Test(){}
int id;
}


test2.d
=
import test1;
void GetPub(BaseType)(){
static foreach (name; __traits(allMembers, BaseType)) 
static if( name[0] !is '_' && __traits(getProtection, 
__traits(getMember, BaseType, name)) == "public" ) {

static assert( hasMember!(BaseType.init, name));
static if( __traits(isStaticFunction, 
__traits(getMember, BaseType, name)) ) {
pragma(msg, 
typeof(BaseType.init).stringof ~ "." ~ name);

}
}
}


extern(C) void main(){
GetPub!XX;
}




Re: need help about get all public static function name

2018-10-22 Thread test via Digitalmars-d-learn
On Monday, 22 October 2018 at 10:29:56 UTC, Stanislav Blinov 
wrote:

On Monday, 22 October 2018 at 10:16:22 UTC, test wrote:
I try use traits get all public static function of a struct 
pass to template...



how to do this ?


void allFunctions(T)() {
import std.stdio;
foreach (name; __traits(allMembers, T)) {
static foreach (overload; __traits(getOverloads, T, 
name)) {
static if (__traits(getProtection, overload) == 
"public" && __traits(isStaticFunction, overload)) {
// 'overload' is an alias to a T's public 
static function

writeln(name, ": ", typeof(overload).stringof);
}
}
}
}


On the line: static foreach (overload; __traits(getOverloads, 
Type, name)) , throw error: is not accessible from module



and


need help about get all public static function name

2018-10-22 Thread test via Digitalmars-d-learn
I try use traits get all public static function of a struct pass 
to template.


Compiler report  " .Function  is not accessible from module " 
with __traits(isStaticFunction, __traits(getMember, Type , name)).


If I add " __traits(getProtection, __traits(getMember, Type, 
name)) == "public" before "__traits(isStaticFunction, 
__traits(getMember, Type , name))", it report " argument void 
function(void*) nothrow @nogc has no protection".




If I add "isFunction!(typeof(__traits(getMember, Type, name)))" 
before all this, It report type State is not an expression.


If I use "isFunction!(__traits(getMember, BaseType, name)) ", it 
report again "argument void function(void*) nothrow @nogc has no 
protection"



how to do this ?


Re: betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC

2018-10-17 Thread test via Digitalmars-d-learn

On Wednesday, 17 October 2018 at 16:50:36 UTC, Paul Backus wrote:

On Wednesday, 17 October 2018 at 07:01:21 UTC, test wrote:




simple example:   you can not use functionAttributes from betterC


betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC

2018-10-17 Thread test via Digitalmars-d-learn



test1:

module test1;
import test2;
enum X = getR(1,3);
void main(string[] args){}

test2:

module test2;
struct R {
int i;
}
R[] getR(int a, int b){
R[] r;
r   ~= R(a);
r   ~= R(b);
return r;
}


to build without betterC:  ldmd2 ./test.d -I.
to build with betterC: ldmd2 ./test.d -I.  -betterC


If build without betterC, all work fine.  if with betterC:

test2.d(3): Error: TypeInfo cannot be used with -betterC





Re: betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC

2018-10-17 Thread test via Digitalmars-d-learn

On Wednesday, 17 October 2018 at 07:01:21 UTC, test wrote:

test2.d(3): Error: TypeInfo cannot be used with -betterC



the first problem is the error message is not clear and can be 
improved.



And my question is how to workaround this to make it work with 
betterC.


how to get UDA only for methods

2018-10-16 Thread test via Digitalmars-d-learn
I need get the Route UDA only for method without (static methods, 
property, constructor, destructor), dont know how to do it.



Route[] getRoutes(T)(){
Route[] routes;
foreach(int i, m; __traits(allMembers, T) ){
pragma(msg, m.stringof);
static foreach(int j, r; getUDAs!(m, Route) ) {
routes  ~= r;
}
}
return routes;
}

struct App {

@Route("/index")
void home(){

}

@Route("/blog")
void blog(){

}
}