Skip to content

WIP: Fix ITK_TEMPLATE_EXPORT for template classes

Victor Poughon requested to merge itk-export2 into develop

Second attempt at !237 (closed). Work in progress but code review welcome

  • Replace ITK_EXPORT by ITK_TEMPLATE_EXPORT in template classes
  • Add ITK_EXPORT_TEMPLATE to template classes that have no export macro

This is only part 1. Part 2 will be fixing non template classes.

Here is my script so far:

#!/usr/bin/env python3

import sys
import re

def get_module_name(filename):
    return 'OTB' + filename.split("/")[-3]

def match_is_good(filename, match):
    if "ITK_TEMPLATE_EXPORT" not in match.group():
        return False
    return True

def match_function(match):
    if not match_is_good(filename, match):
        if not match_is_good(filename, match):
            print("Error in file {} at {start}-{end}".format(filename, start = match.start(), end = match.end()))
            print ("{match}\n".format(match = match.group()))

    # don't touch deprecated stuff
    if "deprecated" in match.group().lower():
        return match.group()

    # replace ITK_EXPORT
    if "ITK_EXPORT" in match.group():
        return match.group().replace("ITK_EXPORT", "ITK_TEMPLATE_EXPORT")

    # replace bare with inheritance
    name_regex = r"(template[^/]*?>\s+class) +(\S+) *(:[^/]*?{)"
    if re.match(name_regex, match.group(), flags=re.MULTILINE | re.DOTALL):
        return re.sub(name_regex, r"\1 ITK_TEMPLATE_EXPORT \2 \3", match.group())

    # replace bare without inheritance
    name_regex = r"(template[^/]*?>\s+class) +(\S+)(\s*?{)"
    if re.match(name_regex, match.group(), flags=re.MULTILINE | re.DOTALL):
        return re.sub(name_regex, r"\1 ITK_TEMPLATE_EXPORT \g<2>\3", match.group())

    return match.group()

def process_file(filename):
    content = open(filename, "r").read()

    # match template class declarations
    regex = r"template[^/]*?>\s+class[^/]*?{"

    matches = list(re.finditer(regex, content, re.MULTILINE | re.DOTALL))

    content = re.sub(regex, match_function, content, re.MULTILINE, re.DOTALL)

    with open(filename, "w") as f:
        f.write(content)

if __name__ == "__main__":
    for filename in sys.argv[1:]:
        process_file(filename)
Edited by Victor Poughon

Merge request reports