Skip to content

Commit

Permalink
Added C++11 support, replaced boost libs with the std equivalent when…
Browse files Browse the repository at this point in the history
… possible/convenient
  • Loading branch information
fedetft committed Aug 11, 2018
1 parent 7cc1f4b commit 291a799
Show file tree
Hide file tree
Showing 31 changed files with 144 additions and 169 deletions.
5 changes: 3 additions & 2 deletions 1_simple/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 3.1)
project(TEST)

## Target
set(CMAKE_CXX_STANDARD 11)
set(TEST_SRCS main.cpp)
add_executable(simple ${TEST_SRCS})

## Link libraries
set(BOOST_LIBS thread date_time system)
set(BOOST_LIBS date_time system)
find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED)
target_link_libraries(simple ${Boost_LIBRARIES})
find_package(Threads REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion 1_simple/Makefile.windows
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Using MinGW distro from http://nuwen.net/mingw.html that contains boost precompiled

all:
g++ -O2 -c main.cpp -D_WIN32_WINNT=0x0501
g++ -O2 -std=c++11 -c main.cpp -D_WIN32_WINNT=0x0501
g++ -o simple.exe main.o -s -lwsock32 -lws2_32 -lboost_system

clean:
Expand Down
5 changes: 3 additions & 2 deletions 2_with_timeout/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 3.1)
project(TEST)

## Target
set(CMAKE_CXX_STANDARD 11)
set(TEST_SRCS main.cpp TimeoutSerial.cpp)
add_executable(timeout ${TEST_SRCS})

## Link libraries
set(BOOST_LIBS thread date_time system)
set(BOOST_LIBS date_time system)
find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED)
target_link_libraries(timeout ${Boost_LIBRARIES})
find_package(Threads REQUIRED)
Expand Down
4 changes: 2 additions & 2 deletions 2_with_timeout/Makefile.windows
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#Using MinGW distro from http://nuwen.net/mingw.html that contains boost precompiled

all:
g++ -O2 -c main.cpp -D_WIN32_WINNT=0x0501
g++ -O2 -c TimeoutSerial.cpp -D_WIN32_WINNT=0x0501
g++ -O2 -std=c++11 -c main.cpp -D_WIN32_WINNT=0x0501
g++ -O2 -std=c++11 -c TimeoutSerial.cpp -D_WIN32_WINNT=0x0501
g++ -o timeout.exe main.o TimeoutSerial.o -s -lwsock32 -lws2_32 -lboost_system

clean:
Expand Down
16 changes: 9 additions & 7 deletions 2_with_timeout/TimeoutSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Distributed under the Boost Software License, Version 1.0.
* Created on September 12, 2009, 3:47 PM
*
* v1.06: C++11 support
*
* v1.05: Fixed a bug regarding reading after a timeout (again).
*
* v1.04: Fixed bug with timeout set to zero
Expand All @@ -27,14 +29,14 @@ using namespace std;
using namespace boost;

TimeoutSerial::TimeoutSerial(): io(), port(io), timer(io),
timeout(posix_time::seconds(0)) {}
timeout(boost::posix_time::seconds(0)) {}

TimeoutSerial::TimeoutSerial(const std::string& devname, unsigned int baud_rate,
asio::serial_port_base::parity opt_parity,
asio::serial_port_base::character_size opt_csize,
asio::serial_port_base::flow_control opt_flow,
asio::serial_port_base::stop_bits opt_stop)
: io(), port(io), timer(io), timeout(posix_time::seconds(0))
: io(), port(io), timer(io), timeout(boost::posix_time::seconds(0))
{
open(devname,baud_rate,opt_parity,opt_csize,opt_flow,opt_stop);
}
Expand Down Expand Up @@ -65,7 +67,7 @@ void TimeoutSerial::close()
port.close();
}

void TimeoutSerial::setTimeout(const posix_time::time_duration& t)
void TimeoutSerial::setTimeout(const boost::posix_time::time_duration& t)
{
timeout=t;
}
Expand Down Expand Up @@ -102,8 +104,8 @@ void TimeoutSerial::read(char *data, size_t size)

//For this code to work, there should always be a timeout, so the
//request for no timeout is translated into a very long timeout
if(timeout!=posix_time::seconds(0)) timer.expires_from_now(timeout);
else timer.expires_from_now(posix_time::hours(100000));
if(timeout!=boost::posix_time::seconds(0)) timer.expires_from_now(timeout);
else timer.expires_from_now(boost::posix_time::hours(100000));

timer.async_wait(boost::bind(&TimeoutSerial::timeoutExpired,this,
asio::placeholders::error));
Expand Down Expand Up @@ -155,8 +157,8 @@ std::string TimeoutSerial::readStringUntil(const std::string& delim)

//For this code to work, there should always be a timeout, so the
//request for no timeout is translated into a very long timeout
if(timeout!=posix_time::seconds(0)) timer.expires_from_now(timeout);
else timer.expires_from_now(posix_time::hours(100000));
if(timeout!=boost::posix_time::seconds(0)) timer.expires_from_now(timeout);
else timer.expires_from_now(boost::posix_time::hours(100000));

timer.async_wait(boost::bind(&TimeoutSerial::timeoutExpired,this,
asio::placeholders::error));
Expand Down
6 changes: 3 additions & 3 deletions 2_with_timeout/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ int main(int argc, char* argv[])
cout<<serial.readStringUntil("\r\n")<<endl;

//Binary test
char values[]={0xde,0xad,0xbe,0xef};
serial.write(values,sizeof(values));
serial.read(values,sizeof(values));
unsigned char values[]={0xde,0xad,0xbe,0xef};
serial.write(reinterpret_cast<char*>(values),sizeof(values));
serial.read(reinterpret_cast<char*>(values),sizeof(values));
for(unsigned int i=0;i<sizeof(values);i++)
{
cout<<static_cast<int>(values[i])<<endl;
Expand Down
30 changes: 17 additions & 13 deletions 3_async/AsyncSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Distributed under the Boost Software License, Version 1.0.
* Created on September 7, 2009, 10:46 AM
*
* v1.03: C++11 support
*
* v1.02: Fixed a bug in BufferedAsyncSerial: Using the default constructor
* the callback was not set up and reading didn't work.
*
Expand All @@ -29,8 +31,10 @@

#include <string>
#include <algorithm>
#include <iostream>
#include <thread>
#include <mutex>
#include <boost/bind.hpp>
#include <boost/shared_array.hpp>

using namespace std;
using namespace boost;
Expand All @@ -49,20 +53,20 @@ class AsyncSerialImpl: private boost::noncopyable

boost::asio::io_service io; ///< Io service object
boost::asio::serial_port port; ///< Serial port object
boost::thread backgroundThread; ///< Thread that runs read/write operations
std::thread backgroundThread; ///< Thread that runs read/write operations
bool open; ///< True if port open
bool error; ///< Error flag
mutable boost::mutex errorMutex; ///< Mutex for access to error
mutable std::mutex errorMutex; ///< Mutex for access to error

/// Data are queued here before they go in writeBuffer
std::vector<char> writeQueue;
boost::shared_array<char> writeBuffer; ///< Data being written
size_t writeBufferSize; ///< Size of writeBuffer
boost::mutex writeQueueMutex; ///< Mutex for access to writeQueue
std::mutex writeQueueMutex; ///< Mutex for access to writeQueue
char readBuffer[AsyncSerial::readBufferSize]; ///< data being read

/// Read complete callback
boost::function<void (const char*, size_t)> callback;
std::function<void (const char*, size_t)> callback;
};

AsyncSerial::AsyncSerial(): pimpl(new AsyncSerialImpl)
Expand Down Expand Up @@ -267,14 +271,15 @@ void AsyncSerial::setErrorStatus(bool e)
pimpl->error=e;
}

void AsyncSerial::setReadCallback(const boost::function<void (const char*, size_t)>& callback)
void AsyncSerial::setReadCallback(const std::function<void (const char*, size_t)>& callback)
{
pimpl->callback=callback;
}

void AsyncSerial::clearReadCallback()
{
pimpl->callback.clear();
std::function<void (const char*, size_t)> empty;
pimpl->callback.swap(empty);
}

#else //__APPLE__
Expand All @@ -300,7 +305,7 @@ class AsyncSerialImpl: private boost::noncopyable
char readBuffer[AsyncSerial::readBufferSize]; ///< data being read

/// Read complete callback
boost::function<void (const char*, size_t)> callback;
std::function<void (const char*, size_t)> callback;
};

AsyncSerial::AsyncSerial(): pimpl(new AsyncSerialImpl)
Expand Down Expand Up @@ -510,15 +515,15 @@ void AsyncSerial::setErrorStatus(bool e)
pimpl->error=e;
}

void AsyncSerial::setReadCallback(const
function<void (const char*, size_t)>& callback)
void AsyncSerial::setReadCallback(const std::function<void (const char*, size_t)>& callback)
{
pimpl->callback=callback;
}

void AsyncSerial::clearReadCallback()
{
pimpl->callback.clear();
std::function<void (const char*, size_t)> empty;
pimpl->callback.swap(empty);
}

#endif //__APPLE__
Expand All @@ -543,8 +548,7 @@ CallbackAsyncSerial::CallbackAsyncSerial(const std::string& devname,

}

void CallbackAsyncSerial::setCallback(const
boost::function<void (const char*, size_t)>& callback)
void CallbackAsyncSerial::setCallback(const std::function<void (const char*, size_t)>& callback)
{
setReadCallback(callback);
}
Expand Down
14 changes: 5 additions & 9 deletions 3_async/AsyncSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
#define ASYNCSERIAL_H

#include <vector>
#include <memory>
#include <functional>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/utility.hpp>
#include <boost/function.hpp>
#include <boost/shared_array.hpp>

/**
* Used internally (pimpl)
Expand Down Expand Up @@ -155,7 +153,7 @@ class AsyncSerial: private boost::noncopyable
*/
void doClose();

boost::shared_ptr<AsyncSerialImpl> pimpl;
std::shared_ptr<AsyncSerialImpl> pimpl;

protected:

Expand All @@ -168,8 +166,7 @@ class AsyncSerial: private boost::noncopyable
/**
* To allow derived classes to set a read callback
*/
void setReadCallback(const
boost::function<void (const char*, size_t)>& callback);
void setReadCallback(const std::function<void (const char*, size_t)>& callback);

/**
* To unregister the read callback in the derived class destructor so it
Expand Down Expand Up @@ -220,8 +217,7 @@ class CallbackAsyncSerial: public AsyncSerial
* serial port.
* \param callback the receive callback
*/
void setCallback(const
boost::function<void (const char*, size_t)>& callback);
void setCallback(const std::function<void (const char*, size_t)>& callback);

/**
* Removes the callback. Any data received after this function call will
Expand Down
7 changes: 3 additions & 4 deletions 3_async/BufferedAsyncSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

#include <string>
#include <algorithm>
#include <iostream>
#include <boost/bind.hpp>

using namespace std;
using namespace std::placeholders;
using namespace boost;

//
Expand All @@ -21,7 +20,7 @@ using namespace boost;

BufferedAsyncSerial::BufferedAsyncSerial(): AsyncSerial()
{
setReadCallback(boost::bind(&BufferedAsyncSerial::readCallback, this, _1, _2));
setReadCallback(std::bind(&BufferedAsyncSerial::readCallback, this, _1, _2));
}

BufferedAsyncSerial::BufferedAsyncSerial(const std::string& devname,
Expand All @@ -32,7 +31,7 @@ BufferedAsyncSerial::BufferedAsyncSerial(const std::string& devname,
asio::serial_port_base::stop_bits opt_stop)
:AsyncSerial(devname,baud_rate,opt_parity,opt_csize,opt_flow,opt_stop)
{
setReadCallback(boost::bind(&BufferedAsyncSerial::readCallback, this, _1, _2));
setReadCallback(std::bind(&BufferedAsyncSerial::readCallback, this, _1, _2));
}

size_t BufferedAsyncSerial::read(char *data, size_t size)
Expand Down
3 changes: 2 additions & 1 deletion 3_async/BufferedAsyncSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "AsyncSerial.h"
#include <mutex>

#ifndef BUFFEREDASYNCSERIAL_H
#define BUFFEREDASYNCSERIAL_H
Expand Down Expand Up @@ -93,7 +94,7 @@ class BufferedAsyncSerial: public AsyncSerial
const std::string& s);

std::vector<char> readQueue;
boost::mutex readQueueMutex;
std::mutex readQueueMutex;
};

#endif //BUFFEREDASYNCSERIAL_H
5 changes: 3 additions & 2 deletions 3_async/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 3.1)
project(TEST)

## Target
set(CMAKE_CXX_STANDARD 11)
set(TEST_SRCS main.cpp AsyncSerial.cpp BufferedAsyncSerial.cpp)
add_executable(async ${TEST_SRCS})

## Link libraries
set(BOOST_LIBS thread date_time system)
set(BOOST_LIBS date_time system)
find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED)
target_link_libraries(async ${Boost_LIBRARIES})
find_package(Threads REQUIRED)
Expand Down
6 changes: 3 additions & 3 deletions 3_async/Makefile.windows
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#Using MinGW distro from http://nuwen.net/mingw.html that contains boost precompiled

all:
g++ -O2 -c main.cpp -D_WIN32_WINNT=0x0501
g++ -O2 -c AsyncSerial.cpp -D_WIN32_WINNT=0x0501
g++ -O2 -c BufferedAsyncSerial.cpp -D_WIN32_WINNT=0x0501
g++ -O2 -std=c++11 -c main.cpp -D_WIN32_WINNT=0x0501
g++ -O2 -std=c++11 -c AsyncSerial.cpp -D_WIN32_WINNT=0x0501
g++ -O2 -std=c++11 -c BufferedAsyncSerial.cpp -D_WIN32_WINNT=0x0501
g++ -o async.exe main.o AsyncSerial.o BufferedAsyncSerial.o -s -lwsock32 -lws2_32 -lboost_system -lboost_thread

clean:
Expand Down
5 changes: 2 additions & 3 deletions 3_async/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

#include <iostream>
#include <boost/thread.hpp>
#include <thread>

#include "BufferedAsyncSerial.h"

using namespace std;
using namespace boost;

int main(int argc, char* argv[])
{
Expand All @@ -19,7 +18,7 @@ int main(int argc, char* argv[])
//Simulate doing something else while the serial device replies.
//When the serial device replies, the second thread stores the received
//data in a buffer.
this_thread::sleep(posix_time::seconds(2));
this_thread::sleep_for(chrono::seconds(2));

//Always returns immediately. If the terminator \r\n has not yet
//arrived, returns an empty string.
Expand Down
Loading

0 comments on commit 291a799

Please sign in to comment.