From 50bbd2d9460fd0c290e2c5b4774db4489994fc30 Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Wed, 13 Jun 2018 17:30:40 +0800 Subject: [PATCH] modify definitation of any --- examples/static_link_example/main.cc | 1 + lib/inc/drogon/HttpAppFramework.h | 1 + lib/inc/drogon/HttpViewData.h | 16 +++++++-------- lib/inc/drogon/Session.h | 18 ++++++++--------- lib/src/HttpAppFramework.cc | 26 ++++++++++++++++++++---- lib/src/HttpServer.cc | 2 +- lib/src/SharedLibManager.cc | 30 ++++++++++++++++++++++++++++ lib/src/SharedLibManager.h | 29 +++++++++++++++++++++++++++ trantor | 2 +- 9 files changed, 102 insertions(+), 23 deletions(-) create mode 100755 lib/src/SharedLibManager.cc create mode 100755 lib/src/SharedLibManager.h diff --git a/examples/static_link_example/main.cc b/examples/static_link_example/main.cc index c67ed525..463482dc 100755 --- a/examples/static_link_example/main.cc +++ b/examples/static_link_example/main.cc @@ -113,6 +113,7 @@ int main() drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle4/{4}/{3}/{1}",func); //start app framework + //drogon::HttpAppFramework::instance().enableDynamicSharedLibLoading(); drogon::HttpAppFramework::instance().run(); } diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 84099297..5cd63fc0 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -79,5 +79,6 @@ namespace drogon virtual const std::string & getDocumentRoot() const =0; virtual void setDocumentRoot(const std::string &rootPath)=0; virtual void setFileTypes(const std::vector &types)=0; + virtual void enableDynamicSharedLibLoading(const std::string &viewPth="views")=0; }; } diff --git a/lib/inc/drogon/HttpViewData.h b/lib/inc/drogon/HttpViewData.h index f52b7e7a..6775b523 100755 --- a/lib/inc/drogon/HttpViewData.h +++ b/lib/inc/drogon/HttpViewData.h @@ -18,14 +18,14 @@ #ifdef USE_STD_ANY #include -typedef std::any Any; -#define Any_cast std::any_cast +using std::any; +using std::any_cast; #elif USE_BOOST #include -typedef boost::any Any; -#define Any_cast boost::any_cast +using boost::any; +using boost::any_cast; #else #error,must use c++17 or boost @@ -35,7 +35,7 @@ typedef boost::any Any; #include #include -typedef std::unordered_map ViewDataMap; +typedef std::unordered_map ViewDataMap; namespace drogon { class HttpViewData @@ -48,7 +48,7 @@ public: if(it!=viewData_.end()) { try { - return Any_cast(it->second); + return any_cast(it->second); } catch (std::exception& e) { @@ -58,11 +58,11 @@ public: T tmp; return tmp; } - void insert(const std::string& key,Any &&obj) + void insert(const std::string& key,any &&obj) { viewData_[key]=std::move(obj); } - void insert(const std::string& key,const Any &obj) + void insert(const std::string& key,const any &obj) { viewData_[key]=obj; } diff --git a/lib/inc/drogon/Session.h b/lib/inc/drogon/Session.h index 260616b1..f87f4dc5 100755 --- a/lib/inc/drogon/Session.h +++ b/lib/inc/drogon/Session.h @@ -21,20 +21,20 @@ #ifdef USE_STD_ANY #include -typedef std::any Any; -#define Any_cast std::any_cast +using std::any; +using std::any_cast; #elif USE_BOOST #include -typedef boost::any Any; -#define Any_cast boost::any_cast +using boost::any; +using boost::any_cast; #else #error,must use c++17 or boost #endif -typedef std::map SessionMap; +typedef std::map SessionMap; namespace drogon { class Session @@ -45,21 +45,21 @@ namespace drogon auto it=sessionMap_.find(key); if(it!=sessionMap_.end()) { - return Any_cast(it->second); + return any_cast(it->second); } T tmp; return tmp; }; - Any &operator[](const std::string &key){ + any &operator[](const std::string &key){ std::lock_guard lck(mutex_); return sessionMap_[key]; }; - void insert(const std::string& key,const Any &obj) + void insert(const std::string& key,const any &obj) { std::lock_guard lck(mutex_); sessionMap_[key]=obj; }; - void insert(const std::string& key,Any &&obj) + void insert(const std::string& key,any &&obj) { std::lock_guard lck(mutex_); sessionMap_[key]=std::move(obj); diff --git a/lib/src/HttpAppFramework.cc b/lib/src/HttpAppFramework.cc index 675ee4e9..f4a9d0c2 100755 --- a/lib/src/HttpAppFramework.cc +++ b/lib/src/HttpAppFramework.cc @@ -11,6 +11,7 @@ * @section DESCRIPTION * */ +#include "SharedLibManager.h" #include "Utilities.h" #include "HttpRequestImpl.h" #include "HttpResponseImpl.h" @@ -51,6 +52,7 @@ namespace drogon virtual const std::string & getDocumentRoot() const override {return _rootPath;} virtual void setDocumentRoot(const std::string &rootPath) override {_rootPath=rootPath;} virtual void setFileTypes(const std::vector &types) override; + virtual void enableDynamicSharedLibLoading(const std::string &viewPth="views") override; ~HttpAppFrameworkImpl(){} private: std::vector> _listeners; @@ -100,7 +102,7 @@ namespace drogon std::set _fileTypeSet={"html","jpg"}; std::string _rootPath="."; - + std::atomic_bool _running=false; //tool funcs @@ -109,11 +111,25 @@ namespace drogon size_t _threadNum=1; + std::string _viewFilePath; + + std::unique_ptr_sharedLibManagerPtr; + + trantor::EventLoop _loop; }; } using namespace drogon; using namespace std::placeholders; +void HttpAppFrameworkImpl::enableDynamicSharedLibLoading(const std::string &viewPath) +{ + assert(!_running); + if(_viewFilePath.empty()) + { + _viewFilePath=_rootPath+"/"+viewPath; + _sharedLibManagerPtr=std::unique_ptr(new SharedLibManager(&_loop,_viewFilePath)); + } +} void HttpAppFrameworkImpl::setFileTypes(const std::vector &types) { for(auto type : types) @@ -226,12 +242,14 @@ void HttpAppFrameworkImpl::setThreadNum(size_t threadNum) } void HttpAppFrameworkImpl::addListener(const std::string &ip, uint16_t port) { + assert(!_running); _listeners.push_back(std::make_pair(ip,port)); } void HttpAppFrameworkImpl::run() { // + _running=true; std::vector> servers; std::vector> loopThreads; initRegex(); @@ -263,9 +281,9 @@ void HttpAppFrameworkImpl::run() interval=_sessionTimeout/1000; limit=_sessionTimeout; } - trantor::EventLoop loop; - _sessionMapPtr=std::unique_ptr>(new CacheMap(&loop,interval,limit)); - loop.loop(); + + _sessionMapPtr=std::unique_ptr>(new CacheMap(&_loop,interval,limit)); + _loop.loop(); } bool HttpAppFrameworkImpl::doFilters(const std::vector &filters, diff --git a/lib/src/HttpServer.cc b/lib/src/HttpServer.cc index e45e5967..b012f0a0 100755 --- a/lib/src/HttpServer.cc +++ b/lib/src/HttpServer.cc @@ -83,7 +83,7 @@ void HttpServer::onConnection(const TcpConnectionPtr& conn) void HttpServer::onMessage(const TcpConnectionPtr& conn, MsgBuffer* buf) { - HttpContext* context = Any_cast(conn->getMutableContext()); + HttpContext* context = any_cast(conn->getMutableContext()); // LOG_INFO << "###:" << string(buf->peek(), buf->readableBytes()); if (!context->parseRequest(buf)) { diff --git a/lib/src/SharedLibManager.cc b/lib/src/SharedLibManager.cc new file mode 100755 index 00000000..1b7aff78 --- /dev/null +++ b/lib/src/SharedLibManager.cc @@ -0,0 +1,30 @@ +/** + * + * @file + * @author An Tao + * @section LICENSE + * + * Copyright 2018, An Tao. All rights reserved. + * Use of this source code is governed by a MIT license + * that can be found in the License file. + * + * @section DESCRIPTION + * + */ + +#include "SharedLibManager.h" +#include + +using namespace drogon; +SharedLibManager::SharedLibManager(trantor::EventLoop *loop,const std::string viewPath): +_loop(loop), +_viewPath(viewPath) +{ + _loop->runEvery(5.0,[=](){ + managerLibs(); + }); +} +void SharedLibManager::managerLibs() +{ + //LOG_DEBUG<<"manager .so libs in path "<<_viewPath; +} \ No newline at end of file diff --git a/lib/src/SharedLibManager.h b/lib/src/SharedLibManager.h new file mode 100755 index 00000000..a598a163 --- /dev/null +++ b/lib/src/SharedLibManager.h @@ -0,0 +1,29 @@ +/** + * + * @file + * @author An Tao + * @section LICENSE + * + * Copyright 2018, An Tao. All rights reserved. + * Use of this source code is governed by a MIT license + * that can be found in the License file. + * + * @section DESCRIPTION + * + */ +#pragma once + +#include +#include +namespace drogon{ + class SharedLibManager:public trantor::NonCopyable + { + public: + SharedLibManager(trantor::EventLoop *loop,const std::string viewPath); + ~SharedLibManager(){} + private: + void managerLibs(); + trantor::EventLoop *_loop; + std::string _viewPath; + }; +} \ No newline at end of file diff --git a/trantor b/trantor index 8cafcd2a..3028e451 160000 --- a/trantor +++ b/trantor @@ -1 +1 @@ -Subproject commit 8cafcd2ab9720ac21f4cd0af9b3af3ea79385d3b +Subproject commit 3028e451c7d81a2e7301d7013a2166003f9cabe1