-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccclass.cpp
More file actions
628 lines (548 loc) · 22.2 KB
/
ccclass.cpp
File metadata and controls
628 lines (548 loc) · 22.2 KB
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
//
// Copyright 2015 Hans-Juergen Lange <hjl@simulated-universe.de>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the “Software”), to deal in the
// Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <string>
#include "helper.h"
#include "melement.h"
#include "cparameter.h"
#include "moperation.h"
#include "coperation.h"
#include "mattribute.h"
#include "cattribute.h"
#include "massociation.h"
#include "cassociation.h"
#include "massociationend.h"
#include "cassociationend.h"
#include "mdependency.h"
#include "cdependency.h"
#include "cclassbase.h"
#include "ccclass.h"
#include "cmodel.h"
std::string CCClass::FQN() const {
return name;
}
void CCClass::Prepare(void) {
PrepareBase();
CollectAttributes();
donelist.clear();
}
void CCClass::SetFromTags(const std::string& name, const std::string&value)
{
if (name == "ExtraInclude") {
extraheader = value;
} else {
}
}
void CCClass::CollectExtraHeader(std::shared_ptr<MElement> e) {
(void)e;
}
void CCClass::CollectFromBase(const TypeNode& aNode, HeaderList& aHeaderList) {
auto nc = aNode.mClassifier;
(void)aHeaderList;
if (nc) {
auto ncc = std::dynamic_pointer_cast<CClassBase>(*nc);
//
// Check the node classifier.
if ((ncc->IsExternClass() && (!ncc->IsExternInModel())) || (nc->type == eElementType::QtClass)) {
extramodelheader.push_back(ncc);
} else {
CollectNeededModelHeader(nc, neededmodelheader);
}
for (auto & p : aNode.mParameter) {
CollectFromBase(p, neededmodelheader);
}
}
}
void CCClass::CollectFromAttribute(const TypeNode& aNode, HeaderList& aHeaderList) {
auto nc = aNode.mClassifier;
if (nc) {
auto ncc = std::dynamic_pointer_cast<CClassBase>(*nc);
//
// Check the node classifier.
if ((ncc->IsExternClass() && (!ncc->IsExternInModel())) || (nc->type == eElementType::QtClass)) {
extramodelheader.push_back(ncc);
} else {
//
// Composition.
if (aNode.mExtension == TypeExtension::None) {
CollectNeededModelHeader(nc, aHeaderList);
} else {
CollectNeededModelHeader(nc, aHeaderList);
}
}
for (auto & p : aNode.mParameter) {
CollectFromAttribute(p, aHeaderList);
}
}
}
void CCClass::CollectFromParameter(const TypeNode& aNode, HeaderList& aHeaderList) {
auto nc = aNode.mClassifier;
if (nc) {
auto ncc = std::dynamic_pointer_cast<CClassBase>(*nc);
if ((ncc->IsExternClass() && (!ncc->IsExternInModel())) || (nc->type == eElementType::QtClass)) {
extramodelheader.push_back(ncc);
} else {
CollectNeededModelHeader(nc, neededmodelheader);
}
for (auto & p : aNode.mParameter) {
CollectFromParameter(p, aHeaderList);
}
}
}
void CCClass::CollectFromSharedAssoc(const TypeNode& aNode, HeaderList& aHeaderList) {
auto nc = aNode.mClassifier;
if (nc) {
auto ncc = std::dynamic_pointer_cast<CClassBase>(*nc);
if ((ncc->IsExternClass() && (!ncc->IsExternInModel())) || (nc->type == eElementType::QtClass)) {
extramodelheader.push_back(ncc);
} else if (nc->type == eElementType::SimObject) {
auto st = MClass::mByFQN.find("tSimObj");
if (st != MClass::mByFQN.end()) {
CollectNeededModelHeader(st->second, aHeaderList);
}
} else {
CollectNeededModelHeader(nc, optionalmodelheader);
}
for (auto & p : aNode.mParameter) {
CollectFromSharedAssoc(p, aHeaderList);
}
}
}
void CCClass::CollectFromCompositeAssoc(const TypeNode& aNode, HeaderList& aHeaderList) {
auto nc = aNode.mClassifier;
if (nc) {
auto ncc = std::dynamic_pointer_cast<CClassBase>(*nc);
if ((ncc->IsExternClass() && (!ncc->IsExternInModel())) || (nc->type == eElementType::QtClass)) {
extramodelheader.push_back(ncc);
} else {
CollectNeededModelHeader(nc, aHeaderList);
}
for (auto & p : aNode.mParameter) {
CollectFromCompositeAssoc(p, aHeaderList);
}
}
}
//
// The next optimization step ahead.
void CCClass::CollectNeededModelHeader(std::shared_ptr<MElement> e, HeaderList& aHeaderList) {
//
// We should work only on class based elements.
// And the ones we have not processed so far.
if ((e->IsClassBased()) && (!aHeaderList.isdone(e))) {
//
// Whatever we do next we need to lockout the element we process through the donelist.
aHeaderList.done(e);
//
// As we work on class based elements only we can cast the element to the classbase
auto c = std::dynamic_pointer_cast<CClassBase>(e);
//
// Go to the base-classes
// Base classes only can be external or similar or are in the needed list.
for (auto & bi : c->Base) {
CollectFromBase(bi.getElement()->mTypeTree, aHeaderList);
}
//
// Go through all attributes.
for (auto & a : c->allAttr) {
//
// Different ways to handle the attribute type.
// If the attribute type is linked to some classifier we use it.
if (a->Classifier && (a->Classifier->IsClassBased())) {
auto ac = std::dynamic_pointer_cast<CClassBase>( *a->Classifier);
//
// Create a list of types from the classifier.
CollectFromAttribute(ac->mTypeTree, aHeaderList);
if (a->isMultiple) {
if (a->Qualifier.empty()) {
auto v = MClass::mByFQN.find("std::vector");
if (v != MClass::mByFQN.end()) {
extramodelheader.push_back(std::dynamic_pointer_cast<CClassBase>(v->second));
}
} else {
auto m = MClass::mByFQN.find("std::map");
if (m != MClass::mByFQN.end()) {
extramodelheader.push_back(std::dynamic_pointer_cast<CClassBase>(m->second));
}
}
}
} else {
TypeNode temp(TypeNode::parse(a->ClassifierName));
temp.fill(mNameSpace.getString());
CollectFromParameter(temp, aHeaderList);
}
}
//
// Go through all association ends.
for (auto & a : c->allEnds) {
if (a->Classifier->IsClassBased()) {
auto ac = std::dynamic_pointer_cast<CClassBase>(*a->Classifier);
//
// The specific classes of an association.
auto assoc = std::dynamic_pointer_cast<CAssociation>(*a->parent);
auto otherend = a;
auto thisend = assoc->OtherEnd(a);
//
// Sanity check of otherend and check if navigable.
if (otherend && otherend->Classifier && (otherend->isNavigable())) {
//
// Check if pointer or not.
if (thisend && (thisend->Aggregation == aShared)) {
CollectFromSharedAssoc(ac->mTypeTree, aHeaderList);
} else if (thisend && (thisend->Aggregation == aComposition)) {
CollectFromCompositeAssoc(ac->mTypeTree, aHeaderList);
}
if (!(otherend->Multiplicity.empty()) && (otherend->Multiplicity != "1")) {
if (otherend->mQualifier.empty()) {
auto v = MClass::mByFQN.find("std::vector");
if (v != MClass::mByFQN.end()) {
extramodelheader.push_back(std::dynamic_pointer_cast<CClassBase>(v->second));
}
} else {
auto m = MClass::mByFQN.find("std::map");
if (m != MClass::mByFQN.end()) {
extramodelheader.push_back(std::dynamic_pointer_cast<CClassBase>(m->second));
}
}
}
}
}
}
for (auto & oi : c->Operation) {
auto operation = std::dynamic_pointer_cast<COperation>(*oi);
//
// Go through the parameters and return values.
for (auto & pi : operation->Parameter) {
auto param = std::dynamic_pointer_cast<CParameter>(*pi);
if (param->Classifier && (param->Classifier->IsClassBased())) {
//
// Create a list of types from the classifier.
CollectFromParameter(std::dynamic_pointer_cast<CClassBase>(*param->Classifier)->mTypeTree, aHeaderList);
} else {
TypeNode temp(TypeNode::parse(param->ClassifierName));
temp.fill(mNameSpace.getString());
CollectFromParameter(temp, aHeaderList);
}
}
}
//
// Here starts the exit sequence.
// We ad ourselfes into the needed list.
aHeaderList.add(e);
//
// Collect the extraheaders. Ignore the extern classes that have inmodel headers.
if (!(!c->IsExternInModel()) && (!c->GetExtraHeader().empty())) {
extramodelheader.push_back(c);
}
//
// If we are on the way out the tree we may add the dependencies.
if (e.get() == this) {
//
// Collect the extraheaders. Ignore the extern classes that have inmodel headers.
if ((!IsExternInModel()) && (!GetExtraHeader().empty())) {
extramodelheader.push_back(std::dynamic_pointer_cast<CClassBase>(MElement::Instances[id]));
}
for (auto & i : Supplier) {
if (i.getElement()->IsClassBased()) {
if ((i.getElement()->type==eElementType::ExternClass) || (i.getElement()->type==eElementType::QtClass)) {
extramodelheader.push_back(std::dynamic_pointer_cast<CClassBase>(i.getElement()));
} else {
CollectNeededModelHeader(i.getElement(), optionalmodelheader);
}
}
}
}
}
}
void CCClass::DumpOperationDecl(std::ostream& hdr) {
bool dump=true;
for (auto & mo : Operation) {
if (mo->visibility == vPublic) {
if (dump) {
dump=false;
}
auto op = std::dynamic_pointer_cast<COperation>(*mo);
std::string pdecl=op->GetParameterDecl(NameSpace());
hdr << op->GetReturnType(NameSpace()) << " ";
hdr << op->name << "(" << pdecl << ") ;\n";
}
}
}
void CCClass::DumpAttributeDecl(std::ostream& hdr) {
bool dump = true;
for (auto & ma : Attribute) {
auto a = std::dynamic_pointer_cast<CAttribute>(*ma);
if ((ma->visibility == vPublic) && (!a->isStatic)) {
if (dump) {
dump=false;
}
std::string cname;
if (a->Classifier) {
cname = a->Classifier->FQN();
} else {
cname = a->ClassifierName;
}
if (!cname.empty()) {
hdr << "extern ";
hdr << cname << " " << a->name;
if ((a->Multiplicity == "1") || (a->Multiplicity.empty())) {
hdr << ";\n";
} else {
if ((a->Multiplicity == "1..*") || (a->Multiplicity == "*") || (a->Multiplicity == "0..*")) {
} else {
hdr << "[" << a->Multiplicity << "];\n";
}
}
}
}
}
//
// Go through the associations
for (auto & me : OtherEnd) {
auto a = std::dynamic_pointer_cast<CAssociationEnd>(*me);
//
// Only use public navigable ends.
if ((me->visibility == vPublic) && a->isNavigable() ){
if (dump) {
dump=false;
}
hdr << "extern ";
std::string cname=a->Classifier->name;
auto assoc = std::dynamic_pointer_cast<CAssociation>(*a->parent);
if (assoc->ends[0] == me) {
if (std::dynamic_pointer_cast<CAssociationEnd>(*assoc->ends[1])->Aggregation == aShared) {
cname.push_back('*');
}
} else {
if (std::dynamic_pointer_cast<CAssociationEnd>(*assoc->ends[0])->Aggregation == aShared) {
cname.push_back('*');
}
}
if ((a->Multiplicity == "1") || (a->Multiplicity.empty())) {
hdr << cname << " " << a->name << ";\n";
} else {
if ((a->Multiplicity == "1..*") || (a->Multiplicity == "*") || (a->Multiplicity == "0..*")) {
hdr << "std::vector<" << cname << "> " << a->name << ";\n";
//
// We expect that the stl is defined somewhere and search the vector definition.
auto vec = MClass::mByFQN.find("std::vector");
if (vec != MClass::mByFQN.end()) {
optionalmodelheader.add(vec->second);
}
} else {
hdr << cname << " " << a->name << "[" << a->Multiplicity << "];\n";
}
}
}
}
}
void CCClass::DumpAttributeDefinition(std::ostream& src, eVisibility vis) {
bool dump = true;
for (auto & ma : Attribute) {
auto a = std::dynamic_pointer_cast<CAttribute>(*ma);
if (ma->visibility == vis) {
if (dump) {
dump=false;
}
std::string cname;
if (a->Classifier) {
cname = a->Classifier->FQN();
} else {
cname = a->ClassifierName;
}
if (!cname.empty()) {
if ((vis == vPrivate) || (a->isStatic)) {
src << "static ";
}
src << cname << " " << a->name;
if ((a->Multiplicity == "1") || (a->Multiplicity.empty())) {
if (!a->defaultValue.empty()) {
src << " = " << a->defaultValue;
}
src << ";\n";
} else {
if ((a->Multiplicity == "1..*") || (a->Multiplicity == "*") || (a->Multiplicity == "0..*")) {
} else {
src << "[" << a->Multiplicity << "];\n";
}
}
}
}
}
for (auto & me : OtherEnd) {
auto a = std::dynamic_pointer_cast<CAssociationEnd>(*me);
if (((*me)->visibility == vis) && a->isNavigable()){
std::string cname=a->Classifier->name;
if (dump) {
dump=false;
}
if (vis == vPrivate) {
src << "static ";
}
auto assoc = std::dynamic_pointer_cast<CAssociation>(*a->parent);
if (assoc->ends[0] == me) {
if (std::dynamic_pointer_cast<CAssociationEnd>(*assoc->ends[1])->Aggregation == aShared) {
cname.push_back('*');
}
} else {
if (std::dynamic_pointer_cast<CAssociationEnd>(*assoc->ends[0])->Aggregation == aShared) {
cname.push_back('*');
}
}
if ((a->Multiplicity == "1") || (a->Multiplicity.empty())) {
src << cname << " " << a->name;
if (!a->defaultValue.empty()) {
src << " = " << a->defaultValue;
}
src << ";\n";
} else {
if ((a->Multiplicity == "1..*") || (a->Multiplicity == "*") || (a->Multiplicity == "0..*")) {
src << "std::vector<" << cname << "> " << a->name << ";\n";
} else {
src << cname << " " << a->name << "[" << a->Multiplicity << "];\n";
}
}
}
}
}
void CCClass::DumpOperationDefinition(std::ostream &src) {
std::string rettype;
for (auto & mo : Operation) {
if (mo->visibility == vPrivate) {
auto op = std::dynamic_pointer_cast<COperation>(*mo);
//
// Dump any comment before the function prototype.
mo->DumpComment(src, 0, 130);
std::string pdef=op->GetParameterDefinition(NameSpace());
rettype=op->GetReturnType(NameSpace());
if (rettype.at(rettype.size()-1)== '&') {
rettype=rettype.substr(0, rettype.size()-1);
}
src << "static " << rettype << " ";
if (pdef == "void") {
src << op->name << "() {\n";
} else {
src << op->name << "(" << pdef << ") {\n";
}
if ((op->GetReturnType(NameSpace()) != "void") && (op->GetReturnType(NameSpace()) == rettype)) {
src << " " << rettype << " " << op->GetReturnName();
if (!op->GetReturnDefault().empty()) {
src << " = " << op->GetReturnDefault();
}
src << ";\n";
}
src << "// User-Defined-Code:" << op->id << "\n";
src << "// End-Of-UDC:" << op->id << "\n";
if ((op->GetReturnType(NameSpace()) != "void") && (op->GetReturnType(NameSpace()) == rettype)) {
src << " return ("<< op->GetReturnName() << ");\n";
}
src << "}\n\n";
}
}
for (auto & mo : Operation) {
if ((mo->visibility == vProtected) || (mo->visibility == vPublic)) {
auto op = std::dynamic_pointer_cast<COperation>(*mo);
//
// Dump any comment before the function prototype.
mo->DumpComment(src, 0, 130);
std::string pdef=op->GetParameterDefinition(NameSpace());
rettype=op->GetReturnType(NameSpace());
if (rettype.at(rettype.size()-1)== '&') {
rettype=rettype.substr(0, rettype.size()-1);
}
src << rettype << " ";
if (pdef == "void") {
src << op->name << "() {\n";
} else {
src << op->name << "(" << pdef << ") {\n";
}
if ((op->GetReturnType(NameSpace()) != "void") && (op->GetReturnType(NameSpace()) == rettype)) {
src << " " << rettype << " " << op->GetReturnName();
if (!op->GetReturnDefault().empty()) {
src << " = " << op->GetReturnDefault();
}
src << ";\n";
}
src << "// User-Defined-Code:" << op->id << "\n";
src << "// End-Of-UDC:" << op->id << "\n";
if ((op->GetReturnType(NameSpace()) != "void") && (op->GetReturnType(NameSpace()) == rettype)) {
src << " return ("<< op->GetReturnName() << ");\n";
}
src << "}\n\n";
}
}
}
void CCClass::Dump(std::shared_ptr<MModel> model) {
std::set<std::shared_ptr<MElement>> oplist;
bool dump;
auto thisbase = std::dynamic_pointer_cast<CClassBase>(MClass::Instances[id]);
//
// Collect all headers needed to dump the files.
CollectNeededModelHeader(thisbase, neededmodelheader);
DumpBase(std::dynamic_pointer_cast<CModel>(model), name);
DumpFileHeader(hdr, name, ".h");
DumpGuardHead(hdr, name);
DumpPublicMacros(hdr);
DumpFileHeader(src, name, ".cpp");
donelist.clear();
//
// Create forwards from list of optional headers.
dump = true;
for (auto & i : optionalmodelheader) {
//
// Forwards are only valid for classes.
if (i->type == eElementType::CxxClass) {
//
// We do not generate forwards for ourselfes.
if (i.get() != this) {
//
// Check if name is in done list. Only if not in the donelist.
// we create a forward.
if (donelist.find(i->name) == donelist.end()) {
if (dump) {
dump = false;
hdr << "//\n"
"// List of forwards needed in this module.\n";
}
//
// Output the forward.
hdr << "class " << i->name << ";\n";
//
// Add name to donelist to prevent us from doing things twice.
donelist.insert(i->name);
}
}
}
}
DumpAttributeDecl(hdr);
DumpOperationDecl(hdr);
hdr << "\n";
DumpGuardTail(hdr, name);
donelist.clear();
DumpExtraIncludes(src, donelist, oplist);
donelist.clear();
DumpNeededIncludes(src, thisbase, donelist, oplist);
src << "// Optional\n";
oplist.clear();
DumpOptionalIncludes(src, thisbase, donelist, oplist);
DumpPrivateMacros(src);
DumpAttributeDefinition(src, vPrivate);
DumpAttributeDefinition(src);
DumpOperationDefinition(src);
CloseStreams();
}