32def build_macro(padding, min_dimension=1, max_dimensions=4, const=""):
33 """Return the macro code CALL_MDEVENT_FUNCTION
35 min_dimension :: to avoid compiler warnings, limit to dimensions higher than this
36 mad_dimension :: by default, maximal numner of dimesnions to be generated
37 const :: set to "const " to make a const equivalent
41/** Macro that makes it possible to call a templated method for
42 * a MDEventWorkspace using a IMDEventWorkspace_sptr as the input.
44 * @param funcname :: name of the function that will be called.
45 * @param workspace :: IMDEventWorkspace_sptr input workspace.
48#define %sCALL_MDEVENT_FUNCTION%s(funcname, workspace) \\
52 macro =
"""%s%sMDEventWorkspace<%s, %d>::sptr %s = std::dynamic_pointer_cast<%sMDEventWorkspace<%s, %d> >(workspace); \\
53if (%s) funcname<%s, %d>(%s); \\
59 suffix =
"%d" % min_dimension
62 s = macro_top % (prefix, suffix)
64 for mdevent_type
in mdevent_types:
65 for nd
in range(1, max_dimensions + 1):
66 if nd >= min_dimension:
67 eventType =
"%s<%d>" % (mdevent_type, nd)
68 varname =
"MDEW_%s_%d" % (mdevent_type.upper(), nd)
70 varname =
"CONST_" + varname
71 s += macro % (padding, const, eventType, nd, varname, const, eventType, nd, varname, eventType, nd, varname)
89 """Look line-by-line in lines[] for a line that starts with pattern. Return
90 the line number in source where the line was found, and the padding (in spaces) before it"""
91 searcher = re.compile(pattern)
92 for n
in range(startat, len(lines)):
93 found = searcher.search(lines[n])
115 """Read the file and separate it into three parts with the part between input markers to be generated and two others left unchanged.
117 @param -- file_name -- full file name to open
118 @param -- start_marker -- the marker which indicate first line of autogenerated file
119 @param -- end_marger -- the margker which indicate last line of autogenerated file
121 @return padding -- the number of spaces to insert in front of autogenerated lines
122 @return lines_before -- list of lines before autogenerated one (including start_marker)
123 @return lines_after -- list of lines after autogenerated one (including end_marker)
126 f = open(file_name,
"r")
129 lines = s.split(
"\n")
133 print(
"Lines for autogenerated code: ", n1, n2)
134 if n1
is None or n2
is None:
135 raise Exception(
"Could not find the marker in the " + file_name +
" file.")
137 lines_before = lines[: n1 + 1]
138 lines_after = lines[n2:]
141 return (padding, lines_before, lines_after)
145 print(
"Generating MDEventFactory")
148 classes_cpp = [
"MDBoxBase",
"MDBox",
"MDEventWorkspace",
"MDGridBox",
"MDBin",
"MDBoxIterator"]
151 "../inc/MantidDataObjects/MDEventFactory.h",
"//### BEGIN AUTO-GENERATED CODE",
"//### END AUTO-GENERATED CODE"
155 print(
" numDimensions to be generated: ", nDim)
157 dimensions = range(1, nDim + 1)
159 header_lines = map(
lambda x: padding + x, header.split(
"\n"))
160 footer_lines = map(
lambda x: padding + x, footer.split(
"\n"))
162 lines += header_lines
169 classes = [
"MDBox",
"MDBoxBase",
"MDGridBox",
"MDEventWorkspace",
"MDBin"]
171 lines.append(
"\n%s// ------------- Typedefs for %s ------------------\n" % (padding, c))
172 mdevent_type =
"MDLeanEvent"
173 for nd
in dimensions:
174 lines.append(
"%s/// Typedef for a %s with %d dimension%s " % (padding, c, nd, [
"",
"s"][nd > 1]))
175 lines.append(
"%s typedef %s<%s<%d>, %d> %s%dLean;" % (padding, c, mdevent_type, nd, nd, c, nd))
176 mdevent_type =
"MDEvent"
177 for nd
in dimensions:
178 lines.append(
"%s/// Typedef for a %s with %d dimension%s " % (padding, c, nd, [
"",
"s"][nd > 1]))
179 lines.append(
"%stypedef %s<%s<%d>, %d> %s%d;" % (padding, c, mdevent_type, nd, nd, c, nd))
183 lines += footer_lines + lines_after
185 f = open(
"../inc/MantidDataObjects/MDEventFactory.h",
"w")
186 f.writelines(line +
"\n" for line
in lines)
189 padding, lines, lines_after =
parse_file(
"./MDEventFactory.cpp",
"//### BEGIN AUTO-GENERATED CODE",
"//### END AUTO-GENERATED CODE")
191 header_lines = map(
lambda x: padding + x, header.split(
"\n"))
192 footer_lines = map(
lambda x: padding + x, footer.split(
"\n"))
194 lines += header_lines
197 for c
in mdevent_types:
198 lines.append(
"%s// Instantiations for %s" % (padding, c))
199 for nd
in dimensions:
200 lines.append(
"%s template class DLLExport %s<%d>;" % (padding, c, nd))
203 for c
in classes_cpp:
204 lines.append(
"%s// Instantiations for %s" % (padding, c))
205 for mdevent_type
in mdevent_types:
206 for nd
in dimensions:
207 lines.append(
"%s template class DLLExport %s<%s<%d>, %d>;" % (padding, c, mdevent_type, nd, nd))
210 lines += footer_lines + lines_after
211 f = open(
"./MDEventFactory.cpp",
"w")
212 f.writelines(line +
"\n" for line
in lines)
218 print(
"The available IDs on the templated MDEventWorkspace classes may have changed.")
219 print(
"Please update the casting IDs in PythonInterface/mantid/api/IMDEventWorkspace accordingly")