forked from AIDASoft/podio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
podio_class_generator.py
executable file
·772 lines (703 loc) · 32.8 KB
/
podio_class_generator.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#!/usr/bin/env python
import os
import string
import sys
import pickle
import yaml
from podio_config_reader import PodioConfigReader
thisdir = os.path.dirname(os.path.abspath(__file__))
_text_ = """
PODIO Data Model
================
Used
%s
to create
%s classes
in
%s/
Read instructions in
the HOWTO.TXT to run
your first example!
"""
class ClassGenerator(object):
def __init__(self,yamlfile, install_dir, package_name, verbose = True):
self.yamlfile = yamlfile
self.install_dir = install_dir
self.package_name =package_name
self.template_dir = os.path.join(thisdir,"../templates")
self.verbose=verbose
self.buildin_types = ["int","float","double","unsigned int","unsigned","short","bool","longlong","ulonglong"]
self.created_classes = []
self.requested_classes = []
self.reader = PodioConfigReader(yamlfile)
self.warnings = []
def load_data_definitions(self):
self.datatypes = self.reader.read()
def process(self):
stream = open(self.yamlfile, "r")
content = yaml.load(stream)
if content.has_key("components"):
self.components = content["components"]
self.process_components(content["components"])
self.load_data_definitions()
self.process_datatypes(self.datatypes)
self.create_selection_xml()
self.print_report()
def process_components(self,content):
for name in content.iterkeys():
self.requested_classes.append(name)
for name, components in content.iteritems():
self.create_component(name, components)
def process_datatypes(self,content):
for name in content.iterkeys():
self.requested_classes.append(name)
self.requested_classes.append("%sData" %name)
for name, components in content.iteritems():
self.create_data(name, components)
self.create_class(name, components)
self.create_collection(name, components)
self.create_obj(name, components)
def print_report(self):
if self.verbose:
pkl = open(os.path.join(thisdir,"figure.txt"))
figure = pickle.load(pkl)
text = _text_ % (self.yamlfile,
len(self.created_classes),
self.install_dir
)
for i, line in enumerate(figure):
print
print line+text.splitlines()[i],
print " 'Homage to the Square' - Josef Albers"
print
def get_template(self,filename):
templatefile = os.path.join(self.template_dir,filename)
return open(templatefile,"r").read()
def create_selection_xml(self):
content = ""
for klass in self.created_classes:
#if not klass.endswith("Collection") or klass.endswith("Data"):
content += ' <class name="std::vector<%s>" />\n' %klass
content += """
<class name="%s">
<field name="m_registry" transient="true"/>
<field name="m_container" transient="true"/>
</class>\n""" %klass
templatefile = os.path.join(self.template_dir,"selection.xml.template")
template = open(templatefile,"r").read()
content = string.Template(template).substitute({"classes" : content})
self.write_file("selection.xml",content)
def create_data(self, classname, definition):
# check whether all member types are known
# and prepare include directives
namespace_open = ""
namespace_close = ""
if "::" in classname:
namespace, rawclassname = classname.split("::")
namespace_open = "namespace %s {" % namespace
namespace_close = "} // namespace %s" % namespace
else:
rawclassname = classname
includes = ""
description = definition["Description"]
author = definition["Author"]
members = definition["Members"]
for member in members:
klass = member["type"]
name = member["type"]
if klass in self.buildin_types:
pass
elif klass in self.requested_classes:
# includes += '#include "%s/%s.h"\n' %(self.package_name,klass)
if "::" in klass:
namespace, klassname = klass.split("::")
includes += '#include "%s.h"\n' % klassname
else:
includes += '#include "%s.h"\n' %(klass)
elif "std::array" in klass:
includes += "#include <array>\n"
self.created_classes.append(klass)
elif "vector" in klass:
includes += "#include <vector>\n"
self.warnings.append("%s defines a vector member %s, that spoils the PODness" %(classname, klass))
elif "[" in klass:
raise Exception("'%s' defines an array type. Array types are not supported yet." %(classname, klass))
else:
raise Exception("'%s' defines a member of a type '%s' that is not (yet) declared!" %(classname, klass))
membersCode = ""
for member in members:
name = member["name"]
klass = member["type"]
description = member["description"]
membersCode+= " %s %s; ///%s \n" %(klass, name, description)
# now handle the vector-members
vectormembers = definition["VectorMembers"]
for vectormember in vectormembers:
name = vectormember["name"]
membersCode+= " unsigned int %s_begin; \n" %(name)
membersCode+= " unsigned int %s_end; \n" %(name)
# now handle the one-to-many relations
refvectors = definition["OneToManyRelations"]
for refvector in refvectors:
name = refvector["name"]
membersCode+= " unsigned int %s_begin; \n" %(name)
membersCode+= " unsigned int %s_end; \n" %(name)
substitutions = {"includes" : includes,
"members" : membersCode,
"name" : rawclassname,
"description" : description,
"author" : author,
"package_name" : self.package_name,
"namespace_open" : namespace_open,
"namespace_close" : namespace_close
}
self.fill_templates("Data",substitutions)
self.created_classes.append(classname+"Data")
def create_class(self, classname, definition):
namespace_open = ""
namespace_close = ""
if "::" in classname:
namespace, rawclassname = classname.split("::")
namespace_open = "namespace %s {" % namespace
namespace_close = "} // namespace %s" % namespace
else:
rawclassname = classname
includes = ""
includes_cc = ""
forward_declarations = ""
forward_declarations_namespace = {"":[]}
getter_implementations = ""
setter_implementations = ""
getter_declarations = ""
setter_declarations = ""
constructor_signature = ""
constructor_body = ""
ConstGetter_implementations = ""
# check whether all member types are known
# and prepare include directives
includes += '#include "%s.h"\n' %(rawclassname+"Data")
#includes += '#include "%s/%s.h"\n' %(self.package_name,classname+"Data")
description = definition["Description"]
author = definition["Author"]
members = definition["Members"]
for member in members:
klass = member["type"]
if klass in self.buildin_types:
pass
elif klass in self.requested_classes:
#includes += '#include "%s/%s.h"\n' %(self.package_name,klass)
if "::" in klass:
namespace, klassname = klass.split("::")
includes += '#include "%s.h"\n' %klassname
else:
includes += '#include "%s.h"\n' %klass
elif "std::array" in klass:
includes += "#include <array>\n"
elif "vector" in klass:
includes += "#include <vector>\n"
else:
raise Exception("'%s' defines a member of a type '%s' that is not declared!" %(classname, klass))
# check on-to-one relations and prepare include directives
oneToOneRelations = definition["OneToOneRelations"]
for member in oneToOneRelations:
klass = member["type"]
if klass in self.requested_classes:
mnamespace = ""
klassname = klass
if "::" in klass:
mnamespace, klassname = klass.split("::")
if mnamespace not in forward_declarations_namespace.keys():
forward_declarations_namespace[mnamespace] = []
forward_declarations_namespace[mnamespace] += ["class %s;\n" %(klassname)]
forward_declarations_namespace[mnamespace] += ["class Const%s;\n" %(klassname)]
includes_cc += '#include "%s.h"\n' %(klassname)
for nsp in forward_declarations_namespace.iterkeys():
if nsp != "":
forward_declarations += "namespace %s {\n" % nsp
forward_declarations += "".join(forward_declarations_namespace[nsp])
if nsp != "":
forward_declarations += "}\n"
# check one-to-many relations for consistency
# and prepare include directives
refvectors = definition["OneToManyRelations"]
if len(refvectors) != 0:
includes += "#include <vector>\n"
for item in refvectors:
klass = item["type"]
if klass in self.requested_classes:
if "::" in klass:
mnamespace, klassname = klass.split("::")
includes += '#include "%s.h"\n' %klassname
else:
includes += '#include "%s.h"\n' %klass
elif "std::array" in klass:
includes += "#include <array>\n"
else:
raise Exception("'%s' declares a non-allowed many-relation to '%s'!" %(classname, klass))
# handle standard members
for member in members:
name = member["name"]
klass = member["type"]
description = member["description"]
getter_declarations+= " const %s& %s() const;\n" %(klass, name)
getter_implementations+= " const %s& %s::%s() const { return m_obj->data.%s; }\n" %(klass,rawclassname,name, name)
# getter_declarations+= " %s& %s() { return m_obj->data.%s; };\n" %(klass, name, name)
if klass in self.buildin_types:
setter_declarations += " void %s(%s value);\n" %(name, klass)
setter_implementations += "void %s::%s(%s value){ m_obj->data.%s = value;}\n" %(rawclassname, name, klass, name)
else:
setter_declarations += " %s& %s();\n" %(klass, name) # getting non-const reference is conceptually a setter
setter_implementations += " %s& %s::%s() { return m_obj->data.%s; }\n" %(klass, rawclassname,name, name)
setter_declarations += " void %s(class %s value);\n" %(name, klass)
setter_implementations += "void %s::%s(class %s value){ m_obj->data.%s = value;}\n" %(rawclassname, name, klass, name)
# set up signature
constructor_signature += "%s %s," %(klass, name)
# constructor
constructor_body += " m_obj->data.%s = %s;" %(name, name)
ConstGetter_implementations += " const %s& Const%s::%s() const { return m_obj->data.%s; }\n" %(klass, rawclassname, name, name)
# one-to-one relations
for member in oneToOneRelations:
name = member["name"]
klass = member["type"]
mnamespace = ""
klassname = klass
if "::" in klass:
mnamespace, klassname = klass.split("::")
setter_declarations += " void %s(%s::Const%s value);\n" %(name, mnamespace, klassname)
setter_implementations += "void %s::%s(%s::Const%s value) { if (m_obj->m_%s != nullptr) delete m_obj->m_%s; m_obj->m_%s = new Const%s(value); }\n" %(rawclassname, name, mnamespace, klassname, name, name, name, klassname)
getter_declarations += " const %s::Const%s %s() const;\n" %(mnamespace, klassname, name)
getter_implementations += " const %s::Const%s %s::%s() const { if (m_obj->m_%s == nullptr) {\n return %s::Const%s(nullptr);}\n return %s::Const%s(*(m_obj->m_%s));}\n" \
%(mnamespace, klassname, rawclassname, name, name, mnamespace, klassname, mnamespace, klassname, name)
ConstGetter_implementations += " const %s::Const%s Const%s::%s() const { if (m_obj->m_%s == nullptr) {\n return %s::Const%s(nullptr);}\n return %s::Const%s(*(m_obj->m_%s));}\n" \
%(mnamespace, klassname, rawclassname, name, name, mnamespace, klassname, mnamespace, klassname, name)
else:
setter_declarations += " void %s(Const%s value);\n" %(name, klassname)
setter_implementations += "void %s::%s(Const%s value) { if (m_obj->m_%s != nullptr) delete m_obj->m_%s; m_obj->m_%s = new Const%s(value); }\n" %(rawclassname,name, klassname, name, name, name,klassname)
getter_declarations += " const Const%s %s() const;\n" %(klassname, name)
getter_implementations += " const Const%s %s::%s() const { if (m_obj->m_%s == nullptr) {\n return Const%s(nullptr);}\n return Const%s(*(m_obj->m_%s));}\n" \
%(klassname, rawclassname, name, name, klassname, klassname, name)
ConstGetter_implementations += " const Const%s Const%s::%s() const { if (m_obj->m_%s == nullptr) {\n return Const%s(nullptr);}\n return Const%s(*(m_obj->m_%s));}\n" \
%(klassname, rawclassname, name, name, klassname, klassname, name)
# handle vector members
vectormembers = definition["VectorMembers"]
if len(vectormembers) != 0:
includes += "#include <vector>\n"
for item in vectormembers:
klass = item["type"]
if klass not in self.buildin_types and klass not in self.components:
raise Exception("'%s' declares a non-allowed vector member of type '%s'!" %(classname, klass))
if klass in self.components:
if "::" in klass:
namespace, klassname = klass.split("::")
includes += '#include "%s.h"\n' %klassname
else:
includes += '#include "%s.h"\n' %klass
# handle constructor from values
constructor_signature = constructor_signature.rstrip(",")
if constructor_signature == "":
constructor_implementation = ""
constructor_declaration = ""
ConstConstructor_declaration = ""
ConstConstructor_implementation = ""
else:
substitutions = { "name" : rawclassname,
"constructor" : constructor_body,
"signature" : constructor_signature
}
constructor_implementation = self.evaluate_template("Object.constructor.cc.template",substitutions)
constructor_declaration = " %s(%s);\n" %(rawclassname, constructor_signature)
ConstConstructor_implementation = self.evaluate_template("ConstObject.constructor.cc.template",substitutions)
ConstConstructor_declaration = "Const%s(%s);\n" %(rawclassname, constructor_signature)
# handle one-to-many relations
references_members = ""
references_declarations = ""
references = ""
ConstReferences_declarations = ""
ConstReferences = ""
references_template = self.get_template("RefVector.cc.template")
references_declarations_template = self.get_template("RefVector.h.template")
ConstReferences_declarations_template = self.get_template("ConstRefVector.h.template")
ConstReferences_template = self.get_template("ConstRefVector.cc.template")
for refvector in refvectors+definition["VectorMembers"]:
relationtype = refvector["type"]
if relationtype not in self.buildin_types and relationtype not in self.components:
relationtype = "Const"+relationtype
substitutions = {"relation" : refvector["name"],
"relationtype" : relationtype,
"classname" : rawclassname,
"package_name" : self.package_name
}
references_declarations += string.Template(references_declarations_template).substitute(substitutions)
references += string.Template(references_template).substitute(substitutions)
references_members += "std::vector<%s>* m_%s; //! transient \n" %(refvector["type"], refvector["name"])
ConstReferences_declarations += string.Template(ConstReferences_declarations_template).substitute(substitutions)
ConstReferences += string.Template(ConstReferences_template).substitute(substitutions)
substitutions = {"includes" : includes,
"includes_cc" : includes_cc,
"forward_declarations" : forward_declarations,
"getters" : getter_implementations,
"getter_declarations": getter_declarations,
"setters" : setter_implementations,
"setter_declarations": setter_declarations,
"constructor_declaration" : constructor_declaration,
"constructor_implementation" : constructor_implementation,
"name" : rawclassname,
"description" : description,
"author" : author,
"relations" : references,
"relation_declarations" : references_declarations,
"relation_members" : references_members,
""
"package_name" : self.package_name,
"namespace_open" : namespace_open,
"namespace_close" : namespace_close
}
self.fill_templates("Object",substitutions)
self.created_classes.append(classname)
substitutions["constructor_declaration"] = ConstConstructor_declaration
substitutions["constructor_implementation"] = ConstConstructor_implementation
substitutions["relation_declarations"] = ConstReferences_declarations
substitutions["relations"] = ConstReferences
substitutions["getters"] = ConstGetter_implementations
self.fill_templates("ConstObject", substitutions)
if "::" in classname:
self.created_classes.append("%s::Const%s" %(namespace, rawclassname))
else:
self.created_classes.append("Const%s" %classname)
def create_collection(self, classname, definition):
namespace_open = ""
namespace_close = ""
if "::" in classname:
namespace, rawclassname = classname.split("::")
namespace_open = "namespace %s {" % namespace
namespace_close = "} // namespace %s" % namespace
else:
rawclassname = classname
members = definition["Members"]
constructorbody = ""
prepareforwritinghead = ""
prepareforwritingbody = ""
vectorized_access_decl, vectorized_access_impl = self.prepare_vectorized_access(rawclassname,members)
setreferences = ""
prepareafterread = ""
includes = ""
initializers = ""
relations = ""
create_relations = ""
clear_relations = ""
push_back_relations = ""
prepareafterread_refmembers = ""
prepareforwriting_refmembers = ""
refmembers = definition["OneToOneRelations"]
refvectors = definition["OneToManyRelations"]
nOfRefVectors = len(refvectors)
nOfRefMembers = len(refmembers)
if nOfRefVectors + nOfRefMembers > 0:
# member initialization
constructorbody += " m_refCollections = new podio::CollRefCollection();\n"
clear_relations += " for (auto& pointer : (*m_refCollections)) {pointer->clear(); }\n"
for counter, item in enumerate(refvectors):
name = item["name"]
klass = item["type"]
substitutions = { "counter" : counter,
"class" : klass,
"name" : name }
mnamespace = ""
klassname = klass
if "::" in klass:
mnamespace, klassname = klass.split("::")
# includes
includes += '#include "%sCollection.h" \n' %(klassname)
if mnamespace != "":
# members
relations += " std::vector<::%s::Const%s>* m_rel_%s; //relation buffer for r/w\n" %(mnamespace, klassname, name)
relations += " std::vector<std::vector<::%s::Const%s>*> m_rel_%s_tmp;\n " %(mnamespace, klassname, name)
# constructor calls
initializers += ",m_rel_%s(new std::vector<::%s::Const%s>())" %(name, mnamespace, klassname)
else:
# members
relations += " std::vector<Const%s>* m_rel_%s; //relation buffer for r/w\n" %(klass, name)
relations += " std::vector<std::vector<Const%s>*> m_rel_%s_tmp;\n " %(klass, name)
# constructor calls
initializers += ",m_rel_%s(new std::vector<Const%s>())" %(name, klass)
constructorbody += " m_refCollections->push_back(new std::vector<podio::ObjectID>());\n"
# relation handling in ::create
create_relations += " m_rel_%s_tmp.push_back(obj->m_%s);\n" %(name,name)
# relation handling in ::clear
clear_relations += " // clear relations to %s. Make sure to unlink() the reference data as they may be gone already\n" %(name)
clear_relations += " for (auto& pointer : m_rel_%s_tmp) {for(auto& item : (*pointer)) {item.unlink();}; delete pointer;}\n" %(name)
clear_relations += " m_rel_%s_tmp.clear();\n" %(name)
clear_relations += " for (auto& item : (*m_rel_%s)) {item.unlink(); }\n" %(name)
clear_relations += " m_rel_%s->clear();\n" %(name)
# relation handling in push_back
push_back_relations += " m_rel_%s_tmp.push_back(obj->m_%s);\n" %(name,name)
# relation handling in ::prepareForWrite
prepareforwritingbody += self.evaluate_template("CollectionPrepareForWriting.cc.template",substitutions)
# relation handling in ::settingReferences
setreferences += self.evaluate_template("CollectionSetReferences.cc.template",substitutions)
prepareafterread += " obj->m_%s = m_rel_%s;" %(name, name)
for counter, item in enumerate(refmembers):
name = item["name"]
klass = item["type"]
mnamespace = ""
klassname = klass
if "::" in klass:
mnamespace, klassname = klass.split("::")
substitutions = { "counter" : counter+nOfRefVectors,
"class" : klass,
"rawclass" : klassname,
"name" : name }
# includes
includes += '#include "%sCollection.h" \n' %(klassname)
if mnamespace != "":
# constructor calls
initializers += ",m_rel_%s(new std::vector<::%s::Const%s>())" %(name, mnamespace, klassname)
# members
relations += " std::vector<::%s::Const%s>* m_rel_%s; //relation buffer for r/w\n" %(mnamespace, klassname, name)
else:
# constructor calls
initializers += ",m_rel_%s(new std::vector<Const%s>())" %(name, klass)
# members
relations += " std::vector<Const%s>* m_rel_%s; //relation buffer for r/w\n" %(klass, name)
constructorbody += " m_refCollections->push_back(new std::vector<podio::ObjectID>());\n"
# relation handling in ::clear
clear_relations += " for (auto& item : (*m_rel_%s)) {item.unlink(); }\n" %(name)
clear_relations += " m_rel_%s->clear();\n" %(name)
# relation handling in ::prepareForWrite
prepareforwriting_refmembers += " for (auto& obj : m_entries) {\nif (obj->m_%s != nullptr){\n(*m_refCollections)[%s]->emplace_back(obj->m_%s->getObjectID());} else {(*m_refCollections)[%s]->push_back({-2,-2}); } }\n" %(name,counter+nOfRefVectors,name,counter+nOfRefVectors)
# relation handling in ::settingReferences
prepareafterread_refmembers += self.evaluate_template("CollectionSetSingleReference.cc.template",substitutions)
substitutions = { "name" : rawclassname,
"constructorbody" : constructorbody,
"prepareforwritinghead" : prepareforwritinghead,
"prepareforwritingbody" : prepareforwritingbody,
"prepareforwriting_refmembers" : prepareforwriting_refmembers,
"setreferences" : setreferences,
"prepareafterread" : prepareafterread,
"prepareafterread_refmembers" : prepareafterread_refmembers,
"includes" : includes,
"initializers" : initializers,
"relations" : relations,
"create_relations" : create_relations,
"clear_relations" : clear_relations,
"push_back_relations" : push_back_relations,
"package_name" : self.package_name,
"vectorized_access_declaration" : vectorized_access_decl,
"vectorized_access_implementation" : vectorized_access_impl,
"namespace_open" : namespace_open,
"namespace_close" : namespace_close
}
self.fill_templates("Collection",substitutions)
self.created_classes.append("%sCollection"%classname)
def create_component(self, classname, components):
""" Create a component class to be used within the data types
Components can only contain simple data types and no user
defined ones
"""
namespace_open = ""
namespace_close = ""
if "::" in classname:
namespace, rawclassname = classname.split("::")
namespace_open = "namespace %s {" % namespace
namespace_close = "} // namespace %s" % namespace
else:
rawclassname = classname
for klass in components.itervalues():
if klass in self.buildin_types or self.components.has_key(klass):
pass
else:
raise Exception("'%s' defines a member of a type '%s' which is not allowed in a component!" %(classname, klass))
includes = ""
members = ""
for name, klass in components.iteritems():
klassname = klass
mnamespace = ""
if "::" in klass:
mnamespace, klassname = klass.split("::")
if mnamespace == "":
members+= " %s %s;\n" %(klassname, name)
else:
members += " ::%s::%s %s;\n" %(mnamespace, klassname, name)
if self.components.has_key(klass):
includes+= '#include "%s.h"\n' %(klassname)
substitutions = { "includes" : includes,
"members" : members,
"name" : rawclassname,
"package_name" : self.package_name,
"namespace_open" : namespace_open,
"namespace_close" : namespace_close
}
self.fill_templates("Component",substitutions)
self.created_classes.append(classname)
def create_obj(self, classname, definition):
""" Create an obj class containing all information
relevant for a given object.
"""
namespace_open = ""
namespace_close = ""
if "::" in classname:
namespace, rawclassname = classname.split("::")
namespace_open = "namespace %s {" % namespace
namespace_close = "} // namespace %s" % namespace
else:
rawclassname = classname
relations = ""
includes = ""
includes_cc = ""
forward_declarations = ""
forward_declarations_namespace = {"":[]}
initialize_relations = ""
deepcopy_relations = ""
delete_relations = ""
refvectors = definition["OneToManyRelations"]
singleRelations = definition["OneToOneRelations"]
# do includes and forward declarations for
# oneToOneRelations and do proper cleanups
for item in singleRelations:
name = item["name"]
klass = item["type"]
klassname = klass
mnamespace = ""
if "::" in klass:
mnamespace, klassname = klass.split("::")
if mnamespace not in forward_declarations_namespace.keys():
forward_declarations_namespace[mnamespace] = []
if mnamespace != "":
relations+= " ::%s::Const%s* m_%s;\n" %(mnamespace, klassname, name)
else:
relations+= " Const%s* m_%s;\n" %(klassname, name)
if klass not in self.buildin_types:
if klass != classname:
forward_declarations_namespace[mnamespace] += ['class Const%s;\n' %(klassname)]
includes_cc += '#include "%sConst.h"\n' %(klassname)
initialize_relations += ",m_%s(nullptr)\n" %(name)
delete_relations+="delete m_%s;\n" %name
for nsp in forward_declarations_namespace.iterkeys():
if nsp != "":
forward_declarations += "namespace %s {" % nsp
forward_declarations += "".join(forward_declarations_namespace[nsp])
if nsp != "":
forward_declarations += "}\n"
if len(refvectors+definition["VectorMembers"]) !=0:
includes += "#include <vector>\n"
for item in refvectors+definition["VectorMembers"]:
name = item["name"]
klass = item["type"]
if klass not in self.buildin_types:
if klass not in self.components:
if "::" in klass:
mnamespace, klassname = klass.split("::")
klassWithQualifier = "::"+mnamespace+"::Const"+klassname
else:
klassWithQualifier = "Const"+klass
else:
klassWithQualifier = klass
relations += " std::vector<%s>* m_%s;\n" %(klassWithQualifier, name)
initialize_relations += ",m_%s(new std::vector<%s>())" %(name,klassWithQualifier)
deepcopy_relations += ",m_%s(new std::vector<%s>(*(other.m_%s)))" %(name,klassWithQualifier,name)
if klass == classname:
includes_cc += '#include "%s.h"\n' %(rawclassname)
else:
if "::" in klass:
mnamespace, klassname = klass.split("::")
includes += '#include "%s.h"\n' %klassname
else:
includes += '#include "%s.h"\n' %klass
else:
relations += " std::vector<%s>* m_%s;\n" %(klass, name)
initialize_relations += ",m_%s(new std::vector<%s>())" %(name,klass)
deepcopy_relations += ",m_%s(new std::vector<%s>(*(other.m_%s)))" %(name,klass,name)
delete_relations += "delete m_%s;\n" %(name)
substitutions = { "name" : rawclassname,
"includes" : includes,
"includes_cc" : includes_cc,
"forward_declarations" : forward_declarations,
"relations" : relations,
"initialize_relations" : initialize_relations,
"deepcopy_relations" : deepcopy_relations,
"delete_relations" : delete_relations,
"namespace_open" : namespace_open,
"namespace_close" : namespace_close
}
self.fill_templates("Obj",substitutions)
self.created_classes.append(classname+"Obj")
def prepare_vectorized_access(self, classname,members ):
implementation = ""
declaration = ""
for member in members:
name = member["name"]
klass = member["type"]
substitutions = { "classname" : classname,
"member" : name,
"type" : klass
}
if klass not in self.buildin_types:
substitutions["type"] = "class %s" % klass
implementation += self.evaluate_template("CollectionReturnArray.cc.template", substitutions)
declaration += " template<size_t arraysize> \n const std::array<%s,arraysize> %s() const;\n" %(klass, name)
return declaration, implementation
def write_file(self, name,content):
#dispatch headers to header dir, the rest to /src
# fullname = os.path.join(self.install_dir,self.package_name,name)
if name.endswith("h"):
fullname = os.path.join(self.install_dir,self.package_name,name)
else:
fullname = os.path.join(self.install_dir,"src",name)
open(fullname, "w").write(content)
def evaluate_template(self, filename, substitutions):
""" reads in a given template, evaluates it
and returns result
"""
templatefile = os.path.join(self.template_dir,filename)
template = open(templatefile,"r").read()
return string.Template(template).substitute(substitutions)
def fill_templates(self, category, substitutions):
# "Data" denotes the real class;
# only headers and the FN should not contain Data
if category == "Data":
FN = "Data"
endings = ("h")
elif category == "Obj":
FN = "Obj"
endings = ("h","cc")
elif category == "Component":
FN = ""
endings = ("h")
elif category == "Object":
FN = ""
endings = ("h","cc")
elif category == "ConstObject":
FN = "Const"
endings = ("h","cc")
else:
FN = category
endings = ("h","cc")
for ending in endings:
templatefile = "%s.%s.template" %(category,ending)
templatefile = os.path.join(self.template_dir,templatefile)
template = open(templatefile,"r").read()
content = string.Template(template).substitute(substitutions)
filename = "%s%s.%s" %(substitutions["name"],FN,ending)
self.write_file(filename, content)
##########################
if __name__ == "__main__":
from optparse import OptionParser
usage = """usage: %prog [options] <description.yaml> <targetdir> <packagename>
Given a <description.yaml>
it creates data classes
and a LinkDef.h file in
the specified <targetdir>:
<packagename>/*.h
src/*.cc
"""
parser = OptionParser(usage)
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="Don't write a report to screen")
(options, args) = parser.parse_args()
if len(args) != 3:
parser.error("incorrect number of arguments")
gen = ClassGenerator(args[0], args[1], args[2], verbose = options.verbose)
gen.process()
for warning in gen.warnings:
print warning