Re: list system drives
On Tuesday, 2 February 2021 at 06:31:27 UTC, Виталий Фадеев wrote: Give, please, Dlang tools for list system drives. Some like a: enumVolumes(); // [ 'C:\', 'D:\' ] I have found this code by a google search. I don't know who the author was. I had to touch it a little since the codebase was old. https://gist.github.com/aferust/5cc3209a6b6caf1062271a082c093b87
Need help for opencvd git submoduling
I am wrapping opencv::cuda modules based on c/c++ wrapper files of gocv. This might be more of a git question. Can one link a folder from a repo to be a subfolder of another git repo? Do I have any option other than submoduling the entire Go repo? I want "https://github.com/hybridgroup/gocv/tree/release/cuda"; to be "https://github.com/aferust/opencvd/tree/master/c/cuda";
Re: Real simple unresolved external symbols question...
On Tuesday, 9 February 2021 at 19:37:17 UTC, WhatMeWorry wrote: I'm trying to create a super simple dynamic library consisting of two files: [...] remove /NOENTRY, and include "mixin SimpleDllMain;" in one of the sources. And link with druntime. link /DLL file2.obj fileB.obj druntime-ldc.lib msvcrt.lib
Re: Web crawler/scraping
On Wednesday, 17 February 2021 at 12:12:56 UTC, Carlos Cabral wrote: Hi, I'm trying to collect some json data from a website/admin panel automatically, which is behind a login form. Is there a D library that can help me with this? Thank you I found this but it looks outdated: https://github.com/gedaiu/selenium.d
Re: Creating 1000 instances
On Friday, 19 February 2021 at 08:04:19 UTC, Виталий Фадеев wrote: We have: class File { // WIN32_FIND_DATAW data; } void fastReadDir() { File[] files; // reserve space, allocating instances files = new File[]( 1000 ); // <--- trouble here ? // filling instances auto file = files.ptr; writeln( file.data );// <--- or trouble here ? // ... } Got: SegFault Goal: Allocate memory for 1000 instances at once. Source: https://run.dlang.io/is/xfaXcv Question: What is the true, fastest, beauty way to create 1000 instances of the class File ? files = new File[]( 1000 ); files[] = new File(); // add this Since classes are reference types all instances of files will be the same reference of "new File()", which you probably don't want.
Re: Creating 1000 instances
On Friday, 19 February 2021 at 08:41:06 UTC, Ferhat Kurtulmuş wrote: On Friday, 19 February 2021 at 08:29:36 UTC, Ferhat Kurtulmuş wrote: On Friday, 19 February 2021 at 08:04:19 UTC, Виталий Фадеев wrote: [...] files = new File[]( 1000 ); files[] = new File(); // add this Since classes are reference types all instances of files will be the same reference of "new File()", which you probably don't want. You can do files[].each!((ref a) => a = new File); oh, now we can remove brackets files.each!((ref a) => a = new File);
Re: Creating 1000 instances
On Friday, 19 February 2021 at 08:29:36 UTC, Ferhat Kurtulmuş wrote: On Friday, 19 February 2021 at 08:04:19 UTC, Виталий Фадеев wrote: [...] files = new File[]( 1000 ); files[] = new File(); // add this Since classes are reference types all instances of files will be the same reference of "new File()", which you probably don't want. You can do files[].each!((ref a) => a = new File);
Re: Creating 1000 instances
On Friday, 19 February 2021 at 10:02:05 UTC, Siemargl wrote: On Friday, 19 February 2021 at 08:29:36 UTC, Ferhat Kurtulmuş wrote: Since classes are reference types all instances of files will be the same reference of "new File()", which you probably don't want. Is any differences between x and y definitions? MyClass [] x, y; x = new MyClass[7]; y= new MyClass[](8); Although I don't usually use the latter, I can say online d editor yields the same ASM output for both: File[] files = new File[10]; File[] files = new File[](10);
Re: Creating a .di file for a custom C library
On Tuesday, 30 March 2021 at 04:01:12 UTC, Brad wrote: I would like to use an updated version of the Termbox library (written in C) with D. I have the .h file. This is new territory for me (why try something easy - right?). I think I need to create a .di file that corresponds to the .h file. I also suspect that I need to put the library file (C code) into the project as a file somehow. I am probably just not looking in the right place for the documentation, but I cannot seem to find a lot of guidance in this area. Thanks in advance. I never needed or used .di files. Dstep[1] can create d modules containing c header definitions. Recently, I used to create a d binding [2] for shapelib[3]. I just added "module shapelib;" at the beginning of the file, nothing else. Some c headers may require extra steps IMO. 1: https://github.com/jacob-carlborg/dstep 2: https://github.com/aferust/shapelib-d/blob/main/shapelib.d 3: https://github.com/OSGeo/shapelib
Re: How to use a shared library created in cython?
On Sunday, 11 April 2021 at 20:08:15 UTC, affonso elias ferreira junior wrote: Hi everyone, I'm trying to use a shared library created in Cython. example. [...] I am on my mobile phone, and cannot reproduce. But i see that this cast(int function()) should be cast(int function(int)). Try correcting function signatures throughout the code. And extern (c) int test() is irrelevant here because you are loading it via a different mechanism.
Re: Trivial simple OpenGl working example
On Thursday, 8 July 2021 at 13:51:51 UTC, Виталий Фадеев wrote: Hi! I searching trivial simple D/OpenGL working in 2021 year example. It may be triangle. It may be based on any library: SDL, GLFW, Derelict, etc. Can you help me ? this one of mine [1] was very simple in the beginning. It even runs on browser now. [1] https://github.com/aferust/drawee
Re: Trivial simple OpenGl working example
On Friday, 9 July 2021 at 05:17:28 UTC, Виталий Фадеев wrote: On Thursday, 8 July 2021 at 17:20:14 UTC, Ferhat Kurtulmuş wrote: On Thursday, 8 July 2021 at 13:51:51 UTC, Виталий Фадеев wrote: Hi! Each ends with error. Dear Vitaly (Google translates it like that :)), I didn't touch that game for a while. I have just tried to compile and those are the steps to build and run it: Using a Windows 10 x64. (Webassembly build needs crazy efforts, don't try it for now. If you are on a posix system please delete *.a folders from the root folder because they are for webassembly) 1) Open the dub.json or dub.selections.json, and change "bcaa": "~>0.0.5" to "bcaa": "0.0.5" (Recently a contributor and I have made some breaking changes in bcaa). 2) Have those lib and dll files in the root folder of the project: chipmunk.lib // I manually compiled this one libfreetype-6.dl libjpeg-9.dll libpng16-16.dll libtiff-5.dll libwebp-7.dll SDL2_image.dll SDL2_image.lib SDL2_ttf.dll SDL2_ttf.lib SDL2.dll SDL2.lib zlib1.dll basically, those are win 64-bit builds of SDL2, its friends, and their dependencies. If you provide an email, I can send them to you via wetransfer. For posix builds, you will have to figure those out.
Re: Trivial simple OpenGl working example
On Friday, 9 July 2021 at 06:16:08 UTC, Ferhat Kurtulmuş wrote: On Friday, 9 July 2021 at 05:17:28 UTC, Виталий Фадеев wrote: [...] [...] Dear Vitaly (Google translates it like that :)), I didn't touch that game for a while. I have just tried to compile and those are the steps to build and run it: [...] You may also need to use the exact version of "bettercmath": "0.3.1".
Re: Can I make system calls directly from D?
On Friday, 9 July 2021 at 08:08:57 UTC, rempas wrote: I just wonder if I'm able to do system calls directly from D or If I have to create bindings from "unistd.h" from C I don't know if it covers what you want but, druntime has those definitions: https://github.com/dlang/druntime/blob/master/src/core/sys/posix/unistd.d import core.sys.posix.unistd; ... do stuff
How to get element type of a slice?
Hello folks, Hope everyone is doing fine. Considering the following code, in the first condition, I am extracting the type Point from the slice Point[]. I searched in the std.traits, and could not find a neater solution something like ElementTypeOf!T. Is there any neater solution for it? Thanks in advance. ```d static if (isArray!VecPoint){ VecPoint dummy; alias Point = typeof(dummy[0]); } else static if (isRandomAccessRange!VecPoint){ alias ASeq2 = TemplateArgsOf!VecPoint; alias Point = ASeq2[0]; } else static assert(0, typeof(VecPoint).stringof ~ " type is not supported"); ``` Ferhat
Re: How to get element type of a slice?
On Tuesday, 17 August 2021 at 12:26:36 UTC, jfondren wrote: On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş wrote: [...] This one's not in std.traits: ```d import std.range : ElementType; struct Point { int x, y; } unittest { Point[] points; assert(is(ElementType!(typeof(points)) == Point)); } ``` Awesome! Have a great day.
Re: How to get element type of a slice?
On Tuesday, 17 August 2021 at 12:32:45 UTC, Paul Backus wrote: On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş wrote: Hello folks, Hope everyone is doing fine. Considering the following code, in the first condition, I am extracting the type Point from the slice Point[]. I searched in the std.traits, and could not find a neater solution something like ElementTypeOf!T. Is there any neater solution for it? Thanks in advance. `typeof(T.init[0])` Note that `std.range.ElementType` will probably not give the result you expect for character arrays (such as `char[]` and `wchar[]`), due to autodecoding. Thank you. This one looks better.
Re: How to get element type of a slice?
On Tuesday, 17 August 2021 at 12:26:36 UTC, jfondren wrote: On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş wrote: [...] This one's not in std.traits: ```d import std.range : ElementType; struct Point { int x, y; } unittest { Point[] points; assert(is(ElementType!(typeof(points)) == Point)); } ``` Hey, thank you again but, I don't want an instance of Point[] I need: alias T = Point[]; alias ElementOfPointSlice = // element type of T
Re: How to get element type of a slice?
On Tuesday, 17 August 2021 at 12:49:02 UTC, drug wrote: 17.08.2021 15:21, Ferhat Kurtulmuş пишет: [...] https://dlang.org/library/std/range/primitives/element_type.html Yes, that is neat. Thank you.
Re: How to get element type of a slice?
On Tuesday, 17 August 2021 at 13:14:44 UTC, Steven Schveighoffer wrote: On 8/17/21 8:21 AM, Ferhat Kurtulmuş wrote: Hello folks, Hope everyone is doing fine. Considering the following code, in the first condition, I am extracting the type Point from the slice Point[]. I searched in the std.traits, and could not find a neater solution something like ElementTypeOf!T. Is there any neater solution for it? Thanks in advance. ```d static if (isArray!VecPoint){ VecPoint dummy; alias Point = typeof(dummy[0]); } else static if (isRandomAccessRange!VecPoint){ alias ASeq2 = TemplateArgsOf!VecPoint; alias Point = ASeq2[0]; } else static assert(0, typeof(VecPoint).stringof ~ " type is not supported"); ``` If you want the element type of a range (i.e. the thing returned by `range.front`), you can use `ElementType!T` (from std.range.primitives). This returns the element type of the range, which for every array *except* character arrays, gives you the element type of the array. If you want always the element type of the array, even for auto-decoded ranges, use `ElementEncodingType!T`. If you know it's an array, you can just use Paul's solution. Your `isRandomAccessRange` branch seems very suspect. -Steve Very informative, thanks. My code is lying here[1]. I want my struct to accept 2d static arrays, random access ranges, and "std.container.Array". I could achieve it with its present form, and I will probably slightly modify it based on your comments. [1]: https://github.com/aferust/earcut-d/blob/master/source/earcutd.d#L34
Re: How to call destructor before free without dropping @nogc?
On Thursday, 19 August 2021 at 07:30:38 UTC, Bienlein wrote: Hello, I allocate some instance of class C manually and then free the memory again: [...] I just wanted to leave this here. https://github.com/AuburnSounds/Dplug/blob/master/core/dplug/core/nogc.d
Re: How to call destructor before free without dropping @nogc?
On Thursday, 19 August 2021 at 15:38:19 UTC, evilrat wrote: On Thursday, 19 August 2021 at 15:12:03 UTC, Ferhat Kurtulmuş This is cool, but even in unit tests for malloc wrapper there is only simple case with class without references to another class and no dtor. If you examine the entire library, there are various use cases of nogc classes. For instance, a derived class containing references to other class objects [1]. I am not using classes heavily with D. I just once happily used dplug's nogc facilities. When I saw this thread, I just wanted to share it here. Seems like the issue is that one have to add @nogc constructor/destructor overloads for emplace/destroy, and the author can't have @nogc dtor because of writeln (IIRC @nogc using GC is allowed with `debug` anyway), and all class members of another classes must recursively provide them as well. I agree with you. D needs more nogc facilities for OOP. It would not be so hard to include those overloads. Probably, this would violate the strictly defended safety principles of D? [1]: https://github.com/AuburnSounds/Dplug/blob/f67c14fd5ba44225d6669e87f942d641c8bf8ab8/window/dplug/window/cocoawindow.d
Re: How to call destructor before free without dropping @nogc?
On Thursday, 19 August 2021 at 15:38:19 UTC, evilrat wrote: On Thursday, 19 August 2021 at 15:12:03 UTC, Ferhat Kurtulmuş Btw, based on https://github.com/dlang/druntime/blob/master/src/object.d#L4209: import core.lifetime; import core.stdc.stdio; import core.stdc.stdlib; extern (C) void rt_finalize(void *data, bool det=true) @nogc nothrow; // cheap hack here alias destroy = rt_finalize; class SomeClass { int a = 42; this() @nogc { } ~this() @nogc {printf("nogc\n");} this(int val) @nogc { a = val; } } @nogc void main() { SomeClass dynAlloc = cast(SomeClass) malloc(__traits(classInstanceSize, SomeClass)); dynAlloc = emplace!SomeClass(dynAlloc, 123); printf("dynamic %d\n", dynAlloc.a); // 123 //rt_finalize(cast(void*)dynAlloc); destroy(cast(void*)dynAlloc); // cast needed :/ dunno consequences though }
Re: How to get element type of a slice?
On Saturday, 21 August 2021 at 02:59:39 UTC, Jesse Phillips wrote: On Thursday, 19 August 2021 at 04:03:31 UTC, jfondren wrote: On Thursday, 19 August 2021 at 03:32:47 UTC, Jesse Phillips wrote: On Tuesday, 17 August 2021 at 12:33:03 UTC, Ferhat Kurtulmuş wrote: Hey, thank you again but, I don't want an instance of Point[] I need: alias T = Point[]; alias ElementOfPointSlice = // element type of T so, what's the problem? This passes tests: ```d import std.range : ElementType; struct Point { int x, y; } alias T = Point[]; alias ElementOfPointSlice = ElementType!(T); unittest { assert(is(ElementOfPointSlice == Point)); } ``` No issue just trying to give Ferhat a final answer to his question. Thank you. I appreciate it.
Re: foreach() behavior on ranges
On Tuesday, 24 August 2021 at 19:06:44 UTC, Alexandru Ermicioi wrote: On Tuesday, 24 August 2021 at 09:15:23 UTC, bauss wrote: [...] Actually the range contracts don't mention that it needs to be a by value type. It can also be a reference type, i.e. a class. [...] True for any forward range and above, not true for input ranges. The problem with them is that some of them are structs, and even if they are not forward ranges they do have this behavior due to implicit copy on assignment, which can potentially make the code confusing. [...] If we follow the definition of ranges, they must not be copy-able at all. The only way to copy/save, would be to have .save method and call that method. This again is not being properly followed by even phobos implementations. Note, that a better approach would be to replace .save in definition of forward range with a copy constructor, then all non-compliant ranges would become suddenly compliant, while those that have .save method should be refactored to a copy constructor version. [...] You should add .save on assignment if range is a forward range, or just remove the assignment if it is not. Best regards, Alexandru. Just out of curiosity, if a range implementation uses malloc in save, is it only possible to free the memory with the dtor? I worry about that especially when using those nogc range implementations with standard library. I don't have a list of the functions calling save in phobos. Is a save function only meaningful for GC ranges?
Re: How to simply parse and print the XML with dxml?
On Thursday, 9 September 2021 at 17:17:23 UTC, tastyminerals wrote: Maybe I missed something obvious in the docs but how can I just parse the XML and print its content? ``` import dxml.parser; auto xml = parseXML!simpleXML(layout); xml.map!(e => e.text).join.writeln; ``` throws `core.exception.AssertError@../../../.dub/packages/dxml-0.4.3/dxml/source/dxml/parser.d(1457): text cannot be called with elementStart`. I am not fully experienced with it, but once I used it for reading glade files [1]. I used dxml.dom. Hope it helps. 1: https://github.com/aferust/makegtkdclass/blob/master/source/gladeparser.d#L43
Re: How to do "C++ classes"?
On Saturday, 18 September 2021 at 22:16:32 UTC, Adam D Ruppe wrote: On Saturday, 18 September 2021 at 15:38:38 UTC, rempas wrote: I'm seeing in the page about "BeterC" and in the part about the [retained features](https://dlang.org/spec/betterc.html#retained), the #11 says about "COM classes and C++ classes". What are the "C++ classes"? I tried to create a class using "extern(C++)" but this didn't worked. Can someone make an example on that? extern(C++) class Foo {} void main() { scope Foo foo = new Foo(); } I thought it's stack-allocated and scoped. But when I try to return a class instance from a function, it still works? Captain Adam I need an explanation please.
Re: How to do "C++ classes"?
On Monday, 20 September 2021 at 15:45:08 UTC, Adam D Ruppe wrote: On Monday, 20 September 2021 at 15:35:02 UTC, Ferhat Kurtulmuş wrote: I thought it's stack-allocated and scoped. It is. But when I try to return a class instance from a function, it still works? dmd only makes that an error if you specify `@safe` and i think `-dip1000`. Try adding one or both of those and recompiling and see what happens. Note that even if the compiler doesn't error on it, it is undefined behavior to return the stack reference so be sure to treat it right. That is what I thought too. I only tried this on the online compiler. Thank you. Have a great day or night captain.
Re: How to do "C++ classes"?
On Monday, 20 September 2021 at 15:56:44 UTC, Ferhat Kurtulmuş wrote: On Monday, 20 September 2021 at 15:45:08 UTC, Adam D Ruppe wrote: On Monday, 20 September 2021 at 15:35:02 UTC, Ferhat Kurtulmuş wrote: I thought it's stack-allocated and scoped. It is. But when I try to return a class instance from a function, it still works? dmd only makes that an error if you specify `@safe` and i think `-dip1000`. Try adding one or both of those and recompiling and see what happens. Note that even if the compiler doesn't error on it, it is undefined behavior to return the stack reference so be sure to treat it right. That is what I thought too. I only tried this on the online compiler. Thank you. Have a great day or night captain. I also think this is a dirty corner of the complier since it must raise an error for scoped instances of classes.
Template mixin problem with EnumMembers
The below code works as expected on https://run.dlang.io/, but not on my computer. And I don't know why? I want to access enum members like Options.OUT_FILE_NAME instead of Options.StringOption.OUT_FILE_NAME. Please note that the solutions like "alias something = int;" don't work for me. that is not what I need. In my local computer, pragma(msg, fullname) outputs like: cast(StringOption)0 cast(StringOption)1 cast(StringOption)2 some error messages I get: ...\options.d-mixin-86(86,11): Error: declaration expected, not ) ...\options.d-mixin-86(86,11): Error: no identifier for declarator tion ```d import std.stdio; class Options{ public: enum StringOption { OUT_FILE_NAME, RPT_FILE_NAME, MAP_FILE_NAME } import std.traits: EnumMembers; mixin template StrOptAliases() { static foreach(fullname; [EnumMembers!(StringOption)]){ mixin("alias " ~ fullname.stringof[13..$] ~ " = " ~ "Options." ~ fullname.stringof ~ ";\n"); pragma(msg, fullname); } } mixin StrOptAliases; } void main() { int opt = Options.OUT_FILE_NAME; writeln(opt); } ```
Re: Template mixin problem with EnumMembers
On Saturday, 2 October 2021 at 22:24:56 UTC, Adam Ruppe wrote: On Saturday, 2 October 2021 at 22:07:23 UTC, Ferhat Kurtulmuş wrote: [...] You used .stringof. That's undefined behavior. Never use stringof. (except for debugging writes) [...] Thank you Adam! I should stop using stringof. Even the docs say it, I ve just seen that https://dlang.org/spec/property.html#stringof. Probably, I will go for your second solution. I appreciate it.
I need a detailed document about druntime.
I want to know what happens if either Runtime.initialize or terminate is called without matching each other. And why there is no ready-to-use thing like initCount which is accessible in the code? What I want is to bypass runtime initialization if it is already initialized. The below code is my workaround, but it can only keep track of my initializations, not the one that automatically get fired where there is a dmain. Should İ use "bool runMain" to differentiate between the compilations of dll and executable? ```d private { import core.runtime; import core.atomic; shared size_t initCount; void initRT(){ if(!atomicLoad!(MemoryOrder.acq)(initCount)){ Runtime.initialize(); atomicOp!"+="(initCount, 1); } } void termRT(){ if(atomicLoad!(MemoryOrder.acq)(initCount) > 0){ Runtime.terminate(); atomicOp!"-="(initCount, 1); } } } ```
Re: I need a detailed document about druntime.
On Monday, 11 October 2021 at 15:09:12 UTC, Adam D Ruppe wrote: On Monday, 11 October 2021 at 14:56:19 UTC, Ferhat Kurtulmuş wrote: What I want is to bypass runtime initialization if it is already initialized. That what it does by itself, you can call it and it has an internal count. Thank you for your response. But this confuses me: "Each call to initialize must be paired by a call to terminate.' https://dlang.org/library/core/runtime/runtime.initialize.html
Re: I need a detailed document about druntime.
On Monday, 11 October 2021 at 15:24:07 UTC, Adam D Ruppe wrote: On Monday, 11 October 2021 at 15:18:11 UTC, Ferhat Kurtulmuş wrote: "Each call to initialize must be paired by a call to terminate.' It is so the refcount works out. When you call initialize, it does something like: if(refcount == 0) actually intialize; // calls constructors etc refcount++; When you call terminate, it does: refcount--; if(refcount == 0) actually terminate; // calls destructors etc If you don't pair it, the refcount will be off, so the next call to terminate will still see ref > 0 and not actually terminate. The D main inserts a call to init before main and a call to terminate after main automatically. That makes sense, now I see, thank you again Adam.
Re: Crosscompile to Windows
On Friday, 5 November 2021 at 18:11:35 UTC, Luis wrote: There is a obvious way to crosscompile a dub project to Windows from a Linux dev machine ? I don't know if it is possible with dmd. But with LDC you can use mtriple. https://wiki.dlang.org/Cross-compiling_with_LDC
Re: Data conversion
On Tuesday, 16 November 2021 at 19:18:50 UTC, pascal111 wrote: I used "to" keyword which "std.conv" includes for data conversions, but I think that there are some other ways for data conversions, or maybe there are common ways like casting, I hope to know about. For example, next program are using "to": // D programming language import std.stdio; import std.conv; import std.string; int main() { char[] s; int x=0, e; do{ try{ e=1; write("Enter a numeric value: "); readln(s); s=strip(s); x=to!int(s);} catch (Exception err){ stderr.writefln!"Warning! %s"(err.msg); e=0;} }while(!e); writeln("Correct numeric value!"); return 0; } sscanf of C is an option where you cannot use to!T. However, it is a c library function, and it doesn't throw an exception on unexpected inputs. https://dlang.org/library/core/stdc/stdio/scanf.html https://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm
Re: Data conversion
On Tuesday, 16 November 2021 at 19:44:04 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 16 November 2021 at 19:18:50 UTC, pascal111 wrote: [...] sscanf of C is an option where you cannot use to!T. However, it is a c library function, and it doesn't throw an exception on unexpected inputs. https://dlang.org/library/core/stdc/stdio/scanf.html https://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm Upps https://dlang.org/library/core/stdc/stdio/sscanf.html
Re: I need some help for my DCV update
On Friday, 26 November 2021 at 09:16:56 UTC, Ferhat Kurtulmuş wrote: I am working on the DCV to make it compilable with the recent versions of LDC, mir libraries, and stuff. I have not yet simply forked it to work on it. I am including modules one by one for my convenience instead. Hope, I am close to the end. Here is my temporary repo: [...] Upps alias Empty2Type = Slice!(V*, 2LU, SliceKind.contiguous); fx = input.conv!(neumann, typeof(input), Empty2Type, Empty2Type) (kx, emptySlice!(2LU, V), emptySlice!(2LU, V), pool); fy = input.conv!(neumann, typeof(input), Empty2Type, Empty2Type) (ky, emptySlice!(2LU, V), emptySlice!(2LU, V), pool);
I need some help for my DCV update
I am working on the DCV to make it compilable with the recent versions of LDC, mir libraries, and stuff. I have not yet simply forked it to work on it. I am including modules one by one for my convenience instead. Hope, I am close to the end. Here is my temporary repo: https://github.com/aferust/ddcv I run into some problems with module convolution, especially the function conv invoked by the below code which calls canny edge filter: ``` Image image = imread("lena.png"); auto slice = image.sliced.rgb2gray; //auto equalized = slice.histEqualize(slice.flattened.calcHistogram); slice.asImage.imshow("Original"); auto edge = slice.canny!ubyte(15); edge.asImage.imshow("edge"); waitKey(); ``` Somehow, the compiler fails in deducting the types. I need some help from Ilya or other people familiar with mir.ndslice. To reproduce it, download my repo and try to compile it as it is. There is a main with the test code in the repo. Just be sure you have a glfw3.dll/.so. ``` source\dcv\imgproc\filter.d(547,18): Error: template `dcv.imgproc.convolution.conv` cannot deduce function from argument types `!()(Slice!(ubyte*, 2LU, mir_slice_kind.contiguous), Slice!(float*, 2LU, mir_slice_kind.contiguous), Slice!(float*, 2LU, mir_slice_kind.contiguous), Slice!(float*, 2LU, mir_slice_kind.contiguous), TaskPool)` source\dcv\imgproc\filter.d(548,18): Error: template `dcv.imgproc.convolution.conv` cannot deduce function from argument types `!()(Slice!(ubyte*, 2LU, mir_sut, KernelTensor kerlice_kind.contiguous), Slice!(float*, 2LU, mir_slice_kind.contiguous), Slice!(float*, 2LU, mir_slice_kind.contiguous), Slice!(float*, 2LU, mir_slice_kind.contiguous), TaskPool)` lice_kind.contiguous source\dcv\imgproc\convolution.d(78,13):Candidate is: `conv(alias bc = neumann, InputTensor, KernelTensor, MaskTensor = KernelTensor)(InputTensor input, KernelTensor kernel, InputTensor prealloc = InputTensor.init, MaskTensor mask = MaskTensor.init, TaskPool pool = taskPool)` ut, KernelTensor ker source\dcv\imgproc\filter.d(674,18): Error: template instance `dcv.imgproc.filter.calcGradients!(Slice!(ubyte*, 2LU, mir_slice_kind.contiguous), float)` error instantiating r instantiating source\dcv\imgproc\filter.d(694,24):instantiated from here: `canny!(ubyte, ubyte, mir_slice_kind.contiguous)` source\app.d(48,34):instantiated from here: `canny!(ubyte, ubyte, mir_slice_kind.contiguous)` ``` I tried explicitly passing template parameters in dcv.imgproc.filter.calcGradients like below, but in that way, I am getting some other compilation errors: ``` alias Empty2Type = Slice!(V*, 2LU, SliceKind.contiguous); fx = input.conv(neumann, typeof(input), Empty2Type, Empty2Type) (kx, emptySlice!(2LU, V), emptySlice!(2LU, V), pool); fy = input.convinput.conv(neumann, typeof(input), Empty2Type, Empty2Type) (ky, emptySlice!(2LU, V), emptySlice!(2LU, V), pool); ``` Thanks in advance!
Re: I need some help for my DCV update
On Friday, 26 November 2021 at 09:31:42 UTC, drug wrote: On 26.11.2021 12:16, Ferhat Kurtulmuş wrote: InputTensor = Slice!(ubyte*, 2LU, mir_slice_kind.contiguous) and KernelTensor = Slice!(float*, 2LU, mir_slice_kind.contiguous). The key argument of slices is a pointee type - InputTensor pointee has ubyte type and KernelTensor has float. I'm not sure this solves your problem but at least I'd start from here. Yes, but this is how the original code was written.
Re: I need some help for my DCV update
On Friday, 26 November 2021 at 09:16:56 UTC, Ferhat Kurtulmuş wrote: I am working on the DCV to make it compilable with the recent versions of LDC, mir libraries, and stuff. I have not yet simply forked it to work on it. I am including modules one by one for my convenience instead. Hope, I am close to the end. Here is my temporary repo: https://github.com/aferust/ddcv After dealing with, various problems, it turns out the main problem was ndiota with pool.parallel. https://github.com/aferust/ddcv/blob/main/source/dcv/imgproc/filter.d#L564 ``` foreach (row; /*pool.parallel(*/ndiota(input.shape)/*)*/) // parallel loop causes a linker error { row.each!(i => calcGradientsImpl(fx[i], fy[i], mag[i], orient[i])); } ``` The parallel loop causes a linker error here? error LNK2019: unresolved external symbol _D3mir7ndslice5slice__T9mir_sliceTSQBhQBg8iterator__T13FieldIteratorTSQCqQCp5field__T11ndIotaFieldVmi2ZQsZQCbVmi1VEQEjQEiQEd14mir_slice_kindi2ZQEq__T10lightScopeZQnMxFNaNbNdNiNfZSQGvQGuQGp__TQGmTQGfVmi1VQDli2ZQHe referenced in function _D3mir7ndslice5slice__T9mir_sliceTSQBhQBg8iterator__T13FieldIteratorTSQCqQCp5field__T11ndIotaFieldVmi2ZQsZQCbVmi1VEQEjQEiQEd14mir_slice_kindi2ZQEq__T8opEqualsTQEvVQBxi2ZQuMxFNaNbNiNeKxSQHbQHaQGv__TQGsTQGlVmi1VQDri2ZQHkZb .dub\build\application-debug-windows-x86_64-ldc_v1.28.0-24645713CE34BFE817BFD3D964187D0E\ddcv.exe : fatal error LNK1120: 1 unresolved externals
Re: I need some help for my DCV update
On Saturday, 27 November 2021 at 11:35:21 UTC, Salih Dincer wrote: I also found similar errors but couldn't solve them. I think it has to do with mir.slice.kind. Exactly Kind Topology... I won't use parallel for it as a workaround until it is solved in the mir-algorithm.
How to do operator overloading for <, >, <=, >=, !=, and == between struct and int?
I am writing an opencv binding and need something like: Mat m = another_mat > 5; Docs does not cover this sitiuation: https://dlang.org/spec/operatoroverloading.html#compare opBinary does not support those operators, and Section "Overloading <, <=, >, and >=" describes overloaded operators returning only int values. My struct is defined here: https://github.com/aferust/opencvd/blob/master/source/opencvd/cvcore.d
Re: How to do operator overloading for <, >, <=, >=, !=, and == between struct and int?
On Sunday, 21 April 2019 at 18:17:00 UTC, Adam D. Ruppe wrote: On Sunday, 21 April 2019 at 18:07:08 UTC, Ferhat Kurtulmuş wrote: I am writing an opencv binding and need something like: Mat m = another_mat > 5; D does not support that. The comparison operators are always just true or false (as determined by the int opCmp or the bool opEquals returns), they do not return other types. You will probably have to use a named method instead. You could kinda fake it with a string template arg: Mat m = another_map.cmp!">"(5); But I'd probably just do it Mat m = another_map.is_greater_than(5); which imo is more readable. but the implementations are the same - in both cases, your custom function. I am a little disappointed :( I will go for named method instead. Thank you for a fast response.
Memory management by interfacing C/C++
Hi, I am wrapping some C++ code for my personal project (opencvd), and I am creating so many array pointers at cpp side and containing them in structs. I want to learn if I am leaking memory like crazy, although I am not facing crashes so far. Is GC of D handling things for me? Here is an example: ``` //declaration in d struct IntVector { int* val; int length; } // in cpp typedef struct IntVector { int* val; int length; } IntVector; // cpp function returning a struct containing an array pointer allocated with "new" op. IntVector Subdiv2D_GetLeadingEdgeList(Subdiv2D sd){ std::vector iv; sd->getLeadingEdgeList(iv); int *cintv = new int[iv.size()]; // I don't call delete anywhere? for(size_t i=0; i < iv.size(); i++){ cintv[i] = iv[i]; } IntVector ret = {cintv, (int)iv.size()}; return ret; }; // call extern c function in d: extern (C) IntVector Subdiv2D_GetLeadingEdgeList(Subdiv2d sd); int[] getLeadingEdgeList(){ IntVector intv = Subdiv2D_GetLeadingEdgeList(this); int[] ret = intv.val[0..intv.length]; // just D magic. Still no delete anywhere! return ret; } ``` The question is now: what will happen to "int *cintv" which is allocated with new operator in cpp code? I have many code similar in the project, but I have not encounter any problem so far even in looped video processings. Is GC of D doing deallocation automagically? https://github.com/aferust/opencvd
Re: Memory management by interfacing C/C++
On Sunday, 28 April 2019 at 03:54:17 UTC, Paul Backus wrote: On Saturday, 27 April 2019 at 22:25:58 UTC, Ferhat Kurtulmuş wrote: Hi, I am wrapping some C++ code for my personal project (opencvd), and I am creating so many array pointers at cpp side and containing them in structs. I want to learn if I am leaking memory like crazy, although I am not facing crashes so far. Is GC of D handling things for me? Here is an example: [...] The question is now: what will happen to "int *cintv" which is allocated with new operator in cpp code? I have many code similar in the project, but I have not encounter any problem so far even in looped video processings. Is GC of D doing deallocation automagically? https://github.com/aferust/opencvd D's GC only collects memory allocated with D's `new` operator. Memory allocated by C++'s `new` operator must be freed by C++'s `delete` operator. You are right. I am rewriting the things using mallocs, and will use core.stdc.stdlib.free on d side. I am not sure if I can use core.stdc.stdlib.free to destroy arrays allocated with new op.
Re: Memory management by interfacing C/C++
On Monday, 29 April 2019 at 00:53:34 UTC, Paul Backus wrote: On Sunday, 28 April 2019 at 23:10:24 UTC, Ferhat Kurtulmuş wrote: You are right. I am rewriting the things using mallocs, and will use core.stdc.stdlib.free on d side. I am not sure if I can use core.stdc.stdlib.free to destroy arrays allocated with new op. core.stdc.stdlib.free is (as the name suggests) the standard C `free` function. As such, it can only be used to free memory allocated by the standard C functions `malloc`, `calloc`, and `realloc`. This is the same in D as it is in C and C++. Thank you. It is now like: /* c/cpp side */ extern (C) void deleteArr(void* arr); void deleteArr(void* arr){ delete[] arr; } struct IntVector Subdiv2D_GetLeadingEdgeList(Subdiv2D sd){ std::vector iv; sd->getLeadingEdgeList(iv); int *cintv = new int[iv.size()]; for(size_t i=0; i < iv.size(); i++){ cintv[i] = iv[i]; } IntVector ret = {cintv, (int)iv.size()}; return ret; }; /* c/cpp side */ ... int[] getLeadingEdgeList(){ // d function IntVector intv = Subdiv2D_GetLeadingEdgeList(this); int[] ret = intv.val[0..intv.length].dup; deleteArr(intv.val); return ret; } ...
Re: Memory management by interfacing C/C++
On Monday, 29 April 2019 at 14:38:54 UTC, 9il wrote: On Saturday, 27 April 2019 at 22:25:58 UTC, Ferhat Kurtulmuş wrote: [...] Hello Ferhat, You can use RCArray!T or Slice!(RCI!T) [1, 2] as common thread safe @nogc types for D and C++ code. See also integration C++ example [3] and C++ headers [4]. RCArray (fixed length) [1] http://mir-algorithm.libmir.org/mir_rc_array.html RCSlice (allows to get subslices) [2] http://mir-algorithm.libmir.org/mir_ndslice_allocation.html#rcslice C++ integration example [3] https://github.com/libmir/mir-algorithm/tree/master/cpp_example C++ headers [4] https://github.com/libmir/mir-algorithm/tree/master/include/mir An opencv d binding using ndslice substituting cv::Mat would be useful like opencv-python using numpy. However, I started opencvd about a month ago, and I am very new to d. For now, I am the only contributor with zero mir.ndslice experience. When I gain more experience with ndslice, I would try it as substitution for cv::Mat. Thank you for links 9il. I will take a look at them.
Is there any performance penalty for static if?
Hi, Maybe I already know the answer, but have to be sure about this. I am emulating this cpp code "int val = mat.at(row, col)" like: T at(T)(int row, int col){ static if (T.stringof == "float"){ return getFloatAt(row, col); } else static if (T.stringof == "double"){ return getDoubleAt(row, col); } else static if (T.stringof == "int"){ return getIntAt(row, col); } else static if (T.stringof == "ubyte"){ return getUCharAt(row, col); } else static if (T.stringof == "byte"){ return getSCharAt(row, col); } else static if (T.stringof == "short"){ return getShortAt(row, col); } } This works as expected, and I know conditions of "static if" is determined at compile time and I assume no speed penalty here?
Re: Is there any performance penalty for static if?
On Wednesday, 15 May 2019 at 22:13:18 UTC, Jonathan M Davis wrote: On Wednesday, May 15, 2019 4:03:39 PM MDT Ferhat Kurtulmuş via Digitalmars- d-learn wrote: [...] If you really want to see what happens (for any piece of code), then you can look at the generated assembly, but static if is completely a compile-time construct. It affects which code ends up in the binary. For instance, if your at function there were instantiated with double, then the resulting function would be [...] Thanks a lot! This is what I was exactly expecting.
Re: Is there any performance penalty for static if?
On Thursday, 16 May 2019 at 00:18:25 UTC, user1234 wrote: On Wednesday, 15 May 2019 at 22:03:39 UTC, Ferhat Kurtulmuş wrote: [...] You've been given the answer but about this particular piece of code, rather use the "is" expression static if (is(T == float)) {} else static if (is(T == double)) {} etc. Yes, it is now much better. Thank you.
Re: How to create GTK+ apps with Glade and D on windows
On Monday, 3 June 2019 at 10:32:25 UTC, Ferhat Kurtulmuş wrote: On Friday, 31 May 2019 at 18:47:06 UTC, Obsidian Jackal wrote: [...] I am writing on my cell phone, so cannot address the whole thing. When I started to learn d, I was playing around a boiler plate to imitate something like javafx and it's builder. Here is a meson based project, which creates a gtkd project including a hello world window, a Glade file, its controller class, and an auto calling python script to update controller class after changing the UI. The paths in src/meson.build must be set before building the project creator. I am sorry, I could not find some free time to make it a more generic project builder, but I think it may give you some idea. Again I was a very beginner when I was working on it. https://gitlab.com/aferust/gtkdappcreator
Re: How to create GTK+ apps with Glade and D on windows
On Friday, 31 May 2019 at 18:47:06 UTC, Obsidian Jackal wrote: I'm new to D and want to create GTK+ apps. I have Visual Studio, Glade, the Gtk+ runtime, DMD, and DUB installed. What steps, guides, or advice should I follow to be able to be able to use these tools together to make a sane app?. I am writing on my cell phone, so cannot address the whole thing. When I started to learn d, I was playing around a boiler plate to imitate something like javafx and it's builder. Here is a meson based project, which creates a gtkd project including a hello world window, a Glade file, its controller class, and an auto calling python script to update controller class after changing the UI. The paths in src/meson.build must be set before building the project creator. I am sorry, I could not find some free time to make it a more generic project builder, but I think it may give you some idea. Again I was a very beginner when I was working on it.
Re: How to evaluate a JSON file at compile time and create a struct out of it?
On Friday, 4 October 2024 at 08:45:49 UTC, holyzantaclara wrote: Hello hello everyone ^_^, I am new in D, and started this morning. I found a way to read a file at compile time with `-J.` and `static string content = import("my_json_file.json")` But then tried to statically evaluate with `static JSONValue j = parseJson(content)` but it fails. ```sh main.d(12): Error: variable `content` cannot be read at compile time main.d(12):called from here: `readJSON(content)` ``` Is there a way to achieve this? My goal is to have a JSONValue at runtime that would be already present in the binary. Cause the JSONValue is huge. Like megabytes of data... And no I don't want to deal with a database for now. Thank you ! Your issue is related to static that seems it does not trigger a compile time evaluation.
Re: How to evaluate a JSON file at compile time and create a struct out of it?
On Friday, 4 October 2024 at 10:33:37 UTC, Ferhat Kurtulmuş wrote: On Friday, 4 October 2024 at 08:45:49 UTC, holyzantaclara wrote: [...] Your issue is related to static that seems it does not trigger a compile time evaluation. According to the docs, static does not imply immutability at al. For your case, you can also use enum. https://dlang.org/spec/attribute.html#static
Re: How to evaluate a JSON file at compile time and create a struct out of it?
On Friday, 4 October 2024 at 08:45:49 UTC, holyzantaclara wrote: Hello hello everyone ^_^, I am new in D, and started this morning. I found a way to read a file at compile time with `-J.` and `static string content = import("my_json_file.json")` But then tried to statically evaluate with `static JSONValue j = parseJson(content)` but it fails. ```sh main.d(12): Error: variable `content` cannot be read at compile time main.d(12):called from here: `readJSON(content)` ``` Is there a way to achieve this? My goal is to have a JSONValue at runtime that would be already present in the binary. Cause the JSONValue is huge. Like megabytes of data... And no I don't want to deal with a database for now. Thank you ! If you use dub, have a "stringImportPaths" field in dub.json which works for me. dub.json: ``` { "name": "foo", "stringImportPaths": [ "./" ] } ``` my_json_file.json: ``` { "name": "foo" } ``` ``` import std; void main(){ immutable content = import("my_json_file.json"); pragma(msg, content); immutable mjson = parseJSON(content); writeln(mjson); } ``` ``` yields: ``` { "name": "foo" } Linking foo Running foo.exe {"name":"foo"} ```