[CMake] 2D arrays

Michael Hertling mhertling at online.de
Wed Nov 30 20:18:02 EST 2011


On 11/30/2011 03:29 AM, Robert Dailey wrote:
> On Tue, Nov 29, 2011 at 5:57 PM, Michael Hertling <mhertling at online.de>wrote:
> 
>> Just some spontaneous questions/remarks:
>>
> 
> Thanks; I really appreciate it!
> 
> 
>> - Why do you use macros instead of functions?
>>
> 
> I use macros so the _array2d_ variables fall through the scope of the macro
> and are available in the next call to array2d_advance(). I could use
> functions + properties but this solution works, I saw no need to use the
> slightly more verbose global properties feature.

What I've had in mind are functions and the PARENT_SCOPE option of SET().

>> - What's about cleaning up the numerous helper variables,
>>  e.g. by an additional array2d_cleanup() function or the like?
>>
> 
> What specific helper variables are you referring to? I would most likely
> use more uniquely mangled local variables instead, but I figured the
> preceeding underscore in front of each was sufficient. What would you
> recommend here? I'd like to avoid a cleanup() method because it seems
> unnecessary. If my local variables cause conflicts later, and if name
> mangling isn't the idiomatic solution, I'd like to hear whatever idea you
> have (that doesn't involve cleanup() if possible).

Because you must preserve some variables between multiple function/macro
invocations - i.e. you can't work with local variables only - one might
like to have a mean to purge these variables when their job is done in
order to not pollute the current scope more than necessary. Of course,
it's not absolutely indispensable, but if a set of non-local variables
is used just internally by a set of related functions, it is possibly
worth a thought how to not have these variables hang around later on.

> - In array2d_begin_loop(), couldn't you rearrange the parameters,
>>  array2d_begin_loop(advanced "fruit;animal" ${two_dee_array}), use
>>  LIST(LENGTH var_names _array2d_width) and take ARGN as the array?
>>
> 
> What would this buy me? Typically I've avoided ARGN unless it is absolutely
> necessary, but a variable in this case seems more direct. Can you explain
> what benefits this has?

AFAICS, you don't need the "width" parameter because it can be obtained
as the length of "var_names". Thus, additionally specifying "width" is
error-prone; e.g. (width,var_names)=(3,"fruit;animal") is inherently
invalid.

Using ARGN would allow the user to specify the array elements freely as
separate arguments at the end of array2d_begin_loop(), without the need
to pass them in as a semicolon-separated list in double quotes.

Regards,

Michael


More information about the CMake mailing list