IOption.hpp
1 #pragma once
2 
3 ///=============================================================================
4 /// \file
5 /// \brief Abstract option classes.
6 /// \author WK
7 /// \date 29.02.2020
8 ///=============================================================================
9 
10 #include <opals/fwd.hpp>
11 #include <opals/UsageLevel.hpp>
12 #include <opals/ValueSource.hpp>
13 #include <opals/OptionNames.hpp>
14 #include <type_traits>
15 
16 namespace opals
17 {
18  /// Contains the public interface of OPALS option types.
19  namespace opts
20  {
21 
22  /// Enumerates all option types
23  enum struct Specs
24  {
25  leaf = 1,
26  group = leaf << 1,
27  vector = group << 1,
28  array = vector << 1,
29  dim1 = vector | array
30  };
31 
32  template< Names nam >
33  using NameT = std::integral_constant< Names, nam >;
34 
35  /// Base class of all option types.
36  struct IBase
37  {
38  /// The option name as enumerator.
39  virtual Names enumerator() const = 0;
40  /// The option name as string.
41  virtual String name() const = 0;
42  /// The option name as string, preceded by the names of its parents, if any, interspersed with '.'
43  virtual String path() const = 0;
44  /// One-liner that describes this option.
45  virtual String shortDescription() const = 0;
46  /// Longer text that describes this option in more detail.
47  virtual String longDescription() const = 0;
48  /// Returns true if this or any of its parents is read-only.
49  virtual bool readOnly() const = 0;
50  /// For a leaf, this is its own usage level. For a node, this is the minimum usage level of all of its descendants: basic < mid < pro
51  virtual UsageLevel minUsageLevel() const = 0;
52  /// The sub-type of this option.
53  virtual Specs specialization() const = 0;
54  protected:
55  virtual ~IBase() {}
56  };
57 
58  /// Leaf that holds a read-only value
59  /** \tparam nam The leaf name.
60  \tparam rdOnly If true, then this leaf's value may not be changed.
61  \tparam T The value type. */
62  template< Names nam, bool rdOnly, class T >
63  struct ILeaf
64  : IBase
65  {
66  typedef NameT<nam> Name;
67 
68  /// Frequency / expected skills of users that access this leaf.
69  virtual UsageLevel usageLevel() const = 0;
70  /// The context in which the value has been set.
71  virtual ValueSource valueSource() const = 0;
72  /// If its value has not been set, a leaf is considered empty.
73  /** Accessing its value then raises an exception. */
74  virtual bool empty() const = 0;
75 
76  /// Get the value.
77  virtual const T& get() const = 0;
78  /// Get the value or default_ if empty.
79  virtual const T& getOr(const T& default_) const = 0;
80  };
81 
82  /// Leaf that holds a read-write value
83  /** \tparam nam The leaf name.
84  \tparam T The value type. */
85  template< Names nam, class T >
86  struct ILeaf< nam, false, T >
87  : ILeaf< nam, true, T >
88  {
89  /// Set the value.
90  virtual void set(const T& value) = 0;
91  };
92 
93  }
94 }
95 
96 #include <opals/IAccess.hpp>
97 
98 namespace opals
99 {
100  namespace opts
101  {
102 
103  /// A group of options
104  /** \tparam nam The group name.
105  \tparam rdOnly If true, then the group provides only constant access to its members. Otherwise, also non-constant.
106  \tparam Opts The group members. */
107  template< Names nam, bool rdOnly, class... Opts >
108  struct IGroup
109  : IAcc< rdOnly, Opts... >
110  {
111  typedef NameT<nam> Name;
112  };
113 
114  /// A container of read-only options.
115  /** \tparam nam The name of the container and its elements.
116  \tparam rdOnly If true, then this container provides only constant access to its elements. Otherwise, also non-constant.
117  \tparam Opts The sequence of group members.
118  Provides access to specific elements and to the default element. */
119  template< Names nam, bool rdOnly, class... Opts >
120  struct IContainer
121  : IBase
122  {
123  typedef NameT<nam> Name;
124  typedef IGroup< nam, rdOnly, Opts... > Element;
125 
126  /// The number of actual elements.
127  virtual std::size_t size() const = 0;
128 
129  /// Access the default element.
130  /** Changes to it are applied immediately to all actual elements. */
131  virtual const Element &all() const = 0;
132 
133  /// Access an actual element.
134  virtual const Element &operator[]( std::size_t idx ) const = 0;
135  };
136 
137  /// A container of read-write options.
138  template< Names nam, class... Opts >
139  struct IContainer< nam, false, Opts... >
140  : IBase
141  {
142  typedef NameT<nam> Name;
143  typedef IGroup< nam, false, Opts... > Element;
144 
145  /// The number of actual elements.
146  virtual std::size_t size() const = 0;
147 
148  /** \name Default element.
149  Changes to it are applied immediately to all actual elements. */
150  ///@{
151  virtual const Element &all() const = 0;
152  virtual Element &all() = 0;
153  ///@}
154 
155  /// \name Access an actual element.
156  ///@{
157  virtual const Element &operator[]( std::size_t idx ) const = 0;
158  virtual Element &operator[]( std::size_t idx ) = 0;
159  ///@}
160  };
161 
162  /// A vector of a variable number of read-only options.
163  /** \tparam nam The name of the vector and its elements.
164  \tparam rdOnly If true, then this vector provides only constant access to its elements. Otherwise, also non-constant.
165  \tparam Opts The sequence of group members. */
166  template< Names nam, bool rdOnly, class... Opts >
167  struct IVector
168  : IContainer< nam, rdOnly, Opts... >
169  {
170  };
171 
172  /// A vector of a variable number of read-write options.
173  template< Names nam, class... Opts >
174  struct IVector< nam, false, Opts... >
175  : IContainer< nam, false, Opts... >
176  {
177  /// Set the number of elements. New elements are initialized with the default element.
178  virtual void resize( std::size_t size ) = 0;
179  };
180 
181  /// An array of N options.
182  /** \tparam nam The name of the array and its elements.
183  \tparam rdOnly If true, then this array provides only constant access to its elements. Otherwise, also non-constant.
184  \tparam N The number of elements.
185  \tparam Opts The sequence of group members. */
186  template< Names nam, bool rdOnly, std::size_t N, class... Opts >
187  struct IArray
188  : IContainer< nam, rdOnly, Opts... >
189  {
190  };
191 
192  }
193 }
virtual Specs specialization() const =0
The sub-type of this option.
virtual UsageLevel minUsageLevel() const =0
For a leaf, this is its own usage level. For a node, this is the minimum usage level of all of its de...
virtual bool readOnly() const =0
Returns true if this or any of its parents is read-only.
A container of read-only options.
Definition: IOption.hpp:120
virtual UsageLevel usageLevel() const =0
Frequency / expected skills of users that access this leaf.
UsageLevel
Frequency of use / needed level of user experience of an option.
Definition: UsageLevel.hpp:8
Leaf that holds a read-only value.
Definition: IOption.hpp:63
virtual String shortDescription() const =0
One-liner that describes this option.
Contains the public interface of OPALS.
Definition: ApplyTrafo.hpp:5
Specs
Enumerates all option types.
Definition: IOption.hpp:23
ValueSource
Origin of an option value.
Definition: ValueSource.hpp:8
virtual String longDescription() const =0
Longer text that describes this option in more detail.
Names
Enumerates all option names.
Definition: OptionNames.hpp:19
virtual String name() const =0
The option name as string.
virtual ValueSource valueSource() const =0
The context in which the value has been set.
An array of N options.
Definition: IOption.hpp:187
virtual Names enumerator() const =0
The option name as enumerator.
virtual bool empty() const =0
If its value has not been set, a leaf is considered empty.
virtual const T & getOr(const T &default_) const =0
Get the value or default_ if empty.
A group of options.
Definition: IOption.hpp:108
@ vector
General vector data file (las, shp, ..)
virtual String path() const =0
The option name as string, preceded by the names of its parents, if any, interspersed with '....
A dynamic character string whose interface conforms to STL's std::string.
Definition: String.hpp:35
virtual const T & get() const =0
Get the value.
typename IAcc_< rdOnly, Opts... >::Type IAcc
The specialization of GetIAcc for the first member of a group.
Definition: IAccess.hpp:6809
A vector of a variable number of read-only options.
Definition: IOption.hpp:167
virtual const Element & all() const =0
Access the default element.
virtual std::size_t size() const =0
The number of actual elements.
Base class of all option types.
Definition: IOption.hpp:36
@ value
strip.trajectory.dX/dY/dZ/dRoll/dPitch/dYaw groups (opalsStripAdjust)
virtual const Element & operator[](std::size_t idx) const =0
Access an actual element.