-
Notifications
You must be signed in to change notification settings - Fork 184
/
IdfObject.cpp
2309 lines (1954 loc) · 72.1 KB
/
IdfObject.cpp
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
/***********************************************************************************************************************
* OpenStudio(R), Copyright (c) Alliance for Sustainable Energy, LLC.
* See also https://openstudio.net/license
***********************************************************************************************************************/
#include "IdfObject.hpp"
#include "IdfObject_Impl.hpp"
#include "IdfExtensibleGroup.hpp"
#include "IdfRegex.hpp"
#include "ValidityReport.hpp"
#include "../idd/IddKey.hpp"
#include <utilities/idd/IddFactory.hxx>
#include <utilities/idd/IddEnums.hxx>
#include "../idd/IddRegex.hpp"
#include "../idd/CommentRegex.hpp"
#include "../idd/Comments.hpp"
#include "../math/FloatCompare.hpp"
#include "../core/Finder.hpp"
#include "../core/Assert.hpp"
#include "../units/Quantity.hpp"
#include "../units/OSOptionalQuantity.hpp"
#include "../units/QuantityConverter.hpp"
#include <boost/lexical_cast.hpp>
#include <iomanip>
namespace openstudio {
namespace detail {
// CONSTRUCTORS
IdfObject_Impl::IdfObject_Impl(const IdfObject_Impl& other, bool keepHandle)
: m_comment(other.comment()), m_iddObject(other.iddObject()), m_fields(other.fields()), m_fieldComments(other.fieldComments()) {
if (keepHandle) {
OS_ASSERT(!other.handle().isNull());
m_handle = other.handle();
} else {
m_handle = openstudio::createUUID();
if (m_iddObject.hasHandleField()) {
bool ok = setString(0, toString(m_handle));
OS_ASSERT(ok);
}
}
}
IdfObject_Impl::IdfObject_Impl(IddObjectType type, bool fastName) : m_handle(openstudio::createUUID()) {
OptionalIddObject candidate = IddFactory::instance().getObject(type);
OS_ASSERT(candidate);
m_iddObject = *candidate;
if (m_iddObject.hasHandleField()) {
bool ok = setString(0, toString(m_handle));
OS_ASSERT(ok);
}
if (fastName) {
boost::optional<unsigned> nameFieldIndex = this->m_iddObject.nameFieldIndex();
if (nameFieldIndex) {
bool ok = setString(*nameFieldIndex, toString(createUUID()));
OS_ASSERT(ok);
} else {
LOG(Error, "Requested setting name field to UUID but object of type '" << this->m_iddObject.name() << "' has no name field");
}
}
resizeToMinFields();
}
IdfObject_Impl::IdfObject_Impl(const IddObject& iddObject, bool fastName) : m_handle(openstudio::createUUID()), m_iddObject(iddObject) {
if (this->m_iddObject.hasHandleField()) {
bool ok = setString(0, toString(m_handle));
OS_ASSERT(ok);
}
if (fastName) {
boost::optional<unsigned> nameFieldIndex = this->m_iddObject.nameFieldIndex();
if (nameFieldIndex) {
bool ok = setString(*nameFieldIndex, toString(createUUID()));
OS_ASSERT(ok);
} else {
LOG(Error, "Requested setting name field to UUID but object of type '" << this->m_iddObject.name() << "' has no name field");
}
}
resizeToMinFields();
}
IdfObject_Impl::IdfObject_Impl(const IddObject& iddObject, bool fastName, bool minimal) : m_iddObject(iddObject) {
OS_ASSERT(!fastName);
OS_ASSERT(minimal);
}
IdfObject_Impl::IdfObject_Impl(const Handle& handle, const std::string& comment, const IddObject& iddObject, const StringVector& fields,
const StringVector& fieldComments)
: m_handle(handle), m_comment(comment), m_iddObject(iddObject), m_fields(fields), m_fieldComments(fieldComments) {
resizeToMinFields();
}
// GETTERS
Handle IdfObject_Impl::handle() const {
return m_handle;
}
IddObject IdfObject_Impl::iddObject() const {
return m_iddObject;
}
std::string IdfObject_Impl::comment() const {
return m_comment;
}
boost::optional<std::string> IdfObject_Impl::fieldComment(unsigned index, bool returnDefault) const {
if (index >= numFields()) {
return boost::none;
}
std::string result;
if (index < m_fieldComments.size()) {
result = m_fieldComments[index];
}
if (returnDefault && result.empty()) {
if (OptionalIddField iddField = m_iddObject.getField(index)) {
std::stringstream ss;
ss << makeIdfEditorComment(iddField->name());
if (m_iddObject.isExtensibleField(index)) {
ExtensibleIndex ei = m_iddObject.extensibleIndex(index);
ss << " " << ei.group + 1;
}
if (OptionalString units = iddField->properties().units) {
if (units) {
ss << " {" << *units << "}";
}
}
result = ss.str();
}
}
return result;
}
boost::optional<std::string> IdfObject_Impl::name(bool returnDefault) const {
if (OptionalUnsigned oi = m_iddObject.nameFieldIndex()) {
unsigned index = *oi;
bool validIndex = m_fields.size() > index;
if (returnDefault && validIndex && m_fields[index].empty()) {
if (OptionalString stringDefault = m_iddObject.nonextensibleFields()[index].properties().stringDefault) {
if (stringDefault) {
return decodeString(*stringDefault);
} else {
return boost::none;
}
}
return decodeString(m_fields[index]);
} else if (validIndex) {
return decodeString(m_fields[index]);
}
}
return boost::none;
}
std::string IdfObject_Impl::nameString(bool returnDefault) const {
boost::optional<std::string> temp = name(returnDefault);
if (temp) {
return temp.get();
}
return "";
}
std::string IdfObject_Impl::briefDescription() const {
std::stringstream ss;
ss << "Object of type '" << m_iddObject.type().valueDescription() << "'";
OptionalString oName = name(true);
if (oName && (!oName->empty())) {
ss << " and named '" << *oName << "'";
}
return ss.str();
}
bool IdfObject_Impl::isEmpty(unsigned index) const {
bool result = true;
if (index < m_fields.size()) {
OptionalString string = getString(index, false, true);
if (string) {
result = false;
}
}
return result;
}
boost::optional<std::string> IdfObject_Impl::getString(unsigned index, bool returnDefault, bool returnUninitializedEmpty) const {
OptionalString result;
if (index < m_fields.size()) {
result = m_fields[index];
}
if (returnDefault && ((result && result->empty()) || (!result))) {
OptionalIddField iddField = m_iddObject.getField(index);
if (iddField && iddField->properties().stringDefault) {
result = *(iddField->properties().stringDefault);
}
}
if (returnUninitializedEmpty && (result && result->empty())) {
result.reset();
}
if (result) {
return decodeString(*result);
}
return boost::none;
}
boost::optional<double> IdfObject_Impl::getDouble(unsigned index, bool returnDefault) const {
OptionalDouble result;
OptionalString value = getString(index, returnDefault, false);
if (value) {
if (!(istringEqual(*value, "") || istringEqual(*value, "autosize") || istringEqual(*value, "autocalculate"))) {
try {
result = boost::lexical_cast<double>(*value);
} catch (const std::exception&) {
LOG(Error, "Could not convert '" << *value << "' to double");
}
}
}
return result;
}
OSOptionalQuantity IdfObject_Impl::getQuantity(unsigned index, bool returnDefault, bool returnIP) const {
OptionalDouble value = getDouble(index, returnDefault);
return getQuantityFromDouble(index, value, returnIP);
}
boost::optional<unsigned> IdfObject_Impl::getUnsigned(unsigned index, bool returnDefault) const {
OptionalUnsigned result;
OptionalString value = getString(index, returnDefault, false);
if (value) {
if (!(istringEqual(*value, "") || istringEqual(*value, "autosize") || istringEqual(*value, "autocalculate"))) {
try {
auto temp = boost::lexical_cast<double>(*value);
result = boost::numeric_cast<unsigned>(temp);
} catch (const std::exception&) {
LOG(Error, "Could not convert '" << *value << "' to unsigned");
}
}
}
return result;
}
boost::optional<int> IdfObject_Impl::getInt(unsigned index, bool returnDefault) const {
OptionalInt result;
OptionalString value = getString(index, returnDefault, false);
if (value) {
if (!(istringEqual(*value, "") || istringEqual(*value, "autosize") || istringEqual(*value, "autocalculate"))) {
try {
auto temp = boost::lexical_cast<double>(*value);
result = boost::numeric_cast<int>(temp);
} catch (const std::exception&) {
LOG(Error, "Could not convert '" << *value << "' to int");
}
}
}
return result;
}
IdfExtensibleGroup IdfObject_Impl::getExtensibleGroup(unsigned groupIndex) const {
unsigned n = numFields();
unsigned i = n;
std::shared_ptr<detail::IdfObject_Impl> p;
IdfExtensibleGroup eg(p, i);
if (m_iddObject.properties().extensible) {
if (m_iddObject.index(ExtensibleIndex(groupIndex, m_iddObject.properties().numExtensible - 1)) < n) {
i = m_iddObject.index(ExtensibleIndex(groupIndex, 0));
eg = IdfExtensibleGroup(std::const_pointer_cast<IdfObject_Impl>(shared_from_this()), i);
}
}
return eg;
}
std::vector<IdfExtensibleGroup> IdfObject_Impl::extensibleGroups() const {
IdfExtensibleGroupVector result;
for (unsigned i = 0, n = numExtensibleGroups(); i < n; ++i) {
result.push_back(getExtensibleGroup(i));
}
return result;
}
// SETTERS
void IdfObject_Impl::setComment(const std::string& comment) {
setComment(comment, true);
this->emitChangeSignals();
}
void IdfObject_Impl::setComment(const std::string& comment, bool /*checkValidity*/) {
m_comment = makeComment(comment);
m_diffs.push_back(IdfObjectDiff(boost::none, boost::none, boost::none));
}
bool IdfObject_Impl::setFieldComment(unsigned index, const std::string& cmnt) {
bool result = setFieldComment(index, cmnt, true);
if (result) {
this->emitChangeSignals();
}
return result;
}
bool IdfObject_Impl::setFieldComment(unsigned index, const std::string& cmnt, bool /*checkValidity*/) {
if (index < m_fields.size()) {
if (index >= m_fieldComments.size()) {
m_fieldComments.resize(index + 1);
}
m_fieldComments[index] = makeComment(cmnt);
m_diffs.push_back(IdfObjectDiff(index, m_fields[index], m_fields[index]));
return true;
}
return false;
}
boost::optional<std::string> IdfObject_Impl::setName(const std::string& newName) {
OptionalString result = IdfObject_Impl::setName(newName, true);
if (result) {
this->emitChangeSignals();
}
return result;
}
boost::optional<std::string> IdfObject_Impl::setName(const std::string& _newName, bool /*checkValidity*/) {
std::string newName = encodeString(_newName);
switch (m_iddObject.type().value()) {
// get all EMS idd object types in both E+ and OS IDD
case openstudio::IddObjectType::EnergyManagementSystem_Actuator:; // deliberate fall through
case openstudio::IddObjectType::OS_EnergyManagementSystem_Actuator:; // deliberate fall through
case openstudio::IddObjectType::EnergyManagementSystem_ConstructionIndexVariable:; // TODO uncomment when wrapped
case openstudio::IddObjectType::OS_EnergyManagementSystem_ConstructionIndexVariable:; // deliberate fall through
case openstudio::IddObjectType::EnergyManagementSystem_CurveOrTableIndexVariable:; // TODO uncomment when wrapped
case openstudio::IddObjectType::OS_EnergyManagementSystem_CurveOrTableIndexVariable:; // deliberate fall through
case openstudio::IddObjectType::EnergyManagementSystem_GlobalVariable:; // deliberate fall through
case openstudio::IddObjectType::OS_EnergyManagementSystem_GlobalVariable:; // deliberate fall through
case openstudio::IddObjectType::EnergyManagementSystem_InternalVariable:; // deliberate fall through
case openstudio::IddObjectType::OS_EnergyManagementSystem_InternalVariable:; // deliberate fall through
case openstudio::IddObjectType::EnergyManagementSystem_Program:; // deliberate fall through
case openstudio::IddObjectType::OS_EnergyManagementSystem_Program:; // deliberate fall through
case openstudio::IddObjectType::EnergyManagementSystem_Sensor:; // deliberate fall through
case openstudio::IddObjectType::OS_EnergyManagementSystem_Sensor:; // deliberate fall through
case openstudio::IddObjectType::EnergyManagementSystem_Subroutine:; // deliberate fall through
case openstudio::IddObjectType::OS_EnergyManagementSystem_Subroutine:; // deliberate fall through
case openstudio::IddObjectType::EnergyManagementSystem_TrendVariable:; // deliberate fall through
case openstudio::IddObjectType::OS_EnergyManagementSystem_TrendVariable:; // deliberate fall through
// replace � � with �_�
std::replace(newName.begin(), newName.end(), ' ', '_');
break;
//case openstudio::IddObjectType::EnergyManagementSystem_OutputVariable:; // deliberate fall through
//case openstudio::IddObjectType::OS_EnergyManagementSystem_OutputVariable:; // deliberate fall through
//TODO Enforce Title-Case
default:
// no-op
break;
}
// check Idd to see if this object has a name
if (OptionalUnsigned index = m_iddObject.nameFieldIndex()) {
// if so, change the name, or create it
unsigned n = numFields();
unsigned i = *index;
OS_ASSERT(i < 2u);
if (n == 0 && i == 1) {
OS_ASSERT(!m_handle.isNull());
m_fields.push_back(toString(m_handle));
m_diffs.push_back(IdfObjectDiff(0u, boost::none, m_fields.back()));
}
n = numFields();
if (i < n) {
std::string oldName = m_fields[i];
m_fields[i] = newName;
m_diffs.push_back(IdfObjectDiff(i, oldName, newName));
} else {
m_fields.push_back(newName);
m_diffs.push_back(IdfObjectDiff(i, boost::none, newName));
}
//return decoded string since we might have made changes to it if its an EMS object.
newName = decodeString(newName);
return newName; // success!
}
return boost::none; // no name
}
boost::optional<std::string> IdfObject_Impl::createName() {
return IdfObject_Impl::createName(true);
}
boost::optional<std::string> IdfObject_Impl::createName(bool overwrite) {
if (OptionalUnsigned index = m_iddObject.nameFieldIndex()) {
OptionalString oName = name();
if (!oName || oName->empty() || overwrite) {
std::string newName = createUniqueName(m_iddObject.name());
return setName(newName);
}
}
return boost::none;
}
bool IdfObject_Impl::setString(unsigned index, const std::string& value) {
bool result = IdfObject_Impl::setString(index, value, true);
if (result) {
this->emitChangeSignals();
}
return result;
}
bool IdfObject_Impl::setString(unsigned index, const std::string& _value, bool checkValidity) {
std::string value = encodeString(_value);
if (m_iddObject.hasNameField() && (index == m_iddObject.nameFieldIndex().get())) {
return IdfObject_Impl::setName(value, checkValidity).has_value();
}
// push fields and groups if necessary and possible
if (m_iddObject.isNonextensibleField(index) || m_iddObject.isExtensibleField(index)) {
bool result = true;
boost::optional<std::string> oldValue;
unsigned n = m_fields.size();
unsigned nn = n;
unsigned diffSize = m_diffs.size();
unsigned iddn = m_iddObject.numFields();
if (index >= m_fields.size()) {
while (result && (index >= nn) && (nn < iddn)) {
// push regular fields
result = result && this->pushString(checkValidity);
nn = m_fields.size();
}
while (result && (index >= nn)) {
// push extensible fields
result = result && !this->pushExtensibleGroup(StringVector(), checkValidity).empty();
nn = m_fields.size();
}
} else {
oldValue = m_fields[index];
}
if (!result) {
// remove diffs
m_diffs.resize(diffSize);
// resize fields
m_fields.resize(n);
if (m_fieldComments.size() > n) {
m_fieldComments.resize(n);
}
return false;
}
OS_ASSERT(index < m_fields.size());
m_fields[index] = value;
m_diffs.emplace_back(index, oldValue, value);
return result;
}
return false;
}
bool IdfObject_Impl::setDouble(unsigned index, double value) {
bool result = setDouble(index, value, true);
if (result) {
this->emitChangeSignals();
}
return result;
}
bool IdfObject_Impl::setDouble(unsigned index, double value, bool checkValidity) {
try {
return setString(index, toString(value), checkValidity);
} catch (...) {
return false;
}
}
bool IdfObject_Impl::setQuantity(unsigned index, const Quantity& q) {
bool result = setQuantity(index, q, true);
if (result) {
this->emitChangeSignals();
}
return result;
}
bool IdfObject_Impl::setQuantity(unsigned index, const Quantity& q, bool checkValidity) {
OptionalDouble value = getDoubleFromQuantity(index, q);
if (!value) {
return false;
}
return setDouble(index, *value, checkValidity);
}
bool IdfObject_Impl::setUnsigned(unsigned index, unsigned value) {
bool result = setUnsigned(index, value, true);
if (result) {
this->emitChangeSignals();
}
return result;
}
bool IdfObject_Impl::setUnsigned(unsigned index, unsigned value, bool checkValidity) {
try {
auto str = boost::lexical_cast<std::string>(value);
return setString(index, str, checkValidity);
} catch (...) {
return false;
}
}
bool IdfObject_Impl::setInt(unsigned index, int value) {
bool result = setInt(index, value, true);
if (result) {
this->emitChangeSignals();
}
return result;
}
bool IdfObject_Impl::setInt(unsigned index, int value, bool checkValidity) {
try {
auto str = boost::lexical_cast<std::string>(value);
return setString(index, str, checkValidity);
} catch (...) {
return false;
}
}
bool IdfObject_Impl::pushString() {
bool result = IdfObject_Impl::pushString(std::string(), true);
if (result) {
this->emitChangeSignals();
}
return result;
}
bool IdfObject_Impl::pushString(bool checkValidity) {
return IdfObject_Impl::pushString(std::string(), checkValidity);
}
bool IdfObject_Impl::pushString(const std::string& value) {
bool result = IdfObject_Impl::pushString(value, true);
if (result) {
this->emitChangeSignals();
}
return result;
}
bool IdfObject_Impl::pushString(const std::string& value, bool checkValidity) {
// get new index
unsigned index = m_fields.size();
if (m_iddObject.hasNameField() && (index == m_iddObject.nameFieldIndex().get())) {
return IdfObject_Impl::setName(value, checkValidity).has_value();
}
// ok if nonextensible, or extensible w/ group size 1
if (m_iddObject.isNonextensibleField(index) || (m_iddObject.isExtensibleField(index) && (m_iddObject.properties().numExtensible == 1))) {
m_fields.push_back(value);
m_diffs.push_back(IdfObjectDiff(index, boost::none, value));
return true;
}
return false;
}
IdfExtensibleGroup IdfObject_Impl::pushExtensibleGroup(const std::vector<std::string>& values) {
IdfExtensibleGroup result = IdfObject_Impl::pushExtensibleGroup(values, true);
if (!result.empty()) {
this->emitChangeSignals();
}
return result;
}
IdfExtensibleGroup IdfObject_Impl::pushExtensibleGroup(const std::vector<std::string>& values, bool checkValidity) {
unsigned groupSize = m_iddObject.properties().numExtensible;
unsigned n = numFields();
IdfObject_ImplPtr p = nullptr;
IdfExtensibleGroup result(p, n);
if (!values.empty() && (values.size() != groupSize)) {
return result;
}
StringVector wValues = values; // copy so can resize empty vector
OptionalUnsigned mf = maxFields();
unsigned diffSize = m_diffs.size();
// push fields as needed
unsigned iddn = m_iddObject.numFields();
if (n < iddn) {
bool ok = this->setString(iddn - 1, "", checkValidity);
if (!ok) {
// remove the diffs
m_diffs.resize(diffSize);
// resize the fields
m_fields.resize(n);
if (m_fieldComments.size() > n) {
m_fieldComments.resize(n);
}
return result;
}
}
n = numFields();
OS_ASSERT(n >= iddn);
// needs to be extensible, have all non-extensible fields defined, and not violate maxFields.
if ((groupSize > 0) && (!mf || (n + groupSize <= *mf))) {
OS_ASSERT(m_iddObject.properties().extensible);
if (wValues.empty()) {
wValues.resize(groupSize);
}
m_fields.resize(n + groupSize);
for (unsigned i = 0; i < groupSize; ++i) {
bool ok = setString(n + i, wValues[i], checkValidity);
if (!ok) {
// remove the diffs
m_diffs.resize(diffSize);
// resize the fields
m_fields.resize(n);
if (m_fieldComments.size() > n) {
m_fieldComments.resize(n);
}
return result;
}
}
result = getExtensibleGroup(numExtensibleGroups() - 1);
OS_ASSERT(!result.empty());
OS_ASSERT(result.groupIndex() == m_iddObject.extensibleIndex(n).group);
}
return result;
}
IdfExtensibleGroup IdfObject_Impl::insertExtensibleGroup(unsigned groupIndex, const std::vector<std::string>& values) {
IdfExtensibleGroup result = insertExtensibleGroup(groupIndex, values, true);
if (!result.empty()) {
this->emitChangeSignals();
}
return result;
}
IdfExtensibleGroup IdfObject_Impl::insertExtensibleGroup(unsigned groupIndex, const std::vector<std::string>& values, bool checkValidity) {
// if really a push request, send it there
if (groupIndex == numExtensibleGroups()) {
return pushExtensibleGroup(values, checkValidity);
}
// now have groupIndex != numExtensibleGroups()
unsigned groupSize = m_iddObject.properties().numExtensible;
unsigned n = numFields();
IdfObject_ImplPtr p;
IdfExtensibleGroup result(p, n);
// nothing to do if not extensible, groupIndex invalid, or values invalid
if ((groupSize == 0) || (groupIndex > numExtensibleGroups())) {
return result;
}
if (!values.empty() && (values.size() != groupSize)) {
return result;
}
// record diffs at start
unsigned diffSize = m_diffs.size();
// from now on, groupIndex < numExtensibleGroups(), and numExtensibleGroups() > 0
OptionalUnsigned mf = maxFields();
// needs to have all non-extensible fields defined, and not violate maxFields.
if ((n >= m_iddObject.numFields()) && (!mf || (n + groupSize <= *mf))) {
OS_ASSERT(m_iddObject.properties().extensible);
StringVector wValues = values;
if (wValues.empty()) {
wValues.resize(groupSize);
}
// push a new group on the end
unsigned i = numExtensibleGroups() - 1;
IdfExtensibleGroup eg = getExtensibleGroup(i);
OS_ASSERT(!eg.empty());
IdfExtensibleGroup temp = pushExtensibleGroup(eg.fields(), checkValidity);
if (temp.empty()) {
OS_ASSERT(numFields() == n);
OS_ASSERT(m_diffs.size() == diffSize);
return result;
}
// copy values from group i to i+1 down to and including groupIndex
bool ok = true;
while (i > groupIndex) {
--i;
IdfExtensibleGroup peg = getExtensibleGroup(i);
OS_ASSERT(!peg.empty());
ok = eg.setFields(peg.fields(), false);
if (!ok) {
// roll back
i += 2;
while (i < numExtensibleGroups() - 1) {
peg = getExtensibleGroup(i);
OS_ASSERT(!peg.empty());
eg = getExtensibleGroup(i + 1);
OS_ASSERT(!eg.empty());
peg.setFields(eg.fields(), false);
++i;
}
popExtensibleGroup(false);
// remove the diffs
m_diffs.resize(diffSize);
return result;
}
eg = peg;
}
// now set the values at this group index
ok = eg.setFields(wValues, checkValidity);
if (!ok) {
i = groupIndex + 1;
IdfExtensibleGroup peg = getExtensibleGroup(i);
OS_ASSERT(!peg.empty());
while (i < numExtensibleGroups() - 1) {
++i;
eg = getExtensibleGroup(i);
OS_ASSERT(!eg.empty());
peg.setFields(eg.fields(), false);
peg = eg;
}
popExtensibleGroup(false);
// remove the diffs
m_diffs.resize(diffSize);
return result;
}
result = eg;
OS_ASSERT(!result.empty());
OS_ASSERT(numFields() == n + groupSize);
}
return result;
}
std::vector<std::string> IdfObject_Impl::popExtensibleGroup() {
StringVector result = IdfObject_Impl::popExtensibleGroup(true);
if (!result.empty()) {
this->emitChangeSignals();
}
return result;
}
/** Pops the final extensible group from the object, if possible. Returns the popped data if
* successful. Otherwise, the returned vector will be empty. */
std::vector<std::string> IdfObject_Impl::popExtensibleGroup(bool /*checkValidity*/) {
unsigned groupSize = m_iddObject.properties().numExtensible;
unsigned numBeforePop = numFields();
unsigned numAfterPop = numBeforePop - groupSize;
StringVector result;
// must be extensible object, with some extensible fields
if ((groupSize > 0) && (numAfterPop >= m_iddObject.numFields())) {
IdfExtensibleGroup egToPop = getExtensibleGroup(numExtensibleGroups() - 1);
OS_ASSERT(!egToPop.empty());
UnsignedVector indices = egToPop.mf_indices();
result = egToPop.fields();
OS_ASSERT(result.size() == groupSize);
// record diffs for each field going backwards
for (unsigned i = 0; i < groupSize; ++i) {
m_diffs.push_back(IdfObjectDiff(numBeforePop - 1 - i, result[i], boost::none));
}
m_fields.resize(numAfterPop);
if (m_fieldComments.size() > m_fields.size()) {
m_fieldComments.resize(numAfterPop);
}
OS_ASSERT(egToPop.empty());
}
return result;
}
/** Erases the extensible group at groupIndex, if possible. Returns the erased data, and
* preserves the relative order of the remaining data if successful. Otherwise, the returned
* vector will be empty. */
std::vector<std::string> IdfObject_Impl::eraseExtensibleGroup(unsigned groupIndex) {
StringVector result = eraseExtensibleGroup(groupIndex, true);
if (!result.empty()) {
this->emitChangeSignals();
}
return result;
}
std::vector<std::string> IdfObject_Impl::eraseExtensibleGroup(unsigned groupIndex, bool checkValidity) {
StringVector result;
if (groupIndex >= numExtensibleGroups()) {
return result;
}
UnsignedVector indices = getExtensibleGroup(groupIndex).mf_indices();
// start at bottom
result = popExtensibleGroup(false);
if (result.empty()) {
return result;
}
// record diffs at start
unsigned diffSize = m_diffs.size();
bool ok = true;
// pop was successful. roll up until overwrite groupIndex
for (int i = numExtensibleGroups() - 1; i >= static_cast<int>(groupIndex); --i) {
StringVector temp = result;
IdfExtensibleGroup eg = getExtensibleGroup(i);
OS_ASSERT(!eg.empty());
result = eg.fields();
ok = eg.setFields(temp, checkValidity);
if (!ok) {
// roll back changes and return
for (unsigned j = i + 1, n = numExtensibleGroups(); j < n; ++j) {
eg = getExtensibleGroup(j);
OS_ASSERT(!eg.empty());
result = eg.fields();
OS_ASSERT(result.size() == temp.size());
eg.setFields(temp, false);
temp = result;
}
eg = pushExtensibleGroup(temp, false);
OS_ASSERT(!eg.empty());
// remove the diffs
m_diffs.resize(diffSize);
return {};
}
}
OS_ASSERT(ok);
return result;
}
/** Pops all \link IdfExtensibleGroup IdfExtensibleGroups\endlink from the object, if possible.
* Returns popped data if successful. Otherwise, the returned vector will be empty. */
std::vector<std::vector<std::string>> IdfObject_Impl::clearExtensibleGroups() {
std::vector<StringVector> result = clearExtensibleGroups(true);
if (!result.empty()) {
this->emitChangeSignals();
}
return result;
}
std::vector<std::vector<std::string>> IdfObject_Impl::clearExtensibleGroups(bool checkValidity) {
// data to restore if cannot delete all groups
std::vector<StringVector> rollbackValues;
std::vector<StringVector> rollbackComments;
unsigned gn = numExtensibleGroups();
if (gn == 0) {
return rollbackValues;
}
// record diffs at start
unsigned diffSize = m_diffs.size();
// loop through groups
UnsignedVector indices;
while (gn > 0) {
// get this group's data
IdfExtensibleGroup eg = getExtensibleGroup(gn - 1);
OS_ASSERT(!eg.empty());
if (indices.empty()) {
indices = eg.mf_indices();
}
rollbackValues.push_back(eg.fields());
rollbackComments.push_back(eg.fieldComments());
// try to pop
StringVector result = popExtensibleGroup(checkValidity);
if (result.empty()) {
// unsuccessful--restore cleared data
rollbackValues.pop_back();
rollbackComments.pop_back();
OS_ASSERT(gn == numExtensibleGroups());
// add back groups already popped
while (!rollbackValues.empty()) {
IdfExtensibleGroup pushed = pushExtensibleGroup(rollbackValues.back(), false);
unsigned fieldIndex = 0;
for (const std::string& cmnt : rollbackComments.back()) {
pushed.setFieldComment(fieldIndex, cmnt);
++fieldIndex;
}
rollbackValues.pop_back();
rollbackComments.pop_back();
}
// remove the diffs
m_diffs.resize(diffSize);
return rollbackValues;
}
--gn;
OS_ASSERT(gn == numExtensibleGroups());
}
OS_ASSERT(!rollbackValues.empty());
OS_ASSERT(!indices.empty());
return rollbackValues;
}
// QUERIES
unsigned IdfObject_Impl::numFields() const {
return m_fields.size();
}
unsigned IdfObject_Impl::numNonextensibleFields() const {
unsigned n = numFields();
unsigned iddN = m_iddObject.numFields();
if (iddN < n) {
return iddN;
}
return n;
}
unsigned IdfObject_Impl::minFields() const {
return m_iddObject.properties().minFields;
}
OptionalUnsigned IdfObject_Impl::maxFields() const {
OptionalUnsigned result;
if (!m_iddObject.properties().extensible) {
result = m_iddObject.nonextensibleFields().size();
} else {
result = m_iddObject.properties().maxFields;
}
return result;
}
unsigned IdfObject_Impl::numExtensibleGroups() const {
unsigned nExtFields = numFields() - numNonextensibleFields();
if (nExtFields == 0) {
return 0;
}
OS_ASSERT(m_iddObject.properties().extensible);
unsigned groupSize = m_iddObject.properties().numExtensible;
OS_ASSERT(nExtFields % groupSize == 0);
return nExtFields / groupSize;
}
unsigned IdfObject_Impl::minExtensibleGroups() const {
return m_iddObject.properties().numExtensibleGroupsRequired;
}
boost::optional<unsigned> IdfObject_Impl::maxExtensibleGroups() const {
OptionalUnsigned result = static_cast<unsigned>(0);
if (!m_iddObject.properties().extensible) {
return result;
}
OptionalUnsigned mf = maxFields();
if (!mf) {
return boost::none;
}
unsigned groupSize = m_iddObject.properties().numExtensible;
unsigned maxExtFields = *mf - m_iddObject.numFields();
OS_ASSERT(maxExtFields % groupSize == 0);
OS_ASSERT(groupSize > 0);
result = maxExtFields / groupSize;
return result;
}
bool IdfObject_Impl::isObjectListField(unsigned index) const {
if (index >= numFields()) {
return false;
}
OptionalIddField oIddField = m_iddObject.getField(index);
if (oIddField) {
return oIddField->isObjectListField();
}
return false;
}
std::vector<unsigned> IdfObject_Impl::objectListFields() const {
UnsignedVector result = m_iddObject.objectListFields();
result = trimFieldIndices(result);
result = repeatExtensibleIndices(result);
return result;
}