Go to the documentation of this file.00001 """
00002 Created on Nov 22, 2014
00003
00004 Software License Agreement (BSD License)
00005 Copyright (c) 2014, LABUST, UNIZG-FER
00006 All rights reserved.
00007
00008 Redistribution and use in source and binary forms, with or without
00009 modification, are permitted provided that the following conditions
00010 are met:
00011
00012 * Redistributions of source code must retain the above copyright
00013 notice, this list of conditions and the following disclaimer.
00014 * Redistributions in binary form must reproduce the above
00015 copyright notice, this list of conditions and the following
00016 disclaimer in the documentation and/or other materials provided
00017 with the distribution.
00018 * Neither the name of the LABUST nor the names of its
00019 contributors may be used to endorse or promote products derived
00020 from this software without specific prior written permission.
00021
00022 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 POSSIBILITY OF SUCH DAMAGE.
00034
00035 @author: Dula Nad
00036 """
00037 from .variable import Variable
00038
00039 class Function:
00040 """
00041 Represents the C++ function in the object form.
00042
00043 Class attributes:
00044 _XMLTAG -- constant string with the xml tag description
00045 """
00046 _XMLTAG = 'func'
00047
00048 def __init__(self, xmlnode = None):
00049 """
00050 Populates the variable from a XML node info.
00051 When the XML node is not defined, does nothing.
00052
00053 xmlnode -- the XML node with the member definition
00054
00055 Instance attributes:
00056 fname -- name of the function
00057 fargs -- the argument list
00058 fret -- the return type
00059 fbody -- function body
00060 finline -- flag i the function is to be in-line
00061 fqual -- the qualifer of the functon
00062 """
00063 self.fname = 'unnamed'
00064 self.fret = 'void'
00065 self.fargs = []
00066 self.fbody = ''
00067 self.finline = False
00068 self.fqual = None
00069
00070 if xmlnode != None: self.from_xml(xmlnode)
00071
00072 def from_xml(self, xmlnode):
00073 """
00074 Extracts the variable attributes from the XML node.
00075
00076 xmlnode -- the XML node with the member definition
00077 """
00078
00079 if xmlnode.tag != self._XMLTAG:
00080 raise NameError(self.__class__.__name__ +
00081 ' expected XML tag: "' +
00082 self._XMLTAG +
00083 '"')
00084
00085 self.fname = xmlnode.get('name',self.fname)
00086 self.fret = xmlnode.get('return',self.fret)
00087 self.finline = xmlnode.get('inline',self.finline)
00088 self.fqual = xmlnode.get('qualifier',self.fqual)
00089
00090 for node in xmlnode.findall('var'):
00091 self.fargs.append(Variable(node))
00092
00093
00094
00095 def gen_code(self, bindent = '', indent = ' '):
00096 """
00097 Generates the full member definition C++ code and
00098 returns it as a string
00099
00100 bindent -- the current indent in the document
00101 indent -- the usual minimum indentation
00102 """
00103 code = bindent + indent + self.fret + ' '
00104 code = code + self.fname + '('
00105
00106 for arg in self.fargs:
00107 code = code + arg.vtype + ' ' + arg.vname + ','
00108
00109 if len(self.fargs): code = code[:-1]
00110 code = code + ')'
00111 if self.fqual != None:
00112 code = code + ' ' + self.fqual
00113
00114 if self.finline:
00115 code = code + '\n{\n' + bindent + indent + self.fbody + '\n}'
00116
00117 return code + ";"
00118
00119 def gen_impl(self, namespace = None, bindent = '', indent = ' '):
00120 """
00121 Generates the full member definition C++ code and
00122 returns it as a string
00123
00124 bindent -- the current indent in the document
00125 indent -- the usual minimum indentation
00126 """
00127 if self.finline: return ''
00128
00129 code = bindent + self.fret + ' '
00130 if namespace != None:
00131 code = code + namespace + '::'
00132
00133 code = code + self.fname + '('
00134
00135 for arg in self.fargs:
00136 code = code + arg.vtype + ' ' + arg.vname + ','
00137
00138 if len(self.fargs): code = code[:-1]
00139
00140 code = code + ')'
00141 if self.fqual != None:
00142 code = code + ' ' + self.fqual
00143
00144 return code + '\n{\n' + indent + self.fbody + '\n}\n'
00145
00146 def __str__(self):
00147 """
00148 Overriden string specifier returns the C++ code snippet
00149 without indentation.
00150 """
00151 return self.gen_code(indent = '')
00152
00153
00154