DbcLib 1.0
CAN DBC C++ library.
Loading...
Searching...
No Matches
attribute.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
8#pragma once
9
10#include <string>
11#include <vector>
12#include <sstream>
13namespace dbc {
17enum class AttributeType : int {
19 DbcNode,
21 DbcSignal,
25};
26
30enum class AttributeValueType : int {
34 EnumValue,
36};
37
49class Attribute {
50 public:
52 Attribute( AttributeType type, const std::string& name );
53
55 void Name(const std::string& name) { name_ = name; }
57 [[nodiscard]] const std::string& Name() const { return name_; }
58
60 void Type(AttributeType type ) { type_ = type; }
62 [[nodiscard]] AttributeType Type() const { return type_; }
63
65 void ValueType(AttributeValueType type ) { value_type_ = type; }
67 [[nodiscard]] AttributeValueType ValueType() const { return value_type_; }
69 [[nodiscard]] std::string ValueTypeAsString() const;
70
71 void Min(double min ) { min_ = min; }
72 [[nodiscard]] double Min() const { return min_; }
73
74 void Max(double max ) { max_ = max; }
75 [[nodiscard]] double Max() const { return max_; }
76
78 void EnumList(const std::vector<std::string>& list);
80 [[nodiscard]] const std::vector<std::string>& EnumList() const;
81
83 template <typename T>
84 void Value(const T& value);
85
87 template <typename T>
88 [[nodiscard]] T Value() const;
89 private:
91 std::string name_;
92 AttributeValueType value_type_ =
94 double value_float_ = 0.0;
95 std::string value_string_;
96 std::vector<std::string> enum_list_;
97 double min_ = 0.0;
98 double max_ = 0.0;
99};
100
101
102template <typename T>
103void Attribute::Value(const T& value) {
104 try {
105 value_float_ = static_cast<double>(value);
106 switch (value_type_) {
108 value_string_ = std::to_string(static_cast<int64_t>(value));
109 break;
110
112 value_string_ = std::to_string(static_cast<double>(value));
113 break;
114
116 const auto index = static_cast<size_t>(value_float_);
117 if ( index < enum_list_.size()) {
118 value_string_ = enum_list_[index];
119 }
120 break;
121 }
122
123 default:
124 value_string_ = std::to_string(value);
125 break;
126 }
127
128 } catch (const std::exception&) {
129 value_float_ = 0.0;
130 value_string_ = "Conversion error";
131 }
132}
133
135template<>
136void Attribute::Value(const std::string& value);
137
138
139template <typename T>
141 T value = {};
142 switch (ValueType()) {
144 std::istringstream temp(value_string_);
145 try {
146 temp >> value;
147 } catch (const std::exception& ) {
148 }
149 break;
150 }
151
152 default:
153 value = static_cast<T>(value_float_);
154 break;
155 }
156 return value;
157}
158
160template <>
161[[nodiscard]] std::string Attribute::Value() const;
162
163
164} // namespace dbc
Support class for handling attributes of network objects.
Definition: attribute.h:49
T Value() const
Returns the attribute value.
Definition: attribute.h:140
std::string ValueTypeAsString() const
Returns the attribute data type as string.
void ValueType(AttributeValueType type)
Sets the attribute data type.
Definition: attribute.h:65
void Min(double min)
Sets the min range.
Definition: attribute.h:71
double Max() const
Max range.
Definition: attribute.h:75
void Type(AttributeType type)
Sets the type of attribute.
Definition: attribute.h:60
const std::vector< std::string > & EnumList() const
Returns an enumerated string list.
void EnumList(const std::vector< std::string > &list)
Sets an enumerate string list.
AttributeType Type() const
Return type of attribute.
Definition: attribute.h:62
void Name(const std::string &name)
Sets the name of the attribute.
Definition: attribute.h:55
double Min() const
Min range.
Definition: attribute.h:72
Attribute(AttributeType type, const std::string &name)
Constructor for an attribute or definition.
AttributeValueType ValueType() const
Returns the attribute data type.
Definition: attribute.h:67
const std::string & Name() const
Retuns the attribute name.
Definition: attribute.h:57
void Max(double max)
Sets the max range.
Definition: attribute.h:74
Wrapper class around a CAN message.
Definition: dbcmessage.h:21
Main namespace for the DBC library.
Definition: attribute.h:13
AttributeType
Define what type of network object the attribute is valid for.
Definition: attribute.h:17
@ DbcNode
Node/Device attribute.
@ NodeSignalRelation
Node signal relation attribute.
@ NodeMessageRelation
Node message relation attribute.
@ DbcNetwork
Network attribute.
@ DbcSignal
Signal attribute.
@ EnvironmentVariable
Environment attribute.
AttributeValueType
Data type of the attribute.
Definition: attribute.h:30
@ HexValue
Hexa-decimal value.
@ EnumValue
Enumerate value.
@ IntegerValue
Integer value.
@ FloatValue
Floating point value.
@ StringValue
Text value.