Skip to content

Commit 7a2b3c9

Browse files
author
Avi SZYCHTER
committed
Removed the shared_ptr because they were actually useless
1 parent 305db9a commit 7a2b3c9

File tree

8 files changed

+260
-219
lines changed

8 files changed

+260
-219
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: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@
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+
struct IDKey {
17+
std::string str_key;
18+
unsigned long long int_key;
19+
};
20+
21+
struct IntIDKeyCompare {
22+
bool operator()(const IDKey& k1, const IDKey& k2) const;
23+
};
24+
25+
26+
using container_type = std::map<IDKey, CANSignal, IntIDKeyCompare>;
1827
using iterator = container_type::iterator;
1928
using const_iterator = container_type::const_iterator;
2029
using reverse_iterator = container_type::reverse_iterator;
@@ -31,12 +40,13 @@ class CANFrame {
3140
* @param dlc DLC of the frame
3241
* @param comment Optional comment for the frame
3342
*/
34-
CANFrame(const std::string& name, unsigned long long can_id, unsigned int dlc, unsigned int period = 0, const std::string& comment = "");
43+
CANFrame(const std::string& name, unsigned long long can_id, unsigned int dlc,
44+
unsigned int period = 0, const std::string& comment = "");
3545

36-
CANFrame(const CANFrame&);
37-
CANFrame& operator=(CANFrame);
38-
CANFrame(CANFrame&&);
39-
CANFrame& operator=(CANFrame&&);
46+
CANFrame(const CANFrame&) = default;
47+
CANFrame& operator=(const CANFrame&) = default;
48+
CANFrame(CANFrame&&) = default;
49+
CANFrame& operator=(CANFrame&&) = default;
4050

4151
public:
4252
/**
@@ -77,41 +87,36 @@ class CANFrame {
7787

7888
public:
7989
/**
80-
* @brief Fetches the signal with the given start bit.
90+
* @brief Fetches the signal with the given name.
8191
* @see at
8292
*/
83-
std::shared_ptr<CANSignal> operator[](unsigned int start_bit) const;
84-
93+
const CANSignal& operator[](const std::string& name) const;
94+
8595
/**
8696
* @brief Fetches the signal with the given name.
8797
* @see at
8898
*/
89-
std::shared_ptr<CANSignal> operator[](const std::string& name) const;
99+
CANSignal& operator[](const std::string& name);
90100

91101
/**
92102
* @brief Fetches the signal with the given name.
93103
*/
94-
std::shared_ptr<CANSignal> at(const std::string& name) const;
95-
104+
const CANSignal& at(const std::string& name) const;
105+
96106
/**
97-
* @brief Fetches the signal with the given start bit.
107+
* @brief Fetches the signal with the given name.
98108
*/
99-
std::shared_ptr<CANSignal> at(unsigned int start_bit) const;
109+
CANSignal& at(const std::string& name);
100110

101111
/**
102112
* @return true if a signal with the given name is already registered with the current frame.
103113
*/
104114
bool contains(const std::string& name) const;
105115

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-
111116
/**
112117
* @brief Registers the given signal with the frame.
113118
*/
114-
void addSignal(std::shared_ptr<CANSignal> signal);
119+
void addSignal(const CANSignal& signal);
115120

116121
/**
117122
* @brief Removes the signal with the given start bit
@@ -151,9 +156,11 @@ class CANFrame {
151156
unsigned long long can_id_;
152157
unsigned int dlc_;
153158
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
156159
std::string comment_;
160+
161+
container_type map_;
162+
std::map<unsigned, IDKey> intKeyIdx_; // Index by start bit
163+
std::map<std::string, IDKey> strKeyIdx_; // Index by name
157164
};
158165

159166
#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)