Forum > General

[SOLVED] $STOP directive with value taken from SizeOf

(1/5) > >>

furious programming:
There are several data types in my project, the size of which must be strictly defined. If the size of a given type is different than the required size, compilation is to be aborted and an appropriate message displayed in the Messages window. Currently, I do it this way:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TMyType = OtherUnit.OtherType;   {$IF SIZEOF(TMyType) <> 1}    {$STOP the size of the TMyType is not 1 byte}  {$ENDIF}
And it works great — in the event of a problem, compilation is interrupted and the fixed message is displayed in the message window.

However, this allows me to know that the size of a given data type is incorrect, but I don't know what it is. Is there any way to use SIZEOF and use the received value to format the message body? I mean something like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TMyType = OtherUnit.OtherType;   {$IF SIZEOF(TMyType) <> 1}    {$STOP the size of the TMyType is SIZEOF(TMyType) byte(s), but must be 1 byte}  {$ENDIF}
to receive this message:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---'the size of the TMyType is 4 byte(s), but must be 1 byte'
Is such a thing possible?

speter:
Could you move the size check to the unit's initialisation section?

Something like (untested code):

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit xxx;interfaceuses ...type  TMyType = ... ...implementationvar  zzz : integer; ... begin  zzz := sizeof(TMyType);  if zzz <> 1 then    showmessage('TMyType is '+zzz.tostring+' bytes');end. 
Of course, this won't happen during compilation, so probably isn't that useful. ;(

cheers
S.

440bx:
Unfortunately I don't have an answer for you but, I have a question instead.  Do you happen to have a link to a page that documents all the functions that can be called at compile time ?

I ask for two reasons, I didn't know that sizeof could  be used in compiler directives.  I eventually found a documentation page where it was stated that "sizeof" could be used in compile time expressions but, I was not able to find a list of compile time functions.

Maybe (big maybe), "length" is another function that can be used in compiler directives (I haven't tried it but, it would be very nice if it were available.)

A page that documents all the functions that can be used in compiler directive expressions would be very nice.  If you happen to know of one, please do share.

furious programming:
@speter: nope, I would like to use compiler directives to abort compilation, not to abort the application startup. 8)


--- Quote from: 440bx on May 08, 2021, 02:37:12 am ---Unfortunately I don't have an answer for you but, I have a question instead.  Do you happen to have a link to a page that documents all the functions that can be called at compile time ?
--- End quote ---

@440bx: hmm… no. Unfortunately, (AFAIK) the issue of directives is not fully documented. For example, the + operator that can be used with DEFINED is not described, but can be used to sum results of the DEFINED and then, test the result against the fixed number. Example:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$IF DEFINED(FOO) + DEFINED(BAR) + DEFINED(QUX) <> 3}  {$STOP not all mandatory symbols declared}{$ENDIF}

--- Quote ---I ask for two reasons, I didn't know that sizeof could  be used in compiler directives.
--- End quote ---

SIZEOF can be used in combination with $IF, but I don't know if it can be used to format the error message text. That's why I'm asking, if it is possible to somehow format the message text with some integers.

440bx:
@furious programming

Thank you for sharing what you know about it.

Navigation

[0] Message Index

[#] Next page

Go to full version