-
Notifications
You must be signed in to change notification settings - Fork 707
/
parameter_handler.cc
2368 lines (2039 loc) · 76.9 KB
/
parameter_handler.cc
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) 1998 - 2023 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#include <deal.II/base/logstream.h>
#include <deal.II/base/memory_consumption.h>
#include <deal.II/base/parameter_handler.h>
#include <deal.II/base/path_search.h>
#include <deal.II/base/utilities.h>
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/algorithm/string.hpp>
#include <boost/io/ios_state.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#undef BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
DEAL_II_NAMESPACE_OPEN
ParameterHandler::ParameterHandler()
: entries(new boost::property_tree::ptree())
{}
namespace
{
std::string
mangle(const std::string &s)
{
std::string u;
// reserve the minimum number of characters we will need. it may
// be more but this is the least we can do
u.reserve(s.size());
// see if the name is special and if so mangle the whole thing
const bool mangle_whole_string = (s == "value");
// for all parts of the string, see if it is an allowed character or not
for (const char c : s)
{
static const std::string allowed_characters(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
if ((!mangle_whole_string) &&
(allowed_characters.find(c) != std::string::npos))
u.push_back(c);
else
{
u.push_back('_');
static const char hex[16] = {'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
u.push_back(hex[static_cast<unsigned char>(c) / 16]);
u.push_back(hex[static_cast<unsigned char>(c) % 16]);
}
}
return u;
}
std::string
demangle(const std::string &s)
{
std::string u;
u.reserve(s.size());
for (unsigned int i = 0; i < s.size(); ++i)
if (s[i] != '_')
u.push_back(s[i]);
else
{
Assert(i + 2 < s.size(),
ExcMessage("Trying to demangle an invalid string."));
unsigned char c = 0;
switch (s[i + 1])
{
case '0':
c = 0 * 16;
break;
case '1':
c = 1 * 16;
break;
case '2':
c = 2 * 16;
break;
case '3':
c = 3 * 16;
break;
case '4':
c = 4 * 16;
break;
case '5':
c = 5 * 16;
break;
case '6':
c = 6 * 16;
break;
case '7':
c = 7 * 16;
break;
case '8':
c = 8 * 16;
break;
case '9':
c = 9 * 16;
break;
case 'a':
c = 10 * 16;
break;
case 'b':
c = 11 * 16;
break;
case 'c':
c = 12 * 16;
break;
case 'd':
c = 13 * 16;
break;
case 'e':
c = 14 * 16;
break;
case 'f':
c = 15 * 16;
break;
default:
Assert(false, ExcInternalError());
}
switch (s[i + 2])
{
case '0':
c += 0;
break;
case '1':
c += 1;
break;
case '2':
c += 2;
break;
case '3':
c += 3;
break;
case '4':
c += 4;
break;
case '5':
c += 5;
break;
case '6':
c += 6;
break;
case '7':
c += 7;
break;
case '8':
c += 8;
break;
case '9':
c += 9;
break;
case 'a':
c += 10;
break;
case 'b':
c += 11;
break;
case 'c':
c += 12;
break;
case 'd':
c += 13;
break;
case 'e':
c += 14;
break;
case 'f':
c += 15;
break;
default:
Assert(false, ExcInternalError());
}
u.push_back(static_cast<char>(c));
// skip the two characters
i += 2;
}
return u;
}
/**
* Return whether a given node is a parameter node (as opposed
* to being a subsection or alias node)
*/
bool
is_parameter_node(const boost::property_tree::ptree &p)
{
return static_cast<bool>(p.get_optional<std::string>("value"));
}
/**
* Return whether a given node is a alias node (as opposed
* to being a subsection or parameter node)
*/
bool
is_alias_node(const boost::property_tree::ptree &p)
{
return static_cast<bool>(p.get_optional<std::string>("alias"));
}
/**
* Given a list of directories and subdirectories that identify
* a particular place in the tree, return the string that identifies
* this place in the way the BOOST property tree libraries likes
* to identify things.
*/
std::string
collate_path_string(const char separator,
const std::vector<std::string> &subsection_path)
{
if (subsection_path.size() > 0)
{
std::string p = mangle(subsection_path[0]);
for (unsigned int i = 1; i < subsection_path.size(); ++i)
{
p += separator;
p += mangle(subsection_path[i]);
}
return p;
}
else
return "";
}
/**
* Sort all parameters of the subsection given by the
* @p target_subsection_path argument in alphabetical order,
* as well as all subsections within it recursively.
*/
void
recursively_sort_parameters(
const char separator,
const std::vector<std::string> &target_subsection_path,
boost::property_tree::ptree &tree)
{
boost::property_tree::ptree ¤t_section =
tree.get_child(collate_path_string(separator, target_subsection_path));
// Custom comparator to ensure that the order of sorting is:
// - sorted parameters and aliases;
// - sorted subsections.
static auto compare =
[](const std::pair<std::string, boost::property_tree::ptree> &a,
const std::pair<std::string, boost::property_tree::ptree> &b) {
bool a_is_param =
(is_parameter_node(a.second) || is_alias_node(a.second));
bool b_is_param =
(is_parameter_node(b.second) || is_alias_node(b.second));
// If a is a parameter/alias and b is a subsection,
// a should go first, and vice-versa.
if (a_is_param && !b_is_param)
return true;
if (!a_is_param && b_is_param)
return false;
// Otherwise, compare a and b.
return a.first < b.first;
};
current_section.sort(compare);
// Now transverse subsections tree recursively.
for (auto &p : current_section)
{
if ((is_parameter_node(p.second) == false) &&
(is_alias_node(p.second) == false))
{
const std::string subsection = demangle(p.first);
std::vector<std::string> subsection_path = target_subsection_path;
subsection_path.emplace_back(subsection);
recursively_sort_parameters(separator, subsection_path, tree);
}
}
}
/**
* Demangle all parameters recursively and attach them to @p tree_out.
* @p is_parameter_or_alias_node indicates, whether a given node
* @p tree_in is a parameter node or an alias node (as opposed to being
* a subsection).
*/
void
recursively_demangle(const boost::property_tree::ptree &tree_in,
boost::property_tree::ptree &tree_out,
const bool is_parameter_or_alias_node = false)
{
for (const auto &p : tree_in)
{
if (is_parameter_or_alias_node)
{
tree_out.put_child(p.first, p.second);
}
else
{
boost::property_tree::ptree temp;
if (const auto val = p.second.get_value_optional<std::string>())
temp.put_value<std::string>(*val);
recursively_demangle(p.second,
temp,
is_parameter_node(p.second) ||
is_alias_node(p.second));
tree_out.put_child(demangle(p.first), temp);
}
}
}
/**
* Assert validity of of given output @p style.
*/
void
assert_validity_of_output_style(const ParameterHandler::OutputStyle style)
{
AssertThrow(
(((style & ParameterHandler::XML) != 0) +
((style & ParameterHandler::JSON) != 0) +
((style & ParameterHandler::PRM) != 0) +
((style & ParameterHandler::Description) != 0) +
((style & ParameterHandler::LaTeX) != 0)) == 1,
ExcMessage(
"You have chosen either no or multiple style formats. You can choose "
"between: PRM, Description, LaTeX, XML, JSON."));
}
} // namespace
std::string
ParameterHandler::get_current_path() const
{
return collate_path_string(path_separator, subsection_path);
}
std::string
ParameterHandler::get_current_full_path(const std::string &name) const
{
std::string path = get_current_path();
if (path.empty() == false)
path += path_separator;
path += mangle(name);
return path;
}
std::string
ParameterHandler::get_current_full_path(
const std::vector<std::string> &sub_path,
const std::string &name) const
{
std::string path = get_current_path();
if (path.empty() == false)
path += path_separator;
if (sub_path.empty() == false)
path += collate_path_string(path_separator, sub_path) + path_separator;
path += mangle(name);
return path;
}
void
ParameterHandler::parse_input(std::istream &input,
const std::string &filename,
const std::string &last_line,
const bool skip_undefined)
{
AssertThrow(input.fail() == false, ExcIO());
// store subsections we are currently in
const std::vector<std::string> saved_path = subsection_path;
std::string input_line;
std::string fully_concatenated_line;
bool is_concatenated = false;
// Maintain both the current line number and the current logical line
// number, where the latter refers to the line number where (possibly) the
// current line continuation started.
unsigned int current_line_n = 0;
unsigned int current_logical_line_n = 0;
// define an action that tries to scan a line.
//
// if that fails, i.e., if scan_line throws
// an exception either because a parameter doesn't match its
// pattern or because an associated action throws an exception,
// then try to rewind the set of subsections to the same
// point where we were when the current function was called.
// this at least allows to read parameters from a predictable
// state, rather than leave the subsection stack in some
// unknown state.
//
// after unwinding the subsection stack, just re-throw the exception
auto scan_line_or_cleanup = [this,
&skip_undefined,
&saved_path](const std::string &line,
const std::string &filename,
const unsigned int line_number) {
try
{
scan_line(line, filename, line_number, skip_undefined);
}
catch (...)
{
while ((saved_path != subsection_path) && (subsection_path.size() > 0))
leave_subsection();
throw;
}
};
while (std::getline(input, input_line))
{
++current_line_n;
if (!is_concatenated)
current_logical_line_n = current_line_n;
// Trim the whitespace at the ends of the line here instead of in
// scan_line. This makes the continuation line logic a lot simpler.
input_line = Utilities::trim(input_line);
// If we see the line which is the same as @p last_line ,
// terminate the parsing.
if (last_line.size() != 0 && input_line == last_line)
break;
// Check whether or not the current line should be joined with the next
// line before calling scan_line.
if (input_line.size() != 0 &&
input_line.find_last_of('\\') == input_line.size() - 1)
{
input_line.erase(input_line.size() - 1); // remove the last '\'
is_concatenated = true;
fully_concatenated_line += input_line;
}
// If the previous line ended in a '\' but the current did not, then we
// should proceed to scan_line.
else if (is_concatenated)
{
fully_concatenated_line += input_line;
is_concatenated = false;
}
// Finally, if neither the previous nor current lines are continuations,
// then the current input line is entirely concatenated.
else
{
fully_concatenated_line = input_line;
}
if (!is_concatenated)
{
scan_line_or_cleanup(fully_concatenated_line,
filename,
current_logical_line_n);
fully_concatenated_line.clear();
}
}
// While it does not make much sense for anyone to actually do this, allow
// the last line to end in a backslash. To do so, we need to parse
// whatever was left in the stash of concatenated lines
if (is_concatenated)
scan_line_or_cleanup(fully_concatenated_line, filename, current_line_n);
if (saved_path != subsection_path)
{
std::stringstream paths_message;
if (saved_path.size() > 0)
{
paths_message << "Path before loading input:\n";
for (unsigned int i = 0; i < subsection_path.size(); ++i)
{
paths_message << std::setw(i * 2 + 4) << " "
<< "subsection " << saved_path[i] << '\n';
}
paths_message << "Current path:\n";
for (unsigned int i = 0; i < subsection_path.size(); ++i)
{
paths_message << std::setw(i * 2 + 4) << " "
<< "subsection " << subsection_path[i]
<< (i == subsection_path.size() - 1 ? "" : "\n");
}
}
// restore subsection we started with before throwing the exception:
subsection_path = saved_path;
AssertThrow(false,
ExcUnbalancedSubsections(filename, paths_message.str()));
}
}
void
ParameterHandler::parse_input(const std::string &filename,
const std::string &last_line,
const bool skip_undefined,
const bool assert_mandatory_entries_are_found)
{
std::ifstream is(filename);
AssertThrow(is, PathSearch::ExcFileNotFound(filename, "ParameterHandler"));
std::string file_ending = filename.substr(filename.find_last_of('.') + 1);
boost::algorithm::to_lower(file_ending);
if (file_ending == "prm")
parse_input(is, filename, last_line, skip_undefined);
else if (file_ending == "xml")
parse_input_from_xml(is, skip_undefined);
else if (file_ending == "json")
parse_input_from_json(is, skip_undefined);
else
AssertThrow(false,
ExcMessage("Unknown input file name extension. Supported types "
"are .prm, .xml, and .json."));
if (assert_mandatory_entries_are_found)
assert_that_entries_have_been_set();
}
void
ParameterHandler::parse_input_from_string(const std::string &s,
const std::string &last_line,
const bool skip_undefined)
{
std::istringstream input_stream(s);
parse_input(input_stream, "input string", last_line, skip_undefined);
}
namespace
{
// Recursively go through the 'source' tree and see if we can find
// corresponding entries in the 'destination' tree. If not, error out
// (i.e. we have just read an XML file that has entries that weren't
// declared in the ParameterHandler object); if so, copy the value of these
// nodes into the destination object
void
read_xml_recursively(
const boost::property_tree::ptree &source,
const std::string ¤t_path,
const char path_separator,
const std::vector<std::unique_ptr<const Patterns::PatternBase>> &patterns,
const bool skip_undefined,
ParameterHandler &prm)
{
for (const auto &p : source)
{
// a sub-tree must either be a parameter node or a subsection
if (p.second.empty())
{
// set the found parameter in the destination argument
if (skip_undefined)
{
try
{
prm.set(demangle(p.first), p.second.data());
}
catch (const ParameterHandler::ExcEntryUndeclared &)
{
// ignore undeclared entry assert
}
}
else
prm.set(demangle(p.first), p.second.data());
}
else if (p.second.get_optional<std::string>("value"))
{
// set the found parameter in the destination argument
if (skip_undefined)
{
try
{
prm.set(demangle(p.first),
p.second.get<std::string>("value"));
}
catch (const ParameterHandler::ExcEntryUndeclared &)
{
// ignore undeclared entry assert
}
}
else
prm.set(demangle(p.first), p.second.get<std::string>("value"));
// this node might have sub-nodes in addition to "value", such as
// "default_value", "documentation", etc. we might at some point
// in the future want to make sure that if they exist that they
// match the ones in the 'destination' tree
}
else if (p.second.get_optional<std::string>("alias"))
{
// it is an alias node. alias nodes are static and there is
// nothing to do here (but the same applies as mentioned in the
// comment above about the static nodes inside parameter nodes
}
else
{
// it must be a subsection
prm.enter_subsection(demangle(p.first));
read_xml_recursively(p.second,
(current_path.empty() ?
p.first :
current_path + path_separator + p.first),
path_separator,
patterns,
skip_undefined,
prm);
prm.leave_subsection();
}
}
}
// Recursively go through the @p source tree and collapse the nodes of the
// format:
//
//"key":
// {
// "value" : "val",
// "default_value" : "...",
// "documentation" : "...",
// "pattern" : "...",
// "pattern_description": "..."
// },
//
// to
//
// "key" : "val";
//
// As an example a JSON file is shown. However, this function also works for
// XML since both formats are build around the same BOOST data structures.
//
// This function is strongly based on read_xml_recursively().
void
recursively_compress_tree(boost::property_tree::ptree &source)
{
for (auto &p : source)
{
if (p.second.get_optional<std::string>("value"))
{
// save the value in a temporal variable
const auto temp = p.second.get<std::string>("value");
// clear node (children and value)
p.second.clear();
// set the correct value
p.second.put_value<std::string>(temp);
}
else if (p.second.get_optional<std::string>("alias"))
{
}
else
{
// it must be a subsection
recursively_compress_tree(p.second);
}
}
}
} // namespace
void
ParameterHandler::parse_input_from_xml(std::istream &in,
const bool skip_undefined)
{
AssertThrow(in.fail() == false, ExcIO());
// read the XML tree assuming that (as we
// do in print_parameters(XML) it has only
// a single top-level node called
// "ParameterHandler"
boost::property_tree::ptree single_node_tree;
// This boost function will raise an exception if this is not a valid XML
// file.
read_xml(in, single_node_tree);
// make sure there is a single top-level element
// called "ParameterHandler"
AssertThrow(single_node_tree.get_optional<std::string>("ParameterHandler"),
ExcInvalidXMLParameterFile("There is no top-level XML element "
"called \"ParameterHandler\"."));
const std::size_t n_top_level_elements =
std::distance(single_node_tree.begin(), single_node_tree.end());
if (n_top_level_elements != 1)
{
std::ostringstream top_level_message;
top_level_message << "The ParameterHandler input parser found "
<< n_top_level_elements
<< " top level elements while reading\n "
<< " an XML format input file, but there should be"
<< " exactly one top level element.\n"
<< " The top level elements are:\n";
unsigned int entry_n = 0;
for (boost::property_tree::ptree::iterator it = single_node_tree.begin();
it != single_node_tree.end();
++it, ++entry_n)
{
top_level_message
<< " " << it->first
<< (entry_n != n_top_level_elements - 1 ? "\n" : "");
}
// repeat assertion condition to make the printed version easier to read
AssertThrow(n_top_level_elements == 1,
ExcInvalidXMLParameterFile(top_level_message.str()));
}
// read the child elements recursively
const boost::property_tree::ptree &my_entries =
single_node_tree.get_child("ParameterHandler");
read_xml_recursively(
my_entries, "", path_separator, patterns, skip_undefined, *this);
}
void
ParameterHandler::parse_input_from_json(std::istream &in,
const bool skip_undefined)
{
AssertThrow(in.fail() == false, ExcIO());
boost::property_tree::ptree node_tree;
// This boost function will raise an exception if this is not a valid JSON
// file.
try
{
read_json(in, node_tree);
}
catch (const std::exception &e)
{
AssertThrow(
false,
ExcMessage(
"The provided JSON file is not valid. Boost aborted with the "
"following assert message: \n\n" +
std::string(e.what())));
}
// The xml function is reused to read in the xml into the parameter file.
// This means that only mangled files can be read.
read_xml_recursively(
node_tree, "", path_separator, patterns, skip_undefined, *this);
}
void
ParameterHandler::clear()
{
entries = std::make_unique<boost::property_tree::ptree>();
entries_set_status.clear();
}
void
ParameterHandler::declare_entry(const std::string &entry,
const std::string &default_value,
const Patterns::PatternBase &pattern,
const std::string &documentation,
const bool has_to_be_set)
{
entries->put(get_current_full_path(entry) + path_separator + "value",
default_value);
entries->put(get_current_full_path(entry) + path_separator + "default_value",
default_value);
entries->put(get_current_full_path(entry) + path_separator + "documentation",
documentation);
// initialize with false
const std::pair<bool, bool> set_status =
std::pair<bool, bool>(has_to_be_set, false);
entries_set_status.insert(
std::pair<std::string, std::pair<bool, bool>>(get_current_full_path(entry),
set_status));
patterns.reserve(patterns.size() + 1);
patterns.emplace_back(pattern.clone());
entries->put(get_current_full_path(entry) + path_separator + "pattern",
static_cast<unsigned int>(patterns.size() - 1));
// also store the description of
// the pattern. we do so because we
// may wish to export the whole
// thing as XML or any other format
// so that external tools can work
// on the parameter file; in that
// case, they will have to be able
// to re-create the patterns as far
// as possible
entries->put(get_current_full_path(entry) + path_separator +
"pattern_description",
patterns.back()->description());
// as documented, do the default value checking at the very end
AssertThrow(pattern.match(default_value),
ExcValueDoesNotMatchPattern(default_value,
pattern.description()));
}
void
ParameterHandler::add_action(
const std::string &entry,
const std::function<void(const std::string &)> &action,
const bool execute_action)
{
actions.push_back(action);
// get the current list of actions, if any
boost::optional<std::string> current_actions =
entries->get_optional<std::string>(get_current_full_path(entry) +
path_separator + "actions");
// if there were actions already associated with this parameter, add
// the current one to it; otherwise, create a one-item list and use
// that
if (current_actions)
{
const std::string all_actions =
current_actions.get() + "," +
Utilities::int_to_string(actions.size() - 1);
entries->put(get_current_full_path(entry) + path_separator + "actions",
all_actions);
}
else
entries->put(get_current_full_path(entry) + path_separator + "actions",
Utilities::int_to_string(actions.size() - 1));
if (execute_action)
{
const std::string default_value = entries->get<std::string>(
get_current_full_path(entry) + path_separator + "default_value");
action(default_value);
}
}
void
ParameterHandler::declare_alias(const std::string &existing_entry_name,
const std::string &alias_name,
const bool alias_is_deprecated)
{
// see if there is anything to refer to already
Assert(entries->get_optional<std::string>(
get_current_full_path(existing_entry_name)),
ExcMessage("You are trying to declare an alias entry <" + alias_name +
"> that references an entry <" + existing_entry_name +
">, but the latter does not exist."));
// then also make sure that what is being referred to is in
// fact a parameter (not an alias or subsection)
Assert(entries->get_optional<std::string>(
get_current_full_path(existing_entry_name) + path_separator +
"value"),
ExcMessage("You are trying to declare an alias entry <" + alias_name +
"> that references an entry <" + existing_entry_name +
">, but the latter does not seem to be a "
"parameter declaration."));
// now also make sure that if the alias has already been
// declared, that it is also an alias and refers to the same
// entry
if (entries->get_optional<std::string>(get_current_full_path(alias_name)))
{
Assert(entries->get_optional<std::string>(
get_current_full_path(alias_name) + path_separator + "alias"),
ExcMessage("You are trying to declare an alias entry <" +
alias_name +
"> but a non-alias entry already exists in this "
"subsection (i.e., there is either a preexisting "
"further subsection, or a parameter entry, with "
"the same name as the alias)."));
Assert(entries->get<std::string>(get_current_full_path(alias_name) +
path_separator + "alias") ==
existing_entry_name,
ExcMessage(
"You are trying to declare an alias entry <" + alias_name +
"> but an alias entry already exists in this "
"subsection and this existing alias references a "
"different parameter entry. Specifically, "
"you are trying to reference the entry <" +
existing_entry_name +
"> whereas the existing alias references "
"the entry <" +
entries->get<std::string>(get_current_full_path(alias_name) +
path_separator + "alias") +
">."));
}
entries->put(get_current_full_path(alias_name) + path_separator + "alias",
existing_entry_name);
entries->put(get_current_full_path(alias_name) + path_separator +
"deprecation_status",
(alias_is_deprecated ? "true" : "false"));
}
void
ParameterHandler::enter_subsection(const std::string &subsection)
{
// if necessary create subsection
if (!entries->get_child_optional(get_current_full_path(subsection)))
entries->add_child(get_current_full_path(subsection),
boost::property_tree::ptree());
// then enter it
subsection_path.push_back(subsection);
}
void
ParameterHandler::leave_subsection()
{
// assert there is a subsection that
// we may leave
Assert(subsection_path.size() != 0, ExcAlreadyAtTopLevel());
if (subsection_path.size() > 0)
subsection_path.pop_back();
}
bool
ParameterHandler::subsection_path_exists(