diff --git a/lib/inc/drogon/HttpViewData.h b/lib/inc/drogon/HttpViewData.h index 7f15ab13..626e1b67 100755 --- a/lib/inc/drogon/HttpViewData.h +++ b/lib/inc/drogon/HttpViewData.h @@ -24,19 +24,19 @@ #include #include -typedef std::unordered_map 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 - 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 ViewDataMap; + mutable ViewDataMap _viewData; }; } // namespace drogon diff --git a/lib/inc/drogon/Session.h b/lib/inc/drogon/Session.h index 7c87cdc3..1ea10dff 100755 --- a/lib/inc/drogon/Session.h +++ b/lib/inc/drogon/Session.h @@ -20,48 +20,47 @@ #include #include -typedef std::map SessionMap; namespace drogon { + class Session { public: template - T get(const std::string &key) const + const T &get(const std::string &key, T &&nullVal = T()) const { - std::lock_guard lck(mutex_); - auto it = sessionMap_.find(key); - if (it != sessionMap_.end()) + std::lock_guard lck(_mutex); + auto it = _sessionMap.find(key); + if (it != _sessionMap.end()) { return *(any_cast(&(it->second))); } - T tmp; - return tmp; + return nullVal; }; any &operator[](const std::string &key) { - std::lock_guard lck(mutex_); - return sessionMap_[key]; + std::lock_guard lck(_mutex); + return _sessionMap[key]; }; void insert(const std::string &key, const any &obj) { - std::lock_guard lck(mutex_); - sessionMap_[key] = obj; + std::lock_guard lck(_mutex); + _sessionMap[key] = obj; }; void insert(const std::string &key, any &&obj) { - std::lock_guard lck(mutex_); - sessionMap_[key] = std::move(obj); + std::lock_guard lck(_mutex); + _sessionMap[key] = std::move(obj); } void erase(const std::string &key) { - std::lock_guard lck(mutex_); - sessionMap_.erase(key); + std::lock_guard lck(_mutex); + _sessionMap.erase(key); } bool find(const std::string &key) { - std::lock_guard lck(mutex_); - if (sessionMap_.find(key) == sessionMap_.end()) + std::lock_guard lck(_mutex); + if (_sessionMap.find(key) == _sessionMap.end()) { return false; } @@ -69,14 +68,16 @@ class Session } void clear() { - std::lock_guard lck(mutex_); - sessionMap_.clear(); + std::lock_guard lck(_mutex); + _sessionMap.clear(); } protected: - SessionMap sessionMap_; - int timeoutInterval_; - mutable std::mutex mutex_; + typedef std::map SessionMap; + SessionMap _sessionMap; + mutable std::mutex _mutex; }; + typedef std::shared_ptr SessionPtr; + } // namespace drogon