camcom 1.0.0-pre2
 
Loading...
Searching...
No Matches
parameter.hpp
Go to the documentation of this file.
1
8
9#ifndef CAMCOM_COMMON_PARAMETER_HPP
10#define CAMCOM_COMMON_PARAMETER_HPP
11
12#include <string>
13#include <cmath>
14#include <optional>
15
20#include "ifw/fnd/defs/base.hpp"
21
22namespace camcom::common {
23
24 class Parameter {
25 public:
26 Parameter(const std::string& name,
27 const Value& value,
28 const DataType& type);
29 Parameter();
30
31 template<typename T> Parameter(const std::string& name, const T& value) {
32 m_name = name;
33 m_value = value;
34 if constexpr (std::is_same_v<T, int16_t>) {
35 m_type = DataType::INT16;
36 }
37 else if constexpr (std::is_same_v<T, int32_t>) {
38 m_type = DataType::INT32;
39 }
40 else if constexpr (std::is_same_v<T, int64_t>) {
41 m_type = DataType::INT64;
42 }
43 else if constexpr (std::is_same_v<T, uint16_t>) {
44 m_type = DataType::UINT16;
45 }
46 else if constexpr (std::is_same_v<T, uint32_t>) {
47 m_type = DataType::UINT32;
48 }
49 else if constexpr (std::is_same_v<T, uint64_t>) {
50 m_type = DataType::UINT64;
51 }
52 else if constexpr (std::is_same_v<T, float>) {
53 m_type = DataType::FLOAT;
54 }
55 else if constexpr (std::is_same_v<T, double_t>) {
56 m_type = DataType::DOUBLE;
57 }
58 else if constexpr (std::is_same_v<T, bool>) {
59 m_type = DataType::BOOL;
60 }
61 else if constexpr (std::is_same_v<T, char>) {
62 m_type = DataType::CHAR;
63 }
64 else if constexpr (std::is_same_v<T, std::string>) {
65 m_type = DataType::STRING;
66 }
67 else if constexpr (std::is_same_v<T, std::byte>) {
68 m_type = DataType::BYTE;
69 }
70 }
71
72 const std::string GetName() const;
73 void SetName(const std::string& name) { m_name = name; }
74
75 const std::string GetValueAsString() const;
76
77 template <typename T> const T GetValue() const {
78 FNDTRACE();
79 try {
80 return std::get<T>(m_value);
81 } catch (std::bad_variant_access&) {
82 FNDTHROW("Illegal type: {} encountered - parameter: {}", typeid(T).name(), m_name);
83 return T();
84 }
85 }
86
87 const DataType& GetType() const;
88 const std::string GetTypeAsString() const;
89
90 void Store(const std::string& name, const Value& value, const DataType& type);
91 template <typename T> void Store(const std::string& name, const T& value);
92
93 const std::string ToString() const;
94
95 // Metadata accessors
96 bool HasMetadata() const { return m_metadata.has_value(); }
97 const ParameterMetadata& GetMetadata() const;
99 void SetMetadata(const ParameterMetadata& metadata) { m_metadata = metadata; }
100 void SetMetadata(ParameterMetadata&& metadata) { m_metadata = std::move(metadata); }
101
102 private:
103 std::string m_name;
104 Value m_value;
105 DataType m_type{DataType::UNSPECIFIED};
106 std::optional<ParameterMetadata> m_metadata;
107 };
108
109 // Template specializations (declared here, defined in .cpp)
110 template <> inline void Parameter::Store<int>(const std::string& name, const int& value) {
111 m_name = name;
112 m_value = value;
113 m_type = DataType::INT32;
114 }
115
116 template <> inline void Parameter::Store<long>(const std::string& name, const long& value) {
117 m_name = name;
118 m_value = static_cast<int>(value);
119 m_type = DataType::INT32;
120 }
121
122 template <> inline void Parameter::Store<float>(const std::string& name, const float& value) {
123 m_name = name;
124 m_value = value;
125 m_type = DataType::FLOAT;
126 }
127
128 template <> inline void Parameter::Store<double_t>(const std::string& name, const double_t& value) {
129 m_name = name;
130 m_value = value;
131 m_type = DataType::DOUBLE;
132 }
133
134 template <> inline void Parameter::Store<std::string>(const std::string& name, const std::string& value) {
135 m_name = name;
136 m_value = value;
137 m_type = DataType::STRING;
138 }
139
140 template <> inline void Parameter::Store<const char*>(const std::string& name, const char* const& value) {
141 m_name = name;
142 m_value = value;
143 m_type = DataType::STRING;
144 }
145
146 template <> inline void Parameter::Store<bool>(const std::string& name, const bool& value) {
147 m_name = name;
148 m_value = value;
149 m_type = DataType::BOOL;
150 }
151
152}
153
154#endif
const std::string GetName() const
Definition parameter.cpp:30
const T GetValue() const
Definition parameter.hpp:77
const std::string ToString() const
Definition parameter.cpp:73
void Store(const std::string &name, const T &value)
Parameter()
Definition parameter.cpp:26
bool HasMetadata() const
Definition parameter.hpp:96
const std::string GetTypeAsString() const
Definition parameter.cpp:61
void SetMetadata(const ParameterMetadata &metadata)
Definition parameter.hpp:99
Parameter(const std::string &name, const T &value)
Definition parameter.hpp:31
void SetName(const std::string &name)
Definition parameter.hpp:73
Parameter(const std::string &name, const Value &value, const DataType &type)
Definition parameter.cpp:17
const DataType & GetType() const
Definition parameter.cpp:56
const std::string GetValueAsString() const
Definition parameter.cpp:35
const ParameterMetadata & GetMetadata() const
Definition parameter.cpp:82
void SetMetadata(ParameterMetadata &&metadata)
Definition parameter.hpp:100
ParameterMetadata & GetMutableMetadata()
Definition parameter.cpp:91
void Store(const std::string &name, const Value &value, const DataType &type)
Definition parameter.cpp:66
CamCom data type — re-export of the ELT IFW (ifw-fnd) data type.
Definition adapterBase.cpp:18
std::variant< int, float, double, std::string, bool > Value
Definition value.hpp:17
ifw::fnd::datatype::DataType DataType
Definition datatype.hpp:27
Header file for the CamCom Common Library.
Parameter metadata containing constraints and descriptive information.
Definition parameterMetadata.hpp:32
Header file for the CamCom Common Library Tools.
Header file for the CamCom Common Library.