forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdalalgorithm.h
2185 lines (1809 loc) · 70.1 KB
/
gdalalgorithm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Project: GDAL
* Purpose: GDALAlgorithm class
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#ifndef GDAL_ALGORITHM_INCLUDED
#define GDAL_ALGORITHM_INCLUDED
#include "cpl_port.h"
#include "cpl_progress.h"
#include "gdal.h"
/************************************************************************/
/************************************************************************/
/* GDAL Algorithm C API */
/************************************************************************/
/************************************************************************/
CPL_C_START
/** Type of an argument */
typedef enum GDALAlgorithmArgType
{
/** Boolean type. Value is a bool. */
GAAT_BOOLEAN,
/** Single-value string type. Value is a std::string */
GAAT_STRING,
/** Single-value integer type. Value is a int */
GAAT_INTEGER,
/** Single-value real type. Value is a double */
GAAT_REAL,
/** Dataset type. Value is a GDALArgDatasetValue */
GAAT_DATASET,
/** Multi-value string type. Value is a std::vector<std::string> */
GAAT_STRING_LIST,
/** Multi-value integer type. Value is a std::vector<int> */
GAAT_INTEGER_LIST,
/** Multi-value real type. Value is a std::vector<double> */
GAAT_REAL_LIST,
/** Multi-value dataset type. Value is a std::vector<GDALArgDatasetValue> */
GAAT_DATASET_LIST,
} GDALAlgorithmArgType;
/** Return whether the argument type is a list / multi-valued one. */
bool CPL_DLL GDALAlgorithmArgTypeIsList(GDALAlgorithmArgType type);
/** Return the string representation of the argument type */
const char CPL_DLL *GDALAlgorithmArgTypeName(GDALAlgorithmArgType type);
/** Opaque C type for GDALArgDatasetValue */
typedef struct GDALArgDatasetValueHS *GDALArgDatasetValueH;
/** Opaque C type for GDALAlgorithmArg */
typedef struct GDALAlgorithmArgHS *GDALAlgorithmArgH;
/** Opaque C type for GDALAlgorithm */
typedef struct GDALAlgorithmHS *GDALAlgorithmH;
/** Opaque C type for GDALAlgorithmRegistry */
typedef struct GDALAlgorithmRegistryHS *GDALAlgorithmRegistryH;
/************************************************************************/
/* GDALAlgorithmRegistryH API */
/************************************************************************/
GDALAlgorithmRegistryH CPL_DLL GDALGetGlobalAlgorithmRegistry(void);
void CPL_DLL GDALAlgorithmRegistryRelease(GDALAlgorithmRegistryH);
char CPL_DLL **GDALAlgorithmRegistryGetAlgNames(GDALAlgorithmRegistryH);
GDALAlgorithmH CPL_DLL GDALAlgorithmRegistryInstantiateAlg(
GDALAlgorithmRegistryH, const char *pszAlgName);
/************************************************************************/
/* GDALAlgorithmH API */
/************************************************************************/
void CPL_DLL GDALAlgorithmRelease(GDALAlgorithmH);
const char CPL_DLL *GDALAlgorithmGetName(GDALAlgorithmH);
const char CPL_DLL *GDALAlgorithmGetDescription(GDALAlgorithmH);
const char CPL_DLL *GDALAlgorithmGetLongDescription(GDALAlgorithmH);
const char CPL_DLL *GDALAlgorithmGetHelpFullURL(GDALAlgorithmH);
bool CPL_DLL GDALAlgorithmHasSubAlgorithms(GDALAlgorithmH);
char CPL_DLL **GDALAlgorithmGetSubAlgorithmNames(GDALAlgorithmH);
GDALAlgorithmH CPL_DLL
GDALAlgorithmInstantiateSubAlgorithm(GDALAlgorithmH, const char *pszSubAlgName);
bool CPL_DLL GDALAlgorithmParseCommandLineArguments(GDALAlgorithmH,
CSLConstList papszArgs);
GDALAlgorithmH CPL_DLL GDALAlgorithmGetActualAlgorithm(GDALAlgorithmH);
bool CPL_DLL GDALAlgorithmRun(GDALAlgorithmH, GDALProgressFunc pfnProgress,
void *pProgressData);
bool CPL_DLL GDALAlgorithmFinalize(GDALAlgorithmH);
char CPL_DLL *GDALAlgorithmGetUsageAsJSON(GDALAlgorithmH);
char CPL_DLL **GDALAlgorithmGetArgNames(GDALAlgorithmH);
GDALAlgorithmArgH CPL_DLL GDALAlgorithmGetArg(GDALAlgorithmH,
const char *pszArgName);
/************************************************************************/
/* GDALAlgorithmArgH API */
/************************************************************************/
void CPL_DLL GDALAlgorithmArgRelease(GDALAlgorithmArgH);
const char CPL_DLL *GDALAlgorithmArgGetName(GDALAlgorithmArgH);
GDALAlgorithmArgType CPL_DLL GDALAlgorithmArgGetType(GDALAlgorithmArgH);
const char CPL_DLL *GDALAlgorithmArgGetDescription(GDALAlgorithmArgH);
const char CPL_DLL *GDALAlgorithmArgGetShortName(GDALAlgorithmArgH);
char CPL_DLL **GDALAlgorithmArgGetAliases(GDALAlgorithmArgH);
const char CPL_DLL *GDALAlgorithmArgGetMetaVar(GDALAlgorithmArgH);
const char CPL_DLL *GDALAlgorithmArgGetCategory(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgIsPositional(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgIsRequired(GDALAlgorithmArgH);
int CPL_DLL GDALAlgorithmArgGetMinCount(GDALAlgorithmArgH);
int CPL_DLL GDALAlgorithmArgGetMaxCount(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgGetPackedValuesAllowed(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgGetRepeatedArgAllowed(GDALAlgorithmArgH);
char CPL_DLL **GDALAlgorithmArgGetChoices(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgIsExplicitlySet(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgHasDefaultValue(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgIsHiddenForCLI(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgIsOnlyForCLI(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgIsInput(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgIsOutput(GDALAlgorithmArgH);
const char CPL_DLL *GDALAlgorithmArgGetMutualExclusionGroup(GDALAlgorithmArgH);
bool CPL_DLL GDALAlgorithmArgGetAsBoolean(GDALAlgorithmArgH);
const char CPL_DLL *GDALAlgorithmArgGetAsString(GDALAlgorithmArgH);
GDALArgDatasetValueH
CPL_DLL GDALAlgorithmArgGetAsDatasetValue(GDALAlgorithmArgH);
int CPL_DLL GDALAlgorithmArgGetAsInteger(GDALAlgorithmArgH);
double CPL_DLL GDALAlgorithmArgGetAsDouble(GDALAlgorithmArgH);
char CPL_DLL **GDALAlgorithmArgGetAsStringList(GDALAlgorithmArgH);
const int CPL_DLL *GDALAlgorithmArgGetAsIntegerList(GDALAlgorithmArgH,
size_t *pnCount);
const double CPL_DLL *GDALAlgorithmArgGetAsDoubleList(GDALAlgorithmArgH,
size_t *pnCount);
bool CPL_DLL GDALAlgorithmArgSetAsBoolean(GDALAlgorithmArgH, bool);
bool CPL_DLL GDALAlgorithmArgSetAsString(GDALAlgorithmArgH, const char *);
bool CPL_DLL GDALAlgorithmArgSetAsDatasetValue(GDALAlgorithmArgH hArg,
GDALArgDatasetValueH value);
bool CPL_DLL GDALAlgorithmArgSetDataset(GDALAlgorithmArgH hArg, GDALDatasetH);
bool CPL_DLL GDALAlgorithmArgSetAsInteger(GDALAlgorithmArgH, int);
bool CPL_DLL GDALAlgorithmArgSetAsDouble(GDALAlgorithmArgH, double);
bool CPL_DLL GDALAlgorithmArgSetAsStringList(GDALAlgorithmArgH, CSLConstList);
bool CPL_DLL GDALAlgorithmArgSetAsIntegerList(GDALAlgorithmArgH, size_t nCount,
const int *pnValues);
bool CPL_DLL GDALAlgorithmArgSetAsDoubleList(GDALAlgorithmArgH, size_t nCount,
const double *pnValues);
/************************************************************************/
/* GDALArgDatasetValueH API */
/************************************************************************/
GDALArgDatasetValueH CPL_DLL GDALArgDatasetValueCreate(void);
void CPL_DLL GDALArgDatasetValueRelease(GDALArgDatasetValueH);
const char CPL_DLL *GDALArgDatasetValueGetName(GDALArgDatasetValueH);
GDALDatasetH CPL_DLL GDALArgDatasetValueGetDatasetRef(GDALArgDatasetValueH);
GDALDatasetH
CPL_DLL GDALArgDatasetValueGetDatasetIncreaseRefCount(GDALArgDatasetValueH);
/** Bit indicating that the name component of GDALArgDatasetValue is accepted. */
#define GADV_NAME (1 << 0)
/** Bit indicating that the dataset component of GDALArgDatasetValue is accepted. */
#define GADV_OBJECT (1 << 1)
/** Binary-or combination of GDAL_OF_RASTER, GDAL_OF_VECTOR and
* GDAL_OF_MULTIDIM_RASTER.
*/
typedef int GDALArgDatasetValueType;
GDALArgDatasetValueType
CPL_DLL GDALArgDatasetValueGetType(GDALArgDatasetValueH);
int CPL_DLL GDALArgDatasetValueGetInputFlags(GDALArgDatasetValueH);
int CPL_DLL GDALArgDatasetValueGetOutputFlags(GDALArgDatasetValueH);
void CPL_DLL GDALArgDatasetValueSetName(GDALArgDatasetValueH, const char *);
void CPL_DLL GDALArgDatasetValueSetDataset(GDALArgDatasetValueH, GDALDatasetH);
CPL_C_END
/************************************************************************/
/************************************************************************/
/* GDAL Algorithm C++ API */
/************************************************************************/
/************************************************************************/
// The rest of this header requires C++17
// _MSC_VER >= 1920 : Visual Studio >= 2019
#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && \
(defined(DOXYGEN_SKIP) || __cplusplus >= 201703L || _MSC_VER >= 1920)
#include "cpl_error.h"
#include <limits>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
class GDALDataset;
/** Common argument category */
constexpr const char *GAAC_COMMON = "Common";
/** Base argument category */
constexpr const char *GAAC_BASE = "Base";
/** Advanced argument category */
constexpr const char *GAAC_ADVANCED = "Advanced";
/** Esoteric argument category */
constexpr const char *GAAC_ESOTERIC = "Esoteric";
/** Argument metadata item that applies to the "input-format" and
* "output-format" argument */
constexpr const char *GAAMDI_REQUIRED_CAPABILITIES = "required_capabilities";
/** Name of the argument for an input dataset. */
constexpr const char *GDAL_ARG_NAME_INPUT = "input";
/** Name of the argument for an output dataset. */
constexpr const char *GDAL_ARG_NAME_OUTPUT = "output";
/** Name of the argument for update. */
constexpr const char *GDAL_ARG_NAME_UPDATE = "update";
/************************************************************************/
/* GDALArgDatasetValue */
/************************************************************************/
/** Return the string representation of GDALArgDatasetValueType */
std::string CPL_DLL GDALArgDatasetValueTypeName(GDALArgDatasetValueType);
class GDALAlgorithmArg;
/** Value for an argument that points to a GDALDataset.
*
* This is the value of arguments of type GAAT_DATASET, GAAT_RASTER_DATASET,
* GAAT_VECTOR_DATASET, GAAT_MULTIDIM_DATASET, GAAT_DATASET_LIST,
* GAAT_RASTER_DATASET_LIST, GAAT_VECTOR_DATASET_LIST,
* GAAT_MULTIDIM_DATASET_LIST
*/
class CPL_DLL GDALArgDatasetValue final
{
public:
/** Default (empty) constructor */
GDALArgDatasetValue() = default;
/** Constructor by dataset name. */
explicit GDALArgDatasetValue(const std::string &name) : m_name(name)
{
}
/** Constructor by dataset instance, increasing its reference counter */
explicit GDALArgDatasetValue(GDALDataset *poDS);
/** Move constructor */
GDALArgDatasetValue(GDALArgDatasetValue &&other);
/** Destructor. Decrease m_poDS reference count, and destroy it if no
* longer referenced. */
~GDALArgDatasetValue();
/** Dereference the dataset object and close it if no longer referenced.
* Return an error if an error occurred during dataset closing. */
bool Close();
/** Move-assignment operator */
GDALArgDatasetValue &operator=(GDALArgDatasetValue &&other);
/** Get the GDALDataset* instance (may be null), and increase its reference
* count if not null. Once done with the dataset, the caller should call
* GDALDataset::Release().
*/
GDALDataset *GetDatasetIncreaseRefCount();
/** Get a GDALDataset* instance (may be null). This does not modify the
* reference counter, hence the lifetime of the returned object is not
* guaranteed to exceed the one of this instance.
*/
GDALDataset *GetDatasetRef()
{
return m_poDS;
}
/** Borrow the GDALDataset* instance (may be null), leaving its reference
* counter unchanged.
*/
GDALDataset *BorrowDataset()
{
GDALDataset *ret = m_poDS;
m_poDS = nullptr;
return ret;
}
/** Borrow the GDALDataset* instance from another GDALArgDatasetValue,
* leaving its reference counter unchange.
*/
void BorrowDatasetFrom(GDALArgDatasetValue &other)
{
Close();
m_poDS = other.BorrowDataset();
m_name = other.m_name;
}
/** Get dataset name */
const std::string &GetName() const
{
return m_name;
}
/** Return whether a dataset name has been set */
bool IsNameSet() const
{
return m_nameSet;
}
/** Indicates which components among name and dataset are accepted as
* input, when this argument serves as an input.
*
* If the GADV_NAME bit is set, it indicates a dataset name is accepted as
* input.
* If the GADV_OBJECT bit is set, it indicates a dataset object is
* accepted as input.
* If both bits are set, the algorithm can accept either a name or a dataset
* object.
*/
int GetInputFlags() const
{
return m_inputFlags;
}
/** Indicates which components among name and dataset are modified,
* when this argument serves as an output.
*
* If the GADV_NAME bit is set, it indicates a dataset name is generated as
* output (that is the algorithm will generate the name. Rarely used).
* If the GADV_OBJECT bit is set, it indicates a dataset object is
* generated as output, and available for use after the algorithm has
* completed.
*/
int GetOutputFlags() const
{
return m_outputFlags;
}
/** Set dataset name */
void Set(const std::string &name);
/** Transfer dataset to this instance (does not affect is reference
* counter). */
void Set(std::unique_ptr<GDALDataset> poDS);
/** Set dataset object, increasing its reference counter. */
void Set(GDALDataset *poDS);
/** Set from other value, increasing the reference counter of the
* GDALDataset object.
*/
void SetFrom(const GDALArgDatasetValue &other);
/** Set which components among name and dataset are accepted as
* input, when this argument serves as an input.
* Should only be used by GDALAlgorithm sub-classes.
*/
void SetInputFlags(int flags)
{
m_inputFlags = flags;
}
/** Set which components among name and dataset are modified when this
* argument serves as an output.
* Should only be used by GDALAlgorithm sub-classes.
*/
void SetOutputFlags(int flags)
{
m_outputFlags = flags;
}
/** Get which type of dataset is allowed / generated.
* Binary-or combination of GDAL_OF_RASTER, GDAL_OF_VECTOR and
* GDAL_OF_MULTIDIM_RASTER.
*/
GDALArgDatasetValueType GetType() const
{
return m_type;
}
/** Set which type of dataset is allowed / generated.
* Binary-or combination of GDAL_OF_RASTER, GDAL_OF_VECTOR and
* GDAL_OF_MULTIDIM_RASTER.
*/
void SetType(GDALArgDatasetValueType type)
{
m_type = type;
}
protected:
friend class GDALAlgorithm;
/** Set the argument that owns us. */
void SetOwnerArgument(GDALAlgorithmArg *arg)
{
CPLAssert(!m_ownerArg);
m_ownerArg = arg;
}
private:
/** The owner argument (may be nullptr for freestanding objects) */
GDALAlgorithmArg *m_ownerArg = nullptr;
/** Dataset object. */
GDALDataset *m_poDS = nullptr;
/** Dataset name */
std::string m_name{};
/** Whether a dataset name (possibly empty for a MEM dataset...) has been set */
bool m_nameSet = false;
/** Dataset type */
GDALArgDatasetValueType m_type =
GDAL_OF_RASTER | GDAL_OF_VECTOR | GDAL_OF_MULTIDIM_RASTER;
/** Which components among name and dataset are accepted as
* input, when this argument serves as an input.
*/
int m_inputFlags = GADV_NAME | GADV_OBJECT;
/** Which components among name and dataset are generated as
* output, when this argument serves as an output.
*/
int m_outputFlags = GADV_OBJECT;
GDALArgDatasetValue(const GDALArgDatasetValue &) = delete;
GDALArgDatasetValue &operator=(const GDALArgDatasetValue &) = delete;
};
/************************************************************************/
/* GDALAlgorithmArgDecl */
/************************************************************************/
/** Argument declaration.
*
* It does not hold its value.
*/
class CPL_DLL GDALAlgorithmArgDecl final
{
public:
/** Special value for the SetMaxCount() / GetMaxCount() to indicate
* unlimited number of values. */
static constexpr int UNBOUNDED = std::numeric_limits<int>::max();
/** Constructor.
*
* @param longName Long name. Must be 2 characters at least. Must not start
* with dash.
* @param chShortName 1-letter short name, or NUL character
* @param description Description.
* @param type Type of the argument.
*/
GDALAlgorithmArgDecl(const std::string &longName, char chShortName,
const std::string &description,
GDALAlgorithmArgType type);
/** Declare an alias. Must be 2 characters at least. */
GDALAlgorithmArgDecl &AddAlias(const std::string &alias)
{
m_aliases.push_back(alias);
return *this;
}
/** Declare an hidden alias (i.e. not exposed in usage).
* Must be 2 characters at least. */
GDALAlgorithmArgDecl &AddHiddenAlias(const std::string &alias)
{
m_hiddenAliases.push_back(alias);
return *this;
}
/** Declare that the argument is positional. Typically input / output files
*/
GDALAlgorithmArgDecl &SetPositional()
{
m_positional = true;
return *this;
}
/** Declare that the argument is required. Default is no
*/
GDALAlgorithmArgDecl &SetRequired()
{
m_required = true;
return *this;
}
/** Declare the "meta-var" hint.
* By default, the meta-var value is the long name of the argument in
* upper case.
*/
GDALAlgorithmArgDecl &SetMetaVar(const std::string &metaVar)
{
m_metaVar = metaVar;
return *this;
}
/** Declare the argument category: GAAC_COMMON, GAAC_BASE, GAAC_ADVANCED,
* GAAC_ESOTERIC or a custom category.
*/
GDALAlgorithmArgDecl &SetCategory(const std::string &category)
{
m_category = category;
return *this;
}
/** Declare a default value for the argument.
*/
template <class T> GDALAlgorithmArgDecl &SetDefault(const T &value)
{
m_hasDefaultValue = true;
if constexpr (std::is_same_v<T, int>)
{
if (m_type == GAAT_REAL)
{
m_defaultValue = static_cast<double>(value);
return *this;
}
}
m_defaultValue = value;
return *this;
}
/** Declare the minimum number of values for the argument. Defaults to 0.
* Only applies to list type of arguments.
* Setting it to non-zero does *not* make the argument required. It just
* sets the minimum number of values when it is specified. To also make
* it required, use SetRequired().
*/
GDALAlgorithmArgDecl &SetMinCount(int count);
/** Declare the maximum number of values for the argument.
* Defaults to 1 for scalar types, and UNBOUNDED for list types.
* Only applies to list type of arguments.
*/
GDALAlgorithmArgDecl &SetMaxCount(int count);
/** Declare whether in --help message one should display hints about the
* minimum/maximum number of values. Defaults to true.
*/
GDALAlgorithmArgDecl &SetDisplayHintAboutRepetition(bool displayHint)
{
m_displayHintAboutRepetition = displayHint;
return *this;
}
/** Declares whether, for list type of arguments, several values, space
* separated, may be specified. That is "--foo=bar,baz".
* The default is true.
*/
GDALAlgorithmArgDecl &SetPackedValuesAllowed(bool allowed)
{
m_packedValuesAllowed = allowed;
return *this;
}
/** Declares whether, for list type of arguments, the argument may be
* repeated. That is "--foo=bar --foo=baz".
* The default is true.
*/
GDALAlgorithmArgDecl &SetRepeatedArgAllowed(bool allowed)
{
m_repeatedArgAllowed = allowed;
return *this;
}
//! @cond Doxygen_Suppress
GDALAlgorithmArgDecl &SetChoices()
{
return *this;
}
//! @endcond
/** Declares the allowed values (as strings) for the argument.
* Only honored for GAAT_STRING and GAAT_STRING_LIST types.
*/
template <typename T, typename... U>
GDALAlgorithmArgDecl &SetChoices(T &&first, U &&...rest)
{
m_choices.push_back(std::forward<T>(first));
SetChoices(std::forward<U>(rest)...);
return *this;
}
/** Declare that the argument must not be mentioned in CLI usage.
* For example, "output-value" for "gdal raster info", which is only
* meant when the algorithm is used from a non-CLI context.
*/
GDALAlgorithmArgDecl &SetHiddenForCLI(bool hiddenForCLI = true)
{
m_hiddenForCLI = hiddenForCLI;
return *this;
}
/** Declare that the argument is only for CLI usage.
* For example "--help" */
GDALAlgorithmArgDecl &SetOnlyForCLI(bool onlyForCLI = true)
{
m_onlyForCLI = onlyForCLI;
return *this;
}
/** Indicate whether the value of the argument is read-only during the
* execution of the algorithm. Default is true.
*/
GDALAlgorithmArgDecl &SetIsInput(bool isInput = true)
{
m_isInput = isInput;
return *this;
}
/** Indicate whether (at least part of) the value of the argument is set
* during the execution of the algorithm.
* For example, "output-value" for "gdal raster info"
* Default is false.
* An argument may return both IsInput() and IsOutput() as true.
* For example the "gdal raster convert" algorithm consumes the dataset
* name of its "output" argument, and sets the dataset object during its
* execution.
*/
GDALAlgorithmArgDecl &SetIsOutput(bool isOutput = true)
{
m_isOutput = isOutput;
return *this;
}
/** Set the name of the mutual exclusion group to which this argument
* belongs to. At most one argument in a group can be specified.
*/
GDALAlgorithmArgDecl &SetMutualExclusionGroup(const std::string &group)
{
m_mutualExclusionGroup = group;
return *this;
}
/** Set user-defined metadata item.
*/
GDALAlgorithmArgDecl &
AddMetadataItem(const std::string &name,
const std::vector<std::string> &values)
{
m_metadata[name] = values;
return *this;
}
/** Set that this (string) argument accepts the \@filename syntax to
* mean that the content of the specified file should be used as the
* value of the argument.
*/
GDALAlgorithmArgDecl &SetReadFromFileAtSyntaxAllowed()
{
m_readFromFileAtSyntaxAllowed = true;
return *this;
}
/** Sets that SQL comments must be removed from a (string) argument.
*/
GDALAlgorithmArgDecl &SetRemoveSQLCommentsEnabled()
{
m_removeSQLComments = true;
return *this;
}
/** Return the (long) name */
inline const std::string &GetName() const
{
return m_longName;
}
/** Return the short name, or empty string if there is none */
inline const std::string &GetShortName() const
{
return m_shortName;
}
/** Return the aliases (potentially none) */
inline const std::vector<std::string> &GetAliases() const
{
return m_aliases;
}
/** Return the description */
inline const std::string &GetDescription() const
{
return m_description;
}
/** Return the "meta-var" hint.
* By default, the meta-var value is the long name of the argument in
* upper case.
*/
inline const std::string &GetMetaVar() const
{
return m_metaVar;
}
/** Return the argument category: GAAC_COMMON, GAAC_BASE, GAAC_ADVANCED,
* GAAC_ESOTERIC or a custom category.
*/
inline const std::string &GetCategory() const
{
return m_category;
}
/** Return the type */
inline GDALAlgorithmArgType GetType() const
{
return m_type;
}
/** Return the allowed values (as strings) for the argument.
* Only honored for GAAT_STRING and GAAT_STRING_LIST types.
*/
inline const std::vector<std::string> &GetChoices() const
{
return m_choices;
}
/** Return whether the argument is required. Defaults to false.
*/
inline bool IsRequired() const
{
return m_required;
}
/** Return the minimum number of values for the argument. Defaults to 0.
* Only applies to list type of arguments.
*/
inline int GetMinCount() const
{
return m_minCount;
}
/** Return the maximum number of values for the argument.
* Defaults to 1 for scalar types, and UNBOUNDED for list types.
* Only applies to list type of arguments.
*/
inline int GetMaxCount() const
{
return m_maxCount;
}
/** Returns whether in --help message one should display hints about the
* minimum/maximum number of values. Defaults to true.
*/
inline bool GetDisplayHintAboutRepetition() const
{
return m_displayHintAboutRepetition;
}
/** Return whether, for list type of arguments, several values, space
* separated, may be specified. That is "--foo=bar,baz".
* The default is true.
*/
inline bool GetPackedValuesAllowed() const
{
return m_packedValuesAllowed;
}
/** Return whether, for list type of arguments, the argument may be
* repeated. That is "--foo=bar --foo=baz".
* The default is true.
*/
inline bool GetRepeatedArgAllowed() const
{
return m_repeatedArgAllowed;
}
/** Return if the argument is a positional one. */
inline bool IsPositional() const
{
return m_positional;
}
/** Return if the argument has a declared default value. */
inline bool HasDefaultValue() const
{
return m_hasDefaultValue;
}
/** Return whether the argument must not be mentioned in CLI usage.
* For example, "output-value" for "gdal raster info", which is only
* meant when the algorithm is used from a non-CLI context.
*/
inline bool IsHiddenForCLI() const
{
return m_hiddenForCLI;
}
/** Return whether the argument is only for CLI usage.
* For example "--help" */
inline bool IsOnlyForCLI() const
{
return m_onlyForCLI;
}
/** Indicate whether the value of the argument is read-only during the
* execution of the algorithm. Default is true.
*/
inline bool IsInput() const
{
return m_isInput;
}
/** Return whether (at least part of) the value of the argument is set
* during the execution of the algorithm.
* For example, "output-value" for "gdal raster info"
* Default is false.
* An argument may return both IsInput() and IsOutput() as true.
* For example the "gdal raster convert" algorithm consumes the dataset
* name of its "output" argument, and sets the dataset object during its
* execution.
*/
inline bool IsOutput() const
{
return m_isOutput;
}
/** Return the name of the mutual exclusion group to which this argument
* belongs to, or empty string if it does not belong to any exclusion
* group.
*/
inline const std::string &GetMutualExclusionGroup() const
{
return m_mutualExclusionGroup;
}
/** Return if this (string) argument accepts the \@filename syntax to
* mean that the content of the specified file should be used as the
* value of the argument.
*/
inline bool IsReadFromFileAtSyntaxAllowed() const
{
return m_readFromFileAtSyntaxAllowed;
}
/** Returns whether SQL comments must be removed from a (string) argument.
*/
bool IsRemoveSQLCommentsEnabled() const
{
return m_removeSQLComments;
}
/** Get user-defined metadata. */
inline const std::map<std::string, std::vector<std::string>>
GetMetadata() const
{
return m_metadata;
}
/** Get user-defined metadata by item name. */
inline const std::vector<std::string> *
GetMetadataItem(const std::string &name) const
{
const auto iter = m_metadata.find(name);
return iter == m_metadata.end() ? nullptr : &(iter->second);
}
/** Return the default value of the argument.
* Must be called with T consistent of the type of the algorithm, and only
* if HasDefaultValue() is true.
* Valid T types are:
* - bool for GAAT_BOOLEAN
* - int for GAAT_INTEGER
* - double for GAAT_REAL
* - std::string for GAAT_STRING
* - GDALArgDatasetValue for GAAT_DATASET, GAAT_RASTER_DATASET,
* GAAT_VECTOR_DATASET, GAAT_MULTIDIM_DATASET
* - std::vector<int> for GAAT_INTEGER_LIST
* - std::vector<double for GAAT_REAL_LIST
* - std::vector<std::string> for GAAT_STRING_LIST
* - std::vector<GDALArgDatasetValue> for GAAT_DATASET_LIST,
* GAAT_RASTER_DATASET_LIST, GAAT_VECTOR_DATASET_LIST,
* GAAT_MULTIDIM_DATASET_LIST
*/
template <class T> inline const T &GetDefault() const
{
return std::get<T>(m_defaultValue);
}
private:
const std::string m_longName;
const std::string m_shortName;
const std::string m_description;
const GDALAlgorithmArgType m_type;
std::string m_category = GAAC_BASE;
std::string m_metaVar{};
std::string m_mutualExclusionGroup{};
int m_minCount = 0;
int m_maxCount = 0;
bool m_required = false;
bool m_positional = false;
bool m_hasDefaultValue = false;
bool m_hiddenForCLI = false;
bool m_onlyForCLI = false;
bool m_isInput = true;
bool m_isOutput = false;
bool m_packedValuesAllowed = true;
bool m_repeatedArgAllowed = true;
bool m_displayHintAboutRepetition = true;
bool m_readFromFileAtSyntaxAllowed = false;
bool m_removeSQLComments = false;
std::map<std::string, std::vector<std::string>> m_metadata{};
std::vector<std::string> m_aliases{};
std::vector<std::string> m_hiddenAliases{};
std::vector<std::string> m_choices{};
std::variant<bool, std::string, int, double, std::vector<std::string>,
std::vector<int>, std::vector<double>>
m_defaultValue{};
};
/************************************************************************/
/* GDALAlgorithmArg */
/************************************************************************/
class GDALAlgorithm;
/** Argument of an algorithm.
*/
class CPL_DLL GDALAlgorithmArg /* non-final */