Skip to content

Commit 97a7f22

Browse files
committed
Different improvment + the library compiles again :)
Frames are now identified by a uns long long integer (was uns int before) Fixed different compilation issues Frames now have an iterator that enables to iterate over the signals of the frame Frames behave a little bit more like standard containers Exceptions are thrown when at() does not find the required key
1 parent 520b5c9 commit 97a7f22

File tree

10 files changed

+395
-164
lines changed

10 files changed

+395
-164
lines changed

CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ set(CPPPARSER_INCLUDE_DIRECTORY
1212
set(CPPPARSER_SRC_FILES
1313
src/models/CANDatabase.cpp
1414
src/models/CANFrame.cpp
15+
src/models/CANSignal.cpp
1516
src/parsing/DBCParser.cpp
1617
src/parsing/Tokenizer.cpp)
1718

1819
add_library(cpp_can_parser STATIC ${CPPPARSER_SRC_FILES})
1920
target_include_directories(cpp_can_parser
2021
PUBLIC ${CPPPARSER_INCLUDE_DIRECTORY})
2122

22-
add_executable(dbcLoadStringExec
23-
tests/dbcLoadStringTest.cpp)
24-
target_link_libraries(dbcLoadStringExec cpp_can_parser)
23+
add_executable(dbcLoadFileExec
24+
tests/dbcLoadFileTest.cpp)
25+
target_link_libraries(dbcLoadFileExec cpp_can_parser)
2526

26-
add_test(NAME dbcLoadStringTest
27-
COMMAND dbcLoadStringExec)
27+
add_test(NAME dbcLoadFileTest
28+
COMMAND dbcLoadFileExec)

include/CANDatabase.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class CANDatabase {
4040
static CANDatabase fromString(const std::string& src_string);
4141

4242
public:
43-
using container_type = std::map<unsigned int, std::shared_ptr<CANFrame>>;
43+
using container_type = std::map<unsigned long long, std::shared_ptr<CANFrame>>;
4444
using str_container_type = std::map<std::string, std::shared_ptr<CANFrame>>;
4545

4646
using iterator = container_type::iterator;
@@ -87,28 +87,28 @@ class CANDatabase {
8787
/**
8888
* @brief Get the frame with the given frame name
8989
*/
90-
std::weak_ptr<CANFrame> at(unsigned int frame_name) const;
90+
std::shared_ptr<CANFrame> at(unsigned long long frame_name) const;
9191

9292
/**
9393
* @brief Get the frame with the given frame id
9494
*/
95-
std::weak_ptr<CANFrame> at(const std::string& frame_name) const;
95+
std::shared_ptr<CANFrame> at(const std::string& frame_name) const;
9696

9797
/**
9898
* @brief Get the frame with the given frame id
9999
* @see getFrame
100100
*/
101-
std::weak_ptr<CANFrame> operator[](unsigned int frame_idx) const;
101+
std::shared_ptr<CANFrame> operator[](unsigned long long frame_idx) const;
102102

103103
/**
104104
* @brief Get the frame with the given frame name
105105
*/
106-
std::weak_ptr<CANFrame> operator[](const std::string& frame_name) const;
106+
std::shared_ptr<CANFrame> operator[](const std::string& frame_name) const;
107107

108108
/**
109109
* @return true if the CANDatabase contains a frame with the given frame id
110110
*/
111-
bool contains(unsigned int frame_id) const;
111+
bool contains(unsigned long long can_id) const;
112112

113113
/**
114114
* @return true if the CANDatabase contains a frame with the given frame name

include/CANFrame.h

Lines changed: 125 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,147 @@
88

99
#include "CANSignal.h"
1010

11+
/**
12+
* @brief Object that gathers all the properties of a single frame.
13+
*/
1114
class CANFrame {
1215
public:
13-
CANFrame(const std::string& name, unsigned int can_id, unsigned int dlc);
16+
using container_type = std::map<unsigned int, std::shared_ptr<CANSignal>>;
17+
using str_container_type = std::map<std::string, std::shared_ptr<CANSignal>>;
18+
using iterator = container_type::iterator;
19+
using const_iterator = container_type::const_iterator;
20+
using reverse_iterator = container_type::reverse_iterator;
21+
using const_reverse_iterator = container_type::const_reverse_iterator;
1422

23+
public:
24+
// You cannot construct an empty frame.
25+
CANFrame() = delete;
26+
27+
/**
28+
* @brief Construct a new frame.
29+
* @param name Name of the frame
30+
* @param can_id CAN ID of the frame
31+
* @param dlc DLC of the frame
32+
* @param comment Optional comment for the frame
33+
*/
34+
CANFrame(const std::string& name, unsigned long long can_id, unsigned int dlc, unsigned int period = 0, const std::string& comment = "");
35+
36+
CANFrame(const CANFrame&);
37+
CANFrame& operator=(CANFrame);
38+
CANFrame(CANFrame&&);
39+
CANFrame& operator=(CANFrame&&);
40+
41+
public:
42+
/**
43+
* @return The name of the frame
44+
*/
1545
const std::string& name() const;
16-
unsigned int can_id() const;
46+
47+
/**
48+
* @return The CAN ID of the frame
49+
*/
50+
unsigned long long can_id() const;
51+
52+
/**
53+
* @return The DLC of the frame
54+
*/
1755
unsigned int dlc() const;
56+
57+
/**
58+
* @return The period of the frame (If unspecified, then return 0)
59+
*/
1860
unsigned int period() const;
61+
62+
/**
63+
* @return The comment associated with the frame (if unspecified, return an empty string)
64+
*/
1965
const std::string& comment() const;
2066

21-
std::weak_ptr<CANSignal> getSignalByName(const std::string& name) const;
22-
std::weak_ptr<CANSignal> getSignalByStartBit(unsigned int start_bit) const;
23-
std::vector<std::weak_ptr<CANSignal>> signals() const;
67+
public:
68+
/**
69+
* @brief Sets a new value for the frame's period.
70+
*/
71+
void setPeriod(unsigned int val);
72+
73+
/**
74+
* @brief Updates the frame's associated comment.
75+
*/
76+
void setComment(const std::string& comment);
77+
78+
public:
79+
/**
80+
* @brief Fetches the signal with the given start bit.
81+
* @see at
82+
*/
83+
std::shared_ptr<CANSignal> operator[](unsigned int start_bit) const;
84+
85+
/**
86+
* @brief Fetches the signal with the given name.
87+
* @see at
88+
*/
89+
std::shared_ptr<CANSignal> operator[](const std::string& name) const;
90+
91+
/**
92+
* @brief Fetches the signal with the given name.
93+
*/
94+
std::shared_ptr<CANSignal> at(const std::string& name) const;
95+
96+
/**
97+
* @brief Fetches the signal with the given start bit.
98+
*/
99+
std::shared_ptr<CANSignal> at(unsigned int start_bit) const;
100+
101+
/**
102+
* @return true if a signal with the given name is already registered with the current frame.
103+
*/
104+
bool contains(const std::string& name) const;
105+
106+
/**
107+
* @return true if a signal with the given start bit is already registered with the current frame.
108+
*/
109+
bool contains(unsigned int start_bit) const;
24110

111+
/**
112+
* @brief Registers the given signal with the frame.
113+
*/
25114
void addSignal(std::shared_ptr<CANSignal> signal);
115+
116+
/**
117+
* @brief Removes the signal with the given start bit
118+
*/
26119
void removeSignal(unsigned int start_bit);
120+
121+
/**
122+
* @brief Removes the signal associated with the given name
123+
*/
27124
void removeSignal(const std::string& name);
28125

29-
bool hasSignal(const std::string& name) const;
126+
public:
127+
iterator begin();
128+
const_iterator begin() const;
129+
const_iterator cbegin() const;
130+
131+
iterator end();
132+
const_iterator end() const;
133+
const_iterator cend() const;
134+
135+
reverse_iterator rbegin();
136+
const_reverse_iterator rbegin() const;
137+
const_reverse_iterator crbegin() const;
138+
139+
reverse_iterator rend();
140+
const_reverse_iterator rend() const;
141+
const_reverse_iterator crend() const;
142+
143+
std::size_t size() const;
144+
145+
void clear();
146+
147+
friend void swap(CANFrame& first, CANFrame& second);
30148

31-
void setPeriod(unsigned int val);
32-
void setComment(const std::string& comment);
33149
private:
34150
std::string name_;
35-
unsigned int can_id_;
151+
unsigned long long can_id_;
36152
unsigned int dlc_;
37153
unsigned int period_;
38154
std::map<unsigned int, std::shared_ptr<CANSignal>> intIndex_; // Index by start bit

include/CANSignal.h

Lines changed: 35 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22
#define CANSignal_H
33

44
#include <string>
5-
#include <memory>
5+
#include <map>
66
#include <iostream>
77

88
class CANSignal {
99
public:
1010
struct Range {
11-
static Range fromString(const std::string& minstr, const std::string& maxstr) {
12-
long min = std::stol(minstr);
13-
long max = std::stol(maxstr);
11+
static Range fromString(const std::string& minstr, const std::string& maxstr);
1412

15-
return Range(min, max);
16-
}
17-
Range() :
18-
defined(false), min(0), max(0) {}
19-
Range(long m, long mm) :
20-
defined(true), min(m), max(mm) {}
13+
Range() = default;
14+
Range(long m, long mm);
2115

2216
bool defined;
2317
long min;
@@ -31,60 +25,39 @@ class CANSignal {
3125
enum Endianness {
3226
BigEndian, LittleEndian
3327
};
28+
3429
public:
30+
CANSignal() = delete;
3531
CANSignal(const std::string& name, unsigned int start_bit, unsigned int length,
36-
double scale, double offset, Signedness signedness, Endianness endianness, Range range = Range()) :
37-
name_(name), start_bit_(start_bit), length_(length),
38-
scale_(scale), offset_(offset), signedness_(signedness), endianness_(endianness),
39-
range_(range) { }
40-
41-
const std::string& name() const {
42-
return name_;
43-
}
44-
45-
unsigned int start_bit() const {
46-
return start_bit_;
47-
}
48-
49-
unsigned int length() const {
50-
return length_;
51-
}
52-
53-
const std::string& comment() const {
54-
return comment_;
55-
}
56-
57-
double scale() const {
58-
return scale_;
59-
}
60-
61-
double offset() const {
62-
return offset_;
63-
}
64-
65-
const Range& range() const {
66-
return range_;
67-
}
68-
69-
Signedness signedness() const {
70-
return signedness_;
71-
}
72-
73-
Endianness endianness() const {
74-
return endianness_;
75-
}
76-
77-
const std::map<unsigned int, std::string>& choices() const {
78-
return choices_;
79-
}
80-
81-
void setComment(const std::string& comment) {
82-
comment_ = comment;
83-
}
84-
85-
void setChoices(const std::map<unsigned int, std::string>& choices) {
86-
choices_ = choices;
87-
}
32+
double scale, double offset, Signedness signedness, Endianness endianness, Range range = Range());
33+
CANSignal(const CANSignal&) = default;
34+
CANSignal(CANSignal&&) = default;
35+
CANSignal& operator=(const CANSignal&) = default;
36+
CANSignal& operator=(CANSignal&&) = default;
37+
38+
const std::string& name() const;
39+
40+
unsigned int start_bit() const;
41+
42+
unsigned int length() const;
43+
44+
const std::string& comment() const;
45+
46+
double scale() const;
47+
48+
double offset() const;
49+
50+
const Range& range() const;
51+
52+
Signedness signedness() const;
53+
54+
Endianness endianness() const;
55+
56+
const std::map<unsigned int, std::string>& choices() const;
57+
58+
void setComment(const std::string& comment);
59+
60+
void setChoices(const std::map<unsigned int, std::string>& choices);
8861

8962
private:
9063
std::string name_;

0 commit comments

Comments
 (0)