From b192790ddfafdfc6a55240ede2b84a1b14126b0a Mon Sep 17 00:00:00 2001 From: antao Date: Fri, 5 Oct 2018 22:43:51 +0800 Subject: [PATCH] Add some methods in class HttpViewData --- lib/inc/drogon/HttpViewData.h | 108 ++++++++++++++++++++++--------- lib/inc/drogon/utils/Utilities.h | 1 + tests/CMakeLists.txt | 1 + tests/HttpViewDataTest.cc | 11 ++++ 4 files changed, 90 insertions(+), 31 deletions(-) create mode 100644 tests/HttpViewDataTest.cc diff --git a/lib/inc/drogon/HttpViewData.h b/lib/inc/drogon/HttpViewData.h index 31ea465d..4866c22e 100755 --- a/lib/inc/drogon/HttpViewData.h +++ b/lib/inc/drogon/HttpViewData.h @@ -16,47 +16,93 @@ #include #include - +#include #include #include +#include typedef std::unordered_map ViewDataMap; namespace drogon { -class HttpViewData -{ -public: - template - const T get(const std::string &key) const + class HttpViewData { - auto it=viewData_.find(key); - if(it!=viewData_.end()) + public: + template + const T get(const std::string &key,T && nullVal=T()) const { - try { - return any_cast(it->second); - } - catch (std::exception& e) + auto it=viewData_.find(key); + if(it!=viewData_.end()) { - LOG_ERROR << e.what(); + try { + return any_cast(it->second); + } + catch (std::exception& e) + { + LOG_ERROR << e.what(); + } } + return nullVal; } - T tmp; - return tmp; - } - void insert(const std::string& key,any &&obj) - { - viewData_[key]=std::move(obj); - } - void insert(const std::string& key,const any &obj) - { - viewData_[key]=obj; - } - any& operator [] (const std::string &key) const - { - return viewData_[key]; - } -protected: - mutable ViewDataMap viewData_; -}; + void insert(const std::string& key,any &&obj) + { + viewData_[key]=std::move(obj); + } + void insert(const std::string& key,const any &obj) + { + viewData_[key]=obj; + } + template + void insertAsString(const std::string &key,T && val) + { + std::stringstream ss; + ss<= 0) && (result < strBuffer.size())) { + strBuffer.resize(result); + } + else + { + // Repeatedly increase buffer size until it fits + while (true) { + if (result < 0) { + // Older snprintf() behavior. :-( Just try doubling the buffer size + strBuffer.resize(strBuffer.size()*2); + } else { + // We need exactly "result+1" characters + strBuffer.resize(result+1); + } + + va_copy(backup_ap, ap); + auto result=vsnprintf((char *)strBuffer.data(), strBuffer.size(), format, backup_ap); + va_end(backup_ap); + + if ((result >= 0) && (result < strBuffer.size())) { + strBuffer.resize(result); + break; + } + } + } + va_end(ap); + viewData_[key]=strBuffer; + } + + any& operator [] (const std::string &key) const + { + return viewData_[key]; + } + protected: + mutable ViewDataMap viewData_; + }; } diff --git a/lib/inc/drogon/utils/Utilities.h b/lib/inc/drogon/utils/Utilities.h index a5aac9f8..f394f8f0 100755 --- a/lib/inc/drogon/utils/Utilities.h +++ b/lib/inc/drogon/utils/Utilities.h @@ -34,6 +34,7 @@ namespace drogon{ char *data, size_t *ndata); std::string getHttpFullDate(const trantor::Date &date); + } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 19f22dd1..0af83452 100755 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,4 +6,5 @@ add_executable(cache_map_test CacheMapTest.cc) add_executable(cookies_test CookiesTest.cc) add_executable(class_name_test ClassNameTest.cc) add_executable(sha1_test Sha1Test.cc) +add_executable(view_data_test HttpViewDataTest.cc) diff --git a/tests/HttpViewDataTest.cc b/tests/HttpViewDataTest.cc new file mode 100644 index 00000000..634834f2 --- /dev/null +++ b/tests/HttpViewDataTest.cc @@ -0,0 +1,11 @@ +#include +#include +int main() +{ + drogon::HttpViewData data; + std::cout<<(data.insert("1",1),data.get("1"))<("2"))<("3"))<("4"))<