1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

Fixed all of the warnings from serial_listener

This commit is contained in:
William Woodall 2012-01-13 10:09:49 -06:00
parent 0046f3f61f
commit a870d49b10
2 changed files with 39 additions and 38 deletions

View File

@ -159,11 +159,11 @@ class Filter
{ {
public: public:
Filter (ComparatorType comparator, DataCallback callback) Filter (ComparatorType comparator, DataCallback callback)
: comparator(comparator), callback(callback) {} : comparator_(comparator), callback_(callback) {}
virtual ~Filter () {} virtual ~Filter () {}
ComparatorType comparator; ComparatorType comparator_;
DataCallback callback; DataCallback callback_;
}; };
/*! /*!
@ -206,14 +206,14 @@ typedef boost::shared_ptr<BufferedFilter> BufferedFilterPtr;
* \param e_what is a std::string that describes the cause of the error. * \param e_what is a std::string that describes the cause of the error.
*/ */
class SerialListenerException : public std::exception { class SerialListenerException : public std::exception {
const std::string e_what; const std::string e_what_;
public: public:
SerialListenerException(const std::string e_what) : e_what(e_what) {} SerialListenerException(const std::string e_what) : e_what_(e_what) {}
~SerialListenerException() throw() {std::exception::~exception();} ~SerialListenerException() throw() {std::exception::~exception();}
virtual const char* what() const throw() { virtual const char* what() const throw() {
std::stringstream ss; std::stringstream ss;
ss << "SerialListenerException: " << this->e_what; ss << "SerialListenerException: " << this->e_what_;
return ss.str().c_str(); return ss.str().c_str();
} }
}; };
@ -250,7 +250,7 @@ public:
return true; return true;
} }
bool timed_wait_and_pop(Data& popped_value, size_t timeout) { bool timed_wait_and_pop(Data& popped_value, long timeout) {
using namespace boost::posix_time; using namespace boost::posix_time;
bool result; bool result;
boost::mutex::scoped_lock lock(the_mutex); boost::mutex::scoped_lock lock(the_mutex);
@ -334,7 +334,7 @@ public:
*/ */
void void
setChunkSize (size_t chunk_size) { setChunkSize (size_t chunk_size) {
this->chunk_size = chunk_size; this->chunk_size_ = chunk_size;
} }
/***** Start and Stop Listening ******/ /***** Start and Stop Listening ******/
@ -585,10 +585,11 @@ public:
/*! /*!
* Sleeps for a given number of milliseconds. * Sleeps for a given number of milliseconds.
* *
* \param ms number of milliseconds to sleep. * \param milliseconds number of milliseconds to sleep.
*/ */
static void static void
sleep (size_t ms) { sleep (long milliseconds) {
boost::int64_t ms(milliseconds);
boost::this_thread::sleep(boost::posix_time::milliseconds(ms)); boost::this_thread::sleep(boost::posix_time::milliseconds(ms));
} }
@ -787,10 +788,11 @@ private:
// Persistent listening variables // Persistent listening variables
bool listening; bool listening;
serial::Serial * serial_port; char serial_port_padding[7];
serial::Serial * serial_port_;
boost::thread listen_thread; boost::thread listen_thread;
std::string data_buffer; std::string data_buffer;
size_t chunk_size; size_t chunk_size_;
// Callback related variables // Callback related variables
// filter id, token // filter id, token
@ -821,13 +823,13 @@ class BlockingFilter
{ {
public: public:
BlockingFilter (ComparatorType comparator, SerialListener &listener) { BlockingFilter (ComparatorType comparator, SerialListener &listener) {
this->listener = &listener; this->listener_ = &listener;
DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1); DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1);
this->filter_ptr = this->listener->createFilter(comparator, cb); this->filter_ptr = this->listener_->createFilter(comparator, cb);
} }
virtual ~BlockingFilter () { virtual ~BlockingFilter () {
this->listener->removeFilter(filter_ptr); this->listener_->removeFilter(filter_ptr);
this->result = ""; this->result = "";
this->cond.notify_all(); this->cond.notify_all();
} }
@ -840,7 +842,7 @@ public:
* *
* \return std::string token that was matched or "" if none were matched. * \return std::string token that was matched or "" if none were matched.
*/ */
std::string wait(size_t ms) { std::string wait(long ms) {
this->result = ""; this->result = "";
boost::unique_lock<boost::mutex> lock(this->mutex); boost::unique_lock<boost::mutex> lock(this->mutex);
this->cond.timed_wait(lock, boost::posix_time::milliseconds(ms)); this->cond.timed_wait(lock, boost::posix_time::milliseconds(ms));
@ -855,7 +857,7 @@ public:
} }
private: private:
SerialListener * listener; SerialListener * listener_;
boost::condition_variable cond; boost::condition_variable cond;
boost::mutex mutex; boost::mutex mutex;
std::string result; std::string result;
@ -883,15 +885,15 @@ class BufferedFilter
public: public:
BufferedFilter (ComparatorType comparator, size_t buffer_size, BufferedFilter (ComparatorType comparator, size_t buffer_size,
SerialListener &listener) SerialListener &listener)
: buffer_size(buffer_size) : buffer_size_(buffer_size)
{ {
this->listener = &listener; this->listener_ = &listener;
DataCallback cb = boost::bind(&BufferedFilter::callback, this, _1); DataCallback cb = boost::bind(&BufferedFilter::callback, this, _1);
this->filter_ptr = this->listener->createFilter(comparator, cb); this->filter_ptr = this->listener_->createFilter(comparator, cb);
} }
virtual ~BufferedFilter () { virtual ~BufferedFilter () {
this->listener->removeFilter(filter_ptr); this->listener_->removeFilter(filter_ptr);
this->queue.clear(); this->queue.clear();
this->result = ""; this->result = "";
} }
@ -907,7 +909,7 @@ public:
* *
* \return std::string token that was matched or "" if none were matched. * \return std::string token that was matched or "" if none were matched.
*/ */
std::string wait(size_t ms) { std::string wait(long ms) {
if (ms == 0) if (ms == 0)
if (!this->queue.try_pop(this->result)) if (!this->queue.try_pop(this->result))
this->result = ""; this->result = "";
@ -935,21 +937,21 @@ public:
* Returns the capacity of the buffer. * Returns the capacity of the buffer.
*/ */
size_t capacity() { size_t capacity() {
return buffer_size; return buffer_size_;
} }
FilterPtr filter_ptr; FilterPtr filter_ptr;
void callback(const std::string &token) { void callback(const std::string &token) {
std::string throw_away; std::string throw_away;
if (this->queue.size() == buffer_size) if (this->queue.size() == this->buffer_size_)
this->queue.wait_and_pop(throw_away); this->queue.wait_and_pop(throw_away);
this->queue.push(token); this->queue.push(token);
} }
private: private:
size_t buffer_size; size_t buffer_size_;
SerialListener * listener; SerialListener * listener_;
ConcurrentQueue<std::string> queue; ConcurrentQueue<std::string> queue;
std::string result; std::string result;

View File

@ -17,11 +17,10 @@ inline void defaultInfoCallback(const std::string& msg) {
inline void defaultExceptionCallback(const std::exception &error) { inline void defaultExceptionCallback(const std::exception &error) {
std::cerr << "SerialListener Unhandled Exception: " << error.what(); std::cerr << "SerialListener Unhandled Exception: " << error.what();
std::cerr << std::endl; std::cerr << std::endl;
throw(error);
} }
inline bool defaultComparator(const std::string &token) { inline bool defaultComparator(const std::string &token) {
return true; return token == token;
} }
using namespace serial; using namespace serial;
@ -34,7 +33,7 @@ SerialListener::default_handler(const std::string &token) {
this->_default_handler(token); this->_default_handler(token);
} }
SerialListener::SerialListener() : listening(false), chunk_size(5) { SerialListener::SerialListener() : listening(false), chunk_size_(5) {
// Set default callbacks // Set default callbacks
this->handle_exc = defaultExceptionCallback; this->handle_exc = defaultExceptionCallback;
this->info = defaultInfoCallback; this->info = defaultInfoCallback;
@ -68,7 +67,7 @@ SerialListener::callback() {
std::cout << (*pair.second) << std::endl; std::cout << (*pair.second) << std::endl;
if (this->listening) { if (this->listening) {
try { try {
pair.first->callback((*pair.second)); pair.first->callback_((*pair.second));
} catch (std::exception &e) { } catch (std::exception &e) {
this->handle_exc(e); this->handle_exc(e);
}// try callback }// try callback
@ -88,8 +87,8 @@ SerialListener::startListening(Serial &serial_port) {
} }
this->listening = true; this->listening = true;
this->serial_port = &serial_port; this->serial_port_ = &serial_port;
if (!this->serial_port->isOpen()) { if (!this->serial_port_->isOpen()) {
throw(SerialListenerException("Serial port not open.")); throw(SerialListenerException("Serial port not open."));
return; return;
} }
@ -110,7 +109,7 @@ SerialListener::stopListening() {
callback_thread.join(); callback_thread.join();
this->data_buffer = ""; this->data_buffer = "";
this->serial_port = NULL; this->serial_port_ = NULL;
// Delete all the filters // Delete all the filters
this->removeAllFilters(); this->removeAllFilters();
@ -121,20 +120,20 @@ SerialListener::determineAmountToRead() {
// TODO: Make a more intelligent method based on the length of the things // TODO: Make a more intelligent method based on the length of the things
// filters are looking for. e.g.: if the filter is looking for 'V=XX\r' // filters are looking for. e.g.: if the filter is looking for 'V=XX\r'
// make the read amount at least 5. // make the read amount at least 5.
return this->chunk_size; return this->chunk_size_;
} }
void void
SerialListener::readSomeData(std::string &temp, size_t this_many) { SerialListener::readSomeData(std::string &temp, size_t this_many) {
// Make sure there is a serial port // Make sure there is a serial port
if (this->serial_port == NULL) { if (this->serial_port_ == NULL) {
this->handle_exc(SerialListenerException("Invalid serial port.")); this->handle_exc(SerialListenerException("Invalid serial port."));
} }
// Make sure the serial port is open // Make sure the serial port is open
if (!this->serial_port->isOpen()) { if (!this->serial_port_->isOpen()) {
this->handle_exc(SerialListenerException("Serial port not open.")); this->handle_exc(SerialListenerException("Serial port not open."));
} }
temp = this->serial_port->read(this_many); temp = this->serial_port_->read(this_many);
} }
void void
@ -159,7 +158,7 @@ SerialListener::filter (FilterPtr filter, std::vector<TokenPtr> &tokens)
if (it == tokens.end()-1) if (it == tokens.end()-1)
continue; continue;
TokenPtr token = (*it); TokenPtr token = (*it);
if (filter->comparator((*token))) if (filter->comparator_((*token)))
callback_queue.push(std::make_pair(filter,token)); callback_queue.push(std::make_pair(filter,token));
} }
} }