/
gnc-option-impl.hpp
1356 lines (1275 loc) · 47.6 KB
/
gnc-option-impl.hpp
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
/********************************************************************\
* gnc-option-impl.hpp -- Application options system *
* Copyright (C) 2019 John Ralls <jralls@ceridwen.us> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
\********************************************************************/
#ifndef GNC_OPTION_IMPL_HPP_
#define GNC_OPTION_IMPL_HPP_
#include "gnc-option.hpp"
extern "C"
{
#include <config.h>
#include <qof.h>
#include <Account.h>
#include <gnc-budget.h>
#include <gnc-commodity.h>
}
#include <gnc-datetime.hpp>
#include <libguile.h>
#include <string>
#include <utility>
#include <vector>
#include <exception>
#include <functional>
#include <variant>
#include <iostream>
#include <limits>
#include "gnc-option-uitype.hpp"
/*
* Unused base class to document the structure of the current Scheme option
* vector, re-expressed in C++. The comment-numbers on the right indicate which
* item in the Scheme vector each item implements.
*
* Not everything here needs to be implemented, nor will it necessarily be
* implemented the same way. For example, part of the purpose of this redesign
* is to convert from saving options as strings of Scheme code to some form of
* key-value pair in the book options, so generate_restore_form() will likely be
* supplanted with save_to_book().
template <typename ValueType>
class GncOptionBase
{
public:
virtual ~GncOption = default;
virtual ValueType get_value() const = 0; //5
virtual ValueType get_default_value() = 0;
virtual SCM get_SCM_value() = 0;
virtual SCM get_SCM_default_value() const = 0; //7
virtual void set_value(ValueType) = 0; //6
// generate_restore_form outputs a Scheme expression (a "form") that finds an
// option and sets it to the current value. e.g.:
//(let ((option (gnc:lookup-option options
// "Display"
// "Amount")))
// ((lambda (option) (if option ((gnc:option-setter option) 'none))) option))
// it uses gnc:value->string to generate the "'none" (or whatever the option's
// value would be as input to the scheme interpreter).
virtual std::string generate_restore_form(); //8
virtual void save_to_book(QofBook*) const noexcept; //9
virtual void read_from_book(QofBook*); //10
virtual std::vector<std::string> get_option_strings(); //15
virtual set_changed_callback(std::function<void(void*)>); //14
protected:
const std::string m_section; //0
const std::string m_name; //1
const std::string m_sort_tag; //2
const std::type_info m_kvp_type; //3
const std::string m_doc_string; //4
std::function<void(void*)> m_changed_callback; //Part of the make-option closure
std::function<void(void*)>m_option_widget_changed_callback; //16
};
*/
static const char* commodity_scm_intro{"'(commodity-scm "};
#ifndef SWIG
size_t constexpr classifier_size_max{50};
size_t constexpr sort_tag_size_max{10};
#endif
struct OptionClassifier
{
std::string m_section;
std::string m_name;
std::string m_sort_tag;
// std::type_info m_kvp_type;
std::string m_doc_string;
};
#ifndef SWIG
auto constexpr size_t_max = std::numeric_limits<std::size_t>::max();
#endif
template <typename ValueType>
class GncOptionValue : public OptionClassifier
{
public:
GncOptionValue<ValueType>(const char* section, const char* name,
const char* key, const char* doc_string,
ValueType value,
GncOptionUIType ui_type = GncOptionUIType::INTERNAL) :
OptionClassifier{section, name, key, doc_string},
m_ui_type(ui_type), m_value{value}, m_default_value{value} {
if constexpr(std::is_same_v<std::decay_t<ValueType>, SCM>) {
if (value) {
scm_gc_protect_object(m_value);
scm_gc_protect_object(m_default_value);
}
}
}
GncOptionValue<ValueType>(const GncOptionValue<ValueType>& from) :
OptionClassifier{from.m_section, from.m_name, from.m_sort_tag,
from.m_doc_string},
m_ui_type(from.get_ui_type()), m_value{from.get_value()},
m_default_value{from.get_default_value()}
{
if constexpr(std::is_same_v<std::decay_t<ValueType>, SCM>) {
if (m_value)
scm_gc_protect_object(m_value);
if (m_default_value)
scm_gc_protect_object(m_default_value);
}
}
GncOptionValue<ValueType>(GncOptionValue<ValueType>&&) = default;
GncOptionValue<ValueType>& operator=(const GncOptionValue<ValueType>&) = default;
GncOptionValue<ValueType>& operator=(GncOptionValue<ValueType>&&) = default;
~GncOptionValue<ValueType>() {
if constexpr(std::is_same_v<std::decay_t<ValueType>, SCM>) {
if (m_value)
scm_gc_unprotect_object(m_value);
if (m_default_value)
scm_gc_unprotect_object(m_default_value);
}
}
ValueType get_value() const { return m_value; }
ValueType get_default_value() const { return m_default_value; }
void set_value(ValueType new_value);
void set_default_value(ValueType new_value);
void reset_default_value();
bool is_changed() const noexcept { return m_value != m_default_value; }
GncOptionUIType get_ui_type() const noexcept { return m_ui_type; }
void make_internal() { m_ui_type = GncOptionUIType::INTERNAL; }
private:
GncOptionUIType m_ui_type;
ValueType m_value;
ValueType m_default_value;
};
template <typename ValueType>
class GncOptionValidatedValue : public OptionClassifier
{
public:
GncOptionValidatedValue<ValueType>() = delete;
GncOptionValidatedValue<ValueType>(const char* section, const char* name,
const char* key, const char* doc_string,
ValueType value,
std::function<bool(ValueType)>validator,
GncOptionUIType ui_type = GncOptionUIType::INTERNAL
) :
OptionClassifier{section, name, key, doc_string},
m_ui_type{ui_type}, m_value{value}, m_default_value{value},
m_validator{validator}
{
if (!this->validate(value))
throw std::invalid_argument("Attempt to create GncValidatedOption with bad value.");
}
GncOptionValidatedValue<ValueType>(const char* section, const char* name,
const char* key, const char* doc_string,
ValueType value,
std::function<bool(ValueType)>validator,
ValueType val_data) :
OptionClassifier{section, name, key, doc_string},
m_ui_type{GncOptionUIType::INTERNAL}, m_value{value},
m_default_value{value}, m_validator{validator}, m_validation_data{val_data}
{
if (!this->validate(value))
throw std::invalid_argument("Attempt to create GncValidatedOption with bad value.");
}
GncOptionValidatedValue<ValueType>(const GncOptionValidatedValue<ValueType>&) = default;
GncOptionValidatedValue<ValueType>(GncOptionValidatedValue<ValueType>&&) = default;
GncOptionValidatedValue<ValueType>& operator=(const GncOptionValidatedValue<ValueType>&) = default;
GncOptionValidatedValue<ValueType>& operator=(GncOptionValidatedValue<ValueType>&&) = default;
ValueType get_value() const { return m_value; }
ValueType get_default_value() const { return m_default_value; }
bool validate(ValueType value) const { return m_validator(value); }
void set_value(ValueType value)
{
if (this->validate(value))
m_value = value;
else
throw std::invalid_argument("Validation failed, value not set.");
}
void set_default_value(ValueType value)
{
if (this->validate(value))
m_value = m_default_value = value;
else
throw std::invalid_argument("Validation failed, value not set.");
}
void reset_default_value() { m_value = m_default_value; }
bool is_changed() const noexcept { return m_value != m_default_value; }
std::ostream& to_scheme(std::ostream&) const;
std::istream& from_scheme(std::istream&);
GncOptionUIType get_ui_type() const noexcept { return m_ui_type; }
void make_internal() { m_ui_type = GncOptionUIType::INTERNAL; }
private:
GncOptionUIType m_ui_type;
ValueType m_value;
ValueType m_default_value;
std::function<bool(ValueType)> m_validator; //11
ValueType m_validation_data;
};
QofInstance* qof_instance_from_string(const std::string& str,
GncOptionUIType type);
QofInstance* qof_instance_from_guid(GncGUID*, GncOptionUIType type);
std::string qof_instance_to_string(const QofInstance* inst);
template <typename T>
struct is_QofInstanceValue
{
static constexpr bool value =
(std::is_same_v<std::decay_t<T>, GncOptionValue<const QofInstance*>> ||
std::is_same_v<std::decay_t<T>,
GncOptionValidatedValue<const QofInstance*>>);
};
template <typename T> inline constexpr bool
is_QofInstanceValue_v = is_QofInstanceValue<T>::value;
/* These will work when m_value is a built-in class; GnuCash class and container
* values will need specialization unless they happen to define operators << and
* >>.
* Note that SWIG 3.0.12 chokes on elaborate enable_if so just hide the
* following templates from SWIG. (Ignoring doesn't work because SWIG still has
* to parse the templates to figure out the symbols.
*/
#ifndef SWIG
template<class OptType,
typename std::enable_if_t<is_OptionClassifier_v<OptType> &&
! (is_QofInstanceValue_v<OptType> ||
is_RangeValue_v<OptType>), int> = 0>
std::ostream& operator<<(std::ostream& oss, const OptType& opt)
{
oss << opt.get_value();
return oss;
}
template<> inline std::ostream&
operator<< <GncOptionValue<bool>>(std::ostream& oss,
const GncOptionValue<bool>& opt)
{
oss << (opt.get_value() ? "#t" : "#f");
return oss;
}
template<class OptType,
typename std::enable_if_t<is_QofInstanceValue_v<OptType>, int> = 0>
inline std::ostream&
operator<< (std::ostream& oss, const OptType& opt)
{
auto value = opt.get_value();
if (auto type = opt.get_ui_type(); type == GncOptionUIType::COMMODITY ||
type == GncOptionUIType::CURRENCY)
{
if (auto type = opt.get_ui_type(); type == GncOptionUIType::COMMODITY)
{
oss << gnc_commodity_get_namespace(GNC_COMMODITY(value)) << " ";
}
oss << gnc_commodity_get_mnemonic(GNC_COMMODITY(value));
}
else
{
oss << qof_instance_to_string(value);
}
return oss;
}
template<class OptType,
typename std::enable_if_t<is_OptionClassifier_v<OptType> &&
!(is_QofInstanceValue_v<OptType> ||
is_RangeValue_v<OptType>), int> = 0>
std::istream& operator>>(std::istream& iss, OptType& opt)
{
std::decay_t<decltype(opt.get_value())> value;
if constexpr (std::is_same_v<std::decay_t<decltype(opt.get_value())>, SCM> ||
std::is_same_v<std::decay_t<decltype(opt.get_value())>, const _gncOwner*> ||
std::is_same_v<std::decay_t<decltype(opt.get_value())>, const _QofQuery*>)
return iss;
else
{
iss >> value;
opt.set_value(value);
return iss;
}
}
template<class OptType,
typename std::enable_if_t<is_QofInstanceValue_v<OptType>, int> = 0>
std::istream&
operator>> (std::istream& iss, OptType& opt)
{
std::string instr;
if (auto type = opt.get_ui_type(); type == GncOptionUIType::COMMODITY ||
type == GncOptionUIType::CURRENCY)
{
std::string name_space, mnemonic;
if (type = opt.get_ui_type(); type == GncOptionUIType::COMMODITY)
{
iss >> name_space;
}
else
name_space = GNC_COMMODITY_NS_CURRENCY;
iss >> mnemonic;
instr = name_space + ":";
instr += mnemonic;
}
else
{
iss >> instr;
}
opt.set_value(qof_instance_from_string(instr, opt.get_ui_type()));
return iss;
}
template<> inline std::istream&
operator>> <GncOptionValue<bool>>(std::istream& iss,
GncOptionValue<bool>& opt)
{
std::string instr;
iss >> instr;
opt.set_value(instr == "#t" ? true : false);
return iss;
}
template<class OptType,
typename std::enable_if_t<std::is_same_v<std::decay_t<OptType>,
GncOptionValidatedValue<const QofInstance*>> ||
std::is_same_v<std::decay_t<OptType>,
GncOptionValue<const QofInstance*>>,
int> = 0>
inline std::ostream&
gnc_option_to_scheme (std::ostream& oss, const OptType& opt)
{
auto value = opt.get_value();
auto type = opt.get_ui_type();
if (type == GncOptionUIType::COMMODITY || type == GncOptionUIType::CURRENCY)
{
if (type == GncOptionUIType::COMMODITY)
{
oss << commodity_scm_intro;
oss << "\"" <<
gnc_commodity_get_namespace(GNC_COMMODITY(value)) << "\" ";
}
oss << "\"" << gnc_commodity_get_mnemonic(GNC_COMMODITY(value)) << "\"";
if (type == GncOptionUIType::COMMODITY)
{
oss << ")";
}
return oss;
}
if constexpr (std::is_same_v<std::decay_t<decltype(value)>, std::string>)
{
if (type == GncOptionUIType::COLOR)
return output_color_value(oss, value);
}
oss << "\"" << qof_instance_to_string(value) << "\"";
return oss;
}
template<class OptType,
typename std::enable_if_t<
std::is_same_v<std::decay_t<OptType>,
GncOptionValidatedValue<const QofInstance*>> ||
std::is_same_v<std::decay_t<OptType>,
GncOptionValue<const QofInstance*>>, int> = 0>
inline std::istream&
gnc_option_from_scheme (std::istream& iss, OptType& opt)
{
std::string instr;
auto type = opt.get_ui_type();
if (type == GncOptionUIType::COMMODITY || type == GncOptionUIType::CURRENCY)
{
std::string name_space, mnemonic;
if (type == GncOptionUIType::COMMODITY)
{
iss.ignore(strlen(commodity_scm_intro) + 1, '"');
std::getline(iss, name_space, '"');
}
else
name_space = GNC_COMMODITY_NS_CURRENCY;
iss.ignore(2, '"');
std::getline(iss, mnemonic, '"');
if (type == GncOptionUIType::COMMODITY)
iss.ignore(2, ')');
else
iss.ignore(1, '"');
instr = name_space + ":";
instr += mnemonic;
}
else
{
iss.ignore(1, '"');
std::getline(iss, instr, '"');
}
opt.set_value(qof_instance_from_string(instr, opt.get_ui_type()));
return iss;
}
#endif // SWIG
/**
* Used for numeric ranges and plot sizes.
*/
template <typename ValueType>
class GncOptionRangeValue : public OptionClassifier
{
public:
GncOptionRangeValue<ValueType>(const char* section, const char* name,
const char* key, const char* doc_string,
ValueType value, ValueType min,
ValueType max, ValueType step) :
OptionClassifier{section, name, key, doc_string},
m_value{value >= min && value <= max ? value : min},
m_default_value{value >= min && value <= max ? value : min},
m_min{min}, m_max{max}, m_step{step} {}
GncOptionRangeValue<ValueType>(const GncOptionRangeValue<ValueType>&) = default;
GncOptionRangeValue<ValueType>(GncOptionRangeValue<ValueType>&&) = default;
GncOptionRangeValue<ValueType>& operator=(const GncOptionRangeValue<ValueType>&) = default;
GncOptionRangeValue<ValueType>& operator=(GncOptionRangeValue<ValueType>&&) = default;
ValueType get_value() const { return m_value; }
ValueType get_default_value() const { return m_default_value; }
bool validate(ValueType value) { return value >= m_min && value <= m_max; }
void set_value(ValueType value)
{
if (this->validate(value))
m_value = value;
else
throw std::invalid_argument("Validation failed, value not set.");
}
void set_default_value(ValueType value)
{
if (this->validate(value))
m_value = m_default_value = value;
else
throw std::invalid_argument("Validation failed, value not set.");
}
void get_limits(ValueType& upper, ValueType& lower, ValueType& step) const noexcept
{
upper = m_max;
lower = m_min;
step = m_step;
}
void reset_default_value() { m_value = m_default_value; }
bool is_changed() const noexcept { return m_value != m_default_value; }
GncOptionUIType get_ui_type() const noexcept { return m_ui_type; }
void make_internal() { m_ui_type = GncOptionUIType::INTERNAL; }
bool is_alternate() const noexcept { return m_alternate; }
void set_alternate(bool value) noexcept {
if (m_ui_type == GncOptionUIType::PLOT_SIZE)
m_alternate = value;
}
private:
GncOptionUIType m_ui_type = GncOptionUIType::NUMBER_RANGE;
ValueType m_value;
ValueType m_default_value;
ValueType m_min;
ValueType m_max;
ValueType m_step;
bool m_alternate = false;
};
template<class OptType,
typename std::enable_if_t<is_RangeValue_v<OptType>, int> = 0>
inline std::ostream&
operator<< (std::ostream& oss, const OptType& opt)
{
if (opt.get_ui_type() == GncOptionUIType::PLOT_SIZE)
oss << (opt.is_alternate() ? "pixels" : "percent") << " ";
oss << opt.get_value();
return oss;
}
template<class OptType,
typename std::enable_if_t<is_RangeValue_v<OptType>, int> = 0>
inline std::istream&
operator>> (std::istream& iss, OptType& opt)
{
if (opt.get_ui_type() == GncOptionUIType::PLOT_SIZE)
{
std::string alt;
iss >> alt;
opt.set_alternate(strncmp(alt.c_str(), "percent",
strlen("percent")) == 0);
}
if constexpr (std::is_same_v<std::decay_t<OptType>,
GncOptionRangeValue<double>>)
{
double d;
iss >> d;
opt.set_value(d);
}
else
{
int i;
iss >> i;
opt.set_value(i);
}
return iss;
}
using GncMultichoiceOptionEntry = std::tuple<const std::string,
const std::string,
GncOptionMultichoiceKeyType>;
using GncMultichoiceOptionIndexVec = std::vector<std::size_t>;
using GncMultichoiceOptionChoices = std::vector<GncMultichoiceOptionEntry>;
/** Multichoice options have a vector of valid options
* (GncMultichoiceOptionChoices) and validate the selection as being one of
* those values. The value is the index of the selected item in the vector. The
* tuple contains three strings, a key, and a display
* name, which * should be localized at the point of use.
*
*
*/
class GncOptionMultichoiceValue : public OptionClassifier
{
public:
GncOptionMultichoiceValue(const char* section, const char* name,
const char* key, const char* doc_string,
const char* value,
GncMultichoiceOptionChoices&& choices,
GncOptionUIType ui_type = GncOptionUIType::MULTICHOICE) :
OptionClassifier{section, name, key, doc_string},
m_ui_type{ui_type},
m_value{}, m_default_value{}, m_choices{std::move(choices)}
{
if (value)
{
if (auto index = find_key(value);
index != size_t_max)
{
m_value.push_back(index);
m_default_value.push_back(index);
}
}
}
GncOptionMultichoiceValue(const char* section, const char* name,
const char* key, const char* doc_string,
size_t index,
GncMultichoiceOptionChoices&& choices,
GncOptionUIType ui_type = GncOptionUIType::MULTICHOICE) :
OptionClassifier{section, name, key, doc_string},
m_ui_type{ui_type},
m_value{}, m_default_value{}, m_choices{std::move(choices)}
{
if (index < m_choices.size())
{
m_value.push_back(index);
m_default_value.push_back(index);
}
}
GncOptionMultichoiceValue(const char* section, const char* name,
const char* key, const char* doc_string,
GncMultichoiceOptionIndexVec&& indices,
GncMultichoiceOptionChoices&& choices,
GncOptionUIType ui_type = GncOptionUIType::LIST) :
OptionClassifier{section, name, key, doc_string},
m_ui_type{ui_type},
m_value{indices}, m_default_value{std::move(indices)},
m_choices{std::move(choices)} {}
GncOptionMultichoiceValue(const GncOptionMultichoiceValue&) = default;
GncOptionMultichoiceValue(GncOptionMultichoiceValue&&) = default;
GncOptionMultichoiceValue& operator=(const GncOptionMultichoiceValue&) = default;
GncOptionMultichoiceValue& operator=(GncOptionMultichoiceValue&&) = default;
const std::string& get_value() const
{
auto vec{m_value.size() > 0 ? m_value : m_default_value};
if (vec.size() == 0)
return c_empty_string;
if (vec.size() == 1)
return std::get<0>(m_choices.at(vec[0]));
else
return c_list_string;
}
const std::string& get_default_value() const
{
if (m_default_value.size() == 1)
return std::get<0>(m_choices.at(m_default_value[0]));
else if (m_default_value.size() == 0)
return c_empty_string;
else
return c_list_string;
}
size_t get_index() const
{
if (m_value.size() > 0)
return m_value[0];
if (m_default_value.size() > 0)
return m_default_value[0];
return 0;
}
const GncMultichoiceOptionIndexVec& get_multiple() const noexcept
{
return m_value;
}
const GncMultichoiceOptionIndexVec& get_default_multiple() const noexcept
{
return m_default_value;
}
bool validate(const std::string& value) const noexcept
{
auto index = find_key(value);
return index != size_t_max;
}
bool validate(const GncMultichoiceOptionIndexVec& indexes) const noexcept
{
for (auto index : indexes)
if (index >= m_choices.size())
return false;
return true;
}
void set_value(const std::string& value)
{
auto index = find_key(value);
if (index != size_t_max)
{
m_value.clear();
m_value.push_back(index);
}
else
throw std::invalid_argument("Value not a valid choice.");
}
void set_value(size_t index)
{
if (index < m_choices.size())
{
m_value.clear();
m_value.push_back(index);
}
else
throw std::invalid_argument("Value not a valid choice.");
}
void set_default_value(const std::string& value)
{
auto index = find_key(value);
if (index != size_t_max)
{
m_value.clear();
m_value.push_back(index);
m_default_value.clear();
m_default_value.push_back(index);
}
else
throw std::invalid_argument("Value not a valid choice.");
}
void set_default_value(size_t index)
{
if (index < m_choices.size())
{
m_value.clear();
m_value.push_back(index);
m_default_value.clear();
m_default_value.push_back(index);
}
else
throw std::invalid_argument("Value not a valid choice.");
}
void set_multiple(const GncMultichoiceOptionIndexVec& indexes)
{
if (validate(indexes))
m_value = indexes;
else
throw std::invalid_argument("One of the supplied indexes was out of range.");
}
void set_default_multiple(const GncMultichoiceOptionIndexVec& indexes)
{
if (validate(indexes))
m_value = m_default_value = indexes;
else
throw std::invalid_argument("One of the supplied indexes was out of range.");
}
std::size_t num_permissible_values() const noexcept
{
return m_choices.size();
}
std::size_t permissible_value_index(const char* key) const noexcept
{
return find_key(key);
}
const char* permissible_value(std::size_t index) const
{
return std::get<0>(m_choices.at(index)).c_str();
}
const char* permissible_value_name(std::size_t index) const
{
return std::get<1>(m_choices.at(index)).c_str();
}
void reset_default_value() { m_value = m_default_value; }
bool is_changed() const noexcept { return m_value != m_default_value; }
GncOptionUIType get_ui_type() const noexcept { return m_ui_type; }
void make_internal() { m_ui_type = GncOptionUIType::INTERNAL; }
GncOptionMultichoiceKeyType get_keytype(unsigned i) const { return std::get<2>(m_choices.at(i)); }
private:
std::size_t find_key (const std::string& key) const noexcept
{
auto iter = std::find_if(m_choices.begin(), m_choices.end(),
[key](auto choice) {
return std::get<0>(choice) == key; });
if (iter != m_choices.end())
return iter - m_choices.begin();
else
return size_t_max;
}
GncOptionUIType m_ui_type;
GncMultichoiceOptionIndexVec m_value;
GncMultichoiceOptionIndexVec m_default_value;
GncMultichoiceOptionChoices m_choices;
static const std::string c_empty_string;
static const std::string c_list_string;
};
template<> inline std::ostream&
operator<< <GncOptionMultichoiceValue>(std::ostream& oss,
const GncOptionMultichoiceValue& opt)
{
auto vec{opt.get_multiple()};
bool first{true};
for (auto index : vec)
{
if (first)
first = false;
else
oss << " ";
oss << opt.permissible_value(index);
}
return oss;
}
template<> inline std::istream&
operator>> <GncOptionMultichoiceValue>(std::istream& iss,
GncOptionMultichoiceValue& opt)
{
GncMultichoiceOptionIndexVec values;
while (true)
{
std::string str;
std::getline(iss, str, ' ');
if (!str.empty())
{
auto index = opt.permissible_value_index(str.c_str());
if (index != size_t_max)
values.push_back(index);
else
{
std::string err = str + " is not one of ";
err += opt.m_name;
err += "'s permissible values.";
throw std::invalid_argument(err);
}
}
else
break;
}
opt.set_multiple(values);
iss.clear();
return iss;
}
template<class OptType,
typename std::enable_if_t<std::is_same_v<std::decay_t<OptType>,
GncOptionMultichoiceValue>,
int> = 0>
inline std::ostream&
gnc_option_to_scheme(std::ostream& oss, const OptType& opt)
{
auto indexes{opt.get_multiple()};
if (indexes.size() > 1)
oss << "'(";
bool first = true;
for (auto index : indexes)
{
if (first)
first = false;
else
oss << " ";
oss << "'" << opt.permissible_value(index);
}
if (indexes.size() > 1)
oss << ')';
return oss;
}
template<class OptType,
typename std::enable_if_t<std::is_same_v<std::decay_t<OptType>,
GncOptionMultichoiceValue>,
int> = 0>
inline std::istream&
gnc_option_from_scheme(std::istream& iss, OptType& opt)
{
iss.ignore(3, '\'');
auto c{iss.peek()};
if (static_cast<char>(c) == '(')
{
GncMultichoiceOptionIndexVec values;
iss.ignore(3, '\'');
while (true)
{
std::string str;
std::getline(iss, str, ' ');
if (!str.empty())
{
if (str.back() == ')')
{
str.pop_back();
break;
}
values.push_back(opt.permissible_value_index(str.c_str()));
iss.ignore(2, '\'');
}
else
break;
}
opt.set_multiple(values);
}
else
{
std::string str;
std::getline(iss, str, ' ');
opt.set_value(str);
}
return iss;
}
using GncOptionAccountList = std::vector<const Account*>;
using GncOptionAccountTypeList = std::vector<GNCAccountType>;
/** Account options
*
* Set one or more accounts on which to report, optionally restricted to certain
* account types. Many calls to make-account-list-option will pass a get-default
* function that retrieves all of the accounts of a list of types.
*
* Some reports (examples/daily-reports.scm and standard/ account-piechart.scm,
* advanced-portfolio.scm, category-barchart.scm, net-charts.scm, and
* portfolio.scm) also provide a validator that rejects accounts that don't meet
* an account-type criterion.
*
* There are two types of option, account-list which permits more than one
* account selection and account-sel, which doesn't.
*
*/
class GncOptionAccountListValue : public OptionClassifier
{
public:
GncOptionAccountListValue(const char* section, const char* name,
const char* key, const char* doc_string,
GncOptionUIType ui_type, bool multi=true) :
OptionClassifier{section, name, key, doc_string}, m_ui_type{ui_type},
m_value{}, m_default_value{}, m_allowed{}, m_multiselect{multi} {}
GncOptionAccountListValue(const char* section, const char* name,
const char* key, const char* doc_string,
GncOptionUIType ui_type,
const GncOptionAccountList& value, bool multi=true) :
OptionClassifier{section, name, key, doc_string}, m_ui_type{ui_type},
m_value{value}, m_default_value{std::move(value)}, m_allowed{},
m_multiselect{multi} {}
GncOptionAccountListValue(const char* section, const char* name,
const char* key, const char* doc_string,
GncOptionUIType ui_type,
GncOptionAccountTypeList&& allowed, bool multi=true) :
OptionClassifier{section, name, key, doc_string}, m_ui_type{ui_type},
m_value{}, m_default_value{}, m_allowed{std::move(allowed)},
m_multiselect{multi} {}
GncOptionAccountListValue(const char* section, const char* name,
const char* key, const char* doc_string,
GncOptionUIType ui_type,
const GncOptionAccountList& value,
GncOptionAccountTypeList&& allowed, bool multi=true) :
OptionClassifier{section, name, key, doc_string}, m_ui_type{ui_type},
m_value{}, m_default_value{}, m_allowed{std::move(allowed)},
m_multiselect{multi} {
if (!validate(value))
throw std::invalid_argument("Supplied Value not in allowed set.");
m_value = value;
m_default_value = std::move(value);
}
/* These aren't const& because if m_default_value hasn't been set
* get_default_value finds the first account that matches the allowed types
* and returns a GncOptionAccountList containing it. That's a stack variable
* and must be returned by value.
*/
GncOptionAccountList get_value() const;
GncOptionAccountList get_default_value() const;
bool validate (const GncOptionAccountList& values) const;
void set_value (GncOptionAccountList values) {
if (validate(values))
//throw!
m_value = values;
}
void set_default_value (GncOptionAccountList values) {
if (validate(values))
//throw!
m_value = m_default_value = values;
}
GList* account_type_list() const noexcept;
void reset_default_value() { m_value = m_default_value; }
bool is_changed() const noexcept { return m_value != m_default_value; }
GncOptionUIType get_ui_type() const noexcept { return m_ui_type; }
void make_internal() { m_ui_type = GncOptionUIType::INTERNAL; }
bool is_multiselect() const noexcept { return m_multiselect; }
private:
GncOptionUIType m_ui_type;
GncOptionAccountList m_value;
GncOptionAccountList m_default_value;
GncOptionAccountTypeList m_allowed;
bool m_multiselect;
};
template<> inline std::ostream&
operator<< <GncOptionAccountListValue>(std::ostream& oss,
const GncOptionAccountListValue& opt)
{
auto values{opt.get_value()};
bool first = true;
for (auto value : values)
{
if (first)
first = false;
else
oss << " ";
oss << qof_instance_to_string(QOF_INSTANCE(value));
}
return oss;
}
template<> inline std::istream&
operator>> <GncOptionAccountListValue>(std::istream& iss,
GncOptionAccountListValue& opt)
{
GncOptionAccountList values;
while (true)
{
std::string str;
std::getline(iss, str, ' ');
if (!str.empty())
values.emplace_back((Account*)qof_instance_from_string(str, opt.get_ui_type()));
else
break;
}
opt.set_value(values);
iss.clear();
return iss;
}