-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from crypto-chassis/improvement
Update: refactor event queue
- Loading branch information
Showing
9 changed files
with
76 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef INCLUDE_CCAPI_CPP_CCAPI_QUEUE_H_ | ||
#define INCLUDE_CCAPI_CPP_CCAPI_QUEUE_H_ | ||
#include <queue> | ||
#include <mutex> | ||
#include <vector> | ||
#include "ccapi_cpp/ccapi_logger.h" | ||
namespace ccapi { | ||
template <class T> | ||
class Queue { | ||
public: | ||
std::string EXCEPTION_QUEUE_FULL = "queue is full"; | ||
std::string EXCEPTION_QUEUE_EMPTY = "queue is empty"; | ||
explicit Queue(const size_t maxSize = 0) | ||
: maxSize(maxSize) {} | ||
void pushBack(T&& t) { | ||
std::lock_guard<std::mutex> lock(this->m); | ||
if (this->maxSize <= 0 || this->queue.size() < this->maxSize) { | ||
CCAPI_LOGGER_TRACE("this->queue.size() = "+size_tToString(this->queue.size())); | ||
this->queue.push_back(t); | ||
} else { | ||
CCAPI_LOGGER_FATAL(EXCEPTION_QUEUE_FULL); | ||
} | ||
} | ||
T popBack() { | ||
std::lock_guard<std::mutex> lock(this->m); | ||
if (this->queue.empty()) { | ||
CCAPI_LOGGER_FATAL(EXCEPTION_QUEUE_EMPTY); | ||
} else { | ||
T t = std::move(this->queue.back()); | ||
this->queue.pop_back(); | ||
return t; | ||
} | ||
} | ||
std::vector<T> purge() { | ||
std::lock_guard<std::mutex> lock(this->m); | ||
std::vector<T> p; | ||
std::swap(p, this->queue); | ||
return p; | ||
} | ||
size_t size() const { | ||
std::lock_guard<std::mutex> lock(this->m); | ||
return this->queue.size(); | ||
} | ||
bool empty() const { | ||
std::lock_guard<std::mutex> lock(this->m); | ||
return this->queue.empty(); | ||
} | ||
|
||
private: | ||
std::vector<T> queue; | ||
mutable std::mutex m; | ||
size_t maxSize{}; | ||
}; | ||
} /* namespace ccapi */ | ||
#endif // INCLUDE_CCAPI_CPP_CCAPI_QUEUE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters