#6 Array support for c++ code generator


Whenever you create a GUI that contains elements in a tabular arrangement its pretty cumbersome that wxFormBuilder creates an individual pointer for each element, it would be much easier to access the elements if they would be put in an array. This patch adds this functionality for the C++ code generator, the change is fully transparent, there is no need to update any generator templates or plugins. This is only added for the C++ generator because i only know this language well enough, i'm not sure if it can be ported to the other generators.

How to use it:
Simply add an array subscript to the element name, e.g. m_item[0], m_item[1], thats all. This will create a static array of the size 2 with the name m_item. It is not necessary for the subscripts to be contigous, holes are allowed.

How its implemented:
For each class element a preprocessing step was added to the code generator. It scans through all child elements and records all array names together with the highest found index. When the code generator creates the element declarations it checks every element name against the found arrays, if it finds a match it checks if a declaration for this array was already done. If not i use the fact that to declare a static array in c++ you simply have to add a subscript with the size behind the name. Temporarily the name of the object gets changed to the name of the array with the size of the array as subscript, the code generator template gets executed and then the object name gets restored and the array gets marked as declared. Because the array is static there is no extra construction step necessary, default construction is sufficient enough. Due to this way its not necessary to update any code generator templates, but i dont know if this can be done for the other generator languages so easy as well.

1 Attachments


  • sodev

    sodev - 2014-07-16

    An update to my previous patch, objects that have the visibility none or are intermediate objects are not exposed in the header and therefor are not touched by the array name processing, this patch includes these objects as well.

    However while using my patch i discovered some problems, some generator templates use the object name and append something to create another name, e.g. containers with items construct the name of the array with the items like this, this leads to an invalid name with the array subscript in the middle of the name. This cannot easily fixed by simply adjusting the name before the call of the template cause in some parts of the template the array decorated name is required. I'm going to look into this further to find a solution.

  • sodev

    sodev - 2014-07-18

    I analyzed the problem further and apparently it only happens when the #append macro is used. This macro is used for two purposes, generate names for local objects or generate names for methods using the name of the object to make it unique. Now if that name contains these brackets [] this results in an invalid name.

    The only solution i found is that the append macro tests the current output and if it ends with a ] it simply removes the brackets but keeps the index between them. This way there will still be unique names generated for each array member and this name will be valid as object or method name.

    This is the least invasive way i found to solve the issue, however this change was implemented in the common part of the code generator and therefor applies to every language, im not sure if this causes or solves problems for the other languages as well.

  • ryanjmulder

    ryanjmulder - 2016-04-25
    • status: open --> wont-fix
  • ryanjmulder

    ryanjmulder - 2016-04-25

    Thanks very much for you patch.
    I think this idea is interesting and helpful, but the implementation adds a lot of arrays vs not arrays handling that I think will be difficult to maintain.