示例#1
0
    def get_csharp_constants(self):
        template = """
		/// <summary>
		/// </summary>
		public const {0} {1}_{2} = {3};
"""
        constants = []

        for constant_group in self.get_constant_groups():
            if constant_group.is_virtual():
                continue

            for constant in constant_group.get_constants():
                if constant_group.get_type() == 'char':
                    value = "'{0}'".format(constant.get_value())
                elif constant_group.get_type() == 'bool':
                    value = str(constant.get_value()).lower()
                else:
                    value = str(constant.get_value())

                constants.append(
                    template.format(
                        csharp_common.get_csharp_type(
                            constant_group.get_type(), 1),
                        constant_group.get_name().upper,
                        constant.get_name().upper, value))
        return '\n' + ''.join(constants)
    def get_csharp_source(self):
        type_ = self.get_type()

        def helper(value):
            if type_ == 'float':
                return common.format_float(value) + 'f'
            elif type_ == 'bool':
                return str(bool(value)).lower()
            elif type_ == 'char':
                return "'{0}'".format(value.replace("'", "\\'"))
            elif type_ == 'string':
                return '"{0}"'.format(value.replace('"', '\\"'))
            elif ':bitmask:' in type_:
                return common.make_c_like_bitmask(value)
            elif type_.endswith(':constant'):
                return self.get_value_constant(value).get_csharp_source()
            else:
                return str(value)

        value = self.get_value()

        if isinstance(value, list):
            return 'new {0}[]{{{1}}}'.format(
                csharp_common.get_csharp_type(self.get_type().split(':')[0],
                                              1),
                ', '.join([helper(item) for item in value]))

        return helper(value)
    def get_csharp_source(self):
        templateA = '{type_} {name}'
        templateB = '{type_}[] {name}'

        if self.get_cardinality() == 1:
            template = templateA
        else:
            template = templateB

        return template.format(type_=csharp_common.get_csharp_type(self.get_type().split(':')[0], 1),
                               name=self.get_name().headless)
    def get_csharp_variable_declaration(self):
        name = self.get_name().headless

        if name == self.get_device().get_initial_name():
            name += '_'

        type_ = csharp_common.get_csharp_type(self.get_type().split(':')[0], 1)

        if self.get_cardinality() > 1 and type_ != 'string':
            type_ += '[]'

        return type_, name