Change some variable names;Use reference capture instead of value capture

This commit is contained in:
antao 2019-01-24 18:37:32 +08:00
parent a3f03df1d4
commit 5e8c8a6666
4 changed files with 43 additions and 43 deletions

View File

@ -25,8 +25,8 @@ void HttpControllersRouter::init()
for (auto &router : _ctrlVector)
{
std::regex reg("\\(\\[\\^/\\]\\*\\)");
std::string tmp = std::regex_replace(router.pathParameterPattern, reg, "[^/]*");
router._regex = std::regex(router.pathParameterPattern, std::regex_constants::icase);
std::string tmp = std::regex_replace(router._pathParameterPattern, reg, "[^/]*");
router._regex = std::regex(router._pathParameterPattern, std::regex_constants::icase);
regString.append("(").append(tmp).append(")|");
}
if (regString.length() > 0)
@ -90,15 +90,15 @@ void HttpControllersRouter::addHttpPath(const std::string &path,
}
auto pathParameterPattern = std::regex_replace(originPath, regex, "([^/]*)");
auto binderInfo = CtrlBinderPtr(new CtrlBinder);
binderInfo->filtersName = filters;
binderInfo->binderPtr = binder;
binderInfo->parameterPlaces = std::move(places);
binderInfo->queryParametersPlaces = std::move(parametersPlaces);
binderInfo->_filtersName = filters;
binderInfo->_binderPtr = binder;
binderInfo->_parameterPlaces = std::move(places);
binderInfo->_queryParametersPlaces = std::move(parametersPlaces);
{
std::lock_guard<std::mutex> guard(_ctrlMutex);
for (auto &router : _ctrlVector)
{
if (router.pathParameterPattern == pathParameterPattern)
if (router._pathParameterPattern == pathParameterPattern)
{
if (validMethods.size() > 0)
{
@ -117,7 +117,7 @@ void HttpControllersRouter::addHttpPath(const std::string &path,
}
}
struct HttpControllerRouterItem router;
router.pathParameterPattern = pathParameterPattern;
router._pathParameterPattern = pathParameterPattern;
if (validMethods.size() > 0)
{
for (auto const &method : validMethods)
@ -156,7 +156,7 @@ void HttpControllersRouter::route(const HttpRequestImplPtr &req,
{
size_t ctlIndex = i - 1;
auto &routerItem = _ctrlVector[ctlIndex];
//LOG_TRACE << "got http access,regex=" << binder.pathParameterPattern;
//LOG_TRACE << "got http access,regex=" << binder._pathParameterPattern;
assert(Invalid > req->method());
auto &binder = routerItem._binders[req->method()];
if (!binder)
@ -167,12 +167,12 @@ void HttpControllersRouter::route(const HttpRequestImplPtr &req,
callback(res);
return;
}
auto &filters = binder->filtersName;
auto &filters = binder->_filtersName;
if (!filters.empty())
{
auto sessionIdPtr = std::make_shared<std::string>(std::move(sessionId));
auto callbackPtr = std::make_shared<std::function<void(const HttpResponsePtr &)>>(std::move(callback));
_appImpl.doFilters(filters, req, callbackPtr, needSetJsessionid, sessionIdPtr, [=]() {
_appImpl.doFilters(filters, req, callbackPtr, needSetJsessionid, sessionIdPtr, [=, &binder, &routerItem]() {
doControllerHandler(binder, routerItem, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr));
});
}
@ -211,8 +211,8 @@ void HttpControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlBinderP
{
HttpResponsePtr responsePtr;
{
std::lock_guard<std::mutex> guard(*(ctrlBinderPtr->binderMtx));
responsePtr = ctrlBinderPtr->responsePtr;
std::lock_guard<std::mutex> guard(ctrlBinderPtr->_binderMtx);
responsePtr = ctrlBinderPtr->_responsePtr;
}
if (responsePtr && (responsePtr->expiredTime() == 0 || (trantor::Date::now() < responsePtr->createDate().after(responsePtr->expiredTime()))))
@ -232,28 +232,28 @@ void HttpControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlBinderP
}
return;
}
std::vector<std::string> params(ctrlBinderPtr->parameterPlaces.size());
std::vector<std::string> params(ctrlBinderPtr->_parameterPlaces.size());
std::smatch r;
if (std::regex_match(req->path(), r, routerItem._regex))
{
for (size_t j = 1; j < r.size(); j++)
{
size_t place = ctrlBinderPtr->parameterPlaces[j - 1];
size_t place = ctrlBinderPtr->_parameterPlaces[j - 1];
if (place > params.size())
params.resize(place);
params[place - 1] = r[j].str();
LOG_TRACE << "place=" << place << " para:" << params[place - 1];
}
}
if (ctrlBinderPtr->queryParametersPlaces.size() > 0)
if (ctrlBinderPtr->_queryParametersPlaces.size() > 0)
{
auto qureyPara = req->getParameters();
for (auto const &parameter : qureyPara)
{
if (ctrlBinderPtr->queryParametersPlaces.find(parameter.first) !=
ctrlBinderPtr->queryParametersPlaces.end())
if (ctrlBinderPtr->_queryParametersPlaces.find(parameter.first) !=
ctrlBinderPtr->_queryParametersPlaces.end())
{
auto place = ctrlBinderPtr->queryParametersPlaces.find(parameter.first)->second;
auto place = ctrlBinderPtr->_queryParametersPlaces.find(parameter.first)->second;
if (place > params.size())
params.resize(place);
params[place - 1] = parameter.second;
@ -261,12 +261,12 @@ void HttpControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlBinderP
}
}
std::list<std::string> paraList;
for (auto &p : params)///Use reference
for (auto &p : params) ///Use reference
{
LOG_TRACE << p;
paraList.push_back(std::move(p));
}
ctrlBinderPtr->binderPtr->handleHttpRequest(paraList, req, [=, callback = std::move(callback), sessionId = std::move(sessionId)](const HttpResponsePtr &resp) {
ctrlBinderPtr->_binderPtr->handleHttpRequest(paraList, req, [=, callback = std::move(callback), sessionId = std::move(sessionId)](const HttpResponsePtr &resp) {
LOG_TRACE << "http resp:needSetJsessionid=" << needSetJsessionid << ";JSESSIONID=" << sessionId;
auto newResp = resp;
if (resp->expiredTime() >= 0)
@ -274,8 +274,8 @@ void HttpControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlBinderP
//cache the response;
std::dynamic_pointer_cast<HttpResponseImpl>(resp)->makeHeaderString();
{
std::lock_guard<std::mutex> guard(*(ctrlBinderPtr->binderMtx));
ctrlBinderPtr->responsePtr = resp;
std::lock_guard<std::mutex> guard(ctrlBinderPtr->_binderMtx);
ctrlBinderPtr->_responsePtr = resp;
}
}
if (needSetJsessionid)

View File

@ -43,17 +43,17 @@ class HttpControllersRouter : public trantor::NonCopyable
private:
struct CtrlBinder
{
internal::HttpBinderBasePtr binderPtr;
std::vector<std::string> filtersName;
std::vector<size_t> parameterPlaces;
std::map<std::string, size_t> queryParametersPlaces;
std::unique_ptr<std::mutex> binderMtx = std::unique_ptr<std::mutex>(new std::mutex);
std::shared_ptr<HttpResponse> responsePtr;
internal::HttpBinderBasePtr _binderPtr;
std::vector<std::string> _filtersName;
std::vector<size_t> _parameterPlaces;
std::map<std::string, size_t> _queryParametersPlaces;
std::mutex _binderMtx;
std::shared_ptr<HttpResponse> _responsePtr;
};
typedef std::shared_ptr<CtrlBinder> CtrlBinderPtr;
struct HttpControllerRouterItem
{
std::string pathParameterPattern;
std::string _pathParameterPattern;
std::regex _regex;
CtrlBinderPtr _binders[Invalid]; //The enum value Invalid is the http methods number
};

View File

@ -51,8 +51,8 @@ void HttpSimpleControllersRouter::registerHttpSimpleController(const std::string
}
}
auto &item = _simpCtrlMap[path];
item.controllerName = ctrlName;
item.filtersName = filters;
item._controllerName = ctrlName;
item._filtersName = filters;
item._validMethodsFlags.clear(); //There may be old data, first clear
if (validMethods.size() > 0)
{
@ -62,12 +62,12 @@ void HttpSimpleControllersRouter::registerHttpSimpleController(const std::string
item._validMethodsFlags[method] = 1;
}
}
auto controller = item.controller;
auto controller = item._controller;
if (!controller)
{
auto _object = std::shared_ptr<DrObjectBase>(DrClassMap::newObject(ctrlName));
controller = std::dynamic_pointer_cast<HttpSimpleControllerBase>(_object);
item.controller = controller;
item._controller = controller;
}
}
@ -94,7 +94,7 @@ void HttpSimpleControllersRouter::route(const HttpRequestImplPtr &req,
return;
}
}
auto &filters = ctrlInfo.filtersName;
auto &filters = ctrlInfo._filtersName;
if (!filters.empty())
{
auto sessionIdPtr = std::make_shared<std::string>(std::move(sessionId));
@ -118,15 +118,15 @@ void HttpSimpleControllersRouter::doControllerHandler(SimpleControllerRouterItem
bool needSetJsessionid,
std::string &&sessionId)
{
const std::string &ctrlName = item.controllerName;
auto &controller = item.controller;
const std::string &ctrlName = item._controllerName;
auto &controller = item._controller;
if (controller)
{
HttpResponsePtr responsePtr;
{
//maybe update controller,so we use lock_guard to protect;
std::lock_guard<std::mutex> guard(item._mutex);
responsePtr = item.responsePtr;
responsePtr = item._responsePtr;
}
if (responsePtr && (responsePtr->expiredTime() == 0 || (trantor::Date::now() < responsePtr->createDate().after(responsePtr->expiredTime()))))
{
@ -154,7 +154,7 @@ void HttpSimpleControllersRouter::doControllerHandler(SimpleControllerRouterItem
std::dynamic_pointer_cast<HttpResponseImpl>(resp)->makeHeaderString();
{
std::lock_guard<std::mutex> guard(item._mutex);
item.responsePtr = resp;
item._responsePtr = resp;
}
}
if (needSetJsessionid)

View File

@ -47,11 +47,11 @@ class HttpSimpleControllersRouter : public trantor::NonCopyable
HttpControllersRouter &_httpCtrlsRouter;
struct SimpleControllerRouterItem
{
std::string controllerName;
std::vector<std::string> filtersName;
std::string _controllerName;
std::vector<std::string> _filtersName;
std::vector<int> _validMethodsFlags;
std::shared_ptr<HttpSimpleControllerBase> controller;
std::shared_ptr<HttpResponse> responsePtr;
std::shared_ptr<HttpSimpleControllerBase> _controller;
std::shared_ptr<HttpResponse> _responsePtr;
std::mutex _mutex;
};
std::unordered_map<std::string, SimpleControllerRouterItem> _simpCtrlMap;