7 from vapi_c_gen import CField, CEnum, CStruct, CSimpleType, CStructType,\
8 CMessage, json_to_c_header_name
9 from vapi_json_parser import JsonParser
12 class CppField(CField):
16 class CppStruct(CStruct):
24 class CppSimpleType (CSimpleType):
28 class CppStructType (CStructType, CppStruct):
32 class CppMessage (CMessage):
33 def get_swap_to_be_template_instantiation(self):
35 "template <> inline void vapi_swap_to_be<%s>(%s *msg)" %
36 (self.get_c_name(), self.get_c_name()),
38 " %s(msg);" % self.get_swap_to_be_func_name(),
42 def get_swap_to_host_template_instantiation(self):
44 "template <> inline void vapi_swap_to_host<%s>(%s *msg)" %
45 (self.get_c_name(), self.get_c_name()),
47 " %s(msg);" % self.get_swap_to_host_func_name(),
51 def get_alloc_template_instantiation(self):
53 "template <> inline %s* vapi_alloc<%s%s>"
54 "(Connection &con%s)" %
55 (self.get_c_name(), self.get_c_name(),
56 ", size_t" * len(self.get_alloc_vla_param_names()),
57 "".join([", size_t %s" % n for n in
58 self.get_alloc_vla_param_names()])
61 " %s* result = %s(con.vapi_ctx%s);" %
62 (self.get_c_name(), self.get_alloc_func_name(),
64 for n in self.get_alloc_vla_param_names()])),
65 "#if VAPI_CPP_DEBUG_LEAKS",
66 " con.on_shm_data_alloc(result);",
72 def get_cpp_name(self):
73 return "%s%s" % (self.name[0].upper(), self.name[1:])
75 def get_req_template_name(self):
76 if self.reply_is_stream:
81 return "%s<%s, %s%s>" % (
84 self.reply.get_c_name(),
85 "".join([", size_t"] * len(self.get_alloc_vla_param_names()))
88 def get_req_template_instantiation(self):
89 return "template class %s;" % self.get_req_template_name()
91 def get_type_alias(self):
92 return "using %s = %s;" % (
93 self.get_cpp_name(), self.get_req_template_name())
95 def get_reply_template_name(self):
96 return "Msg<%s>" % (self.get_c_name())
98 def get_reply_type_alias(self):
99 return "using %s = %s;" % (
100 self.get_cpp_name(), self.get_reply_template_name())
102 def get_msg_class_instantiation(self):
103 return "template class Msg<%s>;" % self.get_c_name()
105 def get_get_msg_id_t_instantiation(self):
107 ("template <> inline vapi_msg_id_t vapi_get_msg_id_t<%s>()"
108 % self.get_c_name()),
110 " return ::%s; " % self.get_msg_id_name(),
113 ("template <> inline vapi_msg_id_t "
114 "vapi_get_msg_id_t<Msg<%s>>()" % self.get_c_name()),
116 " return ::%s; " % self.get_msg_id_name(),
120 def get_cpp_constructor(self):
122 ('static void __attribute__((constructor)) '
123 '__vapi_cpp_constructor_%s()'
126 (' vapi::vapi_msg_set_msg_id<%s>(%s);' % (
127 self.get_c_name(), self.get_msg_id_name())),
132 def gen_json_header(parser, logger, j, io, gen_h_prefix, add_debug_comments):
133 logger.info("Generating header `%s'" % io.name)
134 orig_stdout = sys.stdout
136 d, f = os.path.split(j)
137 include_guard = "__included_hpp_%s" % (
138 f.replace(".", "_").replace("/", "_").replace("-", "_"))
139 print("#ifndef %s" % include_guard)
140 print("#define %s" % include_guard)
142 print("#include <vapi/vapi.hpp>")
143 print("#include <%s%s>" % (gen_h_prefix, json_to_c_header_name(f)))
145 print("namespace vapi {")
147 for m in parser.messages_by_json[j].values():
148 # utility functions need to go first, otherwise internal instantiation
149 # causes headaches ...
150 if add_debug_comments:
151 print("/* m.get_swap_to_be_template_instantiation() */")
152 print("%s" % m.get_swap_to_be_template_instantiation())
154 if add_debug_comments:
155 print("/* m.get_swap_to_host_template_instantiation() */")
156 print("%s" % m.get_swap_to_host_template_instantiation())
158 if add_debug_comments:
159 print("/* m.get_get_msg_id_t_instantiation() */")
160 print("%s" % m.get_get_msg_id_t_instantiation())
162 if add_debug_comments:
163 print("/* m.get_cpp_constructor() */")
164 print("%s" % m.get_cpp_constructor())
166 if not m.is_reply and not m.is_event:
167 if add_debug_comments:
168 print("/* m.get_alloc_template_instantiation() */")
169 print("%s" % m.get_alloc_template_instantiation())
171 if add_debug_comments:
172 print("/* m.get_msg_class_instantiation() */")
173 print("%s" % m.get_msg_class_instantiation())
175 if m.is_reply or m.is_event:
176 if add_debug_comments:
177 print("/* m.get_reply_type_alias() */")
178 print("%s" % m.get_reply_type_alias())
180 if add_debug_comments:
181 print("/* m.get_req_template_instantiation() */")
182 print("%s" % m.get_req_template_instantiation())
184 if add_debug_comments:
185 print("/* m.get_type_alias() */")
186 print("%s" % m.get_type_alias())
188 print("}") # namespace vapi
191 sys.stdout = orig_stdout
194 def json_to_cpp_header_name(json_name):
195 if json_name.endswith(".json"):
196 return "%s.vapi.hpp" % os.path.splitext(json_name)[0]
197 raise Exception("Unexpected json name `%s'!" % json_name)
200 def gen_cpp_headers(parser, logger, prefix, gen_h_prefix, remove_path,
201 add_debug_comments=False):
202 if prefix == "" or prefix is None:
205 prefix = "%s/" % prefix
206 if gen_h_prefix is None:
209 gen_h_prefix = "%s/" % gen_h_prefix
210 for j in parser.json_files:
212 d, f = os.path.split(j)
215 with open('%s%s' % (prefix, json_to_cpp_header_name(f)), "w") as io:
216 gen_json_header(parser, logger, j, io,
217 gen_h_prefix, add_debug_comments)
220 if __name__ == '__main__':
222 verbose = int(os.getenv("V", 0))
233 logging.basicConfig(stream=sys.stdout, level=log_level)
234 logger = logging.getLogger("VAPI CPP GEN")
235 logger.setLevel(log_level)
237 argparser = argparse.ArgumentParser(description="VPP C++ API generator")
238 argparser.add_argument('files', metavar='api-file', action='append',
239 type=str, help='json api file'
240 '(may be specified multiple times)')
241 argparser.add_argument('--prefix', action='store', default=None,
243 argparser.add_argument('--gen-h-prefix', action='store', default=None,
244 help='generated C header prefix')
245 argparser.add_argument('--remove-path', action='store_true',
246 help='remove path from filename')
247 args = argparser.parse_args()
249 jsonparser = JsonParser(logger, args.files,
250 simple_type_class=CppSimpleType,
251 struct_type_class=CppStructType,
252 field_class=CppField,
254 message_class=CppMessage)
256 gen_cpp_headers(jsonparser, logger, args.prefix, args.gen_h_prefix,
259 for e in jsonparser.exceptions: