Lluvia
Parameter.h
Go to the documentation of this file.
1 
8 #ifndef LLUVIA_CORE_NODE_PARAMETER_H_
9 #define LLUVIA_CORE_NODE_PARAMETER_H_
10 
11 #include "lluvia/core/error.h"
13 
14 #include <array>
15 #include <cstdint>
16 #include <string>
17 #include <tuple>
18 #include <type_traits>
19 #include <utility>
20 
21 namespace ll {
22 
23 class Parameter {
24 
25 public:
26  Parameter() = default;
27  Parameter(const Parameter&) = default;
28  Parameter(Parameter&&) = default;
29 
31 
32  ~Parameter() = default;
33 
34  Parameter& operator=(const Parameter&) = default;
35  Parameter& operator=(Parameter&&) = default;
36 
37  ll::ParameterType getType() const noexcept;
38 
39  template <typename T>
40  void set(const T& value)
41  {
42 
43  if constexpr (std::is_integral_v<T>) {
44  m_type = ll::ParameterType::Int;
45  m_value.v_int = static_cast<int32_t>(value);
46  } else if constexpr (std::is_floating_point_v<T>) {
47  m_type = ll::ParameterType::Float;
48  m_value.v_float = static_cast<float>(value);
49  } else if constexpr (std::is_same_v<T, std::string>) {
51  m_value.v_string = std::string {value};
52  } else {
53  // at least we are sure the first if is false
54  static_assert(!std::is_integral_v<T>, "type T does not match any of the supported types");
55  }
56  }
57 
58  template <typename T>
59  const T get() const
60  {
61 
62  if constexpr (std::is_integral_v<T> || std::is_floating_point_v<T> || std::is_same_v<T, bool>) {
63 
64  switch (m_type) {
65  case ParameterType::Int:
66  return static_cast<int32_t>(m_value.v_int);
68  return static_cast<float>(m_value.v_float);
71  "String parameter type not supported when casting to integral or floating point types");
72  }
73  } else if constexpr (std::is_same_v<T, std::string>) {
74 
75  if (m_type == ParameterType::String) {
76  return m_value.v_string;
77  } else {
79  "String parameter type not supported when casting to integral or floating point types");
80  }
81 
82  } else {
83  // at least we are sure the first if is false
84  static_assert(!std::is_integral_v<T>, "type T does not match any of the supported types");
85  }
86  }
87 
88 private:
90 
91  struct {
92  union {
93  int32_t v_int;
94  float v_float;
95  };
96 
97  // complex types cannot go into unions
98  std::string v_string;
99  } m_value;
100 };
101 
102 } // namespace ll
103 
104 #endif // LLUVIA_CORE_NODE_PARAMETER_H_
ParameterType enum.
Definition: Parameter.h:23
~Parameter()=default
Parameter & operator=(Parameter &&)=default
Parameter & operator=(const Parameter &)=default
int32_t v_int
Definition: Parameter.h:93
const T get() const
Definition: Parameter.h:59
ll::ParameterType getType() const noexcept
Parameter()=default
float v_float
Definition: Parameter.h:94
Parameter(ParameterType type)
Parameter(Parameter &&)=default
void set(const T &value)
Definition: Parameter.h:40
Parameter(const Parameter &)=default
std::string v_string
Definition: Parameter.h:98
error related classes and methods.
Definition: Buffer.h:28
ParameterType
Definition: ParameterType.h:15
std::error_code createErrorCode(ll::ErrorCode errorCode)
Creates an error code.
Definition: error.h:138