def write_destructor(self): abbrev_object = strFunctions.abbrev_name(self.class_name) # create doc string header if self.is_cpp_api: title_line = 'Destructor for {0}.'.format(self.object_name) else: title_line = 'Frees this {0} object.'.format(self.object_name) params = [] if not self.is_cpp_api: params.append('@param {0} the {1} structure.' .format(abbrev_object, self.object_name)) return_lines = [] additional = [] if self.is_cpp_api: function = '~{0}'.format(self.object_name) return_type = '' else: function = '{0}_free'.format(self.class_name) return_type = 'void' arguments = [] if not self.is_cpp_api: arguments.append('{0}* {1}'.format(self.object_name, abbrev_object)) # create the function implementation if self.is_cpp_api: implementation = [] code = [] for attrib in self.attributes: if attrib['isArray']: member = attrib['memberName'] code.append(self.create_code_block( 'if', ['{0} != NULL'.format(member), 'delete [] {0}'.format(member)])) code.append(self.create_code_block( 'line', ['{0} = NULL'.format(member)])) for i in range(0, len(self.child_elements)): element = self.child_elements[i] member = element['memberName'] implementation.append('delete {0}'.format(member)) implementation.append('{0} = NULL'.format(member)) if len(implementation) > 0: code.append(self.create_code_block('line', implementation)) else: implementation = ['{0} != NULL'.format(abbrev_object), 'delete {0}'.format(abbrev_object)] code = [self.create_code_block('if', implementation)] return dict({'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': True, 'object_name': self.object_name, 'implementation': code})
def write_clone(self): abbrev_object = strFunctions.abbrev_name(self.class_name) # create doc string header title_line = 'Creates and returns a deep copy of this {0} object.'\ .format(self.object_name) params = [] if not self.is_cpp_api: params.append('@param {0} the {1} structure.'.format( abbrev_object, self.object_name)) return_lines = [ '@return a (deep) copy of this {0} object.'.format( self.object_name) ] additional = [] if self.is_cpp_api: function = 'clone' else: function = '{0}_clone'.format(self.class_name) return_type = '{0}*'.format(self.object_name) arguments = [] if not self.is_cpp_api: arguments.append('const {0}* {1}'.format(self.object_name, abbrev_object)) additional.append('@copydetails doc_returned_owned_pointer') # create the function implementation if self.is_cpp_api: implementation = ['return new {0}(*this)'.format(self.object_name)] code_type = 'line' else: implementation = [ '{0} != NULL'.format(abbrev_object), 'return static_cast<{0}*>({1}->' 'clone())'.format(self.object_name, abbrev_object), 'else', 'return NULL' ] code_type = 'if_else' code = [self.create_code_block(code_type, implementation)] return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': True, 'virtual': True, 'object_name': self.object_name, 'implementation': code })
def __init__(self, language, is_cpp_api, is_list_of, class_object): self.language = language self.cap_language = language.upper() self.package = class_object['package'] self.class_name = class_object['name'] self.is_cpp_api = is_cpp_api self.is_list_of = is_list_of self.is_plugin = False if 'is_plugin' in class_object: self.is_plugin = class_object['is_plugin'] self.is_header = class_object['is_header'] if is_list_of: self.child_name = class_object['lo_child'] else: self.child_name = '' if is_cpp_api: self.object_name = self.class_name self.object_child_name = self.child_name else: if is_list_of: self.object_name = 'ListOf_t' else: self.object_name = self.class_name + '_t' self.object_child_name = self.child_name + '_t' self.child_lo_elements = class_object['child_lo_elements'] self.child_elements = class_object['child_elements'] self.base_class = class_object['baseClass'] if 'is_doc_plugin' in class_object and class_object['is_doc_plugin']: self.std_base = global_variables.baseClass else: self.std_base = class_object['std_base'] self.has_only_math = class_object['has_only_math'] self.num_children = class_object['num_children'] self.num_non_std_children = class_object['num_non_std_children'] # useful variables if not self.is_cpp_api and self.is_list_of: self.struct_name = self.object_child_name else: self.struct_name = self.object_name self.abbrev_parent = strFunctions.abbrev_name(self.object_name) if self.is_cpp_api is False: self.true = '@c 1' self.false = '@c 0' else: self.true = '@c true' self.false = '@c false'
def write_enum_to_string_function(self, index, values=None, str_name=''): enum = self.enums[index] name = enum['name'] abbrev_name = strFunctions.abbrev_name(name) # create comment parts title_line = '' params = [] return_lines = [] additional = [] # create the function declaration arguments = ['{0}_t {1}'.format(name, abbrev_name)] function = '{0}_toString'.format(name) return_type = 'const char*' # create the function implementation if values: implementation = [ 'int min = {0}'.format(values[0]), 'int max = {0}'.format(values[-1]) ] code = [ dict({ 'code_type': 'line', 'code': implementation }), self.create_code_block('if', [ '{0} < min || {0} > max'.format(abbrev_name), 'return \"(Unknown {0} value)\"'.format(name) ]), self.create_code_block( 'line', ['return {0}[{1} - min]'.format(str_name, abbrev_name)]) ] else: code = [] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': False, 'object_name': name, 'implementation': code })
def __init__(self, language, is_cpp_api, is_list_of, class_object): self.language = language self.class_name = class_object['name'] self.is_cpp_api = is_cpp_api self.is_list_of = is_list_of if is_list_of: self.child_name = class_object['lo_child'] else: self.child_name = '' if is_cpp_api: self.object_name = self.class_name self.object_child_name = self.child_name else: if is_list_of: self.object_name = 'ListOf_t' else: self.object_name = self.class_name + '_t' self.object_child_name = self.child_name + '_t' self.concretes = class_object['concretes'] # useful variables if not self.is_cpp_api and self.is_list_of: self.struct_name = self.object_child_name else: self.struct_name = self.object_name self.plural = strFunctions.plural(self.child_name) self.indef_name = strFunctions.get_indefinite(self.object_child_name) self.abbrev_parent = strFunctions.abbrev_name(self.object_name) self.abbrev_child = strFunctions.abbrev_name(self.child_name) if self.is_cpp_api is False: self.true = '@c 1' self.false = '@c 0' else: self.true = '@c true' self.false = '@c false'
def write_is_valid_enum_function(self, index, values=None): enum = self.enums[index] name = enum['name'] abbrev_name = strFunctions.abbrev_name(name) # create comment parts title_line = '' params = [] return_lines = [] additional = [] # create the function declaration arguments = ['{0}_t {1}'.format(name, abbrev_name)] function = '{0}_isValid'.format(name) return_type = 'int' # create the function implementation if values: implementation = [ 'int min = {0}'.format(values[0]), 'int max = {0}'.format(values[-1]) ] code = [ dict({ 'code_type': 'line', 'code': implementation }), self.create_code_block('if_else', [ '{0} < min || {0} >= max'.format(abbrev_name), 'return 0', 'else', 'return 1' ]) ] else: code = [] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': False, 'object_name': name, 'implementation': code })
def write_code_for_children(self, parent, child, code): abbrev = strFunctions.abbrev_name(child['name']) if child['name'] == 'math': code.append(self.create_code_block('line', ['ASTNode* math = SBML_parse' 'L3Formula(\"TBC\")', '{0}->setMath(math)' ''.format(parent)])) else: code.append(self.create_code_block('line', ['{0}* {1} = {2}->create{0}()' ''.format(child['name'], abbrev, parent)])) if 'attribs' in child: self.write_code_for_reqd_attributes(abbrev, child, code) for i in range(0, len(child['children'])): self.write_code_for_children(abbrev, child['children'][i], code)
def __init__(self, language, is_cpp_api, is_list_of, class_object): self.language = language self.cap_language = language.upper() self.package = class_object['package'] self.class_name = class_object['name'] self.is_cpp_api = is_cpp_api self.is_list_of = is_list_of self.is_plugin = False if 'is_plugin' in class_object: self.is_plugin = class_object['is_plugin'] self.is_header = class_object['is_header'] if is_list_of: self.child_name = class_object['lo_child'] else: self.child_name = '' if is_cpp_api: self.object_name = self.class_name self.object_child_name = self.child_name else: if is_list_of: self.object_name = 'ListOf_t' else: self.object_name = self.class_name + '_t' self.object_child_name = self.child_name + '_t' self.child_lo_elements = class_object['child_lo_elements'] self.child_elements = class_object['child_elements'] self.base_class = class_object['baseClass'] self.std_base = class_object['std_base'] self.has_only_math = class_object['has_only_math'] self.num_children = class_object['num_children'] self.num_non_std_children = class_object['num_non_std_children'] # useful variables if not self.is_cpp_api and self.is_list_of: self.struct_name = self.object_child_name else: self.struct_name = self.object_name self.abbrev_parent = strFunctions.abbrev_name(self.object_name) if self.is_cpp_api is False: self.true = '@c 1' self.false = '@c 0' else: self.true = '@c true' self.false = '@c false'
def write_clone(self): abbrev_object = strFunctions.abbrev_name(self.class_name) # create doc string header title_line = 'Creates and returns a deep copy of this {0} object.'\ .format(self.object_name) params = [] if not self.is_cpp_api: params.append('@param {0} the {1} structure.' .format(abbrev_object, self.object_name)) return_lines = ['@return a (deep) copy of this {0} object.'.format( self.object_name)] additional = [] if self.is_cpp_api: function = 'clone' else: function = '{0}_clone'.format(self.class_name) return_type = '{0}*'.format(self.object_name) arguments = [] if not self.is_cpp_api: arguments.append('const {0}* {1}'.format(self.object_name, abbrev_object)) # create the function implementation if self.is_cpp_api: implementation = ['return new {0}(*this)'.format(self.object_name)] code_type = 'line' else: implementation = ['{0} != NULL'.format(abbrev_object), 'return static_cast<{0}*>({1}->' 'clone())'.format(self.object_name, abbrev_object), 'else', 'return NULL'] code_type = 'if_else' code = [self.create_code_block(code_type, implementation)] return dict({'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': True, 'virtual': True, 'object_name': self.object_name, 'implementation': code})
def write_code_for_plugin(self, i, code): parent = self.object_tree[i]['base'] parent_name = strFunctions.lower_first(parent) abbrev = strFunctions.abbrev_name(parent) plugin_class = '{0}{1}Plugin'.format(self.package_up, parent) plugin_name = '{0}plugin'.format(abbrev) code.append(self.create_code_block('line', ['{0}* {1} = document->create{0}()' ''.format(parent, parent_name)])) code.append(self.create_code_block('line', ['{0}* {1} = static_cast<{0}*>' '({2}->getPlugin(\"{3}\"))' ''.format(plugin_class, plugin_name, parent_name, self.package)])) for j in range(0, len(self.object_tree[i]['children'])): self.write_code_for_children(plugin_name, self.object_tree[i]['children'][j], code)
def write_enum_to_string_function(self, index, values=None, str_name=''): enum = self.enums[index] name = enum['name'] abbrev_name = strFunctions.abbrev_name(name) # create comment parts title_line = '' params = [] return_lines = [] additional = [] # create the function declaration arguments = ['{0}_t {1}'.format(name, abbrev_name)] function = '{0}_toString'.format(name) return_type = 'const char*' # create the function implementation if values: implementation = ['int min = {0}'.format(values[0]), 'int max = {0}'.format(values[-1])] code = [dict({'code_type': 'line', 'code': implementation}), self.create_code_block('if', [ '{0} < min || {0} > max'.format(abbrev_name), 'return \"(Unknown {0} value)\"'.format(name)]), self.create_code_block('line', [ 'return {0}[{1} - min]'.format(str_name, abbrev_name)])] else: code = [] # return the parts return dict({'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': False, 'object_name': name, 'implementation': code})
def write_is_valid_enum_function(self, index, values=None): enum = self.enums[index] name = enum['name'] abbrev_name = strFunctions.abbrev_name(name) # create comment parts title_line = '' params = [] return_lines = [] additional = [] # create the function declaration arguments = ['{0}_t {1}'.format(name, abbrev_name)] function = '{0}_isValid'.format(name) return_type = 'int' # create the function implementation if values: implementation = ['int min = {0}'.format(values[0]), 'int max = {0}'.format(values[-1])] code = [dict({'code_type': 'line', 'code': implementation}), self.create_code_block ('if_else', ['{0} < min || {0} >= max'.format(abbrev_name), 'return 0', 'else', 'return 1'])] else: code = [] # return the parts return dict({'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': False, 'object_name': name, 'implementation': code})
def write_is_valid_enum_function(self, index, values=None): enum = self.enums[index] name = enum['name'] classname = self.get_class_name(enum['values'][0]['name']) abbrev_name = strFunctions.abbrev_name(name) # create comment parts title_line = 'Predicate returning @c 1 (true) or @c 0 (false) depending on whether the given #{0}_t is valid.'.format( name) params = [ '@param {0} the #{1}_t enumeration to query.'.format( abbrev_name, name) ] return_lines = ['@return @c 1 (true) if the #{0}_t is'.format(name)] num_vals = len(values) last = num_vals - 1 if num_vals > 0 else 0 penultimate = last - 1 if last > 0 else 0 next_pen = penultimate - 1 if penultimate > 0 else 0 for i in range(0, num_vals - 1): if i == penultimate: return_lines.append('@sbmlconstant{0}{1}, {2}_t{3};'.format( self.open_br, values[i], name, self.close_br)) elif i == next_pen: return_lines.append('@sbmlconstant{0}{1}, {2}_t{3}, or'.format( self.open_br, values[i], name, self.close_br)) else: return_lines.append('@sbmlconstant{0}{1}, {2}_t{3},'.format( self.open_br, values[i], name, self.close_br)) return_lines.append( '@c 0 (false) otherwise (including @sbmlconstant{0}{1}, {2}_t{3}).' .format(self.open_br, values[-1], name, self.close_br)) additional = [ '@if conly', '@memberof {0}_t'.format(classname), '@endif' ] # create the function declaration arguments = ['{0}_t {1}'.format(name, abbrev_name)] function = '{0}_isValid'.format(name) return_type = 'int' # create the function implementation if values: implementation = [ 'int min = {0}'.format(values[0]), 'int max = {0}'.format(values[-1]) ] code = [ dict({ 'code_type': 'line', 'code': implementation }), self.create_code_block('if_else', [ '{0} < min || {0} >= max'.format(abbrev_name), 'return 0', 'else', 'return 1' ]) ] else: code = [] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': False, 'object_name': name, 'implementation': code })
def __init__(self, language, is_java_api, is_list_of, class_object, jsbml_data_tree=None, jsbml_methods=None): self.language = language self.cap_language = language.upper() self.package = class_object['package'] self.class_name = class_object['name'] self.is_java_api = is_java_api self.is_list_of = is_list_of if is_list_of: self.child_name = class_object['lo_child'] else: self.child_name = '' if is_java_api: self.object_name = self.class_name self.object_child_name = self.child_name else: if is_list_of: self.object_name = 'ListOf' else: self.object_name = self.class_name self.object_child_name = self.child_name # class_attributes not suitable # self.attributes = class_object['class_attributes'] self.attributes = class_object['attribs'] self.child_elements = class_object['child_elements'] if 'num_versions' in class_object and class_object['num_versions'] > 1: self.has_multiple_versions = True else: self.has_multiple_versions = False self.document = False if 'document' in class_object: self.document = class_object['document'] # useful variables if not self.is_java_api and self.is_list_of: self.struct_name = self.object_child_name else: self.struct_name = self.object_name if self.is_java_api is False: self.true = '@c 1' self.false = '@c 0' else: self.true = '{@code true}' # For comments self.false = '{@code false}' self.plural = strFunctions.plural(self.child_name) self.indef_name = strFunctions.get_indefinite(self.object_child_name) self.abbrev_parent = strFunctions.abbrev_name(self.object_name) self.abbrev_child = strFunctions.abbrev_name(self.child_name) self.is_header = True if 'is_header' in class_object: self.is_header = class_object['is_header'] self.is_plugin = False if 'is_plugin' in class_object: self.is_plugin = class_object['is_plugin'] self.open_br = '{' self.close_br = '}' self.success = global_variables.ret_success self.failed = global_variables.ret_failed self.invalid_att = global_variables.ret_invalid_att self.invalid_obj = global_variables.ret_invalid_obj # JSBML unique helper data if jsbml_data_tree is not None: self.jsbml_data_tree = jsbml_data_tree if jsbml_methods is not None: self.jsbml_methods = jsbml_methods # Here it will hold mandatory functions informations self.mandatory_data = {} # The order the mandatory function have to be written self.write_order = self.determine_mandatory_methods()
def write_enum_to_string_function(self, index, values=None, str_name=''): enum = self.enums[index] name = enum['name'] abbrev_name = strFunctions.abbrev_name(name) classname = self.get_class_name(enum['values'][0]['name']) # create comment parts title_line = 'Returns the string version of the provided #{0}_t enumeration.'.format( name) params = [ '@param {0} the #{1}_t enumeration value to convert.'.format( abbrev_name, name) ] return_lines = ['@return A string corresponding to the given type:'] for enumVal in enum['values']: return_lines.append('\"{0}\",'.format(enumVal['value'])) return_lines.append( 'or @c NULL if the value is @sbmlconstant{0}{1}, {2}_t{3} or ' 'another invalid enumeration value.'.format( self.open_br, values[-1], name, self.close_br)) additional = [ '@copydetails doc_returned_unowned_char', ' ', '@if conly', '@memberof {0}_t'.format(classname), '@endif' ] # create the function declaration arguments = ['{0}_t {1}'.format(name, abbrev_name)] function = '{0}_toString'.format(name) return_type = 'const char*' # create the function implementation if values: implementation = [ 'int min = {0}'.format(values[0]), 'int max = {0}'.format(values[-1]) ] code = [ dict({ 'code_type': 'line', 'code': implementation }), self.create_code_block('if', [ '{0} < min || {0} > max'.format(abbrev_name), 'return \"(Unknown {0} value)\"'.format(name) ]), self.create_code_block( 'line', ['return {0}[{1} - min]'.format(str_name, abbrev_name)]) ] else: code = [] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': False, 'object_name': name, 'implementation': code })
def write_destructor(self): abbrev_object = strFunctions.abbrev_name(self.class_name) # create doc string header if self.is_cpp_api: title_line = 'Destructor for {0}.'.format(self.object_name) else: title_line = 'Frees this {0} object.'.format(self.object_name) params = [] if not self.is_cpp_api: params.append('@param {0} the {1} structure.'.format( abbrev_object, self.object_name)) return_lines = [] additional = [] if self.is_cpp_api: function = '~{0}'.format(self.object_name) return_type = '' else: function = '{0}_free'.format(self.class_name) return_type = 'void' arguments = [] if not self.is_cpp_api: arguments.append('{0}* {1}'.format(self.object_name, abbrev_object)) # create the function implementation if self.is_cpp_api: implementation = [] code = [] for attrib in self.attributes: if attrib['isArray']: member = attrib['memberName'] code.append( self.create_code_block('if', [ '{0} != NULL'.format(member), 'delete [] {0}'.format(member) ])) code.append( self.create_code_block('line', ['{0} = NULL'.format(member)])) for i in range(0, len(self.child_elements)): element = self.child_elements[i] member = element['memberName'] implementation.append('delete {0}'.format(member)) implementation.append('{0} = NULL'.format(member)) if len(implementation) > 0: code.append(self.create_code_block('line', implementation)) else: implementation = [ '{0} != NULL'.format(abbrev_object), 'delete {0}'.format(abbrev_object) ] code = [self.create_code_block('if', implementation)] return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': True, 'object_name': self.object_name, 'implementation': code })
def __init__(self, language, is_cpp_api, is_list_of, class_object, writing_test=False): self.is_cpp_api = is_cpp_api self.is_list_of = is_list_of if class_object['name'].startswith('SBML'): self.class_name = class_object['name'][4:] else: self.class_name = class_object['name'] self.base_class = class_object['baseClass'] if not writing_test: self.language = language self.cap_language = language.upper() self.package = class_object['package'] if is_list_of: self.child_name = class_object['lo_child'] else: self.child_name = '' if is_cpp_api: self.object_name = self.class_name self.object_child_name = self.child_name else: if is_list_of: self.object_name = 'ListOf_t' else: self.object_name = self.class_name + '_t' self.object_child_name = self.child_name + '_t' self.attributes = class_object['class_attributes'] self.classroot = None if 'root' in class_object: self.classroot = class_object['root'] self.elements = query.get_child_elements( class_object['child_elements'], class_object['child_lo_elements'], self.classroot) self.single_elements = query.get_child_elements( class_object['child_elements'], [], self.classroot) self.lo_elements = query.get_child_elements( [], class_object['child_lo_elements'], self.classroot) if 'num_versions' in class_object and class_object[ 'num_versions'] > 1: self.has_multiple_versions = True else: self.has_multiple_versions = False self.document = False if 'document' in class_object: self.document = class_object['document'] # useful variables if not self.is_cpp_api and self.is_list_of: self.struct_name = self.object_child_name else: self.struct_name = self.object_name if self.is_cpp_api is False: self.true = '@c 1' self.false = '@c 0' else: self.true = '@c true' self.false = '@c false' self.plural = strFunctions.plural(self.child_name) self.indef_name = strFunctions.get_indefinite( self.object_child_name) self.abbrev_parent = strFunctions.abbrev_name(self.object_name) self.abbrev_child = strFunctions.abbrev_name(self.child_name) self.is_header = True if 'is_header' in class_object: self.is_header = class_object['is_header'] self.is_plugin = False if 'is_plugin' in class_object: self.is_plugin = class_object['is_plugin'] self.open_br = '{' self.close_br = '}' self.success = global_variables.ret_success self.failed = global_variables.ret_failed self.invalid_att = global_variables.ret_invalid_att self.invalid_obj = global_variables.ret_invalid_obj self.tests = []