Version: SMASH-3.1
smash::Key< default_type > Class Template Reference

#include <input_keys.h>

template<typename default_type>
class smash::Key< default_type >

Object to store a YAML input file key together with metadata associated to it.

Note
The class is designed such that all keys can be marked as deprecated and as removed. However, it is not possible to mark a key as removed without having deprecated it before. A workaround is to deprecate and remove it in the same version, i.e. specifying the same version twice at construction.
Template Parameters
default_typeType of the key value.

Definition at line 54 of file input_keys.h.

Classes

struct  WrongNumberOfVersions
 Thrown when too few or too many versions are passed to the constructor. More...
 

Public Types

using type = default_type
 Let the clients of this class have access to the key type. More...
 

Public Member Functions

 Key (const std::initializer_list< std::string_view > &labels, const std::initializer_list< std::string_view > &versions)
 Construct a new Key object without default value. More...
 
 Key (const std::initializer_list< std::string_view > &labels, const std::optional< default_type > &value, const std::initializer_list< std::string_view > &versions)
 Construct a new Key object with default value. More...
 
default_type default_value () const
 Get the default value of the key. More...
 
Version introduced_in () const noexcept
 Get the SMASH version in which the key has been introduced. More...
 
Version deprecated_in () const
 Get the SMASH version in which the key has been deprecated. More...
 
Version removed_in () const
 Get the SMASH version in which the key has been removed. More...
 
bool is_deprecated () const noexcept
 Get whether the key is deprecated or not. More...
 
bool is_allowed () const noexcept
 Get whether the key is still allowed or not. More...
 
bool has_same_labels (const KeyLabels &labels) const noexcept
 Check if given labels are the same as those of this object. More...
 
 operator std::string () const noexcept
 Converts a Key to a std::string using all labels. More...
 
const KeyLabelslabels () const
 Method to access the Key labels. More...
 

Private Attributes

Version introduced_in_ {}
 SMASH version in which the key has been introduced. More...
 
std::optional< Versiondeprecated_in_ {}
 SMASH version in which the key has been deprecated, if any. More...
 
std::optional< Versionremoved_in_ {}
 SMASH version in which the key has been removed, if any. More...
 
std::optional< default_type > default_ {}
 Key default value, if any. More...
 
KeyLabels labels_ {}
 The label(s) identifying the key in the YAML input file. More...
 

Member Typedef Documentation

◆ type

template<typename default_type >
using smash::Key< default_type >::type = default_type

Let the clients of this class have access to the key type.

Definition at line 123 of file input_keys.h.

Constructor & Destructor Documentation

◆ Key() [1/2]

template<typename default_type >
smash::Key< default_type >::Key ( const std::initializer_list< std::string_view > &  labels,
const std::initializer_list< std::string_view > &  versions 
)
inlineexplicit

Construct a new Key object without default value.

Parameters
[in]labelsThe label(s) identifying the key in the YAML input file.
[in]versionsA list of one, two or three version numbers identifying the versions in which the key has been introduced, deprecated and removed, respectively.

Definition at line 72 of file input_keys.h.

74  : Key{labels, std::nullopt, versions} {}
Key(const std::initializer_list< std::string_view > &labels, const std::initializer_list< std::string_view > &versions)
Construct a new Key object without default value.
Definition: input_keys.h:72
const KeyLabels & labels() const
Method to access the Key labels.
Definition: input_keys.h:201

◆ Key() [2/2]

template<typename default_type >
smash::Key< default_type >::Key ( const std::initializer_list< std::string_view > &  labels,
const std::optional< default_type > &  value,
const std::initializer_list< std::string_view > &  versions 
)
inline

Construct a new Key object with default value.

Note that the default value could be simply taken as parameter of type default_type . However, this would complicate delegating construction in the other constructor and the caller can still pass a variable of type default_type and the std::optional will be constructed without problems.

Parameters
[in]labelsThe label(s) identifying the key in the YAML input file.
[in]valueThe key default value.
[in]versionsA list of one, two or three version numbers identifying the versions in which the key has been introduced, deprecated and removed, respectively.
Exceptions
WrongNumberOfVersionsIf versions has the wrong size.

Definition at line 93 of file input_keys.h.

96  : default_{value}, labels_{labels.begin(), labels.end()} {
97  /*
98  * The following switch statement is a compact way to initialize the
99  * three version member variables without repetition and lots of logic
100  * clauses. The versions variable can have 1, 2 or 3 entries. The use of
101  * the iterator is needed, since std::initializer_list has no access
102  * operator.
103  */
104  switch (auto it = versions.end(); versions.size()) {
105  case 3:
106  removed_in_ = *(--it);
107  [[fallthrough]];
108  case 2:
109  deprecated_in_ = *(--it);
110  [[fallthrough]];
111  case 1:
112  introduced_in_ = *(--it);
113  break;
114  default:
115  throw WrongNumberOfVersions(
116  "Key constructor needs one, two or three version numbers.");
117  }
118  }
std::optional< Version > removed_in_
SMASH version in which the key has been removed, if any.
Definition: input_keys.h:209
std::optional< Version > deprecated_in_
SMASH version in which the key has been deprecated, if any.
Definition: input_keys.h:207
std::optional< default_type > default_
Key default value, if any.
Definition: input_keys.h:211
Version introduced_in_
SMASH version in which the key has been introduced.
Definition: input_keys.h:205
KeyLabels labels_
The label(s) identifying the key in the YAML input file.
Definition: input_keys.h:213

Member Function Documentation

◆ default_value()

template<typename default_type >
default_type smash::Key< default_type >::default_value ( ) const
inline

Get the default value of the key.

Returns
A default_type variable.
Exceptions
std::bad_optional_accessIf the key has no default value.

Definition at line 132 of file input_keys.h.

132 { return default_.value(); }

◆ introduced_in()

template<typename default_type >
Version smash::Key< default_type >::introduced_in ( ) const
inlinenoexcept

Get the SMASH version in which the key has been introduced.

Returns
A Version variable.

Definition at line 139 of file input_keys.h.

139 { return introduced_in_; }

◆ deprecated_in()

template<typename default_type >
Version smash::Key< default_type >::deprecated_in ( ) const
inline

Get the SMASH version in which the key has been deprecated.

Returns
A Version variable.
Exceptions
std::bad_optional_accessIf the key is not deprecated.

Definition at line 148 of file input_keys.h.

148 { return deprecated_in_.value(); }

◆ removed_in()

template<typename default_type >
Version smash::Key< default_type >::removed_in ( ) const
inline

Get the SMASH version in which the key has been removed.

Returns
A Version variable.
Exceptions
std::bad_optional_accessIf the key is still allowed.

Definition at line 157 of file input_keys.h.

157 { return removed_in_.value(); }

◆ is_deprecated()

template<typename default_type >
bool smash::Key< default_type >::is_deprecated ( ) const
inlinenoexcept

Get whether the key is deprecated or not.

Returns
true if the key is deprecated, false otherwise.

Definition at line 164 of file input_keys.h.

164 { return deprecated_in_.has_value(); }

◆ is_allowed()

template<typename default_type >
bool smash::Key< default_type >::is_allowed ( ) const
inlinenoexcept

Get whether the key is still allowed or not.

Returns
true if the key is allowed, false otherwise.

Definition at line 171 of file input_keys.h.

171 { return !removed_in_.has_value(); }

◆ has_same_labels()

template<typename default_type >
bool smash::Key< default_type >::has_same_labels ( const KeyLabels labels) const
inlinenoexcept

Check if given labels are the same as those of this object.

Parameters
[in]labelsGiven labels to be checked against.
Returns
true if all labels match in the given order,
false otherwise.

Definition at line 181 of file input_keys.h.

181  {
182  return std::equal(std::begin(labels_), std::end(labels_),
183  std::begin(labels), std::end(labels));
184  }

◆ operator std::string()

template<typename default_type >
smash::Key< default_type >::operator std::string ( ) const
inlineexplicitnoexcept

Converts a Key to a std::string using all labels.

Returns
std::string with labels concatenated with :␣ (colon-space) and quotes all around.

Definition at line 192 of file input_keys.h.

192  {
193  return smash::quote(smash::join(labels_, ": "));
194  }
std::string quote(const std::string &s)
Add quotes around string.
std::string join(const std::vector< std::string > &v, const std::string &delim)
Join strings using delimiter.

◆ labels()

template<typename default_type >
const KeyLabels& smash::Key< default_type >::labels ( ) const
inline

Method to access the Key labels.

Returns
A constant reference to the labels member for read-only access.

Definition at line 201 of file input_keys.h.

201 { return labels_; }

Member Data Documentation

◆ introduced_in_

template<typename default_type >
Version smash::Key< default_type >::introduced_in_ {}
private

SMASH version in which the key has been introduced.

Definition at line 205 of file input_keys.h.

◆ deprecated_in_

template<typename default_type >
std::optional<Version> smash::Key< default_type >::deprecated_in_ {}
private

SMASH version in which the key has been deprecated, if any.

Definition at line 207 of file input_keys.h.

◆ removed_in_

template<typename default_type >
std::optional<Version> smash::Key< default_type >::removed_in_ {}
private

SMASH version in which the key has been removed, if any.

Definition at line 209 of file input_keys.h.

◆ default_

template<typename default_type >
std::optional<default_type> smash::Key< default_type >::default_ {}
private

Key default value, if any.

Definition at line 211 of file input_keys.h.

◆ labels_

template<typename default_type >
KeyLabels smash::Key< default_type >::labels_ {}
private

The label(s) identifying the key in the YAML input file.

Definition at line 213 of file input_keys.h.


The documentation for this class was generated from the following file: