Skip to content

Commit 006582a

Browse files
committed
updated to c++14 and cleanup
1 parent 36687d2 commit 006582a

File tree

13 files changed

+90
-96
lines changed

13 files changed

+90
-96
lines changed

CMakeLists.txt

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,58 @@
11
cmake_minimum_required(VERSION 2.8.1)
22
cmake_policy(SET CMP0015 NEW)
33

4-
include_directories(${CSI_INCLUDE_PATH} ${CMAKE_SOURCE_DIR} ${Boost_INCLUDE_DIRS})
4+
option(BUILD_SAMPLES "build examples" ON)
5+
6+
set(CMAKE_CXX_STANDARD 14)
7+
8+
find_package(Boost COMPONENTS log_setup log thread program_options filesystem system REQUIRED)
9+
set(BOOST_LIBS
10+
${Boost_LOG_LIBRARY}
11+
${Boost_LOG_SETUP_LIBRARY}
12+
${Boost_PROGRAM_OPTIONS_LIBRARY}
13+
${Boost_THREAD_LIBRARY}
14+
${Boost_SYSTEM_LIBRARY}
15+
pthread
16+
rt
17+
c
18+
)
519

620
if(WIN32)
7-
if (NOT DEFINED PGSQLSRC)
8-
SET (PGSQLSRC "C:/Program Files/PostgreSQL/9.5")
9-
endif()
10-
include_directories(${PGSQLSRC}/include)
11-
link_directories(${PGSQLSRC}/lib ${Boost_LIBRARY_DIRS}/$(Platform)/lib)
12-
set(EXT_LIBS postgres_asio libpq libeay32 ssleay32 Ws2_32)
13-
else()
14-
link_directories(${Boost_LIBRARY_DIRS})
15-
set(EXT_LIBS postgres_asio boost_log boost_thread boost_system pthread pq)
16-
endif()
21+
if (NOT DEFINED PGSQLSRC)
22+
SET(PGSQLSRC "C:/Program Files/PostgreSQL/9.5")
23+
endif ()
24+
include_directories(${PGSQLSRC}/include ${CMAKE_SOURCE_DIR} ${Boost_INCLUDE_DIR})
25+
link_directories(${PGSQLSRC}/lib ${Boost_LIBRARY_DIRS}/$(Platform)/lib)
26+
set(EXT_LIBS postgres_asio libpq libeay32 ssleay32 Ws2_32)
27+
else ()
28+
SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
29+
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
30+
include_directories(${CMAKE_SOURCE_DIR} ${Boost_INCLUDE_DIR})
31+
link_directories(${Boost_LIBRARY_DIRS})
32+
set(EXT_LIBS postgres_asio pq ${BOOST_LIBS})
1733

18-
add_subdirectory(postgres_asio)
34+
add_definitions(-D_FILE_OFFSET_BITS=64 -D_REENTRANT -DEXTERNAL_LOCKS -DMULTITHREAD)
35+
add_definitions(-fPIC)
36+
add_definitions(-DBOOST_LOG_DYN_LINK)
1937

20-
if(__BUILD_EXAMPLES__)
21-
add_subdirectory(samples)
2238
endif()
2339

40+
SET(LIB_SRCS
41+
postgres_asio/postgres_asio.h
42+
postgres_asio/postgres_asio.cpp
43+
)
44+
45+
add_library(postgres_asio STATIC ${LIB_SRCS})
46+
47+
add_executable(cursor_sample samples/cursor_sample.cpp)
48+
target_link_libraries(cursor_sample ${EXT_LIBS})
49+
50+
add_executable(insert_sample samples/insert_sample.cpp)
51+
target_link_libraries(insert_sample ${EXT_LIBS})
52+
53+
add_executable(query_sample samples/query_sample.cpp)
54+
target_link_libraries(query_sample ${EXT_LIBS})
55+
56+
2457

2558

build_linux.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

postgres_asio/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

postgres_asio/postgres_asio.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <future>
2-
#include <boost/bind.hpp>
32
#include "postgres_asio.h"
3+
#include <boost/bind.hpp>
44
#include <boost/log/core.hpp>
55
#include <boost/log/trivial.hpp>
66
#include <boost/log/expressions.hpp>
@@ -30,8 +30,10 @@ namespace postgres_asio {
3030
_pg_conn(NULL),
3131
_warn_timeout(60000),
3232
_trace_id(trace_id) {
33-
if(!_trace_id.size())
34-
_trace_id = to_string(boost::uuids::random_generator()());
33+
if (!_trace_id.size()) {
34+
auto uuid = boost::uuids::random_generator();
35+
_trace_id = to_string(uuid());
36+
}
3537
BOOST_LOG_TRIVIAL(trace) << _trace_id << ", " << BOOST_CURRENT_FUNCTION;
3638
}
3739

postgres_asio/postgres_asio.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
#pragma once
22
#include <memory>
33
#include <utility>
4+
#include <functional>
5+
#include <deque>
46
#include <boost/asio.hpp>
5-
#include <boost/function.hpp>
67
#include <boost/chrono/system_clocks.hpp>
7-
#include <deque>
88

99
#ifdef WIN32
1010
#include <libpq-fe.h>
11-
#endif
12-
13-
#ifdef __LINUX__
14-
#ifdef __CENTOS__
15-
#include <libpq-fe.h>
1611
#else
1712
#include <postgresql/libpq-fe.h>
1813
#endif
19-
#endif
2014

2115
//inspiration
2216
//https://github.com/brianc/node-libpq
2317

2418
namespace postgres_asio {
2519
class connection : public std::enable_shared_from_this<connection> {
2620
public:
27-
typedef boost::function<void(int ec)> on_connect_callback;
28-
typedef boost::function<void(int ec, std::shared_ptr<PGresult>)> on_query_callback;
21+
typedef std::function<void(int ec)> on_connect_callback;
22+
typedef std::function<void(int ec, std::shared_ptr<PGresult>)> on_query_callback;
2923

3024
connection(boost::asio::io_service& fg, boost::asio::io_service& bg, std::string trace_id = "");
3125
~connection();

rebuild.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
rm -rf bin
4+
rm -rf lib
5+
rm -rf build
6+
mkdir build
7+
cd build
8+
cmake -DCMAKE_BUILD_TYPE=Release ..
9+
make -j8
10+
cd ..
11+

samples/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

samples/cursor_sample/cursor_sample.cpp renamed to samples/cursor_sample.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#include <iostream>
2+
#include <memory>
3+
#include <thread>
24
#include <postgres_asio/postgres_asio.h>
3-
#include <boost/thread.hpp>
4-
#include <boost/bind.hpp>
5-
#include <boost/make_shared.hpp>
65
#include <boost/log/core.hpp>
76
#include <boost/log/trivial.hpp>
87
#include <boost/log/expressions.hpp>
98

10-
119
void handle_fetch1000(std::shared_ptr<postgres_asio::connection> connection, size_t total_count, int ec, std::shared_ptr<PGresult> res) {
1210
if(ec)
1311
return;
@@ -37,10 +35,13 @@ int main(int argc, char *argv[]) {
3735

3836
boost::asio::io_service fg_ios;
3937
boost::asio::io_service bg_ios;
40-
std::auto_ptr<boost::asio::io_service::work> fg_work(new boost::asio::io_service::work(fg_ios)); // this keeps the fg_ios alive
41-
std::auto_ptr<boost::asio::io_service::work> bg_work(new boost::asio::io_service::work(bg_ios)); // this keeps the bg_ios alive
42-
boost::thread fg(boost::bind(&boost::asio::io_service::run, &fg_ios));
43-
boost::thread bg(boost::bind(&boost::asio::io_service::run, &bg_ios));
38+
auto fg_work(std::make_unique<boost::asio::io_service::work>(fg_ios)); // this keeps the fg_ios alive
39+
auto bg_work(std::make_unique<boost::asio::io_service::work>(bg_ios)); // this keeps the bg_ios alive
40+
std::thread fg([&] { fg_ios.run(); });
41+
std::thread bg([&] { bg_ios.run(); });
42+
43+
//boost::thread fg(boost::bind(&boost::asio::io_service::run, &fg_ios));
44+
//boost::thread bg(boost::bind(&boost::asio::io_service::run, &bg_ios));
4445

4546
auto connection = std::make_shared<postgres_asio::connection>(fg_ios, bg_ios);
4647
connection->set_warning_timout(100);

samples/cursor_sample/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

samples/insert_sample/insert_sample.cpp renamed to samples/insert_sample.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <iostream>
2+
#include <thread>
23
#include <postgres_asio/postgres_asio.h>
3-
#include <boost/thread.hpp>
4-
#include <boost/bind.hpp>
5-
#include <boost/make_shared.hpp>
64
#include <boost/log/core.hpp>
75
#include <boost/log/trivial.hpp>
86
#include <boost/log/expressions.hpp>
@@ -21,10 +19,10 @@ int main(int argc, char *argv[]) {
2119

2220
boost::asio::io_service fg_ios;
2321
boost::asio::io_service bg_ios;
24-
std::auto_ptr<boost::asio::io_service::work> fg_work(new boost::asio::io_service::work(fg_ios)); // this keeps the fg_ios alive
25-
std::auto_ptr<boost::asio::io_service::work> bg_work(new boost::asio::io_service::work(bg_ios)); // this keeps the bg_ios alive
26-
boost::thread fg(boost::bind(&boost::asio::io_service::run, &fg_ios));
27-
boost::thread bg(boost::bind(&boost::asio::io_service::run, &bg_ios));
22+
auto fg_work(std::make_unique<boost::asio::io_service::work>(fg_ios)); // this keeps the fg_ios alive
23+
auto bg_work(std::make_unique<boost::asio::io_service::work>(bg_ios)); // this keeps the bg_ios alive
24+
std::thread fg([&] { fg_ios.run(); });
25+
std::thread bg([&] { bg_ios.run(); });
2826

2927
// precondition CREATE TABLE postgres_asio_sample ( id integer, val text )
3028

@@ -35,9 +33,10 @@ int main(int argc, char *argv[]) {
3533
if(!ec) {
3634
std::string statement = "insert into postgres_asio_sample (id, val) VALUES\n";
3735
size_t items = 100;
38-
for(int i = 0; i != items; ++i) {
39-
const char* ch = i < (items - 1) ? ",\n" : ";\n";
40-
std::string val = to_string(boost::uuids::random_generator()());
36+
for (int j = 0; j != items; ++j) {
37+
const char *ch = j < (items - 1) ? ",\n" : ";\n";
38+
auto uuid = boost::uuids::random_generator();
39+
std::string val = to_string(uuid());
4140
statement += " (" + std::to_string(i) + ", '" + val + "')" + ch;
4241
}
4342

samples/insert_sample/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

samples/query_sample/query_sample.cpp renamed to samples/query_sample.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <iostream>
2+
#include <thread>
23
#include <postgres_asio/postgres_asio.h>
3-
#include <boost/thread.hpp>
4-
#include <boost/bind.hpp>
5-
#include <boost/make_shared.hpp>
64
#include <boost/log/core.hpp>
75
#include <boost/log/trivial.hpp>
86
#include <boost/log/expressions.hpp>
@@ -17,10 +15,10 @@ int main(int argc, char *argv[]) {
1715

1816
boost::asio::io_service fg_ios;
1917
boost::asio::io_service bg_ios;
20-
std::auto_ptr<boost::asio::io_service::work> fg_work(new boost::asio::io_service::work(fg_ios)); // this keeps the fg_ios alive
21-
std::auto_ptr<boost::asio::io_service::work> bg_work(new boost::asio::io_service::work(bg_ios)); // this keeps the bg_ios alive
22-
boost::thread fg(boost::bind(&boost::asio::io_service::run, &fg_ios));
23-
boost::thread bg(boost::bind(&boost::asio::io_service::run, &bg_ios));
18+
auto fg_work(std::make_unique<boost::asio::io_service::work>(fg_ios)); // this keeps the fg_ios alive
19+
auto bg_work(std::make_unique<boost::asio::io_service::work>(bg_ios)); // this keeps the bg_ios alive
20+
std::thread fg([&] { fg_ios.run(); });
21+
std::thread bg([&] { bg_ios.run(); });
2422

2523
auto connection = std::make_shared<postgres_asio::connection>(fg_ios, bg_ios);
2624
connection->connect(connect_string, [connection](int ec) {

samples/query_sample/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)