Ejemplo n.º 1
0
    def GenerateJsonParser(self, JsonDecoderDict, file):
        def getMetaParentName(meta):
            if meta.getParent() == 'all':
                return meta.getCppName() + 'Type'
            return meta.getParent().getCppName()

        meta = self._meta
        fnname = '%s_ParseJsonMetadata' % meta.getCppName()
        file.write('bool %s(\n' % fnname)
        file.write('        const contrail_rapidjson::Value &parent,\n')
        file.write('        std::auto_ptr<AutogenProperty > *resultp) {\n')

        if meta.getCType():
            file.write(
                '    return autogen::%s::JsonParseProperty(parent, resultp);\n'
                % meta.getCTypename())
        else:
            indent = ' ' * 4
            info = meta.getMemberInfo()

            cdecl = "    %s::%s *data =\n" % (getMetaParentName(meta),
                                              SimpleTypeWrapper(info))
            file.write(cdecl)
            cdecl = "        new %s::%s();\n" % (getMetaParentName(meta),
                                                 SimpleTypeWrapper(info))
            file.write(cdecl)
            file.write("    resultp->reset(data);\n")
            file.write("    // TODO: get default value from schema\n")
            file.write("    if (parent.IsNull()) return true;\n")

            if info.ctypename == 'std::string':
                file.write(
                    indent +
                    'if (!autogen::ParseString(parent, &data->data)) return false;\n'
                )
            elif info.ctypename == 'int':
                file.write(
                    indent +
                    'if (!autogen::ParseInteger(parent, &data->data)) return false;\n'
                )
            elif info.ctypename == 'bool':
                file.write(
                    indent +
                    'if (!autogen::ParseBoolean(parent, &data->data)) return false;\n'
                )
            file.write(indent + 'return true;\n')

        file.write('}\n\n')
        JsonDecoderDict[meta.getJsonName()] = fnname
    def _GenTypedefs(self, file):
        gen_types = {}
        for ctype in self._identifier.getDataTypes():
            if ctype in gen_types:
                continue
            file.write('    typedef autogen::%s %s;\n' % (ctype, ctype))
            gen_types[ctype] = ctype

        properties = self._identifier.getProperties()
        for prop in properties:
            info = prop.getMemberInfo()
            assert info
            if not info.isComplex:
                if info.ctypename in gen_types:
                    continue
                cdecl = """
    struct %(typename)s : public AutogenProperty {
        %(ctype)s data;
    };
""" % {
                    'typename': SimpleTypeWrapper(info),
                    'ctype': info.ctypename
                }
                file.write(cdecl)
                gen_types[info.ctypename] = info.ctypename

        if len(properties) > 0:
            file.write('    enum PropertyId {\n')
        for prop in properties:
            file.write('        %s,\n' % prop.getPropertyId())
        if len(properties) > 0:
            file.write('        PROPERTY_ID_COUNT\n    };\n')
Ejemplo n.º 3
0
    def GenerateParser(self, DecoderDict, file):
        def getMetaParentName(meta):
            if meta.getParent() == 'all':
                return meta.getCppName() + 'Type'
            return meta.getParent().getCppName()

        meta = self._meta
        fnname = '%s_ParseMetadata' % meta.getCppName()
	decl = """
bool %s(const pugi::xml_node &parent, std::auto_ptr<AutogenProperty > *resultp) {
""" % fnname
	file.write(decl)

	if meta.getCType():
	    file.write('    return autogen::%s::XmlParseProperty(parent, resultp);\n' %
		       meta.getCTypename())
	else:
            indent = ' ' * 4
            info = meta.getMemberInfo()
            cdecl = """
    %(class)s::%(wtype)s *data = new %(class)s::%(wtype)s();
    resultp->reset(data);
""" % {'class': getMetaParentName(meta), 'wtype': SimpleTypeWrapper(info)}
            file.write(cdecl)
            if info.ctypename == 'std::string':
                file.write(indent + 'data->data = parent.child_value();\n')
            elif info.ctypename == 'int':
                file.write(indent +
                           'data->data = atoi(parent.child_value());\n')
            elif info.ctypename == 'bool':
                file.write(indent + 'data->data = parent.text().as_bool();\n');
            file.write(indent + 'return true;\n')

	file.write('}\n')
        DecoderDict[meta.getName()] = fnname
    def _GenSetProperty(self, file):
        header = """
bool %s::SetProperty(const string &property, AutogenProperty *data) {
""" % self.getName()
        file.write(header)
        test_else = ''
        for prop in self._identifier.getProperties():
            membername = prop.getPropertyName() + '_'
            file.write('    %sif (property == "%s") {\n' %
                       (test_else, prop.getName()))
            test_else = 'else '
            # TODO: compare with previous value and return false if unchanged
            info = prop.getMemberInfo()
            assert info
            indent = ' ' * 8
            if info.isSequence:
                file.write(
                    indent +
                    'autogen::%(type)s *v = static_cast<autogen::%(type)s *>(data);\n'
                    % {'type': prop._xelement.getType()})
                cinfo = self._TypeDict[prop._xelement.getType()]
                file.write(indent + '%s.swap(v->%s);\n' %
                           (membername, cinfo._data_members[0].membername))
            elif info.isComplex:
                file.write(indent +
                           '%s.Copy(*static_cast<const %s *>(data));\n' %
                           (membername, info.ctypename))
            else:
                file.write(indent +
                           '%s = static_cast<const %s *>(data)->data;\n' %
                           (membername, SimpleTypeWrapper(info)))

            cdecl = """
        property_set_.set(%s);
    }
""" % prop.getPropertyId()
            file.write(cdecl)

        retval = """
    if (property_set_.is_subset_of(old_property_set_)) {
        return false;
    } else {
        return true;
    }
}
"""
        file.write(retval)
Ejemplo n.º 5
0
    def _GenerateSimpleProperty(self, file, prop):
        name = prop.getCppName() + 'Type'
        if name in self._generated_props:
            return
        self._generated_props[name] = name
        info = prop.getMemberInfo()
        cdecl = """
class %(class)s {
  public:
    struct %(typename)s : public AutogenProperty {
        %(ctype)s data;
    };
};
""" % {'class': name,
       'typename': SimpleTypeWrapper(info),
       'ctype': info.ctypename}
        file.write(cdecl)