www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting __COLUMN__ of source code location.

reply realhet <real_het hotmail.com> writes:
Hi,

I can access the special tokens: __MODULE__, __LINE__, but how 
can I access the column.

Is there a way to "hack" it out from LDC2?

All the error messages contain column information, also I've 
found __traits(getLocation, symbol) also reporting the column.

But how to get this information for the caller's location?

(
Why is this important?
- I have an immediate GUI which is using this __MODULE__LINE__ 
data to generate a unique ID. And I must put every GUI control 
creation command on a different line or provide a unique ID 
manually.
- Now I'm planning an automated data logging debugging thing and 
it will sample expressions. The one expression per source code 
line is also problematic. I can give each PROBE an unique ID by 
using a preprocessor, but that's not a nice solution, that would 
be an overcomplication I try to avoid.

Example:
PROBE!0(somefunction(PROBE!1(expr1), PROBE!2(expr2)));
Here are 3 PROBES on the same line, but the PROBE template is 
unable to distinguish them without the __COLUMN__.
)

Thank You for your time.
Jul 23 2023
parent reply IchorDev <zxinsworld gmail.com> writes:
I'm not aware of any way to do that exact thing. Measuring what 
column a line is on would be quite subjective. How wide is a tab 
space? Technically, it could be any number of columns depending 
on the display configuration used.

On Sunday, 23 July 2023 at 15:01:51 UTC, realhet wrote:
 Why is this important?
 - I have an immediate GUI which is using this __MODULE__LINE__ 
 data to generate a unique ID. And I must put every GUI control 
 creation command on a different line or provide a unique ID 
 manually.
But you'd have to provide a unique ID manually anyway if, for instance, you create UI elements in a for loop: ```d for(size_t i=0; i<10; i++){ button(); } ``` I'd suggest using something more like Dear ImGui's ID system instead.
Jul 27 2023
parent realhet <real_het hotmail.com> writes:
On Thursday, 27 July 2023 at 16:17:28 UTC, IchorDev wrote:
 I'm not aware of any way to do that exact thing. Measuring what 
 column a line is on would be quite subjective.
When I compile(LDC2) a something with an error and using the --vcolumns argument I get this: onlineapp.d(14,5): Error: found `}` when expecting `;` following statement The error is on Line 14, Column 5. (I don't care how much it counts the TAB character, as long as it is unique.) So the information is accessible in the compiler. I just can't access from the language. ``` I can access __LINE__ but I can't access something like __COL__. ``` I just hope there is other ways to get this, maybe with an LDC specific traits or something.
 But you'd have to provide a unique ID manually anyway if, for 
 instance, you create UI elements in a for loop:
 ```d
 for(size_t i=0; i<10; i++){
 	button();
 }
 ```

 I'd suggest using something more like Dear ImGui's ID system 
 instead.
Thanks for suggesting I've checked. As I see it is using an ID stack. I do something similar with a 32bit hash and using XOR operation to simulate a stack. Also for debugging and critical stuff there is an option to store this id stack in a string with meaningful names. So I can modify the id directly, when I pit the button() in a for loop. All the Controls automatically modifying the id by their type, source module and source line number (just a hash or a meaningful string for debug). ``` line 15: with(player){ if(Btn("Play")) play; if(Btn("Stop")) stop; } ``` So the 2 id's for the buttons will be the same: [Btn,module,15] and [Btn,module,15] <- that's a duplicated id. But if I'd have access to sourcecolumn, that would be so cool: [Btn,module,15,22] and [Btn,module,15,34] Here are the 2 workarounds right now: ``` line 15: with(player){ if(Btn("Play"), genericId(1)) play; if(Btn("Stop"), genericId(1)) stop; } ``` ``` line 15: with(player){ if(Btn("Play")) play; line 16: if(Btn("Stop")) stop; } ``` It's not a critically important feature, it's just helping the UI and the code that generates the UI being in the same visual layout: horizontal in this case.
Jul 29 2023