Skip to content

Commit b5e9464

Browse files
author
Cynthia Coan
committed
use lambda over ptr_fun
ptr_fun was deprecated in C++11, and removed in C++17. lambdas have also been around since C++11. so there should be no loss in supported targets, but also adds in support for C++17 and beyond.
1 parent 847eaf7 commit b5e9464

File tree

4 files changed

+104
-41
lines changed

4 files changed

+104
-41
lines changed

CMakeLists.txt

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,36 @@ find_package(Threads REQUIRED)
3939
find_package(CURL REQUIRED)
4040
find_package(jsoncpp)
4141

42-
add_library(restclient-cpp SHARED
43-
source/restclient.cc
44-
source/connection.cc
42+
add_library(restclient-cpp SHARED
43+
source/restclient.cc
44+
source/connection.cc
4545
source/helpers.cc
4646
)
47-
set_property(TARGET restclient-cpp PROPERTY SOVERSION 2.1.1)
47+
set_property(TARGET restclient-cpp PROPERTY SOVERSION 2.1.1)
4848

4949
target_compile_features(restclient-cpp PUBLIC cxx_std_11)
5050

51-
list(APPEND restclient-cpp_PUBLIC_HEADERS
52-
include/restclient-cpp/restclient.h
53-
"${CMAKE_CURRENT_BINARY_DIR}/include/restclient-cpp/version.h"
54-
include/restclient-cpp/connection.h
51+
list(APPEND restclient-cpp_PUBLIC_HEADERS
52+
include/restclient-cpp/restclient.h
53+
"${CMAKE_CURRENT_BINARY_DIR}/include/restclient-cpp/version.h"
54+
include/restclient-cpp/connection.h
5555
include/restclient-cpp/helpers.h
5656
)
5757
# target_sources(restclient-cpp PRIVATE ${restclient-cpp_PUBLIC_HEADERS})
58-
set_property(TARGET restclient-cpp PROPERTY
58+
set_property(TARGET restclient-cpp PROPERTY
5959
PUBLIC_HEADER ${restclient-cpp_PUBLIC_HEADERS})
6060
target_include_directories(restclient-cpp PRIVATE include)
61-
62-
if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/version.h.in")
63-
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/version.h.in" [=[
64-
#ifndef INCLUDE_RESTCLIENT_CPP_VERSION_H_
65-
#define INCLUDE_RESTCLIENT_CPP_VERSION_H_
66-
#define RESTCLIENT_VERSION "@restclient-cpp_VERSION@"
67-
#endif // INCLUDE_RESTCLIENT_CPP_VERSION_H_
68-
]=])
69-
endif()
70-
configure_file("${CMAKE_CURRENT_BINARY_DIR}/version.h.in"
71-
"${CMAKE_CURRENT_BINARY_DIR}/include/restclient-cpp/version.h")
61+
62+
if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/version.h.in")
63+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/version.h.in" [=[
64+
#ifndef INCLUDE_RESTCLIENT_CPP_VERSION_H_
65+
#define INCLUDE_RESTCLIENT_CPP_VERSION_H_
66+
#define RESTCLIENT_VERSION "@restclient-cpp_VERSION@"
67+
#endif // INCLUDE_RESTCLIENT_CPP_VERSION_H_
68+
]=])
69+
endif()
70+
configure_file("${CMAKE_CURRENT_BINARY_DIR}/version.h.in"
71+
"${CMAKE_CURRENT_BINARY_DIR}/include/restclient-cpp/version.h")
7272
target_include_directories(restclient-cpp PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include")
7373

7474
target_link_libraries(restclient-cpp
@@ -90,8 +90,8 @@ install(TARGETS restclient-cpp EXPORT restclient-cppTargets
9090
RESOURCE DESTINATION ${DATA_INSTALL_DIR}
9191
)
9292

93-
include(CMakePackageConfigHelpers)
94-
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in"
93+
include(CMakePackageConfigHelpers)
94+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in"
9595
"@PACKAGE_INIT@\ninclude(\${CMAKE_CURRENT_LIST_DIR}/\@PROJECT_NAME\@Targets.cmake)\n")
9696
configure_package_config_file(
9797
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in
@@ -128,15 +128,16 @@ if(Gtest_FOUND AND jsoncpp_FOUND)
128128
enable_testing()
129129

130130

131-
add_executable(test-program
132-
vendor/jsoncpp-0.10.5/dist/jsoncpp.cpp
133-
test/tests.cpp
134-
test/test_restclient.cc
135-
test/test_connection.cc
131+
add_executable(test-program
132+
vendor/jsoncpp-0.10.5/dist/jsoncpp.cpp
133+
test/tests.cpp
134+
test/test_restclient.cc
135+
test/test_connection.cc
136+
test/test_helpers.cc
136137
)
137-
target_include_directories(test-program
138-
PRIVATE include
139-
PRIVATE vendor/jsoncpp-0.10.5/dist
138+
target_include_directories(test-program
139+
PRIVATE include
140+
PRIVATE vendor/jsoncpp-0.10.5/dist
140141
PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include")
141142

142143
target_link_libraries(test-program
@@ -147,14 +148,14 @@ gtest_discover_tests(test-program
147148
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
148149
EXTRA_ARGS -VV
149150
)
150-
151-
endif()
152-
153-
154-
# TODO: Setup ctest here for valgrind and CI
155-
156-
# TODO: Setup cpack here for automatic packaging.
157-
# Note most of the work is already done above due to use of properties and install commands.
151+
152+
endif()
153+
154+
155+
# TODO: Setup ctest here for valgrind and CI
156+
157+
# TODO: Setup cpack here for automatic packaging.
158+
# Note most of the work is already done above due to use of properties and install commands.
158159

159160
include(FeatureSummary)
160161
feature_summary(WHAT ALL)

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ check_PROGRAMS = test-program
44
pkginclude_HEADERS = include/restclient-cpp/restclient.h include/restclient-cpp/version.h include/restclient-cpp/connection.h include/restclient-cpp/helpers.h
55
BUILT_SOURCES = include/restclient-cpp/version.h
66

7-
test_program_SOURCES = vendor/jsoncpp-0.10.5/dist/jsoncpp.cpp test/tests.cpp test/test_restclient.cc test/test_connection.cc
7+
test_program_SOURCES = vendor/jsoncpp-0.10.5/dist/jsoncpp.cpp test/tests.cpp test/test_helpers.cc test/test_restclient.cc test/test_connection.cc
88
test_program_LDADD = .libs/librestclient-cpp.a
99
test_program_LDFLAGS=-Lvendor/gtest-1.7.0/lib/.libs -lgtest
1010
test_program_CPPFLAGS=-Iinclude -Ivendor/gtest-1.7.0/include -Ivendor/jsoncpp-0.10.5/dist

include/restclient-cpp/helpers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ namespace Helpers {
5353
// trim from start
5454
static inline std::string &ltrim(std::string &s) { // NOLINT
5555
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
56-
std::not1(std::ptr_fun<int, int>(std::isspace))));
56+
[](int c) {return !std::isspace(c);}));
5757
return s;
5858
}
5959

6060
// trim from end
6161
static inline std::string &rtrim(std::string &s) { // NOLINT
6262
s.erase(std::find_if(s.rbegin(), s.rend(),
63-
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
63+
[](int c) {return !std::isspace(c);}).base(), s.end());
6464
return s;
6565
}
6666

test/test_helpers.cc

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "restclient-cpp/helpers.h"
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
5+
class HelpersTest : public ::testing::Test
6+
{
7+
protected:
8+
9+
HelpersTest()
10+
{
11+
}
12+
13+
virtual ~HelpersTest()
14+
{
15+
}
16+
17+
virtual void SetUp()
18+
{
19+
}
20+
21+
virtual void TearDown()
22+
{
23+
}
24+
};
25+
26+
TEST_F(HelpersTest, TrimLeft) {
27+
auto can_trim_left = std::string(" a set of characters");
28+
auto cant_trim_left = std::string("a set of characters");
29+
auto right_trim_ignored = std::string("a set of characters ");
30+
31+
EXPECT_EQ(RestClient::Helpers::ltrim(can_trim_left),
32+
std::string("a set of characters"));
33+
EXPECT_EQ(RestClient::Helpers::ltrim(cant_trim_left),
34+
std::string("a set of characters"));
35+
EXPECT_EQ(RestClient::Helpers::ltrim(right_trim_ignored),
36+
std::string("a set of characters "));
37+
}
38+
39+
TEST_F(HelpersTest, TrimRight) {
40+
auto left_trim_ignored = std::string(" a set of characters");
41+
auto cant_trim_right = std::string("a set of characters");
42+
auto can_trim_right = std::string("a set of characters ");
43+
44+
EXPECT_EQ(RestClient::Helpers::rtrim(left_trim_ignored),
45+
std::string(" a set of characters"));
46+
EXPECT_EQ(RestClient::Helpers::rtrim(cant_trim_right),
47+
std::string("a set of characters"));
48+
EXPECT_EQ(RestClient::Helpers::rtrim(can_trim_right),
49+
std::string("a set of characters"));
50+
}
51+
52+
TEST_F(HelpersTest, TrimBoth) {
53+
auto can_trim_both = std::string(" a set of characters ");
54+
auto can_trim_left = std::string(" a set of characters");
55+
auto can_trim_right = std::string("a set of characters ");
56+
auto cant_trim = std::string("a set of characters");
57+
58+
EXPECT_EQ(RestClient::Helpers::trim(can_trim_both), "a set of characters");
59+
EXPECT_EQ(RestClient::Helpers::trim(can_trim_left), "a set of characters");
60+
EXPECT_EQ(RestClient::Helpers::trim(can_trim_right), "a set of characters");
61+
EXPECT_EQ(RestClient::Helpers::trim(cant_trim), "a set of characters");
62+
}

0 commit comments

Comments
 (0)