diff --git a/include/serial.h b/include/serial.h index 8e25b04..d574089 100644 --- a/include/serial.h +++ b/include/serial.h @@ -47,6 +47,12 @@ #include #include +// A macro to disallow the copy constructor and operator= functions +// This should be used in the private: declarations for a class +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName&); \ + void operator=(const TypeName&) + // DEFINES #define DEFAULT_BAUDRATE 9600 #define DEFAULT_TIMEOUT 0.0 @@ -132,7 +138,7 @@ public: * * @return An integer representing the number of bytes read. */ - int read(char* buffer, int size = 1); + const int read(char* buffer, int size = 1); /** Read size bytes from the serial port. * If a timeout is set it may return less characters than requested. With no timeout @@ -142,7 +148,7 @@ public: * * @return A std::string containing the data read. */ - std::string read(int size = 1); + const std::string read(int size = 1); /** Write length bytes from buffer to the serial port. * @@ -152,7 +158,7 @@ public: * * @return An integer representing the number of bytes written. */ - int write(char data[], int length); + const int write(char data[], int length); /** Write a string to the serial port. * @@ -160,7 +166,7 @@ public: * * @return An integer representing the number of bytes written to the serial port. */ - int write(std::string data); + const int write(std::string data); /** Sets the logic level of the RTS line. * @@ -178,13 +184,13 @@ public: * * @return A boolean value that represents the current logic level of the CTS line. */ - bool getCTS(); + const bool getCTS(); /** Gets the status of the DSR line. * * @return A boolean value that represents the current logic level of the DSR line. */ - bool getDSR(); + const bool getDSR(); /** Sets the timeout for reads in seconds. * @@ -206,7 +212,7 @@ public: * zero (-1) will result in infinite blocking behaviour, i.e. the serial port will * block until either size bytes have been read or an exception has occured. */ - long getTimeoutMilliseconds(); + const long getTimeoutMilliseconds(); /** Sets the baudrate for the serial port. * @@ -218,7 +224,7 @@ public: * * @return An integer that sets the baud rate for the serial port. */ - int getBaudrate(); + const int getBaudrate(); /** Sets the bytesize for the serial port. * @@ -238,7 +244,7 @@ public: * * @throw InvalidBytesizeException */ - bytesize_t getBytesize(); + const bytesize_t getBytesize(); /** Sets the parity for the serial port. * @@ -256,7 +262,7 @@ public: * * @throw InvalidParityException */ - parity_t getParity(); + const parity_t getParity(); /** Sets the stopbits for the serial port. * @@ -274,7 +280,7 @@ public: * * @throw InvalidStopbitsException */ - stopbits_t getStopbits(); + const stopbits_t getStopbits(); /** Sets the flow control for the serial port. * @@ -292,8 +298,9 @@ public: * * @throw InvalidFlowcontrolException */ - flowcontrol_t getFlowcontrol(); + const flowcontrol_t getFlowcontrol(); private: + DISALLOW_COPY_AND_ASSIGN(Serial); void init(); void read_complete(const boost::system::error_code& error, std::size_t bytes_transferred); void timeout_callback(const boost::system::error_code& error); diff --git a/src/serial.cpp b/src/serial.cpp index e2b0732..72b89a9 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -189,7 +189,7 @@ void Serial::close() { this->serial_port->close(); } -int Serial::read(char* buffer, int size) { +const int Serial::read(char* buffer, int size) { this->reading = true; if(this->nonblocking) // Do not wait for data boost::asio::async_read(*this->serial_port, boost::asio::buffer(buffer, size), @@ -215,7 +215,7 @@ int Serial::read(char* buffer, int size) { return this->bytes_read; } -std::string Serial::read(int size) { +const std::string Serial::read(int size) { char *serial_buffer = new char[size]; int bytes_read_ = this->read(serial_buffer, size); std::string return_str(serial_buffer, (std::size_t)bytes_read_); @@ -240,11 +240,11 @@ void Serial::timeout_callback(const boost::system::error_code& error) { } } -int Serial::write(char data[], int length) { +const int Serial::write(char data[], int length) { return boost::asio::write(*this->serial_port, boost::asio::buffer(data, length), boost::asio::transfer_all()); } -int Serial::write(std::string data) { +const int Serial::write(std::string data) { char *cstr = new char[data.size()+1]; std::strcpy(cstr, data.c_str()); int bytes_wrote = this->write(cstr, data.length()); @@ -260,12 +260,12 @@ void Serial::setDTR(bool level) { this->serial_port->set_option(DTRControl(level)); } -bool Serial::getCTS() { +const bool Serial::getCTS() { throw(boost::asio::error::operation_not_supported); return false; } -bool Serial::getDSR() { +const bool Serial::getDSR() { throw(boost::asio::error::operation_not_supported); return false; } @@ -286,7 +286,7 @@ void Serial::setTimeoutMilliseconds(long timeout) { this->nonblocking = false; } -long Serial::getTimeoutMilliseconds() { +const long Serial::getTimeoutMilliseconds() { return this->timeout->total_milliseconds(); } @@ -294,7 +294,7 @@ void Serial::setBaudrate(int baudrate) { this->baudrate = boost::asio::serial_port_base::baud_rate(baudrate); } -int Serial::getBaudrate() { +const int Serial::getBaudrate() { return this->baudrate.value(); } @@ -318,7 +318,7 @@ void Serial::setBytesize(bytesize_t bytesize) { } } -bytesize_t Serial::getBytesize() { +const bytesize_t Serial::getBytesize() { return bytesize_t(this->bytesize.value()); } @@ -339,7 +339,7 @@ void Serial::setParity(parity_t parity) { } } -parity_t Serial::getParity() { +const parity_t Serial::getParity() { switch(this->parity.value()) { case boost::asio::serial_port_base::parity::none: return parity_t(PARITY_NONE); @@ -369,7 +369,7 @@ void Serial::setStopbits(stopbits_t stopbits) { } } -stopbits_t Serial::getStopbits() { +const stopbits_t Serial::getStopbits() { switch(this->stopbits.value()) { case boost::asio::serial_port_base::stop_bits::one: return stopbits_t(STOPBITS_ONE); @@ -399,7 +399,7 @@ void Serial::setFlowcontrol(flowcontrol_t flowcontrol) { } } -flowcontrol_t Serial::getFlowcontrol() { +const flowcontrol_t Serial::getFlowcontrol() { switch(this->flowcontrol.value()) { case boost::asio::serial_port_base::flow_control::none: return flowcontrol_t(FLOWCONTROL_NONE);