-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtype_classgen.py
217 lines (187 loc) · 7.8 KB
/
type_classgen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
from ifmap_global import GetModuleName
class TypeClassGenerator(object):
def __init__(self, cTypeDict):
self._cTypeDict = cTypeDict
self._generated_types = { }
def _GenerateTypeSub(self, file, ctype):
depend_types = ctype.getDependentTypes()
for child in depend_types:
if not child.getName() in self._generated_types:
self._generated_types[child.getName()] = child
self._GenerateTypeSub(file, child)
file.write('\nstruct %s : public AutogenProperty {\n' % ctype.getName())
file.write(' %s();\n' % ctype.getName())
file.write(' virtual ~%s();\n' % ctype.getName())
members = ctype.getDataMembers()
if len(members) == 1 and members[0].isSequence:
member = members[0]
cdecl = """
typedef %(vectype)s::const_iterator const_iterator;
typedef %(vectype)s::size_type size_type;
const_iterator begin() const { return %(membername)s.begin(); }
const_iterator end() const { return %(membername)s.end(); }
size_type size() const { return %(membername)s.size(); }
bool empty() const { return %(membername)s.empty(); }
""" % {'vectype': member.ctypename, 'membername': member.membername}
file.write(cdecl)
for member in ctype.getDataMembers():
file.write(' %s %s;\n' % (member.ctypename, member.membername))
tail = """
void Clear();
void Copy(const %s &rhs);
bool XmlParse(const pugi::xml_node &node);
static bool XmlParseProperty(const pugi::xml_node &node,
std::auto_ptr<AutogenProperty> *resultp);
void Encode(pugi::xml_node *node) const;
void CalculateCrc(boost::crc_32_type *crc) const;
bool JsonParse(const contrail_rapidjson::Value &node);
static bool JsonParseProperty(const contrail_rapidjson::Value &node,
std::auto_ptr<AutogenProperty> *resultp);
};
""" % (ctype.getName())
file.write(tail)
def GenerateType(self, file, ctype):
if not ctype.getName() in self._generated_types:
self._generated_types[ctype.getName()] = ctype
self._GenerateTypeSub(file, ctype)
def Generate(self, file, ctype):
module_name = GetModuleName(file, '_types.h')
header = """
// autogenerated file --- DO NOT EDIT ---
#ifndef __SCHEMA__%(modname)s_TYPES_H__
#define __SCHEMA__%(modname)s_TYPES_H__
#include <iostream>
#include <string.h>
#include <vector>
#include <boost/dynamic_bitset.hpp>
#include <boost/crc.hpp> // for boost::crc_32_type
namespace pugi {
class xml_node;
class xml_document;
} // namespace pugi
#include "rapidjson/document.h"
#include "ifmap/autogen.h"
namespace autogen {
""" % {'modname': module_name.upper()}
file.write(header)
self.GenerateType(file, ctype)
file.write('} // namespace autogen\n')
file.write('#endif // __SCHEMA__%s_TYPES_H__\n' %
module_name.upper())
pass
class TypeImplGenerator(object):
def __init__(self, cTypeDict):
self._cTypeDict = cTypeDict
pass
def GenerateType(self, file, ctype):
construct = """
%(class)s::%(class)s() {
Clear();
}
""" % {'class': ctype.getName()}
file.write(construct)
destruct = """
%(class)s::~%(class)s() {
}
""" % {'class': ctype.getName()}
file.write(destruct)
cleardef = """
void %s::Clear() {
""" % ctype.getName()
file.write(cleardef)
for member in ctype.getDataMembers():
cpptype = member.ctypename
if (cpptype == 'int' or
cpptype == 'uint64_t' or
cpptype == 'time_t'):
file.write(' %s = %s;\n' % (member.membername, member.default or '0'))
elif cpptype == 'bool':
file.write(' %s = %s;\n' % (member.membername, member.default or 'false'))
elif member.isComplex and not member.isSequence:
file.write(' %s.Clear();\n' % member.membername)
elif member.default:
file.write(' %s = "%s";\n' % (member.membername, member.default))
else:
file.write(' %s.clear();\n' % member.membername)
file.write('};\n')
copydef = """
void %s::Copy(const %s &rhs) {
""" % (ctype.getName(), ctype.getName())
file.write(copydef)
for member in ctype.getDataMembers():
cpptype = member.ctypename
if member.isComplex and not member.isSequence:
fmt = ' %s.Copy(rhs.%s);\n'
else:
fmt = ' %s = rhs.%s;\n'
file.write(fmt % (member.membername, member.membername))
file.write('};\n')
crcdef = """
void %s::CalculateCrc(boost::crc_32_type *crc) const {
""" % ctype.getName()
file.write(crcdef)
indent_l1 = ' ' * 4
indent_l11 = ' ' * 9
indent_l2 = ' ' * 8
for member in ctype.getDataMembers():
cpptype = member.ctypename
if member.isSequence:
file.write(indent_l1 + 'for (%s::const_iterator iter = \n' %(cpptype))
file.write(indent_l11 + '%s.begin();\n' %(member.membername))
file.write(indent_l11 + 'iter != %s.end(); ++iter) {\n'
%(member.membername))
# code inside the for loop
sequencetype = member.sequenceType
if sequencetype == 'std::string':
file.write(indent_l2 + 'const std::string &str = *iter;\n');
file.write(indent_l2 +
'crc->process_bytes(str.c_str(), str.size());\n')
elif (sequencetype == 'int' or sequencetype == 'uint64_t' or
sequencetype == 'bool'):
file.write(indent_l2 + 'const %s *obj = iter.operator->();\n'
% member.sequenceType)
file.write(indent_l2 +
'crc->process_bytes(obj, sizeof(*obj));\n')
elif member.isComplex:
file.write(indent_l2 + 'const %s *obj = iter.operator->();\n'
% member.sequenceType)
file.write(indent_l2 + 'obj->CalculateCrc(crc);\n')
else:
assert()
file.write(indent_l1 + '}\n')
elif member.isComplex: # complex but not sequence
file.write(indent_l1 +
'%s.CalculateCrc(crc);\n' % member.membername)
elif (cpptype == 'int' or cpptype == 'uint64_t' or
cpptype == 'time_t' or cpptype == 'bool'):
file.write(indent_l1 + 'crc->process_bytes(&%s, sizeof(%s));\n'
%(member.membername, member.membername));
elif (cpptype == 'std::string'):
file.write(indent_l1 +
'crc->process_bytes(%s.c_str(), %s.size());\n'
%(member.membername, member.membername));
else:
assert()
file.write('};\n')
def Generate(self, hdrname, file):
module_name = GetModuleName(file, '_types.h')
header = """
// autogenerated file --- DO NOT EDIT ---
#ifndef __SCHEMA__%(modname)s_TYPES_H__
#define __SCHEMA__%(modname)s_TYPES_H__
#include "%(hdrname)s"
#include <boost/bind.hpp>
#include "ifmap/autogen.h"
#include <pugixml/pugixml.hpp>
using namespace std;
namespace autogen {
""" % {'modname': module_name.upper(), 'hdrname': hdrname }
file.write(header)
for ctype in self._cTypeDict.values():
self.GenerateType(file, ctype)
file.write('}\n')
file.write('#endif // __SCHEMA__%s_TYPES_H__\n' %
module_name.upper())