7""" Simple script that generates references to all
8needed MDEvent<X>/MDLeanEvent<X> instantiations. """
14mdevent_types = [
"MDLeanEvent",
"MDEvent"]
16header =
"""/* Code below Auto-generated by '%s'
22 os.path.basename(__file__),
23 datetime.datetime.now(),
27/* CODE ABOWE WAS AUTO-GENERATED BY %s - DO NOT EDIT! */
29 os.path.basename(__file__)
33def build_macro(padding, min_dimension=1, max_dimensions=4, const=""):
34 """Return the macro code CALL_MDEVENT_FUNCTION
36 min_dimension :: to avoid compiler warnings, limit to dimensions higher than this
37 mad_dimension :: by default, maximal numner of dimesnions to be generated
38 const :: set to "const " to make a const equivalent
42/** Macro that makes it possible to call a templated method for
43 * a MDEventWorkspace using a IMDEventWorkspace_sptr as the input.
45 * @param funcname :: name of the function that will be called.
46 * @param workspace :: IMDEventWorkspace_sptr input workspace.
49#define %sCALL_MDEVENT_FUNCTION%s(funcname, workspace) \\
53 macro =
"""%s%sMDEventWorkspace<%s, %d>::sptr %s = std::dynamic_pointer_cast<%sMDEventWorkspace<%s, %d> >(workspace); \\
54if (%s) funcname<%s, %d>(%s); \\
60 suffix =
"%d" % min_dimension
63 s = macro_top % (prefix, suffix)
65 for mdevent_type
in mdevent_types:
66 for nd
in xrange(1, max_dimensions + 1):
67 if nd >= min_dimension:
68 eventType =
"%s<%d>" % (mdevent_type, nd)
69 varname =
"MDEW_%s_%d" % (mdevent_type.upper(), nd)
71 varname =
"CONST_" + varname
72 s += macro % (padding, const, eventType, nd, varname, const, eventType, nd, varname, eventType, nd, varname)
79 """Return a string with the spaces padding the start of the given line."""
90 """Look line-by-line in lines[] for a line that starts with pattern. Return
91 the line number in source where the line was found,
and the padding (
in spaces) before it
"""
92 searcher = re.compile(pattern)
93 for n
in xrange(startat, len(lines)):
94 found = searcher.search(lines[n])
104 """Look up through header file and the string which identifies how many dimensions have to be instantiated"""
105 searcher = re.compile(
r"(?<=MAX_MD_DIMENSIONS_NUM)(\s*\=\s*)\d+")
106 for i
in xrange(len(lines)):
107 found = searcher.search(lines[i])
110 return re.search(
r"\d", rez).group()
112 raise IOError(
"can not find the string which defines the number of dimensions to process")
116 """Read the file and separate it into three parts with the part between input markers to be generated and two others left unchanged.
118 @param -- file_name -- full file name to open
119 @param -- start_marker -- the marker which indicate first line of autogenerated file
120 @param -- end_marger -- the margker which indicate last line of autogenerated file
122 @return padding -- the number of spaces to insert
in front of autogenerated lines
123 @return lines_before -- list of lines before autogenerated one (including start_marker)
124 @return lines_after -- list of lines after autogenerated one (including end_marker)
127 f = open(file_name,
"r")
130 lines = s.split(
"\n")
134 print(
"Lines for autogenerated code: ", n1, n2)
135 if n1
is None or n2
is None:
136 raise Exception(
"Could not find the marker in the " + file_name +
" file.")
138 lines_before = lines[: n1 + 1]
139 lines_after = lines[n2:]
142 return (padding, lines_before, lines_after)
146 print(
"Generating MDEventFactory")
149 classes_cpp = [
"MDBoxBase",
"MDBox",
"MDEventWorkspace",
"MDGridBox",
"MDBin",
"MDBoxIterator"]
152 "../inc/MantidDataObjects/MDEventFactory.h",
"//### BEGIN AUTO-GENERATED CODE",
"//### END AUTO-GENERATED CODE"
156 print(
" numDimensions to be generated: ", nDim)
158 dimensions = range(1, nDim + 1)
160 header_lines = map(
lambda x: padding + x, header.split(
"\n"))
161 footer_lines = map(
lambda x: padding + x, footer.split(
"\n"))
163 lines += header_lines
170 classes = [
"MDBox",
"MDBoxBase",
"MDGridBox",
"MDEventWorkspace",
"MDBin"]
172 lines.append(
"\n%s// ------------- Typedefs for %s ------------------\n" % (padding, c))
173 mdevent_type =
"MDLeanEvent"
174 for nd
in dimensions:
175 lines.append(
"%s/// Typedef for a %s with %d dimension%s " % (padding, c, nd, [
"",
"s"][nd > 1]))
176 lines.append(
"%s typedef %s<%s<%d>, %d> %s%dLean;" % (padding, c, mdevent_type, nd, nd, c, nd))
177 mdevent_type =
"MDEvent"
178 for nd
in dimensions:
179 lines.append(
"%s/// Typedef for a %s with %d dimension%s " % (padding, c, nd, [
"",
"s"][nd > 1]))
180 lines.append(
"%stypedef %s<%s<%d>, %d> %s%d;" % (padding, c, mdevent_type, nd, nd, c, nd))
184 lines += footer_lines + lines_after
186 f = open(
"../inc/MantidDataObjects/MDEventFactory.h",
"w")
191 padding, lines, lines_after =
parse_file(
"./MDEventFactory.cpp",
"//### BEGIN AUTO-GENERATED CODE",
"//### END AUTO-GENERATED CODE")
193 header_lines = map(
lambda x: padding + x, header.split(
"\n"))
194 footer_lines = map(
lambda x: padding + x, footer.split(
"\n"))
196 lines += header_lines
199 for c
in mdevent_types:
200 lines.append(
"%s// Instantiations for %s" % (padding, c))
201 for nd
in dimensions:
202 lines.append(
"%s template class DLLExport %s<%d>;" % (padding, c, nd))
205 for c
in classes_cpp:
206 lines.append(
"%s// Instantiations for %s" % (padding, c))
207 for mdevent_type
in mdevent_types:
208 for nd
in dimensions:
209 lines.append(
"%s template class DLLExport %s<%s<%d>, %d>;" % (padding, c, mdevent_type, nd, nd))
212 lines += footer_lines + lines_after
213 f = open(
"./MDEventFactory.cpp",
"w")
221 print(
"The available IDs on the templated MDEventWorkspace classes may have changed.")
222 print(
"Please update the casting IDs in PythonInterface/mantid/api/IMDEventWorkspace accordingly")
225if __name__ ==
"__main__":
def find_line_number(lines, pattern, startat=0)
def parse_file(file_name, start_marker, end_marker)
def build_macro(padding, min_dimension=1, max_dimensions=4, const="")