Skip to content

Commit f5f0975

Browse files
authored
Merge pull request #2 from xatavian/topic-make-coherent-database-again
Make the API coherant again
2 parents 305db9a + ccbdb36 commit f5f0975

File tree

8 files changed

+214
-232
lines changed

8 files changed

+214
-232
lines changed

include/CANDatabase.h

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

4242
public:
43-
using container_type = std::map<unsigned long long, std::shared_ptr<CANFrame>>;
44-
using str_container_type = std::map<std::string, std::shared_ptr<CANFrame>>;
43+
struct IDKey {
44+
std::string str_key;
45+
unsigned long long int_key;
46+
};
47+
48+
struct IntIDKeyCompare {
49+
bool operator()(const IDKey& k1, const IDKey& k2) const;
50+
};
51+
52+
using container_type = std::map<IDKey, CANFrame, IntIDKeyCompare>;
4553

4654
using iterator = container_type::iterator;
4755
using const_iterator = container_type::const_iterator;
@@ -64,46 +72,63 @@ class CANDatabase {
6472
* Creates a copy of the database: the individual frames are deep copied so there is no
6573
* shared memory betwwen the two databases.
6674
*/
67-
CANDatabase(const CANDatabase&);
75+
CANDatabase(const CANDatabase&) = default;
6876

6977
/**
7078
* @brief Makes a copy of the given database
71-
* Note: the source database is passed-by-value for RVO
72-
* (see https://stackoverflow.com/a/3279550/8147455 for more info)
7379
*/
74-
CANDatabase& operator=(const CANDatabase&);
80+
CANDatabase& operator=(const CANDatabase&) = default;
7581

7682
/**
7783
* @brief Moves a CANDatabase object. The CANFrame objects are NOT deep copied.
7884
*/
79-
CANDatabase(CANDatabase&&);
85+
CANDatabase(CANDatabase&&) = default;
8086

8187
/**
8288
* @see CANDatabase(const CANDatabase&&)
8389
*/
84-
CANDatabase& operator=(CANDatabase&&);
90+
CANDatabase& operator=(CANDatabase&&) = default;
8591

8692
public:
8793
/**
8894
* @brief Get the frame with the given frame name
8995
*/
90-
std::shared_ptr<CANFrame> at(unsigned long long frame_name) const;
96+
const CANFrame& at(unsigned long long frame_id) const;
97+
98+
/**
99+
* @brief Get the frame with the given frame name
100+
*/
101+
CANFrame& at(unsigned long long frame_id);
102+
103+
/**
104+
* @brief Get the frame with the given frame id
105+
*/
106+
const CANFrame& at(const std::string& frame_name) const;
91107

92108
/**
93109
* @brief Get the frame with the given frame id
94110
*/
95-
std::shared_ptr<CANFrame> at(const std::string& frame_name) const;
111+
CANFrame& at(const std::string& frame_name);
96112

97113
/**
98114
* @brief Get the frame with the given frame id
99-
* @see getFrame
100115
*/
101-
std::shared_ptr<CANFrame> operator[](unsigned long long frame_idx) const;
116+
const CANFrame& operator[](unsigned long long frame_idx) const;
117+
118+
/**
119+
* @brief Get the frame with the given frame id
120+
*/
121+
CANFrame& operator[](unsigned long long frame_idx);
102122

103123
/**
104124
* @brief Get the frame with the given frame name
105125
*/
106-
std::shared_ptr<CANFrame> operator[](const std::string& frame_name) const;
126+
const CANFrame& operator[](const std::string& frame_name) const;
127+
128+
/**
129+
* @brief Get the frame with the given frame name
130+
*/
131+
CANFrame& operator[](const std::string& frame_name);
107132

108133
/**
109134
* @return true if the CANDatabase contains a frame with the given frame id
@@ -152,14 +177,16 @@ class CANDatabase {
152177

153178
void clear();
154179

155-
void addFrame(std::shared_ptr<CANFrame> frame);
180+
void addFrame(const CANFrame& frame);
156181
void removeFrame(unsigned int idx);
157182
void removeFrame(const std::string& name);
158183

159184
private:
160185
std::string filename_;
161-
str_container_type strIndex_; // Index by frame name
162-
container_type intIndex_; // Index by CAN ID
186+
container_type map_; // Index by CAN ID
187+
188+
std::map<unsigned long long, IDKey> intKeyIndex_;
189+
std::map<std::string, IDKey> strKeyIndex_;
163190
};
164191

165192
#endif

include/CANFrame.h

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
*/
1414
class CANFrame {
1515
public:
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>>;
16+
using container_type = std::map<std::string, CANSignal>;
1817
using iterator = container_type::iterator;
1918
using const_iterator = container_type::const_iterator;
2019
using reverse_iterator = container_type::reverse_iterator;
@@ -31,12 +30,13 @@ class CANFrame {
3130
* @param dlc DLC of the frame
3231
* @param comment Optional comment for the frame
3332
*/
34-
CANFrame(const std::string& name, unsigned long long can_id, unsigned int dlc, unsigned int period = 0, const std::string& comment = "");
33+
CANFrame(const std::string& name, unsigned long long can_id, unsigned int dlc,
34+
unsigned int period = 0, const std::string& comment = "");
3535

36-
CANFrame(const CANFrame&);
37-
CANFrame& operator=(CANFrame);
38-
CANFrame(CANFrame&&);
39-
CANFrame& operator=(CANFrame&&);
36+
CANFrame(const CANFrame&) = default;
37+
CANFrame& operator=(const CANFrame&) = default;
38+
CANFrame(CANFrame&&) = default;
39+
CANFrame& operator=(CANFrame&&) = default;
4040

4141
public:
4242
/**
@@ -77,46 +77,36 @@ class CANFrame {
7777

7878
public:
7979
/**
80-
* @brief Fetches the signal with the given start bit.
80+
* @brief Fetches the signal with the given name.
8181
* @see at
8282
*/
83-
std::shared_ptr<CANSignal> operator[](unsigned int start_bit) const;
84-
83+
const CANSignal& operator[](const std::string& name) const;
84+
8585
/**
8686
* @brief Fetches the signal with the given name.
8787
* @see at
8888
*/
89-
std::shared_ptr<CANSignal> operator[](const std::string& name) const;
89+
CANSignal& operator[](const std::string& name);
9090

9191
/**
9292
* @brief Fetches the signal with the given name.
9393
*/
94-
std::shared_ptr<CANSignal> at(const std::string& name) const;
95-
94+
const CANSignal& at(const std::string& name) const;
95+
9696
/**
97-
* @brief Fetches the signal with the given start bit.
97+
* @brief Fetches the signal with the given name.
9898
*/
99-
std::shared_ptr<CANSignal> at(unsigned int start_bit) const;
99+
CANSignal& at(const std::string& name);
100100

101101
/**
102102
* @return true if a signal with the given name is already registered with the current frame.
103103
*/
104104
bool contains(const std::string& name) const;
105105

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;
110-
111106
/**
112107
* @brief Registers the given signal with the frame.
113108
*/
114-
void addSignal(std::shared_ptr<CANSignal> signal);
115-
116-
/**
117-
* @brief Removes the signal with the given start bit
118-
*/
119-
void removeSignal(unsigned int start_bit);
109+
void addSignal(const CANSignal& signal);
120110

121111
/**
122112
* @brief Removes the signal associated with the given name
@@ -151,9 +141,9 @@ class CANFrame {
151141
unsigned long long can_id_;
152142
unsigned int dlc_;
153143
unsigned int period_;
154-
std::map<unsigned int, std::shared_ptr<CANSignal>> intIndex_; // Index by start bit
155-
std::map<std::string, std::shared_ptr<CANSignal>> strIndex_; // Index by name
156144
std::string comment_;
145+
146+
container_type map_;
157147
};
158148

159149
#endif

src/analysis/CANFrameAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ std::vector<SignalLayoutEntry> compute_layout(const CANFrame& src) {
7575
std::vector<SignalLayoutEntry> result;
7676

7777
for(const auto& signal: src) {
78-
const CANSignal& sig = *signal.second;
78+
const CANSignal& sig = signal.second;
7979
int lr_start_bit, lr_end_bit;
8080

8181
if(sig.endianness() == CANSignal::BigEndian) {

0 commit comments

Comments
 (0)