/
CDataVector.h
1497 lines (1242 loc) · 40.7 KB
/
CDataVector.h
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
// Copyright (C) 2017 - 2018 by Pedro Mendes, Virginia Tech Intellectual
// Properties, Inc., University of Heidelberg, and University of
// of Connecticut School of Medicine.
// All rights reserved.
#ifndef COPASI_CDataVector
#define COPASI_CDataVector
#ifdef WIN32
# pragma warning (disable: 4786)
#endif // WIN32
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <iterator>
#include <cstddef>
#include "copasi/utilities/CCopasiMessage.h"
#include "copasi/utilities/utility.h"
#include "copasi/core/CRegisteredCommonName.h"
#include "copasi/core/CDataContainer.h"
#include "copasi/undo/CData.h"
#undef min
#undef max
#undef ERROR
class CReadConfig;
template <class CType> class CDataVector;
template <class CType>
std::ostream &operator<<(std::ostream &os, const CDataVector<CType> & d);
template < class CType > class CDataVector:
protected std::vector< CType * >, public CDataContainer
{
public:
typedef CDataObjectMap::type_iterator< CType > name_iterator;
typedef CDataObjectMap::const_type_iterator< CType > const_name_iterator;
public:
#ifndef SWIG
class iterator: public std::vector< CType * >::iterator
{
public:
iterator(): std::vector< CType * >::iterator() {}
iterator(const iterator & src): std::vector< CType * >::iterator(src) {}
iterator(const typename std::vector< CType * >::iterator & src): std::vector< CType * >::iterator(src) {}
~iterator() {}
CType & operator*() const
{
return *std::vector< CType * >::iterator::operator*();
}
CType * operator->() const
{
return std::vector< CType * >::iterator::operator*();
}
operator CType * () const
{
return std::vector< CType * >::iterator::operator*();
}
iterator & operator=(CType * pType)
{
std::vector< CType * >::iterator::operator*() = pType;
return *this;
}
iterator & operator++()
{
std::vector< CType * >::iterator::operator++();
return *this;
}
iterator operator++(int)
{
return iterator(std::vector< CType * >::iterator::operator++());
}
iterator & operator--()
{
std::vector< CType * >::iterator::operator--();
return *this;
}
iterator operator--(int)
{
return iterator(std::vector< CType * >::iterator::operator--());
}
iterator & operator+=(const typename std::vector< CType * >::iterator::difference_type & n)
{
std::vector< CType * >::iterator::operator+=(n);
return *this;
}
iterator operator+(const typename std::vector< CType * >::iterator::difference_type & n) const
{
return iterator(std::vector< CType * >::iterator::operator+(n));
}
iterator & operator-=(const typename std::vector< CType * >::iterator::difference_type & n)
{
std::vector< CType * >::iterator::operator-=(n);
return *this;
}
iterator operator-(const typename std::vector< CType * >::iterator::difference_type & n) const
{
return iterator(std::vector< CType * >::iterator::operator-(n));
}
};
class const_iterator: public std::vector< CType * >::const_iterator
{
public:
const_iterator(): std::vector< CType * >::const_iterator() {}
const_iterator(const const_iterator & src): std::vector< CType * >::const_iterator(src) {}
const_iterator(const iterator & src): std::vector< CType * >::const_iterator(src) {}
const_iterator(const typename std::vector< CType * >::const_iterator & src): std::vector< CType * >::const_iterator(src) {}
~const_iterator() {}
const CType & operator*() const
{
return *std::vector< CType * >::const_iterator::operator*();
}
const CType * operator->() const
{
return std::vector< CType * >::const_iterator::operator*();
}
operator const CType * () const
{
return std::vector< CType * >::const_iterator::operator*();
}
const_iterator & operator++()
{
std::vector< CType * >::const_iterator::operator++();
return *this;
}
const_iterator operator++(int)
{
const_iterator Tmp(*this);
operator++();
return Tmp;
}
const_iterator & operator--()
{
std::vector< CType * >::const_iterator::operator--();
return *this;
}
const_iterator operator--(int)
{
const_iterator Tmp(*this);
operator--();
return Tmp;
}
const_iterator & operator+=(const typename std::vector< CType * >::const_iterator::difference_type & n)
{
std::vector< CType * >::const_iterator::operator+=(n);
return *this;
}
const_iterator operator+(const typename std::vector< CType * >::const_iterator::difference_type & n) const
{
return const_iterator(std::vector< CType * >::const_iterator::operator+(n));
}
const_iterator & operator-=(const typename std::vector< CType * >::const_iterator::difference_type & n)
{
std::vector< CType * >::const_iterator::operator-=(n);
return *this;
}
const_iterator operator-(const typename std::vector< CType * >::const_iterator::difference_type & n) const
{
return const_iterator(std::vector< CType * >::const_iterator::operator-(n));
}
CType * constCast()
{
return const_cast< CType * >(std::vector< CType * >::const_iterator::operator*());
}
};
#endif // SWIG
// Operations
/**
* Retrieve the data describing the object
* @return CData data
*/
virtual CData toData() const
{
CData Data;
std::vector< CData > Content;
const_iterator itContent = begin();
const_iterator endContent = end();
for (; itContent != endContent; ++itContent)
{
Content.push_back(itContent->toData());
}
if (!Content.empty())
{
Data.addProperty(CData::VECTOR_CONTENT, Content);
}
return Data;
}
/**
* Apply the provided data to the object
* @param const CData & data
* @return bool success
*/
virtual bool applyData(const CData & data, CUndoData::ChangeSet & changes)
{
bool success = true;
const std::vector< CData > & Content = data.getProperty(CData::VECTOR_CONTENT).toDataVector();
std::vector< CData >::const_iterator it = Content.begin();
std::vector< CData >::const_iterator end = Content.end();
for (; it != end; ++it)
{
size_t Index = it->getProperty(CData::OBJECT_INDEX).toSizeT();
if (Index < size())
{
success &= operator[](Index).applyData(*it, changes);
}
else
{
CType * pNew = dynamic_cast< CType * >(insert(*it));
if (pNew != NULL)
{
success &= pNew->applyData(*it, changes);
}
else
{
success = false;
}
}
}
return success;
}
/**
* Create the undo data which represents the changes recording the
* differences between the provided oldData and the current data.
* @param CUndoData & undoData
* @param const CUndoData::Type & type
* @param const CData & oldData (default: empty data)
* @param const CCore::Framework & framework (default: CCore::Framework::ParticleNumbers)
* @return CUndoData undoData
*/
virtual void createUndoData(CUndoData & undoData,
const CUndoData::Type & type,
const CData & oldData = CData(),
const CCore::Framework & framework = CCore::Framework::ParticleNumbers) const
{
const std::vector< CData > & OldContent = oldData.getProperty(CData::VECTOR_CONTENT).toDataVector();
std::vector< CData >::const_iterator itOld = OldContent.begin();
std::vector< CData >::const_iterator endOld = OldContent.end();
const_iterator itNew = begin();
const_iterator endNew = end();
for (; itOld != endOld && itNew != endNew; ++itOld, ++itNew)
{
CUndoData UndoData;
itNew->createUndoData(UndoData, CUndoData::Type::CHANGE, *itOld, framework);
if (!UndoData.empty())
{
undoData.appendData(UndoData.getOldData(), UndoData.getNewData());
}
}
std::vector< CUndoData > PreProcessData;
// Removal must be done in reverse order
for (; itOld != endOld; ++itOld)
{
PreProcessData.push_back(CUndoData(CUndoData::Type::REMOVE, *itOld));
}
std::vector< CUndoData >::const_reverse_iterator itPre = PreProcessData.rbegin();
std::vector< CUndoData >::const_reverse_iterator endPre = PreProcessData.rbegin();
for (; itPre != endPre; ++itPre)
{
undoData.addPreProcessData(*itPre);
}
for (; itNew != endNew; ++itNew)
{
undoData.addPostProcessData(CUndoData(CUndoData::Type::INSERT, itNew->toData()));
}
}
/**
* Create and insert an undo object based on the given data.
* This method needs to be re-implemented in container which support INSERT and REMOVE
* @param const CData & data
* @return CUndoObjectInterface * pUndoObject
*/
virtual CUndoObjectInterface * insert(const CData & data)
{
CType * pNew = NULL;
size_t Index = 0;
if (data.isSetProperty(CData::OBJECT_POINTER))
{
// We have a reference and the object already exists;
pNew = dynamic_cast< CType * >(reinterpret_cast< CObjectInterface * >(const_cast< void * >(data.getProperty(CData::OBJECT_POINTER).toVoidPointer())));
Index = data.getProperty(CData::OBJECT_REFERENCE_INDEX).toSizeT();
if (pNew != NULL)
{
// Prevent multiple inserts of the same object
if (getIndex(pNew) == C_INVALID_INDEX)
{
std::vector< CType * >::insert(std::vector< CType * >::begin() + std::min(Index, std::vector< CType * >::size()), pNew);
}
else
{
updateIndex(Index, dynamic_cast< CDataObject * >(pNew));
}
CDataContainer::add(pNew, false);
}
}
else
{
pNew = CType::fromData(data, this);
Index = data.getProperty(CData::OBJECT_INDEX).toSizeT();
if (pNew != NULL &&
pNew->getObjectType() != data.getProperty(CData::OBJECT_TYPE).toString())
{
delete pNew;
pNew = NULL;
}
if (pNew != NULL)
{
std::vector< CType * >::insert(std::vector< CType * >::begin() + std::min(Index, std::vector< CType * >::size()), pNew);
CDataContainer::add(pNew, true);
}
}
return dynamic_cast< CDataObject * >(pNew);
}
/**
* Update the index of a contained object
* This method needs to be re-implemented in container which care about the order of contained objects
* @param const size_t & index
* @param const CUndoObjectInterface * pUndoObject
*/
virtual void updateIndex(const size_t & index, const CUndoObjectInterface * pUndoObject)
{
const CType * pObject = dynamic_cast< const CType * >(pUndoObject);
size_t Index = getIndex(pObject);
// We only update the index of container objects.
if (Index == C_INVALID_INDEX ||
Index == index)
{
return;
}
std::vector< CType * >::erase(std::vector< CType * >::begin() + Index);
std::vector< CType * >::insert(std::vector< CType * >::begin() + std::min(index, std::vector< CType * >::size()), const_cast< CType * >(pObject));
}
protected:
CDataVector(const CDataVector < CType > & src);
public:
/**
* Default constructor
* @param const const std::string & name (Default: "NoName")
* @param const CDataContainer * pParent (Default: NULL)
* @param const size_t & flag (Default: flag | CDataObject::Vector)
*/
CDataVector(const std::string & name = "NoName",
const CDataContainer * pParent = NO_PARENT,
const CFlags< Flag > & flag = CFlags< Flag >::None):
std::vector< CType * >(),
CDataContainer(name, pParent, "Vector", flag | CDataObject::Vector)
{CONSTRUCTOR_TRACE;}
/**
* Copy constructor
* @param const CDataVector < CType > & src
* @param const CDataContainer * pParent (Default: NULL)
*/
CDataVector(const CDataVector < CType > & src,
const CDataContainer * pParent):
std::vector< CType * >(src),
CDataContainer(src, pParent)
{
CONSTRUCTOR_TRACE;
size_t i, imax = size();
iterator Target = begin();
const_iterator Source = src.begin();
for (i = 0; i < imax; i++, Target++, Source++)
{
try
{
Target = new CType(*Source, this);
}
catch (...)
{
Target = NULL;
}
if (Target == NULL)
CCopasiMessage ex(CCopasiMessage::EXCEPTION, MCopasiBase + 1, imax * sizeof(CType));
}
}
/**
* Destructor
*/
virtual ~CDataVector()
{
cleanup();
DESTRUCTOR_TRACE;
}
/**
* Assignment operator.
* @param const CDataVector< CType > & rhs
* @return CDataVector< CType > & lhs
*/
CDataVector< CType > & operator = (const CDataVector< CType > & rhs)
{
cleanup();
const_iterator it = rhs.begin();
const_iterator end = rhs.end();
for (; it != end; ++it)
add(*it);
return *this;
}
/**
* Assignment operator. The effect of this operator is that both vectors will
* share the same objects. However, the parentship of the objects is not affected
* This means that the assigned vector must be used with some care.
* @param const std::vector< CType * > & rhs
* @return CDataVector< CType > & lhs
*/
CDataVector< CType > & operator = (const std::vector< CType * > & rhs)
{
cleanup();
typename std::vector< CType * >::const_iterator it = rhs.begin();
typename std::vector< CType * >::const_iterator end = rhs.end();
for (; it != end; ++it)
add(*it, false);
return *this;
}
#ifndef SWIG
bool operator != (const CDataVector< CType > & rhs)
{
if (size() != rhs.size())
{
return true;
}
const_iterator itRhs = rhs.begin();
const_iterator endRhs = rhs.end();
const_iterator it = begin();
for (; itRhs != endRhs; ++itRhs, ++it)
if (*it != *itRhs)
{
return true;
}
return false;
}
#endif
/**
* This creates a deep copy of the source, i.e., all objects are copied and
* the parent of these copies is the current vector.
* @param const CDataVector< CType > & source
*/
void deepCopy(const CDataVector< CType > & source)
{
cleanup();
resize(source.size());
size_t i, imax = size();
iterator Target = begin();
const_iterator Source = source.begin();
for (i = 0; i < imax; i++, Target++, Source++)
{
try
{
Target = new CType(*Source, this);
}
catch (...)
{
Target = NULL;
}
if (Target == NULL)
CCopasiMessage ex(CCopasiMessage::EXCEPTION, MCopasiBase + 1, imax * sizeof(CType));
}
}
iterator begin() {return iterator(std::vector< CType * >::begin());}
const_iterator begin() const {return const_iterator(std::vector< CType * >::begin());}
iterator end() {return iterator(std::vector< CType * >::end());}
const_iterator end() const {return const_iterator(std::vector< CType * >::end());}
name_iterator beginName() {return mObjects.begin();}
const_name_iterator beginName() const {return mObjects.begin();}
name_iterator endName() {return mObjects.end();}
const_name_iterator endName() const {return mObjects.end();}
/**
* Cleanup
*/
virtual void cleanup()
{
typename std::vector< CType * >::iterator it = std::vector< CType * >::begin();
typename std::vector< CType * >::iterator End = std::vector< CType * >::end();
for (; it != End; it++)
if (*it != NULL &&
(*it)->getObjectParent() == this)
{
CDataContainer::remove(*it);
(*it)->setObjectParent(NULL);
delete (*it);
*it = NULL;
}
CDataVector< CType >::clear();
}
/**
* Add a copy of the object to the end of the vector.
* @param const CType & src
* @return bool success.
*/
virtual bool add(const CType & src)
{
CType * pCopy = NULL;
try
{
pCopy = new CType(src, this);
}
catch (...)
{
pCopy = NULL;
}
if (pCopy == NULL)
CCopasiMessage ex(CCopasiMessage::EXCEPTION, MCopasiBase + 1, sizeof(CType));
// This is not very efficient !!!
// It results in a lot of resizing of the vector !!!
std::vector< CType * >::push_back(pCopy);
return CDataContainer::add(pCopy, true);
}
/**
* Swap two objects in the vector.
* @param const size_t & indexFrom
* @param const size_t & indexTo
*/
virtual void swap(const size_t & indexFrom, const size_t & indexTo)
{
size_t Size = size();
if (!(indexFrom < Size))
CCopasiMessage ex(CCopasiMessage::EXCEPTION, MCDataVector + 3, indexFrom, Size - 1);
if (!(indexTo < Size))
CCopasiMessage ex(CCopasiMessage::EXCEPTION, MCDataVector + 3, indexTo, Size - 1);
typename std::vector< CType * >::iterator from = std::vector< CType * >::begin() + indexFrom;
typename std::vector< CType * >::iterator to = std::vector< CType * >::begin() + indexTo;
typename std::vector< CType * >::value_type tmp = *from;
*from = *to;
*to = tmp;
}
/**
* Add an object to the end of the vector, if adopt is true the vector.
* becomes the parent of the object.
* @param CType * src
* @param const bool & adopt (Default: false)
* @return bool success
*/
virtual bool add(CDataObject * pObject, const bool & adopt = true)
{
// This is not very efficient !!!
// It results in a lot of resizing of the vector !!!
CType * pNew = dynamic_cast< CType * >(pObject);
if (pNew != NULL)
{
std::vector< CType * >::push_back(pNew);
}
return CDataContainer::add(pObject, adopt);
}
/**
* Removes the index-th element from the vector
* @param const size_t & index
*/
virtual void remove(const size_t & index)
{
if (!(index < size()))
return;
typename std::vector< CType * >::iterator Target = std::vector< CType * >::begin() + index;
if (*Target)
{
if ((*Target)->getObjectParent() == this)
{
// (*Target)->cleanup();
delete *Target;
}
else
{
CDataContainer::remove(*Target);
std::vector< CType * >::erase(Target, Target + 1);
}
}
}
/**
* Removes the pointer to the object, from the vector
* @param CDataObject * pObject
* @return bool success
*/
virtual bool remove(CDataObject * pObject)
{
const size_t index = getIndex(pObject);
bool success = true;
if (index != C_INVALID_INDEX)
{
typename std::vector< CType * >::iterator Target = std::vector< CType * >::begin() + index;
// Note: erase does not delete pointed to objects
std::vector< CType * >::erase(Target, Target + 1);
}
else
success = false;
success &= CDataContainer::remove(pObject);
return success;
}
/**
* Retrieve the indexed object.
* @param const size_t & index
* @return const value_type & object
*/
const CType & operator[](const size_t & index) const
{
if (!(index < size()))
CCopasiMessage ex(CCopasiMessage::EXCEPTION, MCDataVector + 3, index, size() - 1);
return **(std::vector< CType *>::begin() + index);
}
/**
* Retrieve the indexed object.
* @param const size_t & index
* @return value_type & object
*/
CType & operator[](const size_t & index)
{
if (!(index < size()))
CCopasiMessage ex(CCopasiMessage::EXCEPTION, MCDataVector + 3, index, size() - 1);
return **(std::vector< CType *>::begin() + index);
}
/**
* Retrieve a the CDataObject with the given name.
* @param const CCommonName &name
* @return const CObjectInterface * object
*/
virtual const CObjectInterface * getObject(const CCommonName &name) const
{
size_t Index = name.getElementIndex();
if (Index < size())
{
CDataObject * pObject = *(std::vector< CType * >::begin() + Index);
if (dynamic_cast< const CType *>(pObject) != NULL)
return pObject->getObject(name.getRemainder());
}
return CDataContainer::getObject(name);
}
/**
* Retrieves the size of the vector
* @return size_t size
*/
virtual size_t size() const
{return std::vector< CType * >::size();}
/**
* @return a boolean indicating whether vector is empty
*/
virtual bool empty() const
{
return std::vector< CType * >::empty();
}
/**
* Resizes the vector but does not create new member objects
* @param const size_t & newSize
*/
virtual void resize(const size_t & newSize)
{
size_t OldSize = size();
if (OldSize == newSize) return; // Nothing to do.
if (OldSize < newSize)
{
std::vector< CType * >::resize(newSize);
size_t i;
typename std::vector< CType * >::iterator Target = std::vector< CType * >::begin() + OldSize;
for (i = OldSize; i < newSize; i++, Target++)
*Target = NULL;
}
else
{
typename std::vector< CType * >::iterator Target = std::vector< CType * >::begin() + newSize;
typename std::vector< CType * >::iterator End = std::vector< CType * >::end();
for (; Target != End; Target++)
if (*Target)
{
if ((*Target)->getObjectParent() == this)
{
CDataContainer::remove(*Target);
(*Target)->setObjectParent(NULL);
delete *Target;
}
else
CDataContainer::remove(*Target);
}
std::vector< CType * >::resize(newSize);
}
}
virtual void clear()
{
mValidity.clear();
if (size() == 0) return; // Nothing to do.
typename std::vector< CType * >::iterator Target = std::vector< CType * >::begin();
typename std::vector< CType * >::iterator End = std::vector< CType * >::end();
for (; Target != End; Target++)
if (*Target)
{
if ((*Target)->getObjectParent() == this)
{
CDataContainer::remove(*Target);
(*Target)->setObjectParent(NULL);
delete *Target;
}
else
CDataContainer::remove(*Target);
}
std::vector< CType * >::clear();
}
/**
* Retrieve the index of the pointed to object in the vector. If the object
* is not found C_INVALID_INDEX is returned.
* @param const CDataObject * pObject
* @return size_t index
*/
virtual size_t getIndex(const CDataObject * pObject) const
{
size_t i, imax = size();
typename std::vector< CType * >::const_iterator itTarget = std::vector< CType * >::begin();
for (i = 0; i < imax; i++, itTarget++)
{
if (pObject == static_cast< const CDataObject * >(*itTarget))
{
return i;
}
}
return CDataContainer::getIndex(pObject);
}
/**
* ostream operator
* @param std::ostream & os
* @param const CDataVector<CType> & d
* @return std::ostream & os
*/
#if defined SWIG
friend std::ostream &operator << (std::ostream &os,
const CDataVector<CType> & d);
#else
#if defined _MSC_VER && _MSC_VER < 1201 // 1200 Identifies Visual C++ 6.0
friend std::ostream &operator << (std::ostream &os,
const CDataVector<CType> & d);
#else
friend std::ostream &operator << <>(std::ostream &os,
const CDataVector<CType> & d);
#endif // WIN32
#endif // SWIG
};
template < class CType > class CDataVectorS: public CDataVector < CType >
{
public:
typedef typename std::vector< CType * >::value_type value_type;
typedef typename std::vector< CType * >::iterator iterator;
typedef typename std::vector< CType * >::const_iterator const_iterator;
// Operations
public:
/**
* Default constructor
* @param const std::string & name (Default: "NoName")
* @param const CDataContainer * pParent (Default: NULL)
*/
CDataVectorS(const std::string & name = "NoName",
const CDataContainer * pParent = NO_PARENT):
CDataVector< CType >(name, pParent) {}
/**
* Copy constructor
* @param const CDataVectorS < CType > & src
* @param const CDataContainer * pParent (Default: NULL)
*/
CDataVectorS(const CDataVectorS < CType > & src,
const CDataContainer * pParent) :
CDataVector < CType > (src, pParent) {}
/**
* Destructor
*/
virtual ~CDataVectorS() {}
/**
* Loads an object with data coming from a CReadConfig object.
* (CReadConfig object reads an input stream)
* @param CReadConfig & configbuffer
* @param const size_t & size
*/
virtual void load(CReadConfig & configbuffer, size_t size)
{
size_t i;
CDataVector< CType >::cleanup();
CDataVector< CType >::resize(size);
typename std::vector< CType * >::iterator Target = std::vector< CType * >::begin();
for (i = 0; i < size; i++, Target++)*Target = NULL;
for (i = 0, Target = std::vector< CType * >::begin(); i < size; i++, Target++)
{
try
{
*Target = new CType("NoName", this);
}
catch (...)
{
*Target = NULL;
}
if (*Target == NULL)
CCopasiMessage ex(CCopasiMessage::EXCEPTION, MCopasiBase + 1, size * sizeof(CType));
(*Target)->load(configbuffer);
}
}
};
template < class CType > class CDataVectorN: public CDataVector < CType >
{
// Operations
public:
typedef CDataObjectMap::type_iterator< CType > name_iterator;
typedef CDataObjectMap::const_type_iterator< CType > const_name_iterator;
/**
* Retrieve the data describing the object
* @return CData data
*/
virtual CData toData() const
{
CData Data;
std::vector< CData > Content;
const_name_iterator itContent = CDataVector< CType >::beginName();
const_name_iterator endContent = CDataVector< CType >::endName();
for (; itContent != endContent; ++itContent)
{
Content.push_back(itContent->toData());
}
Data.addProperty(CData::VECTOR_CONTENT, Content);
return Data;
}
/**
* Apply the provided data to the object
* @param const CData & data
* @return bool success
*/
virtual bool applyData(const CData & data, CUndoData::ChangeSet & changes)
{
bool success = true;
bool inserted = false;
const std::vector< CData > & Content = data.getProperty(CData::VECTOR_CONTENT).toDataVector();
std::vector< CData >::const_iterator it = Content.begin();
std::vector< CData >::const_iterator end = Content.end();
for (; it != end; ++it)
{
CType * pObject = const_cast< CType * >(dynamic_cast< const CType *>(getObject("[" + CCommonName::escape(it->getProperty(CData::OBJECT_NAME).toString()) + "]")));
if (pObject == NULL)
{
pObject = dynamic_cast< CType * >(CDataVector< CType >::insert(*it));
inserted &= (pObject != NULL);
}
if (pObject != NULL)