Esempio n. 1
0
    def _write_class_hpp(self, class_):
        out = Writer.write_class(self, class_, FLAG_HPP)
        self._current_class = class_
        behaviors = list()
        for c in class_.behaviors:
            behaviors.append('public ' + c.name)
        behaviors = ', '.join(behaviors)
        objects = self.write_objects(class_.members, FLAG_HPP)
        functions = self.write_functions(class_.functions, FLAG_HPP)
        constructor = _create_constructor_function_hpp(class_)
        destructor = _create_destructor_function_hpp(class_)
        includes, forward_declarations, forward_declarations_out = self._find_includes(
            class_, FLAG_HPP)

        includes = list(set(includes.split('\n')))
        includes.sort()
        includes = '\n'.join(includes)
        forward_declarations = list(set(forward_declarations.split('\n')))
        forward_declarations.sort()
        forward_declarations = '\n'.join(forward_declarations)

        self._current_class = None

        pattern = ''
        if len(class_.behaviors) > 0:
            pattern += '{0} {1} : {2}'
        else:
            pattern += '{0} {1}'
        if functions[FLAG_HPP].strip() == '':
            f = ''
        else:
            f = '\n{4}'
        if objects[FLAG_HPP].strip() == '':
            o = ''
        else:
            o = '{3}'

        if class_.type != 'enum':
            pattern += '\n__begin__\npublic:\n{5}{6}' + f + o + '__end__;\n\n'
        else:
            pattern += '\n__begin__{5}' + o + 'public:' + f + '__end__;\n\n'

        pattern = '{3}\nnamespace {0}\n__begin__{2}\n\n{1}__end__//namespace {0}'.\
            format(_get_namespace(), pattern, forward_declarations, forward_declarations_out)
        pattern = '#ifndef __mg_{0}_h__\n#define __mg_{0}_h__\n{2}\n\n{1}\n\n#endif //#ifndef __{0}_h__'.\
            format(class_.name, pattern, includes)

        out[FLAG_HPP] += pattern.format('class', class_.name, behaviors,
                                        objects[FLAG_HPP], functions[FLAG_HPP],
                                        constructor, destructor)
        out[FLAG_HPP] = re.sub('__begin__', '{', out[FLAG_HPP])
        out[FLAG_HPP] = re.sub('__end__', '}', out[FLAG_HPP])
        return out
Esempio n. 2
0
    def _write_class_cpp(self, class_):
        out = Writer.write_class(self, class_, FLAG_CPP)
        self._current_class = class_
        objects = self.write_objects(class_.members, FLAG_CPP)
        functions = self.write_functions(class_.functions, FLAG_CPP)
        constructor = _create_constructor_function_cpp(self.parser, class_)
        destructor = _create_destructor_function_cpp(class_)
        includes, f, f_out = self._find_includes(class_, FLAG_CPP)
        includes = self._find_includes_in_function_operation(class_, includes)

        includes = list(set(includes.split('\n')))
        includes.sort()
        includes = '\n'.join(includes)

        self._current_class = None
        if class_.type == 'class':
            pattern = '''#include "{0}.h"
                         # include "Generics.h"{4}

                         namespace {3}
                         __begin__{5}{6}
                         {2}
                         {7}
                         {1}__end__'''
        else:
            pattern = '''#include "{0}.h"
                         # include "Generics.h"{4}

                         namespace {3}
                         __begin__
                         {5}
                         {2}{7}
                         {1}__end__'''
        registration = 'REGISTRATION_OBJECT({0});\n'.format(class_.name)
        has_get_type = False
        if class_.is_abstract:
            registration = ''
        for func in class_.functions:
            if func.name == constants.CLASS_FUNCTION_GET_TYPE:
                has_get_type = True
        if not has_get_type:
            registration = ''

        if not class_.is_serialized:
            registration = ''
        if class_.is_abstract:
            registration = ''

        if not registration and \
                not constructor and \
                not destructor and \
                not objects[FLAG_CPP] and \
                not functions[FLAG_CPP]:
            return out

        out[FLAG_CPP] += pattern.format(class_.name,
                                        functions[FLAG_CPP], constructor,
                                        _get_namespace(), includes,
                                        objects[FLAG_CPP], registration,
                                        destructor)
        out[FLAG_CPP] = re.sub('__begin__', '{', out[FLAG_CPP])
        out[FLAG_CPP] = re.sub('__end__', '}', out[FLAG_CPP])
        return out