33
44#include < string>
55#include < memory>
6- #include < exception >
6+ #include < stdexcept >
77#include < map>
8+ #include < vector>
89
910#include " cpp_can_parser_export.h"
10- #include " CANFrame.h"
11- #include " CANDatabaseException.h"
11+
12+ namespace CppCAN {
13+
14+ /* *
15+ * @brief A CAN signal of a frame in the CAN Database
16+ * A CANSignal is represented by the following attributes:
17+ * - Name
18+ * - Start bit
19+ * - Length
20+ * - Scale
21+ * - Offset
22+ * - Endianness
23+ * - Signedness
24+ * - Minimum (optional)
25+ * - Maximum (optional)
26+ * - Comment (optional)
27+ * - Choices (optional) : map of unsigned int -> std::string (so one can associate
28+ * a string value to an integer value)
29+ *
30+ * All the attributes except for the comment and choices must be defined at the instanciation
31+ * and are immutable.
32+ */
33+ class CPP_CAN_PARSER_EXPORT CANSignal {
34+ public:
35+ struct CPP_CAN_PARSER_EXPORT Range {
36+ static Range fromString (const std::string& minstr, const std::string& maxstr);
37+
38+ Range () = default ;
39+ Range (long m, long mm);
40+
41+ bool defined ;
42+ long min;
43+ long max;
44+ };
45+
46+ enum Signedness {
47+ Unsigned, Signed
48+ };
49+
50+ enum Endianness {
51+ BigEndian, LittleEndian
52+ };
53+
54+ public:
55+ CANSignal () = delete ;
56+ CANSignal (const std::string& name, unsigned int start_bit, unsigned int length,
57+ double scale, double offset, Signedness signedness, Endianness endianness, Range range = Range());
58+ CANSignal (const CANSignal&) = default ;
59+ CANSignal (CANSignal&&) = default ;
60+ CANSignal& operator =(const CANSignal&) = default ;
61+ CANSignal& operator =(CANSignal&&) = default ;
62+
63+ const std::string& name () const ;
64+
65+ unsigned int start_bit () const ;
66+
67+ unsigned int length () const ;
68+
69+ const std::string& comment () const ;
70+
71+ double scale () const ;
72+
73+ double offset () const ;
74+
75+ const Range& range () const ;
76+
77+ Signedness signedness () const ;
78+
79+ Endianness endianness () const ;
80+
81+ const std::map<unsigned int , std::string>& choices () const ;
82+
83+ void setComment (const std::string& comment);
84+
85+ void setChoices (const std::map<unsigned int , std::string>& choices);
86+
87+ private:
88+ std::string name_;
89+ unsigned int start_bit_;
90+ unsigned int length_;
91+ double scale_;
92+ double offset_;
93+ Signedness signedness_;
94+ Endianness endianness_;
95+ Range range_;
96+ std::string comment_;
97+ std::map<unsigned int , std::string> choices_;
98+ };
99+
100+ /* *
101+ * @brief Object that gathers all the properties of a single frame.
102+ *
103+ * A CANFrame instance is characterized by the following properties:
104+ * - Name
105+ * - CAN ID
106+ * - DLC
107+ * - Period (optional)
108+ * - Comment (optional)
109+ * - List of signals
110+ *
111+ * The name, CAN ID and DLC must be defined at the instanciation and are immutable.
112+ * The comment and period can respectivelly be changed with setComment() and setPeriod().
113+ * The list of signals can be modified with addSignal() and removeSignal(). Use clear()
114+ * to empty the signals' list.
115+ *
116+ * One can access the CANSignal with at() or operator[]. **Be careful as both will throw
117+ * a CANDatabaseException if the given key does noy match any signal in the frame.** To check
118+ * if a signal is present, use contains(). Signals can be found both by their start bit
119+ * or their name.
120+ *
121+ * CANFrame also behaves like a regular iterable: it defines the functions begin(), end(),
122+ * cbegin(), cend(), ... You can traverse all the signals of the frame in a range-for loop
123+ * or use the standard library's algorithms !
124+ */
125+ class CPP_CAN_PARSER_EXPORT CANFrame {
126+ public:
127+ using container_type = std::map<std::string, CANSignal>;
128+ using iterator = container_type::iterator;
129+ using const_iterator = container_type::const_iterator;
130+ using reverse_iterator = container_type::reverse_iterator;
131+ using const_reverse_iterator = container_type::const_reverse_iterator;
132+
133+ public:
134+ // You cannot construct an empty frame.
135+ CANFrame () = delete ;
136+
137+ /* *
138+ * @brief Construct a new frame.
139+ * @param name Name of the frame
140+ * @param can_id CAN ID of the frame
141+ * @param dlc DLC of the frame
142+ * @param comment Optional comment for the frame
143+ */
144+ CANFrame (const std::string& name, unsigned long long can_id, unsigned int dlc,
145+ unsigned int period = 0 , const std::string& comment = " " );
146+
147+ CANFrame (const CANFrame&) = default ;
148+ CANFrame& operator =(const CANFrame&) = default ;
149+ CANFrame (CANFrame&&) = default ;
150+ CANFrame& operator =(CANFrame&&) = default ;
151+
152+ public:
153+ /* *
154+ * @return The name of the frame
155+ */
156+ const std::string& name () const ;
157+
158+ /* *
159+ * @return The CAN ID of the frame
160+ */
161+ unsigned long long can_id () const ;
162+
163+ /* *
164+ * @return The DLC of the frame
165+ */
166+ unsigned int dlc () const ;
167+
168+ /* *
169+ * @return The period of the frame (If unspecified, then return 0)
170+ */
171+ unsigned int period () const ;
172+
173+ /* *
174+ * @return The comment associated with the frame (if unspecified, return an empty string)
175+ */
176+ const std::string& comment () const ;
177+
178+ public:
179+ /* *
180+ * @brief Sets a new value for the frame's period.
181+ */
182+ void setPeriod (unsigned int val);
183+
184+ /* *
185+ * @brief Updates the frame's associated comment.
186+ */
187+ void setComment (const std::string& comment);
188+
189+ public:
190+ /* *
191+ * @brief Fetches the signal with the given name.
192+ * @see at
193+ */
194+ const CANSignal& operator [](const std::string& name) const ;
195+
196+ /* *
197+ * @brief Fetches the signal with the given name.
198+ * @see at
199+ */
200+ CANSignal& operator [](const std::string& name);
201+
202+ /* *
203+ * @brief Fetches the signal with the given name.
204+ */
205+ const CANSignal& at (const std::string& name) const ;
206+
207+ /* *
208+ * @brief Fetches the signal with the given name.
209+ */
210+ CANSignal& at (const std::string& name);
211+
212+ /* *
213+ * @return true if a signal with the given name is already registered with the current frame.
214+ */
215+ bool contains (const std::string& name) const ;
216+
217+ /* *
218+ * @brief Registers the given signal with the frame.
219+ */
220+ void addSignal (const CANSignal& signal);
221+
222+ /* *
223+ * @brief Removes the signal associated with the given name
224+ */
225+ void removeSignal (const std::string& name);
226+
227+ public:
228+ iterator begin ();
229+ const_iterator begin () const ;
230+ const_iterator cbegin () const ;
231+
232+ iterator end ();
233+ const_iterator end () const ;
234+ const_iterator cend () const ;
235+
236+ reverse_iterator rbegin ();
237+ const_reverse_iterator rbegin () const ;
238+ const_reverse_iterator crbegin () const ;
239+
240+ reverse_iterator rend ();
241+ const_reverse_iterator rend () const ;
242+ const_reverse_iterator crend () const ;
243+
244+ std::size_t size () const ;
245+
246+ void clear ();
247+
248+ friend void swap (CANFrame& first, CANFrame& second);
249+
250+ private:
251+ std::string name_;
252+ unsigned long long can_id_;
253+ unsigned int dlc_;
254+ unsigned int period_;
255+ std::string comment_;
256+
257+ container_type map_;
258+ };
12259
13260/* *
14261 * @brief A CAN database object
@@ -29,7 +276,7 @@ class CPP_CAN_PARSER_EXPORT CANDatabase {
29276 /* *
30277 * @brief A parsing warning and its location
31278 */
32- struct parsing_warning {
279+ struct CPP_CAN_PARSER_EXPORT parsing_warning {
33280 unsigned long long line;
34281 std::string description;
35282 };
@@ -54,12 +301,12 @@ class CPP_CAN_PARSER_EXPORT CANDatabase {
54301 const std::string& src_string, std::vector<parsing_warning>* warnings = nullptr );
55302
56303public:
57- struct IDKey {
304+ struct CPP_CAN_PARSER_EXPORT IDKey {
58305 std::string str_key;
59306 unsigned long long int_key;
60307 };
61308
62- struct IntIDKeyCompare {
309+ struct CPP_CAN_PARSER_EXPORT IntIDKeyCompare {
63310 bool operator ()(const IDKey& k1, const IDKey& k2) const ;
64311 };
65312
@@ -104,6 +351,7 @@ class CPP_CAN_PARSER_EXPORT CANDatabase {
104351 CANDatabase& operator =(CANDatabase&&);
105352
106353 ~CANDatabase ();
354+
107355public:
108356 /* *
109357 * @brief Get the frame with the given frame name
@@ -201,4 +449,13 @@ class CPP_CAN_PARSER_EXPORT CANDatabase {
201449 CANDatabaseImpl* impl;
202450};
203451
452+ /* *
453+ * @brief Exception type for the library's operations
454+ */
455+ class CANDatabaseException : public std ::runtime_error {
456+ public:
457+ using std::runtime_error::runtime_error;
458+ };
459+
460+ }
204461#endif
0 commit comments