Modify the Session class and the HttpViewData class

This commit is contained in:
antao 2019-02-12 15:47:32 +08:00
parent 2d3ff02e03
commit 8829203d04
2 changed files with 34 additions and 32 deletions

View File

@ -24,19 +24,19 @@
#include <stdio.h>
#include <stdarg.h>
typedef std::unordered_map<std::string, any> ViewDataMap;
namespace drogon
{
/// This class represents the data set displayed in views.
class HttpViewData
{
public:
/// The function template is used to get an item in the data set by the @param key.
template <typename T>
const T get(const std::string &key, T &&nullVal = T()) const
const T &get(const std::string &key, T &&nullVal = T()) const
{
auto it = viewData_.find(key);
if (it != viewData_.end())
auto it = _viewData.find(key);
if (it != _viewData.end())
{
try
{
@ -53,11 +53,11 @@ class HttpViewData
/// Insert an item identified by the @param key into the data set;
void insert(const std::string &key, any &&obj)
{
viewData_[key] = std::move(obj);
_viewData[key] = std::move(obj);
}
void insert(const std::string &key, const any &obj)
{
viewData_[key] = obj;
_viewData[key] = obj;
}
/// Insert an item identified by the @param key into the data set; The item will be converted to a string.
@ -66,7 +66,7 @@ class HttpViewData
{
std::stringstream ss;
ss << val;
viewData_[key] = ss.str();
_viewData[key] = ss.str();
}
/// Insert a formated string identified by the @param key.
@ -110,17 +110,18 @@ class HttpViewData
}
}
va_end(ap);
viewData_[key] = std::move(strBuffer);
_viewData[key] = std::move(strBuffer);
}
/// Get the 'any' object by the @param key.
any &operator[](const std::string &key) const
{
return viewData_[key];
return _viewData[key];
}
protected:
mutable ViewDataMap viewData_;
typedef std::unordered_map<std::string, any> ViewDataMap;
mutable ViewDataMap _viewData;
};
} // namespace drogon

View File

@ -20,48 +20,47 @@
#include <thread>
#include <drogon/config.h>
typedef std::map<std::string, any> SessionMap;
namespace drogon
{
class Session
{
public:
template <typename T>
T get(const std::string &key) const
const T &get(const std::string &key, T &&nullVal = T()) const
{
std::lock_guard<std::mutex> lck(mutex_);
auto it = sessionMap_.find(key);
if (it != sessionMap_.end())
std::lock_guard<std::mutex> lck(_mutex);
auto it = _sessionMap.find(key);
if (it != _sessionMap.end())
{
return *(any_cast<T>(&(it->second)));
}
T tmp;
return tmp;
return nullVal;
};
any &operator[](const std::string &key)
{
std::lock_guard<std::mutex> lck(mutex_);
return sessionMap_[key];
std::lock_guard<std::mutex> lck(_mutex);
return _sessionMap[key];
};
void insert(const std::string &key, const any &obj)
{
std::lock_guard<std::mutex> lck(mutex_);
sessionMap_[key] = obj;
std::lock_guard<std::mutex> lck(_mutex);
_sessionMap[key] = obj;
};
void insert(const std::string &key, any &&obj)
{
std::lock_guard<std::mutex> lck(mutex_);
sessionMap_[key] = std::move(obj);
std::lock_guard<std::mutex> lck(_mutex);
_sessionMap[key] = std::move(obj);
}
void erase(const std::string &key)
{
std::lock_guard<std::mutex> lck(mutex_);
sessionMap_.erase(key);
std::lock_guard<std::mutex> lck(_mutex);
_sessionMap.erase(key);
}
bool find(const std::string &key)
{
std::lock_guard<std::mutex> lck(mutex_);
if (sessionMap_.find(key) == sessionMap_.end())
std::lock_guard<std::mutex> lck(_mutex);
if (_sessionMap.find(key) == _sessionMap.end())
{
return false;
}
@ -69,14 +68,16 @@ class Session
}
void clear()
{
std::lock_guard<std::mutex> lck(mutex_);
sessionMap_.clear();
std::lock_guard<std::mutex> lck(_mutex);
_sessionMap.clear();
}
protected:
SessionMap sessionMap_;
int timeoutInterval_;
mutable std::mutex mutex_;
typedef std::map<std::string, any> SessionMap;
SessionMap _sessionMap;
mutable std::mutex _mutex;
};
typedef std::shared_ptr<Session> SessionPtr;
} // namespace drogon