summaryrefslogtreecommitdiffstats
path: root/lib/interfaces/codemodel.cpp
blob: 567f785a46d272a298767f449e93e9e657959f9d (plain)
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
/* This file is part of KDevelop
    Copyright (C) 2003 Roberto Raggi <roberto@kdevelop.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

#include "codemodel.h"
#include "driver.h"
#include <kdebug.h>
#include <kdatastream.h>

///Little helper-functions to save a lot of typing and possible errors
template<class MapContainer>
bool eachCanUpdate( const MapContainer& old, const MapContainer& newMap ) {
  if( old.size() != newMap.size() ) return false;

  typename MapContainer::const_iterator oldIt = old.begin();
  typename MapContainer::const_iterator newIt = newMap.begin();
  while( oldIt != old.end() ) {
    typedef typename MapContainer::mapped_type ListType;
    if( (*oldIt).size() != (*newIt).size() ) return false;
    typename ListType::const_iterator it1 = (*oldIt).begin();
    typename ListType::const_iterator it2 = (*newIt).begin();

    while( it1 != (*oldIt).end() ) {
      if( !(*it1)->canUpdate( *it2 ) ) return false;
      ++it1;
      ++it2;
    }
    ++oldIt;
    ++newIt;
  }
  return true;
}

template<class MapContainer>
void eachUpdate( MapContainer& old, const MapContainer& newMap  ) {
  if( old.size() != newMap.size() ) kdError( 9007 ) << "error in eachUpdate(...) 1" << endl;
  typename MapContainer::iterator oldIt = old.begin();
  typename MapContainer::const_iterator newIt = newMap.begin();
  while( oldIt != old.end() ) {
    if( (*oldIt).size() != (*newIt).size() ) kdError( 9007 ) << "error in eachUpdate(...) 2" << endl;
    typedef typename MapContainer::mapped_type ListType;
    typename ListType::iterator it1 = (*oldIt).begin();
    typename ListType::const_iterator it2 = (*newIt).begin();
    while( it1 != (*oldIt).end() ) {
      (*it1)->update( *it2 );
      ++it1;
      ++it2;
    }
    ++oldIt;
    ++newIt;
  }
}

///Versions for contains that do not contain maps again
template<class MapContainer>
bool eachCanUpdateSingle( const MapContainer& old, const MapContainer& newMap ) {
  if( old.size() != newMap.size() ) return false;

  typename MapContainer::const_iterator oldIt = old.begin();
  typename MapContainer::const_iterator newIt = newMap.begin();
  while( oldIt != old.end() ) {
    if( !(*oldIt)->canUpdate( *newIt ) ) return false;
    ++oldIt;
    ++newIt;
  }
  return true;
}

template<class MapContainer>
void eachUpdateSingle( MapContainer& old, const MapContainer& newMap  ) {
  if( old.size() != newMap.size() ) kdError( 9007 ) << "error in eachUpdate(...) 1" << endl;
  typename MapContainer::iterator oldIt = old.begin();
  typename MapContainer::const_iterator newIt = newMap.begin();
  while( oldIt != old.end() ) {
    (*oldIt)->update( *newIt );
    ++oldIt;
    ++newIt;
  }
}

CodeModel::CodeModel()
{
    wipeout();
    m_currentGroupId = 1;   ///0 stands for invalid group
}

CodeModel::~ CodeModel( )
{
}

int CodeModel::newGroupId() {
    return (m_currentGroupId++) * 2;
}

inline bool isSingleGroup( const int group ) {
    return (group % 2) == 0;
}

QStringList CodeModel::getGroupStrings(int gid) const {
    QStringList ret;
    for(QMap<QString, FileDom>::ConstIterator it = m_files.begin(); it != m_files.end(); ++it) {
        if((*it)->groupId() == gid) ret.append( (*it)-> name() );
    }
    return ret;
}


FileList CodeModel::getGroup(int gid) const {
    FileList ret;
    for(QMap<QString, FileDom>::ConstIterator it = m_files.begin(); it != m_files.end(); ++it) {
        if((*it)->groupId() == gid) ret.append(*it);
    }
    return ret;
}

FileList CodeModel::getGroup( const FileDom& dom) const {
    return getGroup( dom->groupId() );
}

int CodeModel::mergeGroups( int g1, int g2) {
    if( !g1 || !g2 ) return 0;
    if( g1 == g2 ) return g1;
    int ng = isSingleGroup( g1 ) ? g2 : g1;
    if( isSingleGroup( ng ) )
        ng = newGroupId() + 1;
    
    for( QMap<QString, FileDom>::iterator it = m_files.begin(); it != m_files.end(); ++it ) {
        if( (*it)->groupId() == g2 || (*it)->groupId() == g1 ) (*it)->setGroupId( ng );
    }
    return ng;
}

template<class Type> static void dumpMap( std::ostream& file, QMap<QString, Type>& map ) {
    typename QMap<QString, Type>::Iterator it = map.begin();
    for( ; it != map.end(); ++it) {
        typename Type::Iterator it2 = (*it).begin();
        for( ; it2 != (*it).end(); ++it2) {
            (*it2) -> dump( file, true );
        }
    }
}

template<class Type> static void dumpMapDirect( std::ostream& file, QMap<QString, Type>& map ) {
    typename QMap<QString, Type>::Iterator it = map.begin();
    for( ; it != map.end(); ++it) {
        (*it) -> dump( file, true );
    }
}

void CodeModelItem::dump( std::ostream& file, bool recurse, QString Info ) 
{
    ostringstream str( ostringstream::out );
    
    str << "name: " << name().ascii() << "\n";
    str << "kind: " << m_kind << "  ";
    
    if( isFile() ) str << "isFile ";
    if( isNamespace() ) str << "isNamespace ";
    if( isClass() ) str << "isClass ";
    if( isFunction() ) str << "isFunction ";
    if( isFunctionDefinition() ) str << "isFunctionDefinition ";
    if( isVariable() ) str << "isVariable ";
    if( isArgument() ) str << "isArgument ";
    if( isEnum() ) str << "isEnum ";
    if( isEnumerator() ) str << "isEnumerator ";
    if( isTypeAlias() ) str << "isTypeAlias ";
    if( isCustom() ) str << "isCustom ";
    str << "\n";
    str << "File: " << fileName().ascii() << " ";
    int line, col;
    getStartPosition( &line, &col );
    str << "s:(" << line << ", " << col << ") ";
    getEndPosition( &line, &col );
    str << "e:(" << line << ", " << col << ")\n";
    
    
    Info.prepend( str.str().c_str() );
    
    file << Info.ascii() << "\n";
    if(recurse) {} ///just to get rid of the warning
}

void ClassModel::dump( std::ostream& file, bool recurse, QString Info ) 
{
    ostringstream str( ostringstream::out );
    
    
    str << "scope: " << m_scope.join("::").ascii() << "\n";
    str << "bases: " << m_baseClassList.join(" ").ascii() << "\n";
    
    Info.prepend( str.str().c_str() );
    
    CodeModelItem::dump( file, false, Info );
    
    if( recurse ) {
        dumpMap( file, m_classes );
    }
}

void NamespaceAliasModel::read( QDataStream& stream ) {
  QString tempFileName;
  stream >> m_name >> m_aliasName >> tempFileName;
  m_fileName = HashedString( tempFileName );
}

void NamespaceAliasModel::write( QDataStream& stream ) const {
  stream << m_name << m_aliasName << m_fileName.str();
}

void NamespaceImportModel::read( QDataStream& stream ) {
  QString tempFileName;
  stream >> m_name >> tempFileName;
  m_fileName = HashedString( tempFileName );
}

void NamespaceImportModel::write( QDataStream& stream ) const {
  stream << m_name << m_fileName.str();
}

void NamespaceModel::dump( std::ostream& file, bool recurse, QString Info ) 
{
    ostringstream str( ostringstream::out );

    Info.prepend( str.str().c_str() );

    ClassModel::dump( file, false, Info );

    if( recurse ) {
        dumpMapDirect( file, m_namespaces );
    }
}    

void ArgumentModel::dump( std::ostream& file, bool recurse, QString Info ) 
{
    ostringstream str( ostringstream::out );

    str << "type: " << m_type.ascii() << " default: " << m_defaultValue.ascii() << "\n";
    
    Info.prepend( str.str().c_str() );

    CodeModelItem::dump( file, false, Info );
    
    if(recurse) {} ///just to get rid of the warning
}

void FunctionModel::dump( std::ostream& file, bool recurse, QString Info ) 
{
    ostringstream str( ostringstream::out );

    str << "access: " << m_access;
        
    str << " scope: " << m_scope.join("::").ascii() << "\n";
        
    if(isAbstract()) str << "isAbstract ";
    if(isConstant()) str << "isConstant ";
    if(isFunction()) str << "isFunction ";
    if(isInline()) str << "isInline ";
    if(isSignal()) str << "isSignal ";
    if(isSlot()) str << "isSlot ";
    if(isStatic()) str << "isStatic ";
    if(isVirtual()) str << "isVirtual ";
        
    str << "\n";
    str << "result-type: " << resultType().ascii() << "\n";
        
    Info.prepend( str.str().c_str() );

    CodeModelItem::dump( file, false, Info );

    if(recurse) {
        for( ArgumentList::iterator it = m_arguments.begin(); it != m_arguments.end(); ++it) {
            (*it) -> dump( file, true );
        }
    }
}

void VariableModel::dump( std::ostream& file, bool recurse, QString Info ) 
{
    ostringstream str( ostringstream::out );

    str << "access: " << m_access << "type: " << m_type.ascii() << "\n";
    
    if(isStatic()) str << "isStatic ";

    str << "\n";

    Info.prepend( str.str().c_str() );

    CodeModelItem::dump( file, false, Info );
    
    if(recurse) {} ///just to get rid of the warning
}

void CodeModel::dump( std::ostream& file, QString Info ) {
    ostringstream str(ostringstream::out);
            
    Info.prepend( str.str().c_str() );
    
    file << Info.ascii() << "\n";
    
    QMap<QString, FileDom>::iterator it = m_files.begin();
    for(; it != m_files.end(); ++it) {
        (*it) -> dump( file, true );
    }
}

void EnumModel::dump( std::ostream& file, bool recurse, QString Info ) 
{
    ostringstream str( ostringstream::out );

    str << "access: " << m_access << "\n";
    
    Info.prepend( str.str().c_str() );

    CodeModelItem::dump( file, false, Info );
    
    if( recurse ) {
        dumpMapDirect( file, m_enumerators );
    }
}

void EnumeratorModel::dump( std::ostream& file, bool recurse, QString Info ) 
{
    ostringstream str( ostringstream::out );

    str << "value: " << m_value.ascii() << "\n";
    
    Info.prepend( str.str().c_str() );

    CodeModelItem::dump( file, false, Info );
    
    if(recurse) {} ///just to get rid of the warning
}

void TypeAliasModel::dump( std::ostream& file, bool recurse, QString Info ) {
    ostringstream str( ostringstream::out );

    str << "type: " << m_type.ascii() << "\n";
    
    Info.prepend( str.str().c_str() );

    CodeModelItem::dump( file, false, Info );
    
    if(recurse) {} ///just to get rid of the warning
}

void CodeModel::wipeout()
{
    m_files.clear();
    NamespaceDom ns = create<NamespaceModel>();
    ns->setName( "::" );

    m_globalNamespace = ns;
}

FileList CodeModel::fileList( )
{
    return m_files.values();
}

const FileList CodeModel::fileList( ) const
{
    return m_files.values();
}

bool CodeModel::hasFile( const QString & name ) const
{
    return m_files.contains( name );
}

FileDom CodeModel::fileByName( const QString & name )
{
    QMap<QString, FileDom>::const_iterator it = m_files.find( name );
    if( it != m_files.end() ) {
      return *it;
    } else {
      return FileDom();
    }
}

const FileDom CodeModel::fileByName( const QString & name ) const
{
    QMap<QString, FileDom>::const_iterator it = m_files.find( name );
    if( it != m_files.end() ) {
      return *it;
    } else {
      return FileDom();
    }
}

void CodeModel::addNamespace( NamespaceDom target, NamespaceDom source )
{
    if( source->name().isEmpty() ){
	return;
    } else if( !target->hasNamespace(source->name()) ){
	NamespaceDom ns = this->create<NamespaceModel>();
	ns->setName( source->name() );
	ns->setFileName( source->fileName() ); /// \FIXME ROBE
	ns->setScope( source->scope() );
	target->addNamespace( ns );
    }

    NamespaceDom ns = target->namespaceByName( source->name() );

    NamespaceList namespaceList = source->namespaceList();
    ClassList classList = source->classList();
    FunctionList functionList = source->functionList();
    FunctionDefinitionList functionDefinitionList = source->functionDefinitionList();
    VariableList variableList = source->variableList();
    EnumList enumList = source->enumList();
    TypeAliasList typeAliasList = source->typeAliasList();
    const NamespaceModel::NamespaceAliasModelList& namespaceAliases = source->namespaceAliases();
    const NamespaceModel::NamespaceImportModelList& namespaceImports = source->namespaceImports();

    for( NamespaceList::Iterator it=namespaceList.begin(); it!=namespaceList.end(); ++it )
	addNamespace( ns, *it );
    for( ClassList::Iterator it=classList.begin(); it!=classList.end(); ++it )
	ns->addClass( *it );
    for( FunctionList::Iterator it=functionList.begin(); it!=functionList.end(); ++it )
	ns->addFunction( *it );
    for( FunctionDefinitionList::Iterator it=functionDefinitionList.begin(); it!=functionDefinitionList.end(); ++it )
	ns->addFunctionDefinition( *it );
    for( VariableList::Iterator it=variableList.begin(); it!=variableList.end(); ++it )
	ns->addVariable( *it );
    for( EnumList::Iterator it=enumList.begin(); it!=enumList.end(); ++it )
	ns->addEnum( *it );
    for( TypeAliasList::Iterator it=typeAliasList.begin(); it!=typeAliasList.end(); ++it )
	ns->addTypeAlias( *it );
  for( NamespaceModel::NamespaceAliasModelList::const_iterator it=namespaceAliases.begin(); it != namespaceAliases.end(); ++it )
    ns->addNamespaceAlias( *it );
  for( NamespaceModel::NamespaceImportModelList::const_iterator it=namespaceImports.begin(); it != namespaceImports.end(); ++it )
    ns->addNamespaceImport( *it );
}

void CodeModel::removeNamespace( NamespaceDom target, NamespaceDom source )
{
    if( source->name().isEmpty() || !target->hasNamespace(source->name()) )
	return;

    NamespaceDom ns = target->namespaceByName( source->name() );

    NamespaceList namespaceList = source->namespaceList();
    ClassList classList = source->classList();
    FunctionList functionList = source->functionList();
    FunctionDefinitionList functionDefinitionList = source->functionDefinitionList();
    VariableList variableList = source->variableList();
    EnumList enumList = source->enumList();
    TypeAliasList typeAliasList = source->typeAliasList();
    const NamespaceModel::NamespaceAliasModelList& namespaceAliases = source->namespaceAliases();
    const NamespaceModel::NamespaceImportModelList& namespaceImports = source->namespaceImports();
  
    for( NamespaceList::Iterator it=namespaceList.begin(); it!=namespaceList.end(); ++it )
	removeNamespace( ns, *it );
    for( ClassList::Iterator it=classList.begin(); it!=classList.end(); ++it )
	ns->removeClass( *it );
    for( FunctionList::Iterator it=functionList.begin(); it!=functionList.end(); ++it )
	ns->removeFunction( *it );
    for( FunctionDefinitionList::Iterator it=functionDefinitionList.begin(); it!=functionDefinitionList.end(); ++it )
	ns->removeFunctionDefinition( *it );
    for( VariableList::Iterator it=variableList.begin(); it!=variableList.end(); ++it )
	ns->removeVariable( *it );
    for( EnumList::Iterator it=enumList.begin(); it!=enumList.end(); ++it )
	ns->removeEnum( *it );
    for( TypeAliasList::Iterator it=typeAliasList.begin(); it!=typeAliasList.end(); ++it )
	ns->removeTypeAlias( *it );
  for( NamespaceModel::NamespaceAliasModelList::const_iterator it=namespaceAliases.begin(); it != namespaceAliases.end(); ++it )
    ns->removeNamespaceAlias( *it );
  for( NamespaceModel::NamespaceImportModelList::const_iterator it=namespaceImports.begin(); it != namespaceImports.end(); ++it )
    ns->removeNamespaceImport( *it );
  
    if( ns->namespaceList().isEmpty() && 
    	ns->classList().isEmpty() && 
	ns->functionList().isEmpty() && 
	ns->functionDefinitionList().isEmpty() && 
	ns->variableList().isEmpty() &&
	ns->enumList().isEmpty() && 
	ns->typeAliasList().isEmpty() &&
  ns->namespaceImports().empty() &&
  ns->namespaceAliases().empty() )
    {
        target->removeNamespace( ns );
    }
}

bool CodeModel::addFile( FileDom file )
{
    if( file->name().isEmpty() )
		return false;

		if( m_files.find( file->name() ) != m_files.end() ) {
			///the error-channel is set to 9007 because this problem appears with the cpp-support, so it is needed while debugging it
			kdDebug(9007) << "file " << file->name() << " was added to code-model without removing it before! \n" << kdBacktrace() << endl;
			removeFile( fileByName( file->name() ) );
		}

    // update global namespace
    NamespaceList namespaceList = file->namespaceList();
    ClassList classList = file->classList();
    FunctionList functionList = file->functionList();
    FunctionDefinitionList functionDefinitionList = file->functionDefinitionList();
    VariableList variableList = file->variableList();
    EnumList enumList = file->enumList();
    TypeAliasList typeAliasList = file->typeAliasList();
    const NamespaceModel::NamespaceAliasModelList& namespaceAliases = file->namespaceAliases();
    const NamespaceModel::NamespaceImportModelList& namespaceImports = file->namespaceImports();
  
    for( NamespaceList::Iterator it=namespaceList.begin(); it!=namespaceList.end(); ++it )
	addNamespace( m_globalNamespace, *it );
    for( ClassList::Iterator it=classList.begin(); it!=classList.end(); ++it )
	m_globalNamespace->addClass( *it );
    for( FunctionList::Iterator it=functionList.begin(); it!=functionList.end(); ++it )
	m_globalNamespace->addFunction( *it );
    for( FunctionDefinitionList::Iterator it=functionDefinitionList.begin(); it!=functionDefinitionList.end(); ++it )
	m_globalNamespace->addFunctionDefinition( *it );
    for( VariableList::Iterator it=variableList.begin(); it!=variableList.end(); ++it )
	m_globalNamespace->addVariable( *it );
    for( EnumList::Iterator it=enumList.begin(); it!=enumList.end(); ++it )
	m_globalNamespace->addEnum( *it );
    for( TypeAliasList::Iterator it=typeAliasList.begin(); it!=typeAliasList.end(); ++it )
	m_globalNamespace->addTypeAlias( *it );
  for( NamespaceModel::NamespaceAliasModelList::const_iterator it=namespaceAliases.begin(); it != namespaceAliases.end(); ++it )
    m_globalNamespace->addNamespaceAlias( *it );
  for( NamespaceModel::NamespaceImportModelList::const_iterator it=namespaceImports.begin(); it != namespaceImports.end(); ++it )
    m_globalNamespace->addNamespaceImport( *it );
  
    m_files.insert( file->name(), file );
    return true;
}

void CodeModel::removeFile( FileDom file )
{
    // update global namespace
    NamespaceList namespaceList = file->namespaceList();
    ClassList classList = file->classList();
    FunctionList functionList = file->functionList();
    FunctionDefinitionList functionDefinitionList = file->functionDefinitionList();
    VariableList variableList = file->variableList();
    EnumList enumList = file->enumList();
    TypeAliasList typeAliasList = file->typeAliasList();
    const NamespaceModel::NamespaceAliasModelList& namespaceAliases = file->namespaceAliases();
    const NamespaceModel::NamespaceImportModelList& namespaceImports = file->namespaceImports();
  
    for( NamespaceList::Iterator it=namespaceList.begin(); it!=namespaceList.end(); ++it )
	removeNamespace( m_globalNamespace, *it );
    for( ClassList::Iterator it=classList.begin(); it!=classList.end(); ++it )
	m_globalNamespace->removeClass( *it );
    for( FunctionList::Iterator it=functionList.begin(); it!=functionList.end(); ++it )
	m_globalNamespace->removeFunction( *it );
    for( FunctionDefinitionList::Iterator it=functionDefinitionList.begin(); it!=functionDefinitionList.end(); ++it )
	m_globalNamespace->removeFunctionDefinition( *it );
    for( VariableList::Iterator it=variableList.begin(); it!=variableList.end(); ++it )
	m_globalNamespace->removeVariable( *it );
    for( EnumList::Iterator it=enumList.begin(); it!=enumList.end(); ++it )
	m_globalNamespace->removeEnum( *it );
    for( TypeAliasList::Iterator it=typeAliasList.begin(); it!=typeAliasList.end(); ++it )
	m_globalNamespace->removeTypeAlias( *it );
  for( NamespaceModel::NamespaceAliasModelList::const_iterator it=namespaceAliases.begin(); it != namespaceAliases.end(); ++it )
    m_globalNamespace->removeNamespaceAlias( *it );
  for( NamespaceModel::NamespaceImportModelList::const_iterator it=namespaceImports.begin(); it != namespaceImports.end(); ++it )
    m_globalNamespace->removeNamespaceImport( *it );
  
    m_files.remove( file->name() );
}

// ------------------------------------------------------------------------
CodeModelItem::CodeModelItem( int kind, CodeModel* model )
    : m_kind( kind ), m_model( model )
{
    //kdDebug() << "CodeModelItem::CodeModelItem()" << endl;
    m_startLine = 0;
    m_startColumn = 0;
    m_endLine = 0;
    m_endColumn = 0;
}

CodeModelItem::~ CodeModelItem( )
{
}

QString CodeModelItem::name( ) const
{
    return m_name;
}

void CodeModelItem::setName( const QString & name )
{
    m_name = name;
}

const FileDom CodeModelItem::file( ) const
{
    return m_model->fileByName( m_fileName );
}

FileDom CodeModelItem::file( )
{
    return m_model->fileByName( m_fileName );
}

QString CodeModelItem::fileName() const
{
    return m_fileName;
}

void CodeModelItem::setFileName( const QString& fileName )
{
    m_fileName = fileName;
}

void CodeModelItem::getStartPosition( int * line, int * column ) const
{
    if( line ) *line = m_startLine;
    if( column ) *column = m_startColumn;
}

void CodeModelItem::setStartPosition( int line, int column )
{
    m_startLine = line;
    m_startColumn = column;
}

void CodeModelItem::getEndPosition( int * line, int * column ) const
{
    if( line ) *line = m_endLine;
    if( column ) *column = m_endColumn;
}

void CodeModelItem::setEndPosition( int line, int column )
{
    m_endLine = line;
    m_endColumn = column;
}

void CodeModelItem::update( const CodeModelItem* i ) {
  m_startLine = i->m_startLine;
  m_startColumn = i->m_startColumn;
  m_endLine = i->m_endLine;
  m_endColumn = i->m_endColumn;
}

bool CodeModelItem::canUpdate( const CodeModelItem* i ) const {
  if( i->m_kind != m_kind || i->m_name != m_name )
    return false;
  return true;
}


// ------------------------------------------------------------------------
NamespaceModel::NamespaceModel( CodeModel* model )
    : ClassModel( model )
{
    setKind( Namespace );
}

NamespaceList NamespaceModel::namespaceList( )
{
    return m_namespaces.values();
}

const NamespaceList NamespaceModel::namespaceList( ) const
{
    return m_namespaces.values();
}

NamespaceDom NamespaceModel::namespaceByName( const QString & name )
{
    return m_namespaces.contains( name ) ? m_namespaces[ name ] : NamespaceDom();
}

const NamespaceDom NamespaceModel::namespaceByName( const QString & name ) const
{
    return m_namespaces.contains( name ) ? m_namespaces[ name ] : NamespaceDom();
}

bool NamespaceModel::hasNamespace( const QString & name ) const
{
    return m_namespaces.contains( name );
}

bool NamespaceModel::addNamespace( NamespaceDom ns )
{
    if( ns->name().isEmpty() )
	return false;

    m_namespaces[ ns->name() ] = ns;
    return true;
}

void NamespaceModel::removeNamespace( NamespaceDom ns )
{
    m_namespaces.remove( ns->name() );
}

// ------------------------------------------------------------------------
FileModel::FileModel( CodeModel* model )
    : NamespaceModel( model ), m_groupId( model->newGroupId() ), m_parseResult( 0 )
{
}

// ------------------------------------------------------------------------
ClassModel::ClassModel( CodeModel* model )
    : CodeModelItem( Class, model)
{
}

QStringList ClassModel::baseClassList( ) const
{
    return m_baseClassList;
}

bool ClassModel::addBaseClass( const QString & baseClass )
{
    m_baseClassList.push_back( baseClass );
    return true;
}

void ClassModel::removeBaseClass( const QString & baseClass )
{
    m_baseClassList.remove( baseClass );
}

ClassList ClassModel::classList( )
{
    ClassList l;
    QMap<QString, ClassList>::Iterator it = m_classes.begin();
    while( it != m_classes.end() ){
	l += *it;
	++it;
    }

    return l;
}

const ClassList ClassModel::classList( ) const
{
    ClassList l;
    QMap<QString, ClassList>::ConstIterator it = m_classes.begin();
    while( it != m_classes.end() ){
	l += *it;
	++it;
    }

    return l;
}

bool ClassModel::hasClass( const QString & name ) const
{
    return m_classes.contains( name );
}

ClassList ClassModel::classByName( const QString & name )
{
    return m_classes.contains( name ) ? m_classes[ name ] : ClassList();
}

const ClassList ClassModel::classByName( const QString & name ) const
{
    return m_classes.contains( name ) ? m_classes[ name ] : ClassList();
}

bool ClassModel::addClass( ClassDom klass )
{
    if( klass->name().isEmpty() )
	return false;

    m_classes[ klass->name() ].push_back( klass );
    return true;
}

void ClassModel::removeClass( ClassDom klass )
{
    m_classes[ klass->name() ].remove( klass );

    if( m_classes[klass->name()].isEmpty() )
	m_classes.remove( klass->name() );
}

FunctionList ClassModel::functionList( )
{
    FunctionList l;
    QMap<QString, FunctionList>::Iterator it = m_functions.begin();
    while( it != m_functions.end() ){
	l += *it;
	++it;
    }

    return l;
}

const FunctionList ClassModel::functionList( ) const
{
    FunctionList l;
    QMap<QString, FunctionList>::ConstIterator it = m_functions.begin();
    while( it != m_functions.end() ){
	l += *it;
	++it;
    }

    return l;
}

bool ClassModel::hasFunction( const QString & name ) const
{
    return m_functions.contains( name );
}

FunctionList ClassModel::functionByName( const QString & name )
{
    return m_functions.contains( name ) ? m_functions[ name ] : FunctionList();
}

const FunctionList ClassModel::functionByName( const QString & name ) const
{
    return m_functions.contains( name ) ? m_functions[ name ] : FunctionList();
}

bool ClassModel::addFunction( FunctionDom fun )
{
    if( fun->name().isEmpty() )
	return false;

    m_functions[ fun->name() ].push_back( fun );
    return true;
}

void ClassModel::removeFunction( FunctionDom fun )
{
    m_functions[ fun->name() ].remove( fun );

    if( m_functions[fun->name()].isEmpty() )
	m_functions.remove( fun->name() );
}

FunctionDefinitionList ClassModel::functionDefinitionList( )
{
    FunctionDefinitionList l;
    QMap<QString, FunctionDefinitionList>::Iterator it = m_functionDefinitions.begin();
    while( it != m_functionDefinitions.end() ){
	l += *it;
	++it;
    }

    return l;
}

const FunctionDefinitionList ClassModel::functionDefinitionList( ) const
{
    FunctionDefinitionList l;
    QMap<QString, FunctionDefinitionList>::ConstIterator it = m_functionDefinitions.begin();
    while( it != m_functionDefinitions.end() ){
	l += *it;
	++it;
    }

    return l;
}

bool ClassModel::hasFunctionDefinition( const QString & name ) const
{
    return m_functionDefinitions.contains( name );
}

FunctionDefinitionList ClassModel::functionDefinitionByName( const QString & name )
{
    return m_functionDefinitions.contains( name ) ? m_functionDefinitions[ name ] : FunctionDefinitionList();
}

const FunctionDefinitionList ClassModel::functionDefinitionByName( const QString & name ) const
{
    return m_functionDefinitions.contains( name ) ? m_functionDefinitions[ name ] : FunctionDefinitionList();
}

bool ClassModel::addFunctionDefinition( FunctionDefinitionDom fun )
{
    if( fun->name().isEmpty() )
	return false;

    m_functionDefinitions[ fun->name() ].push_back( fun );
    return true;
}

void ClassModel::removeFunctionDefinition( FunctionDefinitionDom fun )
{
    m_functionDefinitions[ fun->name() ].remove( fun );

    if( m_functionDefinitions[fun->name()].isEmpty() )
	m_functionDefinitions.remove( fun->name() );
}

VariableList ClassModel::variableList( )
{
    return m_variables.values();
}

const VariableList ClassModel::variableList( ) const
{
    return m_variables.values();
}

VariableDom ClassModel::variableByName( const QString & name )
{
    return m_variables.contains( name ) ? m_variables[ name ] : VariableDom();
}

const VariableDom ClassModel::variableByName( const QString & name ) const
{
    return m_variables.contains( name ) ? m_variables[ name ] : VariableDom();
}

bool ClassModel::hasVariable( const QString & name ) const
{
    return m_variables.contains( name );
}

bool ClassModel::addVariable( VariableDom var )
{
    if( var->name().isEmpty() )
	return false;

    m_variables.insert( var->name(), var );
    return true;
}

void ClassModel::removeVariable( VariableDom var )
{
    m_variables.remove( var->name() );
}

EnumList ClassModel::enumList( )
{
    return m_enumerators.values();
}

const EnumList ClassModel::enumList( ) const
{
    return m_enumerators.values();
}

EnumDom ClassModel::enumByName( const QString & name )
{
    return m_enumerators.contains( name ) ? m_enumerators[ name ] : EnumDom();
}

const EnumDom ClassModel::enumByName( const QString & name ) const
{
    return m_enumerators.contains( name ) ? m_enumerators[ name ] : EnumDom();
}

bool ClassModel::hasEnum( const QString & name ) const
{
    return m_enumerators.contains( name );
}

bool ClassModel::addEnum( EnumDom e )
{
    if( e->name().isEmpty() )
	return false;

    m_enumerators.insert( e->name(), e );
    return true;
}

void ClassModel::update( const ClassModel* klass ) {
  CodeModelItem::update( klass );
  eachUpdate( m_classes, klass->m_classes ) ;
  eachUpdate( m_functions, klass->m_functions ) ;
  eachUpdate( m_functionDefinitions, klass->m_functionDefinitions ) ;
  eachUpdateSingle( m_variables, klass->m_variables ) ;
  eachUpdateSingle( m_enumerators, klass->m_enumerators ) ;
  eachUpdate( m_typeAliases, klass->m_typeAliases );
}

bool ClassModel::canUpdate( const ClassModel* klass ) const {
    if( !CodeModelItem::canUpdate( klass ) )
      return false;
    
    return eachCanUpdate( m_classes, klass->m_classes ) &&
        eachCanUpdate( m_functions, klass->m_functions ) &&
        eachCanUpdate( m_functionDefinitions, klass->m_functionDefinitions ) &&
        eachCanUpdateSingle( m_variables, klass->m_variables ) &&
        eachCanUpdateSingle( m_enumerators, klass->m_enumerators ) &&
        eachCanUpdate( m_typeAliases, klass->m_typeAliases );
}

void ClassModel::removeEnum( EnumDom e )
{
    m_enumerators.remove( e->name() );
}

TypeAliasList ClassModel::typeAliasList( )
{
    TypeAliasList l;
    QMap<QString, TypeAliasList>::Iterator it = m_typeAliases.begin();
    while( it != m_typeAliases.end() ){
	l += *it;
	++it;
    }

    return l;
}

const TypeAliasList ClassModel::typeAliasList( ) const
{
    TypeAliasList l;
    QMap<QString, TypeAliasList>::ConstIterator it = m_typeAliases.begin();
    while( it != m_typeAliases.end() ){
	l += *it;
	++it;
    }

    return l;
}

bool ClassModel::hasTypeAlias( const QString & name ) const
{
    return m_typeAliases.contains( name );
}

TypeAliasList ClassModel::typeAliasByName( const QString & name )
{
    return m_typeAliases.contains( name ) ? m_typeAliases[ name ] : TypeAliasList();
}

const TypeAliasList ClassModel::typeAliasByName( const QString & name ) const
{
    return m_typeAliases.contains( name ) ? m_typeAliases[ name ] : TypeAliasList();
}

bool ClassModel::addTypeAlias( TypeAliasDom typeAlias )
{
    if( typeAlias->name().isEmpty() )
	return false;

    m_typeAliases[ typeAlias->name() ].push_back( typeAlias );
    return true;
}

void ClassModel::removeTypeAlias( TypeAliasDom typeAlias )
{
    m_typeAliases[ typeAlias->name() ].remove( typeAlias );

    if( m_typeAliases[typeAlias->name()].isEmpty() )
	m_typeAliases.remove( typeAlias->name() );
}



// ------------------------------------------------------------------------
ArgumentModel::ArgumentModel( CodeModel* model )
    : CodeModelItem( Argument, model)
{
}

QString ArgumentModel::type( ) const
{
    return m_type;
}

void ArgumentModel::setType( const QString& type )
{
    m_type = type;
}

QString ArgumentModel::defaultValue( ) const
{
    return m_defaultValue;
}

void ArgumentModel::setDefaultValue( const QString & defaultValue )
{
    m_defaultValue = defaultValue;
}

// ------------------------------------------------------------------------
FunctionModel::FunctionModel( CodeModel* model )
    : CodeModelItem( Function, model)
{
    m_access = Public;
    d.v.m_signal = false;
    d.v.m_slot = false;
    d.v.m_virtual = false;
    d.v.m_static = false;
    d.v.m_inline = false;
    d.v.m_constant = false;
    d.v.m_abstract = false;
}

bool FunctionModel::isVirtual( ) const
{
    return d.v.m_virtual;
}

void FunctionModel::setVirtual( bool isVirtual )
{
    d.v.m_virtual = isVirtual;
}

bool FunctionModel::isStatic( ) const
{
    return d.v.m_static;
}

void FunctionModel::setStatic( bool isStatic )
{
    d.v.m_static = isStatic;
}

bool FunctionModel::isInline( ) const
{
    return d.v.m_inline;
}

void FunctionModel::setInline( bool isInline )
{
    d.v.m_inline = isInline;
}

bool FunctionModel::isConstant( ) const
{
    return d.v.m_constant;
}

void FunctionModel::setConstant( bool isConstant )
{
    d.v.m_constant = isConstant;
}

bool FunctionModel::isAbstract( ) const
{
    return d.v.m_abstract;
}

void FunctionModel::setAbstract( bool isAbstract )
{
    d.v.m_abstract = isAbstract;
}

QString FunctionModel::resultType( ) const
{
    return m_resultType;
}

void FunctionModel::setResultType( const QString& type )
{
    m_resultType = type;
}

ArgumentList FunctionModel::argumentList( )
{
    return m_arguments;
}

const ArgumentList FunctionModel::argumentList( ) const
{
    return m_arguments;
}

bool FunctionModel::addArgument( ArgumentDom arg )
{
    m_arguments.push_back( arg );
    return true;
}

void FunctionModel::removeArgument( ArgumentDom arg )
{
    m_arguments.remove( arg );
}

void FunctionModel::update( const FunctionModel* i ) {
  m_access = i->m_access;
  CodeModelItem::update( i );
}

bool FunctionModel::canUpdate( const FunctionModel* i ) const {
  if( !CodeModelItem::canUpdate( i ) )
    return false;
  if( m_resultType != i->m_resultType || m_arguments.count() != i->m_arguments.count() || m_scope != i->m_scope )
    return false;
  return true;
}


// ------------------------------------------------------------------------
VariableModel::VariableModel( CodeModel* model )
    : CodeModelItem( Variable, model)
{
    m_access = Public;
    m_static = false;
		m_isEnumeratorVariable = false;
}

bool VariableModel::isStatic( ) const
{
    return m_static;
}

void VariableModel::setStatic( bool isStatic )
{
    m_static = isStatic;
}

QString VariableModel::type( ) const
{
    return m_type;
}

void VariableModel::setType( const QString& type )
{
    m_type = type;
}

bool VariableModel::isEnumeratorVariable() const {
	return m_isEnumeratorVariable;
}

void VariableModel::setEnumeratorVariable( bool b) {
	m_isEnumeratorVariable = b;
}

int FunctionModel::access( ) const
{
    return m_access;
}

void FunctionModel::setAccess( int access )
{
    m_access = access;
}

bool FunctionModel::isSignal( ) const
{
    return d.v.m_signal;
}

void FunctionModel::setSignal( bool isSignal )
{
    d.v.m_signal = isSignal;
}

bool FunctionModel::isSlot( ) const
{
    return d.v.m_slot;
}

void FunctionModel::setSlot( bool isSlot )
{
    d.v.m_slot = isSlot;
}

FunctionDefinitionModel::FunctionDefinitionModel( CodeModel* model )
    : FunctionModel( model )
{
}

int VariableModel::access( ) const
{
    return m_access;
}

void VariableModel::setAccess( int access )
{
    m_access = access;
}

const NamespaceDom CodeModel::globalNamespace( ) const
{
    return m_globalNamespace;
}

void CodeModelItem::read( QDataStream & stream )
{
    stream
	>> m_kind
	>> m_name
	>> m_fileName
	>> m_startLine
	>> m_startColumn
	>> m_endLine
	>> m_endColumn
        >> m_comment;
    
    if( isTemplateable() ) {
        TemplateModelItem* t = (TemplateModelItem*)( this );
        
        t->read( stream );
    }
}

void CodeModelItem::write( QDataStream & stream ) const
{
    stream
	<< m_kind
	<< m_name
	<< m_fileName
	<< m_startLine
	<< m_startColumn
	<< m_endLine
	<< m_endColumn
        << m_comment;
    
    if( isTemplateable() ) {
        TemplateModelItem* t = (TemplateModelItem*)( this );
        t-> write( stream );
    }
}

void ClassModel::read( QDataStream & stream )
{
    CodeModelItem::read( stream );

		TemplateModelItem::read( stream );
		
    stream >> m_scope >> m_baseClassList;

    int n;

    m_classes.clear();
    stream >> n;
    for( int i=0; i<n; ++i ){
	ClassDom klass = codeModel()->create<ClassModel>();
	klass->read( stream );
	addClass( klass );
    }

    m_functions.clear();
    stream >> n;
    for( int i=0; i<n; ++i ){
	FunctionDom fun = codeModel()->create<FunctionModel>();
	fun->read( stream );
	addFunction( fun );
    }

    m_functionDefinitions.clear();
    stream >> n;
    for( int i=0; i<n; ++i ){
	FunctionDefinitionDom fun = codeModel()->create<FunctionDefinitionModel>();
	fun->read( stream );
	addFunctionDefinition( fun );
    }

    m_variables.clear();
    stream >> n;
    for( int i=0; i<n; ++i ){
	VariableDom var = codeModel()->create<VariableModel>();
	var->read( stream );
	addVariable( var );
    }
    
    m_enumerators.clear();
    stream >> n;
    for( int i=0; i<n; ++i ){
	EnumDom e = codeModel()->create<EnumModel>();
	e->read( stream );
	addEnum( e );
    }

    m_typeAliases.clear();
    stream >> n;
    for( int i=0; i<n; ++i ){
	TypeAliasDom typeAlias = codeModel()->create<TypeAliasModel>();
	typeAlias->read( stream );
	addTypeAlias( typeAlias );
    }
}

void ClassModel::write( QDataStream & stream ) const
{
    CodeModelItem::write( stream );

		TemplateModelItem::write( stream );
		
    stream << m_scope << m_baseClassList;

    const ClassList class_list = classList();
    stream << int( class_list.size() );
    for( ClassList::ConstIterator it = class_list.begin(); it!=class_list.end(); ++it )
	(*it)->write( stream );

    const FunctionList function_list = functionList();
    stream << int( function_list.size() );
    for( FunctionList::ConstIterator it = function_list.begin(); it!=function_list.end(); ++it )
	(*it)->write( stream );

    const FunctionDefinitionList function_definition_list = functionDefinitionList();
    stream << int( function_definition_list.size() );
    for( FunctionDefinitionList::ConstIterator it = function_definition_list.begin(); it!=function_definition_list.end(); ++it )
	(*it)->write( stream );

    const VariableList variable_list = variableList();
    stream << int( variable_list.size() );
    for( VariableList::ConstIterator it = variable_list.begin(); it!=variable_list.end(); ++it )
	(*it)->write( stream );
    
    const EnumList enum_list = enumList();
    stream << int( enum_list.size() );
    for( EnumList::ConstIterator it = enum_list.begin(); it!=enum_list.end(); ++it )
	(*it)->write( stream );    
	
    const TypeAliasList type_alias_list = typeAliasList();
    stream << int( type_alias_list.size() );
    for( TypeAliasList::ConstIterator it = type_alias_list.begin(); it!=type_alias_list.end(); ++it )
	(*it)->write( stream );    
	
}

void NamespaceModel::read( QDataStream & stream )
{
    ClassModel::read( stream );

    int n;

    m_namespaces.clear(); m_namespaceAliases.clear(); m_namespaceImports.clear();
    stream >> n;
    for( int i=0; i<n; ++i ){
	NamespaceDom ns = codeModel()->create<NamespaceModel>();
	ns->read( stream );
	addNamespace( ns );
    }

  stream >> n;
  for( int a = 0; a < n; a++ ) {
    NamespaceAliasModel m;
    m.read( stream );
    m_namespaceAliases.insert( m );
  }
  stream >> n;
  for( int a = 0; a < n; a++ ) {
    NamespaceImportModel m;
    m.read( stream );
    m_namespaceImports.insert( m );
  }
}

void NamespaceModel::addNamespaceImport( const NamespaceImportModel& import ) {
  m_namespaceImports.insert( import );
}

void NamespaceModel::addNamespaceAlias( const NamespaceAliasModel& alias ) {
  m_namespaceAliases.insert( alias );
}

void NamespaceModel::removeNamespaceImport( const NamespaceImportModel& import ) {
  m_namespaceImports.erase( import );
}

void NamespaceModel::removeNamespaceAlias( const NamespaceAliasModel& alias ) {
  m_namespaceAliases.erase( alias );
}

void NamespaceModel::write( QDataStream & stream ) const
{
    ClassModel::write( stream );

    const NamespaceList namespace_list = namespaceList();
    stream << int( namespace_list.size() );
    for( NamespaceList::ConstIterator it = namespace_list.begin(); it!=namespace_list.end(); ++it )
	(*it)->write( stream );

  stream << int( m_namespaceAliases.size() );
  for( NamespaceAliasModelList::const_iterator it = m_namespaceAliases.begin(); it != m_namespaceAliases.end(); ++it )
    (*it).write( stream );
  stream << int( m_namespaceImports.size() );
  for( NamespaceImportModelList::const_iterator it = m_namespaceImports.begin(); it != m_namespaceImports.end(); ++it )
    (*it).write( stream );
}

bool NamespaceModel::canUpdate( const NamespaceModel* ns ) const {
  if( !ClassModel::canUpdate( ns ) )
    return false;
    
    const NamespaceAliasModelList& aliases = namespaceAliases();
    const NamespaceImportModelList& imports = namespaceImports();
    const NamespaceAliasModelList& aliases2 = ns->namespaceAliases();
    const NamespaceImportModelList& imports2 = ns->namespaceImports();

    if( aliases.size() != aliases2.size() ) return false;
    if( imports.size() != imports2.size() ) return false;

    ///Test if all aliases are same, if not return false
    NamespaceModel::NamespaceAliasModelList::const_iterator it_al1 = aliases.begin();
    NamespaceModel::NamespaceAliasModelList::const_iterator it_al2 = aliases2.begin();
    while( it_al1 != aliases.end() ) {
      if( !(*it_al1 == *it_al2) )
        return false;

      ++it_al1;
      ++it_al2;
    }
    
    ///Test if all imports are same, if not return false
    NamespaceModel::NamespaceImportModelList::const_iterator it_ip1 = imports.begin();
    NamespaceModel::NamespaceImportModelList::const_iterator it_ip2 = imports2.begin();
    while( it_ip1 != imports.end() ) {
      if( !(*it_ip1 == *it_ip2) )
        return false;

      ++it_ip1;
      ++it_ip2;
    }
    
    return eachCanUpdateSingle( m_namespaces, ns->m_namespaces );
}

void NamespaceModel::update( const NamespaceModel* ns )
{
  ClassModel::update( ns );

    eachUpdateSingle( m_namespaces, ns->m_namespaces );
}

void FileModel::read( QDataStream & stream )
{
    stream >> m_groupId;
    bool b;
    stream >> b;
    if( b ) {
      int i;
      stream >> i;
      ParsedFileType t( (ParsedFileType) i );
      switch( t ) {
      case CppParsedFile:
        m_parseResult = (AbstractParseResult*)(new ParsedFile( stream ));
        break;
      }
    }

    NamespaceModel::read( stream );
}

void FileModel::write( QDataStream & stream ) const
{
    stream << m_groupId;
    bool b = m_parseResult;
    stream << b;
    if( b ) {
        int i = m_parseResult->type();
        stream << i;
        m_parseResult->write( stream );
    }
    
    NamespaceModel::write( stream );
}

void ArgumentModel::read( QDataStream & stream )
{
    CodeModelItem::read( stream );
  

    stream >> m_type >> m_defaultValue;
}

void ArgumentModel::write( QDataStream & stream ) const
{
    CodeModelItem::write( stream );

    stream << m_type << m_defaultValue;
}

void FunctionModel::read( QDataStream & stream )
{
    CodeModelItem::read( stream );
		TemplateModelItem::read( stream );

    stream >> m_scope;
    stream >> d.flags;

    int n;

    m_arguments.clear();
    stream >> n;
    for( int i=0; i<n; ++i ){
	ArgumentDom arg = codeModel()->create<ArgumentModel>();
	arg->read( stream );
	addArgument( arg );
    }

    stream
	>> m_resultType;
}

void FunctionModel::write( QDataStream & stream ) const
{
    CodeModelItem::write( stream );
		TemplateModelItem::write( stream );

    stream << m_scope;
    stream << d.flags;

    const ArgumentList argument_list = argumentList();
    stream << int( argument_list.size() );
    for( ArgumentList::ConstIterator it = argument_list.begin(); it!=argument_list.end(); ++it )
	(*it)->write( stream );

    stream
	<< m_resultType;
}

void CodeModel::read( QDataStream & stream )
{
    int n;

    m_files.clear();

    stream >> n;
    for( int i=0; i<n; ++i ){
	FileDom file = this->create<FileModel>();
	file->read( stream );
	addFile( file );
    }
}

void CodeModel::write( QDataStream & stream ) const
{
    const FileList file_list = fileList();
    stream << int( file_list.size() );
    for( FileList::ConstIterator it = file_list.begin(); it!=file_list.end(); ++it )
	(*it)->write( stream );
}

void VariableModel::read( QDataStream & stream )
{
    CodeModelItem::read( stream );
		stream >> m_access >> m_static >> m_type >> m_isEnumeratorVariable;
}

void VariableModel::write( QDataStream & stream ) const
{
    CodeModelItem::write( stream );
		stream << m_access << m_static << m_type << m_isEnumeratorVariable;
}

void VariableModel::update( const VariableModel* i ) {
  m_access = i->m_access;
  CodeModelItem::update( i );
}

bool VariableModel::canUpdate( const VariableModel* i ) const {
  if( !CodeModelItem::canUpdate( i ) )
    return false;
  if( m_access != i->m_access || m_static != i->m_static || m_type != i->m_type || m_isEnumeratorVariable != i->m_isEnumeratorVariable )
    return false;
  return true;
}

// -------------------------------------------------------
EnumModel::EnumModel( CodeModel * model )
    : CodeModelItem( Enum, model)
{
}

int EnumModel::access( ) const
{
    return m_access;
}

void EnumModel::setAccess( int access )
{
    m_access = access;
}

EnumeratorList EnumModel::enumeratorList( )
{
    return m_enumerators.values();
}

const EnumeratorList EnumModel::enumeratorList( ) const
{
    return m_enumerators.values();
}

void EnumModel::addEnumerator( EnumeratorDom enumerator )
{
    m_enumerators.insert( enumerator->name(), enumerator );
}

void EnumModel::read( QDataStream & stream )
{
    CodeModelItem::read( stream );
    stream >> m_access;
    
    int n;
    stream >> n;
    for( int i=0; i<n; ++i ){
	EnumeratorDom e = codeModel()->create<EnumeratorModel>();
	e->read( stream );
	addEnumerator( e );
    }
}

void EnumModel::write( QDataStream & stream ) const
{
    CodeModelItem::write( stream );
    
    stream << m_access;
    const EnumeratorList enumerator_list = enumeratorList();
    stream << int( enumerator_list.size() );
    for( EnumeratorList::ConstIterator it = enumerator_list.begin(); it!=enumerator_list.end(); ++it )
	(*it)->write( stream );
}

EnumeratorModel::EnumeratorModel( CodeModel * model )
    : CodeModelItem( Enumerator, model )
{
}

QString EnumeratorModel::value( ) const
{
    return m_value;
}

void EnumeratorModel::setValue( const QString & value )
{
    m_value = value;
}

void EnumeratorModel::read( QDataStream & stream )
{
    CodeModelItem::read( stream );
    stream >> m_value;
}

void EnumeratorModel::write( QDataStream & stream ) const
{
    CodeModelItem::write( stream );
    stream << m_value;
}

void EnumModel::removeEnumerator( EnumeratorDom e )
{
    m_enumerators.remove( e->name() );
}

void EnumModel::update( const EnumModel* i ) {
  m_access = i->m_access;
  CodeModelItem::update( i );
}

bool EnumModel::canUpdate( const EnumModel* i ) const {
  if( !CodeModelItem::canUpdate( i ) )
    return false;
  ///@todo check not complete
  if( m_access != i->m_access || m_enumerators.count() != i->m_enumerators.count() )
    return false;
  return true;
}

// ---------------------------------------------------------------
TypeAliasModel::TypeAliasModel( CodeModel * model )
    : CodeModelItem( TypeAlias, model )
{
}

void TypeAliasModel::read( QDataStream & stream )
{
    CodeModelItem::read( stream );
    
    stream >> m_type;
}

void TypeAliasModel::write( QDataStream & stream ) const
{
    CodeModelItem::write( stream );
    
    stream << m_type;
}

QString TypeAliasModel::type( ) const
{
    return m_type;
}

void TypeAliasModel::setType( const QString & type )
{
    m_type = type;
}

void TypeAliasModel::update( const TypeAliasModel* i ) {
  CodeModelItem::update( i );
}

bool TypeAliasModel::canUpdate( const TypeAliasModel* i ) const {
  if( !CodeModelItem::canUpdate( i ) )
    return false;
  return m_type == i->m_type;
}

void FileModel::update( const FileModel* file ) {
  m_parseResult = file->m_parseResult;
  NamespaceModel::update( file );
}

FileList FileModel::wholeGroup() {
    if( isSingleGroup( m_groupId ) ) return ( FileList() << FileDom(this) );
    return codeModel()->getGroup( m_groupId );
}

QStringList FileModel::wholeGroupStrings() const {
    if( isSingleGroup( m_groupId ) ) return (QStringList() << name() );
    return codeModel()->getGroupStrings( m_groupId );
}

ParseResultPointer FileModel::parseResult() const {
    return m_parseResult;
}

void FileModel::setParseResult( const ParseResultPointer& result ) {
    m_parseResult = result;
}