Skip to content

Commit 520b5c9

Browse files
committed
Added rule of five and swap to CANDatabase
1 parent 3e23cc9 commit 520b5c9

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

include/CANDatabase.h

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

42+
public:
43+
using container_type = std::map<unsigned int, std::shared_ptr<CANFrame>>;
44+
using str_container_type = std::map<std::string, std::shared_ptr<CANFrame>>;
45+
46+
using iterator = container_type::iterator;
47+
using const_iterator = container_type::const_iterator;
48+
using reverse_iterator = container_type::reverse_iterator;
49+
using const_reverse_iterator = container_type::const_reverse_iterator;
50+
4251
public:
4352
/**
4453
* @brief Creates a CANDatabase object with no source file
@@ -51,6 +60,29 @@ class CANDatabase {
5160
*/
5261
CANDatabase(const std::string& filename);
5362

63+
/**
64+
* Creates a copy of the database: the individual frames are deep copied so there is no
65+
* shared memory betwwen the two databases.
66+
*/
67+
CANDatabase(const CANDatabase&);
68+
69+
/**
70+
* @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)
73+
*/
74+
CANDatabase& operator=(CANDatabase);
75+
76+
/**
77+
* @brief Moves a CANDatabase object. The CANFrame objects are NOT deep copied.
78+
*/
79+
CANDatabase(CANDatabase&&);
80+
81+
/**
82+
* @see CANDatabase(const CANDatabase&&)
83+
*/
84+
CANDatabase& operator=(CANDatabase&&);
85+
5486
public:
5587
/**
5688
* @brief Get the frame with the given frame name
@@ -83,6 +115,10 @@ class CANDatabase {
83115
*/
84116
bool contains(const std::string& frame_name) const;
85117

118+
/**
119+
* @brief Swaps the content of the two given databases
120+
*/
121+
friend void swap(CANDatabase& first, CANDatabase& second);
86122
/**
87123
* @return File name of the source file if the database was constructed from a file.
88124
* Otherwise, returns an empty string.
@@ -96,13 +132,6 @@ class CANDatabase {
96132
97133
The iterators have a random order. */
98134
public:
99-
using container_type = std::map<unsigned int, std::shared_ptr<CANFrame>>;
100-
using str_container_type = std::map<std::string, std::shared_ptr<CANFrame>>;
101-
using iterator = container_type::iterator;
102-
using const_iterator = container_type::const_iterator;
103-
using reverse_iterator = container_type::reverse_iterator;
104-
using const_reverse_iterator = container_type::const_reverse_iterator;
105-
106135
iterator begin();
107136
const_iterator begin() const;
108137
const_iterator cbegin() const;

src/models/CANDatabase.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@
66
CANDatabase::CANDatabase(const std::string& filename)
77
: filename_(filename), strIndex_(), intIndex_() { }
88

9+
CANDatabase::CANDatabase(const CANDatabase& src):
10+
filename_(src.filename_), strIndex_(), intIndex_() {
11+
12+
for (const auto& frame : src) {
13+
auto copy = std::make_shared<CANFrame>(frame);
14+
intIndex_.insert(std::make_pair(frame.second->can_id(), copy));
15+
strIndex_.insert(std::make_pair(frame.second->name(), copy));
16+
}
17+
}
18+
19+
CANDatabase & CANDatabase::operator=(CANDatabase src) {
20+
swap(*this, src);
21+
return *this;
22+
}
23+
24+
CANDatabase::CANDatabase(CANDatabase && src)
25+
: filename_(), strIndex_(), intIndex_() {
26+
27+
swap(*this, src);
28+
}
29+
30+
CANDatabase & CANDatabase::operator=(CANDatabase && src) {
31+
swap(*this, src);
32+
return *this;
33+
}
34+
935
const std::string& CANDatabase::filename() const {
1036
return filename_;
1137
}
@@ -161,3 +187,9 @@ void CANDatabase::clear() {
161187
intIndex_.clear();
162188
strIndex_.clear();
163189
}
190+
191+
void swap(CANDatabase & first, CANDatabase & second) {
192+
std::swap(first.intIndex_, second.intIndex_);
193+
std::swap(first.strIndex_, second.strIndex_);
194+
std::swap(first.filename_, second.filename_);
195+
}

0 commit comments

Comments
 (0)