Skip to content

Commit 7b8d468

Browse files
committed
Implementation of thread safe copy constructor, assignment operator and added a new API.
1 parent bf0b97d commit 7b8d468

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

include/curl_exception.h

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,19 @@ namespace curl {
5151
*/
5252
class curl_exception : public std::exception {
5353
public:
54-
5554
/**
5655
* This constructor is used to build the error.
5756
*/
5857
curl_exception(const std::string&, const std::string&);
58+
/**
59+
* The copy constructor allows to copy the object in a thread safe way.
60+
*/
61+
curl_exception(const curl_exception &);
62+
/**
63+
* The assignment operator allows to assign the object to another object in
64+
* a thread safe way.
65+
*/
66+
curl_exception & operator=(curl_exception &);
5967
/**
6068
* The destructor, in this case, doesn't do anything.
6169
*/
@@ -73,6 +81,11 @@ namespace curl {
7381
* Simple method which clears the entire error stack.
7482
*/
7583
void clear_traceback() const;
84+
/**
85+
* Simple method which clears the error stack saving it (before cleaning) in a
86+
* traceback specified in input.
87+
*/
88+
void clear_traceback(curlcpp_traceback &) const;
7689
private:
7790
/**
7891
* The error container must be static or will be cleared
@@ -88,11 +101,11 @@ namespace curl {
88101

89102
// Implementation of print_traceback
90103
inline void curl_exception::print_traceback() const {
91-
curl_exception::tracebackLocker.lock();
104+
curl_exception::tracebackLocker.lock();
92105
std::for_each(curl_exception::traceback.begin(),curl_exception::traceback.end(),[](const curlcpp_traceback_object &value) {
93106
std::cout<<"ERROR: "<<value.first<<" ::::: FUNCTION: "<<value.second<<std::endl;
94107
});
95-
curl_exception::tracebackLocker.unlock();
108+
curl_exception::tracebackLocker.unlock();
96109
}
97110

98111
// Implementation of clear method.
@@ -101,10 +114,21 @@ namespace curl {
101114
curl_exception::traceback.clear();
102115
curl_exception::tracebackLocker.unlock();
103116
}
117+
118+
// Implementation of overloaded clear method.
119+
inline void curl_exception::clear_traceback(curlcpp_traceback& traceback_ref) const {
120+
curl_exception::tracebackLocker.lock();
121+
traceback_ref = curl_exception::traceback;
122+
curl_exception::traceback.clear();
123+
curl_exception::tracebackLocker.unlock();
124+
}
104125

105126
// Implementation of get_traceback.
106127
inline curlcpp_traceback curl_exception::get_traceback() const {
107-
return curl_exception::traceback;
128+
curl_exception::tracebackLocker.lock();
129+
curlcpp_traceback tmp = curl_exception::traceback;
130+
curl_exception::tracebackLocker.unlock();
131+
return tmp;
108132
}
109133

110134
/**
@@ -129,7 +153,6 @@ namespace curl {
129153
inline CURLcode get_code() const {
130154
return code;
131155
}
132-
133156
private:
134157
CURLcode code;
135158
};
@@ -156,7 +179,6 @@ namespace curl {
156179
inline CURLMcode get_code() const {
157180
return code;
158181
}
159-
160182
private:
161183
CURLMcode code;
162184
};
@@ -183,7 +205,6 @@ namespace curl {
183205
inline CURLSHcode get_code() const {
184206
return code;
185207
}
186-
187208
private:
188209
CURLSHcode code;
189210
};

src/curl_exception.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using curl::curlcpp_traceback;
1010

1111
// Need to define the traceback here to separate declaration from definition, or we'll get a linker error...
1212
curlcpp_traceback curl::curl_exception::traceback;
13-
// .. same for the traceback mutexe.
13+
// .. same for the traceback mutex.
1414
std::mutex curl::curl_exception::tracebackLocker;
1515

1616
// Constructor implementation. Every call will push into the calls stack the function name and the error occurred.
@@ -20,6 +20,23 @@ curl_exception::curl_exception(const std::string &error, const std::string &fun_
2020
curl_exception::tracebackLocker.unlock();
2121
}
2222

23+
// Copy constructor implementation. It makes a copy of the traceback in a thread safe way.
24+
curl_exception::curl_exception(const curl_exception &object) {
25+
curl_exception::tracebackLocker.lock();
26+
curl_exception::traceback = object.get_traceback();
27+
curl_exception::tracebackLocker.unlock();
28+
}
29+
30+
// Assignment operator implementation. Implement the assignment operation in a thread safe way avoiding self assignment.
31+
curl_exception& curl_exception::operator=(curl_exception &object) {
32+
if (&object != this) {
33+
curl_exception::tracebackLocker.lock();
34+
curl_exception::traceback = object.get_traceback();
35+
curl_exception::tracebackLocker.unlock();
36+
}
37+
return *this;
38+
}
39+
2340
// Implementation of destructor.
2441
curl_exception::~curl_exception() NOEXCEPT {
2542
// ... nothing to do here ...

0 commit comments

Comments
 (0)