From a1ef364247a2195ffdadb13ba1c6030a990bfc46 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 24 Apr 2013 06:09:19 -0400 Subject: [PATCH] Changed to use c++11 features. --- httplib.h | 8 +++-- test/Makefile | 8 ++--- test/test.cc | 97 ++++++++------------------------------------------- 3 files changed, 24 insertions(+), 89 deletions(-) diff --git a/httplib.h b/httplib.h index 7686600..c0e61d8 100644 --- a/httplib.h +++ b/httplib.h @@ -31,6 +31,7 @@ typedef SOCKET socket_t; #include #include #include +#include #include #include #include @@ -40,6 +41,7 @@ typedef int socket_t; #include #include +#include #include #include #include @@ -263,7 +265,7 @@ inline int get_header_value_int(const MultiMap& map, const char* key, int def) { auto it = map.find(key); if (it != map.end()) { - return std::atoi(it->second.c_str()); + return std::stoi(it->second); } return def; } @@ -741,11 +743,11 @@ inline bool Client::read_response_line(FILE* fp, Response& res) return false; } - static std::regex re("HTTP/1\\.[01] (\\d+?) .+\r\n"); + const static std::regex re("HTTP/1\\.[01] (\\d+?) .+\r\n"); std::cmatch m; if (std::regex_match(buf, m, re)) { - res.status = std::atoi(std::string(m[1]).c_str()); + res.status = std::stoi(std::string(m[1])); } return true; diff --git a/test/Makefile b/test/Makefile index 4c2c838..7f6c4ea 100644 --- a/test/Makefile +++ b/test/Makefile @@ -3,14 +3,14 @@ USE_CLANG = 1 ifdef USE_CLANG CC = clang++ -CFLAGS = -std=c++0x -stdlib=libc++ -g -DGTEST_USE_OWN_TR1_TUPLE +CCFLAGS = -std=c++0x -stdlib=libc++ -g -DGTEST_USE_OWN_TR1_TUPLE else -CC = g++ -CFLAGS = -std=c++11 -g +CC = g++-4.7 +CCFLAGS = -std=c++11 -g endif all : test ./test test : test.cc ../httplib.h - $(CC) -o test $(CFLAGS) -I.. -I. test.cc gtest/gtest-all.cc gtest/gtest_main.cc + $(CC) -o test $(CCFLAGS) -I.. -I. test.cc gtest/gtest-all.cc gtest/gtest_main.cc diff --git a/test/test.cc b/test/test.cc index d22dd17..7fe89fe 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1,7 +1,7 @@ #include #include -//#include +#include #include #ifdef _WIN32 @@ -17,70 +17,6 @@ using namespace httplib; const char* HOST = "localhost"; const int PORT = 1234; -class thread -{ -public: - thread(std::function fn); - ~thread(); - - void join(); - -private: - thread(); - -#ifdef _WIN32 - HANDLE thread_; - static unsigned int __stdcall TreadFunc(void* arg); -#else - pthread_t thread_; - static void* TreadFunc(void* arg); -#endif - - static std::map> tasks_; -}; - -std::map> thread::tasks_; - -inline thread::thread(std::function fn) - : thread_(NULL) -{ - tasks_[this] = fn; -#ifdef _WIN32 - thread_ = (HANDLE)_beginthreadex(NULL, 0, TreadFunc, this, 0, NULL); -#else - pthread_create(&thread_, NULL, TreadFunc, this); -#endif -} - -inline thread::~thread() -{ -#ifdef _WIN32 - CloseHandle(thread_); -#endif -} - -inline void thread::join() -{ -#ifdef _WIN32 - ::WaitForSingleObject(thread_, INFINITE); -#else - pthread_join(thread_, NULL); -#endif -} - -#ifdef _WIN32 -unsigned int __stdcall thread::TreadFunc(void* arg) -#else -void* thread::TreadFunc(void* arg) -#endif -{ - thread* pThis = static_cast(arg); - tasks_[pThis](); - tasks_.erase(pThis); - - return 0; -} - #ifdef _WIN32 TEST(StartupTest, WSAStartup) { @@ -137,7 +73,7 @@ TEST(GetHeaderValueTest, DefaultValue) { //MultiMap map = {{"Dummy","Dummy"}}; MultiMap map; - map.insert(std::make_pair("Dummy", "Dummy")); + map.insert(make_pair("Dummy", "Dummy")); auto val = detail::get_header_value_text(map, "Content-Type", "text/plain"); ASSERT_STREQ("text/plain", val); } @@ -146,7 +82,7 @@ TEST(GetHeaderValueTest, DefaultValueInt) { //MultiMap map = {{"Dummy","Dummy"}}; MultiMap map; - map.insert(std::make_pair("Dummy", "Dummy")); + map.insert(make_pair("Dummy", "Dummy")); auto val = detail::get_header_value_int(map, "Content-Length", 100); EXPECT_EQ(100, val); } @@ -155,8 +91,8 @@ TEST(GetHeaderValueTest, RegularValue) { //MultiMap map = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; MultiMap map; - map.insert(std::make_pair("Content-Type","text/html")); - map.insert(std::make_pair("Dummy", "Dummy")); + map.insert(make_pair("Content-Type","text/html")); + map.insert(make_pair("Dummy", "Dummy")); auto val = detail::get_header_value_text(map, "Content-Type", "text/plain"); ASSERT_STREQ("text/html", val); } @@ -165,8 +101,8 @@ TEST(GetHeaderValueTest, RegularValueInt) { //MultiMap map = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; MultiMap map; - map.insert(std::make_pair("Content-Length", "100")); - map.insert(std::make_pair("Dummy", "Dummy")); + map.insert(make_pair("Content-Length", "100")); + map.insert(make_pair("Dummy", "Dummy")); auto val = detail::get_header_value_int(map, "Content-Length", 0); EXPECT_EQ(100, val); } @@ -194,7 +130,7 @@ protected: }); svr_.get("/person/(.*)", [&](const Request& req, Response& res) { - std::string name = req.matches[1]; + string name = req.matches[1]; if (persons_.find(name) != persons_.end()) { auto note = persons_[name]; res.set_content(note, "text/plain"); @@ -209,8 +145,7 @@ protected: persons_["john"] = "programmer"; - //f_ = async([&](){ svr_.listen(HOST, PORT); }); - t_ = std::make_shared([&](){ + f_ = async([&](){ up_ = true; svr_.listen(HOST, PORT); }); @@ -224,16 +159,14 @@ protected: //svr_.stop(); // NOTE: This causes dead lock on Windows. cli_.get("/stop"); - //f_.get(); - t_->join(); + f_.get(); } - std::map persons_; - Server svr_; - Client cli_; - //std::future f_; - std::shared_ptr t_; - bool up_; + map persons_; + Server svr_; + Client cli_; + future f_; + bool up_; }; TEST_F(ServerTest, GetMethod200)