1
0
mirror of synced 2025-04-20 11:47:43 +03:00

Changed to use c++11 features.

This commit is contained in:
yhirose 2013-04-24 06:09:19 -04:00
parent e7e8f5e70e
commit a1ef364247
3 changed files with 24 additions and 89 deletions

View File

@ -31,6 +31,7 @@ typedef SOCKET socket_t;
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <netdb.h> #include <netdb.h>
#include <cstring>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -40,6 +41,7 @@ typedef int socket_t;
#include <functional> #include <functional>
#include <map> #include <map>
#include <memory>
#include <regex> #include <regex>
#include <string> #include <string>
#include <assert.h> #include <assert.h>
@ -263,7 +265,7 @@ inline int get_header_value_int(const MultiMap& map, const char* key, int def)
{ {
auto it = map.find(key); auto it = map.find(key);
if (it != map.end()) { if (it != map.end()) {
return std::atoi(it->second.c_str()); return std::stoi(it->second);
} }
return def; return def;
} }
@ -741,11 +743,11 @@ inline bool Client::read_response_line(FILE* fp, Response& res)
return false; 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; std::cmatch m;
if (std::regex_match(buf, m, re)) { 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; return true;

View File

@ -3,14 +3,14 @@ USE_CLANG = 1
ifdef USE_CLANG ifdef USE_CLANG
CC = 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 else
CC = g++ CC = g++-4.7
CFLAGS = -std=c++11 -g CCFLAGS = -std=c++11 -g
endif endif
all : test all : test
./test ./test
test : test.cc ../httplib.h 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

View File

@ -1,7 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <httplib.h> #include <httplib.h>
//#include <future> #include <future>
#include <iostream> #include <iostream>
#ifdef _WIN32 #ifdef _WIN32
@ -17,70 +17,6 @@ using namespace httplib;
const char* HOST = "localhost"; const char* HOST = "localhost";
const int PORT = 1234; const int PORT = 1234;
class thread
{
public:
thread(std::function<void ()> 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<void*, std::function<void ()>> tasks_;
};
std::map<void*, std::function<void ()>> thread::tasks_;
inline thread::thread(std::function<void ()> 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<thread*>(arg);
tasks_[pThis]();
tasks_.erase(pThis);
return 0;
}
#ifdef _WIN32 #ifdef _WIN32
TEST(StartupTest, WSAStartup) TEST(StartupTest, WSAStartup)
{ {
@ -137,7 +73,7 @@ TEST(GetHeaderValueTest, DefaultValue)
{ {
//MultiMap map = {{"Dummy","Dummy"}}; //MultiMap map = {{"Dummy","Dummy"}};
MultiMap map; 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"); auto val = detail::get_header_value_text(map, "Content-Type", "text/plain");
ASSERT_STREQ("text/plain", val); ASSERT_STREQ("text/plain", val);
} }
@ -146,7 +82,7 @@ TEST(GetHeaderValueTest, DefaultValueInt)
{ {
//MultiMap map = {{"Dummy","Dummy"}}; //MultiMap map = {{"Dummy","Dummy"}};
MultiMap map; 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); auto val = detail::get_header_value_int(map, "Content-Length", 100);
EXPECT_EQ(100, val); EXPECT_EQ(100, val);
} }
@ -155,8 +91,8 @@ TEST(GetHeaderValueTest, RegularValue)
{ {
//MultiMap map = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; //MultiMap map = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
MultiMap map; MultiMap map;
map.insert(std::make_pair("Content-Type","text/html")); map.insert(make_pair("Content-Type","text/html"));
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"); auto val = detail::get_header_value_text(map, "Content-Type", "text/plain");
ASSERT_STREQ("text/html", val); ASSERT_STREQ("text/html", val);
} }
@ -165,8 +101,8 @@ TEST(GetHeaderValueTest, RegularValueInt)
{ {
//MultiMap map = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; //MultiMap map = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
MultiMap map; MultiMap map;
map.insert(std::make_pair("Content-Length", "100")); map.insert(make_pair("Content-Length", "100"));
map.insert(std::make_pair("Dummy", "Dummy")); map.insert(make_pair("Dummy", "Dummy"));
auto val = detail::get_header_value_int(map, "Content-Length", 0); auto val = detail::get_header_value_int(map, "Content-Length", 0);
EXPECT_EQ(100, val); EXPECT_EQ(100, val);
} }
@ -194,7 +130,7 @@ protected:
}); });
svr_.get("/person/(.*)", [&](const Request& req, Response& res) { 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()) { if (persons_.find(name) != persons_.end()) {
auto note = persons_[name]; auto note = persons_[name];
res.set_content(note, "text/plain"); res.set_content(note, "text/plain");
@ -209,8 +145,7 @@ protected:
persons_["john"] = "programmer"; persons_["john"] = "programmer";
//f_ = async([&](){ svr_.listen(HOST, PORT); }); f_ = async([&](){
t_ = std::make_shared<thread>([&](){
up_ = true; up_ = true;
svr_.listen(HOST, PORT); svr_.listen(HOST, PORT);
}); });
@ -224,15 +159,13 @@ protected:
//svr_.stop(); // NOTE: This causes dead lock on Windows. //svr_.stop(); // NOTE: This causes dead lock on Windows.
cli_.get("/stop"); cli_.get("/stop");
//f_.get(); f_.get();
t_->join();
} }
std::map<std::string, std::string> persons_; map<string, string> persons_;
Server svr_; Server svr_;
Client cli_; Client cli_;
//std::future<void> f_; future<void> f_;
std::shared_ptr<thread> t_;
bool up_; bool up_;
}; };