Skip to content

Commit ccbdb36

Browse files
author
Avi SZYCHTER
committed
CANSignals are only indexed by name now
1 parent 7a2b3c9 commit ccbdb36

File tree

2 files changed

+11
-70
lines changed

2 files changed

+11
-70
lines changed

include/CANFrame.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,7 @@
1313
*/
1414
class CANFrame {
1515
public:
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>;
16+
using container_type = std::map<std::string, CANSignal>;
2717
using iterator = container_type::iterator;
2818
using const_iterator = container_type::const_iterator;
2919
using reverse_iterator = container_type::reverse_iterator;
@@ -118,11 +108,6 @@ class CANFrame {
118108
*/
119109
void addSignal(const CANSignal& signal);
120110

121-
/**
122-
* @brief Removes the signal with the given start bit
123-
*/
124-
void removeSignal(unsigned int start_bit);
125-
126111
/**
127112
* @brief Removes the signal associated with the given name
128113
*/
@@ -159,8 +144,6 @@ class CANFrame {
159144
std::string comment_;
160145

161146
container_type map_;
162-
std::map<unsigned, IDKey> intKeyIdx_; // Index by start bit
163-
std::map<std::string, IDKey> strKeyIdx_; // Index by name
164147
};
165148

166149
#endif

src/models/CANFrame.cpp

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,15 @@ void CANFrame::setComment(const std::string& comment) {
3636
}
3737

3838
bool CANFrame::contains(const std::string& name) const {
39-
return strKeyIdx_.find(name) != strKeyIdx_.end();
39+
return map_.find(name) != map_.end();
4040
}
4141

4242
const CANSignal& CANFrame::at(const std::string& name) const {
43-
try {
44-
const IDKey& map_key = strKeyIdx_.at(name);
45-
return map_.at(map_key);
46-
}
47-
catch(const std::out_of_range& e) {
48-
throw;
49-
}
43+
return map_.at(name);
5044
}
5145

5246
CANSignal& CANFrame::at(const std::string& name) {
53-
try {
54-
const IDKey& map_key = strKeyIdx_.at(name);
55-
return map_.at(map_key);
56-
}
57-
catch(const std::out_of_range& e) {
58-
throw;
59-
}
47+
return map_.at(name);
6048
}
6149

6250
const CANSignal& CANFrame::operator[](const std::string& name) const {
@@ -67,27 +55,20 @@ CANSignal& CANFrame::operator[](const std::string& name) {
6755
return at(name);
6856
}
6957

70-
void CANFrame::addSignal(const CANSignal& signal) {
71-
IDKey map_key = { signal.name(), signal.start_bit() };
72-
73-
map_.insert(std::make_pair(map_key, signal));
74-
intKeyIdx_.insert(std::make_pair( signal.start_bit(), map_key));
75-
strKeyIdx_.insert(std::make_pair( signal.name(), map_key));
58+
void CANFrame::addSignal(const CANSignal& signal) {
59+
map_.insert(std::make_pair(signal.name(), signal));
7660
}
7761

7862
void CANFrame::removeSignal(const std::string& name) {
79-
try {
80-
const IDKey& map_key = strKeyIdx_.at(name);
8163

82-
intKeyIdx_.erase(intKeyIdx_.find(map_key.int_key));
83-
strKeyIdx_.erase(strKeyIdx_.find(map_key.str_key));
84-
map_.erase(map_.find(map_key));
85-
}
86-
catch(const std::out_of_range&) {
64+
auto ite = map_.find(name);
65+
if(ite == map_.end()) {
8766
std::string excepText = "Cannot remove signal with name \"" + name +
8867
"\" from frame \"" + this->name() + "\"";
8968
throw std::out_of_range(excepText);
9069
}
70+
71+
map_.erase(ite);
9172
}
9273

9374
CANFrame::iterator CANFrame::begin() {
@@ -155,23 +136,6 @@ std::size_t CANFrame::size() const
155136

156137
void CANFrame::clear() {
157138
map_.clear();
158-
intKeyIdx_.clear();
159-
strKeyIdx_.clear();
160-
}
161-
162-
void CANFrame::removeSignal(unsigned int start_bit) {
163-
try {
164-
const IDKey& map_key = intKeyIdx_.at(start_bit);
165-
166-
intKeyIdx_.erase(intKeyIdx_.find(map_key.int_key));
167-
strKeyIdx_.erase(strKeyIdx_.find(map_key.str_key));
168-
map_.erase(map_.find(map_key));
169-
}
170-
catch(const std::out_of_range&) {
171-
std::string excepText = "Cannot remove signal with start bit " + std::to_string(start_bit) +
172-
"\" from frame \"" + this->name() + "\"";
173-
throw std::out_of_range(excepText);
174-
}
175139
}
176140

177141
void swap(CANFrame & first, CANFrame & second) {
@@ -180,11 +144,5 @@ void swap(CANFrame & first, CANFrame & second) {
180144
std::swap(first.dlc_, second.dlc_);
181145
std::swap(first.period_, second.period_);
182146
std::swap(first.map_, second.map_);
183-
std::swap(first.intKeyIdx_, second.intKeyIdx_);
184-
std::swap(first.strKeyIdx_, second.strKeyIdx_);
185147
std::swap(first.comment_, second.comment_);
186-
}
187-
188-
bool CANFrame::IntIDKeyCompare::operator()(const IDKey& k1, const IDKey& k2) const {
189-
return k1.int_key < k2.int_key;
190-
}
148+
}

0 commit comments

Comments
 (0)