7 from vapi_c_gen import CField, CStruct, CSimpleType, CStructType, CMessage, \
9 from vapi_json_parser import JsonParser
12 class CppField(CField):
19 super().__init__(field_name, field_type, array_len, nelem_field)
22 class CppStruct(CStruct):
23 def __init__(self, name, fields):
24 super().__init__(name, fields)
27 class CppSimpleType (CSimpleType):
29 def __init__(self, name):
30 super().__init__(name)
33 class CppStructType (CStructType, CppStruct):
34 def __init__(self, definition, typedict, field_class):
35 super().__init__(definition, typedict, field_class)
38 class CppMessage (CMessage):
39 def __init__(self, logger, definition, typedict,
40 struct_type_class, simple_type_class, field_class):
41 super().__init__(logger, definition, typedict, struct_type_class,
42 simple_type_class, field_class)
44 def get_swap_to_be_template_instantiation(self):
46 "template <> inline void vapi_swap_to_be<%s>(%s *msg)" %
47 (self.get_c_name(), self.get_c_name()),
49 " %s(msg);" % self.get_swap_to_be_func_name(),
53 def get_swap_to_host_template_instantiation(self):
55 "template <> inline void vapi_swap_to_host<%s>(%s *msg)" %
56 (self.get_c_name(), self.get_c_name()),
58 " %s(msg);" % self.get_swap_to_host_func_name(),
62 def get_alloc_template_instantiation(self):
64 "template <> inline %s* vapi_alloc<%s%s>"
65 "(Connection &con%s)" %
66 (self.get_c_name(), self.get_c_name(),
67 ", size_t" * len(self.get_alloc_vla_param_names()),
68 "".join([", size_t %s" % n for n in
69 self.get_alloc_vla_param_names()])
72 " %s* result = %s(con.vapi_ctx%s);" %
73 (self.get_c_name(), self.get_alloc_func_name(),
75 for n in self.get_alloc_vla_param_names()])),
76 "#if VAPI_CPP_DEBUG_LEAKS",
77 " con.on_shm_data_alloc(result);",
83 def get_cpp_name(self):
84 return "%s%s" % (self.name[0].upper(), self.name[1:])
86 def get_req_template_name(self):
92 return "%s<%s, %s%s>" % (
95 self.reply.get_c_name(),
96 "".join([", size_t"] * len(self.get_alloc_vla_param_names()))
99 def get_req_template_instantiation(self):
100 return "template class %s;" % self.get_req_template_name()
102 def get_type_alias(self):
103 return "using %s = %s;" % (
104 self.get_cpp_name(), self.get_req_template_name())
106 def get_reply_template_name(self):
107 return "Msg<%s>" % (self.get_c_name())
109 def get_reply_type_alias(self):
110 return "using %s = %s;" % (
111 self.get_cpp_name(), self.get_reply_template_name())
113 def get_msg_class_instantiation(self):
114 return "template class Msg<%s>;" % self.get_c_name()
116 def get_get_msg_id_t_instantiation(self):
118 ("template <> inline vapi_msg_id_t vapi_get_msg_id_t<%s>()"
119 % self.get_c_name()),
121 " return ::%s; " % self.get_msg_id_name(),
124 ("template <> inline vapi_msg_id_t "
125 "vapi_get_msg_id_t<Msg<%s>>()" % self.get_c_name()),
127 " return ::%s; " % self.get_msg_id_name(),
131 def get_cpp_constructor(self):
133 ('static void __attribute__((constructor)) '
134 '__vapi_cpp_constructor_%s()'
137 (' vapi::vapi_msg_set_msg_id<%s>(%s);' % (
138 self.get_c_name(), self.get_msg_id_name())),
143 def gen_json_header(parser, logger, j, io, gen_h_prefix, add_debug_comments):
144 logger.info("Generating header `%s'" % io.name)
145 orig_stdout = sys.stdout
147 include_guard = "__included_hpp_%s" % (
148 j.replace(".", "_").replace("/", "_").replace("-", "_"))
149 print("#ifndef %s" % include_guard)
150 print("#define %s" % include_guard)
152 print("#include <vapi/vapi.hpp>")
153 print("#include <%s%s>" % (gen_h_prefix, json_to_c_header_name(j)))
155 print("namespace vapi {")
157 for m in parser.messages_by_json[j].values():
158 # utility functions need to go first, otherwise internal instantiation
159 # causes headaches ...
160 if add_debug_comments:
161 print("/* m.get_swap_to_be_template_instantiation() */")
162 print("%s" % m.get_swap_to_be_template_instantiation())
164 if add_debug_comments:
165 print("/* m.get_swap_to_host_template_instantiation() */")
166 print("%s" % m.get_swap_to_host_template_instantiation())
168 if add_debug_comments:
169 print("/* m.get_get_msg_id_t_instantiation() */")
170 print("%s" % m.get_get_msg_id_t_instantiation())
172 if add_debug_comments:
173 print("/* m.get_cpp_constructor() */")
174 print("%s" % m.get_cpp_constructor())
177 if add_debug_comments:
178 print("/* m.get_alloc_template_instantiation() */")
179 print("%s" % m.get_alloc_template_instantiation())
181 if add_debug_comments:
182 print("/* m.get_msg_class_instantiation() */")
183 print("%s" % m.get_msg_class_instantiation())
186 if add_debug_comments:
187 print("/* m.get_reply_type_alias() */")
188 print("%s" % m.get_reply_type_alias())
190 if add_debug_comments:
191 print("/* m.get_req_template_instantiation() */")
192 print("%s" % m.get_req_template_instantiation())
194 if add_debug_comments:
195 print("/* m.get_type_alias() */")
196 print("%s" % m.get_type_alias())
198 print("}") # namespace vapi
201 sys.stdout = orig_stdout
204 def json_to_cpp_header_name(json_name):
205 if json_name.endswith(".json"):
206 return "%s.vapi.hpp" % os.path.splitext(json_name)[0]
207 raise Exception("Unexpected json name `%s'!" % json_name)
210 def gen_cpp_headers(parser, logger, prefix, gen_h_prefix,
211 add_debug_comments=False):
212 if prefix == "" or prefix is None:
215 prefix = "%s/" % prefix
216 if gen_h_prefix is None:
219 gen_h_prefix = "%s/" % gen_h_prefix
220 for j in parser.json_files:
221 with open('%s%s' % (prefix, json_to_cpp_header_name(j)), "w") as io:
222 gen_json_header(parser, logger, j, io,
223 gen_h_prefix, add_debug_comments)
226 if __name__ == '__main__':
228 verbose = int(os.getenv("V", 0))
239 logging.basicConfig(stream=sys.stdout, level=log_level)
240 logger = logging.getLogger("VAPI CPP GEN")
241 logger.setLevel(log_level)
243 argparser = argparse.ArgumentParser(description="VPP C++ API generator")
244 argparser.add_argument('files', metavar='api-file', action='append',
245 type=str, help='json api file'
246 '(may be specified multiple times)')
247 argparser.add_argument('--prefix', action='store', default=None,
249 argparser.add_argument('--gen-h-prefix', action='store', default=None,
250 help='generated C header prefix')
251 args = argparser.parse_args()
253 jsonparser = JsonParser(logger, args.files,
254 simple_type_class=CppSimpleType,
255 struct_type_class=CppStructType,
256 field_class=CppField,
257 message_class=CppMessage)
259 gen_cpp_headers(jsonparser, logger, args.prefix, args.gen_h_prefix)
261 for e in jsonparser.exceptions: